[mono-api-info] First filter, then order, and use ordinal comparison.
[mono-project.git] / mono / unit-tests / test-sgen-qsort.c
blob951b1b7321bb04a0f35ef4d9408308265748ac41
1 /*
2 * test-sgen-qsort.c: Unit test for quicksort.
4 * Copyright (C) 2013 Xamarin Inc
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License 2.0 as published by the Free Software Foundation;
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License 2.0 along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "config.h"
22 #include <sgen/sgen-gc.h>
23 #include <sgen/sgen-qsort.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <assert.h>
30 static int
31 compare_ints (const void *pa, const void *pb)
33 int a = *(const int*)pa;
34 int b = *(const int*)pb;
35 if (a < b)
36 return -1;
37 if (a == b)
38 return 0;
39 return 1;
42 typedef struct {
43 int key;
44 int val;
45 } teststruct_t;
47 static int
48 compare_teststructs (const void *pa, const void *pb)
50 int a = ((const teststruct_t*)pa)->key;
51 int b = ((const teststruct_t*)pb)->key;
52 if (a < b)
53 return -1;
54 if (a == b)
55 return 0;
56 return 1;
59 static int
60 compare_teststructs2 (const void *pa, const void *pb)
62 int a = (*((const teststruct_t**)pa))->key;
63 int b = (*((const teststruct_t**)pb))->key;
64 if (a < b)
65 return -1;
66 if (a == b)
67 return 0;
68 return 1;
71 DEF_QSORT_INLINE(test_struct, teststruct_t*, compare_teststructs)
73 static void
74 compare_sorts (void *base, size_t nel, size_t width, int (*compar) (const void*, const void*))
76 size_t len = nel * width;
77 void *b1 = malloc (len);
78 void *b2 = malloc (len);
80 memcpy (b1, base, len);
81 memcpy (b2, base, len);
83 qsort (b1, nel, width, compar);
84 sgen_qsort (b2, nel, width, compar);
86 assert (!memcmp (b1, b2, len));
88 free (b1);
89 free (b2);
92 static void
93 compare_sorts2 (void *base, size_t nel)
95 size_t len = nel * sizeof (teststruct_t*);
96 void *b1 = malloc (len);
97 void *b2 = malloc (len);
99 memcpy (b1, base, len);
100 memcpy (b2, base, len);
102 qsort (b1, nel, sizeof (teststruct_t*), compare_teststructs2);
103 qsort_test_struct ((teststruct_t **)b2, nel);
105 assert (!memcmp (b1, b2, len));
107 free (b1);
108 free (b2);
111 main (void)
113 int i;
114 for (i = 1; i < 4000; ++i) {
115 int a [i];
116 int j;
118 for (j = 0; j < i; ++j)
119 a [j] = i - j - 1;
120 compare_sorts (a, i, sizeof (int), compare_ints);
123 srandom (time (NULL));
124 for (i = 0; i < 2000; ++i) {
125 teststruct_t a [200];
126 int j;
127 for (j = 0; j < 200; ++j) {
128 a [j].key = random ();
129 a [j].val = random ();
132 compare_sorts (a, 200, sizeof (teststruct_t), compare_teststructs);
135 srandom (time (NULL));
136 for (i = 0; i < 2000; ++i) {
137 teststruct_t a [200];
138 teststruct_t *b [200];
139 int j;
140 for (j = 0; j < 200; ++j) {
141 a [j].key = random ();
142 a [j].val = random ();
143 b [j] = &a[j];
146 compare_sorts2 (b, 200);
148 return 0;