2 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
16 num_compare(const void *_a, const void *_b)
18 const int a = *((const int *) _a);
19 const int b = *((const int *) _b);
20 return (a < b) ? -1 : ((a > b) ? 1 : 0);
24 test_sort(const char *desc, int *nums, const int arraylen)
29 SDL_Log("test: %s arraylen=%d", desc, arraylen);
31 SDL_qsort(nums, arraylen, sizeof (nums[0]), num_compare);
34 for (i = 1; i < arraylen; i++) {
35 const int val = nums[i];
37 SDL_Log("sort is broken!");
45 main(int argc, char *argv[])
47 static int nums[1024 * 100];
48 static const int itervals[] = { SDL_arraysize(nums), 12 };
50 SDLTest_RandomContext rndctx;
56 if (argv[1][0] == '0' && argv[1][1] == 'x')
57 success = SDL_sscanf(argv[1] + 2, "%llx", &seed);
59 success = SDL_sscanf(argv[1], "%llu", &seed);
61 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
64 if (seed <= ((Uint64)0xffffffff)) {
65 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
68 SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
72 SDLTest_RandomInitTime(&rndctx);
74 SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
76 for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
77 const int arraylen = itervals[iteration];
80 for (i = 0; i < arraylen; i++) {
83 test_sort("already sorted", nums, arraylen);
85 for (i = 0; i < arraylen; i++) {
88 nums[arraylen-1] = -1;
89 test_sort("already sorted except last element", nums, arraylen);
91 for (i = 0; i < arraylen; i++) {
92 nums[i] = (arraylen-1) - i;
94 test_sort("reverse sorted", nums, arraylen);
96 for (i = 0; i < arraylen; i++) {
97 nums[i] = SDLTest_RandomInt(&rndctx);
99 test_sort("random sorted", nums, arraylen);
105 /* vi: set ts=4 sw=4 expandtab: */