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.6 |
Runnable[] throwingActions = { |
40 |
|
|
() -> r.ints(-1L), |
41 |
|
|
() -> r.ints(-1L, 2, 3), |
42 |
|
|
() -> r.longs(-1L), |
43 |
|
|
() -> r.longs(-1L, -1L, 1L), |
44 |
|
|
() -> r.doubles(-1L), |
45 |
|
|
() -> r.doubles(-1L, .5, .6), |
46 |
|
|
}; |
47 |
|
|
assertThrows(IllegalArgumentException.class, throwingActions); |
48 |
jsr166 |
1.1 |
} |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Invoking bounded ints, long, doubles, with illegal bounds throws |
52 |
|
|
* IllegalArgumentException |
53 |
|
|
*/ |
54 |
|
|
public void testBadStreamBounds() { |
55 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
56 |
jsr166 |
1.6 |
Runnable[] throwingActions = { |
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 |
|
|
assertThrows(IllegalArgumentException.class, throwingActions); |
65 |
jsr166 |
1.1 |
} |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* A parallel sized stream of ints generates the given number of values |
69 |
|
|
*/ |
70 |
|
|
public void testIntsCount() { |
71 |
|
|
LongAdder counter = new LongAdder(); |
72 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
73 |
|
|
long size = 0; |
74 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
75 |
|
|
counter.reset(); |
76 |
jsr166 |
1.3 |
r.ints(size).parallel().forEach(x -> counter.increment()); |
77 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
78 |
|
|
size += 524959; |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* A parallel sized stream of longs generates the given number of values |
84 |
|
|
*/ |
85 |
|
|
public void testLongsCount() { |
86 |
|
|
LongAdder counter = new LongAdder(); |
87 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
88 |
|
|
long size = 0; |
89 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
90 |
|
|
counter.reset(); |
91 |
jsr166 |
1.3 |
r.longs(size).parallel().forEach(x -> counter.increment()); |
92 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
93 |
|
|
size += 524959; |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* A parallel sized stream of doubles generates the given number of values |
99 |
|
|
*/ |
100 |
|
|
public void testDoublesCount() { |
101 |
|
|
LongAdder counter = new LongAdder(); |
102 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
103 |
|
|
long size = 0; |
104 |
|
|
for (int reps = 0; reps < REPS; ++reps) { |
105 |
|
|
counter.reset(); |
106 |
jsr166 |
1.3 |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
107 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
108 |
|
|
size += 524959; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
/** |
113 |
|
|
* Each of a parallel sized stream of bounded ints is within bounds |
114 |
|
|
*/ |
115 |
|
|
public void testBoundedInts() { |
116 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
117 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
118 |
|
|
long size = 12345L; |
119 |
|
|
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
120 |
|
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
121 |
|
|
final int lo = least, hi = bound; |
122 |
jsr166 |
1.8 |
r.ints(size, lo, hi).parallel().forEach( |
123 |
|
|
x -> { |
124 |
|
|
if (x < lo || x >= hi) |
125 |
|
|
fails.getAndIncrement(); }); |
126 |
jsr166 |
1.1 |
} |
127 |
|
|
} |
128 |
|
|
assertEquals(0, fails.get()); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
/** |
132 |
|
|
* Each of a parallel sized stream of bounded longs is within bounds |
133 |
|
|
*/ |
134 |
|
|
public void testBoundedLongs() { |
135 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
136 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
137 |
|
|
long size = 123L; |
138 |
|
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
139 |
|
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
140 |
|
|
final long lo = least, hi = bound; |
141 |
jsr166 |
1.8 |
r.longs(size, lo, hi).parallel().forEach( |
142 |
|
|
x -> { |
143 |
|
|
if (x < lo || x >= hi) |
144 |
|
|
fails.getAndIncrement(); }); |
145 |
jsr166 |
1.1 |
} |
146 |
|
|
} |
147 |
|
|
assertEquals(0, fails.get()); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* Each of a parallel sized stream of bounded doubles is within bounds |
152 |
|
|
*/ |
153 |
|
|
public void testBoundedDoubles() { |
154 |
|
|
AtomicInteger fails = new AtomicInteger(0); |
155 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
156 |
|
|
long size = 456; |
157 |
|
|
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
158 |
|
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
159 |
|
|
final double lo = least, hi = bound; |
160 |
jsr166 |
1.8 |
r.doubles(size, lo, hi).parallel().forEach( |
161 |
|
|
x -> { |
162 |
|
|
if (x < lo || x >= hi) |
163 |
|
|
fails.getAndIncrement(); }); |
164 |
jsr166 |
1.1 |
} |
165 |
|
|
} |
166 |
|
|
assertEquals(0, fails.get()); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
/** |
170 |
|
|
* A parallel unsized stream of ints generates at least 100 values |
171 |
|
|
*/ |
172 |
|
|
public void testUnsizedIntsCount() { |
173 |
|
|
LongAdder counter = new LongAdder(); |
174 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
175 |
|
|
long size = 100; |
176 |
jsr166 |
1.3 |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
177 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
/** |
181 |
|
|
* A parallel unsized stream of longs generates at least 100 values |
182 |
|
|
*/ |
183 |
|
|
public void testUnsizedLongsCount() { |
184 |
|
|
LongAdder counter = new LongAdder(); |
185 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
186 |
|
|
long size = 100; |
187 |
jsr166 |
1.3 |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
188 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
/** |
192 |
|
|
* A parallel unsized stream of doubles generates at least 100 values |
193 |
|
|
*/ |
194 |
|
|
public void testUnsizedDoublesCount() { |
195 |
|
|
LongAdder counter = new LongAdder(); |
196 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
197 |
|
|
long size = 100; |
198 |
jsr166 |
1.3 |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
199 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
/** |
203 |
|
|
* A sequential unsized stream of ints generates at least 100 values |
204 |
|
|
*/ |
205 |
|
|
public void testUnsizedIntsCountSeq() { |
206 |
|
|
LongAdder counter = new LongAdder(); |
207 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
208 |
|
|
long size = 100; |
209 |
jsr166 |
1.3 |
r.ints().limit(size).forEach(x -> counter.increment()); |
210 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
/** |
214 |
|
|
* A sequential unsized stream of longs generates at least 100 values |
215 |
|
|
*/ |
216 |
|
|
public void testUnsizedLongsCountSeq() { |
217 |
|
|
LongAdder counter = new LongAdder(); |
218 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
219 |
|
|
long size = 100; |
220 |
jsr166 |
1.3 |
r.longs().limit(size).forEach(x -> counter.increment()); |
221 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
/** |
225 |
|
|
* A sequential unsized stream of doubles generates at least 100 values |
226 |
|
|
*/ |
227 |
|
|
public void testUnsizedDoublesCountSeq() { |
228 |
|
|
LongAdder counter = new LongAdder(); |
229 |
|
|
ThreadLocalRandom r = ThreadLocalRandom.current(); |
230 |
|
|
long size = 100; |
231 |
jsr166 |
1.3 |
r.doubles().limit(size).forEach(x -> counter.increment()); |
232 |
jsr166 |
1.1 |
assertEquals(size, counter.sum()); |
233 |
|
|
} |
234 |
|
|
|
235 |
jsr166 |
1.10 |
/** |
236 |
|
|
* A deserialized ThreadLocalRandom is always identical to |
237 |
|
|
* ThreadLocalRandom.current() |
238 |
|
|
*/ |
239 |
|
|
public void testSerialization() { |
240 |
jsr166 |
1.11 |
assertSame( |
241 |
|
|
ThreadLocalRandom.current(), |
242 |
|
|
serialClone(ThreadLocalRandom.current())); |
243 |
|
|
// In the current implementation, there is exactly one shared instance |
244 |
|
|
if (testImplementationDetails) |
245 |
|
|
assertSame( |
246 |
|
|
ThreadLocalRandom.current(), |
247 |
|
|
java.util.concurrent.CompletableFuture.supplyAsync( |
248 |
|
|
() -> serialClone(ThreadLocalRandom.current())).join()); |
249 |
jsr166 |
1.10 |
} |
250 |
|
|
|
251 |
jsr166 |
1.1 |
} |