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.21 by jsr166, Thu Nov 17 22:09:52 2016 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 525 | 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