testautomation_math.c 33 KB

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