ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
Revision: 1.8
Committed: Tue Sep 24 06:17:12 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +18 -48 lines
Log Message:
use assertThrows

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.4 * nextInt(negative) throws IllegalArgumentException
132 dl 1.1 */
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 jsr166 1.4 * nextInt(least >= bound) throws IllegalArgumentException
143 dl 1.1 */
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 jsr166 1.4 * repeated calls produce at least two distinct results
155 dl 1.1 */
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 jsr166 1.4 * repeated calls produce at least two distinct results
176 dl 1.1 */
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 jsr166 1.4 * nextLong(negative) throws IllegalArgumentException
197 dl 1.1 */
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 jsr166 1.4 * nextLong(least >= bound) throws IllegalArgumentException
208 dl 1.1 */
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 jsr166 1.4 * repeated calls produce at least two distinct results
220 dl 1.1 */
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 jsr166 1.4 * repeated calls produce at least two distinct results
240 dl 1.1 */
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 jsr166 1.4 * repeated calls produce at least two distinct results
262 dl 1.1 */
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 jsr166 1.8 Runnable[] throwingActions = {
288     () -> { java.util.stream.IntStream x = r.ints(-1L); },
289     () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
290     () -> { java.util.stream.LongStream x = r.longs(-1L); },
291     () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
292     () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
293     () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); },
294     };
295     assertThrows(IllegalArgumentException.class, throwingActions);
296 dl 1.1 }
297    
298     /**
299     * Invoking bounded ints, long, doubles, with illegal bounds throws
300     * IllegalArgumentException
301     */
302     public void testBadStreamBounds() {
303     SplittableRandom r = new SplittableRandom();
304 jsr166 1.8 Runnable[] throwingActions = {
305     () -> { java.util.stream.IntStream x = r.ints(2, 1); },
306     () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
307     () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
308     () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
309     () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
310     () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); },
311     };
312     assertThrows(IllegalArgumentException.class, throwingActions);
313 dl 1.1 }
314    
315     /**
316     * A parallel sized stream of ints generates the given number of values
317     */
318     public void testIntsCount() {
319     LongAdder counter = new LongAdder();
320     SplittableRandom r = new SplittableRandom();
321     long size = 0;
322     for (int reps = 0; reps < REPS; ++reps) {
323     counter.reset();
324 jsr166 1.6 r.ints(size).parallel().forEach(x -> counter.increment());
325 jsr166 1.4 assertEquals(size, counter.sum());
326 dl 1.1 size += 524959;
327     }
328     }
329    
330     /**
331     * A parallel sized stream of longs generates the given number of values
332     */
333     public void testLongsCount() {
334     LongAdder counter = new LongAdder();
335     SplittableRandom r = new SplittableRandom();
336     long size = 0;
337     for (int reps = 0; reps < REPS; ++reps) {
338     counter.reset();
339 jsr166 1.6 r.longs(size).parallel().forEach(x -> counter.increment());
340 jsr166 1.4 assertEquals(size, counter.sum());
341 dl 1.1 size += 524959;
342     }
343     }
344    
345     /**
346     * A parallel sized stream of doubles generates the given number of values
347     */
348     public void testDoublesCount() {
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 jsr166 1.6 r.doubles(size).parallel().forEach(x -> counter.increment());
355 jsr166 1.4 assertEquals(size, counter.sum());
356 dl 1.1 size += 524959;
357     }
358     }
359    
360     /**
361     * Each of a parallel sized stream of bounded ints is within bounds
362     */
363     public void testBoundedInts() {
364     AtomicInteger fails = new AtomicInteger(0);
365     SplittableRandom r = new SplittableRandom();
366     long size = 12345L;
367     for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
368     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
369     final int lo = least, hi = bound;
370     r.ints(size, lo, hi).parallel().
371 jsr166 1.3 forEach(x -> {if (x < lo || x >= hi)
372 dl 1.1 fails.getAndIncrement(); });
373     }
374     }
375 jsr166 1.4 assertEquals(0, fails.get());
376 dl 1.1 }
377    
378     /**
379     * Each of a parallel sized stream of bounded longs is within bounds
380     */
381     public void testBoundedLongs() {
382     AtomicInteger fails = new AtomicInteger(0);
383     SplittableRandom r = new SplittableRandom();
384     long size = 123L;
385     for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
386     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
387     final long lo = least, hi = bound;
388     r.longs(size, lo, hi).parallel().
389 jsr166 1.3 forEach(x -> {if (x < lo || x >= hi)
390 dl 1.1 fails.getAndIncrement(); });
391     }
392     }
393 jsr166 1.4 assertEquals(0, fails.get());
394 dl 1.1 }
395    
396     /**
397     * Each of a parallel sized stream of bounded doubles is within bounds
398     */
399     public void testBoundedDoubles() {
400     AtomicInteger fails = new AtomicInteger(0);
401     SplittableRandom r = new SplittableRandom();
402     long size = 456;
403     for (double least = 0.00011; least < 1.0e20; least *= 9) {
404     for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
405     final double lo = least, hi = bound;
406     r.doubles(size, lo, hi).parallel().
407 jsr166 1.3 forEach(x -> {if (x < lo || x >= hi)
408 dl 1.1 fails.getAndIncrement(); });
409     }
410     }
411 jsr166 1.4 assertEquals(0, fails.get());
412 dl 1.1 }
413    
414 dl 1.2 /**
415     * A parallel unsized stream of ints generates at least 100 values
416     */
417     public void testUnsizedIntsCount() {
418     LongAdder counter = new LongAdder();
419     SplittableRandom r = new SplittableRandom();
420     long size = 100;
421 jsr166 1.6 r.ints().limit(size).parallel().forEach(x -> counter.increment());
422 jsr166 1.4 assertEquals(size, counter.sum());
423 dl 1.2 }
424    
425     /**
426     * A parallel unsized stream of longs generates at least 100 values
427     */
428     public void testUnsizedLongsCount() {
429     LongAdder counter = new LongAdder();
430     SplittableRandom r = new SplittableRandom();
431     long size = 100;
432 jsr166 1.6 r.longs().limit(size).parallel().forEach(x -> counter.increment());
433 jsr166 1.4 assertEquals(size, counter.sum());
434 dl 1.2 }
435    
436     /**
437     * A parallel unsized stream of doubles generates at least 100 values
438     */
439     public void testUnsizedDoublesCount() {
440     LongAdder counter = new LongAdder();
441     SplittableRandom r = new SplittableRandom();
442     long size = 100;
443 jsr166 1.6 r.doubles().limit(size).parallel().forEach(x -> counter.increment());
444 jsr166 1.4 assertEquals(size, counter.sum());
445 dl 1.2 }
446    
447     /**
448     * A sequential unsized stream of ints generates at least 100 values
449     */
450     public void testUnsizedIntsCountSeq() {
451     LongAdder counter = new LongAdder();
452     SplittableRandom r = new SplittableRandom();
453     long size = 100;
454 jsr166 1.6 r.ints().limit(size).forEach(x -> counter.increment());
455 jsr166 1.4 assertEquals(size, counter.sum());
456 dl 1.2 }
457    
458     /**
459     * A sequential unsized stream of longs generates at least 100 values
460     */
461     public void testUnsizedLongsCountSeq() {
462     LongAdder counter = new LongAdder();
463     SplittableRandom r = new SplittableRandom();
464     long size = 100;
465 jsr166 1.6 r.longs().limit(size).forEach(x -> counter.increment());
466 jsr166 1.4 assertEquals(size, counter.sum());
467 dl 1.2 }
468    
469     /**
470     * A sequential unsized stream of doubles generates at least 100 values
471     */
472     public void testUnsizedDoublesCountSeq() {
473     LongAdder counter = new LongAdder();
474     SplittableRandom r = new SplittableRandom();
475     long size = 100;
476 jsr166 1.6 r.doubles().limit(size).forEach(x -> counter.increment());
477 jsr166 1.4 assertEquals(size, counter.sum());
478 dl 1.2 }
479    
480 dl 1.1 }