icculus@10109: /* icculus@10109: Copyright (C) 1997-2016 Sam Lantinga icculus@10109: icculus@10109: This software is provided 'as-is', without any express or implied icculus@10109: warranty. In no event will the authors be held liable for any damages icculus@10109: arising from the use of this software. icculus@10109: icculus@10109: Permission is granted to anyone to use this software for any purpose, icculus@10109: including commercial applications, and to alter it and redistribute it icculus@10109: freely. icculus@10109: */ icculus@10109: slouken@10415: #include "SDL_test.h" icculus@10109: icculus@10109: static int icculus@10109: num_compare(const void *_a, const void *_b) icculus@10109: { icculus@10109: const int a = *((const int *) _a); icculus@10109: const int b = *((const int *) _b); icculus@10109: return (a < b) ? -1 : ((a > b) ? 1 : 0); icculus@10109: } icculus@10109: icculus@10109: static void icculus@10109: test_sort(const char *desc, int *nums, const int arraylen) icculus@10109: { icculus@10109: int i; icculus@10109: int prev; icculus@10109: icculus@10109: SDL_Log("test: %s arraylen=%d", desc, arraylen); icculus@10109: icculus@10109: SDL_qsort(nums, arraylen, sizeof (nums[0]), num_compare); icculus@10109: icculus@10109: prev = nums[0]; icculus@10109: for (i = 1; i < arraylen; i++) { icculus@10109: const int val = nums[i]; icculus@10109: if (val < prev) { icculus@10109: SDL_Log("sort is broken!"); icculus@10109: return; icculus@10109: } icculus@10109: prev = val; icculus@10109: } icculus@10109: } icculus@10109: icculus@10109: int icculus@10109: main(int argc, char *argv[]) icculus@10109: { icculus@10109: static int nums[1024 * 100]; icculus@10109: static const int itervals[] = { SDL_arraysize(nums), 12 }; icculus@10109: int iteration; slouken@10415: SDLTest_RandomContext rndctx; slouken@10415: slouken@10415: if (argc > 1) slouken@10415: { slouken@10415: int success; slouken@10415: Uint64 seed = 0; slouken@10415: if (argv[1][0] == '0' && argv[1][1] == 'x') slouken@10415: success = SDL_sscanf(argv[1] + 2, "%llx", &seed); slouken@10415: else slouken@10415: success = SDL_sscanf(argv[1], "%llu", &seed); slouken@10415: if (!success) slouken@10415: { slouken@10415: SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n"); slouken@10415: return 1; slouken@10415: } slouken@10415: if (seed <= 0xffffffff) slouken@10415: { slouken@10415: SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n"); slouken@10415: return 1; slouken@10415: } slouken@10415: SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff)); slouken@10415: } slouken@10415: else slouken@10415: { slouken@10415: SDLTest_RandomInitTime(&rndctx); slouken@10415: } slouken@10415: SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c); icculus@10109: icculus@10109: for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) { icculus@10109: const int arraylen = itervals[iteration]; icculus@10109: int i; icculus@10109: icculus@10109: for (i = 0; i < arraylen; i++) { icculus@10109: nums[i] = i; icculus@10109: } icculus@10109: test_sort("already sorted", nums, arraylen); icculus@10109: icculus@10109: for (i = 0; i < arraylen; i++) { icculus@10109: nums[i] = i; icculus@10109: } icculus@10109: nums[arraylen-1] = -1; icculus@10109: test_sort("already sorted except last element", nums, arraylen); icculus@10109: icculus@10109: for (i = 0; i < arraylen; i++) { icculus@10109: nums[i] = (arraylen-1) - i; icculus@10109: } icculus@10109: test_sort("reverse sorted", nums, arraylen); icculus@10109: icculus@10109: for (i = 0; i < arraylen; i++) { slouken@10415: nums[i] = SDLTest_RandomInt(&rndctx); icculus@10109: } icculus@10109: test_sort("random sorted", nums, arraylen); icculus@10109: } icculus@10109: icculus@10109: return 0; icculus@10109: } icculus@10109: icculus@10109: /* vi: set ts=4 sw=4 expandtab: */ icculus@10109: