blob: c2edf4fee5ef8e32ce8c659524fd2f09411835e3 [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;
28import java.util.Arrays;
29import java.util.List;
30import java.util.ArrayList;
31import java.util.Random;
32
33import java.util.concurrent.Executors;
34import java.util.concurrent.ExecutorService;
35import java.util.concurrent.ThreadLocalRandom;
36import java.util.concurrent.TimeUnit;
37import java.util.stream.IntStream;
38import java.util.stream.LongStream;
39import java.util.stream.DoubleStream;
40import java.util.stream.Stream;
41
42import static org.testng.Assert.assertEquals;
43import static org.testng.Assert.assertFalse;
44import static org.testng.Assert.assertTrue;
45
46/**
47 * @test
48 * @run testng RandomStreamTest
49 * @summary test stream methods on Random
50 * @author Brian Goetz
51 */
52public class RandomStreamTest {
53
54 private static final int SIZE = 1000;
55
56 @DataProvider(name = "suppliers")
57 public Object[][] randomSuppliers() {
58 return new Object[][] {
59 {new Random(), SIZE},
60 {new SecureRandom(), SIZE}
61 };
62 }
63
64 @Test(dataProvider = "suppliers")
65 public void testRandomIntStream(final Random random, final int count) {
66 final List<Integer> destination = new ArrayList<>(count);
67 random.ints().limit(count).forEach(destination::add);
68 assertEquals(destination.size(), count);
69 }
70
71 @Test(dataProvider = "suppliers")
72 public void testRandomLongStream(final Random random, final int count) {
73 final List<Long> destination = new ArrayList<>(count);
74 random.longs().limit(count).forEach(destination::add);
75 assertEquals(destination.size(), count);
76 }
77
78 @Test(dataProvider = "suppliers")
79 public void testRandomDoubleStream(final Random random, final int count) {
80 final List<Double> destination = new ArrayList<>(count);
81 random.doubles().limit(count).forEach(destination::add);
82 random.doubles().limit(count).forEach(d -> assertTrue(d >= 0.0 && d < 1.0));
83 assertEquals(destination.size(), count);
84 }
85
86 @Test(dataProvider = "suppliers")
87 public void testRandomGaussianStream(final Random random, final int count) {
88 final List<Double> destination = new ArrayList<>(count);
89 random.gaussians().limit(count).forEach(destination::add);
90 assertEquals(destination.size(), count);
91 }
92
93 @Test
94 public void testIntStream() {
95 final long seed = System.currentTimeMillis();
96 final Random r1 = new Random(seed);
97 final int[] a = new int[SIZE];
98 for (int i=0; i < SIZE; i++) {
99 a[i] = r1.nextInt();
100 }
101
102 final Random r2 = new Random(seed); // same seed
103 final int[] b = r2.ints().limit(SIZE).toArray();
104 assertEquals(a, b);
105 }
106
107 @Test
108 public void testLongStream() {
109 final long seed = System.currentTimeMillis();
110 final Random r1 = new Random(seed);
111 final long[] a = new long[SIZE];
112 for (int i=0; i < SIZE; i++) {
113 a[i] = r1.nextLong();
114 }
115
116 final Random r2 = new Random(seed); // same seed
117 final long[] b = r2.longs().limit(SIZE).toArray();
118 assertEquals(a, b);
119 }
120
121 @Test
122 public void testDoubleStream() {
123 final long seed = System.currentTimeMillis();
124 final Random r1 = new Random(seed);
125 final double[] a = new double[SIZE];
126 for (int i=0; i < SIZE; i++) {
127 a[i] = r1.nextDouble();
128 }
129
130 final Random r2 = new Random(seed); // same seed
131 final double[] b = r2.doubles().limit(SIZE).toArray();
132 assertEquals(a, b);
133 }
134
135 @Test
136 public void testGaussianStream() {
137 final long seed = System.currentTimeMillis();
138 final Random r1 = new Random(seed);
139 final double[] a = new double[SIZE];
140 for (int i=0; i < SIZE; i++) {
141 a[i] = r1.nextGaussian();
142 }
143
144 final Random r2 = new Random(seed); // same seed
145 final double[] b = r2.gaussians().limit(SIZE).toArray();
146 assertEquals(a, b);
147 }
148
149 @Test
150 public void testThreadLocalIntStream() throws InterruptedException {
151 final ExecutorService e = Executors.newFixedThreadPool(10);
152 final ThreadLocalRandom tlr = ThreadLocalRandom.current();
153
154 final class RandomTask implements Runnable {
155 int[] randoms;
156
157 @Override
158 public void run() {
159 randoms = tlr.ints().limit(SIZE).toArray();
160 }
161 }
162 final RandomTask[] tasks = new RandomTask[10];
163 for (int i=0; i < tasks.length; i++) {
164 tasks[i] = new RandomTask();
165 }
166 for (int i=0; i < tasks.length; i++) {
167 e.submit(tasks[i]);
168 }
169 e.shutdown();
170 e.awaitTermination(3, TimeUnit.SECONDS);
171 for (int i=1; i < tasks.length; i++) {
172 assertFalse(Arrays.equals(tasks[0].randoms, tasks[i].randoms));
173 }
174 }
175
176 @Test
177 public void testThreadLocalLongStream() throws InterruptedException {
178 final ExecutorService e = Executors.newFixedThreadPool(10);
179 final ThreadLocalRandom tlr = ThreadLocalRandom.current();
180
181 final class RandomTask implements Runnable {
182 long[] randoms;
183
184 @Override
185 public void run() {
186 randoms = tlr.longs().limit(SIZE).toArray();
187 }
188 }
189 final RandomTask[] tasks = new RandomTask[10];
190 for (int i=0; i < tasks.length; i++) {
191 tasks[i] = new RandomTask();
192 }
193 for (int i=0; i < tasks.length; i++) {
194 e.submit(tasks[i]);
195 }
196 e.shutdown();
197 e.awaitTermination(3, TimeUnit.SECONDS);
198 for (int i=1; i < tasks.length; i++) {
199 assertFalse(Arrays.equals(tasks[0].randoms, tasks[i].randoms));
200 }
201 }
202
203 @Test
204 public void testThreadLocalDoubleStream() throws InterruptedException {
205 final ExecutorService e = Executors.newFixedThreadPool(10);
206 final ThreadLocalRandom tlr = ThreadLocalRandom.current();
207
208 final class RandomTask implements Runnable {
209 double[] randoms;
210
211 @Override
212 public void run() {
213 randoms = tlr.doubles().limit(SIZE).toArray();
214 }
215 }
216 final RandomTask[] tasks = new RandomTask[10];
217 for (int i=0; i < tasks.length; i++) {
218 tasks[i] = new RandomTask();
219 }
220 for (int i=0; i < tasks.length; i++) {
221 e.submit(tasks[i]);
222 }
223 e.shutdown();
224 e.awaitTermination(3, TimeUnit.SECONDS);
225 for (int i=1; i < tasks.length; i++) {
226 assertFalse(Arrays.equals(tasks[0].randoms, tasks[i].randoms));
227 }
228 }
229
230 @Test
231 public void testThreadLocalGaussianStream() throws InterruptedException {
232 final ExecutorService e = Executors.newFixedThreadPool(10);
233 final ThreadLocalRandom tlr = ThreadLocalRandom.current();
234
235 final class RandomTask implements Runnable {
236 double[] randoms;
237
238 @Override
239 public void run() {
240 randoms = tlr.gaussians().limit(SIZE).toArray();
241 }
242 }
243 final RandomTask[] tasks = new RandomTask[10];
244 for (int i=0; i < tasks.length; i++) {
245 tasks[i] = new RandomTask();
246 }
247 for (int i=0; i < tasks.length; i++) {
248 e.submit(tasks[i]);
249 }
250 e.shutdown();
251 e.awaitTermination(3, TimeUnit.SECONDS);
252 for (int i=1; i < tasks.length; i++) {
253 assertFalse(Arrays.equals(tasks[0].randoms, tasks[i].randoms));
254 }
255 }
256
257}