2 * test-sgen-qsort.c: Unit test for quicksort.
4 * Copyright (C) 2013 Xamarin Inc
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #include <sgen/sgen-gc.h>
12 #include <sgen/sgen-qsort.h>
20 compare_ints (const void *pa, const void *pb)
22 int a = *(const int*)pa;
23 int b = *(const int*)pb;
37 compare_teststructs (const void *pa, const void *pb)
39 int a = ((const teststruct_t*)pa)->key;
40 int b = ((const teststruct_t*)pb)->key;
49 compare_teststructs2 (const void *pa, const void *pb)
51 int a = (*((const teststruct_t**)pa))->key;
52 int b = (*((const teststruct_t**)pb))->key;
60 DEF_QSORT_INLINE(test_struct, teststruct_t*, compare_teststructs)
63 compare_sorts (void *base, size_t nel, size_t width, int (*compar) (const void*, const void*))
65 size_t len = nel * width;
66 void *b1 = malloc (len);
67 void *b2 = malloc (len);
69 memcpy (b1, base, len);
70 memcpy (b2, base, len);
72 qsort (b1, nel, width, compar);
73 sgen_qsort (b2, nel, width, compar);
75 assert (!memcmp (b1, b2, len));
82 compare_sorts2 (void *base, size_t nel)
84 size_t len = nel * sizeof (teststruct_t*);
85 void *b1 = malloc (len);
86 void *b2 = malloc (len);
88 memcpy (b1, base, len);
89 memcpy (b2, base, len);
91 qsort (b1, nel, sizeof (teststruct_t*), compare_teststructs2);
92 qsort_test_struct ((teststruct_t **)b2, nel);
94 assert (!memcmp (b1, b2, len));
103 for (i = 1; i < 4000; ++i) {
107 for (j = 0; j < i; ++j)
109 compare_sorts (a, i, sizeof (int), compare_ints);
112 srandom (time (NULL));
113 for (i = 0; i < 2000; ++i) {
114 teststruct_t a [200];
116 for (j = 0; j < 200; ++j) {
117 a [j].key = random ();
118 a [j].val = random ();
121 compare_sorts (a, 200, sizeof (teststruct_t), compare_teststructs);
124 srandom (time (NULL));
125 for (i = 0; i < 2000; ++i) {
126 teststruct_t a [200];
127 teststruct_t *b [200];
129 for (j = 0; j < 200; ++j) {
130 a [j].key = random ();
131 a [j].val = random ();
135 compare_sorts2 (b, 200);