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