testautomation_math.c 4.1 KB

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