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.20 by jsr166, Sun Nov 13 03:36:50 2016 UTC vs.
Revision 1.23 by jsr166, Fri Oct 13 02:34:59 2017 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 + import java.util.Arrays;
8 + import java.util.ArrayList;
9 + import java.util.List;
10   import java.util.SplittableRandom;
11   import java.util.concurrent.atomic.AtomicInteger;
12   import java.util.concurrent.atomic.LongAdder;
13 + import java.lang.reflect.Method;
14 + import java.util.function.Predicate;
15 + import java.util.stream.Collectors;
16  
17   import junit.framework.Test;
18   import junit.framework.TestSuite;
# Line 50 | Line 56 | public class SplittableRandomTest extend
56          Integer.getInteger("SplittableRandomTest.reps", 4);
57  
58      /**
53     * Repeated calls to next (only accessible via reflection) produce
54     * at least two distinct results, and repeated calls produce all
55     * possible values.
56     */
57    public void testNext() throws ReflectiveOperationException {
58        SplittableRandom rnd = new SplittableRandom();
59        try {
60            java.lang.reflect.Method m
61                = SplittableRandom.class.getDeclaredMethod(
62                    "next", new Class[] { int.class });
63            m.setAccessible(true);
64
65            int i;
66            {
67                int val = new java.util.Random().nextInt(4);
68                for (i = 0; i < NCALLS; i++) {
69                    int q = (int) m.invoke(rnd, new Object[] { 2 });
70                    if (val == q) break;
71                }
72                assertTrue(i < NCALLS);
73            }
74
75            {
76                int r = (int) m.invoke(rnd, new Object[] { 3 });
77                for (i = 0; i < NCALLS; i++) {
78                    int q = (int) m.invoke(rnd, new Object[] { 3 });
79                    assertTrue(q < (1<<3));
80                    if (r != q) break;
81                }
82                assertTrue(i < NCALLS);
83            }
84        } catch (SecurityException acceptable) {}
85    }
86
87    /**
59       * Repeated calls to nextInt produce at least two distinct results
60       */
61      public void testNextInt() {
# Line 560 | Line 531 | public class SplittableRandomTest extend
531          assertEquals(size, counter.sum());
532      }
533  
534 +    /**
535 +     * SplittableRandom should implement most of Random's public methods
536 +     */
537 +    public void testShouldImplementMostRandomMethods() throws Throwable {
538 +        Predicate<Method> wasForgotten = method -> {
539 +            String name = method.getName();
540 +            // some methods deliberately not implemented
541 +            if (name.equals("setSeed")) return false;
542 +            if (name.equals("nextFloat")) return false;
543 +            if (name.equals("nextGaussian")) return false;
544 +            try {
545 +                SplittableRandom.class.getMethod(
546 +                    method.getName(), method.getParameterTypes());
547 +            } catch (ReflectiveOperationException ex) {
548 +                return true;
549 +            }
550 +            return false;
551 +        };
552 +        List<Method> forgotten =
553 +            Arrays.stream(java.util.Random.class.getMethods())
554 +            .filter(wasForgotten)
555 +            .collect(Collectors.toList());
556 +        if (!forgotten.isEmpty())
557 +            throw new AssertionError("Please implement: " + forgotten);
558 +    }
559 +
560 +    /**
561 +     * Repeated calls to nextBytes produce at least values of different signs for every byte
562 +     */
563 +    public void testNextBytes() {
564 +        SplittableRandom sr = new SplittableRandom();
565 +        int n = sr.nextInt(1, 20);
566 +        byte[] bytes = new byte[n];
567 +        outer:
568 +        for (int i = 0; i < n; i++) {
569 +            for (int tries = NCALLS; tries-->0; ) {
570 +                byte before = bytes[i];
571 +                sr.nextBytes(bytes);
572 +                byte after = bytes[i];
573 +                if (after * before < 0)
574 +                    continue outer;
575 +            }
576 +            fail("not enough variation in random bytes");
577 +        }
578 +    }
579 +
580 +    /**
581 +     * Filling an empty array with random bytes succeeds without effect.
582 +     */
583 +    public void testNextBytes_emptyArray() {
584 +        new SplittableRandom().nextBytes(new byte[0]);
585 +    }
586 +
587 +    public void testNextBytes_nullArray() {
588 +        try {
589 +            new SplittableRandom().nextBytes(null);
590 +            shouldThrow();
591 +        } catch (NullPointerException success) {}
592 +    }
593 +
594   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines