1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
jsr166 |
1.16 |
|
7 |
jsr166 |
1.22 |
import java.util.Arrays; |
8 |
|
|
import java.util.List; |
9 |
dl |
1.1 |
import java.util.SplittableRandom; |
10 |
|
|
import java.util.concurrent.atomic.AtomicInteger; |
11 |
|
|
import java.util.concurrent.atomic.LongAdder; |
12 |
jsr166 |
1.22 |
import java.lang.reflect.Method; |
13 |
|
|
import java.util.function.Predicate; |
14 |
|
|
import java.util.stream.Collectors; |
15 |
dl |
1.1 |
|
16 |
jsr166 |
1.16 |
import junit.framework.Test; |
17 |
|
|
import junit.framework.TestSuite; |
18 |
|
|
|
19 |
dl |
1.1 |
public class SplittableRandomTest extends JSR166TestCase { |
20 |
|
|
|
21 |
|
|
public static void main(String[] args) { |
22 |
jsr166 |
1.18 |
main(suite(), args); |
23 |
dl |
1.1 |
} |
24 |
|
|
public static Test suite() { |
25 |
|
|
return new TestSuite(SplittableRandomTest.class); |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
/* |
29 |
|
|
* Testing coverage notes: |
30 |
|
|
* |
31 |
jsr166 |
1.4 |
* 1. Many of the test methods are adapted from ThreadLocalRandomTest. |
32 |
dl |
1.1 |
* |
33 |
jsr166 |
1.4 |
* 2. These tests do not check for random number generator quality. |
34 |
|
|
* But we check for minimal API compliance by requiring that |
35 |
|
|
* repeated calls to nextX methods, up to NCALLS tries, produce at |
36 |
|
|
* least two distinct results. (In some possible universe, a |
37 |
|
|
* "correct" implementation might fail, but the odds are vastly |
38 |
|
|
* less than that of encountering a hardware failure while running |
39 |
|
|
* the test.) For bounded nextX methods, we sample various |
40 |
|
|
* intervals across multiples of primes. In other tests, we repeat |
41 |
|
|
* under REPS different values. |
42 |
dl |
1.1 |
*/ |
43 |
|
|
|
44 |
|
|
// max numbers of calls to detect getting stuck on one value |
45 |
|
|
static final int NCALLS = 10000; |
46 |
|
|
|
47 |
|
|
// max sampled int bound |
48 |
jsr166 |
1.11 |
static final int MAX_INT_BOUND = (1 << 26); |
49 |
dl |
1.1 |
|
50 |
jsr166 |
1.4 |
// max sampled long bound |
51 |
jsr166 |
1.12 |
static final long MAX_LONG_BOUND = (1L << 40); |
52 |
dl |
1.1 |
|
53 |
|
|
// Number of replications for other checks |
54 |
jsr166 |
1.7 |
static final int REPS = |
55 |
|
|
Integer.getInteger("SplittableRandomTest.reps", 4); |
56 |
dl |
1.1 |
|
57 |
|
|
/** |
58 |
jsr166 |
1.4 |
* Repeated calls to nextInt produce at least two distinct results |
59 |
dl |
1.1 |
*/ |
60 |
|
|
public void testNextInt() { |
61 |
|
|
SplittableRandom sr = new SplittableRandom(); |
62 |
|
|
int f = sr.nextInt(); |
63 |
|
|
int i = 0; |
64 |
|
|
while (i < NCALLS && sr.nextInt() == f) |
65 |
|
|
++i; |
66 |
|
|
assertTrue(i < NCALLS); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
jsr166 |
1.4 |
* Repeated calls to nextLong produce at least two distinct results |
71 |
dl |
1.1 |
*/ |
72 |
|
|
public void testNextLong() { |
73 |
|
|
SplittableRandom sr = new SplittableRandom(); |
74 |
|
|
long f = sr.nextLong(); |
75 |
|
|
int i = 0; |
76 |
|
|
while (i < NCALLS && sr.nextLong() == f) |
77 |
|
|
++i; |
78 |
|
|
assertTrue(i < NCALLS); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/** |
82 |
jsr166 |
1.4 |
* Repeated calls to nextDouble produce at least two distinct results |
83 |
dl |
1.1 |
*/ |
84 |
|
|
public void testNextDouble() { |
85 |
|
|
SplittableRandom sr = new SplittableRandom(); |
86 |
|
|
double f = sr.nextDouble(); |
87 |
jsr166 |
1.4 |
int i = 0; |
88 |
dl |
1.1 |
while (i < NCALLS && sr.nextDouble() == f) |
89 |
|
|
++i; |
90 |
|
|
assertTrue(i < NCALLS); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* Two SplittableRandoms created with the same seed produce the |
95 |
|
|
* same values for nextLong. |
96 |
|
|
*/ |
97 |
|
|
public void testSeedConstructor() { |
98 |
jsr166 |
1.14 |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
99 |
dl |
1.1 |
SplittableRandom sr1 = new SplittableRandom(seed); |
100 |
|
|
SplittableRandom sr2 = new SplittableRandom(seed); |
101 |
jsr166 |
1.3 |
for (int i = 0; i < REPS; ++i) |
102 |
dl |
1.1 |
assertEquals(sr1.nextLong(), sr2.nextLong()); |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
/** |
107 |
|
|
* A SplittableRandom produced by split() of a default-constructed |
108 |
|
|
* SplittableRandom generates a different sequence |
109 |
|
|
*/ |
110 |
|
|
public void testSplit1() { |
111 |
|
|
SplittableRandom sr = new SplittableRandom(); |
112 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
113 |
|
|
SplittableRandom sc = sr.split(); |
114 |
|
|
int i = 0; |
115 |
|
|
while (i < NCALLS && sr.nextLong() == sc.nextLong()) |
116 |
|
|
++i; |
117 |
|
|
assertTrue(i < NCALLS); |
118 |
|
|
} |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/** |
122 |
|
|
* A SplittableRandom produced by split() of a seeded-constructed |
123 |
|
|
* SplittableRandom generates a different sequence |
124 |
|
|
*/ |
125 |
|
|
public void testSplit2() { |
126 |
|
|
SplittableRandom sr = new SplittableRandom(12345); |
127 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
128 |
|
|
SplittableRandom sc = sr.split(); |
129 |
|
|
int i = 0; |
130 |
|
|
while (i < NCALLS && sr.nextLong() == sc.nextLong()) |
131 |
|
|
++i; |
132 |
|
|
assertTrue(i < NCALLS); |
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
jsr166 |
1.9 |
* nextInt(non-positive) throws IllegalArgumentException |
138 |
dl |
1.1 |
*/ |
139 |
jsr166 |
1.13 |
public void testNextIntBoundNonPositive() { |
140 |
dl |
1.1 |
SplittableRandom sr = new SplittableRandom(); |
141 |
jsr166 |
1.25 |
assertThrows( |
142 |
|
|
IllegalArgumentException.class, |
143 |
jsr166 |
1.9 |
() -> sr.nextInt(-17), |
144 |
|
|
() -> sr.nextInt(0), |
145 |
jsr166 |
1.25 |
() -> sr.nextInt(Integer.MIN_VALUE)); |
146 |
dl |
1.1 |
} |
147 |
|
|
|
148 |
|
|
/** |
149 |
jsr166 |
1.4 |
* nextInt(least >= bound) throws IllegalArgumentException |
150 |
dl |
1.1 |
*/ |
151 |
|
|
public void testNextIntBadBounds() { |
152 |
|
|
SplittableRandom sr = new SplittableRandom(); |
153 |
jsr166 |
1.25 |
assertThrows( |
154 |
|
|
IllegalArgumentException.class, |
155 |
jsr166 |
1.10 |
() -> sr.nextInt(17, 2), |
156 |
|
|
() -> sr.nextInt(-42, -42), |
157 |
jsr166 |
1.25 |
() -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE)); |
158 |
dl |
1.1 |
} |
159 |
|
|
|
160 |
|
|
/** |
161 |
|
|
* nextInt(bound) returns 0 <= value < bound; |
162 |
jsr166 |
1.4 |
* repeated calls produce at least two distinct results |
163 |
dl |
1.1 |
*/ |
164 |
|
|
public void testNextIntBounded() { |
165 |
|
|
SplittableRandom sr = new SplittableRandom(); |
166 |
jsr166 |
1.19 |
for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1)); |
167 |
dl |
1.1 |
// sample bound space across prime number increments |
168 |
|
|
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { |
169 |
|
|
int f = sr.nextInt(bound); |
170 |
|
|
assertTrue(0 <= f && f < bound); |
171 |
|
|
int i = 0; |
172 |
|
|
int j; |
173 |
|
|
while (i < NCALLS && |
174 |
|
|
(j = sr.nextInt(bound)) == f) { |
175 |
|
|
assertTrue(0 <= j && j < bound); |
176 |
|
|
++i; |
177 |
|
|
} |
178 |
|
|
assertTrue(i < NCALLS); |
179 |
|
|
} |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
/** |
183 |
|
|
* nextInt(least, bound) returns least <= value < bound; |
184 |
jsr166 |
1.4 |
* repeated calls produce at least two distinct results |
185 |
dl |
1.1 |
*/ |
186 |
|
|
public void testNextIntBounded2() { |
187 |
|
|
SplittableRandom sr = new SplittableRandom(); |
188 |
|
|
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { |
189 |
|
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) { |
190 |
|
|
int f = sr.nextInt(least, bound); |
191 |
|
|
assertTrue(least <= f && f < bound); |
192 |
|
|
int i = 0; |
193 |
|
|
int j; |
194 |
|
|
while (i < NCALLS && |
195 |
|
|
(j = sr.nextInt(least, bound)) == f) { |
196 |
|
|
assertTrue(least <= j && j < bound); |
197 |
|
|
++i; |
198 |
|
|
} |
199 |
|
|
assertTrue(i < NCALLS); |
200 |
|
|
} |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
/** |
205 |
jsr166 |
1.9 |
* nextLong(non-positive) throws IllegalArgumentException |
206 |
dl |
1.1 |
*/ |
207 |
jsr166 |
1.13 |
public void testNextLongBoundNonPositive() { |
208 |
dl |
1.1 |
SplittableRandom sr = new SplittableRandom(); |
209 |
jsr166 |
1.25 |
assertThrows( |
210 |
|
|
IllegalArgumentException.class, |
211 |
jsr166 |
1.9 |
() -> sr.nextLong(-17L), |
212 |
|
|
() -> sr.nextLong(0L), |
213 |
jsr166 |
1.25 |
() -> sr.nextLong(Long.MIN_VALUE)); |
214 |
dl |
1.1 |
} |
215 |
|
|
|
216 |
|
|
/** |
217 |
jsr166 |
1.4 |
* nextLong(least >= bound) throws IllegalArgumentException |
218 |
dl |
1.1 |
*/ |
219 |
|
|
public void testNextLongBadBounds() { |
220 |
|
|
SplittableRandom sr = new SplittableRandom(); |
221 |
jsr166 |
1.25 |
assertThrows( |
222 |
|
|
IllegalArgumentException.class, |
223 |
jsr166 |
1.10 |
() -> sr.nextLong(17L, 2L), |
224 |
|
|
() -> sr.nextLong(-42L, -42L), |
225 |
jsr166 |
1.25 |
() -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE)); |
226 |
dl |
1.1 |
} |
227 |
|
|
|
228 |
|
|
/** |
229 |
|
|
* nextLong(bound) returns 0 <= value < bound; |
230 |
jsr166 |
1.4 |
* repeated calls produce at least two distinct results |
231 |
dl |
1.1 |
*/ |
232 |
|
|
public void testNextLongBounded() { |
233 |
|
|
SplittableRandom sr = new SplittableRandom(); |
234 |
jsr166 |
1.19 |
for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L)); |
235 |
dl |
1.1 |
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
236 |
|
|
long f = sr.nextLong(bound); |
237 |
|
|
assertTrue(0 <= f && f < bound); |
238 |
|
|
int i = 0; |
239 |
|
|
long j; |
240 |
|
|
while (i < NCALLS && |
241 |
|
|
(j = sr.nextLong(bound)) == f) { |
242 |
|
|
assertTrue(0 <= j && j < bound); |
243 |
|
|
++i; |
244 |
|
|
} |
245 |
|
|
assertTrue(i < NCALLS); |
246 |
|
|
} |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
/** |
250 |
|
|
* nextLong(least, bound) returns least <= value < bound; |
251 |
jsr166 |
1.4 |
* repeated calls produce at least two distinct results |
252 |
dl |
1.1 |
*/ |
253 |
|
|
public void testNextLongBounded2() { |
254 |
|
|
SplittableRandom sr = new SplittableRandom(); |
255 |
|
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { |
256 |
|
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
257 |
|
|
long f = sr.nextLong(least, bound); |
258 |
|
|
assertTrue(least <= f && f < bound); |
259 |
|
|
int i = 0; |
260 |
|
|
long j; |
261 |
|
|
while (i < NCALLS && |
262 |
|
|
(j = sr.nextLong(least, bound)) == f) { |
263 |
|
|
assertTrue(least <= j && j < bound); |
264 |
|
|
++i; |
265 |
|
|
} |
266 |
|
|
assertTrue(i < NCALLS); |
267 |
|
|
} |
268 |
|
|
} |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
/** |
272 |
jsr166 |
1.9 |
* nextDouble(non-positive) throws IllegalArgumentException |
273 |
|
|
*/ |
274 |
jsr166 |
1.13 |
public void testNextDoubleBoundNonPositive() { |
275 |
jsr166 |
1.9 |
SplittableRandom sr = new SplittableRandom(); |
276 |
jsr166 |
1.25 |
assertThrows( |
277 |
|
|
IllegalArgumentException.class, |
278 |
jsr166 |
1.9 |
() -> sr.nextDouble(-17.0d), |
279 |
|
|
() -> sr.nextDouble(0.0d), |
280 |
|
|
() -> sr.nextDouble(-Double.MIN_VALUE), |
281 |
|
|
() -> sr.nextDouble(Double.NEGATIVE_INFINITY), |
282 |
jsr166 |
1.25 |
() -> sr.nextDouble(Double.NaN)); |
283 |
jsr166 |
1.9 |
} |
284 |
|
|
|
285 |
|
|
/** |
286 |
jsr166 |
1.10 |
* nextDouble(! (least < bound)) throws IllegalArgumentException |
287 |
|
|
*/ |
288 |
|
|
public void testNextDoubleBadBounds() { |
289 |
|
|
SplittableRandom sr = new SplittableRandom(); |
290 |
jsr166 |
1.25 |
assertThrows( |
291 |
|
|
IllegalArgumentException.class, |
292 |
jsr166 |
1.10 |
() -> sr.nextDouble(17.0d, 2.0d), |
293 |
|
|
() -> sr.nextDouble(-42.0d, -42.0d), |
294 |
|
|
() -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), |
295 |
|
|
() -> sr.nextDouble(Double.NaN, 0.0d), |
296 |
jsr166 |
1.25 |
() -> sr.nextDouble(0.0d, Double.NaN)); |
297 |
jsr166 |
1.10 |
} |
298 |
|
|
|
299 |
|
|
// TODO: Test infinite bounds! |
300 |
|
|
//() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d), |
301 |
|
|
//() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY), |
302 |
|
|
|
303 |
|
|
/** |
304 |
dl |
1.1 |
* nextDouble(least, bound) returns least <= value < bound; |
305 |
jsr166 |
1.4 |
* repeated calls produce at least two distinct results |
306 |
dl |
1.1 |
*/ |
307 |
|
|
public void testNextDoubleBounded2() { |
308 |
|
|
SplittableRandom sr = new SplittableRandom(); |
309 |
|
|
for (double least = 0.0001; least < 1.0e20; least *= 8) { |
310 |
|
|
for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) { |
311 |
|
|
double f = sr.nextDouble(least, bound); |
312 |
|
|
assertTrue(least <= f && f < bound); |
313 |
|
|
int i = 0; |
314 |
|
|
double j; |
315 |
|
|
while (i < NCALLS && |
316 |
|
|
(j = sr.nextDouble(least, bound)) == f) { |
317 |
|
|
assertTrue(least <= j && j < bound); |
318 |
|
|
++i; |
319 |
|
|
} |
320 |
|
|
assertTrue(i < NCALLS); |
321 |
|
|
} |
322 |
|
|
} |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
/** |
326 |
|
|
* Invoking sized ints, long, doubles, with negative sizes throws |
327 |
|
|
* IllegalArgumentException |
328 |
|
|
*/ |
329 |
|
|
public void testBadStreamSize() { |
330 |
|
|
SplittableRandom r = new SplittableRandom(); |
331 |
jsr166 |
1.25 |
assertThrows( |
332 |
|
|
IllegalArgumentException.class, |
333 |
jsr166 |
1.8 |
() -> { java.util.stream.IntStream x = r.ints(-1L); }, |
334 |
|
|
() -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); }, |
335 |
|
|
() -> { java.util.stream.LongStream x = r.longs(-1L); }, |
336 |
|
|
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); }, |
337 |
|
|
() -> { java.util.stream.DoubleStream x = r.doubles(-1L); }, |
338 |
jsr166 |
1.25 |
() -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); }); |
339 |
dl |
1.1 |
} |
340 |
|
|
|
341 |
|
|
/** |
342 |
|
|
* Invoking bounded ints, long, doubles, with illegal bounds throws |
343 |
|
|
* IllegalArgumentException |
344 |
|
|
*/ |
345 |
|
|
public void testBadStreamBounds() { |
346 |
|
|
SplittableRandom r = new SplittableRandom(); |
347 |
jsr166 |
1.25 |
assertThrows( |
348 |
|
|
IllegalArgumentException.class, |
349 |
jsr166 |
1.8 |
() -> { java.util.stream.IntStream x = r.ints(2, 1); }, |
350 |
|
|
() -> { java.util.stream.IntStream x = r.ints(10, 42, 42); }, |
351 |
|
|
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L); }, |
352 |
|
|
() -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); }, |
353 |
|
|
() -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); }, |
354 |
jsr166 |
1.25 |
() -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); }); |
355 |
dl |
1.1 |
} |
356 |
|
|
|
357 |
|
|
/** |
358 |
|
|
* A parallel sized stream of ints generates the given number of values |
359 |
|
|
*/ |
360 |
|
|
public void testIntsCount() { |
361 |
|
|
LongAdder counter = new LongAdder(); |
362 |
|
|
SplittableRandom r = new SplittableRandom(); |
363 |
|
|
long size = 0; |
364 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
365 |
|
|
counter.reset(); |
366 |
jsr166 |
1.6 |
r.ints(size).parallel().forEach(x -> counter.increment()); |
367 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
368 |
dl |
1.1 |
size += 524959; |
369 |
|
|
} |
370 |
|
|
} |
371 |
|
|
|
372 |
|
|
/** |
373 |
|
|
* A parallel sized stream of longs generates the given number of values |
374 |
|
|
*/ |
375 |
|
|
public void testLongsCount() { |
376 |
|
|
LongAdder counter = new LongAdder(); |
377 |
|
|
SplittableRandom r = new SplittableRandom(); |
378 |
|
|
long size = 0; |
379 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
380 |
|
|
counter.reset(); |
381 |
jsr166 |
1.6 |
r.longs(size).parallel().forEach(x -> counter.increment()); |
382 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
383 |
dl |
1.1 |
size += 524959; |
384 |
|
|
} |
385 |
|
|
} |
386 |
|
|
|
387 |
|
|
/** |
388 |
|
|
* A parallel sized stream of doubles generates the given number of values |
389 |
|
|
*/ |
390 |
|
|
public void testDoublesCount() { |
391 |
|
|
LongAdder counter = new LongAdder(); |
392 |
|
|
SplittableRandom r = new SplittableRandom(); |
393 |
|
|
long size = 0; |
394 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
395 |
|
|
counter.reset(); |
396 |
jsr166 |
1.6 |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
397 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
398 |
dl |
1.1 |
size += 524959; |
399 |
|
|
} |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
/** |
403 |
|
|
* Each of a parallel sized stream of bounded ints is within bounds |
404 |
|
|
*/ |
405 |
|
|
public void testBoundedInts() { |
406 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
407 |
|
|
SplittableRandom r = new SplittableRandom(); |
408 |
|
|
long size = 12345L; |
409 |
|
|
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
410 |
|
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
411 |
|
|
final int lo = least, hi = bound; |
412 |
jsr166 |
1.17 |
r.ints(size, lo, hi).parallel().forEach( |
413 |
|
|
x -> { |
414 |
|
|
if (x < lo || x >= hi) |
415 |
|
|
fails.getAndIncrement(); }); |
416 |
dl |
1.1 |
} |
417 |
|
|
} |
418 |
jsr166 |
1.4 |
assertEquals(0, fails.get()); |
419 |
dl |
1.1 |
} |
420 |
|
|
|
421 |
|
|
/** |
422 |
|
|
* Each of a parallel sized stream of bounded longs is within bounds |
423 |
|
|
*/ |
424 |
|
|
public void testBoundedLongs() { |
425 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
426 |
|
|
SplittableRandom r = new SplittableRandom(); |
427 |
|
|
long size = 123L; |
428 |
|
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
429 |
|
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
430 |
|
|
final long lo = least, hi = bound; |
431 |
jsr166 |
1.17 |
r.longs(size, lo, hi).parallel().forEach( |
432 |
|
|
x -> { |
433 |
|
|
if (x < lo || x >= hi) |
434 |
|
|
fails.getAndIncrement(); }); |
435 |
dl |
1.1 |
} |
436 |
|
|
} |
437 |
jsr166 |
1.4 |
assertEquals(0, fails.get()); |
438 |
dl |
1.1 |
} |
439 |
|
|
|
440 |
|
|
/** |
441 |
|
|
* Each of a parallel sized stream of bounded doubles is within bounds |
442 |
|
|
*/ |
443 |
|
|
public void testBoundedDoubles() { |
444 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
445 |
|
|
SplittableRandom r = new SplittableRandom(); |
446 |
|
|
long size = 456; |
447 |
|
|
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
448 |
|
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
449 |
|
|
final double lo = least, hi = bound; |
450 |
jsr166 |
1.17 |
r.doubles(size, lo, hi).parallel().forEach( |
451 |
|
|
x -> { |
452 |
|
|
if (x < lo || x >= hi) |
453 |
|
|
fails.getAndIncrement(); }); |
454 |
dl |
1.1 |
} |
455 |
|
|
} |
456 |
jsr166 |
1.4 |
assertEquals(0, fails.get()); |
457 |
dl |
1.1 |
} |
458 |
|
|
|
459 |
dl |
1.2 |
/** |
460 |
|
|
* A parallel unsized stream of ints generates at least 100 values |
461 |
|
|
*/ |
462 |
|
|
public void testUnsizedIntsCount() { |
463 |
|
|
LongAdder counter = new LongAdder(); |
464 |
|
|
SplittableRandom r = new SplittableRandom(); |
465 |
|
|
long size = 100; |
466 |
jsr166 |
1.6 |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
467 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
468 |
dl |
1.2 |
} |
469 |
|
|
|
470 |
|
|
/** |
471 |
|
|
* A parallel unsized stream of longs generates at least 100 values |
472 |
|
|
*/ |
473 |
|
|
public void testUnsizedLongsCount() { |
474 |
|
|
LongAdder counter = new LongAdder(); |
475 |
|
|
SplittableRandom r = new SplittableRandom(); |
476 |
|
|
long size = 100; |
477 |
jsr166 |
1.6 |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
478 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
479 |
dl |
1.2 |
} |
480 |
|
|
|
481 |
|
|
/** |
482 |
|
|
* A parallel unsized stream of doubles generates at least 100 values |
483 |
|
|
*/ |
484 |
|
|
public void testUnsizedDoublesCount() { |
485 |
|
|
LongAdder counter = new LongAdder(); |
486 |
|
|
SplittableRandom r = new SplittableRandom(); |
487 |
|
|
long size = 100; |
488 |
jsr166 |
1.6 |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
489 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
490 |
dl |
1.2 |
} |
491 |
|
|
|
492 |
|
|
/** |
493 |
|
|
* A sequential unsized stream of ints generates at least 100 values |
494 |
|
|
*/ |
495 |
|
|
public void testUnsizedIntsCountSeq() { |
496 |
|
|
LongAdder counter = new LongAdder(); |
497 |
|
|
SplittableRandom r = new SplittableRandom(); |
498 |
|
|
long size = 100; |
499 |
jsr166 |
1.6 |
r.ints().limit(size).forEach(x -> counter.increment()); |
500 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
501 |
dl |
1.2 |
} |
502 |
|
|
|
503 |
|
|
/** |
504 |
|
|
* A sequential unsized stream of longs generates at least 100 values |
505 |
|
|
*/ |
506 |
|
|
public void testUnsizedLongsCountSeq() { |
507 |
|
|
LongAdder counter = new LongAdder(); |
508 |
|
|
SplittableRandom r = new SplittableRandom(); |
509 |
|
|
long size = 100; |
510 |
jsr166 |
1.6 |
r.longs().limit(size).forEach(x -> counter.increment()); |
511 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
512 |
dl |
1.2 |
} |
513 |
|
|
|
514 |
|
|
/** |
515 |
|
|
* A sequential unsized stream of doubles generates at least 100 values |
516 |
|
|
*/ |
517 |
|
|
public void testUnsizedDoublesCountSeq() { |
518 |
|
|
LongAdder counter = new LongAdder(); |
519 |
|
|
SplittableRandom r = new SplittableRandom(); |
520 |
|
|
long size = 100; |
521 |
jsr166 |
1.6 |
r.doubles().limit(size).forEach(x -> counter.increment()); |
522 |
jsr166 |
1.4 |
assertEquals(size, counter.sum()); |
523 |
dl |
1.2 |
} |
524 |
|
|
|
525 |
jsr166 |
1.22 |
/** |
526 |
|
|
* SplittableRandom should implement most of Random's public methods |
527 |
|
|
*/ |
528 |
|
|
public void testShouldImplementMostRandomMethods() throws Throwable { |
529 |
|
|
Predicate<Method> wasForgotten = method -> { |
530 |
|
|
String name = method.getName(); |
531 |
|
|
// some methods deliberately not implemented |
532 |
|
|
if (name.equals("setSeed")) return false; |
533 |
|
|
if (name.equals("nextFloat")) return false; |
534 |
|
|
if (name.equals("nextGaussian")) return false; |
535 |
|
|
try { |
536 |
|
|
SplittableRandom.class.getMethod( |
537 |
|
|
method.getName(), method.getParameterTypes()); |
538 |
|
|
} catch (ReflectiveOperationException ex) { |
539 |
|
|
return true; |
540 |
|
|
} |
541 |
|
|
return false; |
542 |
|
|
}; |
543 |
|
|
List<Method> forgotten = |
544 |
|
|
Arrays.stream(java.util.Random.class.getMethods()) |
545 |
|
|
.filter(wasForgotten) |
546 |
|
|
.collect(Collectors.toList()); |
547 |
|
|
if (!forgotten.isEmpty()) |
548 |
|
|
throw new AssertionError("Please implement: " + forgotten); |
549 |
|
|
} |
550 |
|
|
|
551 |
|
|
/** |
552 |
|
|
* Repeated calls to nextBytes produce at least values of different signs for every byte |
553 |
|
|
*/ |
554 |
|
|
public void testNextBytes() { |
555 |
|
|
SplittableRandom sr = new SplittableRandom(); |
556 |
jsr166 |
1.23 |
int n = sr.nextInt(1, 20); |
557 |
jsr166 |
1.22 |
byte[] bytes = new byte[n]; |
558 |
|
|
outer: |
559 |
|
|
for (int i = 0; i < n; i++) { |
560 |
|
|
for (int tries = NCALLS; tries-->0; ) { |
561 |
|
|
byte before = bytes[i]; |
562 |
|
|
sr.nextBytes(bytes); |
563 |
|
|
byte after = bytes[i]; |
564 |
|
|
if (after * before < 0) |
565 |
|
|
continue outer; |
566 |
|
|
} |
567 |
|
|
fail("not enough variation in random bytes"); |
568 |
|
|
} |
569 |
|
|
} |
570 |
|
|
|
571 |
jsr166 |
1.23 |
/** |
572 |
|
|
* Filling an empty array with random bytes succeeds without effect. |
573 |
|
|
*/ |
574 |
|
|
public void testNextBytes_emptyArray() { |
575 |
|
|
new SplittableRandom().nextBytes(new byte[0]); |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
public void testNextBytes_nullArray() { |
579 |
|
|
try { |
580 |
|
|
new SplittableRandom().nextBytes(null); |
581 |
|
|
shouldThrow(); |
582 |
|
|
} catch (NullPointerException success) {} |
583 |
|
|
} |
584 |
|
|
|
585 |
dl |
1.1 |
} |