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.concurrent.ThreadLocalRandom; |
9 |
import java.util.concurrent.atomic.AtomicLong; |
10 |
import java.util.concurrent.atomic.AtomicInteger; |
11 |
import java.util.concurrent.atomic.AtomicReference; |
12 |
import java.util.concurrent.atomic.LongAdder; |
13 |
|
14 |
public class ThreadLocalRandom8Test extends JSR166TestCase { |
15 |
|
16 |
public static void main(String[] args) { |
17 |
junit.textui.TestRunner.run(suite()); |
18 |
} |
19 |
public static Test suite() { |
20 |
return new TestSuite(ThreadLocalRandom8Test.class); |
21 |
} |
22 |
|
23 |
// max sampled int bound |
24 |
static final int MAX_INT_BOUND = (1 << 28); |
25 |
|
26 |
// max sampled long bound |
27 |
static final long MAX_LONG_BOUND = (1L << 42); |
28 |
|
29 |
// Number of replications for other checks |
30 |
static final int REPS = 20; |
31 |
|
32 |
/** |
33 |
* Invoking sized ints, long, doubles, with negative sizes throws |
34 |
* IllegalArgumentException |
35 |
*/ |
36 |
public void testBadStreamSize() { |
37 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
38 |
try { |
39 |
java.util.stream.IntStream x = r.ints(-1L); |
40 |
shouldThrow(); |
41 |
} catch (IllegalArgumentException success) {} |
42 |
try { |
43 |
java.util.stream.IntStream x = r.ints(-1L, 2, 3); |
44 |
shouldThrow(); |
45 |
} catch (IllegalArgumentException success) {} |
46 |
try { |
47 |
java.util.stream.LongStream x = r.longs(-1L); |
48 |
shouldThrow(); |
49 |
} catch (IllegalArgumentException success) {} |
50 |
try { |
51 |
java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); |
52 |
shouldThrow(); |
53 |
} catch (IllegalArgumentException success) {} |
54 |
try { |
55 |
java.util.stream.DoubleStream x = r.doubles(-1L); |
56 |
shouldThrow(); |
57 |
} catch (IllegalArgumentException success) {} |
58 |
try { |
59 |
java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); |
60 |
shouldThrow(); |
61 |
} catch (IllegalArgumentException success) {} |
62 |
} |
63 |
|
64 |
/** |
65 |
* Invoking bounded ints, long, doubles, with illegal bounds throws |
66 |
* IllegalArgumentException |
67 |
*/ |
68 |
public void testBadStreamBounds() { |
69 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
70 |
try { |
71 |
java.util.stream.IntStream x = r.ints(2, 1); |
72 |
shouldThrow(); |
73 |
} catch (IllegalArgumentException success) {} |
74 |
try { |
75 |
java.util.stream.IntStream x = r.ints(10, 42, 42); |
76 |
shouldThrow(); |
77 |
} catch (IllegalArgumentException success) {} |
78 |
try { |
79 |
java.util.stream.LongStream x = r.longs(-1L, -1L); |
80 |
shouldThrow(); |
81 |
} catch (IllegalArgumentException success) {} |
82 |
try { |
83 |
java.util.stream.LongStream x = r.longs(10, 1L, -2L); |
84 |
shouldThrow(); |
85 |
} catch (IllegalArgumentException success) {} |
86 |
try { |
87 |
java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); |
88 |
shouldThrow(); |
89 |
} catch (IllegalArgumentException success) {} |
90 |
try { |
91 |
java.util.stream.DoubleStream x = r.doubles(10, .5, .4); |
92 |
shouldThrow(); |
93 |
} catch (IllegalArgumentException success) {} |
94 |
} |
95 |
|
96 |
/** |
97 |
* A parallel sized stream of ints generates the given number of values |
98 |
*/ |
99 |
public void testIntsCount() { |
100 |
LongAdder counter = new LongAdder(); |
101 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
102 |
long size = 0; |
103 |
for (int reps = 0; reps < REPS; ++reps) { |
104 |
counter.reset(); |
105 |
r.ints(size).parallel().forEach(x -> {counter.increment();}); |
106 |
assertEquals(size, counter.sum()); |
107 |
size += 524959; |
108 |
} |
109 |
} |
110 |
|
111 |
/** |
112 |
* A parallel sized stream of longs generates the given number of values |
113 |
*/ |
114 |
public void testLongsCount() { |
115 |
LongAdder counter = new LongAdder(); |
116 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
117 |
long size = 0; |
118 |
for (int reps = 0; reps < REPS; ++reps) { |
119 |
counter.reset(); |
120 |
r.longs(size).parallel().forEach(x -> {counter.increment();}); |
121 |
assertEquals(size, counter.sum()); |
122 |
size += 524959; |
123 |
} |
124 |
} |
125 |
|
126 |
/** |
127 |
* A parallel sized stream of doubles generates the given number of values |
128 |
*/ |
129 |
public void testDoublesCount() { |
130 |
LongAdder counter = new LongAdder(); |
131 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
132 |
long size = 0; |
133 |
for (int reps = 0; reps < REPS; ++reps) { |
134 |
counter.reset(); |
135 |
r.doubles(size).parallel().forEach(x -> {counter.increment();}); |
136 |
assertEquals(size, counter.sum()); |
137 |
size += 524959; |
138 |
} |
139 |
} |
140 |
|
141 |
/** |
142 |
* Each of a parallel sized stream of bounded ints is within bounds |
143 |
*/ |
144 |
public void testBoundedInts() { |
145 |
AtomicInteger fails = new AtomicInteger(0); |
146 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
147 |
long size = 12345L; |
148 |
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
149 |
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
150 |
final int lo = least, hi = bound; |
151 |
r.ints(size, lo, hi).parallel(). |
152 |
forEach(x -> {if (x < lo || x >= hi) |
153 |
fails.getAndIncrement(); }); |
154 |
} |
155 |
} |
156 |
assertEquals(0, fails.get()); |
157 |
} |
158 |
|
159 |
/** |
160 |
* Each of a parallel sized stream of bounded longs is within bounds |
161 |
*/ |
162 |
public void testBoundedLongs() { |
163 |
AtomicInteger fails = new AtomicInteger(0); |
164 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
165 |
long size = 123L; |
166 |
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
167 |
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
168 |
final long lo = least, hi = bound; |
169 |
r.longs(size, lo, hi).parallel(). |
170 |
forEach(x -> {if (x < lo || x >= hi) |
171 |
fails.getAndIncrement(); }); |
172 |
} |
173 |
} |
174 |
assertEquals(0, fails.get()); |
175 |
} |
176 |
|
177 |
/** |
178 |
* Each of a parallel sized stream of bounded doubles is within bounds |
179 |
*/ |
180 |
public void testBoundedDoubles() { |
181 |
AtomicInteger fails = new AtomicInteger(0); |
182 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
183 |
long size = 456; |
184 |
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
185 |
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
186 |
final double lo = least, hi = bound; |
187 |
r.doubles(size, lo, hi).parallel(). |
188 |
forEach(x -> {if (x < lo || x >= hi) |
189 |
fails.getAndIncrement(); }); |
190 |
} |
191 |
} |
192 |
assertEquals(0, fails.get()); |
193 |
} |
194 |
|
195 |
/** |
196 |
* A parallel unsized stream of ints generates at least 100 values |
197 |
*/ |
198 |
public void testUnsizedIntsCount() { |
199 |
LongAdder counter = new LongAdder(); |
200 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
201 |
long size = 100; |
202 |
r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); |
203 |
assertEquals(size, counter.sum()); |
204 |
} |
205 |
|
206 |
/** |
207 |
* A parallel unsized stream of longs generates at least 100 values |
208 |
*/ |
209 |
public void testUnsizedLongsCount() { |
210 |
LongAdder counter = new LongAdder(); |
211 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
212 |
long size = 100; |
213 |
r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); |
214 |
assertEquals(size, counter.sum()); |
215 |
} |
216 |
|
217 |
/** |
218 |
* A parallel unsized stream of doubles generates at least 100 values |
219 |
*/ |
220 |
public void testUnsizedDoublesCount() { |
221 |
LongAdder counter = new LongAdder(); |
222 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
223 |
long size = 100; |
224 |
r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); |
225 |
assertEquals(size, counter.sum()); |
226 |
} |
227 |
|
228 |
/** |
229 |
* A sequential unsized stream of ints generates at least 100 values |
230 |
*/ |
231 |
public void testUnsizedIntsCountSeq() { |
232 |
LongAdder counter = new LongAdder(); |
233 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
234 |
long size = 100; |
235 |
r.ints().limit(size).forEach(x -> {counter.increment();}); |
236 |
assertEquals(size, counter.sum()); |
237 |
} |
238 |
|
239 |
/** |
240 |
* A sequential unsized stream of longs generates at least 100 values |
241 |
*/ |
242 |
public void testUnsizedLongsCountSeq() { |
243 |
LongAdder counter = new LongAdder(); |
244 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
245 |
long size = 100; |
246 |
r.longs().limit(size).forEach(x -> {counter.increment();}); |
247 |
assertEquals(size, counter.sum()); |
248 |
} |
249 |
|
250 |
/** |
251 |
* A sequential unsized stream of doubles generates at least 100 values |
252 |
*/ |
253 |
public void testUnsizedDoublesCountSeq() { |
254 |
LongAdder counter = new LongAdder(); |
255 |
ThreadLocalRandom r = ThreadLocalRandom.current(); |
256 |
long size = 100; |
257 |
r.doubles().limit(size).forEach(x -> {counter.increment();}); |
258 |
assertEquals(size, counter.sum()); |
259 |
} |
260 |
|
261 |
} |