]> git.saurik.com Git - apple/libc.git/blob - tests/qsort.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / qsort.c
1 /*
2 * Randomized test for qsort() routine.
3 */
4
5 #include <sys/cdefs.h>
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <darwintest.h>
11 #include <darwintest_utils.h>
12
13 #include "freebsd_qsort.h"
14
15 #define BUFLEN (32 * 1024)
16
17 static int tests = 10;
18
19 T_DECL(qsort_random, "qsort random test", T_META_CHECK_LEAKS(NO))
20 {
21 char testvector[BUFLEN];
22 char sresvector[BUFLEN];
23 size_t i;
24
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);
29
30 /* Populate test vectors */
31 arc4random_buf(testvector, sizeof(testvector));
32 memcpy(sresvector, testvector, sizeof(testvector));
33
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);
38
39 /* Compare results */
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);
47 break;
48 }
49 }
50 if (i == elmcount) {
51 T_PASS("%d: Sorted successfully.", tests);
52 }
53 }
54 }