4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
+ |
import java.util.Arrays; |
8 |
+ |
import java.util.List; |
9 |
|
import java.util.SplittableRandom; |
10 |
|
import java.util.concurrent.atomic.AtomicInteger; |
11 |
|
import java.util.concurrent.atomic.LongAdder; |
12 |
+ |
import java.lang.reflect.Method; |
13 |
+ |
import java.util.function.Predicate; |
14 |
+ |
import java.util.stream.Collectors; |
15 |
+ |
import java.util.stream.DoubleStream; |
16 |
+ |
import java.util.stream.IntStream; |
17 |
+ |
import java.util.stream.LongStream; |
18 |
|
|
19 |
|
import junit.framework.Test; |
20 |
|
import junit.framework.TestSuite; |
141 |
|
*/ |
142 |
|
public void testNextIntBoundNonPositive() { |
143 |
|
SplittableRandom sr = new SplittableRandom(); |
144 |
< |
Runnable[] throwingActions = { |
144 |
> |
assertThrows( |
145 |
> |
IllegalArgumentException.class, |
146 |
|
() -> sr.nextInt(-17), |
147 |
|
() -> sr.nextInt(0), |
148 |
< |
() -> sr.nextInt(Integer.MIN_VALUE), |
140 |
< |
}; |
141 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
148 |
> |
() -> sr.nextInt(Integer.MIN_VALUE)); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
153 |
|
*/ |
154 |
|
public void testNextIntBadBounds() { |
155 |
|
SplittableRandom sr = new SplittableRandom(); |
156 |
< |
Runnable[] throwingActions = { |
156 |
> |
assertThrows( |
157 |
> |
IllegalArgumentException.class, |
158 |
|
() -> sr.nextInt(17, 2), |
159 |
|
() -> sr.nextInt(-42, -42), |
160 |
< |
() -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE), |
153 |
< |
}; |
154 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
160 |
> |
() -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE)); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
209 |
|
*/ |
210 |
|
public void testNextLongBoundNonPositive() { |
211 |
|
SplittableRandom sr = new SplittableRandom(); |
212 |
< |
Runnable[] throwingActions = { |
212 |
> |
assertThrows( |
213 |
> |
IllegalArgumentException.class, |
214 |
|
() -> sr.nextLong(-17L), |
215 |
|
() -> sr.nextLong(0L), |
216 |
< |
() -> sr.nextLong(Long.MIN_VALUE), |
210 |
< |
}; |
211 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
216 |
> |
() -> sr.nextLong(Long.MIN_VALUE)); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
221 |
|
*/ |
222 |
|
public void testNextLongBadBounds() { |
223 |
|
SplittableRandom sr = new SplittableRandom(); |
224 |
< |
Runnable[] throwingActions = { |
224 |
> |
assertThrows( |
225 |
> |
IllegalArgumentException.class, |
226 |
|
() -> sr.nextLong(17L, 2L), |
227 |
|
() -> sr.nextLong(-42L, -42L), |
228 |
< |
() -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE), |
223 |
< |
}; |
224 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
228 |
> |
() -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE)); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
276 |
|
*/ |
277 |
|
public void testNextDoubleBoundNonPositive() { |
278 |
|
SplittableRandom sr = new SplittableRandom(); |
279 |
< |
Runnable[] throwingActions = { |
279 |
> |
assertThrows( |
280 |
> |
IllegalArgumentException.class, |
281 |
|
() -> sr.nextDouble(-17.0d), |
282 |
|
() -> sr.nextDouble(0.0d), |
283 |
|
() -> sr.nextDouble(-Double.MIN_VALUE), |
284 |
|
() -> sr.nextDouble(Double.NEGATIVE_INFINITY), |
285 |
< |
() -> sr.nextDouble(Double.NaN), |
281 |
< |
}; |
282 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
285 |
> |
() -> sr.nextDouble(Double.NaN)); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
290 |
|
*/ |
291 |
|
public void testNextDoubleBadBounds() { |
292 |
|
SplittableRandom sr = new SplittableRandom(); |
293 |
< |
Runnable[] throwingActions = { |
293 |
> |
assertThrows( |
294 |
> |
IllegalArgumentException.class, |
295 |
|
() -> sr.nextDouble(17.0d, 2.0d), |
296 |
|
() -> sr.nextDouble(-42.0d, -42.0d), |
297 |
|
() -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), |
298 |
|
() -> sr.nextDouble(Double.NaN, 0.0d), |
299 |
< |
() -> sr.nextDouble(0.0d, Double.NaN), |
296 |
< |
}; |
297 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
299 |
> |
() -> sr.nextDouble(0.0d, Double.NaN)); |
300 |
|
} |
301 |
|
|
302 |
|
// TODO: Test infinite bounds! |
331 |
|
*/ |
332 |
|
public void testBadStreamSize() { |
333 |
|
SplittableRandom r = new SplittableRandom(); |
334 |
< |
Runnable[] throwingActions = { |
335 |
< |
() -> { java.util.stream.IntStream x = r.ints(-1L); }, |
336 |
< |
() -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); }, |
337 |
< |
() -> { java.util.stream.LongStream x = r.longs(-1L); }, |
338 |
< |
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); }, |
339 |
< |
() -> { java.util.stream.DoubleStream x = r.doubles(-1L); }, |
340 |
< |
() -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); }, |
341 |
< |
}; |
340 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
334 |
> |
assertThrows( |
335 |
> |
IllegalArgumentException.class, |
336 |
> |
() -> { IntStream unused = r.ints(-1L); }, |
337 |
> |
() -> { IntStream unused = r.ints(-1L, 2, 3); }, |
338 |
> |
() -> { LongStream unused = r.longs(-1L); }, |
339 |
> |
() -> { LongStream unused = r.longs(-1L, -1L, 1L); }, |
340 |
> |
() -> { DoubleStream unused = r.doubles(-1L); }, |
341 |
> |
() -> { DoubleStream unused = r.doubles(-1L, .5, .6); }); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
347 |
|
*/ |
348 |
|
public void testBadStreamBounds() { |
349 |
|
SplittableRandom r = new SplittableRandom(); |
350 |
< |
Runnable[] throwingActions = { |
351 |
< |
() -> { java.util.stream.IntStream x = r.ints(2, 1); }, |
352 |
< |
() -> { java.util.stream.IntStream x = r.ints(10, 42, 42); }, |
353 |
< |
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L); }, |
354 |
< |
() -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); }, |
355 |
< |
() -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); }, |
356 |
< |
() -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); }, |
357 |
< |
}; |
357 |
< |
assertThrows(IllegalArgumentException.class, throwingActions); |
350 |
> |
assertThrows( |
351 |
> |
IllegalArgumentException.class, |
352 |
> |
() -> { IntStream unused = r.ints(2, 1); }, |
353 |
> |
() -> { IntStream unused = r.ints(10, 42, 42); }, |
354 |
> |
() -> { LongStream unused = r.longs(-1L, -1L); }, |
355 |
> |
() -> { LongStream unused = r.longs(10, 1L, -2L); }, |
356 |
> |
() -> { DoubleStream unused = r.doubles(0.0, 0.0); }, |
357 |
> |
() -> { DoubleStream unused = r.doubles(10, .5, .4); }); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
525 |
|
assertEquals(size, counter.sum()); |
526 |
|
} |
527 |
|
|
528 |
+ |
/** |
529 |
+ |
* SplittableRandom should implement most of Random's public methods |
530 |
+ |
*/ |
531 |
+ |
public void testShouldImplementMostRandomMethods() throws Throwable { |
532 |
+ |
Predicate<Method> wasForgotten = method -> { |
533 |
+ |
String name = method.getName(); |
534 |
+ |
// some methods deliberately not implemented |
535 |
+ |
if (name.equals("setSeed")) return false; |
536 |
+ |
if (name.equals("nextFloat")) return false; |
537 |
+ |
if (name.equals("nextGaussian")) return false; |
538 |
+ |
try { |
539 |
+ |
SplittableRandom.class.getMethod( |
540 |
+ |
method.getName(), method.getParameterTypes()); |
541 |
+ |
} catch (ReflectiveOperationException ex) { |
542 |
+ |
return true; |
543 |
+ |
} |
544 |
+ |
return false; |
545 |
+ |
}; |
546 |
+ |
List<Method> forgotten = |
547 |
+ |
Arrays.stream(java.util.Random.class.getMethods()) |
548 |
+ |
.filter(wasForgotten) |
549 |
+ |
.collect(Collectors.toList()); |
550 |
+ |
if (!forgotten.isEmpty()) |
551 |
+ |
throw new AssertionError("Please implement: " + forgotten); |
552 |
+ |
} |
553 |
+ |
|
554 |
+ |
/** |
555 |
+ |
* Repeated calls to nextBytes produce at least values of different signs for every byte |
556 |
+ |
*/ |
557 |
+ |
public void testNextBytes() { |
558 |
+ |
SplittableRandom sr = new SplittableRandom(); |
559 |
+ |
int n = sr.nextInt(1, 20); |
560 |
+ |
byte[] bytes = new byte[n]; |
561 |
+ |
outer: |
562 |
+ |
for (int i = 0; i < n; i++) { |
563 |
+ |
for (int tries = NCALLS; tries-->0; ) { |
564 |
+ |
byte before = bytes[i]; |
565 |
+ |
sr.nextBytes(bytes); |
566 |
+ |
byte after = bytes[i]; |
567 |
+ |
if (after * before < 0) |
568 |
+ |
continue outer; |
569 |
+ |
} |
570 |
+ |
fail("not enough variation in random bytes"); |
571 |
+ |
} |
572 |
+ |
} |
573 |
+ |
|
574 |
+ |
/** |
575 |
+ |
* Filling an empty array with random bytes succeeds without effect. |
576 |
+ |
*/ |
577 |
+ |
public void testNextBytes_emptyArray() { |
578 |
+ |
new SplittableRandom().nextBytes(new byte[0]); |
579 |
+ |
} |
580 |
+ |
|
581 |
+ |
public void testNextBytes_nullArray() { |
582 |
+ |
try { |
583 |
+ |
new SplittableRandom().nextBytes(null); |
584 |
+ |
shouldThrow(); |
585 |
+ |
} catch (NullPointerException success) {} |
586 |
+ |
} |
587 |
+ |
|
588 |
|
} |