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.*; |
6 |
> |
|
7 |
|
import java.util.concurrent.ThreadLocalRandom; |
8 |
|
import java.util.concurrent.atomic.AtomicInteger; |
9 |
|
import java.util.concurrent.atomic.LongAdder; |
10 |
|
|
11 |
+ |
import junit.framework.Test; |
12 |
+ |
import junit.framework.TestSuite; |
13 |
+ |
|
14 |
|
public class ThreadLocalRandom8Test extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run(suite()); |
17 |
> |
main(suite(), args); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ThreadLocalRandom8Test.class); |
36 |
|
*/ |
37 |
|
public void testBadStreamSize() { |
38 |
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
39 |
< |
try { |
40 |
< |
java.util.stream.IntStream x = r.ints(-1L); |
41 |
< |
shouldThrow(); |
42 |
< |
} catch (IllegalArgumentException success) {} |
43 |
< |
try { |
44 |
< |
java.util.stream.IntStream x = r.ints(-1L, 2, 3); |
45 |
< |
shouldThrow(); |
46 |
< |
} catch (IllegalArgumentException success) {} |
45 |
< |
try { |
46 |
< |
java.util.stream.LongStream x = r.longs(-1L); |
47 |
< |
shouldThrow(); |
48 |
< |
} catch (IllegalArgumentException success) {} |
49 |
< |
try { |
50 |
< |
java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); |
51 |
< |
shouldThrow(); |
52 |
< |
} catch (IllegalArgumentException success) {} |
53 |
< |
try { |
54 |
< |
java.util.stream.DoubleStream x = r.doubles(-1L); |
55 |
< |
shouldThrow(); |
56 |
< |
} catch (IllegalArgumentException success) {} |
57 |
< |
try { |
58 |
< |
java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); |
59 |
< |
shouldThrow(); |
60 |
< |
} catch (IllegalArgumentException success) {} |
39 |
> |
assertThrows( |
40 |
> |
IllegalArgumentException.class, |
41 |
> |
() -> r.ints(-1L), |
42 |
> |
() -> r.ints(-1L, 2, 3), |
43 |
> |
() -> r.longs(-1L), |
44 |
> |
() -> r.longs(-1L, -1L, 1L), |
45 |
> |
() -> r.doubles(-1L), |
46 |
> |
() -> r.doubles(-1L, .5, .6)); |
47 |
|
} |
48 |
|
|
49 |
|
/** |
52 |
|
*/ |
53 |
|
public void testBadStreamBounds() { |
54 |
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
55 |
< |
try { |
56 |
< |
java.util.stream.IntStream x = r.ints(2, 1); |
57 |
< |
shouldThrow(); |
58 |
< |
} catch (IllegalArgumentException success) {} |
59 |
< |
try { |
60 |
< |
java.util.stream.IntStream x = r.ints(10, 42, 42); |
61 |
< |
shouldThrow(); |
62 |
< |
} catch (IllegalArgumentException success) {} |
77 |
< |
try { |
78 |
< |
java.util.stream.LongStream x = r.longs(-1L, -1L); |
79 |
< |
shouldThrow(); |
80 |
< |
} catch (IllegalArgumentException success) {} |
81 |
< |
try { |
82 |
< |
java.util.stream.LongStream x = r.longs(10, 1L, -2L); |
83 |
< |
shouldThrow(); |
84 |
< |
} catch (IllegalArgumentException success) {} |
85 |
< |
try { |
86 |
< |
java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); |
87 |
< |
shouldThrow(); |
88 |
< |
} catch (IllegalArgumentException success) {} |
89 |
< |
try { |
90 |
< |
java.util.stream.DoubleStream x = r.doubles(10, .5, .4); |
91 |
< |
shouldThrow(); |
92 |
< |
} catch (IllegalArgumentException success) {} |
55 |
> |
assertThrows( |
56 |
> |
IllegalArgumentException.class, |
57 |
> |
() -> r.ints(2, 1), |
58 |
> |
() -> r.ints(10, 42, 42), |
59 |
> |
() -> r.longs(-1L, -1L), |
60 |
> |
() -> r.longs(10, 1L, -2L), |
61 |
> |
() -> r.doubles(0.0, 0.0), |
62 |
> |
() -> r.doubles(10, .5, .4)); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
117 |
|
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
118 |
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
119 |
|
final int lo = least, hi = bound; |
120 |
< |
r.ints(size, lo, hi).parallel(). |
121 |
< |
forEach(x -> {if (x < lo || x >= hi) |
122 |
< |
fails.getAndIncrement(); }); |
120 |
> |
r.ints(size, lo, hi).parallel().forEach( |
121 |
> |
x -> { |
122 |
> |
if (x < lo || x >= hi) |
123 |
> |
fails.getAndIncrement(); }); |
124 |
|
} |
125 |
|
} |
126 |
|
assertEquals(0, fails.get()); |
136 |
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
137 |
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
138 |
|
final long lo = least, hi = bound; |
139 |
< |
r.longs(size, lo, hi).parallel(). |
140 |
< |
forEach(x -> {if (x < lo || x >= hi) |
141 |
< |
fails.getAndIncrement(); }); |
139 |
> |
r.longs(size, lo, hi).parallel().forEach( |
140 |
> |
x -> { |
141 |
> |
if (x < lo || x >= hi) |
142 |
> |
fails.getAndIncrement(); }); |
143 |
|
} |
144 |
|
} |
145 |
|
assertEquals(0, fails.get()); |
155 |
|
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
156 |
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
157 |
|
final double lo = least, hi = bound; |
158 |
< |
r.doubles(size, lo, hi).parallel(). |
159 |
< |
forEach(x -> {if (x < lo || x >= hi) |
160 |
< |
fails.getAndIncrement(); }); |
158 |
> |
r.doubles(size, lo, hi).parallel().forEach( |
159 |
> |
x -> { |
160 |
> |
if (x < lo || x >= hi) |
161 |
> |
fails.getAndIncrement(); }); |
162 |
|
} |
163 |
|
} |
164 |
|
assertEquals(0, fails.get()); |
230 |
|
assertEquals(size, counter.sum()); |
231 |
|
} |
232 |
|
|
233 |
+ |
/** |
234 |
+ |
* A deserialized/reserialized ThreadLocalRandom is always |
235 |
+ |
* identical to ThreadLocalRandom.current() |
236 |
+ |
*/ |
237 |
+ |
public void testSerialization() { |
238 |
+ |
assertSame( |
239 |
+ |
ThreadLocalRandom.current(), |
240 |
+ |
serialClone(ThreadLocalRandom.current())); |
241 |
+ |
// In the current implementation, there is exactly one shared instance |
242 |
+ |
if (testImplementationDetails) |
243 |
+ |
assertSame( |
244 |
+ |
ThreadLocalRandom.current(), |
245 |
+ |
java.util.concurrent.CompletableFuture.supplyAsync( |
246 |
+ |
() -> serialClone(ThreadLocalRandom.current())).join()); |
247 |
+ |
} |
248 |
+ |
|
249 |
|
} |