ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
Revision: 1.9
Committed: Tue Sep 24 06:35:35 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +31 -12 lines
Log Message:
Add more bad bounds tests

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.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 jsr166 1.4 * 1. Many of the test methods are adapted from ThreadLocalRandomTest.
26 dl 1.1 *
27 jsr166 1.4 * 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 dl 1.1 */
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 jsr166 1.4 // max sampled long bound
45 dl 1.1 static final long MAX_LONG_BOUND = (1L << 42);
46    
47     // Number of replications for other checks
48 jsr166 1.7 static final int REPS =
49     Integer.getInteger("SplittableRandomTest.reps", 4);
50 dl 1.1
51     /**
52 jsr166 1.4 * Repeated calls to nextInt produce at least two distinct results
53 dl 1.1 */
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 jsr166 1.4 * Repeated calls to nextLong produce at least two distinct results
65 dl 1.1 */
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 jsr166 1.4 * Repeated calls to nextDouble produce at least two distinct results
77 dl 1.1 */
78     public void testNextDouble() {
79     SplittableRandom sr = new SplittableRandom();
80     double f = sr.nextDouble();
81 jsr166 1.4 int i = 0;
82 dl 1.1 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 jsr166 1.3 for (int i = 0; i < REPS; ++i)
96 dl 1.1 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 jsr166 1.9 * nextInt(non-positive) throws IllegalArgumentException
132 dl 1.1 */
133 jsr166 1.9 public void testNextIntNonPositive() {
134 dl 1.1 SplittableRandom sr = new SplittableRandom();
135 jsr166 1.9 Runnable[] throwingActions = {
136     () -> sr.nextInt(-17),
137     () -> sr.nextInt(0),
138     () -> sr.nextInt(Integer.MIN_VALUE),
139     };
140     assertThrows(IllegalArgumentException.class, throwingActions);
141 dl 1.1 }
142    
143     /**
144 jsr166 1.4 * nextInt(least >= bound) throws IllegalArgumentException
145 dl 1.1 */
146     public void testNextIntBadBounds() {
147     SplittableRandom sr = new SplittableRandom();
148     try {
149     int f = sr.nextInt(17, 2);
150     shouldThrow();
151     } catch (IllegalArgumentException success) {}
152     }
153    
154     /**
155     * nextInt(bound) returns 0 <= value < bound;
156 jsr166 1.4 * repeated calls produce at least two distinct results
157 dl 1.1 */
158     public void testNextIntBounded() {
159     SplittableRandom sr = new SplittableRandom();
160     // sample bound space across prime number increments
161     for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
162     int f = sr.nextInt(bound);
163     assertTrue(0 <= f && f < bound);
164     int i = 0;
165     int j;
166     while (i < NCALLS &&
167     (j = sr.nextInt(bound)) == f) {
168     assertTrue(0 <= j && j < bound);
169     ++i;
170     }
171     assertTrue(i < NCALLS);
172     }
173     }
174    
175     /**
176     * nextInt(least, bound) returns least <= value < bound;
177 jsr166 1.4 * repeated calls produce at least two distinct results
178 dl 1.1 */
179     public void testNextIntBounded2() {
180     SplittableRandom sr = new SplittableRandom();
181     for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
182     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
183     int f = sr.nextInt(least, bound);
184     assertTrue(least <= f && f < bound);
185     int i = 0;
186     int j;
187     while (i < NCALLS &&
188     (j = sr.nextInt(least, bound)) == f) {
189     assertTrue(least <= j && j < bound);
190     ++i;
191     }
192     assertTrue(i < NCALLS);
193     }
194     }
195     }
196    
197     /**
198 jsr166 1.9 * nextLong(non-positive) throws IllegalArgumentException
199 dl 1.1 */
200 jsr166 1.9 public void testNextLongNonPositive() {
201 dl 1.1 SplittableRandom sr = new SplittableRandom();
202 jsr166 1.9 Runnable[] throwingActions = {
203     () -> sr.nextLong(-17L),
204     () -> sr.nextLong(0L),
205     () -> sr.nextLong(Long.MIN_VALUE),
206     };
207     assertThrows(IllegalArgumentException.class, throwingActions);
208 dl 1.1 }
209    
210     /**
211 jsr166 1.4 * nextLong(least >= bound) throws IllegalArgumentException
212 dl 1.1 */
213     public void testNextLongBadBounds() {
214     SplittableRandom sr = new SplittableRandom();
215     try {
216     long f = sr.nextLong(17, 2);
217     shouldThrow();
218     } catch (IllegalArgumentException success) {}
219     }
220    
221     /**
222     * nextLong(bound) returns 0 <= value < bound;
223 jsr166 1.4 * repeated calls produce at least two distinct results
224 dl 1.1 */
225     public void testNextLongBounded() {
226     SplittableRandom sr = new SplittableRandom();
227     for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
228     long f = sr.nextLong(bound);
229     assertTrue(0 <= f && f < bound);
230     int i = 0;
231     long j;
232     while (i < NCALLS &&
233     (j = sr.nextLong(bound)) == f) {
234     assertTrue(0 <= j && j < bound);
235     ++i;
236     }
237     assertTrue(i < NCALLS);
238     }
239     }
240    
241     /**
242     * nextLong(least, bound) returns least <= value < bound;
243 jsr166 1.4 * repeated calls produce at least two distinct results
244 dl 1.1 */
245     public void testNextLongBounded2() {
246     SplittableRandom sr = new SplittableRandom();
247     for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
248     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
249     long f = sr.nextLong(least, bound);
250     assertTrue(least <= f && f < bound);
251     int i = 0;
252     long j;
253     while (i < NCALLS &&
254     (j = sr.nextLong(least, bound)) == f) {
255     assertTrue(least <= j && j < bound);
256     ++i;
257     }
258     assertTrue(i < NCALLS);
259     }
260     }
261     }
262    
263     /**
264 jsr166 1.9 * nextDouble(non-positive) throws IllegalArgumentException
265     */
266     public void testNextDoubleNonPositive() {
267     SplittableRandom sr = new SplittableRandom();
268     Runnable[] throwingActions = {
269     () -> sr.nextDouble(-17.0d),
270     () -> sr.nextDouble(0.0d),
271     () -> sr.nextDouble(-Double.MIN_VALUE),
272     () -> sr.nextDouble(Double.NEGATIVE_INFINITY),
273     () -> sr.nextDouble(Double.NaN),
274     };
275     assertThrows(IllegalArgumentException.class, throwingActions);
276     }
277    
278     /**
279 dl 1.1 * nextDouble(least, bound) returns least <= value < bound;
280 jsr166 1.4 * repeated calls produce at least two distinct results
281 dl 1.1 */
282     public void testNextDoubleBounded2() {
283     SplittableRandom sr = new SplittableRandom();
284     for (double least = 0.0001; least < 1.0e20; least *= 8) {
285     for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
286     double f = sr.nextDouble(least, bound);
287     assertTrue(least <= f && f < bound);
288     int i = 0;
289     double j;
290     while (i < NCALLS &&
291     (j = sr.nextDouble(least, bound)) == f) {
292     assertTrue(least <= j && j < bound);
293     ++i;
294     }
295     assertTrue(i < NCALLS);
296     }
297     }
298     }
299    
300     /**
301     * Invoking sized ints, long, doubles, with negative sizes throws
302     * IllegalArgumentException
303     */
304     public void testBadStreamSize() {
305     SplittableRandom r = new SplittableRandom();
306 jsr166 1.8 Runnable[] throwingActions = {
307     () -> { java.util.stream.IntStream x = r.ints(-1L); },
308     () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
309     () -> { java.util.stream.LongStream x = r.longs(-1L); },
310     () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
311     () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
312     () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); },
313     };
314     assertThrows(IllegalArgumentException.class, throwingActions);
315 dl 1.1 }
316    
317     /**
318     * Invoking bounded ints, long, doubles, with illegal bounds throws
319     * IllegalArgumentException
320     */
321     public void testBadStreamBounds() {
322     SplittableRandom r = new SplittableRandom();
323 jsr166 1.8 Runnable[] throwingActions = {
324     () -> { java.util.stream.IntStream x = r.ints(2, 1); },
325     () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
326     () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
327     () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
328     () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
329     () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); },
330     };
331     assertThrows(IllegalArgumentException.class, throwingActions);
332 dl 1.1 }
333    
334     /**
335     * A parallel sized stream of ints generates the given number of values
336     */
337     public void testIntsCount() {
338     LongAdder counter = new LongAdder();
339     SplittableRandom r = new SplittableRandom();
340     long size = 0;
341     for (int reps = 0; reps < REPS; ++reps) {
342     counter.reset();
343 jsr166 1.6 r.ints(size).parallel().forEach(x -> counter.increment());
344 jsr166 1.4 assertEquals(size, counter.sum());
345 dl 1.1 size += 524959;
346     }
347     }
348    
349     /**
350     * A parallel sized stream of longs generates the given number of values
351     */
352     public void testLongsCount() {
353     LongAdder counter = new LongAdder();
354     SplittableRandom r = new SplittableRandom();
355     long size = 0;
356     for (int reps = 0; reps < REPS; ++reps) {
357     counter.reset();
358 jsr166 1.6 r.longs(size).parallel().forEach(x -> counter.increment());
359 jsr166 1.4 assertEquals(size, counter.sum());
360 dl 1.1 size += 524959;
361     }
362     }
363    
364     /**
365     * A parallel sized stream of doubles generates the given number of values
366     */
367     public void testDoublesCount() {
368     LongAdder counter = new LongAdder();
369     SplittableRandom r = new SplittableRandom();
370     long size = 0;
371     for (int reps = 0; reps < REPS; ++reps) {
372     counter.reset();
373 jsr166 1.6 r.doubles(size).parallel().forEach(x -> counter.increment());
374 jsr166 1.4 assertEquals(size, counter.sum());
375 dl 1.1 size += 524959;
376     }
377     }
378    
379     /**
380     * Each of a parallel sized stream of bounded ints is within bounds
381     */
382     public void testBoundedInts() {
383     AtomicInteger fails = new AtomicInteger(0);
384     SplittableRandom r = new SplittableRandom();
385     long size = 12345L;
386     for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
387     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
388     final int lo = least, hi = bound;
389     r.ints(size, lo, hi).parallel().
390 jsr166 1.3 forEach(x -> {if (x < lo || x >= hi)
391 dl 1.1 fails.getAndIncrement(); });
392     }
393     }
394 jsr166 1.4 assertEquals(0, fails.get());
395 dl 1.1 }
396    
397     /**
398     * Each of a parallel sized stream of bounded longs is within bounds
399     */
400     public void testBoundedLongs() {
401     AtomicInteger fails = new AtomicInteger(0);
402     SplittableRandom r = new SplittableRandom();
403     long size = 123L;
404     for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
405     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
406     final long lo = least, hi = bound;
407     r.longs(size, lo, hi).parallel().
408 jsr166 1.3 forEach(x -> {if (x < lo || x >= hi)
409 dl 1.1 fails.getAndIncrement(); });
410     }
411     }
412 jsr166 1.4 assertEquals(0, fails.get());
413 dl 1.1 }
414    
415     /**
416     * Each of a parallel sized stream of bounded doubles is within bounds
417     */
418     public void testBoundedDoubles() {
419     AtomicInteger fails = new AtomicInteger(0);
420     SplittableRandom r = new SplittableRandom();
421     long size = 456;
422     for (double least = 0.00011; least < 1.0e20; least *= 9) {
423     for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
424     final double lo = least, hi = bound;
425     r.doubles(size, lo, hi).parallel().
426 jsr166 1.3 forEach(x -> {if (x < lo || x >= hi)
427 dl 1.1 fails.getAndIncrement(); });
428     }
429     }
430 jsr166 1.4 assertEquals(0, fails.get());
431 dl 1.1 }
432    
433 dl 1.2 /**
434     * A parallel unsized stream of ints generates at least 100 values
435     */
436     public void testUnsizedIntsCount() {
437     LongAdder counter = new LongAdder();
438     SplittableRandom r = new SplittableRandom();
439     long size = 100;
440 jsr166 1.6 r.ints().limit(size).parallel().forEach(x -> counter.increment());
441 jsr166 1.4 assertEquals(size, counter.sum());
442 dl 1.2 }
443    
444     /**
445     * A parallel unsized stream of longs generates at least 100 values
446     */
447     public void testUnsizedLongsCount() {
448     LongAdder counter = new LongAdder();
449     SplittableRandom r = new SplittableRandom();
450     long size = 100;
451 jsr166 1.6 r.longs().limit(size).parallel().forEach(x -> counter.increment());
452 jsr166 1.4 assertEquals(size, counter.sum());
453 dl 1.2 }
454    
455     /**
456     * A parallel unsized stream of doubles generates at least 100 values
457     */
458     public void testUnsizedDoublesCount() {
459     LongAdder counter = new LongAdder();
460     SplittableRandom r = new SplittableRandom();
461     long size = 100;
462 jsr166 1.6 r.doubles().limit(size).parallel().forEach(x -> counter.increment());
463 jsr166 1.4 assertEquals(size, counter.sum());
464 dl 1.2 }
465    
466     /**
467     * A sequential unsized stream of ints generates at least 100 values
468     */
469     public void testUnsizedIntsCountSeq() {
470     LongAdder counter = new LongAdder();
471     SplittableRandom r = new SplittableRandom();
472     long size = 100;
473 jsr166 1.6 r.ints().limit(size).forEach(x -> counter.increment());
474 jsr166 1.4 assertEquals(size, counter.sum());
475 dl 1.2 }
476    
477     /**
478     * A sequential unsized stream of longs generates at least 100 values
479     */
480     public void testUnsizedLongsCountSeq() {
481     LongAdder counter = new LongAdder();
482     SplittableRandom r = new SplittableRandom();
483     long size = 100;
484 jsr166 1.6 r.longs().limit(size).forEach(x -> counter.increment());
485 jsr166 1.4 assertEquals(size, counter.sum());
486 dl 1.2 }
487    
488     /**
489     * A sequential unsized stream of doubles generates at least 100 values
490     */
491     public void testUnsizedDoublesCountSeq() {
492     LongAdder counter = new LongAdder();
493     SplittableRandom r = new SplittableRandom();
494     long size = 100;
495 jsr166 1.6 r.doubles().limit(size).forEach(x -> counter.increment());
496 jsr166 1.4 assertEquals(size, counter.sum());
497 dl 1.2 }
498    
499 dl 1.1 }