1 #include "test/jemalloc_test.h"
2
3 static void
test_prng_lg_range_u32(bool atomic)4 test_prng_lg_range_u32(bool atomic)
5 {
6 uint32_t sa, sb, ra, rb;
7 unsigned lg_range;
8
9 sa = 42;
10 ra = prng_lg_range_u32(&sa, 32, atomic);
11 sa = 42;
12 rb = prng_lg_range_u32(&sa, 32, atomic);
13 assert_u32_eq(ra, rb,
14 "Repeated generation should produce repeated results");
15
16 sb = 42;
17 rb = prng_lg_range_u32(&sb, 32, atomic);
18 assert_u32_eq(ra, rb,
19 "Equivalent generation should produce equivalent results");
20
21 sa = 42;
22 ra = prng_lg_range_u32(&sa, 32, atomic);
23 rb = prng_lg_range_u32(&sa, 32, atomic);
24 assert_u32_ne(ra, rb,
25 "Full-width results must not immediately repeat");
26
27 sa = 42;
28 ra = prng_lg_range_u32(&sa, 32, atomic);
29 for (lg_range = 31; lg_range > 0; lg_range--) {
30 sb = 42;
31 rb = prng_lg_range_u32(&sb, lg_range, atomic);
32 assert_u32_eq((rb & (UINT32_C(0xffffffff) << lg_range)),
33 0, "High order bits should be 0, lg_range=%u", lg_range);
34 assert_u32_eq(rb, (ra >> (32 - lg_range)),
35 "Expected high order bits of full-width result, "
36 "lg_range=%u", lg_range);
37 }
38 }
39
40 static void
test_prng_lg_range_u64(void)41 test_prng_lg_range_u64(void)
42 {
43 uint64_t sa, sb, ra, rb;
44 unsigned lg_range;
45
46 sa = 42;
47 ra = prng_lg_range_u64(&sa, 64);
48 sa = 42;
49 rb = prng_lg_range_u64(&sa, 64);
50 assert_u64_eq(ra, rb,
51 "Repeated generation should produce repeated results");
52
53 sb = 42;
54 rb = prng_lg_range_u64(&sb, 64);
55 assert_u64_eq(ra, rb,
56 "Equivalent generation should produce equivalent results");
57
58 sa = 42;
59 ra = prng_lg_range_u64(&sa, 64);
60 rb = prng_lg_range_u64(&sa, 64);
61 assert_u64_ne(ra, rb,
62 "Full-width results must not immediately repeat");
63
64 sa = 42;
65 ra = prng_lg_range_u64(&sa, 64);
66 for (lg_range = 63; lg_range > 0; lg_range--) {
67 sb = 42;
68 rb = prng_lg_range_u64(&sb, lg_range);
69 assert_u64_eq((rb & (UINT64_C(0xffffffffffffffff) << lg_range)),
70 0, "High order bits should be 0, lg_range=%u", lg_range);
71 assert_u64_eq(rb, (ra >> (64 - lg_range)),
72 "Expected high order bits of full-width result, "
73 "lg_range=%u", lg_range);
74 }
75 }
76
77 static void
test_prng_lg_range_zu(bool atomic)78 test_prng_lg_range_zu(bool atomic)
79 {
80 size_t sa, sb, ra, rb;
81 unsigned lg_range;
82
83 sa = 42;
84 ra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);
85 sa = 42;
86 rb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);
87 assert_zu_eq(ra, rb,
88 "Repeated generation should produce repeated results");
89
90 sb = 42;
91 rb = prng_lg_range_zu(&sb, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);
92 assert_zu_eq(ra, rb,
93 "Equivalent generation should produce equivalent results");
94
95 sa = 42;
96 ra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);
97 rb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);
98 assert_zu_ne(ra, rb,
99 "Full-width results must not immediately repeat");
100
101 sa = 42;
102 ra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);
103 for (lg_range = (ZU(1) << (3 + LG_SIZEOF_PTR)) - 1; lg_range > 0;
104 lg_range--) {
105 sb = 42;
106 rb = prng_lg_range_zu(&sb, lg_range, atomic);
107 assert_zu_eq((rb & (SIZE_T_MAX << lg_range)),
108 0, "High order bits should be 0, lg_range=%u", lg_range);
109 assert_zu_eq(rb, (ra >> ((ZU(1) << (3 + LG_SIZEOF_PTR)) -
110 lg_range)), "Expected high order bits of full-width "
111 "result, lg_range=%u", lg_range);
112 }
113 }
114
TEST_BEGIN(test_prng_lg_range_u32_nonatomic)115 TEST_BEGIN(test_prng_lg_range_u32_nonatomic)
116 {
117
118 test_prng_lg_range_u32(false);
119 }
120 TEST_END
121
TEST_BEGIN(test_prng_lg_range_u32_atomic)122 TEST_BEGIN(test_prng_lg_range_u32_atomic)
123 {
124
125 test_prng_lg_range_u32(true);
126 }
127 TEST_END
128
TEST_BEGIN(test_prng_lg_range_u64_nonatomic)129 TEST_BEGIN(test_prng_lg_range_u64_nonatomic)
130 {
131
132 test_prng_lg_range_u64();
133 }
134 TEST_END
135
TEST_BEGIN(test_prng_lg_range_zu_nonatomic)136 TEST_BEGIN(test_prng_lg_range_zu_nonatomic)
137 {
138
139 test_prng_lg_range_zu(false);
140 }
141 TEST_END
142
TEST_BEGIN(test_prng_lg_range_zu_atomic)143 TEST_BEGIN(test_prng_lg_range_zu_atomic)
144 {
145
146 test_prng_lg_range_zu(true);
147 }
148 TEST_END
149
150 static void
test_prng_range_u32(bool atomic)151 test_prng_range_u32(bool atomic)
152 {
153 uint32_t range;
154 #define MAX_RANGE 10000000
155 #define RANGE_STEP 97
156 #define NREPS 10
157
158 for (range = 2; range < MAX_RANGE; range += RANGE_STEP) {
159 uint32_t s;
160 unsigned rep;
161
162 s = range;
163 for (rep = 0; rep < NREPS; rep++) {
164 uint32_t r = prng_range_u32(&s, range, atomic);
165
166 assert_u32_lt(r, range, "Out of range");
167 }
168 }
169 }
170
171 static void
test_prng_range_u64(void)172 test_prng_range_u64(void)
173 {
174 uint64_t range;
175 #define MAX_RANGE 10000000
176 #define RANGE_STEP 97
177 #define NREPS 10
178
179 for (range = 2; range < MAX_RANGE; range += RANGE_STEP) {
180 uint64_t s;
181 unsigned rep;
182
183 s = range;
184 for (rep = 0; rep < NREPS; rep++) {
185 uint64_t r = prng_range_u64(&s, range);
186
187 assert_u64_lt(r, range, "Out of range");
188 }
189 }
190 }
191
192 static void
test_prng_range_zu(bool atomic)193 test_prng_range_zu(bool atomic)
194 {
195 size_t range;
196 #define MAX_RANGE 10000000
197 #define RANGE_STEP 97
198 #define NREPS 10
199
200 for (range = 2; range < MAX_RANGE; range += RANGE_STEP) {
201 size_t s;
202 unsigned rep;
203
204 s = range;
205 for (rep = 0; rep < NREPS; rep++) {
206 size_t r = prng_range_zu(&s, range, atomic);
207
208 assert_zu_lt(r, range, "Out of range");
209 }
210 }
211 }
212
TEST_BEGIN(test_prng_range_u32_nonatomic)213 TEST_BEGIN(test_prng_range_u32_nonatomic)
214 {
215
216 test_prng_range_u32(false);
217 }
218 TEST_END
219
TEST_BEGIN(test_prng_range_u32_atomic)220 TEST_BEGIN(test_prng_range_u32_atomic)
221 {
222
223 test_prng_range_u32(true);
224 }
225 TEST_END
226
TEST_BEGIN(test_prng_range_u64_nonatomic)227 TEST_BEGIN(test_prng_range_u64_nonatomic)
228 {
229
230 test_prng_range_u64();
231 }
232 TEST_END
233
TEST_BEGIN(test_prng_range_zu_nonatomic)234 TEST_BEGIN(test_prng_range_zu_nonatomic)
235 {
236
237 test_prng_range_zu(false);
238 }
239 TEST_END
240
TEST_BEGIN(test_prng_range_zu_atomic)241 TEST_BEGIN(test_prng_range_zu_atomic)
242 {
243
244 test_prng_range_zu(true);
245 }
246 TEST_END
247
248 int
main(void)249 main(void)
250 {
251
252 return (test(
253 test_prng_lg_range_u32_nonatomic,
254 test_prng_lg_range_u32_atomic,
255 test_prng_lg_range_u64_nonatomic,
256 test_prng_lg_range_zu_nonatomic,
257 test_prng_lg_range_zu_atomic,
258 test_prng_range_u32_nonatomic,
259 test_prng_range_u32_atomic,
260 test_prng_range_u64_nonatomic,
261 test_prng_range_zu_nonatomic,
262 test_prng_range_zu_atomic));
263 }
264