testautomation_math.c 27 KB

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