testautomation_math.c 4.4 KB

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