diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testqsort.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/test/testqsort.c | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testqsort.c b/src/contrib/SDL-3.2.20/test/testqsort.c new file mode 100644 index 0000000..6230e74 --- /dev/null +++ b/src/contrib/SDL-3.2.20/test/testqsort.c | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
3 | |||
4 | This software is provided 'as-is', without any express or implied | ||
5 | warranty. In no event will the authors be held liable for any damages | ||
6 | arising from the use of this software. | ||
7 | |||
8 | Permission is granted to anyone to use this software for any purpose, | ||
9 | including commercial applications, and to alter it and redistribute it | ||
10 | freely. | ||
11 | */ | ||
12 | |||
13 | #include <SDL3/SDL.h> | ||
14 | #include <SDL3/SDL_main.h> | ||
15 | #include <SDL3/SDL_test.h> | ||
16 | |||
17 | static int a_global_var = 77; | ||
18 | |||
19 | static int SDLCALL | ||
20 | num_compare(const void *_a, const void *_b) | ||
21 | { | ||
22 | const int a = *((const int *)_a); | ||
23 | const int b = *((const int *)_b); | ||
24 | return (a < b) ? -1 : ((a > b) ? 1 : 0); | ||
25 | } | ||
26 | |||
27 | static int SDLCALL | ||
28 | num_compare_r(void *userdata, const void *a, const void *b) | ||
29 | { | ||
30 | if (userdata != &a_global_var) { | ||
31 | SDL_Log("Uhoh, invalid userdata during qsort!"); | ||
32 | } | ||
33 | return num_compare(a, b); | ||
34 | } | ||
35 | |||
36 | static void | ||
37 | test_sort(const char *desc, int *nums, const int arraylen) | ||
38 | { | ||
39 | static int nums_copy[1024 * 100]; | ||
40 | int i; | ||
41 | int prev; | ||
42 | |||
43 | SDL_assert(SDL_arraysize(nums_copy) >= arraylen); | ||
44 | |||
45 | SDL_Log("test: %s arraylen=%d", desc, arraylen); | ||
46 | |||
47 | SDL_memcpy(nums_copy, nums, arraylen * sizeof (*nums)); | ||
48 | |||
49 | SDL_qsort(nums, arraylen, sizeof(nums[0]), num_compare); | ||
50 | SDL_qsort_r(nums_copy, arraylen, sizeof(nums[0]), num_compare_r, &a_global_var); | ||
51 | |||
52 | prev = nums[0]; | ||
53 | for (i = 1; i < arraylen; i++) { | ||
54 | const int val = nums[i]; | ||
55 | const int val2 = nums_copy[i]; | ||
56 | if ((val < prev) || (val != val2)) { | ||
57 | SDL_Log("sort is broken!"); | ||
58 | return; | ||
59 | } | ||
60 | prev = val; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | int main(int argc, char *argv[]) | ||
65 | { | ||
66 | static int nums[1024 * 100]; | ||
67 | static const int itervals[] = { SDL_arraysize(nums), 12 }; | ||
68 | int i; | ||
69 | int iteration; | ||
70 | SDLTest_CommonState *state; | ||
71 | Uint64 seed = 0; | ||
72 | int seed_seen = 0; | ||
73 | |||
74 | /* Initialize test framework */ | ||
75 | state = SDLTest_CommonCreateState(argv, 0); | ||
76 | if (!state) { | ||
77 | return 1; | ||
78 | } | ||
79 | |||
80 | /* Parse commandline */ | ||
81 | for (i = 1; i < argc;) { | ||
82 | int consumed; | ||
83 | |||
84 | consumed = SDLTest_CommonArg(state, i); | ||
85 | if (!consumed) { | ||
86 | if (!seed_seen) { | ||
87 | char *endptr = NULL; | ||
88 | |||
89 | seed = (Uint64)SDL_strtoull(argv[i], &endptr, 0); | ||
90 | if (endptr != argv[i] && *endptr == '\0') { | ||
91 | seed_seen = 1; | ||
92 | consumed = 1; | ||
93 | } else { | ||
94 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number."); | ||
95 | return 1; | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | if (consumed <= 0) { | ||
100 | static const char *options[] = { "[seed]", NULL }; | ||
101 | SDLTest_CommonLogUsage(state, argv[0], options); | ||
102 | return 1; | ||
103 | } | ||
104 | |||
105 | i += consumed; | ||
106 | } | ||
107 | |||
108 | if (!seed_seen) { | ||
109 | seed = SDL_GetPerformanceCounter(); | ||
110 | } | ||
111 | SDL_Log("Using random seed 0x%" SDL_PRIx64, seed); | ||
112 | |||
113 | for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) { | ||
114 | const int arraylen = itervals[iteration]; | ||
115 | |||
116 | for (i = 0; i < arraylen; i++) { | ||
117 | nums[i] = i; | ||
118 | } | ||
119 | test_sort("already sorted", nums, arraylen); | ||
120 | |||
121 | for (i = 0; i < arraylen; i++) { | ||
122 | nums[i] = i; | ||
123 | } | ||
124 | nums[arraylen - 1] = -1; | ||
125 | test_sort("already sorted except last element", nums, arraylen); | ||
126 | |||
127 | for (i = 0; i < arraylen; i++) { | ||
128 | nums[i] = (arraylen - 1) - i; | ||
129 | } | ||
130 | test_sort("reverse sorted", nums, arraylen); | ||
131 | |||
132 | for (i = 0; i < arraylen; i++) { | ||
133 | nums[i] = SDL_rand_r(&seed, 1000000); | ||
134 | } | ||
135 | test_sort("random sorted", nums, arraylen); | ||
136 | } | ||
137 | |||
138 | SDL_Quit(); | ||
139 | SDLTest_CommonDestroyState(state); | ||
140 | |||
141 | return 0; | ||
142 | } | ||