ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SplittableRandomTest.java (file contents):
Revision 1.2 by dl, Thu Jul 11 23:06:47 2013 UTC vs.
Revision 1.15 by jsr166, Wed Dec 31 16:44:02 2014 UTC

# Line 7 | Line 7 | 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;
10   import java.util.concurrent.atomic.LongAdder;
11  
12   public class SplittableRandomTest extends JSR166TestCase {
# Line 22 | Line 21 | public class SplittableRandomTest extend
21      /*
22       * Testing coverage notes:
23       *
24 <     * 1. Many of the test methods are adapted from ThreadLocalRandomTest
24 >     * 1. Many of the test methods are adapted from ThreadLocalRandomTest.
25       *
26 <     * 2. This set of tests do not check for random number generator
27 <     * quality. But we check for minimal API compliance by requiring
28 <     * that repeated calls to nextX methods, up to NCALLS tries,
29 <     * produce at least one different result. (In some possible
30 <     * universe, a "correct" implementation might fail, but the odds
31 <     * are vastly less than that of encountering a hardware failure
32 <     * while running the test.) For bounded nextX methods, we sample
33 <     * various intervals across multiples of primes. In other tests,
34 <     * we repeat under REPS different values.
26 >     * 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       */
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 <    static final int MAX_INT_BOUND = (1 << 28);
41 >    static final int MAX_INT_BOUND = (1 << 26);
42  
43 <    // Max sampled long bound
44 <    static final long MAX_LONG_BOUND = (1L << 42);
43 >    // max sampled long bound
44 >    static final long MAX_LONG_BOUND = (1L << 40);
45  
46      // Number of replications for other checks
47 <    static final int REPS = 20;
47 >    static final int REPS =
48 >        Integer.getInteger("SplittableRandomTest.reps", 4);
49  
50      /**
51 <     * Repeated calls to nextInt produce at least one different result
51 >     * Repeated calls to nextInt produce at least two distinct results
52       */
53      public void testNextInt() {
54          SplittableRandom sr = new SplittableRandom();
# Line 60 | Line 60 | public class SplittableRandomTest extend
60      }
61  
62      /**
63 <     * Repeated calls to nextLong produce at least one different result
63 >     * Repeated calls to nextLong produce at least two distinct results
64       */
65      public void testNextLong() {
66          SplittableRandom sr = new SplittableRandom();
# Line 72 | Line 72 | public class SplittableRandomTest extend
72      }
73  
74      /**
75 <     * Repeated calls to nextDouble produce at least one different result
75 >     * Repeated calls to nextDouble produce at least two distinct results
76       */
77      public void testNextDouble() {
78          SplittableRandom sr = new SplittableRandom();
79          double f = sr.nextDouble();
80 <        double i = 0;
80 >        int i = 0;
81          while (i < NCALLS && sr.nextDouble() == f)
82              ++i;
83          assertTrue(i < NCALLS);
# Line 88 | Line 88 | public class SplittableRandomTest extend
88       * same values for nextLong.
89       */
90      public void testSeedConstructor() {
91 <        for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863)  {
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)
94 >            for (int i = 0; i < REPS; ++i)
95                  assertEquals(sr1.nextLong(), sr2.nextLong());
96          }
97      }
# Line 127 | Line 127 | public class SplittableRandomTest extend
127      }
128  
129      /**
130 <     * nextInt(negative) throws IllegalArgumentException;
130 >     * nextInt(non-positive) throws IllegalArgumentException
131       */
132 <    public void testNextIntBoundedNeg() {
132 >    public void testNextIntBoundNonPositive() {
133          SplittableRandom sr = new SplittableRandom();
134 <        try {
135 <            int f = sr.nextInt(-17);
136 <            shouldThrow();
137 <        } catch (IllegalArgumentException success) {}
134 >        Runnable[] throwingActions = {
135 >            () -> sr.nextInt(-17),
136 >            () -> sr.nextInt(0),
137 >            () -> sr.nextInt(Integer.MIN_VALUE),
138 >        };
139 >        assertThrows(IllegalArgumentException.class, throwingActions);
140      }
141  
142      /**
143 <     * nextInt(least >= bound) throws IllegalArgumentException;
143 >     * nextInt(least >= bound) throws IllegalArgumentException
144       */
145      public void testNextIntBadBounds() {
146          SplittableRandom sr = new SplittableRandom();
147 <        try {
148 <            int f = sr.nextInt(17, 2);
149 <            shouldThrow();
150 <        } catch (IllegalArgumentException success) {}
147 >        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      }
154  
155      /**
156       * nextInt(bound) returns 0 <= value < bound;
157 <     * repeated calls produce at least one different result
157 >     * repeated calls produce at least two distinct results
158       */
159      public void testNextIntBounded() {
160          SplittableRandom sr = new SplittableRandom();
# Line 171 | Line 175 | public class SplittableRandomTest extend
175  
176      /**
177       * nextInt(least, bound) returns least <= value < bound;
178 <     * repeated calls produce at least one different result
178 >     * repeated calls produce at least two distinct results
179       */
180      public void testNextIntBounded2() {
181          SplittableRandom sr = new SplittableRandom();
# Line 192 | Line 196 | public class SplittableRandomTest extend
196      }
197  
198      /**
199 <     * nextLong(negative) throws IllegalArgumentException;
199 >     * nextLong(non-positive) throws IllegalArgumentException
200       */
201 <    public void testNextLongBoundedNeg() {
201 >    public void testNextLongBoundNonPositive() {
202          SplittableRandom sr = new SplittableRandom();
203 <        try {
204 <            long f = sr.nextLong(-17);
205 <            shouldThrow();
206 <        } catch (IllegalArgumentException success) {}
203 >        Runnable[] throwingActions = {
204 >            () -> sr.nextLong(-17L),
205 >            () -> sr.nextLong(0L),
206 >            () -> sr.nextLong(Long.MIN_VALUE),
207 >        };
208 >        assertThrows(IllegalArgumentException.class, throwingActions);
209      }
210  
211      /**
212 <     * nextLong(least >= bound) throws IllegalArgumentException;
212 >     * nextLong(least >= bound) throws IllegalArgumentException
213       */
214      public void testNextLongBadBounds() {
215          SplittableRandom sr = new SplittableRandom();
216 <        try {
217 <            long f = sr.nextLong(17, 2);
218 <            shouldThrow();
219 <        } catch (IllegalArgumentException success) {}
216 >        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      }
223  
224      /**
225       * nextLong(bound) returns 0 <= value < bound;
226 <     * repeated calls produce at least one different result
226 >     * repeated calls produce at least two distinct results
227       */
228      public void testNextLongBounded() {
229          SplittableRandom sr = new SplittableRandom();
# Line 235 | Line 243 | public class SplittableRandomTest extend
243  
244      /**
245       * nextLong(least, bound) returns least <= value < bound;
246 <     * repeated calls produce at least one different result
246 >     * repeated calls produce at least two distinct results
247       */
248      public void testNextLongBounded2() {
249          SplittableRandom sr = new SplittableRandom();
# Line 256 | Line 264 | public class SplittableRandomTest extend
264      }
265  
266      /**
267 +     * nextDouble(non-positive) throws IllegalArgumentException
268 +     */
269 +    public void testNextDoubleBoundNonPositive() {
270 +        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 +     * 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       * nextDouble(least, bound) returns least <= value < bound;
302 <     * repeated calls produce at least one different result
302 >     * repeated calls produce at least two distinct results
303       */
304      public void testNextDoubleBounded2() {
305          SplittableRandom sr = new SplittableRandom();
# Line 283 | Line 325 | public class SplittableRandomTest extend
325       */
326      public void testBadStreamSize() {
327          SplittableRandom r = new SplittableRandom();
328 <        try {
329 <            java.util.stream.IntStream x = r.ints(-1L);
330 <            shouldThrow();
331 <        } catch (IllegalArgumentException ok) {
332 <        }
333 <        try {
334 <            java.util.stream.LongStream x = r.longs(-1L);
335 <            shouldThrow();
336 <        } catch (IllegalArgumentException ok) {
295 <        }
296 <        try {
297 <            java.util.stream.DoubleStream x = r.doubles(-1L);
298 <            shouldThrow();
299 <        } catch (IllegalArgumentException ok) {
300 <        }
328 >        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      }
338  
339      /**
# Line 306 | Line 342 | public class SplittableRandomTest extend
342       */
343      public void testBadStreamBounds() {
344          SplittableRandom r = new SplittableRandom();
345 <        try {
346 <            java.util.stream.IntStream x = r.ints(2, 1);
347 <            shouldThrow();
348 <        } catch (IllegalArgumentException ok) {
349 <        }
350 <        try {
351 <            java.util.stream.LongStream x = r.longs(1, -2);
352 <            shouldThrow();
353 <        } catch (IllegalArgumentException ok) {
318 <        }
319 <        try {
320 <            java.util.stream.DoubleStream x = r.doubles(0, 0);
321 <            shouldThrow();
322 <        } catch (IllegalArgumentException ok) {
323 <        }
345 >        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      }
355  
356      /**
# Line 332 | Line 362 | public class SplittableRandomTest extend
362          long size = 0;
363          for (int reps = 0; reps < REPS; ++reps) {
364              counter.reset();
365 <            r.ints(size).parallel().forEach(x -> {counter.increment();});
366 <            assertEquals(counter.sum(), size);
365 >            r.ints(size).parallel().forEach(x -> counter.increment());
366 >            assertEquals(size, counter.sum());
367              size += 524959;
368          }
369      }
# Line 347 | Line 377 | public class SplittableRandomTest extend
377          long size = 0;
378          for (int reps = 0; reps < REPS; ++reps) {
379              counter.reset();
380 <            r.longs(size).parallel().forEach(x -> {counter.increment();});
381 <            assertEquals(counter.sum(), size);
380 >            r.longs(size).parallel().forEach(x -> counter.increment());
381 >            assertEquals(size, counter.sum());
382              size += 524959;
383          }
384      }
# Line 362 | Line 392 | public class SplittableRandomTest extend
392          long size = 0;
393          for (int reps = 0; reps < REPS; ++reps) {
394              counter.reset();
395 <            r.doubles(size).parallel().forEach(x -> {counter.increment();});
396 <            assertEquals(counter.sum(), size);
395 >            r.doubles(size).parallel().forEach(x -> counter.increment());
396 >            assertEquals(size, counter.sum());
397              size += 524959;
398          }
399      }
400  
371
401      /**
402       * Each of a parallel sized stream of bounded ints is within bounds
403       */
# Line 380 | Line 409 | public class SplittableRandomTest extend
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 <                    forEach(x -> {if (x < lo || x >= hi)
412 >                    forEach(x -> {if (x < lo || x >= hi)
413                                  fails.getAndIncrement(); });
414              }
415          }
416 <        assertEquals(fails.get(), 0);
416 >        assertEquals(0, fails.get());
417      }
418  
419      /**
# Line 398 | Line 427 | public class SplittableRandomTest extend
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 <                    forEach(x -> {if (x < lo || x >= hi)
430 >                    forEach(x -> {if (x < lo || x >= hi)
431                                  fails.getAndIncrement(); });
432              }
433          }
434 <        assertEquals(fails.get(), 0);
434 >        assertEquals(0, fails.get());
435      }
436  
437      /**
# Line 416 | Line 445 | public class SplittableRandomTest extend
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 <                    forEach(x -> {if (x < lo || x >= hi)
448 >                    forEach(x -> {if (x < lo || x >= hi)
449                                  fails.getAndIncrement(); });
450              }
451          }
452 <        assertEquals(fails.get(), 0);
452 >        assertEquals(0, fails.get());
453      }
454  
455      /**
# Line 430 | Line 459 | public class SplittableRandomTest extend
459          LongAdder counter = new LongAdder();
460          SplittableRandom r = new SplittableRandom();
461          long size = 100;
462 <        r.ints().limit(size).parallel().forEach(x -> {counter.increment();});
463 <        assertEquals(counter.sum(), size);
462 >        r.ints().limit(size).parallel().forEach(x -> counter.increment());
463 >        assertEquals(size, counter.sum());
464      }
465  
466      /**
# Line 441 | Line 470 | public class SplittableRandomTest extend
470          LongAdder counter = new LongAdder();
471          SplittableRandom r = new SplittableRandom();
472          long size = 100;
473 <        r.longs().limit(size).parallel().forEach(x -> {counter.increment();});
474 <        assertEquals(counter.sum(), size);
473 >        r.longs().limit(size).parallel().forEach(x -> counter.increment());
474 >        assertEquals(size, counter.sum());
475      }
476  
448
477      /**
478       * A parallel unsized stream of doubles generates at least 100 values
479       */
# Line 453 | Line 481 | public class SplittableRandomTest extend
481          LongAdder counter = new LongAdder();
482          SplittableRandom r = new SplittableRandom();
483          long size = 100;
484 <        r.doubles().limit(size).parallel().forEach(x -> {counter.increment();});
485 <        assertEquals(counter.sum(), size);
484 >        r.doubles().limit(size).parallel().forEach(x -> counter.increment());
485 >        assertEquals(size, counter.sum());
486      }
487  
488      /**
# Line 464 | Line 492 | public class SplittableRandomTest extend
492          LongAdder counter = new LongAdder();
493          SplittableRandom r = new SplittableRandom();
494          long size = 100;
495 <        r.ints().limit(size).forEach(x -> {counter.increment();});
496 <        assertEquals(counter.sum(), size);
495 >        r.ints().limit(size).forEach(x -> counter.increment());
496 >        assertEquals(size, counter.sum());
497      }
498  
499      /**
# Line 475 | Line 503 | public class SplittableRandomTest extend
503          LongAdder counter = new LongAdder();
504          SplittableRandom r = new SplittableRandom();
505          long size = 100;
506 <        r.longs().limit(size).forEach(x -> {counter.increment();});
507 <        assertEquals(counter.sum(), size);
506 >        r.longs().limit(size).forEach(x -> counter.increment());
507 >        assertEquals(size, counter.sum());
508      }
509  
482
510      /**
511       * A sequential unsized stream of doubles generates at least 100 values
512       */
# Line 487 | Line 514 | public class SplittableRandomTest extend
514          LongAdder counter = new LongAdder();
515          SplittableRandom r = new SplittableRandom();
516          long size = 100;
517 <        r.doubles().limit(size).forEach(x -> {counter.increment();});
518 <        assertEquals(counter.sum(), size);
517 >        r.doubles().limit(size).forEach(x -> counter.increment());
518 >        assertEquals(size, counter.sum());
519      }
520  
494
521   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines