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.11 by jsr166, Tue Sep 24 16:39:38 2013 UTC vs.
Revision 1.20 by jsr166, Sun Nov 13 03:36:50 2016 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.SplittableRandom;
8   import java.util.concurrent.atomic.AtomicInteger;
10 import java.util.concurrent.atomic.AtomicLong;
9   import java.util.concurrent.atomic.LongAdder;
10  
11 + import junit.framework.Test;
12 + import junit.framework.TestSuite;
13 +
14   public class SplittableRandomTest extends JSR166TestCase {
15  
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run(suite());
17 >        main(suite(), args);
18      }
19      public static Test suite() {
20          return new TestSuite(SplittableRandomTest.class);
# Line 42 | Line 43 | public class SplittableRandomTest extend
43      static final int MAX_INT_BOUND = (1 << 26);
44  
45      // max sampled long bound
46 <    static final long MAX_LONG_BOUND = (1L << 42);
46 >    static final long MAX_LONG_BOUND = (1L << 40);
47  
48      // Number of replications for other checks
49      static final int REPS =
50          Integer.getInteger("SplittableRandomTest.reps", 4);
51  
52      /**
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 +    /**
88       * Repeated calls to nextInt produce at least two distinct results
89       */
90      public void testNextInt() {
# Line 89 | Line 125 | public class SplittableRandomTest extend
125       * same values for nextLong.
126       */
127      public void testSeedConstructor() {
128 <        for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863)  {
128 >        for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) {
129              SplittableRandom sr1 = new SplittableRandom(seed);
130              SplittableRandom sr2 = new SplittableRandom(seed);
131              for (int i = 0; i < REPS; ++i)
# Line 130 | Line 166 | public class SplittableRandomTest extend
166      /**
167       * nextInt(non-positive) throws IllegalArgumentException
168       */
169 <    public void testNextIntNonPositive() {
169 >    public void testNextIntBoundNonPositive() {
170          SplittableRandom sr = new SplittableRandom();
171          Runnable[] throwingActions = {
172              () -> sr.nextInt(-17),
# Line 159 | Line 195 | public class SplittableRandomTest extend
195       */
196      public void testNextIntBounded() {
197          SplittableRandom sr = new SplittableRandom();
198 +        for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1));
199          // sample bound space across prime number increments
200          for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
201              int f = sr.nextInt(bound);
# Line 199 | Line 236 | public class SplittableRandomTest extend
236      /**
237       * nextLong(non-positive) throws IllegalArgumentException
238       */
239 <    public void testNextLongNonPositive() {
239 >    public void testNextLongBoundNonPositive() {
240          SplittableRandom sr = new SplittableRandom();
241          Runnable[] throwingActions = {
242              () -> sr.nextLong(-17L),
# Line 228 | Line 265 | public class SplittableRandomTest extend
265       */
266      public void testNextLongBounded() {
267          SplittableRandom sr = new SplittableRandom();
268 +        for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L));
269          for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
270              long f = sr.nextLong(bound);
271              assertTrue(0 <= f && f < bound);
# Line 267 | Line 305 | public class SplittableRandomTest extend
305      /**
306       * nextDouble(non-positive) throws IllegalArgumentException
307       */
308 <    public void testNextDoubleNonPositive() {
308 >    public void testNextDoubleBoundNonPositive() {
309          SplittableRandom sr = new SplittableRandom();
310          Runnable[] throwingActions = {
311              () -> sr.nextDouble(-17.0d),
# Line 409 | Line 447 | public class SplittableRandomTest extend
447          for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
448              for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
449                  final int lo = least, hi = bound;
450 <                r.ints(size, lo, hi).parallel().
451 <                    forEach(x -> {if (x < lo || x >= hi)
452 <                                fails.getAndIncrement(); });
450 >                r.ints(size, lo, hi).parallel().forEach(
451 >                    x -> {
452 >                        if (x < lo || x >= hi)
453 >                            fails.getAndIncrement(); });
454              }
455          }
456          assertEquals(0, fails.get());
# Line 427 | Line 466 | public class SplittableRandomTest extend
466          for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
467              for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
468                  final long lo = least, hi = bound;
469 <                r.longs(size, lo, hi).parallel().
470 <                    forEach(x -> {if (x < lo || x >= hi)
471 <                                fails.getAndIncrement(); });
469 >                r.longs(size, lo, hi).parallel().forEach(
470 >                    x -> {
471 >                        if (x < lo || x >= hi)
472 >                            fails.getAndIncrement(); });
473              }
474          }
475          assertEquals(0, fails.get());
# Line 445 | Line 485 | public class SplittableRandomTest extend
485          for (double least = 0.00011; least < 1.0e20; least *= 9) {
486              for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
487                  final double lo = least, hi = bound;
488 <                r.doubles(size, lo, hi).parallel().
489 <                    forEach(x -> {if (x < lo || x >= hi)
490 <                                fails.getAndIncrement(); });
488 >                r.doubles(size, lo, hi).parallel().forEach(
489 >                    x -> {
490 >                        if (x < lo || x >= hi)
491 >                            fails.getAndIncrement(); });
492              }
493          }
494          assertEquals(0, fails.get());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines