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.19 by jsr166, Sun Sep 20 21:16:08 2015 UTC vs.
Revision 1.25 by jsr166, Fri Feb 22 19:27:47 2019 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 133 | Line 138 | public class SplittableRandomTest extend
138       */
139      public void testNextIntBoundNonPositive() {
140          SplittableRandom sr = new SplittableRandom();
141 <        Runnable[] throwingActions = {
141 >        assertThrows(
142 >            IllegalArgumentException.class,
143              () -> sr.nextInt(-17),
144              () -> sr.nextInt(0),
145 <            () -> sr.nextInt(Integer.MIN_VALUE),
140 <        };
141 <        assertThrows(IllegalArgumentException.class, throwingActions);
145 >            () -> sr.nextInt(Integer.MIN_VALUE));
146      }
147  
148      /**
# Line 146 | Line 150 | public class SplittableRandomTest extend
150       */
151      public void testNextIntBadBounds() {
152          SplittableRandom sr = new SplittableRandom();
153 <        Runnable[] throwingActions = {
153 >        assertThrows(
154 >            IllegalArgumentException.class,
155              () -> sr.nextInt(17, 2),
156              () -> sr.nextInt(-42, -42),
157 <            () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE),
153 <        };
154 <        assertThrows(IllegalArgumentException.class, throwingActions);
157 >            () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE));
158      }
159  
160      /**
# Line 203 | Line 206 | public class SplittableRandomTest extend
206       */
207      public void testNextLongBoundNonPositive() {
208          SplittableRandom sr = new SplittableRandom();
209 <        Runnable[] throwingActions = {
209 >        assertThrows(
210 >            IllegalArgumentException.class,
211              () -> sr.nextLong(-17L),
212              () -> sr.nextLong(0L),
213 <            () -> sr.nextLong(Long.MIN_VALUE),
210 <        };
211 <        assertThrows(IllegalArgumentException.class, throwingActions);
213 >            () -> sr.nextLong(Long.MIN_VALUE));
214      }
215  
216      /**
# Line 216 | Line 218 | public class SplittableRandomTest extend
218       */
219      public void testNextLongBadBounds() {
220          SplittableRandom sr = new SplittableRandom();
221 <        Runnable[] throwingActions = {
221 >        assertThrows(
222 >            IllegalArgumentException.class,
223              () -> sr.nextLong(17L, 2L),
224              () -> sr.nextLong(-42L, -42L),
225 <            () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE),
223 <        };
224 <        assertThrows(IllegalArgumentException.class, throwingActions);
225 >            () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE));
226      }
227  
228      /**
# Line 272 | Line 273 | public class SplittableRandomTest extend
273       */
274      public void testNextDoubleBoundNonPositive() {
275          SplittableRandom sr = new SplittableRandom();
276 <        Runnable[] throwingActions = {
276 >        assertThrows(
277 >            IllegalArgumentException.class,
278              () -> sr.nextDouble(-17.0d),
279              () -> sr.nextDouble(0.0d),
280              () -> sr.nextDouble(-Double.MIN_VALUE),
281              () -> sr.nextDouble(Double.NEGATIVE_INFINITY),
282 <            () -> sr.nextDouble(Double.NaN),
281 <        };
282 <        assertThrows(IllegalArgumentException.class, throwingActions);
282 >            () -> sr.nextDouble(Double.NaN));
283      }
284  
285      /**
# Line 287 | Line 287 | public class SplittableRandomTest extend
287       */
288      public void testNextDoubleBadBounds() {
289          SplittableRandom sr = new SplittableRandom();
290 <        Runnable[] throwingActions = {
290 >        assertThrows(
291 >            IllegalArgumentException.class,
292              () -> sr.nextDouble(17.0d, 2.0d),
293              () -> sr.nextDouble(-42.0d, -42.0d),
294              () -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE),
295              () -> sr.nextDouble(Double.NaN, 0.0d),
296 <            () -> sr.nextDouble(0.0d, Double.NaN),
296 <        };
297 <        assertThrows(IllegalArgumentException.class, throwingActions);
296 >            () -> sr.nextDouble(0.0d, Double.NaN));
297      }
298  
299      // TODO: Test infinite bounds!
# Line 329 | Line 328 | public class SplittableRandomTest extend
328       */
329      public void testBadStreamSize() {
330          SplittableRandom r = new SplittableRandom();
331 <        Runnable[] throwingActions = {
331 >        assertThrows(
332 >            IllegalArgumentException.class,
333              () -> { java.util.stream.IntStream x = r.ints(-1L); },
334              () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
335              () -> { java.util.stream.LongStream x = r.longs(-1L); },
336              () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
337              () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
338 <            () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); },
339 <        };
340 <        assertThrows(IllegalArgumentException.class, throwingActions);
338 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); });
339      }
340  
341      /**
# Line 346 | Line 344 | public class SplittableRandomTest extend
344       */
345      public void testBadStreamBounds() {
346          SplittableRandom r = new SplittableRandom();
347 <        Runnable[] throwingActions = {
347 >        assertThrows(
348 >            IllegalArgumentException.class,
349              () -> { java.util.stream.IntStream x = r.ints(2, 1); },
350              () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
351              () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
352              () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
353              () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
354 <            () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); },
356 <        };
357 <        assertThrows(IllegalArgumentException.class, throwingActions);
354 >            () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); });
355      }
356  
357      /**
# Line 525 | Line 522 | public class SplittableRandomTest extend
522          assertEquals(size, counter.sum());
523      }
524  
525 +    /**
526 +     * SplittableRandom should implement most of Random's public methods
527 +     */
528 +    public void testShouldImplementMostRandomMethods() throws Throwable {
529 +        Predicate<Method> wasForgotten = method -> {
530 +            String name = method.getName();
531 +            // some methods deliberately not implemented
532 +            if (name.equals("setSeed")) return false;
533 +            if (name.equals("nextFloat")) return false;
534 +            if (name.equals("nextGaussian")) return false;
535 +            try {
536 +                SplittableRandom.class.getMethod(
537 +                    method.getName(), method.getParameterTypes());
538 +            } catch (ReflectiveOperationException ex) {
539 +                return true;
540 +            }
541 +            return false;
542 +        };
543 +        List<Method> forgotten =
544 +            Arrays.stream(java.util.Random.class.getMethods())
545 +            .filter(wasForgotten)
546 +            .collect(Collectors.toList());
547 +        if (!forgotten.isEmpty())
548 +            throw new AssertionError("Please implement: " + forgotten);
549 +    }
550 +
551 +    /**
552 +     * Repeated calls to nextBytes produce at least values of different signs for every byte
553 +     */
554 +    public void testNextBytes() {
555 +        SplittableRandom sr = new SplittableRandom();
556 +        int n = sr.nextInt(1, 20);
557 +        byte[] bytes = new byte[n];
558 +        outer:
559 +        for (int i = 0; i < n; i++) {
560 +            for (int tries = NCALLS; tries-->0; ) {
561 +                byte before = bytes[i];
562 +                sr.nextBytes(bytes);
563 +                byte after = bytes[i];
564 +                if (after * before < 0)
565 +                    continue outer;
566 +            }
567 +            fail("not enough variation in random bytes");
568 +        }
569 +    }
570 +
571 +    /**
572 +     * Filling an empty array with random bytes succeeds without effect.
573 +     */
574 +    public void testNextBytes_emptyArray() {
575 +        new SplittableRandom().nextBytes(new byte[0]);
576 +    }
577 +
578 +    public void testNextBytes_nullArray() {
579 +        try {
580 +            new SplittableRandom().nextBytes(null);
581 +            shouldThrow();
582 +        } catch (NullPointerException success) {}
583 +    }
584 +
585   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines