testautomation_math.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /**
  2. * Math test suite
  3. */
  4. #include <math.h>
  5. #include "SDL.h"
  6. #include "SDL_test.h"
  7. /* ================= Test Structs ================== */
  8. /**
  9. * Stores a single input and the expected result
  10. */
  11. typedef struct
  12. {
  13. double input;
  14. double expected;
  15. } d_to_d;
  16. /**
  17. * Stores a pair of inputs and the expected result
  18. */
  19. typedef struct
  20. {
  21. double x_input, y_input;
  22. double expected;
  23. } dd_to_d;
  24. /* ================= Test Case Implementation ================== */
  25. /* SDL_floor tests functions */
  26. /**
  27. * \brief Checks edge cases (0 and infinity) for themselves.
  28. */
  29. static int
  30. floor_edgeCases(void *args)
  31. {
  32. double result;
  33. result = SDL_floor(INFINITY);
  34. SDLTest_AssertCheck(INFINITY == result, "Floor(%f), expected %f, got %f",
  35. INFINITY, INFINITY, result);
  36. result = SDL_floor(-INFINITY);
  37. SDLTest_AssertCheck(-INFINITY == result, "Floor(%f), expected %f, got %f",
  38. -INFINITY, -INFINITY, result);
  39. result = SDL_floor(0.0);
  40. SDLTest_AssertCheck(0.0 == result, "Floor(%.1f), expected %.1f, got %.1f",
  41. 0.0, 0.0, result);
  42. result = SDL_floor(-0.0);
  43. SDLTest_AssertCheck(-0.0 == result, "Floor(%.1f), expected %.1f, got %.1f",
  44. -0.0, -0.0, result);
  45. return TEST_COMPLETED;
  46. }
  47. /**
  48. * \brief Checks the NaN case.
  49. */
  50. static int
  51. floor_nanCase(void *args)
  52. {
  53. SDLTest_AssertCheck(isnan(SDL_floor(NAN)), "Floor(nan), expected nan");
  54. return TEST_COMPLETED;
  55. }
  56. /**
  57. * \brief Checks round values (x.0) for themselves
  58. */
  59. static int
  60. floor_roundNumbersCases(void *args)
  61. {
  62. Uint32 i;
  63. const double round_cases[] = {
  64. 1.0,
  65. -1.0,
  66. 15.0,
  67. -15.0,
  68. 125.0,
  69. -125.0,
  70. 1024.0,
  71. -1024.0
  72. };
  73. for (i = 0; i < SDL_arraysize(round_cases); i++) {
  74. const double result = SDL_floor(round_cases[i]);
  75. SDLTest_AssertCheck(result == round_cases[i],
  76. "Floor(%.1f), expected %.1f, got %.1f", round_cases[i],
  77. round_cases[i], result);
  78. }
  79. return TEST_COMPLETED;
  80. }
  81. /**
  82. * \brief Checks a set of fractions
  83. */
  84. static int
  85. floor_fractionCases(void *args)
  86. {
  87. Uint32 i;
  88. const d_to_d frac_cases[] = {
  89. { 1.0 / 2.0, 0.0 },
  90. { -1.0 / 2.0, -1.0 },
  91. { 4.0 / 3.0, 1.0 },
  92. { -4.0 / 3.0, -2.0 },
  93. { 76.0 / 7.0, 10.0 },
  94. { -76.0 / 7.0, -11.0 },
  95. { 535.0 / 8.0, 66.0 },
  96. { -535.0 / 8.0, -67.0 },
  97. { 19357.0 / 53.0, 365.0 },
  98. { -19357.0 / 53.0, -366.0 }
  99. };
  100. for (i = 0; i < SDL_arraysize(frac_cases); i++) {
  101. const double result = SDL_floor(frac_cases[i].input);
  102. SDLTest_AssertCheck(result == frac_cases[i].expected,
  103. "Floor(%f), expected %.1f, got %f", frac_cases[i].input,
  104. frac_cases[i].expected, result);
  105. }
  106. return TEST_COMPLETED;
  107. }
  108. /**
  109. * \brief Checks a range of values between 0 and UINT32_MAX
  110. */
  111. static int
  112. floor_rangeTest(void *args)
  113. {
  114. const Uint32 ITERATIONS = 10000000;
  115. const Uint32 STEP = SDL_MAX_UINT32 / ITERATIONS;
  116. Uint32 i;
  117. double test_value = 0.0;
  118. SDLTest_AssertPass("Floor: Testing a range of %u values with %u steps",
  119. ITERATIONS, STEP);
  120. for (i = 0; i < ITERATIONS; i++, test_value += STEP) {
  121. double result;
  122. /* These are tested elsewhere */
  123. if (isnan(test_value) || isinf(test_value)) {
  124. continue;
  125. }
  126. result = SDL_floor(test_value);
  127. if (result != test_value) { /* Only log failures to save performances */
  128. SDLTest_AssertPass("Floor(%.1f), expected %.1f, got %.1f", test_value,
  129. test_value, result);
  130. return TEST_ABORTED;
  131. }
  132. }
  133. return TEST_COMPLETED;
  134. }
  135. /* SDL_ceil tests functions */
  136. /**
  137. * \brief Checks edge cases (0 and infinity) for themselves.
  138. */
  139. static int
  140. ceil_edgeCases(void *args)
  141. {
  142. double result;
  143. result = SDL_ceil(INFINITY);
  144. SDLTest_AssertCheck(INFINITY == result, "Ceil(%f), expected %f, got %f",
  145. INFINITY, INFINITY, result);
  146. result = SDL_ceil(-INFINITY);
  147. SDLTest_AssertCheck(-INFINITY == result, "Ceil(%f), expected %f, got %f",
  148. -INFINITY, -INFINITY, result);
  149. result = SDL_ceil(0.0);
  150. SDLTest_AssertCheck(0.0 == result, "Ceil(%.1f), expected %.1f, got %.1f",
  151. 0.0, 0.0, result);
  152. result = SDL_ceil(-0.0);
  153. SDLTest_AssertCheck(-0.0 == result, "Ceil(%.1f), expected %.1f, got %.1f",
  154. -0.0, -0.0, result);
  155. return TEST_COMPLETED;
  156. }
  157. /**
  158. * \brief Checks the NaN case.
  159. */
  160. static int
  161. ceil_nanCase(void *args)
  162. {
  163. SDLTest_AssertCheck(isnan(SDL_ceil(NAN)), "Ceil(nan), expected nan");
  164. return TEST_COMPLETED;
  165. }
  166. /**
  167. * \brief Checks round values (x.0) for themselves
  168. */
  169. static int
  170. ceil_roundNumbersCases(void *args)
  171. {
  172. Uint32 i;
  173. const double round_cases[] = {
  174. 1.0,
  175. -1.0,
  176. 15.0,
  177. -15.0,
  178. 125.0,
  179. -125.0,
  180. 1024.0,
  181. -1024.0
  182. };
  183. for (i = 0; i < SDL_arraysize(round_cases); i++) {
  184. const double result = SDL_ceil(round_cases[i]);
  185. SDLTest_AssertCheck(result == round_cases[i],
  186. "Ceil(%.1f), expected %.1f, got %.1f", round_cases[i],
  187. round_cases[i], result);
  188. }
  189. return TEST_COMPLETED;
  190. }
  191. /**
  192. * \brief Checks a set of fractions
  193. */
  194. static int
  195. ceil_fractionCases(void *args)
  196. {
  197. Uint32 i;
  198. const d_to_d frac_cases[] = {
  199. { 1.0 / 2.0, 1.0 },
  200. { -1.0 / 2.0, -0.0 },
  201. { 4.0 / 3.0, 2.0 },
  202. { -4.0 / 3.0, -1.0 },
  203. { 76.0 / 7.0, 11.0 },
  204. { -76.0 / 7.0, -10.0 },
  205. { 535.0 / 8.0, 67.0 },
  206. { -535.0 / 8.0, -66.0 },
  207. { 19357.0 / 53.0, 366.0 },
  208. { -19357.0 / 53.0, -365.0 }
  209. };
  210. for (i = 0; i < SDL_arraysize(frac_cases); i++) {
  211. const double result = SDL_ceil(frac_cases[i].input);
  212. SDLTest_AssertCheck(result == frac_cases[i].expected,
  213. "Ceil(%f), expected %.1f, got %f", frac_cases[i].input,
  214. frac_cases[i].expected, result);
  215. }
  216. return TEST_COMPLETED;
  217. }
  218. /**
  219. * \brief Checks a range of values between 0 and UINT32_MAX
  220. */
  221. static int
  222. ceil_rangeTest(void *args)
  223. {
  224. const Uint32 ITERATIONS = 10000000;
  225. const Uint32 STEP = SDL_MAX_UINT32 / ITERATIONS;
  226. Uint32 i;
  227. double test_value = 0.0;
  228. SDLTest_AssertPass("Ceil: Testing a range of %u values with %u steps",
  229. ITERATIONS, STEP);
  230. for (i = 0; i < ITERATIONS; i++, test_value += STEP) {
  231. double result;
  232. /* These are tested elsewhere */
  233. if (isnan(test_value) || isinf(test_value)) {
  234. continue;
  235. }
  236. result = SDL_ceil(test_value);
  237. if (result != test_value) { /* Only log failures to save performances */
  238. SDLTest_AssertPass("Ceil(%.1f), expected %.1f, got %.1f", test_value,
  239. test_value, result);
  240. return TEST_ABORTED;
  241. }
  242. }
  243. return TEST_COMPLETED;
  244. }
  245. /* SDL_trunc tests functions */
  246. /**
  247. * \brief Checks edge cases (0 and infinity) for themselves.
  248. */
  249. static int
  250. trunc_edgeCases(void *args)
  251. {
  252. double result;
  253. result = SDL_trunc(INFINITY);
  254. SDLTest_AssertCheck(INFINITY == result, "Trunc(%f), expected %f, got %f",
  255. INFINITY, INFINITY, result);
  256. result = SDL_trunc(-INFINITY);
  257. SDLTest_AssertCheck(-INFINITY == result, "Trunc(%f), expected %f, got %f",
  258. -INFINITY, -INFINITY, result);
  259. result = SDL_trunc(0.0);
  260. SDLTest_AssertCheck(0.0 == result, "Trunc(%.1f), expected %.1f, got %.1f",
  261. 0.0, 0.0, result);
  262. result = SDL_trunc(-0.0);
  263. SDLTest_AssertCheck(-0.0 == result, "Trunc(%.1f), expected %.1f, got %.1f",
  264. -0.0, -0.0, result);
  265. return TEST_COMPLETED;
  266. }
  267. /**
  268. * \brief Checks the NaN case.
  269. */
  270. static int
  271. trunc_nanCase(void *args)
  272. {
  273. SDLTest_AssertCheck(isnan(SDL_trunc(NAN)), "Trunc(nan), expected nan");
  274. return TEST_COMPLETED;
  275. }
  276. /**
  277. * \brief Checks round values (x.0) for themselves
  278. */
  279. static int
  280. trunc_roundNumbersCases(void *args)
  281. {
  282. Uint32 i;
  283. const double round_cases[] = {
  284. 1.0,
  285. -1.0,
  286. 15.0,
  287. -15.0,
  288. 125.0,
  289. -125.0,
  290. 1024.0,
  291. -1024.0
  292. };
  293. for (i = 0; i < SDL_arraysize(round_cases); i++) {
  294. const double result = SDL_trunc(round_cases[i]);
  295. SDLTest_AssertCheck(result == round_cases[i],
  296. "Trunc(%.1f), expected %.1f, got %.1f", round_cases[i],
  297. round_cases[i], result);
  298. }
  299. return TEST_COMPLETED;
  300. }
  301. /**
  302. * \brief Checks a set of fractions
  303. */
  304. static int
  305. trunc_fractionCases(void *args)
  306. {
  307. Uint32 i;
  308. const d_to_d frac_cases[] = {
  309. { 1.0 / 2.0, 0.0 },
  310. { -1.0 / 2.0, -0.0 },
  311. { 4.0 / 3.0, 1.0 },
  312. { -4.0 / 3.0, -1.0 },
  313. { 76.0 / 7.0, 10.0 },
  314. { -76.0 / 7.0, -10.0 },
  315. { 535.0 / 8.0, 66.0 },
  316. { -535.0 / 8.0, -66.0 },
  317. { 19357.0 / 53.0, 365.0 },
  318. { -19357.0 / 53.0, -365.0 }
  319. };
  320. for (i = 0; i < SDL_arraysize(frac_cases); i++) {
  321. const double result = SDL_trunc(frac_cases[i].input);
  322. SDLTest_AssertCheck(result == frac_cases[i].expected,
  323. "Trunc(%f), expected %.1f, got %f", frac_cases[i].input,
  324. frac_cases[i].expected, result);
  325. }
  326. return TEST_COMPLETED;
  327. }
  328. /**
  329. * \brief Checks a range of values between 0 and UINT32_MAX
  330. */
  331. static int
  332. trunc_rangeTest(void *args)
  333. {
  334. const Uint32 ITERATIONS = 10000000;
  335. const Uint32 STEP = SDL_MAX_UINT32 / ITERATIONS;
  336. Uint32 i;
  337. double test_value = 0.0;
  338. SDLTest_AssertPass("Trunc: Testing a range of %u values with %u steps",
  339. ITERATIONS, STEP);
  340. for (i = 0; i < ITERATIONS; i++, test_value += STEP) {
  341. double result;
  342. /* These are tested elsewhere */
  343. if (isnan(test_value) || isinf(test_value)) {
  344. continue;
  345. }
  346. result = SDL_trunc(test_value);
  347. if (result != test_value) { /* Only log failures to save performances */
  348. SDLTest_AssertPass("Trunc(%.1f), expected %.1f, got %.1f", test_value,
  349. test_value, result);
  350. return TEST_ABORTED;
  351. }
  352. }
  353. return TEST_COMPLETED;
  354. }
  355. /* ================= Test References ================== */
  356. /* SDL_floor test cases */
  357. static const SDLTest_TestCaseReference floorTest1 = {
  358. (SDLTest_TestCaseFp) floor_edgeCases, "floor_edgeCases",
  359. "Check positive and negative infinity and 0", TEST_ENABLED
  360. };
  361. static const SDLTest_TestCaseReference floorTest2 = {
  362. (SDLTest_TestCaseFp) floor_nanCase, "floor_nanCase",
  363. "Check the NaN special case", TEST_ENABLED
  364. };
  365. static const SDLTest_TestCaseReference floorTest3 = {
  366. (SDLTest_TestCaseFp) floor_roundNumbersCases, "floor_roundNumberCases",
  367. "Check a set of round numbers", TEST_ENABLED
  368. };
  369. static const SDLTest_TestCaseReference floorTest4 = {
  370. (SDLTest_TestCaseFp) floor_fractionCases, "floor_fractionCases",
  371. "Check a set of fractions", TEST_ENABLED
  372. };
  373. static const SDLTest_TestCaseReference floorTest5 = {
  374. (SDLTest_TestCaseFp) floor_rangeTest, "floor_rangeTest",
  375. "Check a range of positive integer", TEST_ENABLED
  376. };
  377. /* SDL_ceil test cases */
  378. static const SDLTest_TestCaseReference ceilTest1 = {
  379. (SDLTest_TestCaseFp) ceil_edgeCases, "ceil_edgeCases",
  380. "Check positive and negative infinity and 0", TEST_ENABLED
  381. };
  382. static const SDLTest_TestCaseReference ceilTest2 = {
  383. (SDLTest_TestCaseFp) ceil_nanCase, "ceil_nanCase",
  384. "Check the NaN special case", TEST_ENABLED
  385. };
  386. static const SDLTest_TestCaseReference ceilTest3 = {
  387. (SDLTest_TestCaseFp) ceil_roundNumbersCases, "ceil_roundNumberCases",
  388. "Check a set of round numbers", TEST_ENABLED
  389. };
  390. static const SDLTest_TestCaseReference ceilTest4 = {
  391. (SDLTest_TestCaseFp) ceil_fractionCases, "ceil_fractionCases",
  392. "Check a set of fractions", TEST_ENABLED
  393. };
  394. static const SDLTest_TestCaseReference ceilTest5 = {
  395. (SDLTest_TestCaseFp) ceil_rangeTest, "ceil_rangeTest",
  396. "Check a range of positive integer", TEST_ENABLED
  397. };
  398. /* SDL_trunc test cases */
  399. static const SDLTest_TestCaseReference truncTest1 = {
  400. (SDLTest_TestCaseFp) trunc_edgeCases, "trunc_edgeCases",
  401. "Check positive and negative infinity and 0", TEST_ENABLED
  402. };
  403. static const SDLTest_TestCaseReference truncTest2 = {
  404. (SDLTest_TestCaseFp) trunc_nanCase, "trunc_nanCase",
  405. "Check the NaN special case", TEST_ENABLED
  406. };
  407. static const SDLTest_TestCaseReference truncTest3 = {
  408. (SDLTest_TestCaseFp) trunc_roundNumbersCases, "trunc_roundNumberCases",
  409. "Check a set of round numbers", TEST_ENABLED
  410. };
  411. static const SDLTest_TestCaseReference truncTest4 = {
  412. (SDLTest_TestCaseFp) trunc_fractionCases, "trunc_fractionCases",
  413. "Check a set of fractions", TEST_ENABLED
  414. };
  415. static const SDLTest_TestCaseReference truncTest5 = {
  416. (SDLTest_TestCaseFp) trunc_rangeTest, "trunc_rangeTest",
  417. "Check a range of positive integer", TEST_ENABLED
  418. };
  419. static const SDLTest_TestCaseReference *mathTests[] = {
  420. &floorTest1, &floorTest2, &floorTest3, &floorTest4, &floorTest5,
  421. &ceilTest1, &ceilTest2, &ceilTest3, &ceilTest4, &ceilTest5,
  422. &truncTest1, &truncTest2, &truncTest3, &truncTest4, &truncTest5,
  423. NULL
  424. };
  425. SDLTest_TestSuiteReference mathTestSuite = { "Math", NULL, mathTests, NULL };