Atlas - testqsort.c

Home / ext / SDL2 / test Lines: 4 | Size: 2889 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2018 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 15static int 16num_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 23static void 24test_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 44int 45main(int argc, char *argv[]) 46{ 47 static int nums[1024 * 100]; 48 static const int itervals[] = { SDL_arraysize(nums), 12 }; 49 int iteration; 50 SDLTest_RandomContext rndctx; 51 52 if (argc > 1) 53 { 54 int success; 55 Uint64 seed = 0; 56 if (argv[1][0] == '0' && argv[1][1] == 'x') 57 success = SDL_sscanf(argv[1] + 2, "%llx", &seed); 58 else 59 success = SDL_sscanf(argv[1], "%llu", &seed); 60 if (!success) 61 { 62 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n"); 63 return 1; 64 } 65 if (seed <= 0xffffffff) 66 { 67 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n"); 68 return 1; 69 } 70 SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff)); 71 } 72 else 73 { 74 SDLTest_RandomInitTime(&rndctx); 75 } 76 SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c); 77 78 for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) { 79 const int arraylen = itervals[iteration]; 80 int i; 81 82 for (i = 0; i < arraylen; i++) { 83 nums[i] = i; 84 } 85 test_sort("already sorted", nums, arraylen); 86 87 for (i = 0; i < arraylen; i++) { 88 nums[i] = i; 89 } 90 nums[arraylen-1] = -1; 91 test_sort("already sorted except last element", nums, arraylen); 92 93 for (i = 0; i < arraylen; i++) { 94 nums[i] = (arraylen-1) - i; 95 } 96 test_sort("reverse sorted", nums, arraylen); 97 98 for (i = 0; i < arraylen; i++) { 99 nums[i] = SDLTest_RandomInt(&rndctx); 100 } 101 test_sort("random sorted", nums, arraylen); 102 } 103 104 return 0; 105} 106 107/* vi: set ts=4 sw=4 expandtab: */ 108 109
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.