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.
13 #include <mono/sgen/sgen-gc.h>
14 #include <mono/sgen/sgen-qsort.h>
22 compare_ints (const void *pa
, const void *pb
)
24 int a
= *(const int*)pa
;
25 int b
= *(const int*)pb
;
39 compare_teststructs (const void *pa
, const void *pb
)
41 int a
= ((const teststruct_t
*)pa
)->key
;
42 int b
= ((const teststruct_t
*)pb
)->key
;
51 compare_teststructs2 (const void *pa
, const void *pb
)
53 int a
= (*((const teststruct_t
**)pa
))->key
;
54 int b
= (*((const teststruct_t
**)pb
))->key
;
62 DEF_QSORT_INLINE(test_struct
, teststruct_t
*, compare_teststructs
)
65 compare_sorts (void *base
, size_t nel
, size_t width
, int (*compar
) (const void*, const void*))
67 size_t len
= nel
* width
;
68 void *b1
= malloc (len
);
69 void *b2
= malloc (len
);
71 memcpy (b1
, base
, len
);
72 memcpy (b2
, base
, len
);
74 mono_qsort (b1
, nel
, width
, compar
);
75 sgen_qsort (b2
, nel
, width
, compar
);
77 /* We can't assert that qsort and sgen_qsort produce the same results
78 * because qsort is not guaranteed to be stable, so they will tend to differ
79 * in adjacent equal elements. Instead, we assert that the array is sorted
80 * according to the comparator.
82 for (size_t i
= 0; i
< nel
- 1; ++i
)
83 assert (compar ((char *)b2
+ i
* width
, (char *)b2
+ (i
+ 1) * width
) <= 0);
90 compare_sorts2 (void *base
, size_t nel
)
92 size_t width
= sizeof (teststruct_t
*);
93 size_t len
= nel
* width
;
94 void *b1
= malloc (len
);
95 void *b2
= malloc (len
);
97 memcpy (b1
, base
, len
);
98 memcpy (b2
, base
, len
);
100 qsort (b1
, nel
, sizeof (teststruct_t
*), compare_teststructs2
);
101 qsort_test_struct ((teststruct_t
**)b2
, nel
);
103 for (size_t i
= 0; i
< nel
- 1; ++i
)
104 assert (compare_teststructs2 ((char *)b2
+ i
* width
, (char *)b2
+ (i
+ 1) * width
) <= 0);
114 test_sgen_qsort_main (void);
117 test_sgen_qsort_main (void)
120 for (i
= 1; i
< 4000; ++i
) {
124 for (j
= 0; j
< i
; ++j
)
126 compare_sorts (a
, i
, sizeof (int), compare_ints
);
129 srandom (time (NULL
));
130 for (i
= 0; i
< 2000; ++i
) {
131 teststruct_t a
[200];
133 for (j
= 0; j
< 200; ++j
) {
134 a
[j
].key
= random ();
135 a
[j
].val
= random ();
138 compare_sorts (a
, 200, sizeof (teststruct_t
), compare_teststructs
);
141 srandom (time (NULL
));
142 for (i
= 0; i
< 2000; ++i
) {
143 teststruct_t a
[200];
144 teststruct_t
*b
[200];
146 for (j
= 0; j
< 200; ++j
) {
147 a
[j
].key
= random ();
148 a
[j
].val
= random ();
152 compare_sorts2 (b
, 200);