testautomation_math.c 30 KB

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