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 package test.java.util.Random; 25 26 import org.testng.annotations.DataProvider; 27 import org.testng.annotations.Test; 28 29 import java.security.SecureRandom; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 import java.util.Random; 34 import java.util.Set; 35 import java.util.concurrent.CompletableFuture; 36 import java.util.concurrent.ExecutionException; 37 import java.util.concurrent.ThreadLocalRandom; 38 import java.util.concurrent.TimeUnit; 39 import java.util.concurrent.TimeoutException; 40 import java.util.function.Supplier; 41 import java.util.stream.Stream; 42 43 import static java.util.stream.Collectors.toList; 44 import static java.util.stream.Collectors.toSet; 45 import static org.testng.Assert.*; 46 47 /** 48 * @test 49 * @run testng RandomStreamTest 50 * @summary test stream methods on Random 51 * @author Brian Goetz 52 * @key randomness 53 */ 54 public class RandomStreamTest { 55 56 private static final int SIZE = 1000; 57 58 @DataProvider(name = "suppliers") randomSuppliers()59 public Object[][] randomSuppliers() { 60 return new Object[][] { 61 {new Random(), SIZE}, 62 {new SecureRandom(), SIZE} 63 }; 64 } 65 66 @Test(dataProvider = "suppliers") testRandomIntStream(final Random random, final int count)67 public void testRandomIntStream(final Random random, final int count) { 68 final List<Integer> destination = new ArrayList<>(count); 69 random.ints().limit(count).forEach(destination::add); 70 assertEquals(destination.size(), count); 71 } 72 73 @Test(dataProvider = "suppliers") testRandomLongStream(final Random random, final int count)74 public void testRandomLongStream(final Random random, final int count) { 75 final List<Long> destination = new ArrayList<>(count); 76 random.longs().limit(count).forEach(destination::add); 77 assertEquals(destination.size(), count); 78 } 79 80 @Test(dataProvider = "suppliers") testRandomDoubleStream(final Random random, final int count)81 public void testRandomDoubleStream(final Random random, final int count) { 82 final List<Double> destination = new ArrayList<>(count); 83 random.doubles().limit(count).forEach(destination::add); 84 random.doubles().limit(count).forEach(d -> assertTrue(d >= 0.0 && d < 1.0)); 85 assertEquals(destination.size(), count); 86 } 87 88 @Test testIntStream()89 public void testIntStream() { 90 final long seed = System.currentTimeMillis(); 91 final Random r1 = new Random(seed); 92 final int[] a = new int[SIZE]; 93 for (int i=0; i < SIZE; i++) { 94 a[i] = r1.nextInt(); 95 } 96 97 final Random r2 = new Random(seed); // same seed 98 final int[] b = r2.ints().limit(SIZE).toArray(); 99 assertEquals(a, b); 100 } 101 102 @Test testLongStream()103 public void testLongStream() { 104 final long seed = System.currentTimeMillis(); 105 final Random r1 = new Random(seed); 106 final long[] a = new long[SIZE]; 107 for (int i=0; i < SIZE; i++) { 108 a[i] = r1.nextLong(); 109 } 110 111 final Random r2 = new Random(seed); // same seed 112 final long[] b = r2.longs().limit(SIZE).toArray(); 113 assertEquals(a, b); 114 } 115 116 @Test testDoubleStream()117 public void testDoubleStream() { 118 final long seed = System.currentTimeMillis(); 119 final Random r1 = new Random(seed); 120 final double[] a = new double[SIZE]; 121 for (int i=0; i < SIZE; i++) { 122 a[i] = r1.nextDouble(); 123 } 124 125 final Random r2 = new Random(seed); // same seed 126 final double[] b = r2.doubles().limit(SIZE).toArray(); 127 assertEquals(a, b); 128 } 129 130 @Test testThreadLocalIntStream()131 public void testThreadLocalIntStream() throws InterruptedException, ExecutionException, TimeoutException { 132 ThreadLocalRandom tlr = ThreadLocalRandom.current(); 133 testRandomResultSupplierConcurrently(() -> tlr.ints().limit(SIZE).boxed().collect(toList())); 134 } 135 136 @Test testThreadLocalLongStream()137 public void testThreadLocalLongStream() throws InterruptedException, ExecutionException, TimeoutException { 138 ThreadLocalRandom tlr = ThreadLocalRandom.current(); 139 testRandomResultSupplierConcurrently(() -> tlr.longs().limit(SIZE).boxed().collect(toList())); 140 } 141 142 @Test testThreadLocalDoubleStream()143 public void testThreadLocalDoubleStream() throws InterruptedException, ExecutionException, TimeoutException { 144 ThreadLocalRandom tlr = ThreadLocalRandom.current(); 145 testRandomResultSupplierConcurrently(() -> tlr.doubles().limit(SIZE).boxed().collect(toList())); 146 } 147 testRandomResultSupplierConcurrently(Supplier<T> s)148 <T> void testRandomResultSupplierConcurrently(Supplier<T> s) throws InterruptedException, ExecutionException, TimeoutException { 149 // Produce 10 completable future tasks 150 final int tasks = 10; 151 List<CompletableFuture<T>> cfs = Stream.generate(() -> CompletableFuture.supplyAsync(s)). 152 limit(tasks).collect(toList()); 153 154 // Wait for all tasks to complete 155 // Timeout is beyond reasonable doubt that completion should 156 // have occurred unless there is an issue 157 CompletableFuture<Void> all = CompletableFuture.allOf(cfs.stream().toArray(CompletableFuture[]::new)); 158 all.get(1, TimeUnit.MINUTES); 159 160 // Count the distinct results, which should equal the number of tasks 161 long rc = cfs.stream().map(CompletableFuture::join).distinct().count(); 162 assertEquals(rc, tasks); 163 } 164 } 165