Debian Codegraph @

test/testqsort.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
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 "SDL_test.h"
14
15 static int SDLCALL
16 num_compare(const void *_a, const void *_b)
17 {
18 const int a = *((const int *)_a);
19 const int b = *((const int *)_b);
20 return (a < b) ? -1 : ((a > b) ? 1 : 0);
21 }
22
23 static void
24 test_sort(const char *desc, int *nums, const int arraylen)
25 {
26 int i;
27 int prev;
28
29 SDL_Log("test: %s arraylen=%d", desc, arraylen);
30
31 SDL_qsort(nums, arraylen, sizeof(nums[0]), num_compare);
32
33 prev = nums[0];
34 for (i = 1; i < arraylen; i++) {
35 const int val = nums[i];
36 if (val < prev) {
37 SDL_Log("sort is broken!");
38 return;
39 }
40 prev = val;
41 }
42 }
43
44 int main(int argc, char *argv[])
45 {
46 SDLTest_CommonState *state;
47 static int nums[1024 * 100];
48 static const int itervals[] = { SDL_arraysize(nums), 12 };
49 int iteration;
50 int i;
51 SDL_bool custom_seed = SDL_FALSE;
52 Uint64 seed;
53 SDLTest_RandomContext rndctx;
54
55 state = SDLTest_CommonCreateState(argv, 0);
56 if (!state) {
57 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
58 return 1;
59 }
60
61 /* Enable standard application logging */
62 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
63
64 /* Parse commandline */
65 for (i = 1; i < argc;) {
66 int consumed;
67
68 consumed = SDLTest_CommonArg(state, i);
69 if (!consumed) {
70 if (!custom_seed) {
71 int success;
72
73 if (argv[i][0] == '0' && argv[i][1] == 'x') {
74 success = SDL_sscanf(argv[i] + 2, "%" SDL_PRIx64, &seed);
75 } else {
76 success = SDL_sscanf(argv[i], "%" SDL_PRIu64, &seed);
77 }
78 if (!success) {
79 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
80 return 1;
81 }
82 if (seed <= ((Uint64)0xffffffff)) {
83 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
84 return 1;
85 }
86 custom_seed = SDL_TRUE;
87 consumed = 1;
88 }
89 }
90
91 if (consumed <= 0) {
92 static const char *options[] = { "[SEED]", NULL };
93 SDLTest_CommonLogUsage(state, argv[0], options);
94 return 1;
95 }
96
97 i += consumed;
98 }
99
100 if (custom_seed) {
101 SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
102 } else {
103 SDLTest_RandomInitTime(&rndctx);
104 }
105 SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
106
107 if (!SDLTest_CommonInit(state)) {
108 return 1;
109 }
110
111 for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
112 const int arraylen = itervals[iteration];
113
114 for (i = 0; i < arraylen; i++) {
115 nums[i] = i;
116 }
117 test_sort("already sorted", nums, arraylen);
118
119 for (i = 0; i < arraylen; i++) {
120 nums[i] = i;
121 }
122 nums[arraylen - 1] = -1;
123 test_sort("already sorted except last element", nums, arraylen);
124
125 for (i = 0; i < arraylen; i++) {
126 nums[i] = (arraylen - 1) - i;
127 }
128 test_sort("reverse sorted", nums, arraylen);
129
130 for (i = 0; i < arraylen; i++) {
131 nums[i] = SDLTest_RandomInt(&rndctx);
132 }
133 test_sort("random sorted", nums, arraylen);
134 }
135
136 SDLTest_CommonQuit(state);
137
138 return 0;
139 }
140
141 /* vi: set ts=4 sw=4 expandtab: */
142