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.3 by jsr166, Fri Jul 12 01:14:30 2013 UTC vs.
Revision 1.25 by jsr166, Fri Feb 22 19:27:47 2019 UTC

# Line 3 | Line 3
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.*;
6 >
7 > import java.util.Arrays;
8 > import java.util.List;
9   import java.util.SplittableRandom;
10   import java.util.concurrent.atomic.AtomicInteger;
10 import java.util.concurrent.atomic.AtomicLong;
11   import java.util.concurrent.atomic.LongAdder;
12 + import java.lang.reflect.Method;
13 + import java.util.function.Predicate;
14 + import java.util.stream.Collectors;
15 +
16 + import junit.framework.Test;
17 + import junit.framework.TestSuite;
18  
19   public class SplittableRandomTest extends JSR166TestCase {
20  
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run(suite());
22 >        main(suite(), args);
23      }
24      public static Test suite() {
25          return new TestSuite(SplittableRandomTest.class);
# Line 22 | Line 28 | public class SplittableRandomTest extend
28      /*
29       * Testing coverage notes:
30       *
31 <     * 1. Many of the test methods are adapted from ThreadLocalRandomTest
31 >     * 1. Many of the test methods are adapted from ThreadLocalRandomTest.
32       *
33 <     * 2. This set of tests do not check for random number generator
34 <     * quality. But we check for minimal API compliance by requiring
35 <     * that repeated calls to nextX methods, up to NCALLS tries,
36 <     * produce at least one different result. (In some possible
37 <     * universe, a "correct" implementation might fail, but the odds
38 <     * are vastly less than that of encountering a hardware failure
39 <     * while running the test.) For bounded nextX methods, we sample
40 <     * various intervals across multiples of primes. In other tests,
41 <     * we repeat under REPS different values.
33 >     * 2. These tests do not check for random number generator quality.
34 >     * But we check for minimal API compliance by requiring that
35 >     * repeated calls to nextX methods, up to NCALLS tries, produce at
36 >     * least two distinct results. (In some possible universe, a
37 >     * "correct" implementation might fail, but the odds are vastly
38 >     * less than that of encountering a hardware failure while running
39 >     * the test.) For bounded nextX methods, we sample various
40 >     * intervals across multiples of primes. In other tests, we repeat
41 >     * under REPS different values.
42       */
43  
44      // max numbers of calls to detect getting stuck on one value
45      static final int NCALLS = 10000;
46  
47      // max sampled int bound
48 <    static final int MAX_INT_BOUND = (1 << 28);
48 >    static final int MAX_INT_BOUND = (1 << 26);
49  
50 <    // Max sampled long bound
51 <    static final long MAX_LONG_BOUND = (1L << 42);
50 >    // max sampled long bound
51 >    static final long MAX_LONG_BOUND = (1L << 40);
52  
53      // Number of replications for other checks
54 <    static final int REPS = 20;
54 >    static final int REPS =
55 >        Integer.getInteger("SplittableRandomTest.reps", 4);
56  
57      /**
58 <     * Repeated calls to nextInt produce at least one different result
58 >     * Repeated calls to nextInt produce at least two distinct results
59       */
60      public void testNextInt() {
61          SplittableRandom sr = new SplittableRandom();
# Line 60 | Line 67 | public class SplittableRandomTest extend
67      }
68  
69      /**
70 <     * Repeated calls to nextLong produce at least one different result
70 >     * Repeated calls to nextLong produce at least two distinct results
71       */
72      public void testNextLong() {
73          SplittableRandom sr = new SplittableRandom();
# Line 72 | Line 79 | public class SplittableRandomTest extend
79      }
80  
81      /**
82 <     * Repeated calls to nextDouble produce at least one different result
82 >     * Repeated calls to nextDouble produce at least two distinct results
83       */
84      public void testNextDouble() {
85          SplittableRandom sr = new SplittableRandom();
86          double f = sr.nextDouble();
87 <        double i = 0;
87 >        int i = 0;
88          while (i < NCALLS && sr.nextDouble() == f)
89              ++i;
90          assertTrue(i < NCALLS);
# Line 88 | Line 95 | public class SplittableRandomTest extend
95       * same values for nextLong.
96       */
97      public void testSeedConstructor() {
98 <        for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863)  {
98 >        for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) {
99              SplittableRandom sr1 = new SplittableRandom(seed);
100              SplittableRandom sr2 = new SplittableRandom(seed);
101              for (int i = 0; i < REPS; ++i)
# Line 127 | Line 134 | public class SplittableRandomTest extend
134      }
135  
136      /**
137 <     * nextInt(negative) throws IllegalArgumentException;
137 >     * nextInt(non-positive) throws IllegalArgumentException
138       */
139 <    public void testNextIntBoundedNeg() {
139 >    public void testNextIntBoundNonPositive() {
140          SplittableRandom sr = new SplittableRandom();
141 <        try {
142 <            int f = sr.nextInt(-17);
143 <            shouldThrow();
144 <        } catch (IllegalArgumentException success) {}
141 >        assertThrows(
142 >            IllegalArgumentException.class,
143 >            () -> sr.nextInt(-17),
144 >            () -> sr.nextInt(0),
145 >            () -> sr.nextInt(Integer.MIN_VALUE));
146      }
147  
148      /**
149 <     * nextInt(least >= bound) throws IllegalArgumentException;
149 >     * nextInt(least >= bound) throws IllegalArgumentException
150       */
151      public void testNextIntBadBounds() {
152          SplittableRandom sr = new SplittableRandom();
153 <        try {
154 <            int f = sr.nextInt(17, 2);
155 <            shouldThrow();
156 <        } catch (IllegalArgumentException success) {}
153 >        assertThrows(
154 >            IllegalArgumentException.class,
155 >            () -> sr.nextInt(17, 2),
156 >            () -> sr.nextInt(-42, -42),
157 >            () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE));
158      }
159  
160      /**
161       * nextInt(bound) returns 0 <= value < bound;
162 <     * repeated calls produce at least one different result
162 >     * repeated calls produce at least two distinct results
163       */
164      public void testNextIntBounded() {
165          SplittableRandom sr = new SplittableRandom();
166 +        for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1));
167          // sample bound space across prime number increments
168          for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
169              int f = sr.nextInt(bound);
# Line 171 | Line 181 | public class SplittableRandomTest extend
181  
182      /**
183       * nextInt(least, bound) returns least <= value < bound;
184 <     * repeated calls produce at least one different result
184 >     * repeated calls produce at least two distinct results
185       */
186      public void testNextIntBounded2() {
187          SplittableRandom sr = new SplittableRandom();
# Line 192 | Line 202 | public class SplittableRandomTest extend
202      }
203  
204      /**
205 <     * nextLong(negative) throws IllegalArgumentException;
205 >     * nextLong(non-positive) throws IllegalArgumentException
206       */
207 <    public void testNextLongBoundedNeg() {
207 >    public void testNextLongBoundNonPositive() {
208          SplittableRandom sr = new SplittableRandom();
209 <        try {
210 <            long f = sr.nextLong(-17);
211 <            shouldThrow();
212 <        } catch (IllegalArgumentException success) {}
209 >        assertThrows(
210 >            IllegalArgumentException.class,
211 >            () -> sr.nextLong(-17L),
212 >            () -> sr.nextLong(0L),
213 >            () -> sr.nextLong(Long.MIN_VALUE));
214      }
215  
216      /**
217 <     * nextLong(least >= bound) throws IllegalArgumentException;
217 >     * nextLong(least >= bound) throws IllegalArgumentException
218       */
219      public void testNextLongBadBounds() {
220          SplittableRandom sr = new SplittableRandom();
221 <        try {
222 <            long f = sr.nextLong(17, 2);
223 <            shouldThrow();
224 <        } catch (IllegalArgumentException success) {}
221 >        assertThrows(
222 >            IllegalArgumentException.class,
223 >            () -> sr.nextLong(17L, 2L),
224 >            () -> sr.nextLong(-42L, -42L),
225 >            () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE));
226      }
227  
228      /**
229       * nextLong(bound) returns 0 <= value < bound;
230 <     * repeated calls produce at least one different result
230 >     * repeated calls produce at least two distinct results
231       */
232      public void testNextLongBounded() {
233          SplittableRandom sr = new SplittableRandom();
234 +        for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L));
235          for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
236              long f = sr.nextLong(bound);
237              assertTrue(0 <= f && f < bound);
# Line 235 | Line 248 | public class SplittableRandomTest extend
248  
249      /**
250       * nextLong(least, bound) returns least <= value < bound;
251 <     * repeated calls produce at least one different result
251 >     * repeated calls produce at least two distinct results
252       */
253      public void testNextLongBounded2() {
254          SplittableRandom sr = new SplittableRandom();
# Line 256 | Line 269 | public class SplittableRandomTest extend
269      }
270  
271      /**
272 +     * nextDouble(non-positive) throws IllegalArgumentException
273 +     */
274 +    public void testNextDoubleBoundNonPositive() {
275 +        SplittableRandom sr = new SplittableRandom();
276 +        assertThrows(
277 +            IllegalArgumentException.class,
278 +            () -> sr.nextDouble(-17.0d),
279 +            () -> sr.nextDouble(0.0d),
280 +            () -> sr.nextDouble(-Double.MIN_VALUE),
281 +            () -> sr.nextDouble(Double.NEGATIVE_INFINITY),
282 +            () -> sr.nextDouble(Double.NaN));
283 +    }
284 +
285 +    /**
286 +     * nextDouble(! (least < bound)) throws IllegalArgumentException
287 +     */
288 +    public void testNextDoubleBadBounds() {
289 +        SplittableRandom sr = new SplittableRandom();
290 +        assertThrows(
291 +            IllegalArgumentException.class,
292 +            () -> sr.nextDouble(17.0d, 2.0d),
293 +            () -> sr.nextDouble(-42.0d, -42.0d),
294 +            () -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE),
295 +            () -> sr.nextDouble(Double.NaN, 0.0d),
296 +            () -> sr.nextDouble(0.0d, Double.NaN));
297 +    }
298 +
299 +    // TODO: Test infinite bounds!
300 +    //() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d),
301 +    //() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY),
302 +
303 +    /**
304       * nextDouble(least, bound) returns least <= value < bound;
305 <     * repeated calls produce at least one different result
305 >     * repeated calls produce at least two distinct results
306       */
307      public void testNextDoubleBounded2() {
308          SplittableRandom sr = new SplittableRandom();
# Line 283 | Line 328 | public class SplittableRandomTest extend
328       */
329      public void testBadStreamSize() {
330          SplittableRandom r = new SplittableRandom();
331 <        try {
332 <            java.util.stream.IntStream x = r.ints(-1L);
333 <            shouldThrow();
334 <        } catch (IllegalArgumentException ok) {
335 <        }
336 <        try {
337 <            java.util.stream.LongStream x = r.longs(-1L);
338 <            shouldThrow();
294 <        } catch (IllegalArgumentException ok) {
295 <        }
296 <        try {
297 <            java.util.stream.DoubleStream x = r.doubles(-1L);
298 <            shouldThrow();
299 <        } catch (IllegalArgumentException ok) {
300 <        }
331 >        assertThrows(
332 >            IllegalArgumentException.class,
333 >            () -> { java.util.stream.IntStream x = r.ints(-1L); },
334 >            () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
335 >            () -> { java.util.stream.LongStream x = r.longs(-1L); },
336 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
337 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
338 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); });
339      }
340  
341      /**
# Line 306 | Line 344 | public class SplittableRandomTest extend
344       */
345      public void testBadStreamBounds() {
346          SplittableRandom r = new SplittableRandom();
347 <        try {
348 <            java.util.stream.IntStream x = r.ints(2, 1);
349 <            shouldThrow();
350 <        } catch (IllegalArgumentException ok) {
351 <        }
352 <        try {
353 <            java.util.stream.LongStream x = r.longs(1, -2);
354 <            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 <        }
347 >        assertThrows(
348 >            IllegalArgumentException.class,
349 >            () -> { java.util.stream.IntStream x = r.ints(2, 1); },
350 >            () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
351 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
352 >            () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
353 >            () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
354 >            () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); });
355      }
356  
357      /**
# Line 332 | Line 363 | public class SplittableRandomTest extend
363          long size = 0;
364          for (int reps = 0; reps < REPS; ++reps) {
365              counter.reset();
366 <            r.ints(size).parallel().forEach(x -> {counter.increment();});
367 <            assertEquals(counter.sum(), size);
366 >            r.ints(size).parallel().forEach(x -> counter.increment());
367 >            assertEquals(size, counter.sum());
368              size += 524959;
369          }
370      }
# Line 347 | Line 378 | public class SplittableRandomTest extend
378          long size = 0;
379          for (int reps = 0; reps < REPS; ++reps) {
380              counter.reset();
381 <            r.longs(size).parallel().forEach(x -> {counter.increment();});
382 <            assertEquals(counter.sum(), size);
381 >            r.longs(size).parallel().forEach(x -> counter.increment());
382 >            assertEquals(size, counter.sum());
383              size += 524959;
384          }
385      }
# Line 362 | Line 393 | public class SplittableRandomTest extend
393          long size = 0;
394          for (int reps = 0; reps < REPS; ++reps) {
395              counter.reset();
396 <            r.doubles(size).parallel().forEach(x -> {counter.increment();});
397 <            assertEquals(counter.sum(), size);
396 >            r.doubles(size).parallel().forEach(x -> counter.increment());
397 >            assertEquals(size, counter.sum());
398              size += 524959;
399          }
400      }
401  
371
402      /**
403       * Each of a parallel sized stream of bounded ints is within bounds
404       */
# Line 379 | Line 409 | public class SplittableRandomTest extend
409          for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
410              for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
411                  final int lo = least, hi = bound;
412 <                r.ints(size, lo, hi).parallel().
413 <                    forEach(x -> {if (x < lo || x >= hi)
414 <                                fails.getAndIncrement(); });
412 >                r.ints(size, lo, hi).parallel().forEach(
413 >                    x -> {
414 >                        if (x < lo || x >= hi)
415 >                            fails.getAndIncrement(); });
416              }
417          }
418 <        assertEquals(fails.get(), 0);
418 >        assertEquals(0, fails.get());
419      }
420  
421      /**
# Line 397 | Line 428 | public class SplittableRandomTest extend
428          for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
429              for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
430                  final long lo = least, hi = bound;
431 <                r.longs(size, lo, hi).parallel().
432 <                    forEach(x -> {if (x < lo || x >= hi)
433 <                                fails.getAndIncrement(); });
431 >                r.longs(size, lo, hi).parallel().forEach(
432 >                    x -> {
433 >                        if (x < lo || x >= hi)
434 >                            fails.getAndIncrement(); });
435              }
436          }
437 <        assertEquals(fails.get(), 0);
437 >        assertEquals(0, fails.get());
438      }
439  
440      /**
# Line 415 | Line 447 | public class SplittableRandomTest extend
447          for (double least = 0.00011; least < 1.0e20; least *= 9) {
448              for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
449                  final double lo = least, hi = bound;
450 <                r.doubles(size, lo, hi).parallel().
451 <                    forEach(x -> {if (x < lo || x >= hi)
452 <                                fails.getAndIncrement(); });
450 >                r.doubles(size, lo, hi).parallel().forEach(
451 >                    x -> {
452 >                        if (x < lo || x >= hi)
453 >                            fails.getAndIncrement(); });
454              }
455          }
456 <        assertEquals(fails.get(), 0);
456 >        assertEquals(0, fails.get());
457      }
458  
459      /**
# Line 430 | Line 463 | public class SplittableRandomTest extend
463          LongAdder counter = new LongAdder();
464          SplittableRandom r = new SplittableRandom();
465          long size = 100;
466 <        r.ints().limit(size).parallel().forEach(x -> {counter.increment();});
467 <        assertEquals(counter.sum(), size);
466 >        r.ints().limit(size).parallel().forEach(x -> counter.increment());
467 >        assertEquals(size, counter.sum());
468      }
469  
470      /**
# Line 441 | Line 474 | public class SplittableRandomTest extend
474          LongAdder counter = new LongAdder();
475          SplittableRandom r = new SplittableRandom();
476          long size = 100;
477 <        r.longs().limit(size).parallel().forEach(x -> {counter.increment();});
478 <        assertEquals(counter.sum(), size);
477 >        r.longs().limit(size).parallel().forEach(x -> counter.increment());
478 >        assertEquals(size, counter.sum());
479      }
480  
448
481      /**
482       * A parallel unsized stream of doubles generates at least 100 values
483       */
# Line 453 | Line 485 | public class SplittableRandomTest extend
485          LongAdder counter = new LongAdder();
486          SplittableRandom r = new SplittableRandom();
487          long size = 100;
488 <        r.doubles().limit(size).parallel().forEach(x -> {counter.increment();});
489 <        assertEquals(counter.sum(), size);
488 >        r.doubles().limit(size).parallel().forEach(x -> counter.increment());
489 >        assertEquals(size, counter.sum());
490      }
491  
492      /**
# Line 464 | Line 496 | public class SplittableRandomTest extend
496          LongAdder counter = new LongAdder();
497          SplittableRandom r = new SplittableRandom();
498          long size = 100;
499 <        r.ints().limit(size).forEach(x -> {counter.increment();});
500 <        assertEquals(counter.sum(), size);
499 >        r.ints().limit(size).forEach(x -> counter.increment());
500 >        assertEquals(size, counter.sum());
501      }
502  
503      /**
# Line 475 | Line 507 | public class SplittableRandomTest extend
507          LongAdder counter = new LongAdder();
508          SplittableRandom r = new SplittableRandom();
509          long size = 100;
510 <        r.longs().limit(size).forEach(x -> {counter.increment();});
511 <        assertEquals(counter.sum(), size);
510 >        r.longs().limit(size).forEach(x -> counter.increment());
511 >        assertEquals(size, counter.sum());
512      }
513  
482
514      /**
515       * A sequential unsized stream of doubles generates at least 100 values
516       */
# Line 487 | Line 518 | public class SplittableRandomTest extend
518          LongAdder counter = new LongAdder();
519          SplittableRandom r = new SplittableRandom();
520          long size = 100;
521 <        r.doubles().limit(size).forEach(x -> {counter.increment();});
522 <        assertEquals(counter.sum(), size);
521 >        r.doubles().limit(size).forEach(x -> counter.increment());
522 >        assertEquals(size, counter.sum());
523 >    }
524 >
525 >    /**
526 >     * SplittableRandom should implement most of Random's public methods
527 >     */
528 >    public void testShouldImplementMostRandomMethods() throws Throwable {
529 >        Predicate<Method> wasForgotten = method -> {
530 >            String name = method.getName();
531 >            // some methods deliberately not implemented
532 >            if (name.equals("setSeed")) return false;
533 >            if (name.equals("nextFloat")) return false;
534 >            if (name.equals("nextGaussian")) return false;
535 >            try {
536 >                SplittableRandom.class.getMethod(
537 >                    method.getName(), method.getParameterTypes());
538 >            } catch (ReflectiveOperationException ex) {
539 >                return true;
540 >            }
541 >            return false;
542 >        };
543 >        List<Method> forgotten =
544 >            Arrays.stream(java.util.Random.class.getMethods())
545 >            .filter(wasForgotten)
546 >            .collect(Collectors.toList());
547 >        if (!forgotten.isEmpty())
548 >            throw new AssertionError("Please implement: " + forgotten);
549 >    }
550 >
551 >    /**
552 >     * Repeated calls to nextBytes produce at least values of different signs for every byte
553 >     */
554 >    public void testNextBytes() {
555 >        SplittableRandom sr = new SplittableRandom();
556 >        int n = sr.nextInt(1, 20);
557 >        byte[] bytes = new byte[n];
558 >        outer:
559 >        for (int i = 0; i < n; i++) {
560 >            for (int tries = NCALLS; tries-->0; ) {
561 >                byte before = bytes[i];
562 >                sr.nextBytes(bytes);
563 >                byte after = bytes[i];
564 >                if (after * before < 0)
565 >                    continue outer;
566 >            }
567 >            fail("not enough variation in random bytes");
568 >        }
569      }
570  
571 +    /**
572 +     * Filling an empty array with random bytes succeeds without effect.
573 +     */
574 +    public void testNextBytes_emptyArray() {
575 +        new SplittableRandom().nextBytes(new byte[0]);
576 +    }
577 +
578 +    public void testNextBytes_nullArray() {
579 +        try {
580 +            new SplittableRandom().nextBytes(null);
581 +            shouldThrow();
582 +        } catch (NullPointerException success) {}
583 +    }
584  
585   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines