Fix vsyslog namespace (bug 18533).
[glibc.git] / stdlib / tst-qsort2.c
blob10d16852b07c27babc9cb167630a3e1d962bcd3c
1 #include <stdio.h>
2 #include <stdlib.h>
4 char *array;
5 char *array_end;
6 size_t member_size;
8 int
9 compare (const void *a1, const void *b1)
11 const char *a = a1;
12 const char *b = b1;
14 if (! (array <= a && a < array_end
15 && array <= b && b < array_end))
17 puts ("compare arguments not inside of the array");
18 exit (EXIT_FAILURE);
20 int ret = b[0] - a[0];
21 if (ret)
22 return ret;
23 if (member_size > 1)
24 return b[1] - a[1];
25 return 0;
28 int
29 test (size_t nmemb, size_t size)
31 array = malloc (nmemb * size);
32 if (array == NULL)
34 printf ("%zd x %zd: no memory", nmemb, size);
35 return 1;
38 array_end = array + nmemb * size;
39 member_size = size;
41 char *p;
42 size_t i;
43 size_t bias = random ();
44 for (i = 0, p = array; i < nmemb; i++, p += size)
46 p[0] = (char) (i + bias);
47 if (size > 1)
48 p[1] = (char) ((i + bias) >> 8);
51 qsort (array, nmemb, size, compare);
53 for (i = 0, p = array; i < nmemb - 1; i++, p += size)
55 if (p[0] < p[size]
56 || (size > 1 && p[0] == p[size] && p[1] < p[size + 1]))
58 printf ("%zd x %zd: failure at offset %zd\n", nmemb,
59 size, i);
60 free (array);
61 return 1;
65 free (array);
66 return 0;
69 int
70 main (int argc, char **argv)
72 int ret = 0;
73 if (argc >= 3)
74 ret |= test (atoi (argv[1]), atoi (argv[2]));
75 else
77 ret |= test (10000, 1);
78 ret |= test (200000, 2);
79 ret |= test (2000000, 3);
80 ret |= test (2132310, 4);
81 ret |= test (1202730, 7);
82 ret |= test (1184710, 8);
83 ret |= test (272710, 12);
84 ret |= test (14170, 32);
85 ret |= test (4170, 320);
88 return ret;