]>
git.saurik.com Git - apple/libc.git/blob - tests/psort.c
4 #include <mach/clock_types.h>
6 #include <darwintest.h>
8 typedef unsigned long T
;
11 comparT(const void* a
, const void* b
) {
12 const T x
= *(T
*)a
, y
= *(T
*)b
;
13 return x
< y
? -1 : x
> y
? 1 : 0;
16 T_DECL(psort
, "psort(3)")
18 struct timeval tv_start
, tv_stop
;
19 struct rusage ru_start
, ru_stop
;
20 unsigned long pwt
, put
, qwt
, qut
;
23 const size_t nel
= 20480000;
24 const size_t width
= sizeof(T
), bufsiz
= nel
* width
;
27 arc4random_buf(buf
, bufsiz
);
28 sorted
= malloc(bufsiz
);
29 memcpy(sorted
, buf
, bufsiz
);
31 getrusage(RUSAGE_SELF
, &ru_start
);
32 gettimeofday(&tv_start
, NULL
);
33 psort(sorted
, nel
, width
, comparT
);
34 gettimeofday(&tv_stop
, NULL
);
35 getrusage(RUSAGE_SELF
, &ru_stop
);
37 pwt
= ((uint64_t)tv_stop
.tv_sec
* USEC_PER_SEC
+ tv_stop
.tv_usec
) -
38 ((uint64_t)tv_start
.tv_sec
* USEC_PER_SEC
+ tv_start
.tv_usec
);
39 put
= ((uint64_t)ru_stop
.ru_utime
.tv_sec
* USEC_PER_SEC
+ ru_stop
.ru_utime
.tv_usec
) -
40 ((uint64_t)ru_start
.ru_utime
.tv_sec
* USEC_PER_SEC
+ ru_start
.ru_utime
.tv_usec
);
42 getrusage(RUSAGE_SELF
, &ru_start
);
43 gettimeofday(&tv_start
, NULL
);
44 qsort(buf
, nel
, width
, comparT
);
45 gettimeofday(&tv_stop
, NULL
);
46 getrusage(RUSAGE_SELF
, &ru_stop
);
48 qwt
= ((uint64_t)tv_stop
.tv_sec
* USEC_PER_SEC
+ tv_stop
.tv_usec
) -
49 ((uint64_t)tv_start
.tv_sec
* USEC_PER_SEC
+ tv_start
.tv_usec
);
50 qut
= ((uint64_t)ru_stop
.ru_utime
.tv_sec
* USEC_PER_SEC
+ ru_stop
.ru_utime
.tv_usec
) -
51 ((uint64_t)ru_start
.ru_utime
.tv_sec
* USEC_PER_SEC
+ ru_start
.ru_utime
.tv_usec
);
54 for (size_t i
= 0; i
< nel
; i
++) {
55 if (!(match
= (buf
[i
] == sorted
[i
]))) break;
61 T_MAYFAIL
; T_EXPECT_LE((double)pwt
/qwt
, 1.0, "psort/qsort wall time");
62 T_MAYFAIL
; T_EXPECT_LE((double)qut
/put
, 1.0, "qsort/psort user time");
63 T_EXPECT_TRUE(match
, "psort matches qsort");