ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
Revision: 1.6
Committed: Mon Sep 23 17:05:52 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +9 -9 lines
Log Message:
use nicer lambda syntax

File Contents

# Content
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 import junit.framework.*;
7 import java.util.*;
8 import java.util.SplittableRandom;
9 import java.util.concurrent.atomic.AtomicInteger;
10 import java.util.concurrent.atomic.AtomicLong;
11 import java.util.concurrent.atomic.LongAdder;
12
13 public class SplittableRandomTest extends JSR166TestCase {
14
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run(suite());
17 }
18 public static Test suite() {
19 return new TestSuite(SplittableRandomTest.class);
20 }
21
22 /*
23 * Testing coverage notes:
24 *
25 * 1. Many of the test methods are adapted from ThreadLocalRandomTest.
26 *
27 * 2. These tests do not check for random number generator quality.
28 * But we check for minimal API compliance by requiring that
29 * repeated calls to nextX methods, up to NCALLS tries, produce at
30 * least two distinct results. (In some possible universe, a
31 * "correct" implementation might fail, but the odds are vastly
32 * less than that of encountering a hardware failure while running
33 * the test.) For bounded nextX methods, we sample various
34 * intervals across multiples of primes. In other tests, we repeat
35 * under REPS different values.
36 */
37
38 // max numbers of calls to detect getting stuck on one value
39 static final int NCALLS = 10000;
40
41 // max sampled int bound
42 static final int MAX_INT_BOUND = (1 << 28);
43
44 // max sampled long bound
45 static final long MAX_LONG_BOUND = (1L << 42);
46
47 // Number of replications for other checks
48 static final int REPS = 20;
49
50 /**
51 * Repeated calls to nextInt produce at least two distinct results
52 */
53 public void testNextInt() {
54 SplittableRandom sr = new SplittableRandom();
55 int f = sr.nextInt();
56 int i = 0;
57 while (i < NCALLS && sr.nextInt() == f)
58 ++i;
59 assertTrue(i < NCALLS);
60 }
61
62 /**
63 * Repeated calls to nextLong produce at least two distinct results
64 */
65 public void testNextLong() {
66 SplittableRandom sr = new SplittableRandom();
67 long f = sr.nextLong();
68 int i = 0;
69 while (i < NCALLS && sr.nextLong() == f)
70 ++i;
71 assertTrue(i < NCALLS);
72 }
73
74 /**
75 * Repeated calls to nextDouble produce at least two distinct results
76 */
77 public void testNextDouble() {
78 SplittableRandom sr = new SplittableRandom();
79 double f = sr.nextDouble();
80 int i = 0;
81 while (i < NCALLS && sr.nextDouble() == f)
82 ++i;
83 assertTrue(i < NCALLS);
84 }
85
86 /**
87 * Two SplittableRandoms created with the same seed produce the
88 * same values for nextLong.
89 */
90 public void testSeedConstructor() {
91 for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) {
92 SplittableRandom sr1 = new SplittableRandom(seed);
93 SplittableRandom sr2 = new SplittableRandom(seed);
94 for (int i = 0; i < REPS; ++i)
95 assertEquals(sr1.nextLong(), sr2.nextLong());
96 }
97 }
98
99 /**
100 * A SplittableRandom produced by split() of a default-constructed
101 * SplittableRandom generates a different sequence
102 */
103 public void testSplit1() {
104 SplittableRandom sr = new SplittableRandom();
105 for (int reps = 0; reps < REPS; ++reps) {
106 SplittableRandom sc = sr.split();
107 int i = 0;
108 while (i < NCALLS && sr.nextLong() == sc.nextLong())
109 ++i;
110 assertTrue(i < NCALLS);
111 }
112 }
113
114 /**
115 * A SplittableRandom produced by split() of a seeded-constructed
116 * SplittableRandom generates a different sequence
117 */
118 public void testSplit2() {
119 SplittableRandom sr = new SplittableRandom(12345);
120 for (int reps = 0; reps < REPS; ++reps) {
121 SplittableRandom sc = sr.split();
122 int i = 0;
123 while (i < NCALLS && sr.nextLong() == sc.nextLong())
124 ++i;
125 assertTrue(i < NCALLS);
126 }
127 }
128
129 /**
130 * nextInt(negative) throws IllegalArgumentException
131 */
132 public void testNextIntBoundedNeg() {
133 SplittableRandom sr = new SplittableRandom();
134 try {
135 int f = sr.nextInt(-17);
136 shouldThrow();
137 } catch (IllegalArgumentException success) {}
138 }
139
140 /**
141 * nextInt(least >= bound) throws IllegalArgumentException
142 */
143 public void testNextIntBadBounds() {
144 SplittableRandom sr = new SplittableRandom();
145 try {
146 int f = sr.nextInt(17, 2);
147 shouldThrow();
148 } catch (IllegalArgumentException success) {}
149 }
150
151 /**
152 * nextInt(bound) returns 0 <= value < bound;
153 * repeated calls produce at least two distinct results
154 */
155 public void testNextIntBounded() {
156 SplittableRandom sr = new SplittableRandom();
157 // sample bound space across prime number increments
158 for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
159 int f = sr.nextInt(bound);
160 assertTrue(0 <= f && f < bound);
161 int i = 0;
162 int j;
163 while (i < NCALLS &&
164 (j = sr.nextInt(bound)) == f) {
165 assertTrue(0 <= j && j < bound);
166 ++i;
167 }
168 assertTrue(i < NCALLS);
169 }
170 }
171
172 /**
173 * nextInt(least, bound) returns least <= value < bound;
174 * repeated calls produce at least two distinct results
175 */
176 public void testNextIntBounded2() {
177 SplittableRandom sr = new SplittableRandom();
178 for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
179 for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
180 int f = sr.nextInt(least, bound);
181 assertTrue(least <= f && f < bound);
182 int i = 0;
183 int j;
184 while (i < NCALLS &&
185 (j = sr.nextInt(least, bound)) == f) {
186 assertTrue(least <= j && j < bound);
187 ++i;
188 }
189 assertTrue(i < NCALLS);
190 }
191 }
192 }
193
194 /**
195 * nextLong(negative) throws IllegalArgumentException
196 */
197 public void testNextLongBoundedNeg() {
198 SplittableRandom sr = new SplittableRandom();
199 try {
200 long f = sr.nextLong(-17);
201 shouldThrow();
202 } catch (IllegalArgumentException success) {}
203 }
204
205 /**
206 * nextLong(least >= bound) throws IllegalArgumentException
207 */
208 public void testNextLongBadBounds() {
209 SplittableRandom sr = new SplittableRandom();
210 try {
211 long f = sr.nextLong(17, 2);
212 shouldThrow();
213 } catch (IllegalArgumentException success) {}
214 }
215
216 /**
217 * nextLong(bound) returns 0 <= value < bound;
218 * repeated calls produce at least two distinct results
219 */
220 public void testNextLongBounded() {
221 SplittableRandom sr = new SplittableRandom();
222 for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
223 long f = sr.nextLong(bound);
224 assertTrue(0 <= f && f < bound);
225 int i = 0;
226 long j;
227 while (i < NCALLS &&
228 (j = sr.nextLong(bound)) == f) {
229 assertTrue(0 <= j && j < bound);
230 ++i;
231 }
232 assertTrue(i < NCALLS);
233 }
234 }
235
236 /**
237 * nextLong(least, bound) returns least <= value < bound;
238 * repeated calls produce at least two distinct results
239 */
240 public void testNextLongBounded2() {
241 SplittableRandom sr = new SplittableRandom();
242 for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
243 for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
244 long f = sr.nextLong(least, bound);
245 assertTrue(least <= f && f < bound);
246 int i = 0;
247 long j;
248 while (i < NCALLS &&
249 (j = sr.nextLong(least, bound)) == f) {
250 assertTrue(least <= j && j < bound);
251 ++i;
252 }
253 assertTrue(i < NCALLS);
254 }
255 }
256 }
257
258 /**
259 * nextDouble(least, bound) returns least <= value < bound;
260 * repeated calls produce at least two distinct results
261 */
262 public void testNextDoubleBounded2() {
263 SplittableRandom sr = new SplittableRandom();
264 for (double least = 0.0001; least < 1.0e20; least *= 8) {
265 for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
266 double f = sr.nextDouble(least, bound);
267 assertTrue(least <= f && f < bound);
268 int i = 0;
269 double j;
270 while (i < NCALLS &&
271 (j = sr.nextDouble(least, bound)) == f) {
272 assertTrue(least <= j && j < bound);
273 ++i;
274 }
275 assertTrue(i < NCALLS);
276 }
277 }
278 }
279
280 /**
281 * Invoking sized ints, long, doubles, with negative sizes throws
282 * IllegalArgumentException
283 */
284 public void testBadStreamSize() {
285 SplittableRandom r = new SplittableRandom();
286 try {
287 java.util.stream.IntStream x = r.ints(-1L);
288 shouldThrow();
289 } catch (IllegalArgumentException success) {}
290 try {
291 java.util.stream.IntStream x = r.ints(-1L, 2, 3);
292 shouldThrow();
293 } catch (IllegalArgumentException success) {}
294 try {
295 java.util.stream.LongStream x = r.longs(-1L);
296 shouldThrow();
297 } catch (IllegalArgumentException success) {}
298 try {
299 java.util.stream.LongStream x = r.longs(-1L, -1L, 1L);
300 shouldThrow();
301 } catch (IllegalArgumentException success) {}
302 try {
303 java.util.stream.DoubleStream x = r.doubles(-1L);
304 shouldThrow();
305 } catch (IllegalArgumentException success) {}
306 try {
307 java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6);
308 shouldThrow();
309 } catch (IllegalArgumentException success) {}
310 }
311
312 /**
313 * Invoking bounded ints, long, doubles, with illegal bounds throws
314 * IllegalArgumentException
315 */
316 public void testBadStreamBounds() {
317 SplittableRandom r = new SplittableRandom();
318 try {
319 java.util.stream.IntStream x = r.ints(2, 1);
320 shouldThrow();
321 } catch (IllegalArgumentException success) {}
322 try {
323 java.util.stream.IntStream x = r.ints(10, 42, 42);
324 shouldThrow();
325 } catch (IllegalArgumentException success) {}
326 try {
327 java.util.stream.LongStream x = r.longs(-1L, -1L);
328 shouldThrow();
329 } catch (IllegalArgumentException success) {}
330 try {
331 java.util.stream.LongStream x = r.longs(10, 1L, -2L);
332 shouldThrow();
333 } catch (IllegalArgumentException success) {}
334 try {
335 java.util.stream.DoubleStream x = r.doubles(0.0, 0.0);
336 shouldThrow();
337 } catch (IllegalArgumentException success) {}
338 try {
339 java.util.stream.DoubleStream x = r.doubles(10, .5, .4);
340 shouldThrow();
341 } catch (IllegalArgumentException success) {}
342 }
343
344 /**
345 * A parallel sized stream of ints generates the given number of values
346 */
347 public void testIntsCount() {
348 LongAdder counter = new LongAdder();
349 SplittableRandom r = new SplittableRandom();
350 long size = 0;
351 for (int reps = 0; reps < REPS; ++reps) {
352 counter.reset();
353 r.ints(size).parallel().forEach(x -> counter.increment());
354 assertEquals(size, counter.sum());
355 size += 524959;
356 }
357 }
358
359 /**
360 * A parallel sized stream of longs generates the given number of values
361 */
362 public void testLongsCount() {
363 LongAdder counter = new LongAdder();
364 SplittableRandom r = new SplittableRandom();
365 long size = 0;
366 for (int reps = 0; reps < REPS; ++reps) {
367 counter.reset();
368 r.longs(size).parallel().forEach(x -> counter.increment());
369 assertEquals(size, counter.sum());
370 size += 524959;
371 }
372 }
373
374 /**
375 * A parallel sized stream of doubles generates the given number of values
376 */
377 public void testDoublesCount() {
378 LongAdder counter = new LongAdder();
379 SplittableRandom r = new SplittableRandom();
380 long size = 0;
381 for (int reps = 0; reps < REPS; ++reps) {
382 counter.reset();
383 r.doubles(size).parallel().forEach(x -> counter.increment());
384 assertEquals(size, counter.sum());
385 size += 524959;
386 }
387 }
388
389 /**
390 * Each of a parallel sized stream of bounded ints is within bounds
391 */
392 public void testBoundedInts() {
393 AtomicInteger fails = new AtomicInteger(0);
394 SplittableRandom r = new SplittableRandom();
395 long size = 12345L;
396 for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
397 for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
398 final int lo = least, hi = bound;
399 r.ints(size, lo, hi).parallel().
400 forEach(x -> {if (x < lo || x >= hi)
401 fails.getAndIncrement(); });
402 }
403 }
404 assertEquals(0, fails.get());
405 }
406
407 /**
408 * Each of a parallel sized stream of bounded longs is within bounds
409 */
410 public void testBoundedLongs() {
411 AtomicInteger fails = new AtomicInteger(0);
412 SplittableRandom r = new SplittableRandom();
413 long size = 123L;
414 for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
415 for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
416 final long lo = least, hi = bound;
417 r.longs(size, lo, hi).parallel().
418 forEach(x -> {if (x < lo || x >= hi)
419 fails.getAndIncrement(); });
420 }
421 }
422 assertEquals(0, fails.get());
423 }
424
425 /**
426 * Each of a parallel sized stream of bounded doubles is within bounds
427 */
428 public void testBoundedDoubles() {
429 AtomicInteger fails = new AtomicInteger(0);
430 SplittableRandom r = new SplittableRandom();
431 long size = 456;
432 for (double least = 0.00011; least < 1.0e20; least *= 9) {
433 for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
434 final double lo = least, hi = bound;
435 r.doubles(size, lo, hi).parallel().
436 forEach(x -> {if (x < lo || x >= hi)
437 fails.getAndIncrement(); });
438 }
439 }
440 assertEquals(0, fails.get());
441 }
442
443 /**
444 * A parallel unsized stream of ints generates at least 100 values
445 */
446 public void testUnsizedIntsCount() {
447 LongAdder counter = new LongAdder();
448 SplittableRandom r = new SplittableRandom();
449 long size = 100;
450 r.ints().limit(size).parallel().forEach(x -> counter.increment());
451 assertEquals(size, counter.sum());
452 }
453
454 /**
455 * A parallel unsized stream of longs generates at least 100 values
456 */
457 public void testUnsizedLongsCount() {
458 LongAdder counter = new LongAdder();
459 SplittableRandom r = new SplittableRandom();
460 long size = 100;
461 r.longs().limit(size).parallel().forEach(x -> counter.increment());
462 assertEquals(size, counter.sum());
463 }
464
465 /**
466 * A parallel unsized stream of doubles generates at least 100 values
467 */
468 public void testUnsizedDoublesCount() {
469 LongAdder counter = new LongAdder();
470 SplittableRandom r = new SplittableRandom();
471 long size = 100;
472 r.doubles().limit(size).parallel().forEach(x -> counter.increment());
473 assertEquals(size, counter.sum());
474 }
475
476 /**
477 * A sequential unsized stream of ints generates at least 100 values
478 */
479 public void testUnsizedIntsCountSeq() {
480 LongAdder counter = new LongAdder();
481 SplittableRandom r = new SplittableRandom();
482 long size = 100;
483 r.ints().limit(size).forEach(x -> counter.increment());
484 assertEquals(size, counter.sum());
485 }
486
487 /**
488 * A sequential unsized stream of longs generates at least 100 values
489 */
490 public void testUnsizedLongsCountSeq() {
491 LongAdder counter = new LongAdder();
492 SplittableRandom r = new SplittableRandom();
493 long size = 100;
494 r.longs().limit(size).forEach(x -> counter.increment());
495 assertEquals(size, counter.sum());
496 }
497
498 /**
499 * A sequential unsized stream of doubles generates at least 100 values
500 */
501 public void testUnsizedDoublesCountSeq() {
502 LongAdder counter = new LongAdder();
503 SplittableRandom r = new SplittableRandom();
504 long size = 100;
505 r.doubles().limit(size).forEach(x -> counter.increment());
506 assertEquals(size, counter.sum());
507 }
508
509 }