SDL  2.0
testqsort.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2020 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 "SDL_test.h"
14 
15 static int
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
45 main(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  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
62  return 1;
63  }
64  if (seed <= ((Uint64)0xffffffff)) {
65  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
66  return 1;
67  }
68  SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
69  }
70  else
71  {
72  SDLTest_RandomInitTime(&rndctx);
73  }
74  SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
75 
76  for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
77  const int arraylen = itervals[iteration];
78  int i;
79 
80  for (i = 0; i < arraylen; i++) {
81  nums[i] = i;
82  }
83  test_sort("already sorted", nums, arraylen);
84 
85  for (i = 0; i < arraylen; i++) {
86  nums[i] = i;
87  }
88  nums[arraylen-1] = -1;
89  test_sort("already sorted except last element", nums, arraylen);
90 
91  for (i = 0; i < arraylen; i++) {
92  nums[i] = (arraylen-1) - i;
93  }
94  test_sort("reverse sorted", nums, arraylen);
95 
96  for (i = 0; i < arraylen; i++) {
97  nums[i] = SDLTest_RandomInt(&rndctx);
98  }
99  test_sort("random sorted", nums, arraylen);
100  }
101 
102  return 0;
103 }
104 
105 /* vi: set ts=4 sw=4 expandtab: */
106 
num_compare
static int num_compare(const void *_a, const void *_b)
Definition: testqsort.c:16
main
int main(int argc, char *argv[])
Definition: testqsort.c:45
SDL_test.h
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1112
SDL_qsort
#define SDL_qsort
Definition: SDL_dynapi_overrides.h:380
SDLTest_RandomContext
Definition: SDL_test_random.h:59
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1112
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
test_sort
static void test_sort(const char *desc, int *nums, const int arraylen)
Definition: testqsort.c:24
iteration
static void iteration()
Definition: testaudiohotplug.c:84
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDLTest_RandomInt
#define SDLTest_RandomInt(c)
Definition: SDL_test_random.h:54
SDLTest_RandomContext::x
unsigned int x
Definition: SDL_test_random.h:61
SDL_sscanf
#define SDL_sscanf
Definition: SDL_dynapi_overrides.h:39
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
SDLTest_RandomContext::c
unsigned int c
Definition: SDL_test_random.h:62
SDLTest_RandomInit
void SDLTest_RandomInit(SDLTest_RandomContext *rndContext, unsigned int xi, unsigned int ci)
Initialize random number generator with two integers.
Definition: SDL_test_random.c:41
val
GLuint GLfloat * val
Definition: SDL_opengl_glext.h:1495
SDLTest_RandomInitTime
void SDLTest_RandomInitTime(SDLTest_RandomContext *rndContext)
Initialize random number generator based on current system time.
Definition: SDL_test_random.c:65
Uint64
uint64_t Uint64
Definition: SDL_stdinc.h:216
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50