testautomation_math.c 4.4 KB

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