ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
Revision: 1.15
Committed: Wed Dec 31 16:44:02 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +0 -1 lines
Log Message:
remove unused imports

File Contents

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