1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
import junit.framework.*; |
7 |
import java.util.*; |
8 |
import java.util.SplittableRandom; |
9 |
import java.util.concurrent.atomic.AtomicInteger; |
10 |
import java.util.concurrent.atomic.AtomicLong; |
11 |
import java.util.concurrent.atomic.LongAdder; |
12 |
|
13 |
public class SplittableRandomTest extends JSR166TestCase { |
14 |
|
15 |
public static void main(String[] args) { |
16 |
junit.textui.TestRunner.run(suite()); |
17 |
} |
18 |
public static Test suite() { |
19 |
return new TestSuite(SplittableRandomTest.class); |
20 |
} |
21 |
|
22 |
/* |
23 |
* Testing coverage notes: |
24 |
* |
25 |
* 1. Many of the test methods are adapted from ThreadLocalRandomTest. |
26 |
* |
27 |
* 2. These tests do not check for random number generator quality. |
28 |
* But we check for minimal API compliance by requiring that |
29 |
* repeated calls to nextX methods, up to NCALLS tries, produce at |
30 |
* least two distinct results. (In some possible universe, a |
31 |
* "correct" implementation might fail, but the odds are vastly |
32 |
* less than that of encountering a hardware failure while running |
33 |
* the test.) For bounded nextX methods, we sample various |
34 |
* intervals across multiples of primes. In other tests, we repeat |
35 |
* under REPS different values. |
36 |
*/ |
37 |
|
38 |
// max numbers of calls to detect getting stuck on one value |
39 |
static final int NCALLS = 10000; |
40 |
|
41 |
// max sampled int bound |
42 |
static final int MAX_INT_BOUND = (1 << 28); |
43 |
|
44 |
// max sampled long bound |
45 |
static final long MAX_LONG_BOUND = (1L << 42); |
46 |
|
47 |
// Number of replications for other checks |
48 |
static final int REPS = |
49 |
Integer.getInteger("SplittableRandomTest.reps", 4); |
50 |
|
51 |
/** |
52 |
* Repeated calls to nextInt produce at least two distinct results |
53 |
*/ |
54 |
public void testNextInt() { |
55 |
SplittableRandom sr = new SplittableRandom(); |
56 |
int f = sr.nextInt(); |
57 |
int i = 0; |
58 |
while (i < NCALLS && sr.nextInt() == f) |
59 |
++i; |
60 |
assertTrue(i < NCALLS); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Repeated calls to nextLong produce at least two distinct results |
65 |
*/ |
66 |
public void testNextLong() { |
67 |
SplittableRandom sr = new SplittableRandom(); |
68 |
long f = sr.nextLong(); |
69 |
int i = 0; |
70 |
while (i < NCALLS && sr.nextLong() == f) |
71 |
++i; |
72 |
assertTrue(i < NCALLS); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Repeated calls to nextDouble produce at least two distinct results |
77 |
*/ |
78 |
public void testNextDouble() { |
79 |
SplittableRandom sr = new SplittableRandom(); |
80 |
double f = sr.nextDouble(); |
81 |
int i = 0; |
82 |
while (i < NCALLS && sr.nextDouble() == f) |
83 |
++i; |
84 |
assertTrue(i < NCALLS); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Two SplittableRandoms created with the same seed produce the |
89 |
* same values for nextLong. |
90 |
*/ |
91 |
public void testSeedConstructor() { |
92 |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
93 |
SplittableRandom sr1 = new SplittableRandom(seed); |
94 |
SplittableRandom sr2 = new SplittableRandom(seed); |
95 |
for (int i = 0; i < REPS; ++i) |
96 |
assertEquals(sr1.nextLong(), sr2.nextLong()); |
97 |
} |
98 |
} |
99 |
|
100 |
/** |
101 |
* A SplittableRandom produced by split() of a default-constructed |
102 |
* SplittableRandom generates a different sequence |
103 |
*/ |
104 |
public void testSplit1() { |
105 |
SplittableRandom sr = new SplittableRandom(); |
106 |
for (int reps = 0; reps < REPS; ++reps) { |
107 |
SplittableRandom sc = sr.split(); |
108 |
int i = 0; |
109 |
while (i < NCALLS && sr.nextLong() == sc.nextLong()) |
110 |
++i; |
111 |
assertTrue(i < NCALLS); |
112 |
} |
113 |
} |
114 |
|
115 |
/** |
116 |
* A SplittableRandom produced by split() of a seeded-constructed |
117 |
* SplittableRandom generates a different sequence |
118 |
*/ |
119 |
public void testSplit2() { |
120 |
SplittableRandom sr = new SplittableRandom(12345); |
121 |
for (int reps = 0; reps < REPS; ++reps) { |
122 |
SplittableRandom sc = sr.split(); |
123 |
int i = 0; |
124 |
while (i < NCALLS && sr.nextLong() == sc.nextLong()) |
125 |
++i; |
126 |
assertTrue(i < NCALLS); |
127 |
} |
128 |
} |
129 |
|
130 |
/** |
131 |
* nextInt(negative) throws IllegalArgumentException |
132 |
*/ |
133 |
public void testNextIntBoundedNeg() { |
134 |
SplittableRandom sr = new SplittableRandom(); |
135 |
try { |
136 |
int f = sr.nextInt(-17); |
137 |
shouldThrow(); |
138 |
} catch (IllegalArgumentException success) {} |
139 |
} |
140 |
|
141 |
/** |
142 |
* nextInt(least >= bound) throws IllegalArgumentException |
143 |
*/ |
144 |
public void testNextIntBadBounds() { |
145 |
SplittableRandom sr = new SplittableRandom(); |
146 |
try { |
147 |
int f = sr.nextInt(17, 2); |
148 |
shouldThrow(); |
149 |
} catch (IllegalArgumentException success) {} |
150 |
} |
151 |
|
152 |
/** |
153 |
* nextInt(bound) returns 0 <= value < bound; |
154 |
* repeated calls produce at least two distinct results |
155 |
*/ |
156 |
public void testNextIntBounded() { |
157 |
SplittableRandom sr = new SplittableRandom(); |
158 |
// sample bound space across prime number increments |
159 |
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { |
160 |
int f = sr.nextInt(bound); |
161 |
assertTrue(0 <= f && f < bound); |
162 |
int i = 0; |
163 |
int j; |
164 |
while (i < NCALLS && |
165 |
(j = sr.nextInt(bound)) == f) { |
166 |
assertTrue(0 <= j && j < bound); |
167 |
++i; |
168 |
} |
169 |
assertTrue(i < NCALLS); |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* nextInt(least, bound) returns least <= value < bound; |
175 |
* repeated calls produce at least two distinct results |
176 |
*/ |
177 |
public void testNextIntBounded2() { |
178 |
SplittableRandom sr = new SplittableRandom(); |
179 |
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { |
180 |
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) { |
181 |
int f = sr.nextInt(least, bound); |
182 |
assertTrue(least <= f && f < bound); |
183 |
int i = 0; |
184 |
int j; |
185 |
while (i < NCALLS && |
186 |
(j = sr.nextInt(least, bound)) == f) { |
187 |
assertTrue(least <= j && j < bound); |
188 |
++i; |
189 |
} |
190 |
assertTrue(i < NCALLS); |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
/** |
196 |
* nextLong(negative) throws IllegalArgumentException |
197 |
*/ |
198 |
public void testNextLongBoundedNeg() { |
199 |
SplittableRandom sr = new SplittableRandom(); |
200 |
try { |
201 |
long f = sr.nextLong(-17); |
202 |
shouldThrow(); |
203 |
} catch (IllegalArgumentException success) {} |
204 |
} |
205 |
|
206 |
/** |
207 |
* nextLong(least >= bound) throws IllegalArgumentException |
208 |
*/ |
209 |
public void testNextLongBadBounds() { |
210 |
SplittableRandom sr = new SplittableRandom(); |
211 |
try { |
212 |
long f = sr.nextLong(17, 2); |
213 |
shouldThrow(); |
214 |
} catch (IllegalArgumentException success) {} |
215 |
} |
216 |
|
217 |
/** |
218 |
* nextLong(bound) returns 0 <= value < bound; |
219 |
* repeated calls produce at least two distinct results |
220 |
*/ |
221 |
public void testNextLongBounded() { |
222 |
SplittableRandom sr = new SplittableRandom(); |
223 |
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
224 |
long f = sr.nextLong(bound); |
225 |
assertTrue(0 <= f && f < bound); |
226 |
int i = 0; |
227 |
long j; |
228 |
while (i < NCALLS && |
229 |
(j = sr.nextLong(bound)) == f) { |
230 |
assertTrue(0 <= j && j < bound); |
231 |
++i; |
232 |
} |
233 |
assertTrue(i < NCALLS); |
234 |
} |
235 |
} |
236 |
|
237 |
/** |
238 |
* nextLong(least, bound) returns least <= value < bound; |
239 |
* repeated calls produce at least two distinct results |
240 |
*/ |
241 |
public void testNextLongBounded2() { |
242 |
SplittableRandom sr = new SplittableRandom(); |
243 |
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { |
244 |
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
245 |
long f = sr.nextLong(least, bound); |
246 |
assertTrue(least <= f && f < bound); |
247 |
int i = 0; |
248 |
long j; |
249 |
while (i < NCALLS && |
250 |
(j = sr.nextLong(least, bound)) == f) { |
251 |
assertTrue(least <= j && j < bound); |
252 |
++i; |
253 |
} |
254 |
assertTrue(i < NCALLS); |
255 |
} |
256 |
} |
257 |
} |
258 |
|
259 |
/** |
260 |
* nextDouble(least, bound) returns least <= value < bound; |
261 |
* repeated calls produce at least two distinct results |
262 |
*/ |
263 |
public void testNextDoubleBounded2() { |
264 |
SplittableRandom sr = new SplittableRandom(); |
265 |
for (double least = 0.0001; least < 1.0e20; least *= 8) { |
266 |
for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) { |
267 |
double f = sr.nextDouble(least, bound); |
268 |
assertTrue(least <= f && f < bound); |
269 |
int i = 0; |
270 |
double j; |
271 |
while (i < NCALLS && |
272 |
(j = sr.nextDouble(least, bound)) == f) { |
273 |
assertTrue(least <= j && j < bound); |
274 |
++i; |
275 |
} |
276 |
assertTrue(i < NCALLS); |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
/** |
282 |
* Invoking sized ints, long, doubles, with negative sizes throws |
283 |
* IllegalArgumentException |
284 |
*/ |
285 |
public void testBadStreamSize() { |
286 |
SplittableRandom r = new SplittableRandom(); |
287 |
try { |
288 |
java.util.stream.IntStream x = r.ints(-1L); |
289 |
shouldThrow(); |
290 |
} catch (IllegalArgumentException success) {} |
291 |
try { |
292 |
java.util.stream.IntStream x = r.ints(-1L, 2, 3); |
293 |
shouldThrow(); |
294 |
} catch (IllegalArgumentException success) {} |
295 |
try { |
296 |
java.util.stream.LongStream x = r.longs(-1L); |
297 |
shouldThrow(); |
298 |
} catch (IllegalArgumentException success) {} |
299 |
try { |
300 |
java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); |
301 |
shouldThrow(); |
302 |
} catch (IllegalArgumentException success) {} |
303 |
try { |
304 |
java.util.stream.DoubleStream x = r.doubles(-1L); |
305 |
shouldThrow(); |
306 |
} catch (IllegalArgumentException success) {} |
307 |
try { |
308 |
java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); |
309 |
shouldThrow(); |
310 |
} catch (IllegalArgumentException success) {} |
311 |
} |
312 |
|
313 |
/** |
314 |
* Invoking bounded ints, long, doubles, with illegal bounds throws |
315 |
* IllegalArgumentException |
316 |
*/ |
317 |
public void testBadStreamBounds() { |
318 |
SplittableRandom r = new SplittableRandom(); |
319 |
try { |
320 |
java.util.stream.IntStream x = r.ints(2, 1); |
321 |
shouldThrow(); |
322 |
} catch (IllegalArgumentException success) {} |
323 |
try { |
324 |
java.util.stream.IntStream x = r.ints(10, 42, 42); |
325 |
shouldThrow(); |
326 |
} catch (IllegalArgumentException success) {} |
327 |
try { |
328 |
java.util.stream.LongStream x = r.longs(-1L, -1L); |
329 |
shouldThrow(); |
330 |
} catch (IllegalArgumentException success) {} |
331 |
try { |
332 |
java.util.stream.LongStream x = r.longs(10, 1L, -2L); |
333 |
shouldThrow(); |
334 |
} catch (IllegalArgumentException success) {} |
335 |
try { |
336 |
java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); |
337 |
shouldThrow(); |
338 |
} catch (IllegalArgumentException success) {} |
339 |
try { |
340 |
java.util.stream.DoubleStream x = r.doubles(10, .5, .4); |
341 |
shouldThrow(); |
342 |
} catch (IllegalArgumentException success) {} |
343 |
} |
344 |
|
345 |
/** |
346 |
* A parallel sized stream of ints generates the given number of values |
347 |
*/ |
348 |
public void testIntsCount() { |
349 |
LongAdder counter = new LongAdder(); |
350 |
SplittableRandom r = new SplittableRandom(); |
351 |
long size = 0; |
352 |
for (int reps = 0; reps < REPS; ++reps) { |
353 |
counter.reset(); |
354 |
r.ints(size).parallel().forEach(x -> counter.increment()); |
355 |
assertEquals(size, counter.sum()); |
356 |
size += 524959; |
357 |
} |
358 |
} |
359 |
|
360 |
/** |
361 |
* A parallel sized stream of longs generates the given number of values |
362 |
*/ |
363 |
public void testLongsCount() { |
364 |
LongAdder counter = new LongAdder(); |
365 |
SplittableRandom r = new SplittableRandom(); |
366 |
long size = 0; |
367 |
for (int reps = 0; reps < REPS; ++reps) { |
368 |
counter.reset(); |
369 |
r.longs(size).parallel().forEach(x -> counter.increment()); |
370 |
assertEquals(size, counter.sum()); |
371 |
size += 524959; |
372 |
} |
373 |
} |
374 |
|
375 |
/** |
376 |
* A parallel sized stream of doubles generates the given number of values |
377 |
*/ |
378 |
public void testDoublesCount() { |
379 |
LongAdder counter = new LongAdder(); |
380 |
SplittableRandom r = new SplittableRandom(); |
381 |
long size = 0; |
382 |
for (int reps = 0; reps < REPS; ++reps) { |
383 |
counter.reset(); |
384 |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
385 |
assertEquals(size, counter.sum()); |
386 |
size += 524959; |
387 |
} |
388 |
} |
389 |
|
390 |
/** |
391 |
* Each of a parallel sized stream of bounded ints is within bounds |
392 |
*/ |
393 |
public void testBoundedInts() { |
394 |
AtomicInteger fails = new AtomicInteger(0); |
395 |
SplittableRandom r = new SplittableRandom(); |
396 |
long size = 12345L; |
397 |
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
398 |
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
399 |
final int lo = least, hi = bound; |
400 |
r.ints(size, lo, hi).parallel(). |
401 |
forEach(x -> {if (x < lo || x >= hi) |
402 |
fails.getAndIncrement(); }); |
403 |
} |
404 |
} |
405 |
assertEquals(0, fails.get()); |
406 |
} |
407 |
|
408 |
/** |
409 |
* Each of a parallel sized stream of bounded longs is within bounds |
410 |
*/ |
411 |
public void testBoundedLongs() { |
412 |
AtomicInteger fails = new AtomicInteger(0); |
413 |
SplittableRandom r = new SplittableRandom(); |
414 |
long size = 123L; |
415 |
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
416 |
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
417 |
final long lo = least, hi = bound; |
418 |
r.longs(size, lo, hi).parallel(). |
419 |
forEach(x -> {if (x < lo || x >= hi) |
420 |
fails.getAndIncrement(); }); |
421 |
} |
422 |
} |
423 |
assertEquals(0, fails.get()); |
424 |
} |
425 |
|
426 |
/** |
427 |
* Each of a parallel sized stream of bounded doubles is within bounds |
428 |
*/ |
429 |
public void testBoundedDoubles() { |
430 |
AtomicInteger fails = new AtomicInteger(0); |
431 |
SplittableRandom r = new SplittableRandom(); |
432 |
long size = 456; |
433 |
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
434 |
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
435 |
final double lo = least, hi = bound; |
436 |
r.doubles(size, lo, hi).parallel(). |
437 |
forEach(x -> {if (x < lo || x >= hi) |
438 |
fails.getAndIncrement(); }); |
439 |
} |
440 |
} |
441 |
assertEquals(0, fails.get()); |
442 |
} |
443 |
|
444 |
/** |
445 |
* A parallel unsized stream of ints generates at least 100 values |
446 |
*/ |
447 |
public void testUnsizedIntsCount() { |
448 |
LongAdder counter = new LongAdder(); |
449 |
SplittableRandom r = new SplittableRandom(); |
450 |
long size = 100; |
451 |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
452 |
assertEquals(size, counter.sum()); |
453 |
} |
454 |
|
455 |
/** |
456 |
* A parallel unsized stream of longs generates at least 100 values |
457 |
*/ |
458 |
public void testUnsizedLongsCount() { |
459 |
LongAdder counter = new LongAdder(); |
460 |
SplittableRandom r = new SplittableRandom(); |
461 |
long size = 100; |
462 |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
463 |
assertEquals(size, counter.sum()); |
464 |
} |
465 |
|
466 |
/** |
467 |
* A parallel unsized stream of doubles generates at least 100 values |
468 |
*/ |
469 |
public void testUnsizedDoublesCount() { |
470 |
LongAdder counter = new LongAdder(); |
471 |
SplittableRandom r = new SplittableRandom(); |
472 |
long size = 100; |
473 |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
474 |
assertEquals(size, counter.sum()); |
475 |
} |
476 |
|
477 |
/** |
478 |
* A sequential unsized stream of ints generates at least 100 values |
479 |
*/ |
480 |
public void testUnsizedIntsCountSeq() { |
481 |
LongAdder counter = new LongAdder(); |
482 |
SplittableRandom r = new SplittableRandom(); |
483 |
long size = 100; |
484 |
r.ints().limit(size).forEach(x -> counter.increment()); |
485 |
assertEquals(size, counter.sum()); |
486 |
} |
487 |
|
488 |
/** |
489 |
* A sequential unsized stream of longs generates at least 100 values |
490 |
*/ |
491 |
public void testUnsizedLongsCountSeq() { |
492 |
LongAdder counter = new LongAdder(); |
493 |
SplittableRandom r = new SplittableRandom(); |
494 |
long size = 100; |
495 |
r.longs().limit(size).forEach(x -> counter.increment()); |
496 |
assertEquals(size, counter.sum()); |
497 |
} |
498 |
|
499 |
/** |
500 |
* A sequential unsized stream of doubles generates at least 100 values |
501 |
*/ |
502 |
public void testUnsizedDoublesCountSeq() { |
503 |
LongAdder counter = new LongAdder(); |
504 |
SplittableRandom r = new SplittableRandom(); |
505 |
long size = 100; |
506 |
r.doubles().limit(size).forEach(x -> counter.increment()); |
507 |
assertEquals(size, counter.sum()); |
508 |
} |
509 |
|
510 |
} |