testqsort.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #ifdef TEST_STDLIB_QSORT
  11. #define _GNU_SOURCE
  12. #include <stdlib.h>
  13. #endif
  14. #include <SDL3/SDL.h>
  15. #include <SDL3/SDL_main.h>
  16. #include <SDL3/SDL_test.h>
  17. typedef struct {
  18. Uint8 major;
  19. Uint8 minor;
  20. Uint8 micro;
  21. } VersionTuple;
  22. static int a_global_var = 77;
  23. static int (SDLCALL * global_compare_cbfn)(const void *_a, const void *_b);
  24. static unsigned long arraylens[16] = { 12, 1024 * 100 };
  25. static unsigned int count_arraylens = 2;
  26. static int SDLCALL
  27. compare_int(const void *_a, const void *_b)
  28. {
  29. const int a = *((const int *)_a);
  30. const int b = *((const int *)_b);
  31. return (a < b) ? -1 : ((a > b) ? 1 : 0);
  32. }
  33. static int SDLCALL
  34. compare_float(const void *_a, const void *_b)
  35. {
  36. const float a = *((const float *)_a);
  37. const float b = *((const float *)_b);
  38. return (a < b) ? -1 : ((a > b) ? 1 : 0);
  39. }
  40. static int SDLCALL
  41. compare_double(const void *_a, const void *_b)
  42. {
  43. const double a = *((const double *)_a);
  44. const double b = *((const double *)_b);
  45. return (a < b) ? -1 : ((a > b) ? 1 : 0);
  46. }
  47. static int SDLCALL
  48. compare_intptr(const void *_a, const void *_b)
  49. {
  50. const int* a = *((const int **)_a);
  51. const int* b = *((const int **)_b);
  52. return compare_int(a, b);
  53. }
  54. static int SDLCALL
  55. compare_version(const void *_a, const void *_b)
  56. {
  57. const VersionTuple *a = ((const VersionTuple *)_a);
  58. const VersionTuple *b = ((const VersionTuple *)_b);
  59. int d;
  60. d = (int)a->major - (int)b->major;
  61. if (d != 0) {
  62. return d;
  63. }
  64. d = (int)a->minor - (int)b->minor;
  65. if (d != 0) {
  66. return d;
  67. }
  68. return (int)a->micro - (int)b->micro;
  69. }
  70. #ifdef TEST_STDLIB_QSORT
  71. #define SDL_qsort qsort
  72. #define SDL_qsort_r qsort_r
  73. #endif
  74. static int SDLCALL
  75. #ifdef TEST_STDLIB_QSORT
  76. generic_compare_r(const void *a, const void *b, void *userdata)
  77. #else
  78. generic_compare_r(void *userdata, const void *a, const void *b)
  79. #endif
  80. {
  81. if (userdata != &a_global_var) {
  82. SDLTest_AssertCheck(userdata == &a_global_var, "User data of callback must be identical to global data");
  83. }
  84. return global_compare_cbfn(a, b);
  85. }
  86. #define STR2(S) "[" #S "] "
  87. #define STR(S) STR2(S)
  88. #define TEST_ARRAY_IS_SORTED(TYPE, ARRAY, SIZE, IS_LE) \
  89. do { \
  90. size_t sorted_index; \
  91. Uint64 count_non_sorted = 0; \
  92. for (sorted_index = 0; sorted_index < (SIZE) - 1; sorted_index++) { \
  93. if (!IS_LE((ARRAY)[sorted_index], (ARRAY)[sorted_index + 1])) { \
  94. count_non_sorted += 1; \
  95. } \
  96. } \
  97. SDLTest_AssertCheck(count_non_sorted == 0, \
  98. STR(TYPE) "Array (size=%d) is sorted (bad count=%" SDL_PRIu64 ")", (SIZE), count_non_sorted); \
  99. } while (0)
  100. /* This test is O(n^2), so very slow (a hashmap can speed this up):
  101. * - we cannot trust qsort
  102. * - the arrays can contain duplicate items
  103. * - the arrays are not int
  104. */
  105. #define TEST_ARRAY_LOST_NO_ELEMENTS(TYPE, ORIGINAL, SORTED, SIZE, IS_SAME) \
  106. do { \
  107. size_t original_index; \
  108. bool *original_seen = SDL_calloc((SIZE), sizeof(bool)); \
  109. Uint64 lost_count = 0; \
  110. SDL_assert(original_seen != NULL); \
  111. for (original_index = 0; original_index < (SIZE); original_index++) { \
  112. size_t sorted_index; \
  113. for (sorted_index = 0; sorted_index < (SIZE); sorted_index++) { \
  114. if (IS_SAME((ORIGINAL)[original_index], (SORTED)[sorted_index])) { \
  115. original_seen[original_index] = true; \
  116. break; \
  117. } \
  118. } \
  119. } \
  120. for (original_index = 0; original_index < (SIZE); original_index++) { \
  121. if (!original_seen[original_index]) { \
  122. SDLTest_AssertCheck(original_seen[original_index], \
  123. STR(TYPE) "Element %d is missing in the sorted array", (int)original_index); \
  124. lost_count += 1; \
  125. } \
  126. } \
  127. SDLTest_AssertCheck(lost_count == 0, \
  128. STR(TYPE) "No elements were lost (lost count=%" SDL_PRIu64 ")", lost_count); \
  129. SDL_free(original_seen); \
  130. } while (0)
  131. #define TEST_QSORT_ARRAY_GENERIC(TYPE, ARRAY, SIZE, QSORT_CALL, CHECK_ARRAY_ELEMS, IS_LE, WHAT) \
  132. do { \
  133. SDL_memcpy(sorted, (ARRAY), sizeof(TYPE) * (SIZE)); \
  134. SDLTest_AssertPass(STR(TYPE) "About to call " WHAT "(%d, %d)", \
  135. (int)(SIZE), (int)sizeof(TYPE)); \
  136. QSORT_CALL; \
  137. SDLTest_AssertPass(STR(TYPE) WHAT " finished"); \
  138. TEST_ARRAY_IS_SORTED(TYPE, sorted, SIZE, IS_LE); \
  139. SDLTest_AssertPass(STR(TYPE) "Verifying element preservation..."); \
  140. CHECK_ARRAY_ELEMS(TYPE, sorted, (ARRAY), (SIZE)); \
  141. } while (0)
  142. #define TEST_QSORT_ARRAY_QSORT(TYPE, ARRAY, SIZE, COMPARE_CBFN, CHECK_ARRAY_ELEMS, IS_LE) \
  143. do { \
  144. SDLTest_AssertPass(STR(TYPE) "Testing SDL_qsort of array with size %u", (unsigned)(SIZE)); \
  145. global_compare_cbfn = NULL; \
  146. TEST_QSORT_ARRAY_GENERIC(TYPE, ARRAY, SIZE, \
  147. SDL_qsort(sorted, (SIZE), sizeof(TYPE), COMPARE_CBFN), \
  148. CHECK_ARRAY_ELEMS, IS_LE, "SDL_qsort"); \
  149. } while (0);
  150. #if defined(SDL_PLATFORM_WINDOWS) && defined(TEST_STDLIB_QSORT)
  151. #define TEST_QSORT_ARRAY_QSORT_R(TYPE, ARRAY, SIZE, COMPARE_CBFN, CHECK_ARRAY_ELEMS, IS_LE) \
  152. do { \
  153. SDLTest_AssertPass(STR(TYPE) "qsort_r is not available on current platform"); \
  154. } while (0)
  155. #else
  156. #define TEST_QSORT_ARRAY_QSORT_R(TYPE, ARRAY, SIZE, COMPARE_CBFN, CHECK_ARRAY_ELEMS, IS_LE) \
  157. do { \
  158. SDLTest_AssertPass(STR(TYPE) "Testing SDL_qsort_r of array with size %u", (unsigned)(SIZE)); \
  159. global_compare_cbfn = (COMPARE_CBFN); \
  160. TEST_QSORT_ARRAY_GENERIC(TYPE, ARRAY, SIZE, \
  161. SDL_qsort_r(sorted, (SIZE), sizeof(TYPE), generic_compare_r, &a_global_var), \
  162. CHECK_ARRAY_ELEMS, IS_LE, "SDL_qsort_r"); \
  163. } while (0);
  164. #endif
  165. #define TEST_QSORT_ARRAY(TYPE, ARRAY, SIZE, COMPARE_CBFN, CHECK_ARRAY_ELEMS, IS_LE) \
  166. do { \
  167. TYPE *sorted = SDL_calloc((SIZE), sizeof(TYPE)); \
  168. SDL_assert(sorted != NULL); \
  169. \
  170. TEST_QSORT_ARRAY_QSORT(TYPE, ARRAY, SIZE, COMPARE_CBFN, CHECK_ARRAY_ELEMS, IS_LE); \
  171. \
  172. TEST_QSORT_ARRAY_QSORT_R(TYPE, ARRAY, SIZE, COMPARE_CBFN, CHECK_ARRAY_ELEMS, IS_LE); \
  173. \
  174. SDL_free(sorted); \
  175. } while (0)
  176. #define INT_ISLE(A, B) ((A) <= (B))
  177. #define INTPTR_ISLE(A, B) (*(A) <= *(B))
  178. #define FLOAT_ISLE(A, B) ((A) <= (B))
  179. #define DOUBLE_ISLE(A, B) ((A) <= (B))
  180. #define VERSION_ISLE(A, B) (compare_version(&(A), &(B)) <= 0)
  181. #define CHECK_ELEMS_SORTED_ARRAY(TYPE, SORTED, INPUT, SIZE) \
  182. do { \
  183. unsigned int check_index; \
  184. for (check_index = 0; check_index < (SIZE); check_index++) { \
  185. if ((SORTED)[check_index] != (INPUT)[check_index]) { \
  186. SDLTest_AssertCheck(false, \
  187. "sorted[%u] == input[%u]", \
  188. check_index, check_index); \
  189. } \
  190. } \
  191. } while (0)
  192. static int SDLCALL qsort_testAlreadySorted(void *arg)
  193. {
  194. unsigned int iteration;
  195. (void)arg;
  196. for (iteration = 0; iteration < count_arraylens; iteration++) {
  197. const unsigned int arraylen = arraylens[iteration];
  198. unsigned int i;
  199. int *ints = SDL_malloc(sizeof(int) * arraylen);
  200. int **intptrs = SDL_malloc(sizeof(int *) * arraylen);
  201. double *doubles = SDL_malloc(sizeof(double) * arraylen);
  202. for (i = 0; i < arraylen; i++) {
  203. ints[i] = i;
  204. intptrs[i] = &ints[i];
  205. doubles[i] = (double)i * SDL_PI_D;
  206. }
  207. TEST_QSORT_ARRAY(int, ints, arraylen, compare_int, CHECK_ELEMS_SORTED_ARRAY, INT_ISLE);
  208. TEST_QSORT_ARRAY(int *, intptrs, arraylen, compare_intptr, CHECK_ELEMS_SORTED_ARRAY, INTPTR_ISLE);
  209. TEST_QSORT_ARRAY(double, doubles, arraylen, compare_double, CHECK_ELEMS_SORTED_ARRAY, DOUBLE_ISLE);
  210. SDL_free(ints);
  211. SDL_free(intptrs);
  212. SDL_free(doubles);
  213. }
  214. return TEST_COMPLETED;
  215. }
  216. #define CHECK_ELEMS_SORTED_ARRAY_EXCEPT_LAST(TYPE, SORTED, INPUT, SIZE) \
  217. do { \
  218. unsigned int check_index; \
  219. for (check_index = 0; check_index < (SIZE) - 1; check_index++) { \
  220. if (SDL_memcmp(&(SORTED)[check_index + 1], &(INPUT)[check_index], sizeof(TYPE)) != 0) { \
  221. SDLTest_AssertCheck(false, \
  222. STR(TYPE) "sorted[%u] == input[%u]", \
  223. check_index + 1, check_index); \
  224. } \
  225. } \
  226. } while (0)
  227. static int SDLCALL qsort_testAlreadySortedExceptLast(void *arg)
  228. {
  229. unsigned int iteration;
  230. (void)arg;
  231. for (iteration = 0; iteration < count_arraylens; iteration++) {
  232. const unsigned int arraylen = arraylens[iteration];
  233. unsigned int i;
  234. int *ints = SDL_malloc(sizeof(int) * arraylen);
  235. int **intptrs = SDL_malloc(sizeof(int *) * arraylen);
  236. double *doubles = SDL_malloc(sizeof(double) * arraylen);
  237. VersionTuple *versions = SDL_calloc(arraylen, sizeof(VersionTuple));
  238. for (i = 0; i < arraylen; i++) {
  239. ints[i] = i;
  240. intptrs[i] = &ints[i];
  241. doubles[i] = (double)i * SDL_PI_D;
  242. versions[i].micro = (i + 1) % 256;
  243. versions[i].minor = (i + 1) % (256 * 256) / 256;
  244. versions[i].major = (i + 1) % (256 * 256 * 256) / 256 / 256;
  245. }
  246. ints[arraylen - 1] = -1;
  247. doubles[arraylen - 1] = -1.;
  248. versions[arraylen - 1].major = 0;
  249. versions[arraylen - 1].minor = 0;
  250. versions[arraylen - 1].micro = 0;
  251. TEST_QSORT_ARRAY(int, ints, arraylen, compare_int, CHECK_ELEMS_SORTED_ARRAY_EXCEPT_LAST, INT_ISLE);
  252. TEST_QSORT_ARRAY(int *, intptrs, arraylen, compare_intptr, CHECK_ELEMS_SORTED_ARRAY_EXCEPT_LAST, INTPTR_ISLE);
  253. TEST_QSORT_ARRAY(double, doubles, arraylen, compare_double, CHECK_ELEMS_SORTED_ARRAY_EXCEPT_LAST, DOUBLE_ISLE);
  254. TEST_QSORT_ARRAY(VersionTuple, versions, arraylen, compare_version, CHECK_ELEMS_SORTED_ARRAY_EXCEPT_LAST, VERSION_ISLE);
  255. SDL_free(ints);
  256. SDL_free(intptrs);
  257. SDL_free(doubles);
  258. SDL_free(versions);
  259. }
  260. return TEST_COMPLETED;
  261. }
  262. #define CHECK_ELEMS_SORTED_ARRAY_REVERSED(TYPE, SORTED, INPUT, SIZE) \
  263. do { \
  264. unsigned int check_index; \
  265. for (check_index = 0; check_index < (SIZE); check_index++) { \
  266. if (SDL_memcmp(&(SORTED)[check_index], &(INPUT)[(SIZE) - check_index - 1], sizeof(TYPE)) != 0) { \
  267. SDLTest_AssertCheck(false, \
  268. STR(TYPE) "sorted[%u] != input[%u]", \
  269. check_index, (SIZE) - check_index - 1); \
  270. } \
  271. } \
  272. } while (0)
  273. static int SDLCALL qsort_testReverseSorted(void *arg)
  274. {
  275. unsigned int iteration;
  276. (void)arg;
  277. for (iteration = 0; iteration < count_arraylens; iteration++) {
  278. const unsigned int arraylen = arraylens[iteration];
  279. unsigned int i;
  280. int *ints = SDL_malloc(sizeof(int) * arraylen);
  281. int **intptrs = SDL_malloc(sizeof(int *) * arraylen);
  282. double *doubles = SDL_malloc(sizeof(double) * arraylen);
  283. VersionTuple *versions = SDL_calloc(arraylen, sizeof(VersionTuple));
  284. for (i = 0; i < arraylen; i++) {
  285. ints[i] = (arraylen - 1) - i;
  286. intptrs[i] = &ints[i];
  287. doubles[i] = (double)((arraylen - 1) - i) * SDL_PI_D;
  288. versions[i].micro = ints[i] % 256;
  289. versions[i].minor = ints[i] % (256 * 256) / 256;
  290. versions[i].major = ints[i] % (256 * 256 * 256) / 256 / 256;
  291. }
  292. TEST_QSORT_ARRAY(int, ints, arraylen, compare_int, CHECK_ELEMS_SORTED_ARRAY_REVERSED, INT_ISLE);
  293. TEST_QSORT_ARRAY(int *, intptrs, arraylen, compare_intptr, CHECK_ELEMS_SORTED_ARRAY_REVERSED, INTPTR_ISLE);
  294. TEST_QSORT_ARRAY(double, doubles, arraylen, compare_double, CHECK_ELEMS_SORTED_ARRAY_REVERSED, DOUBLE_ISLE);
  295. TEST_QSORT_ARRAY(VersionTuple, versions, arraylen, compare_version, CHECK_ELEMS_SORTED_ARRAY_REVERSED, VERSION_ISLE);
  296. SDL_free(ints);
  297. SDL_free(intptrs);
  298. SDL_free(doubles);
  299. SDL_free(versions);
  300. }
  301. return TEST_COMPLETED;
  302. }
  303. #define MAX_RANDOM_INT_VALUE (1024 * 1024)
  304. #define CHECK_ELEMS_SORTED_ARRAY_RANDOM_INT(TYPE, SORTED, INPUT, SIZE) \
  305. do { \
  306. int *presences = SDL_calloc(MAX_RANDOM_INT_VALUE, sizeof(int)); \
  307. unsigned int check_index; \
  308. for (check_index = 0; check_index < (SIZE); check_index++) { \
  309. presences[(SORTED)[check_index]] += 1; \
  310. presences[(INPUT)[check_index]] -= 1; \
  311. } \
  312. for (check_index = 0; check_index < MAX_RANDOM_INT_VALUE; check_index++) { \
  313. if (presences[check_index] != 0) { \
  314. SDLTest_AssertCheck(false, "Value %d appears %s in sorted array", \
  315. check_index, \
  316. presences[check_index] > 0 ? "MORE" : "LESS"); \
  317. } \
  318. } \
  319. SDL_free(presences); \
  320. } while (0)
  321. #define VERSION_TO_INT(VERSION) (((VERSION).major * 256 + (VERSION).minor) * 256 + (VERSION).micro)
  322. #define INT_VERSION_MAJOR(V) (((V) / (256 * 256) % 256))
  323. #define INT_VERSION_MINOR(V) (((V) / (256)) % 256)
  324. #define INT_VERSION_MICRO(V) ((V) % 256)
  325. #define CHECK_ELEMS_SORTED_ARRAY_RANDOM_VERSION(TYPE, SORTED, INPUT, SIZE) \
  326. do { \
  327. int *presences = SDL_calloc(256 * 256 * 256, sizeof(int)); \
  328. unsigned int check_index; \
  329. for (check_index = 0; check_index < (SIZE); check_index++) { \
  330. presences[VERSION_TO_INT((SORTED)[check_index])] += 1; \
  331. presences[VERSION_TO_INT((INPUT)[check_index])] -= 1; \
  332. } \
  333. for (check_index = 0; check_index < 256 * 256 * 256; check_index++) { \
  334. if (presences[check_index] != 0) { \
  335. SDLTest_AssertCheck(false, STR(TYPE) "Version %d.%d.%d appears %s in sorted array", \
  336. INT_VERSION_MAJOR(check_index), \
  337. INT_VERSION_MINOR(check_index), \
  338. INT_VERSION_MICRO(check_index), \
  339. presences[check_index] > 0 ? "MORE" : "LESS"); \
  340. } \
  341. } \
  342. SDL_free(presences); \
  343. } while (0)
  344. #define CHECK_ELEMS_SORTED_ARRAY_RPS(TYPE, SORTED, INPUT, SIZE) \
  345. do { \
  346. int presences[3] = { 0 }; \
  347. unsigned int check_index; \
  348. for (check_index = 0; check_index < (SIZE); check_index++) { \
  349. presences[(SORTED)[check_index]] += 1; \
  350. presences[(INPUT)[check_index]] -= 1; \
  351. } \
  352. for (check_index = 0; check_index < SDL_arraysize(presences); check_index++) { \
  353. if (presences[check_index] != 0) { \
  354. SDLTest_AssertCheck(false, STR(TYPE) "%d appeared or disappeared", \
  355. check_index); \
  356. } \
  357. } \
  358. } while (0)
  359. #define CHECK_ELEMS_SORTED_ARRAY_RANDOM_NOP(TYPE, SORTED, INPUT, SIZE) \
  360. SDLTest_AssertPass(STR(TYPE) "Skipping elements presence check")
  361. static int SDLCALL qsort_testRandomSorted(void *arg)
  362. {
  363. unsigned int iteration;
  364. (void)arg;
  365. for (iteration = 0; iteration < count_arraylens; iteration++) {
  366. const unsigned int arraylen = arraylens[iteration];
  367. unsigned int i;
  368. int *ints = SDL_malloc(sizeof(int) * arraylen);
  369. float *floats = SDL_malloc(sizeof(float) * arraylen);
  370. VersionTuple *versions = SDL_calloc(arraylen, sizeof(VersionTuple));
  371. for (i = 0; i < arraylen; i++) {
  372. ints[i] = SDLTest_RandomIntegerInRange(0, MAX_RANDOM_INT_VALUE - 1);
  373. floats[i] = SDLTest_RandomFloat() * SDL_PI_F;
  374. versions[i].micro = SDLTest_RandomIntegerInRange(0, 255);
  375. versions[i].minor = SDLTest_RandomIntegerInRange(0, 255);
  376. versions[i].major = SDLTest_RandomIntegerInRange(0, 255);
  377. }
  378. TEST_QSORT_ARRAY(int, ints, arraylen, compare_int, CHECK_ELEMS_SORTED_ARRAY_RANDOM_INT, INT_ISLE);
  379. TEST_QSORT_ARRAY(float, floats, arraylen, compare_float, CHECK_ELEMS_SORTED_ARRAY_RANDOM_NOP, FLOAT_ISLE);
  380. TEST_QSORT_ARRAY(VersionTuple, versions, arraylen, compare_version, CHECK_ELEMS_SORTED_ARRAY_RANDOM_VERSION, VERSION_ISLE);
  381. SDL_free(ints);
  382. SDL_free(floats);
  383. SDL_free(versions);
  384. }
  385. return TEST_COMPLETED;
  386. }
  387. static const SDLTest_TestCaseReference qsortTestAlreadySorted = {
  388. qsort_testAlreadySorted, "qsort_testAlreadySorted", "Test sorting already sorted array", TEST_ENABLED
  389. };
  390. static const SDLTest_TestCaseReference qsortTestAlreadySortedExceptLast = {
  391. qsort_testAlreadySortedExceptLast, "qsort_testAlreadySortedExceptLast", "Test sorting nearly sorted array (last item is not in order)", TEST_ENABLED
  392. };
  393. static const SDLTest_TestCaseReference qsortTestReverseSorted = {
  394. qsort_testReverseSorted, "qsort_testReverseSorted", "Test sorting an array in reverse order", TEST_ENABLED
  395. };
  396. static const SDLTest_TestCaseReference qsortTestRandomSorted = {
  397. qsort_testRandomSorted, "qsort_testRandomSorted", "Test sorting a random array", TEST_ENABLED
  398. };
  399. static const SDLTest_TestCaseReference *qsortTests[] = {
  400. &qsortTestAlreadySorted,
  401. &qsortTestAlreadySortedExceptLast,
  402. &qsortTestReverseSorted,
  403. &qsortTestRandomSorted,
  404. NULL
  405. };
  406. static SDLTest_TestSuiteReference qsortTestSuite = {
  407. "qsort",
  408. NULL,
  409. qsortTests,
  410. NULL
  411. };
  412. static SDLTest_TestSuiteReference *testSuites[] = {
  413. &qsortTestSuite,
  414. NULL
  415. };
  416. int main(int argc, char *argv[])
  417. {
  418. int i;
  419. int result;
  420. SDLTest_CommonState *state;
  421. SDLTest_TestSuiteRunner *runner;
  422. bool list = false;
  423. /* Initialize test framework */
  424. state = SDLTest_CommonCreateState(argv, 0);
  425. if (!state) {
  426. return 1;
  427. }
  428. runner = SDLTest_CreateTestSuiteRunner(state, testSuites);
  429. /* Parse commandline */
  430. for (i = 1; i < argc;) {
  431. int consumed;
  432. consumed = SDLTest_CommonArg(state, i);
  433. if (!consumed) {
  434. if (SDL_strcasecmp(argv[i], "--array-lengths") == 0) {
  435. count_arraylens = 0;
  436. consumed = 1;
  437. while (argv[i + consumed] && argv[i + consumed][0] != '-') {
  438. char *endptr = NULL;
  439. unsigned int arraylen = (unsigned int)SDL_strtoul(argv[i + 1], &endptr, 10);
  440. if (*endptr != '\0') {
  441. count_arraylens = 0;
  442. break;
  443. }
  444. if (count_arraylens >= SDL_arraysize(arraylens)) {
  445. SDL_LogWarn(SDL_LOG_CATEGORY_TEST, "Dropping array length %d", arraylen);
  446. } else {
  447. arraylens[count_arraylens] = arraylen;
  448. }
  449. count_arraylens++;
  450. consumed++;
  451. }
  452. if (consumed == 0) {
  453. SDL_LogError(SDL_LOG_CATEGORY_TEST, "--array-lengths needs positive int numbers");
  454. }
  455. } else if (SDL_strcasecmp(argv[i], "--list") == 0) {
  456. consumed = 1;
  457. list = true;
  458. }
  459. }
  460. if (consumed <= 0) {
  461. static const char *options[] = {
  462. "[--list]",
  463. "[--array-lengths N1 [N2 [N3 [...]]]",
  464. NULL
  465. };
  466. SDLTest_CommonLogUsage(state, argv[0], options);
  467. return 1;
  468. }
  469. i += consumed;
  470. }
  471. /* List all suites. */
  472. if (list) {
  473. int suiteCounter;
  474. for (suiteCounter = 0; testSuites[suiteCounter]; ++suiteCounter) {
  475. int testCounter;
  476. SDLTest_TestSuiteReference *testSuite = testSuites[suiteCounter];
  477. SDL_Log("Test suite: %s", testSuite->name);
  478. for (testCounter = 0; testSuite->testCases[testCounter]; ++testCounter) {
  479. const SDLTest_TestCaseReference *testCase = testSuite->testCases[testCounter];
  480. SDL_Log(" test: %s%s", testCase->name, testCase->enabled ? "" : " (disabled)");
  481. }
  482. }
  483. result = 0;
  484. } else {
  485. result = SDLTest_ExecuteTestSuiteRunner(runner);
  486. }
  487. SDL_Quit();
  488. SDLTest_DestroyTestSuiteRunner(runner);
  489. SDLTest_CommonDestroyState(state);
  490. return result;
  491. }