7 static int scmp(const void *a
, const void *b
)
9 return strcmp(*(char **)a
, *(char **)b
);
12 static int icmp(const void *a
, const void *b
)
14 return *(int*)a
- *(int*)b
;
17 static int ccmp(const void *a
, const void *b
)
19 return *(char*)a
- *(char*)b
;
22 static int cmp64(const void *a
, const void *b
)
24 const uint64_t *ua
= a
, *ub
= b
;
25 return *ua
< *ub
? -1 : *ua
!= *ub
;
28 /* 26 items -- even */
29 static const char *s
[] = {
30 "Bob", "Alice", "John", "Ceres",
31 "Helga", "Drepper", "Emeralda", "Zoran",
32 "Momo", "Frank", "Pema", "Xavier",
33 "Yeva", "Gedun", "Irina", "Nono",
34 "Wiener", "Vincent", "Tsering", "Karnica",
35 "Lulu", "Quincy", "Osama", "Riley",
38 static const char *s_sorted
[] = {
39 "Alice", "Bob", "Ceres", "Drepper",
40 "Emeralda", "Frank", "Gedun", "Helga",
41 "Irina", "John", "Karnica", "Lulu",
42 "Momo", "Nono", "Osama", "Pema",
43 "Quincy", "Riley", "Sam", "Tsering",
44 "Ursula", "Vincent", "Wiener", "Xavier",
48 /* 23 items -- odd, prime */
50 879045, 394, 99405644, 33434, 232323, 4334, 5454,
51 343, 45545, 454, 324, 22, 34344, 233, 45345, 343,
52 848405, 3434, 3434344, 3535, 93994, 2230404, 4334
54 static int n_sorted
[] = {
55 22, 233, 324, 343, 343, 394, 454, 3434,
56 3535, 4334, 4334, 5454, 33434, 34344, 45345, 45545,
57 93994, 232323, 848405, 879045, 2230404, 3434344, 99405644
60 static void str_test(const char **a
, const char **a_sorted
, int len
)
63 qsort(a
, len
, sizeof *a
, scmp
);
64 for (i
=0; i
<len
; i
++) {
65 if (strcmp(a
[i
], a_sorted
[i
]) != 0) {
66 t_error("string sort failed at index %d\n", i
);
67 t_printf("\ti\tgot\twant\n");
69 t_printf("\t%d\t%s\t%s\n", i
, a
[i
], a_sorted
[i
]);
75 static void int_test(int *a
, int *a_sorted
, int len
)
78 qsort(a
, len
, sizeof *a
, icmp
);
79 for (i
=0; i
<len
; i
++) {
80 if (a
[i
] != a_sorted
[i
]) {
81 t_error("integer sort failed at index %d\n", i
);
82 t_printf("\ti\tgot\twant\n");
84 t_printf("\t%d\t%d\t%d\n", i
, a
[i
], a_sorted
[i
]);
90 static void uint64_gen(uint64_t *p
, uint64_t *p_sorted
, int n
)
95 for (i
= 0; i
< n
; i
++) {
99 memcpy(p_sorted
, p
, n
* sizeof *p
);
103 static void uint64_test(uint64_t *a
, uint64_t *a_sorted
, int len
)
106 qsort(a
, len
, sizeof *a
, cmp64
);
107 for (i
=0; i
<len
; i
++) {
108 if (a
[i
] != a_sorted
[i
]) {
109 t_error("uint64 sort failed at index %d\n", i
);
110 t_printf("\ti\tgot\twant\n");
111 for (i
=0; i
<len
; i
++)
112 t_printf("\t%d\t%" PRIu64
"\t%" PRIu64
"\n", i
, a
[i
], a_sorted
[i
]);
118 #define T(a, a_sorted) do { \
120 qsort(p, sizeof p - 1, 1, ccmp); \
121 if (memcmp(p, a_sorted, sizeof p) != 0) { \
122 t_error("character sort failed\n"); \
123 t_printf("\tgot: \"%s\"\n", p); \
124 t_printf("\twant: \"%s\"\n", a_sorted); \
128 static void char_test(void)
150 T("261435", "123456");
151 T("4517263", "1234567");
152 T("37245618", "12345678");
153 T("812436597", "123456789");
154 T("987654321", "123456789");
155 T("321321321", "111222333");
156 T("49735862185236174", "11223344556677889");
163 str_test(s
, s_sorted
, sizeof s
/sizeof*s
);
164 int_test(n
, n_sorted
, sizeof n
/sizeof*n
);
166 for (i
= 1023; i
<=1026; i
++) {
167 uint64_t p
[1026], p_sorted
[1026];
168 uint64_gen(p
, p_sorted
, i
);
169 uint64_test(p
, p_sorted
, i
);