testqsort.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  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. #include <SDL3/SDL.h>
  11. #include <SDL3/SDL_main.h>
  12. #include <SDL3/SDL_test.h>
  13. static int a_global_var = 77;
  14. static int SDLCALL
  15. num_compare(const void *_a, const void *_b)
  16. {
  17. const int a = *((const int *)_a);
  18. const int b = *((const int *)_b);
  19. return (a < b) ? -1 : ((a > b) ? 1 : 0);
  20. }
  21. static int SDLCALL
  22. num_compare_r(void *userdata, const void *a, const void *b)
  23. {
  24. if (userdata != &a_global_var) {
  25. SDL_Log("Uhoh, invalid userdata during qsort!");
  26. }
  27. return num_compare(a, b);
  28. }
  29. static void
  30. test_sort(const char *desc, int *nums, const int arraylen)
  31. {
  32. static int nums_copy[1024 * 100];
  33. int i;
  34. int prev;
  35. SDL_assert(SDL_arraysize(nums_copy) >= arraylen);
  36. SDL_Log("test: %s arraylen=%d", desc, arraylen);
  37. SDL_memcpy(nums_copy, nums, arraylen * sizeof (*nums));
  38. SDL_qsort(nums, arraylen, sizeof(nums[0]), num_compare);
  39. SDL_qsort_r(nums_copy, arraylen, sizeof(nums[0]), num_compare_r, &a_global_var);
  40. prev = nums[0];
  41. for (i = 1; i < arraylen; i++) {
  42. const int val = nums[i];
  43. const int val2 = nums_copy[i];
  44. if ((val < prev) || (val != val2)) {
  45. SDL_Log("sort is broken!");
  46. return;
  47. }
  48. prev = val;
  49. }
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. static int nums[1024 * 100];
  54. static const int itervals[] = { SDL_arraysize(nums), 12 };
  55. int i;
  56. int iteration;
  57. SDLTest_CommonState *state;
  58. Uint64 seed = 0;
  59. int seed_seen = 0;
  60. /* Initialize test framework */
  61. state = SDLTest_CommonCreateState(argv, 0);
  62. if (!state) {
  63. return 1;
  64. }
  65. /* Parse commandline */
  66. for (i = 1; i < argc;) {
  67. int consumed;
  68. consumed = SDLTest_CommonArg(state, i);
  69. if (!consumed) {
  70. if (!seed_seen) {
  71. char *endptr = NULL;
  72. seed = (Uint64)SDL_strtoull(argv[i], &endptr, 0);
  73. if (endptr != argv[i] && *endptr == '\0') {
  74. seed_seen = 1;
  75. consumed = 1;
  76. } else {
  77. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
  78. return 1;
  79. }
  80. }
  81. }
  82. if (consumed <= 0) {
  83. static const char *options[] = { "[seed]", NULL };
  84. SDLTest_CommonLogUsage(state, argv[0], options);
  85. return 1;
  86. }
  87. i += consumed;
  88. }
  89. if (!seed_seen) {
  90. seed = SDL_GetPerformanceCounter();
  91. }
  92. SDL_Log("Using random seed 0x%" SDL_PRIx64 "\n", seed);
  93. for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
  94. const int arraylen = itervals[iteration];
  95. for (i = 0; i < arraylen; i++) {
  96. nums[i] = i;
  97. }
  98. test_sort("already sorted", nums, arraylen);
  99. for (i = 0; i < arraylen; i++) {
  100. nums[i] = i;
  101. }
  102. nums[arraylen - 1] = -1;
  103. test_sort("already sorted except last element", nums, arraylen);
  104. for (i = 0; i < arraylen; i++) {
  105. nums[i] = (arraylen - 1) - i;
  106. }
  107. test_sort("reverse sorted", nums, arraylen);
  108. for (i = 0; i < arraylen; i++) {
  109. nums[i] = SDL_rand_r(&seed, 1000000);
  110. }
  111. test_sort("random sorted", nums, arraylen);
  112. }
  113. SDL_Quit();
  114. SDLTest_CommonDestroyState(state);
  115. return 0;
  116. }