First set of licensing changes
[mono.git] / mono / unit-tests / test-sgen-qsort.c
1 /*
2  * test-sgen-qsort.c: Unit test for quicksort.
3  *
4  * Copyright (C) 2013 Xamarin Inc
5  *
6  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7  */
8
9 #include "config.h"
10
11 #include <sgen/sgen-gc.h>
12 #include <sgen/sgen-qsort.h>
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include <assert.h>
18
19 static int
20 compare_ints (const void *pa, const void *pb)
21 {
22         int a = *(const int*)pa;
23         int b = *(const int*)pb;
24         if (a < b)
25                 return -1;
26         if (a == b)
27                 return 0;
28         return 1;
29 }
30
31 typedef struct {
32         int key;
33         int val;
34 } teststruct_t;
35
36 static int
37 compare_teststructs (const void *pa, const void *pb)
38 {
39         int a = ((const teststruct_t*)pa)->key;
40         int b = ((const teststruct_t*)pb)->key;
41         if (a < b)
42                 return -1;
43         if (a == b)
44                 return 0;
45         return 1;
46 }
47
48 static int
49 compare_teststructs2 (const void *pa, const void *pb)
50 {
51         int a = (*((const teststruct_t**)pa))->key;
52         int b = (*((const teststruct_t**)pb))->key;
53         if (a < b)
54                 return -1;
55         if (a == b)
56                 return 0;
57         return 1;
58 }
59
60 DEF_QSORT_INLINE(test_struct, teststruct_t*, compare_teststructs)
61
62 static void
63 compare_sorts (void *base, size_t nel, size_t width, int (*compar) (const void*, const void*))
64 {
65         size_t len = nel * width;
66         void *b1 = malloc (len);
67         void *b2 = malloc (len);
68
69         memcpy (b1, base, len);
70         memcpy (b2, base, len);
71
72         qsort (b1, nel, width, compar);
73         sgen_qsort (b2, nel, width, compar);
74
75         assert (!memcmp (b1, b2, len));
76
77         free (b1);
78         free (b2);
79 }
80
81 static void
82 compare_sorts2 (void *base, size_t nel)
83 {
84         size_t len = nel * sizeof (teststruct_t*);
85         void *b1 = malloc (len);
86         void *b2 = malloc (len);
87
88         memcpy (b1, base, len);
89         memcpy (b2, base, len);
90
91         qsort (b1, nel, sizeof (teststruct_t*), compare_teststructs2);
92         qsort_test_struct ((teststruct_t **)b2, nel);
93
94         assert (!memcmp (b1, b2, len));
95
96         free (b1);
97         free (b2);
98 }
99 int
100 main (void)
101 {
102         int i;
103         for (i = 1; i < 4000; ++i) {
104                 int a [i];
105                 int j;
106
107                 for (j = 0; j < i; ++j)
108                         a [j] = i - j - 1;
109                 compare_sorts (a, i, sizeof (int), compare_ints);
110         }
111
112         srandom (time (NULL));
113         for (i = 0; i < 2000; ++i) {
114                 teststruct_t a [200];
115                 int j;
116                 for (j = 0; j < 200; ++j) {
117                         a [j].key = random ();
118                         a [j].val = random ();
119                 }
120
121                 compare_sorts (a, 200, sizeof (teststruct_t), compare_teststructs);
122         }
123
124         srandom (time (NULL));
125         for (i = 0; i < 2000; ++i) {
126                 teststruct_t a [200];
127                 teststruct_t *b [200];
128                 int j;
129                 for (j = 0; j < 200; ++j) {
130                         a [j].key = random ();
131                         a [j].val = random ();
132                         b [j] = &a[j];
133                 }
134
135                 compare_sorts2 (b, 200);
136         }
137         return 0;
138 }