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.16 by jsr166, Wed Dec 31 19:05:43 2014 UTC vs.
Revision 1.24 by jsr166, Fri Oct 13 16:14:40 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.List;
9   import java.util.SplittableRandom;
10   import java.util.concurrent.atomic.AtomicInteger;
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;
# Line 14 | Line 19 | import junit.framework.TestSuite;
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 160 | 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 229 | 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 410 | 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 428 | 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 446 | 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 520 | 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