ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
Revision: 1.2
Committed: Thu Jul 11 23:06:47 2013 UTC (10 years, 10 months ago) by dl
Branch: MAIN
Changes since 1.1: +70 -0 lines
Log Message:
Improve nextDouble()

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