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.7 by jsr166, Mon Sep 23 17:23:50 2013 UTC vs.
Revision 1.24 by jsr166, Fri Oct 13 16:14:40 2017 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 39 | Line 45 | public class SplittableRandomTest extend
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);
51 >    static final long MAX_LONG_BOUND = (1L << 40);
52  
53      // Number of replications for other checks
54      static final int REPS =
# Line 89 | 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 128 | 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 >        Runnable[] throwingActions = {
142 >            () -> sr.nextInt(-17),
143 >            () -> sr.nextInt(0),
144 >            () -> sr.nextInt(Integer.MIN_VALUE),
145 >        };
146 >        assertThrows(IllegalArgumentException.class, throwingActions);
147      }
148  
149      /**
# Line 143 | Line 151 | public class SplittableRandomTest extend
151       */
152      public void testNextIntBadBounds() {
153          SplittableRandom sr = new SplittableRandom();
154 <        try {
155 <            int f = sr.nextInt(17, 2);
156 <            shouldThrow();
157 <        } catch (IllegalArgumentException success) {}
154 >        Runnable[] throwingActions = {
155 >            () -> sr.nextInt(17, 2),
156 >            () -> sr.nextInt(-42, -42),
157 >            () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE),
158 >        };
159 >        assertThrows(IllegalArgumentException.class, throwingActions);
160      }
161  
162      /**
# Line 155 | Line 165 | public class SplittableRandomTest extend
165       */
166      public void testNextIntBounded() {
167          SplittableRandom sr = new SplittableRandom();
168 +        for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1));
169          // sample bound space across prime number increments
170          for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
171              int f = sr.nextInt(bound);
# Line 193 | Line 204 | public class SplittableRandomTest extend
204      }
205  
206      /**
207 <     * nextLong(negative) throws IllegalArgumentException
207 >     * nextLong(non-positive) throws IllegalArgumentException
208       */
209 <    public void testNextLongBoundedNeg() {
209 >    public void testNextLongBoundNonPositive() {
210          SplittableRandom sr = new SplittableRandom();
211 <        try {
212 <            long f = sr.nextLong(-17);
213 <            shouldThrow();
214 <        } catch (IllegalArgumentException success) {}
211 >        Runnable[] throwingActions = {
212 >            () -> sr.nextLong(-17L),
213 >            () -> sr.nextLong(0L),
214 >            () -> sr.nextLong(Long.MIN_VALUE),
215 >        };
216 >        assertThrows(IllegalArgumentException.class, throwingActions);
217      }
218  
219      /**
# Line 208 | Line 221 | public class SplittableRandomTest extend
221       */
222      public void testNextLongBadBounds() {
223          SplittableRandom sr = new SplittableRandom();
224 <        try {
225 <            long f = sr.nextLong(17, 2);
226 <            shouldThrow();
227 <        } catch (IllegalArgumentException success) {}
224 >        Runnable[] throwingActions = {
225 >            () -> sr.nextLong(17L, 2L),
226 >            () -> sr.nextLong(-42L, -42L),
227 >            () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE),
228 >        };
229 >        assertThrows(IllegalArgumentException.class, throwingActions);
230      }
231  
232      /**
# Line 220 | Line 235 | public class SplittableRandomTest extend
235       */
236      public void testNextLongBounded() {
237          SplittableRandom sr = new SplittableRandom();
238 +        for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L));
239          for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
240              long f = sr.nextLong(bound);
241              assertTrue(0 <= f && f < bound);
# Line 257 | Line 273 | public class SplittableRandomTest extend
273      }
274  
275      /**
276 +     * nextDouble(non-positive) throws IllegalArgumentException
277 +     */
278 +    public void testNextDoubleBoundNonPositive() {
279 +        SplittableRandom sr = new SplittableRandom();
280 +        Runnable[] throwingActions = {
281 +            () -> sr.nextDouble(-17.0d),
282 +            () -> sr.nextDouble(0.0d),
283 +            () -> sr.nextDouble(-Double.MIN_VALUE),
284 +            () -> sr.nextDouble(Double.NEGATIVE_INFINITY),
285 +            () -> sr.nextDouble(Double.NaN),
286 +        };
287 +        assertThrows(IllegalArgumentException.class, throwingActions);
288 +    }
289 +
290 +    /**
291 +     * nextDouble(! (least < bound)) throws IllegalArgumentException
292 +     */
293 +    public void testNextDoubleBadBounds() {
294 +        SplittableRandom sr = new SplittableRandom();
295 +        Runnable[] throwingActions = {
296 +            () -> sr.nextDouble(17.0d, 2.0d),
297 +            () -> sr.nextDouble(-42.0d, -42.0d),
298 +            () -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE),
299 +            () -> sr.nextDouble(Double.NaN, 0.0d),
300 +            () -> sr.nextDouble(0.0d, Double.NaN),
301 +        };
302 +        assertThrows(IllegalArgumentException.class, throwingActions);
303 +    }
304 +
305 +    // TODO: Test infinite bounds!
306 +    //() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d),
307 +    //() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY),
308 +
309 +    /**
310       * nextDouble(least, bound) returns least <= value < bound;
311       * repeated calls produce at least two distinct results
312       */
# Line 284 | Line 334 | public class SplittableRandomTest extend
334       */
335      public void testBadStreamSize() {
336          SplittableRandom r = new SplittableRandom();
337 <        try {
338 <            java.util.stream.IntStream x = r.ints(-1L);
339 <            shouldThrow();
340 <        } catch (IllegalArgumentException success) {}
341 <        try {
342 <            java.util.stream.IntStream x = r.ints(-1L, 2, 3);
343 <            shouldThrow();
344 <        } catch (IllegalArgumentException success) {}
345 <        try {
296 <            java.util.stream.LongStream x = r.longs(-1L);
297 <            shouldThrow();
298 <        } catch (IllegalArgumentException success) {}
299 <        try {
300 <            java.util.stream.LongStream x = r.longs(-1L, -1L, 1L);
301 <            shouldThrow();
302 <        } catch (IllegalArgumentException success) {}
303 <        try {
304 <            java.util.stream.DoubleStream x = r.doubles(-1L);
305 <            shouldThrow();
306 <        } catch (IllegalArgumentException success) {}
307 <        try {
308 <            java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6);
309 <            shouldThrow();
310 <        } catch (IllegalArgumentException success) {}
337 >        Runnable[] throwingActions = {
338 >            () -> { java.util.stream.IntStream x = r.ints(-1L); },
339 >            () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
340 >            () -> { java.util.stream.LongStream x = r.longs(-1L); },
341 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
342 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
343 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); },
344 >        };
345 >        assertThrows(IllegalArgumentException.class, throwingActions);
346      }
347  
348      /**
# Line 316 | Line 351 | public class SplittableRandomTest extend
351       */
352      public void testBadStreamBounds() {
353          SplittableRandom r = new SplittableRandom();
354 <        try {
355 <            java.util.stream.IntStream x = r.ints(2, 1);
356 <            shouldThrow();
357 <        } catch (IllegalArgumentException success) {}
358 <        try {
359 <            java.util.stream.IntStream x = r.ints(10, 42, 42);
360 <            shouldThrow();
361 <        } catch (IllegalArgumentException success) {}
362 <        try {
328 <            java.util.stream.LongStream x = r.longs(-1L, -1L);
329 <            shouldThrow();
330 <        } catch (IllegalArgumentException success) {}
331 <        try {
332 <            java.util.stream.LongStream x = r.longs(10, 1L, -2L);
333 <            shouldThrow();
334 <        } catch (IllegalArgumentException success) {}
335 <        try {
336 <            java.util.stream.DoubleStream x = r.doubles(0.0, 0.0);
337 <            shouldThrow();
338 <        } catch (IllegalArgumentException success) {}
339 <        try {
340 <            java.util.stream.DoubleStream x = r.doubles(10, .5, .4);
341 <            shouldThrow();
342 <        } catch (IllegalArgumentException success) {}
354 >        Runnable[] throwingActions = {
355 >            () -> { java.util.stream.IntStream x = r.ints(2, 1); },
356 >            () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
357 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
358 >            () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
359 >            () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
360 >            () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); },
361 >        };
362 >        assertThrows(IllegalArgumentException.class, throwingActions);
363      }
364  
365      /**
# Line 397 | Line 417 | public class SplittableRandomTest extend
417          for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
418              for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
419                  final int lo = least, hi = bound;
420 <                r.ints(size, lo, hi).parallel().
421 <                    forEach(x -> {if (x < lo || x >= hi)
422 <                                fails.getAndIncrement(); });
420 >                r.ints(size, lo, hi).parallel().forEach(
421 >                    x -> {
422 >                        if (x < lo || x >= hi)
423 >                            fails.getAndIncrement(); });
424              }
425          }
426          assertEquals(0, fails.get());
# Line 415 | Line 436 | public class SplittableRandomTest extend
436          for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
437              for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
438                  final long lo = least, hi = bound;
439 <                r.longs(size, lo, hi).parallel().
440 <                    forEach(x -> {if (x < lo || x >= hi)
441 <                                fails.getAndIncrement(); });
439 >                r.longs(size, lo, hi).parallel().forEach(
440 >                    x -> {
441 >                        if (x < lo || x >= hi)
442 >                            fails.getAndIncrement(); });
443              }
444          }
445          assertEquals(0, fails.get());
# Line 433 | Line 455 | public class SplittableRandomTest extend
455          for (double least = 0.00011; least < 1.0e20; least *= 9) {
456              for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
457                  final double lo = least, hi = bound;
458 <                r.doubles(size, lo, hi).parallel().
459 <                    forEach(x -> {if (x < lo || x >= hi)
460 <                                fails.getAndIncrement(); });
458 >                r.doubles(size, lo, hi).parallel().forEach(
459 >                    x -> {
460 >                        if (x < lo || x >= hi)
461 >                            fails.getAndIncrement(); });
462              }
463          }
464          assertEquals(0, fails.get());
# Line 507 | Line 530 | public class SplittableRandomTest extend
530          assertEquals(size, counter.sum());
531      }
532  
533 +    /**
534 +     * SplittableRandom should implement most of Random's public methods
535 +     */
536 +    public void testShouldImplementMostRandomMethods() throws Throwable {
537 +        Predicate<Method> wasForgotten = method -> {
538 +            String name = method.getName();
539 +            // some methods deliberately not implemented
540 +            if (name.equals("setSeed")) return false;
541 +            if (name.equals("nextFloat")) return false;
542 +            if (name.equals("nextGaussian")) return false;
543 +            try {
544 +                SplittableRandom.class.getMethod(
545 +                    method.getName(), method.getParameterTypes());
546 +            } catch (ReflectiveOperationException ex) {
547 +                return true;
548 +            }
549 +            return false;
550 +        };
551 +        List<Method> forgotten =
552 +            Arrays.stream(java.util.Random.class.getMethods())
553 +            .filter(wasForgotten)
554 +            .collect(Collectors.toList());
555 +        if (!forgotten.isEmpty())
556 +            throw new AssertionError("Please implement: " + forgotten);
557 +    }
558 +
559 +    /**
560 +     * Repeated calls to nextBytes produce at least values of different signs for every byte
561 +     */
562 +    public void testNextBytes() {
563 +        SplittableRandom sr = new SplittableRandom();
564 +        int n = sr.nextInt(1, 20);
565 +        byte[] bytes = new byte[n];
566 +        outer:
567 +        for (int i = 0; i < n; i++) {
568 +            for (int tries = NCALLS; tries-->0; ) {
569 +                byte before = bytes[i];
570 +                sr.nextBytes(bytes);
571 +                byte after = bytes[i];
572 +                if (after * before < 0)
573 +                    continue outer;
574 +            }
575 +            fail("not enough variation in random bytes");
576 +        }
577 +    }
578 +
579 +    /**
580 +     * Filling an empty array with random bytes succeeds without effect.
581 +     */
582 +    public void testNextBytes_emptyArray() {
583 +        new SplittableRandom().nextBytes(new byte[0]);
584 +    }
585 +
586 +    public void testNextBytes_nullArray() {
587 +        try {
588 +            new SplittableRandom().nextBytes(null);
589 +            shouldThrow();
590 +        } catch (NullPointerException success) {}
591 +    }
592 +
593   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines