]>
git.saurik.com Git - apple/libc.git/blob - tests/qsort.c
2 * Randomized test for qsort() routine.
10 #include <darwintest.h>
11 #include <darwintest_utils.h>
13 #include "freebsd_qsort.h"
15 #define BUFLEN (32 * 1024)
17 static int tests
= 10;
19 T_DECL(qsort_random
, "qsort random test", T_META_CHECK_LEAKS(NO
))
21 char testvector
[BUFLEN
];
22 char sresvector
[BUFLEN
];
25 while (--tests
>= 0) {
26 size_t elmsize
= (tests
% 32) + 1;
27 size_t elmcount
= sizeof(testvector
) / elmsize
;
28 T_LOG("%d: size = %zu, count = %zu", tests
, elmsize
, elmcount
);
30 /* Populate test vectors */
31 arc4random_buf(testvector
, sizeof(testvector
));
32 memcpy(sresvector
, testvector
, sizeof(testvector
));
34 /* Sort using qsort(3) */
35 qsort_r(testvector
, elmcount
, elmsize
, (void*)elmsize
, szsorthelp
);
36 /* Sort using reference slow sorting routine */
37 szsort(sresvector
, elmcount
, elmsize
);
40 for (i
= 0; i
< elmcount
; i
++){
41 if (memcmp(testvector
+ (i
* elmsize
), sresvector
+ (i
* elmsize
), elmsize
) != 0) {
42 T_LOG("testvector =");
43 dt_print_hex_dump((uint8_t*)testvector
, sizeof(testvector
));
44 T_LOG("sresvector =");
45 dt_print_hex_dump((uint8_t*)sresvector
, sizeof(sresvector
));
46 T_ASSERT_FAIL("%d: item at index %zd should match", tests
, i
);
51 T_PASS("%d: Sorted successfully.", tests
);