blob: f7b799c484a2762bd04a4b61ccb87c72b093129a [file] [log] [blame]
mduigou072224c2013-05-02 09:18:56 -07001/*
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
24import org.testng.annotations.DataProvider;
25import org.testng.annotations.Test;
26
27import java.security.SecureRandom;
mduigou072224c2013-05-02 09:18:56 -070028import java.util.ArrayList;
psandoz8bc2ea62013-07-30 14:03:25 +010029import java.util.List;
mduigou072224c2013-05-02 09:18:56 -070030
psandoz8bc2ea62013-07-30 14:03:25 +010031import java.util.Random;
32import java.util.Set;
33import java.util.concurrent.CompletableFuture;
34import java.util.concurrent.ExecutionException;
mduigou072224c2013-05-02 09:18:56 -070035import java.util.concurrent.ThreadLocalRandom;
36import java.util.concurrent.TimeUnit;
psandoz8bc2ea62013-07-30 14:03:25 +010037import java.util.concurrent.TimeoutException;
38import java.util.function.Supplier;
mduigou072224c2013-05-02 09:18:56 -070039import java.util.stream.Stream;
40
psandoz8bc2ea62013-07-30 14:03:25 +010041import static java.util.stream.Collectors.toList;
42import static java.util.stream.Collectors.toSet;
43import static org.testng.Assert.*;
mduigou072224c2013-05-02 09:18:56 -070044
45/**
46 * @test
47 * @run testng RandomStreamTest
48 * @summary test stream methods on Random
49 * @author Brian Goetz
50 */
51public 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(dataProvider = "suppliers")
86 public void testRandomGaussianStream(final Random random, final int count) {
87 final List<Double> destination = new ArrayList<>(count);
88 random.gaussians().limit(count).forEach(destination::add);
89 assertEquals(destination.size(), count);
90 }
91
92 @Test
93 public void testIntStream() {
94 final long seed = System.currentTimeMillis();
95 final Random r1 = new Random(seed);
96 final int[] a = new int[SIZE];
97 for (int i=0; i < SIZE; i++) {
98 a[i] = r1.nextInt();
99 }
100
101 final Random r2 = new Random(seed); // same seed
102 final int[] b = r2.ints().limit(SIZE).toArray();
103 assertEquals(a, b);
104 }
105
106 @Test
107 public void testLongStream() {
108 final long seed = System.currentTimeMillis();
109 final Random r1 = new Random(seed);
110 final long[] a = new long[SIZE];
111 for (int i=0; i < SIZE; i++) {
112 a[i] = r1.nextLong();
113 }
114
115 final Random r2 = new Random(seed); // same seed
116 final long[] b = r2.longs().limit(SIZE).toArray();
117 assertEquals(a, b);
118 }
119
120 @Test
121 public void testDoubleStream() {
122 final long seed = System.currentTimeMillis();
123 final Random r1 = new Random(seed);
124 final double[] a = new double[SIZE];
125 for (int i=0; i < SIZE; i++) {
126 a[i] = r1.nextDouble();
127 }
128
129 final Random r2 = new Random(seed); // same seed
130 final double[] b = r2.doubles().limit(SIZE).toArray();
131 assertEquals(a, b);
132 }
133
134 @Test
135 public void testGaussianStream() {
136 final long seed = System.currentTimeMillis();
137 final Random r1 = new Random(seed);
138 final double[] a = new double[SIZE];
139 for (int i=0; i < SIZE; i++) {
140 a[i] = r1.nextGaussian();
141 }
142
143 final Random r2 = new Random(seed); // same seed
144 final double[] b = r2.gaussians().limit(SIZE).toArray();
145 assertEquals(a, b);
146 }
147
148 @Test
psandoz8bc2ea62013-07-30 14:03:25 +0100149 public void testThreadLocalIntStream() throws InterruptedException, ExecutionException, TimeoutException {
150 ThreadLocalRandom tlr = ThreadLocalRandom.current();
151 testRandomResultSupplierConcurrently(() -> tlr.ints().limit(SIZE).boxed().collect(toList()));
mduigou072224c2013-05-02 09:18:56 -0700152 }
153
154 @Test
psandoz8bc2ea62013-07-30 14:03:25 +0100155 public void testThreadLocalLongStream() throws InterruptedException, ExecutionException, TimeoutException {
156 ThreadLocalRandom tlr = ThreadLocalRandom.current();
157 testRandomResultSupplierConcurrently(() -> tlr.longs().limit(SIZE).boxed().collect(toList()));
mduigou072224c2013-05-02 09:18:56 -0700158 }
159
160 @Test
psandoz8bc2ea62013-07-30 14:03:25 +0100161 public void testThreadLocalDoubleStream() throws InterruptedException, ExecutionException, TimeoutException {
162 ThreadLocalRandom tlr = ThreadLocalRandom.current();
163 testRandomResultSupplierConcurrently(() -> tlr.doubles().limit(SIZE).boxed().collect(toList()));
mduigou072224c2013-05-02 09:18:56 -0700164 }
165
166 @Test
psandoz8bc2ea62013-07-30 14:03:25 +0100167 public void testThreadLocalGaussianStream() throws InterruptedException, ExecutionException, TimeoutException {
168 ThreadLocalRandom tlr = ThreadLocalRandom.current();
169 testRandomResultSupplierConcurrently(() -> tlr.gaussians().limit(SIZE).boxed().collect(toList()));
mduigou072224c2013-05-02 09:18:56 -0700170 }
171
psandoz8bc2ea62013-07-30 14:03:25 +0100172 <T> void testRandomResultSupplierConcurrently(Supplier<T> s) throws InterruptedException, ExecutionException, TimeoutException {
173 // Produce 10 completable future tasks
174 final int tasks = 10;
175 List<CompletableFuture<T>> cfs = Stream.generate(() -> CompletableFuture.supplyAsync(s)).
176 limit(tasks).collect(toList());
177
178 // Wait for all tasks to complete
179 // Timeout is beyond reasonable doubt that completion should
180 // have occurred unless there is an issue
181 CompletableFuture<Void> all = CompletableFuture.allOf(cfs.stream().toArray(CompletableFuture[]::new));
182 all.get(1, TimeUnit.MINUTES);
183
184 // Count the distinct results, which should equal the number of tasks
185 long rc = cfs.stream().map(CompletableFuture::join).distinct().count();
186 assertEquals(rc, tasks);
187 }
mduigou072224c2013-05-02 09:18:56 -0700188}