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