blob: e96d57e2639a32ca60073c93c02484b70e17d498 [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
mduigou072224c2013-05-02 09:18:56 -070085 @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
psandoz8bc2ea62013-07-30 14:03:25 +0100128 public void testThreadLocalIntStream() throws InterruptedException, ExecutionException, TimeoutException {
129 ThreadLocalRandom tlr = ThreadLocalRandom.current();
130 testRandomResultSupplierConcurrently(() -> tlr.ints().limit(SIZE).boxed().collect(toList()));
mduigou072224c2013-05-02 09:18:56 -0700131 }
132
133 @Test
psandoz8bc2ea62013-07-30 14:03:25 +0100134 public void testThreadLocalLongStream() throws InterruptedException, ExecutionException, TimeoutException {
135 ThreadLocalRandom tlr = ThreadLocalRandom.current();
136 testRandomResultSupplierConcurrently(() -> tlr.longs().limit(SIZE).boxed().collect(toList()));
mduigou072224c2013-05-02 09:18:56 -0700137 }
138
139 @Test
psandoz8bc2ea62013-07-30 14:03:25 +0100140 public void testThreadLocalDoubleStream() throws InterruptedException, ExecutionException, TimeoutException {
141 ThreadLocalRandom tlr = ThreadLocalRandom.current();
142 testRandomResultSupplierConcurrently(() -> tlr.doubles().limit(SIZE).boxed().collect(toList()));
mduigou072224c2013-05-02 09:18:56 -0700143 }
144
psandoz8bc2ea62013-07-30 14:03:25 +0100145 <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 }
mduigou072224c2013-05-02 09:18:56 -0700161}