Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Random/RandomStreamTest.java
41149 views
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
* @key randomness
51
*/
52
public 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
87
public void testIntStream() {
88
final long seed = System.currentTimeMillis();
89
final Random r1 = new Random(seed);
90
final int[] a = new int[SIZE];
91
for (int i=0; i < SIZE; i++) {
92
a[i] = r1.nextInt();
93
}
94
95
final Random r2 = new Random(seed); // same seed
96
final int[] b = r2.ints().limit(SIZE).toArray();
97
assertEquals(a, b);
98
}
99
100
@Test
101
public void testLongStream() {
102
final long seed = System.currentTimeMillis();
103
final Random r1 = new Random(seed);
104
final long[] a = new long[SIZE];
105
for (int i=0; i < SIZE; i++) {
106
a[i] = r1.nextLong();
107
}
108
109
final Random r2 = new Random(seed); // same seed
110
final long[] b = r2.longs().limit(SIZE).toArray();
111
assertEquals(a, b);
112
}
113
114
@Test
115
public void testDoubleStream() {
116
final long seed = System.currentTimeMillis();
117
final Random r1 = new Random(seed);
118
final double[] a = new double[SIZE];
119
for (int i=0; i < SIZE; i++) {
120
a[i] = r1.nextDouble();
121
}
122
123
final Random r2 = new Random(seed); // same seed
124
final double[] b = r2.doubles().limit(SIZE).toArray();
125
assertEquals(a, b);
126
}
127
128
@Test
129
public void testThreadLocalIntStream() throws InterruptedException, ExecutionException, TimeoutException {
130
ThreadLocalRandom tlr = ThreadLocalRandom.current();
131
testRandomResultSupplierConcurrently(() -> tlr.ints().limit(SIZE).boxed().collect(toList()));
132
}
133
134
@Test
135
public void testThreadLocalLongStream() throws InterruptedException, ExecutionException, TimeoutException {
136
ThreadLocalRandom tlr = ThreadLocalRandom.current();
137
testRandomResultSupplierConcurrently(() -> tlr.longs().limit(SIZE).boxed().collect(toList()));
138
}
139
140
@Test
141
public void testThreadLocalDoubleStream() throws InterruptedException, ExecutionException, TimeoutException {
142
ThreadLocalRandom tlr = ThreadLocalRandom.current();
143
testRandomResultSupplierConcurrently(() -> tlr.doubles().limit(SIZE).boxed().collect(toList()));
144
}
145
146
<T> void testRandomResultSupplierConcurrently(Supplier<T> s) throws InterruptedException, ExecutionException, TimeoutException {
147
// Produce 10 completable future tasks
148
final int tasks = 10;
149
List<CompletableFuture<T>> cfs = Stream.generate(() -> CompletableFuture.supplyAsync(s)).
150
limit(tasks).collect(toList());
151
152
// Wait for all tasks to complete
153
// Timeout is beyond reasonable doubt that completion should
154
// have occurred unless there is an issue
155
CompletableFuture<Void> all = CompletableFuture.allOf(cfs.stream().toArray(CompletableFuture[]::new));
156
all.get(1, TimeUnit.MINUTES);
157
158
// Count the distinct results, which should equal the number of tasks
159
long rc = cfs.stream().map(CompletableFuture::join).distinct().count();
160
assertEquals(rc, tasks);
161
}
162
}
163
164