ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
Revision: 1.4
Committed: Sun Jul 14 16:55:01 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +42 -52 lines
Log Message:
actually test SplittableRandom; tidying pass; coding style consistency

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