testautomation_math.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Math test suite
  3. */
  4. #include <math.h>
  5. #include "SDL.h"
  6. #include "SDL_test.h"
  7. /* ================= Test Case Implementation ================== */
  8. /* SDL_floor tests functions */
  9. /**
  10. * \brief Checks edge cases (0 and infinity) for themselves.
  11. */
  12. static int floor_edgeCases(void* args) {
  13. static const double edge_cases[] = {INFINITY, -INFINITY, 0.0, -0.0};
  14. for (size_t i = 0; i < SDL_arraysize(edge_cases); i++) {
  15. const double result = SDL_floor(edge_cases[i]);
  16. SDLTest_AssertCheck(result == edge_cases[i],
  17. "Floor(%.1f), expected %.1f, got %.1f", edge_cases[i],
  18. edge_cases[i], result);
  19. }
  20. return TEST_COMPLETED;
  21. }
  22. /**
  23. * \brief Checks the NaN case.
  24. */
  25. static int floor_nanCase(void* args) {
  26. SDLTest_AssertCheck(isnan(SDL_floor(NAN)), "Floor(nan), expected nan");
  27. return TEST_COMPLETED;
  28. }
  29. /**
  30. * \brief Checks round values (x.0) for themselves
  31. */
  32. static int floor_roundNumbersCases(void* args) {
  33. static const double round_cases[] = {1.0, -1.0, 15.0, -15.0,
  34. 125.0, -125.0, 1024.0, -1024.0};
  35. for (size_t i = 0; i < SDL_arraysize(round_cases); i++) {
  36. const double result = SDL_floor(round_cases[i]);
  37. SDLTest_AssertCheck(result == round_cases[i],
  38. "Floor(%.1f), expected %.1f, got %.1f", round_cases[i],
  39. round_cases[i], result);
  40. }
  41. return TEST_COMPLETED;
  42. }
  43. /**
  44. * \brief Checks a set of fractions
  45. */
  46. static int floor_fractionCases(void* args) {
  47. static const struct {
  48. double input, expected;
  49. } frac_cases[] = {{1.0 / 2.0, 0.0}, {-1.0 / 2.0, -1.0},
  50. {4.0 / 3.0, 1.0}, {-4.0 / 3.0, -2.0},
  51. {76.0 / 7.0, 10.0}, {-76.0 / 7.0, -11.0},
  52. {535.0 / 8.0, 66.0}, {-535.0 / 8.0, -67.0},
  53. {19357.0 / 53.0, 365.0}, {-19357.0 / 53.0, -366.0}};
  54. for (size_t i = 0; i < SDL_arraysize(frac_cases); i++) {
  55. const double result = SDL_floor(frac_cases[i].input);
  56. SDLTest_AssertCheck(result == frac_cases[i].expected,
  57. "Floor(%f), expected %.1f, got %f", frac_cases[i].input,
  58. frac_cases[i].expected, result);
  59. }
  60. return TEST_COMPLETED;
  61. }
  62. /**
  63. * \brief Checks a range of values between 0 and UINT32_MAX
  64. */
  65. static int floor_rangeTest(void* args) {
  66. static const Uint32 ITERATIONS = 10000000;
  67. static const Uint32 STEP = UINT32_MAX / ITERATIONS;
  68. double test_value = 0.0;
  69. SDLTest_AssertPass("Floor: Testing a range of %u values with %u steps",
  70. ITERATIONS, STEP);
  71. for (Uint32 i = 0; i < ITERATIONS; i++, test_value += STEP) {
  72. double result;
  73. /* These are tested elsewhere */
  74. if (isnan(test_value) || isinf(test_value)) {
  75. continue;
  76. }
  77. result = SDL_floor(test_value);
  78. if (result != test_value) { /* Only log failures to save performances */
  79. SDLTest_AssertPass("Floor(%.1f), expected %.1f, got %.1f", test_value,
  80. test_value, result);
  81. return TEST_ABORTED;
  82. }
  83. }
  84. return TEST_COMPLETED;
  85. }
  86. /* ================= Test References ================== */
  87. /* SDL_floor test cases */
  88. static const SDLTest_TestCaseReference floorTest1 = {
  89. (SDLTest_TestCaseFp)floor_edgeCases, "floor_edgeCases",
  90. "Check positive and negative infinity and 0", TEST_ENABLED};
  91. static const SDLTest_TestCaseReference floorTest2 = {
  92. (SDLTest_TestCaseFp)floor_nanCase, "floor_nanCase",
  93. "Check the NaN special case", TEST_ENABLED};
  94. static const SDLTest_TestCaseReference floorTest3 = {
  95. (SDLTest_TestCaseFp)floor_roundNumbersCases, "floor_roundNumberCases",
  96. "Check a set of round numbers", TEST_ENABLED};
  97. static const SDLTest_TestCaseReference floorTest4 = {
  98. (SDLTest_TestCaseFp)floor_fractionCases, "floor_fractionCases",
  99. "Check a set of fractions", TEST_ENABLED};
  100. static const SDLTest_TestCaseReference floorTest5 = {
  101. (SDLTest_TestCaseFp)floor_rangeTest, "floor_rangeTest",
  102. "Check a range of positive integer", TEST_ENABLED};
  103. static const SDLTest_TestCaseReference* mathTests[] = {
  104. &floorTest1, &floorTest2, &floorTest3, &floorTest4, &floorTest5, NULL};
  105. SDLTest_TestSuiteReference mathTestSuite = {"Math", NULL, mathTests, NULL};