]> git.proxmox.com Git - rustc.git/blob - src/jemalloc/test/unit/prng.c
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / jemalloc / test / unit / prng.c
1 #include "test/jemalloc_test.h"
2
3 TEST_BEGIN(test_prng_lg_range)
4 {
5 uint64_t sa, sb, ra, rb;
6 unsigned lg_range;
7
8 sa = 42;
9 ra = prng_lg_range(&sa, 64);
10 sa = 42;
11 rb = prng_lg_range(&sa, 64);
12 assert_u64_eq(ra, rb,
13 "Repeated generation should produce repeated results");
14
15 sb = 42;
16 rb = prng_lg_range(&sb, 64);
17 assert_u64_eq(ra, rb,
18 "Equivalent generation should produce equivalent results");
19
20 sa = 42;
21 ra = prng_lg_range(&sa, 64);
22 rb = prng_lg_range(&sa, 64);
23 assert_u64_ne(ra, rb,
24 "Full-width results must not immediately repeat");
25
26 sa = 42;
27 ra = prng_lg_range(&sa, 64);
28 for (lg_range = 63; lg_range > 0; lg_range--) {
29 sb = 42;
30 rb = prng_lg_range(&sb, lg_range);
31 assert_u64_eq((rb & (UINT64_C(0xffffffffffffffff) << lg_range)),
32 0, "High order bits should be 0, lg_range=%u", lg_range);
33 assert_u64_eq(rb, (ra >> (64 - lg_range)),
34 "Expected high order bits of full-width result, "
35 "lg_range=%u", lg_range);
36 }
37 }
38 TEST_END
39
40 TEST_BEGIN(test_prng_range)
41 {
42 uint64_t range;
43 #define MAX_RANGE 10000000
44 #define RANGE_STEP 97
45 #define NREPS 10
46
47 for (range = 2; range < MAX_RANGE; range += RANGE_STEP) {
48 uint64_t s;
49 unsigned rep;
50
51 s = range;
52 for (rep = 0; rep < NREPS; rep++) {
53 uint64_t r = prng_range(&s, range);
54
55 assert_u64_lt(r, range, "Out of range");
56 }
57 }
58 }
59 TEST_END
60
61 int
62 main(void)
63 {
64
65 return (test(
66 test_prng_lg_range,
67 test_prng_range));
68 }