1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import org.testng.annotations.DataProvider;
  25 import org.testng.annotations.Test;
  26 
  27 import java.security.SecureRandom;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import java.util.Random;
  32 import java.util.Set;
  33 import java.util.concurrent.CompletableFuture;
  34 import java.util.concurrent.ExecutionException;
  35 import java.util.concurrent.ThreadLocalRandom;
  36 import java.util.concurrent.TimeUnit;
  37 import java.util.concurrent.TimeoutException;
  38 import java.util.function.Supplier;
  39 import java.util.stream.Stream;
  40 
  41 import static java.util.stream.Collectors.toList;
  42 import static java.util.stream.Collectors.toSet;
  43 import static org.testng.Assert.*;
  44 
  45 /**
  46  * @test
  47  * @run testng RandomStreamTest
  48  * @summary test stream methods on Random
  49  * @author Brian Goetz
  50  */
  51 public class RandomStreamTest {
  52 
  53     private static final int SIZE = 1000;
  54 
  55     @DataProvider(name = "suppliers")
  56     public Object[][] randomSuppliers() {
  57         return new Object[][] {
  58             {new Random(), SIZE},
  59             {new SecureRandom(), SIZE}
  60         };
  61     }
  62 
  63     @Test(dataProvider = "suppliers")
  64     public void testRandomIntStream(final Random random, final int count) {
  65         final List<Integer> destination = new ArrayList<>(count);
  66         random.ints().limit(count).forEach(destination::add);
  67         assertEquals(destination.size(), count);
  68     }
  69 
  70     @Test(dataProvider = "suppliers")
  71     public void testRandomLongStream(final Random random, final int count) {
  72         final List<Long> destination = new ArrayList<>(count);
  73         random.longs().limit(count).forEach(destination::add);
  74         assertEquals(destination.size(), count);
  75     }
  76 
  77     @Test(dataProvider = "suppliers")
  78     public void testRandomDoubleStream(final Random random, final int count) {
  79         final List<Double> destination = new ArrayList<>(count);
  80         random.doubles().limit(count).forEach(destination::add);
  81         random.doubles().limit(count).forEach(d -> assertTrue(d >= 0.0 && d < 1.0));
  82         assertEquals(destination.size(), count);
  83     }
  84 
  85     @Test
  86     public void testIntStream() {
  87         final long seed = System.currentTimeMillis();
  88         final Random r1 = new Random(seed);
  89         final int[] a = new int[SIZE];
  90         for (int i=0; i < SIZE; i++) {
  91             a[i] = r1.nextInt();
  92         }
  93 
  94         final Random r2 = new Random(seed); // same seed
  95         final int[] b = r2.ints().limit(SIZE).toArray();
  96         assertEquals(a, b);
  97     }
  98 
  99     @Test
 100     public void testLongStream() {
 101         final long seed = System.currentTimeMillis();
 102         final Random r1 = new Random(seed);
 103         final long[] a = new long[SIZE];
 104         for (int i=0; i < SIZE; i++) {
 105             a[i] = r1.nextLong();
 106         }
 107 
 108         final Random r2 = new Random(seed); // same seed
 109         final long[] b = r2.longs().limit(SIZE).toArray();
 110         assertEquals(a, b);
 111     }
 112 
 113     @Test
 114     public void testDoubleStream() {
 115         final long seed = System.currentTimeMillis();
 116         final Random r1 = new Random(seed);
 117         final double[] a = new double[SIZE];
 118         for (int i=0; i < SIZE; i++) {
 119             a[i] = r1.nextDouble();
 120         }
 121 
 122         final Random r2 = new Random(seed); // same seed
 123         final double[] b = r2.doubles().limit(SIZE).toArray();
 124         assertEquals(a, b);
 125     }
 126 
 127     @Test
 128     public void testThreadLocalIntStream() throws InterruptedException, ExecutionException, TimeoutException {
 129         ThreadLocalRandom tlr = ThreadLocalRandom.current();
 130         testRandomResultSupplierConcurrently(() -> tlr.ints().limit(SIZE).boxed().collect(toList()));
 131     }
 132 
 133     @Test
 134     public void testThreadLocalLongStream() throws InterruptedException, ExecutionException, TimeoutException {
 135         ThreadLocalRandom tlr = ThreadLocalRandom.current();
 136         testRandomResultSupplierConcurrently(() -> tlr.longs().limit(SIZE).boxed().collect(toList()));
 137     }
 138 
 139     @Test
 140     public void testThreadLocalDoubleStream() throws InterruptedException, ExecutionException, TimeoutException {
 141         ThreadLocalRandom tlr = ThreadLocalRandom.current();
 142         testRandomResultSupplierConcurrently(() -> tlr.doubles().limit(SIZE).boxed().collect(toList()));
 143     }
 144 
 145     <T> void testRandomResultSupplierConcurrently(Supplier<T> s) throws InterruptedException, ExecutionException, TimeoutException {
 146         // Produce 10 completable future tasks
 147         final int tasks = 10;
 148         List<CompletableFuture<T>> cfs = Stream.generate(() -> CompletableFuture.supplyAsync(s)).
 149                 limit(tasks).collect(toList());
 150 
 151         // Wait for all tasks to complete
 152         // Timeout is beyond reasonable doubt that completion should
 153         // have occurred unless there is an issue
 154         CompletableFuture<Void> all = CompletableFuture.allOf(cfs.stream().toArray(CompletableFuture[]::new));
 155         all.get(1, TimeUnit.MINUTES);
 156 
 157         // Count the distinct results, which should equal the number of tasks
 158         long rc = cfs.stream().map(CompletableFuture::join).distinct().count();
 159         assertEquals(rc, tasks);
 160     }
 161 }