testautomation_math.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /**
  2. * Math test suite
  3. */
  4. #include <math.h>
  5. #include "SDL.h"
  6. #include "SDL_test.h"
  7. /* Range tests parameters */
  8. #define RANGE_TEST_ITERATIONS 10000000
  9. #define RANGE_TEST_STEP SDL_MAX_UINT32 / RANGE_TEST_ITERATIONS
  10. /* ================= Test Structs ================== */
  11. /**
  12. * Stores a single input and the expected result
  13. */
  14. typedef struct
  15. {
  16. double input;
  17. double expected;
  18. } d_to_d;
  19. /**
  20. * Stores a pair of inputs and the expected result
  21. */
  22. typedef struct
  23. {
  24. double x_input, y_input;
  25. double expected;
  26. } dd_to_d;
  27. /*
  28. NB: You cannot create an array of these structures containing INFINITY or NAN.
  29. On platforms such as OS/2, they are defined as 'extern const double' making them
  30. not compile-time constant.
  31. */
  32. /* ================= Test Helpers ================== */
  33. typedef double(SDLCALL *d_to_d_func)(double);
  34. typedef double(SDLCALL *dd_to_d_func)(double, double);
  35. /**
  36. * \brief Runs all the cases on a given function with a signature double -> double
  37. *
  38. * \param func_name, the name of the tested function.
  39. * \param func, the function to call.
  40. * \param cases, an array of all the cases.
  41. * \param cases_size, the size of the cases array.
  42. */
  43. static int
  44. helper_dtod(const char *func_name, d_to_d_func func,
  45. const d_to_d *cases, const size_t cases_size)
  46. {
  47. Uint32 i;
  48. for (i = 0; i < cases_size; i++) {
  49. const double result = func(cases[i].input);
  50. SDLTest_AssertCheck(result == cases[i].expected,
  51. "%s(%f), expected %f, got %f",
  52. func_name,
  53. cases[i].input,
  54. cases[i].expected, result);
  55. }
  56. return TEST_COMPLETED;
  57. }
  58. /**
  59. * \brief Runs all the cases on a given function with a signature (double, double) -> double
  60. *
  61. * \param func_name, the name of the tested function.
  62. * \param func, the function to call.
  63. * \param cases, an array of all the cases.
  64. * \param cases_size, the size of the cases array.
  65. */
  66. static int
  67. helper_ddtod(const char *func_name, dd_to_d_func func,
  68. const dd_to_d *cases, const size_t cases_size)
  69. {
  70. Uint32 i;
  71. for (i = 0; i < cases_size; i++) {
  72. const double result = func(cases[i].x_input, cases[i].y_input);
  73. SDLTest_AssertCheck(result == cases[i].expected,
  74. "%s(%f,%f), expected %f, got %f",
  75. func_name,
  76. cases[i].x_input, cases[i].y_input,
  77. cases[i].expected, result);
  78. }
  79. return TEST_COMPLETED;
  80. }
  81. /**
  82. * \brief Runs a range of values on a given function with a signature double -> double
  83. *
  84. * This function is only meant to test functions that returns the input value if it is
  85. * integral: f(x) -> x for x in N.
  86. *
  87. * \param func_name, the name of the tested function.
  88. * \param func, the function to call.
  89. */
  90. static int
  91. helper_range(const char *func_name, d_to_d_func func)
  92. {
  93. Uint32 i;
  94. double test_value = 0.0;
  95. SDLTest_AssertPass("%s: Testing a range of %u values with steps of %u",
  96. func_name,
  97. RANGE_TEST_ITERATIONS,
  98. RANGE_TEST_STEP);
  99. for (i = 0; i < RANGE_TEST_ITERATIONS; i++, test_value += RANGE_TEST_STEP) {
  100. double result;
  101. /* These are tested elsewhere */
  102. if (isnan(test_value) || isinf(test_value)) {
  103. continue;
  104. }
  105. result = func(test_value);
  106. if (result != test_value) { /* Only log failures to save performances */
  107. SDLTest_AssertPass("%s(%.1f), expected %.1f, got %.1f",
  108. func_name, test_value,
  109. test_value, result);
  110. return TEST_ABORTED;
  111. }
  112. }
  113. return TEST_COMPLETED;
  114. }
  115. /* ================= Test Case Implementation ================== */
  116. /* SDL_floor tests functions */
  117. /**
  118. * \brief Checks positive and negative infinity.
  119. */
  120. static int
  121. floor_infCases(void *args)
  122. {
  123. double result;
  124. result = SDL_floor(INFINITY);
  125. SDLTest_AssertCheck(INFINITY == result,
  126. "Floor(%f), expected %f, got %f",
  127. INFINITY, INFINITY, result);
  128. result = SDL_floor(-INFINITY);
  129. SDLTest_AssertCheck(-INFINITY == result,
  130. "Floor(%f), expected %f, got %f",
  131. -INFINITY, -INFINITY, result);
  132. return TEST_COMPLETED;
  133. }
  134. /**
  135. * \brief Checks positive and negative zero.
  136. */
  137. static int
  138. floor_zeroCases(void *args)
  139. {
  140. const d_to_d zero_cases[] = { { 0.0, 0.0 }, { -0.0, -0.0 } };
  141. return helper_dtod("Floor", SDL_floor, zero_cases, SDL_arraysize(zero_cases));
  142. }
  143. /**
  144. * \brief Checks the NaN case.
  145. */
  146. static int
  147. floor_nanCase(void *args)
  148. {
  149. const double result = SDL_floor(NAN);
  150. SDLTest_AssertCheck(isnan(result),
  151. "Floor(nan), expected nan, got %f",
  152. result);
  153. return TEST_COMPLETED;
  154. }
  155. /**
  156. * \brief Checks round values (x.0) for themselves
  157. */
  158. static int
  159. floor_roundNumbersCases(void *args)
  160. {
  161. const d_to_d round_cases[] = {
  162. { 1.0, 1.0 },
  163. { -1.0, -1.0 },
  164. { 15.0, 15.0 },
  165. { -15.0, -15.0 },
  166. { 125.0, 125.0 },
  167. { -125.0, -125.0 },
  168. { 1024.0, 1024.0 },
  169. { -1024.0, -1024.0 }
  170. };
  171. return helper_dtod("Floor", SDL_floor, round_cases, SDL_arraysize(round_cases));
  172. }
  173. /**
  174. * \brief Checks a set of fractions
  175. */
  176. static int
  177. floor_fractionCases(void *args)
  178. {
  179. const d_to_d frac_cases[] = {
  180. { 1.0 / 2.0, 0.0 },
  181. { -1.0 / 2.0, -1.0 },
  182. { 4.0 / 3.0, 1.0 },
  183. { -4.0 / 3.0, -2.0 },
  184. { 76.0 / 7.0, 10.0 },
  185. { -76.0 / 7.0, -11.0 },
  186. { 535.0 / 8.0, 66.0 },
  187. { -535.0 / 8.0, -67.0 },
  188. { 19357.0 / 53.0, 365.0 },
  189. { -19357.0 / 53.0, -366.0 }
  190. };
  191. return helper_dtod("Floor", SDL_floor, frac_cases, SDL_arraysize(frac_cases));
  192. }
  193. /**
  194. * \brief Checks a range of values between 0 and UINT32_MAX
  195. */
  196. static int
  197. floor_rangeTest(void *args)
  198. {
  199. return helper_range("Floor", SDL_floor);
  200. }
  201. /* SDL_ceil tests functions */
  202. /**
  203. * \brief Checks positive and negative infinity.
  204. */
  205. static int
  206. ceil_infCases(void *args)
  207. {
  208. double result;
  209. result = SDL_ceil(INFINITY);
  210. SDLTest_AssertCheck(INFINITY == result,
  211. "Ceil(%f), expected %f, got %f",
  212. INFINITY, INFINITY, result);
  213. result = SDL_ceil(-INFINITY);
  214. SDLTest_AssertCheck(-INFINITY == result,
  215. "Ceil(%f), expected %f, got %f",
  216. -INFINITY, -INFINITY, result);
  217. return TEST_COMPLETED;
  218. }
  219. /**
  220. * \brief Checks positive and negative zero.
  221. */
  222. static int
  223. ceil_zeroCases(void *args)
  224. {
  225. const d_to_d zero_cases[] = { { 0.0, 0.0 }, { -0.0, -0.0 } };
  226. return helper_dtod("Ceil", SDL_ceil, zero_cases, SDL_arraysize(zero_cases));
  227. }
  228. /**
  229. * \brief Checks the NaN case.
  230. */
  231. static int
  232. ceil_nanCase(void *args)
  233. {
  234. const double result = SDL_ceil(NAN);
  235. SDLTest_AssertCheck(isnan(result),
  236. "Ceil(nan), expected nan, got %f",
  237. result);
  238. return TEST_COMPLETED;
  239. }
  240. /**
  241. * \brief Checks round values (x.0) for themselves
  242. */
  243. static int
  244. ceil_roundNumbersCases(void *args)
  245. {
  246. const d_to_d round_cases[] = {
  247. { 1.0, 1.0 },
  248. { -1.0, -1.0 },
  249. { 15.0, 15.0 },
  250. { -15.0, -15.0 },
  251. { 125.0, 125.0 },
  252. { -125.0, -125.0 },
  253. { 1024.0, 1024.0 },
  254. { -1024.0, -1024.0 }
  255. };
  256. return helper_dtod("Ceil", SDL_ceil, round_cases, SDL_arraysize(round_cases));
  257. }
  258. /**
  259. * \brief Checks a set of fractions
  260. */
  261. static int
  262. ceil_fractionCases(void *args)
  263. {
  264. const d_to_d frac_cases[] = {
  265. { 1.0 / 2.0, 1.0 },
  266. { -1.0 / 2.0, -0.0 },
  267. { 4.0 / 3.0, 2.0 },
  268. { -4.0 / 3.0, -1.0 },
  269. { 76.0 / 7.0, 11.0 },
  270. { -76.0 / 7.0, -10.0 },
  271. { 535.0 / 8.0, 67.0 },
  272. { -535.0 / 8.0, -66.0 },
  273. { 19357.0 / 53.0, 366.0 },
  274. { -19357.0 / 53.0, -365.0 }
  275. };
  276. return helper_dtod("Ceil", SDL_ceil, frac_cases, SDL_arraysize(frac_cases));
  277. }
  278. /**
  279. * \brief Checks a range of values between 0 and UINT32_MAX
  280. */
  281. static int
  282. ceil_rangeTest(void *args)
  283. {
  284. return helper_range("Ceil", SDL_ceil);
  285. }
  286. /* SDL_trunc tests functions */
  287. /**
  288. * \brief Checks positive and negative infinity.
  289. */
  290. static int
  291. trunc_infCases(void *args)
  292. {
  293. double result;
  294. result = SDL_trunc(INFINITY);
  295. SDLTest_AssertCheck(INFINITY == result,
  296. "Trunc(%f), expected %f, got %f",
  297. INFINITY, INFINITY, result);
  298. result = SDL_trunc(-INFINITY);
  299. SDLTest_AssertCheck(-INFINITY == result,
  300. "Trunc(%f), expected %f, got %f",
  301. -INFINITY, -INFINITY, result);
  302. return TEST_COMPLETED;
  303. }
  304. /**
  305. * \brief Checks positive and negative zero.
  306. */
  307. static int
  308. trunc_zeroCases(void *args)
  309. {
  310. const d_to_d zero_cases[] = { { 0.0, 0.0 }, { -0.0, -0.0 } };
  311. return helper_dtod("Trunc", SDL_trunc, zero_cases, SDL_arraysize(zero_cases));
  312. }
  313. /**
  314. * \brief Checks the NaN case.
  315. */
  316. static int
  317. trunc_nanCase(void *args)
  318. {
  319. const double result = SDL_trunc(NAN);
  320. SDLTest_AssertCheck(isnan(result),
  321. "Trunc(nan), expected nan, got %f",
  322. result);
  323. return TEST_COMPLETED;
  324. }
  325. /**
  326. * \brief Checks round values (x.0) for themselves
  327. */
  328. static int
  329. trunc_roundNumbersCases(void *args)
  330. {
  331. const d_to_d round_cases[] = {
  332. { 1.0, 1.0 },
  333. { -1.0, -1.0 },
  334. { 15.0, 15.0 },
  335. { -15.0, -15.0 },
  336. { 125.0, 125.0 },
  337. { -125.0, -125.0 },
  338. { 1024.0, 1024.0 },
  339. { -1024.0, -1024.0 }
  340. };
  341. return helper_dtod("Trunc", SDL_trunc, round_cases, SDL_arraysize(round_cases));
  342. }
  343. /**
  344. * \brief Checks a set of fractions
  345. */
  346. static int
  347. trunc_fractionCases(void *args)
  348. {
  349. const d_to_d frac_cases[] = {
  350. { 1.0 / 2.0, 0.0 },
  351. { -1.0 / 2.0, -0.0 },
  352. { 4.0 / 3.0, 1.0 },
  353. { -4.0 / 3.0, -1.0 },
  354. { 76.0 / 7.0, 10.0 },
  355. { -76.0 / 7.0, -10.0 },
  356. { 535.0 / 8.0, 66.0 },
  357. { -535.0 / 8.0, -66.0 },
  358. { 19357.0 / 53.0, 365.0 },
  359. { -19357.0 / 53.0, -365.0 }
  360. };
  361. return helper_dtod("Trunc", SDL_trunc, frac_cases, SDL_arraysize(frac_cases));
  362. }
  363. /**
  364. * \brief Checks a range of values between 0 and UINT32_MAX
  365. */
  366. static int
  367. trunc_rangeTest(void *args)
  368. {
  369. return helper_range("Trunc", SDL_trunc);
  370. }
  371. /* SDL_round tests functions */
  372. /**
  373. * \brief Checks positive and negative infinity.
  374. */
  375. static int
  376. round_infCases(void *args)
  377. {
  378. double result;
  379. result = SDL_round(INFINITY);
  380. SDLTest_AssertCheck(INFINITY == result,
  381. "Round(%f), expected %f, got %f",
  382. INFINITY, INFINITY, result);
  383. result = SDL_round(-INFINITY);
  384. SDLTest_AssertCheck(-INFINITY == result,
  385. "Round(%f), expected %f, got %f",
  386. -INFINITY, -INFINITY, result);
  387. return TEST_COMPLETED;
  388. }
  389. /**
  390. * \brief Checks positive and negative zero.
  391. */
  392. static int
  393. round_zeroCases(void *args)
  394. {
  395. const d_to_d zero_cases[] = { { 0.0, 0.0 }, { -0.0, -0.0 } };
  396. return helper_dtod("Round", SDL_round, zero_cases, SDL_arraysize(zero_cases));
  397. }
  398. /**
  399. * \brief Checks the NaN case.
  400. */
  401. static int
  402. round_nanCase(void *args)
  403. {
  404. const double result = SDL_round(NAN);
  405. SDLTest_AssertCheck(isnan(result),
  406. "Round(nan), expected nan, got %f",
  407. result);
  408. return TEST_COMPLETED;
  409. }
  410. /**
  411. * \brief Checks round values (x.0) for themselves
  412. */
  413. static int
  414. round_roundNumbersCases(void *args)
  415. {
  416. const d_to_d round_cases[] = {
  417. { 1.0, 1.0 },
  418. { -1.0, -1.0 },
  419. { 15.0, 15.0 },
  420. { -15.0, -15.0 },
  421. { 125.0, 125.0 },
  422. { -125.0, -125.0 },
  423. { 1024.0, 1024.0 },
  424. { -1024.0, -1024.0 }
  425. };
  426. return helper_dtod("Round", SDL_round, round_cases, SDL_arraysize(round_cases));
  427. }
  428. /**
  429. * \brief Checks a set of fractions
  430. */
  431. static int
  432. round_fractionCases(void *args)
  433. {
  434. const d_to_d frac_cases[] = {
  435. { 1.0 / 2.0, 1.0 },
  436. { -1.0 / 2.0, -1.0 },
  437. { 4.0 / 3.0, 1.0 },
  438. { -4.0 / 3.0, -1.0 },
  439. { 76.0 / 7.0, 11.0 },
  440. { -76.0 / 7.0, -11.0 },
  441. { 535.0 / 8.0, 67.0 },
  442. { -535.0 / 8.0, -67.0 },
  443. { 19357.0 / 53.0, 365.0 },
  444. { -19357.0 / 53.0, -365.0 }
  445. };
  446. return helper_dtod("Round", SDL_round, frac_cases, SDL_arraysize(frac_cases));
  447. }
  448. /**
  449. * \brief Checks a range of values between 0 and UINT32_MAX
  450. */
  451. static int
  452. round_rangeTest(void *args)
  453. {
  454. return helper_range("Round", SDL_round);
  455. }
  456. /* SDL_fabs tests functions */
  457. /**
  458. * \brief Checks positive and negative infinity.
  459. */
  460. static int
  461. fabs_infCases(void *args)
  462. {
  463. double result;
  464. result = SDL_fabs(INFINITY);
  465. SDLTest_AssertCheck(INFINITY == result,
  466. "Fabs(%f), expected %f, got %f",
  467. INFINITY, INFINITY, result);
  468. result = SDL_fabs(-INFINITY);
  469. SDLTest_AssertCheck(INFINITY == result,
  470. "Fabs(%f), expected %f, got %f",
  471. -INFINITY, INFINITY, result);
  472. return TEST_COMPLETED;
  473. }
  474. /**
  475. * \brief Checks positive and negative zero
  476. */
  477. static int
  478. fabs_zeroCases(void *args)
  479. {
  480. const d_to_d zero_cases[] = { { 0.0, 0.0 }, { -0.0, 0.0 } };
  481. return helper_dtod("Fabs", SDL_fabs, zero_cases, SDL_arraysize(zero_cases));
  482. }
  483. /**
  484. * \brief Checks the NaN case.
  485. */
  486. static int
  487. fabs_nanCase(void *args)
  488. {
  489. const double result = SDL_fabs(NAN);
  490. SDLTest_AssertCheck(isnan(result),
  491. "Fabs(nan), expected nan, got %f",
  492. result);
  493. return TEST_COMPLETED;
  494. }
  495. /**
  496. * \brief Checks a range of values between 0 and UINT32_MAX
  497. */
  498. static int
  499. fabs_rangeTest(void *args)
  500. {
  501. return helper_range("Fabs", SDL_fabs);
  502. }
  503. /* SDL_copysign tests functions */
  504. /**
  505. * \brief Checks positive and negative inifnity.
  506. */
  507. static int
  508. copysign_infCases(void *args)
  509. {
  510. double result;
  511. result = SDL_copysign(INFINITY, -1.0);
  512. SDLTest_AssertCheck(-INFINITY == result,
  513. "Copysign(%f,%.1f), expected %f, got %f",
  514. INFINITY, -1.0, -INFINITY, result);
  515. result = SDL_copysign(INFINITY, 1.0);
  516. SDLTest_AssertCheck(INFINITY == result,
  517. "Copysign(%f,%.1f), expected %f, got %f",
  518. INFINITY, 1.0, INFINITY, result);
  519. result = SDL_copysign(-INFINITY, -1.0);
  520. SDLTest_AssertCheck(-INFINITY == result,
  521. "Copysign(%f,%.1f), expected %f, got %f",
  522. -INFINITY, -1.0, -INFINITY, result);
  523. result = SDL_copysign(-INFINITY, 1.0);
  524. SDLTest_AssertCheck(INFINITY == result,
  525. "Copysign(%f,%.1f), expected %f, got %f",
  526. -INFINITY, 1.0, INFINITY, result);
  527. return TEST_COMPLETED;
  528. }
  529. /**
  530. * \brief Checks positive and negative zero.
  531. */
  532. static int
  533. copysign_zeroCases(void *args)
  534. {
  535. const dd_to_d zero_cases[] = {
  536. { 0.0, 1.0, 0.0 },
  537. { 0.0, -1.0, -0.0 },
  538. { -0.0, 1.0, 0.0 },
  539. { -0.0, -1.0, -0.0 }
  540. };
  541. return helper_ddtod("Copysign", SDL_copysign, zero_cases, SDL_arraysize(zero_cases));
  542. }
  543. /**
  544. * \brief Checks the NaN cases.
  545. */
  546. static int
  547. copysign_nanCases(void *args)
  548. {
  549. double result;
  550. result = SDL_copysign(NAN, 1.0);
  551. SDLTest_AssertCheck(isnan(result),
  552. "Copysign(nan,1.0), expected nan, got %f",
  553. result);
  554. result = SDL_copysign(NAN, -1.0);
  555. SDLTest_AssertCheck(isnan(result),
  556. "Copysign(nan,-1.0), expected nan, got %f",
  557. result);
  558. return TEST_COMPLETED;
  559. }
  560. /**
  561. * \brief Checks a range of values between 0 and UINT32_MAX
  562. */
  563. static int
  564. copysign_rangeTest(void *args)
  565. {
  566. Uint32 i;
  567. double test_value = 0.0;
  568. SDLTest_AssertPass("Copysign: Testing a range of %u values with steps of %u",
  569. RANGE_TEST_ITERATIONS,
  570. RANGE_TEST_STEP);
  571. for (i = 0; i < RANGE_TEST_ITERATIONS; i++, test_value += RANGE_TEST_STEP) {
  572. double result;
  573. /* These are tested elsewhere */
  574. if (isnan(test_value) || isinf(test_value)) {
  575. continue;
  576. }
  577. /* Only log failures to save performances */
  578. result = SDL_copysign(test_value, 1.0);
  579. if (result != test_value) {
  580. SDLTest_AssertPass("Copysign(%.1f,%.1f), expected %.1f, got %.1f",
  581. test_value, 1.0,
  582. test_value, result);
  583. return TEST_ABORTED;
  584. }
  585. result = SDL_copysign(test_value, -1.0);
  586. if (result != -test_value) {
  587. SDLTest_AssertPass("Copysign(%.1f,%.1f), expected %.1f, got %.1f",
  588. test_value, -1.0,
  589. -test_value, result);
  590. return TEST_ABORTED;
  591. }
  592. }
  593. return TEST_COMPLETED;
  594. }
  595. /* SDL_fmod tests functions */
  596. /**
  597. * \brief Checks division of positive and negative inifnity.
  598. */
  599. static int
  600. fmod_divOfInfCases(void *args)
  601. {
  602. double result;
  603. result = SDL_fmod(INFINITY, -1.0);
  604. SDLTest_AssertCheck(isnan(result),
  605. "Fmod(%f,%.1f), expected %f, got %f",
  606. INFINITY, -1.0, NAN, result);
  607. result = SDL_fmod(INFINITY, 1.0);
  608. SDLTest_AssertCheck(isnan(result),
  609. "Fmod(%f,%.1f), expected %f, got %f",
  610. INFINITY, 1.0, NAN, result);
  611. result = SDL_fmod(-INFINITY, -1.0);
  612. SDLTest_AssertCheck(isnan(result),
  613. "Fmod(%f,%.1f), expected %f, got %f",
  614. -INFINITY, -1.0, NAN, result);
  615. result = SDL_fmod(-INFINITY, 1.0);
  616. SDLTest_AssertCheck(isnan(result),
  617. "Fmod(%f,%.1f), expected %f, got %f",
  618. -INFINITY, 1.0, NAN, result);
  619. return TEST_COMPLETED;
  620. }
  621. /**
  622. * \brief Checks division by positive and negative inifnity.
  623. */
  624. static int
  625. fmod_divByInfCases(void *args)
  626. {
  627. double result;
  628. result = SDL_fmod(1.0, INFINITY);
  629. SDLTest_AssertCheck(1.0 == result,
  630. "Fmod(%.1f,%f), expected %f, got %f",
  631. 1.0, INFINITY, 1.0, result);
  632. result = SDL_fmod(-1.0, INFINITY);
  633. SDLTest_AssertCheck(-1.0 == result,
  634. "Fmod(%.1f,%f), expected %f, got %f",
  635. -1.0, INFINITY, -1.0, result);
  636. result = SDL_fmod(1.0, -INFINITY);
  637. SDLTest_AssertCheck(1.0 == result,
  638. "Fmod(%.1f,%f), expected %f, got %f",
  639. 1.0, -INFINITY, 1.0, result);
  640. result = SDL_fmod(-1.0, -INFINITY);
  641. SDLTest_AssertCheck(-1.0 == result,
  642. "Fmod(%.1f,%f), expected %f, got %f",
  643. -1.0, -INFINITY, -1.0, result);
  644. return TEST_COMPLETED;
  645. }
  646. /**
  647. * \brief Checks division of positive and negative zero.
  648. */
  649. static int
  650. fmod_divOfZeroCases(void *args)
  651. {
  652. const dd_to_d zero_cases[] = {
  653. { 0.0, 1.0, 0.0 },
  654. { 0.0, -1.0, 0.0 },
  655. { -0.0, 1.0, -0.0 },
  656. { -0.0, -1.0, -0.0 }
  657. };
  658. return helper_ddtod("Fmod", SDL_fmod, zero_cases, SDL_arraysize(zero_cases));
  659. }
  660. /**
  661. * \brief Checks division by positive and negative zero.
  662. */
  663. static int
  664. fmod_divByZeroCases(void *args)
  665. {
  666. double result;
  667. result = SDL_fmod(1.0, 0.0);
  668. SDLTest_AssertCheck(isnan(result),
  669. "Fmod(1.0,0.0), expected nan, got %f",
  670. result);
  671. result = SDL_fmod(-1.0, 0.0);
  672. SDLTest_AssertCheck(isnan(result),
  673. "Fmod(-1.0,0.0), expected nan, got %f",
  674. result);
  675. result = SDL_fmod(1.0, -0.0);
  676. SDLTest_AssertCheck(isnan(result),
  677. "Fmod(1.0,-0.0), expected nan, got %f",
  678. result);
  679. result = SDL_fmod(-1.0, -0.0);
  680. SDLTest_AssertCheck(isnan(result),
  681. "Fmod(-1.0,-0.0), expected nan, got %f",
  682. result);
  683. return TEST_COMPLETED;
  684. }
  685. /**
  686. * \brief Checks the NaN cases.
  687. */
  688. static int
  689. fmod_nanCases(void *args)
  690. {
  691. double result;
  692. result = SDL_fmod(NAN, 1.0);
  693. SDLTest_AssertCheck(isnan(result),
  694. "Fmod(nan,1.0), expected nan, got %f",
  695. result);
  696. result = SDL_fmod(NAN, -1.0);
  697. SDLTest_AssertCheck(isnan(result),
  698. "Fmod(nan,-1.0), expected nan, got %f",
  699. result);
  700. result = SDL_fmod(1.0, NAN);
  701. SDLTest_AssertCheck(isnan(result),
  702. "Fmod(1.0,nan), expected nan, got %f",
  703. result);
  704. result = SDL_fmod(-1.0, NAN);
  705. SDLTest_AssertCheck(isnan(result),
  706. "Fmod(-1.0,nan), expected nan, got %f",
  707. result);
  708. return TEST_COMPLETED;
  709. }
  710. /**
  711. * \brief Checks a set of regular values.
  712. */
  713. static int
  714. fmod_regularCases(void *args)
  715. {
  716. const dd_to_d regular_cases[] = {
  717. { 3.5, 2.0, 1.5 },
  718. { -6.25, 3.0, -0.25 },
  719. { 7.5, 2.5, 0.0 },
  720. { 2.0 / 3.0, -1.0 / 3.0, 0.0 }
  721. };
  722. return helper_ddtod("Fmod", SDL_fmod, regular_cases, SDL_arraysize(regular_cases));
  723. }
  724. /**
  725. * \brief Checks a range of values between 0 and UINT32_MAX
  726. */
  727. static int
  728. fmod_rangeTest(void *args)
  729. {
  730. Uint32 i;
  731. double test_value = 0.0;
  732. SDLTest_AssertPass("Fmod: Testing a range of %u values with steps of %u",
  733. RANGE_TEST_ITERATIONS,
  734. RANGE_TEST_STEP);
  735. for (i = 0; i < RANGE_TEST_ITERATIONS; i++, test_value += RANGE_TEST_STEP) {
  736. double result;
  737. /* These are tested elsewhere */
  738. if (isnan(test_value) || isinf(test_value)) {
  739. continue;
  740. }
  741. /* Only log failures to save performances */
  742. result = SDL_fmod(test_value, 1.0);
  743. if (0.0 != result) {
  744. SDLTest_AssertPass("Fmod(%.1f,%.1f), expected %.1f, got %.1f",
  745. test_value, 1.0,
  746. 0.0, result);
  747. return TEST_ABORTED;
  748. }
  749. }
  750. return TEST_COMPLETED;
  751. }
  752. /* ================= Test References ================== */
  753. /* SDL_floor test cases */
  754. static const SDLTest_TestCaseReference floorTestInf = {
  755. (SDLTest_TestCaseFp) floor_infCases, "floor_infCases",
  756. "Check positive and negative infinity", TEST_ENABLED
  757. };
  758. static const SDLTest_TestCaseReference floorTestZero = {
  759. (SDLTest_TestCaseFp) floor_zeroCases, "floor_zeroCases",
  760. "Check positive and negative zero", TEST_ENABLED
  761. };
  762. static const SDLTest_TestCaseReference floorTestNan = {
  763. (SDLTest_TestCaseFp) floor_nanCase, "floor_nanCase",
  764. "Check the NaN special case", TEST_ENABLED
  765. };
  766. static const SDLTest_TestCaseReference floorTestRound = {
  767. (SDLTest_TestCaseFp) floor_roundNumbersCases, "floor_roundNumberCases",
  768. "Check a set of round numbers", TEST_ENABLED
  769. };
  770. static const SDLTest_TestCaseReference floorTestFraction = {
  771. (SDLTest_TestCaseFp) floor_fractionCases, "floor_fractionCases",
  772. "Check a set of fractions", TEST_ENABLED
  773. };
  774. static const SDLTest_TestCaseReference floorTestRange = {
  775. (SDLTest_TestCaseFp) floor_rangeTest, "floor_rangeTest",
  776. "Check a range of positive integer", TEST_ENABLED
  777. };
  778. /* SDL_ceil test cases */
  779. static const SDLTest_TestCaseReference ceilTestInf = {
  780. (SDLTest_TestCaseFp) ceil_infCases, "ceil_infCases",
  781. "Check positive and negative infinity", TEST_ENABLED
  782. };
  783. static const SDLTest_TestCaseReference ceilTestZero = {
  784. (SDLTest_TestCaseFp) ceil_zeroCases, "ceil_zeroCases",
  785. "Check positive and negative zero", TEST_ENABLED
  786. };
  787. static const SDLTest_TestCaseReference ceilTestNan = {
  788. (SDLTest_TestCaseFp) ceil_nanCase, "ceil_nanCase",
  789. "Check the NaN special case", TEST_ENABLED
  790. };
  791. static const SDLTest_TestCaseReference ceilTestRound = {
  792. (SDLTest_TestCaseFp) ceil_roundNumbersCases, "ceil_roundNumberCases",
  793. "Check a set of round numbers", TEST_ENABLED
  794. };
  795. static const SDLTest_TestCaseReference ceilTestFraction = {
  796. (SDLTest_TestCaseFp) ceil_fractionCases, "ceil_fractionCases",
  797. "Check a set of fractions", TEST_ENABLED
  798. };
  799. static const SDLTest_TestCaseReference ceilTestRange = {
  800. (SDLTest_TestCaseFp) ceil_rangeTest, "ceil_rangeTest",
  801. "Check a range of positive integer", TEST_ENABLED
  802. };
  803. /* SDL_trunc test cases */
  804. static const SDLTest_TestCaseReference truncTestInf = {
  805. (SDLTest_TestCaseFp) trunc_infCases, "trunc_infCases",
  806. "Check positive and negative infinity", TEST_ENABLED
  807. };
  808. static const SDLTest_TestCaseReference truncTestZero = {
  809. (SDLTest_TestCaseFp) trunc_zeroCases, "trunc_zeroCases",
  810. "Check positive and negative zero", TEST_ENABLED
  811. };
  812. static const SDLTest_TestCaseReference truncTestNan = {
  813. (SDLTest_TestCaseFp) trunc_nanCase, "trunc_nanCase",
  814. "Check the NaN special case", TEST_ENABLED
  815. };
  816. static const SDLTest_TestCaseReference truncTestRound = {
  817. (SDLTest_TestCaseFp) trunc_roundNumbersCases, "trunc_roundNumberCases",
  818. "Check a set of round numbers", TEST_ENABLED
  819. };
  820. static const SDLTest_TestCaseReference truncTestFraction = {
  821. (SDLTest_TestCaseFp) trunc_fractionCases, "trunc_fractionCases",
  822. "Check a set of fractions", TEST_ENABLED
  823. };
  824. static const SDLTest_TestCaseReference truncTestRange = {
  825. (SDLTest_TestCaseFp) trunc_rangeTest, "trunc_rangeTest",
  826. "Check a range of positive integer", TEST_ENABLED
  827. };
  828. /* SDL_round test cases */
  829. static const SDLTest_TestCaseReference roundTestInf = {
  830. (SDLTest_TestCaseFp) round_infCases, "round_infCases",
  831. "Check positive and negative infinity", TEST_ENABLED
  832. };
  833. static const SDLTest_TestCaseReference roundTestZero = {
  834. (SDLTest_TestCaseFp) round_zeroCases, "round_zeroCases",
  835. "Check positive and negative zero", TEST_ENABLED
  836. };
  837. static const SDLTest_TestCaseReference roundTestNan = {
  838. (SDLTest_TestCaseFp) round_nanCase, "round_nanCase",
  839. "Check the NaN special case", TEST_ENABLED
  840. };
  841. static const SDLTest_TestCaseReference roundTestRound = {
  842. (SDLTest_TestCaseFp) round_roundNumbersCases, "round_roundNumberCases",
  843. "Check a set of round numbers", TEST_ENABLED
  844. };
  845. static const SDLTest_TestCaseReference roundTestFraction = {
  846. (SDLTest_TestCaseFp) round_fractionCases, "round_fractionCases",
  847. "Check a set of fractions", TEST_ENABLED
  848. };
  849. static const SDLTest_TestCaseReference roundTestRange = {
  850. (SDLTest_TestCaseFp) round_rangeTest, "round_rangeTest",
  851. "Check a range of positive integer", TEST_ENABLED
  852. };
  853. /* SDL_fabs test cases */
  854. static const SDLTest_TestCaseReference fabsTestInf = {
  855. (SDLTest_TestCaseFp) fabs_infCases, "fabs_infCases",
  856. "Check positive and negative infinity", TEST_ENABLED
  857. };
  858. static const SDLTest_TestCaseReference fabsTestZero = {
  859. (SDLTest_TestCaseFp) fabs_zeroCases, "fabs_zeroCases",
  860. "Check positive and negative zero", TEST_ENABLED
  861. };
  862. static const SDLTest_TestCaseReference fabsTestNan = {
  863. (SDLTest_TestCaseFp) fabs_nanCase, "fabs_nanCase",
  864. "Check the NaN special case", TEST_ENABLED
  865. };
  866. static const SDLTest_TestCaseReference fabsTestRange = {
  867. (SDLTest_TestCaseFp) fabs_rangeTest, "fabs_rangeTest",
  868. "Check a range of positive integer", TEST_ENABLED
  869. };
  870. /* SDL_copysign test cases */
  871. static const SDLTest_TestCaseReference copysignTestInf = {
  872. (SDLTest_TestCaseFp) copysign_infCases, "copysign_infCases",
  873. "Check positive and negative infinity", TEST_ENABLED
  874. };
  875. static const SDLTest_TestCaseReference copysignTestZero = {
  876. (SDLTest_TestCaseFp) copysign_zeroCases, "copysign_zeroCases",
  877. "Check positive and negative zero", TEST_ENABLED
  878. };
  879. static const SDLTest_TestCaseReference copysignTestNan = {
  880. (SDLTest_TestCaseFp) copysign_nanCases, "copysign_nanCases",
  881. "Check the NaN special cases", TEST_ENABLED
  882. };
  883. static const SDLTest_TestCaseReference copysignTestRange = {
  884. (SDLTest_TestCaseFp) copysign_rangeTest, "copysign_rangeTest",
  885. "Check a range of positive integer", TEST_ENABLED
  886. };
  887. /* SDL_fmod test cases */
  888. static const SDLTest_TestCaseReference fmodTestDivOfInf = {
  889. (SDLTest_TestCaseFp) fmod_divOfInfCases, "fmod_divOfInfCases",
  890. "Check division of positive and negative infinity", TEST_ENABLED
  891. };
  892. static const SDLTest_TestCaseReference fmodTestDivByInf = {
  893. (SDLTest_TestCaseFp) fmod_divByInfCases, "fmod_divByInfCases",
  894. "Check division by positive and negative infinity", TEST_ENABLED
  895. };
  896. static const SDLTest_TestCaseReference fmodTestDivOfZero = {
  897. (SDLTest_TestCaseFp) fmod_divOfZeroCases, "fmod_divOfZeroCases",
  898. "Check division of positive and negative zero", TEST_ENABLED
  899. };
  900. static const SDLTest_TestCaseReference fmodTestDivByZero = {
  901. (SDLTest_TestCaseFp) fmod_divByZeroCases, "fmod_divByZeroCases",
  902. "Check division by positive and negative zero", TEST_ENABLED
  903. };
  904. static const SDLTest_TestCaseReference fmodTestNan = {
  905. (SDLTest_TestCaseFp) fmod_nanCases, "fmod_nanCases",
  906. "Check the NaN special cases", TEST_ENABLED
  907. };
  908. static const SDLTest_TestCaseReference fmodTestRegular = {
  909. (SDLTest_TestCaseFp) fmod_regularCases, "fmod_regularCases",
  910. "Check a set of regular values", TEST_ENABLED
  911. };
  912. static const SDLTest_TestCaseReference fmodTestRange = {
  913. (SDLTest_TestCaseFp) fmod_rangeTest, "fmod_rangeTest",
  914. "Check a range of positive integer", TEST_ENABLED
  915. };
  916. static const SDLTest_TestCaseReference *mathTests[] = {
  917. &floorTestInf, &floorTestZero, &floorTestNan,
  918. &floorTestRound, &floorTestFraction, &floorTestRange,
  919. &ceilTestInf, &ceilTestZero, &ceilTestNan,
  920. &ceilTestRound, &ceilTestFraction, &ceilTestRange,
  921. &truncTestInf, &truncTestZero, &truncTestNan,
  922. &truncTestRound, &truncTestFraction, &truncTestRange,
  923. &roundTestInf, &roundTestZero, &roundTestNan,
  924. &roundTestRound, &roundTestFraction, &roundTestRange,
  925. &fabsTestInf, &fabsTestZero, &fabsTestNan, &fabsTestRange,
  926. &copysignTestInf, &copysignTestZero, &copysignTestNan, &copysignTestRange,
  927. &fmodTestDivOfInf, &fmodTestDivByInf, &fmodTestDivOfZero, &fmodTestDivByZero,
  928. &fmodTestNan, &fmodTestRegular, &fmodTestRange,
  929. NULL
  930. };
  931. SDLTest_TestSuiteReference mathTestSuite = { "Math", NULL, mathTests, NULL };