< prev index next >

test/jdk/java/util/concurrent/tck/SplittableRandomTest.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/publicdomain/zero/1.0/
  32  */
  33 
  34 import java.util.Arrays;
  35 import java.util.List;
  36 import java.util.SplittableRandom;
  37 import java.util.concurrent.atomic.AtomicInteger;
  38 import java.util.concurrent.atomic.LongAdder;
  39 import java.lang.reflect.Method;
  40 import java.util.function.Predicate;
  41 import java.util.stream.Collectors;



  42 
  43 import junit.framework.Test;
  44 import junit.framework.TestSuite;
  45 
  46 public class SplittableRandomTest extends JSR166TestCase {
  47 
  48     public static void main(String[] args) {
  49         main(suite(), args);
  50     }
  51     public static Test suite() {
  52         return new TestSuite(SplittableRandomTest.class);
  53     }
  54 
  55     /*
  56      * Testing coverage notes:
  57      *
  58      * 1. Many of the test methods are adapted from ThreadLocalRandomTest.
  59      *
  60      * 2. These tests do not check for random number generator quality.
  61      * But we check for minimal API compliance by requiring that


 340                 int i = 0;
 341                 double j;
 342                 while (i < NCALLS &&
 343                        (j = sr.nextDouble(least, bound)) == f) {
 344                     assertTrue(least <= j && j < bound);
 345                     ++i;
 346                 }
 347                 assertTrue(i < NCALLS);
 348             }
 349         }
 350     }
 351 
 352     /**
 353      * Invoking sized ints, long, doubles, with negative sizes throws
 354      * IllegalArgumentException
 355      */
 356     public void testBadStreamSize() {
 357         SplittableRandom r = new SplittableRandom();
 358         assertThrows(
 359             IllegalArgumentException.class,
 360             () -> { java.util.stream.IntStream x = r.ints(-1L); },
 361             () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
 362             () -> { java.util.stream.LongStream x = r.longs(-1L); },
 363             () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
 364             () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
 365             () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); });
 366     }
 367 
 368     /**
 369      * Invoking bounded ints, long, doubles, with illegal bounds throws
 370      * IllegalArgumentException
 371      */
 372     public void testBadStreamBounds() {
 373         SplittableRandom r = new SplittableRandom();
 374         assertThrows(
 375             IllegalArgumentException.class,
 376             () -> { java.util.stream.IntStream x = r.ints(2, 1); },
 377             () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
 378             () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
 379             () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
 380             () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
 381             () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); });
 382     }
 383 
 384     /**
 385      * A parallel sized stream of ints generates the given number of values
 386      */
 387     public void testIntsCount() {
 388         LongAdder counter = new LongAdder();
 389         SplittableRandom r = new SplittableRandom();
 390         long size = 0;
 391         for (int reps = 0; reps < REPS; ++reps) {
 392             counter.reset();
 393             r.ints(size).parallel().forEach(x -> counter.increment());
 394             assertEquals(size, counter.sum());
 395             size += 524959;
 396         }
 397     }
 398 
 399     /**
 400      * A parallel sized stream of longs generates the given number of values
 401      */




  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/publicdomain/zero/1.0/
  32  */
  33 
  34 import java.util.Arrays;
  35 import java.util.List;
  36 import java.util.SplittableRandom;
  37 import java.util.concurrent.atomic.AtomicInteger;
  38 import java.util.concurrent.atomic.LongAdder;
  39 import java.lang.reflect.Method;
  40 import java.util.function.Predicate;
  41 import java.util.stream.Collectors;
  42 import java.util.stream.DoubleStream;
  43 import java.util.stream.IntStream;
  44 import java.util.stream.LongStream;
  45 
  46 import junit.framework.Test;
  47 import junit.framework.TestSuite;
  48 
  49 public class SplittableRandomTest extends JSR166TestCase {
  50 
  51     public static void main(String[] args) {
  52         main(suite(), args);
  53     }
  54     public static Test suite() {
  55         return new TestSuite(SplittableRandomTest.class);
  56     }
  57 
  58     /*
  59      * Testing coverage notes:
  60      *
  61      * 1. Many of the test methods are adapted from ThreadLocalRandomTest.
  62      *
  63      * 2. These tests do not check for random number generator quality.
  64      * But we check for minimal API compliance by requiring that


 343                 int i = 0;
 344                 double j;
 345                 while (i < NCALLS &&
 346                        (j = sr.nextDouble(least, bound)) == f) {
 347                     assertTrue(least <= j && j < bound);
 348                     ++i;
 349                 }
 350                 assertTrue(i < NCALLS);
 351             }
 352         }
 353     }
 354 
 355     /**
 356      * Invoking sized ints, long, doubles, with negative sizes throws
 357      * IllegalArgumentException
 358      */
 359     public void testBadStreamSize() {
 360         SplittableRandom r = new SplittableRandom();
 361         assertThrows(
 362             IllegalArgumentException.class,
 363             () -> { IntStream unused = r.ints(-1L); },
 364             () -> { IntStream unused = r.ints(-1L, 2, 3); },
 365             () -> { LongStream unused = r.longs(-1L); },
 366             () -> { LongStream unused = r.longs(-1L, -1L, 1L); },
 367             () -> { DoubleStream unused = r.doubles(-1L); },
 368             () -> { DoubleStream unused = r.doubles(-1L, .5, .6); });
 369     }
 370 
 371     /**
 372      * Invoking bounded ints, long, doubles, with illegal bounds throws
 373      * IllegalArgumentException
 374      */
 375     public void testBadStreamBounds() {
 376         SplittableRandom r = new SplittableRandom();
 377         assertThrows(
 378             IllegalArgumentException.class,
 379             () -> { IntStream unused = r.ints(2, 1); },
 380             () -> { IntStream unused = r.ints(10, 42, 42); },
 381             () -> { LongStream unused = r.longs(-1L, -1L); },
 382             () -> { LongStream unused = r.longs(10, 1L, -2L); },
 383             () -> { DoubleStream unused = r.doubles(0.0, 0.0); },
 384             () -> { DoubleStream unused = r.doubles(10, .5, .4); });
 385     }
 386 
 387     /**
 388      * A parallel sized stream of ints generates the given number of values
 389      */
 390     public void testIntsCount() {
 391         LongAdder counter = new LongAdder();
 392         SplittableRandom r = new SplittableRandom();
 393         long size = 0;
 394         for (int reps = 0; reps < REPS; ++reps) {
 395             counter.reset();
 396             r.ints(size).parallel().forEach(x -> counter.increment());
 397             assertEquals(size, counter.sum());
 398             size += 524959;
 399         }
 400     }
 401 
 402     /**
 403      * A parallel sized stream of longs generates the given number of values
 404      */


< prev index next >