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.
22 #include "metadata/sgen-gc.h"
30 compare_ints (const void *pa, const void *pb)
32 int a = *(const int*)pa;
33 int b = *(const int*)pb;
47 compare_teststructs (const void *pa, const void *pb)
49 int a = ((const teststruct_t*)pa)->key;
50 int b = ((const teststruct_t*)pb)->key;
59 compare_sorts (void *base, size_t nel, size_t width, int (*compar) (const void*, const void*))
61 size_t len = nel * width;
62 void *b1 = malloc (len);
63 void *b2 = malloc (len);
65 memcpy (b1, base, len);
66 memcpy (b2, base, len);
68 qsort (b1, nel, width, compar);
69 sgen_qsort (b2, nel, width, compar);
71 assert (!memcmp (b1, b2, len));
81 for (i = 0; i < 4000; ++i) {
85 for (j = 0; j < i; ++j)
87 compare_sorts (a, i, sizeof (int), compare_ints);
90 srandom (time (NULL));
91 for (i = 0; i < 2000; ++i) {
94 for (j = 0; j < 200; ++j) {
95 a [j].key = random ();
96 a [j].val = random ();
99 compare_sorts (a, 200, sizeof (teststruct_t), compare_teststructs);