1 |
jsr166 |
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.7 |
|
7 |
jsr166 |
1.1 |
import java.util.concurrent.ThreadLocalRandom; |
8 |
|
|
import java.util.concurrent.atomic.AtomicInteger; |
9 |
|
|
import java.util.concurrent.atomic.LongAdder; |
10 |
|
|
|
11 |
jsr166 |
1.7 |
import junit.framework.Test; |
12 |
|
|
import junit.framework.TestSuite; |
13 |
|
|
|
14 |
jsr166 |
1.1 |
public class ThreadLocalRandom8Test extends JSR166TestCase { |
15 |
|
|
|
16 |
|
|
public static void main(String[] args) { |
17 |
jsr166 |
1.9 |
main(suite(), args); |
18 |
jsr166 |
1.1 |
} |
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(ThreadLocalRandom8Test.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
// max sampled int bound |
24 |
jsr166 |
1.5 |
static final int MAX_INT_BOUND = (1 << 26); |
25 |
jsr166 |
1.1 |
|
26 |
|
|
// max sampled long bound |
27 |
|
|
static final long MAX_LONG_BOUND = (1L << 42); |
28 |
|
|
|
29 |
|
|
// Number of replications for other checks |
30 |
jsr166 |
1.4 |
static final int REPS = |
31 |
|
|
Integer.getInteger("ThreadLocalRandom8Test.reps", 4); |
32 |
jsr166 |
1.1 |
|
33 |
|
|
/** |
34 |
|
|
* Invoking sized ints, long, doubles, with negative sizes throws |
35 |
|
|
* IllegalArgumentException |
36 |
|
|
*/ |
37 |
|
|
public void testBadStreamSize() { |
38 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
39 |
jsr166 |
1.13 |
assertThrows( |
40 |
|
|
IllegalArgumentException.class, |
41 |
jsr166 |
1.6 |
() -> r.ints(-1L), |
42 |
|
|
() -> r.ints(-1L, 2, 3), |
43 |
|
|
() -> r.longs(-1L), |
44 |
|
|
() -> r.longs(-1L, -1L, 1L), |
45 |
|
|
() -> r.doubles(-1L), |
46 |
jsr166 |
1.13 |
() -> r.doubles(-1L, .5, .6)); |
47 |
jsr166 |
1.1 |
} |
48 |
|
|
|
49 |
|
|
/** |
50 |
|
|
* Invoking bounded ints, long, doubles, with illegal bounds throws |
51 |
|
|
* IllegalArgumentException |
52 |
|
|
*/ |
53 |
|
|
public void testBadStreamBounds() { |
54 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
55 |
jsr166 |
1.13 |
assertThrows( |
56 |
|
|
IllegalArgumentException.class, |
57 |
jsr166 |
1.6 |
() -> 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 |
jsr166 |
1.13 |
() -> r.doubles(10, .5, .4)); |
63 |
jsr166 |
1.1 |
} |
64 |
|
|
|
65 |
|
|
/** |
66 |
|
|
* A parallel sized stream of ints generates the given number of values |
67 |
|
|
*/ |
68 |
|
|
public void testIntsCount() { |
69 |
|
|
LongAdder counter = new LongAdder(); |
70 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
71 |
|
|
long size = 0; |
72 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
73 |
|
|
counter.reset(); |
74 |
jsr166 |
1.3 |
r.ints(size).parallel().forEach(x -> counter.increment()); |
75 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
76 |
|
|
size += 524959; |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* A parallel sized stream of longs generates the given number of values |
82 |
|
|
*/ |
83 |
|
|
public void testLongsCount() { |
84 |
|
|
LongAdder counter = new LongAdder(); |
85 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
86 |
|
|
long size = 0; |
87 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
88 |
|
|
counter.reset(); |
89 |
jsr166 |
1.3 |
r.longs(size).parallel().forEach(x -> counter.increment()); |
90 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
91 |
|
|
size += 524959; |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/** |
96 |
|
|
* A parallel sized stream of doubles generates the given number of values |
97 |
|
|
*/ |
98 |
|
|
public void testDoublesCount() { |
99 |
|
|
LongAdder counter = new LongAdder(); |
100 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
101 |
|
|
long size = 0; |
102 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
103 |
|
|
counter.reset(); |
104 |
jsr166 |
1.3 |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
105 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
106 |
|
|
size += 524959; |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* Each of a parallel sized stream of bounded ints is within bounds |
112 |
|
|
*/ |
113 |
|
|
public void testBoundedInts() { |
114 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
115 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
116 |
|
|
long size = 12345L; |
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 |
jsr166 |
1.8 |
r.ints(size, lo, hi).parallel().forEach( |
121 |
|
|
x -> { |
122 |
|
|
if (x < lo || x >= hi) |
123 |
|
|
fails.getAndIncrement(); }); |
124 |
jsr166 |
1.1 |
} |
125 |
|
|
} |
126 |
|
|
assertEquals(0, fails.get()); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
|
|
* Each of a parallel sized stream of bounded longs is within bounds |
131 |
|
|
*/ |
132 |
|
|
public void testBoundedLongs() { |
133 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
134 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
135 |
|
|
long size = 123L; |
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 |
jsr166 |
1.8 |
r.longs(size, lo, hi).parallel().forEach( |
140 |
|
|
x -> { |
141 |
|
|
if (x < lo || x >= hi) |
142 |
|
|
fails.getAndIncrement(); }); |
143 |
jsr166 |
1.1 |
} |
144 |
|
|
} |
145 |
|
|
assertEquals(0, fails.get()); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
/** |
149 |
|
|
* Each of a parallel sized stream of bounded doubles is within bounds |
150 |
|
|
*/ |
151 |
|
|
public void testBoundedDoubles() { |
152 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
153 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
154 |
|
|
long size = 456; |
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 |
jsr166 |
1.8 |
r.doubles(size, lo, hi).parallel().forEach( |
159 |
|
|
x -> { |
160 |
|
|
if (x < lo || x >= hi) |
161 |
|
|
fails.getAndIncrement(); }); |
162 |
jsr166 |
1.1 |
} |
163 |
|
|
} |
164 |
|
|
assertEquals(0, fails.get()); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
|
|
* A parallel unsized stream of ints generates at least 100 values |
169 |
|
|
*/ |
170 |
|
|
public void testUnsizedIntsCount() { |
171 |
|
|
LongAdder counter = new LongAdder(); |
172 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
173 |
|
|
long size = 100; |
174 |
jsr166 |
1.3 |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
175 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
|
|
* A parallel unsized stream of longs generates at least 100 values |
180 |
|
|
*/ |
181 |
|
|
public void testUnsizedLongsCount() { |
182 |
|
|
LongAdder counter = new LongAdder(); |
183 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
184 |
|
|
long size = 100; |
185 |
jsr166 |
1.3 |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
186 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
/** |
190 |
|
|
* A parallel unsized stream of doubles generates at least 100 values |
191 |
|
|
*/ |
192 |
|
|
public void testUnsizedDoublesCount() { |
193 |
|
|
LongAdder counter = new LongAdder(); |
194 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
195 |
|
|
long size = 100; |
196 |
jsr166 |
1.3 |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
197 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
|
|
* A sequential unsized stream of ints generates at least 100 values |
202 |
|
|
*/ |
203 |
|
|
public void testUnsizedIntsCountSeq() { |
204 |
|
|
LongAdder counter = new LongAdder(); |
205 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
206 |
|
|
long size = 100; |
207 |
jsr166 |
1.3 |
r.ints().limit(size).forEach(x -> counter.increment()); |
208 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
/** |
212 |
|
|
* A sequential unsized stream of longs generates at least 100 values |
213 |
|
|
*/ |
214 |
|
|
public void testUnsizedLongsCountSeq() { |
215 |
|
|
LongAdder counter = new LongAdder(); |
216 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
217 |
|
|
long size = 100; |
218 |
jsr166 |
1.3 |
r.longs().limit(size).forEach(x -> counter.increment()); |
219 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
/** |
223 |
|
|
* A sequential unsized stream of doubles generates at least 100 values |
224 |
|
|
*/ |
225 |
|
|
public void testUnsizedDoublesCountSeq() { |
226 |
|
|
LongAdder counter = new LongAdder(); |
227 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
228 |
|
|
long size = 100; |
229 |
jsr166 |
1.3 |
r.doubles().limit(size).forEach(x -> counter.increment()); |
230 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
231 |
|
|
} |
232 |
|
|
|
233 |
jsr166 |
1.10 |
/** |
234 |
jsr166 |
1.12 |
* A deserialized/reserialized ThreadLocalRandom is always |
235 |
|
|
* identical to ThreadLocalRandom.current() |
236 |
jsr166 |
1.10 |
*/ |
237 |
|
|
public void testSerialization() { |
238 |
jsr166 |
1.11 |
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 |
jsr166 |
1.10 |
} |
248 |
|
|
|
249 |
jsr166 |
1.1 |
} |