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 |
|
|
13 |
|
public class ThreadLocalRandomTest extends JSR166TestCase { |
19 |
|
return new TestSuite(ThreadLocalRandomTest.class); |
20 |
|
} |
21 |
|
|
22 |
< |
/** |
22 |
> |
/* |
23 |
|
* Testing coverage notes: |
24 |
|
* |
25 |
|
* We don't test randomness properties, but only that repeated |
28 |
|
* across multiples of primes. |
29 |
|
*/ |
30 |
|
|
31 |
< |
// |
31 |
> |
// max numbers of calls to detect getting stuck on one value |
32 |
|
static final int NCALLS = 10000; |
33 |
|
|
34 |
|
// max sampled int bound |
35 |
|
static final int MAX_INT_BOUND = (1 << 28); |
36 |
|
|
37 |
< |
// Max sampled long bound |
37 |
> |
// max sampled long bound |
38 |
|
static final long MAX_LONG_BOUND = (1L << 42); |
39 |
|
|
40 |
+ |
// Number of replications for other checks |
41 |
+ |
static final int REPS = 20; |
42 |
+ |
|
43 |
|
/** |
44 |
|
* setSeed throws UnsupportedOperationException |
45 |
|
*/ |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* Repeated calls to nextInt produce at least one different result |
54 |
> |
* Repeated calls to nextInt produce at least two distinct results |
55 |
|
*/ |
56 |
|
public void testNextInt() { |
57 |
|
int f = ThreadLocalRandom.current().nextInt(); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* Repeated calls to nextLong produce at least one different result |
65 |
> |
* Repeated calls to nextLong produce at least two distinct results |
66 |
|
*/ |
67 |
|
public void testNextLong() { |
68 |
|
long f = ThreadLocalRandom.current().nextLong(); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
< |
* Repeated calls to nextBoolean produce at least one different result |
76 |
> |
* Repeated calls to nextBoolean produce at least two distinct results |
77 |
|
*/ |
78 |
|
public void testNextBoolean() { |
79 |
|
boolean f = ThreadLocalRandom.current().nextBoolean(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* Repeated calls to nextFloat produce at least one different result |
87 |
> |
* Repeated calls to nextFloat produce at least two distinct results |
88 |
|
*/ |
89 |
|
public void testNextFloat() { |
90 |
|
float f = ThreadLocalRandom.current().nextFloat(); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* Repeated calls to nextDouble produce at least one different result |
98 |
> |
* Repeated calls to nextDouble produce at least two distinct results |
99 |
|
*/ |
100 |
|
public void testNextDouble() { |
101 |
|
double f = ThreadLocalRandom.current().nextDouble(); |
102 |
< |
double i = 0; |
102 |
> |
int i = 0; |
103 |
|
while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f) |
104 |
|
++i; |
105 |
|
assertTrue(i < NCALLS); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* Repeated calls to nextGaussian produce at least one different result |
109 |
> |
* Repeated calls to nextGaussian produce at least two distinct results |
110 |
|
*/ |
111 |
|
public void testNextGaussian() { |
112 |
|
double f = ThreadLocalRandom.current().nextGaussian(); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
< |
* nextInt(negative) throws IllegalArgumentException; |
120 |
> |
* nextInt(negative) throws IllegalArgumentException |
121 |
|
*/ |
122 |
|
public void testNextIntBoundedNeg() { |
123 |
|
try { |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
< |
* nextInt(least >= bound) throws IllegalArgumentException; |
130 |
> |
* nextInt(least >= bound) throws IllegalArgumentException |
131 |
|
*/ |
132 |
|
public void testNextIntBadBounds() { |
133 |
|
try { |
138 |
|
|
139 |
|
/** |
140 |
|
* nextInt(bound) returns 0 <= value < bound; |
141 |
< |
* repeated calls produce at least one different result |
141 |
> |
* repeated calls produce at least two distinct results |
142 |
|
*/ |
143 |
|
public void testNextIntBounded() { |
144 |
|
// sample bound space across prime number increments |
158 |
|
|
159 |
|
/** |
160 |
|
* nextInt(least, bound) returns least <= value < bound; |
161 |
< |
* repeated calls produce at least one different result |
161 |
> |
* repeated calls produce at least two distinct results |
162 |
|
*/ |
163 |
|
public void testNextIntBounded2() { |
164 |
|
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* nextLong(negative) throws IllegalArgumentException; |
181 |
> |
* nextLong(negative) throws IllegalArgumentException |
182 |
|
*/ |
183 |
|
public void testNextLongBoundedNeg() { |
184 |
|
try { |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* nextLong(least >= bound) throws IllegalArgumentException; |
191 |
> |
* nextLong(least >= bound) throws IllegalArgumentException |
192 |
|
*/ |
193 |
|
public void testNextLongBadBounds() { |
194 |
|
try { |
199 |
|
|
200 |
|
/** |
201 |
|
* nextLong(bound) returns 0 <= value < bound; |
202 |
< |
* repeated calls produce at least one different result |
202 |
> |
* repeated calls produce at least two distinct results |
203 |
|
*/ |
204 |
|
public void testNextLongBounded() { |
205 |
|
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
218 |
|
|
219 |
|
/** |
220 |
|
* nextLong(least, bound) returns least <= value < bound; |
221 |
< |
* repeated calls produce at least one different result |
221 |
> |
* repeated calls produce at least two distinct results |
222 |
|
*/ |
223 |
|
public void testNextLongBounded2() { |
224 |
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { |
239 |
|
|
240 |
|
/** |
241 |
|
* nextDouble(least, bound) returns least <= value < bound; |
242 |
< |
* repeated calls produce at least one different result |
242 |
> |
* repeated calls produce at least two distinct results |
243 |
|
*/ |
244 |
|
public void testNextDoubleBounded2() { |
245 |
|
for (double least = 0.0001; least < 1.0e20; least *= 8) { |
275 |
|
public void realRun() { |
276 |
|
ThreadLocalRandom current = ThreadLocalRandom.current(); |
277 |
|
assertSame(current, ThreadLocalRandom.current()); |
278 |
< |
assertNotSame(current, threadLocalRandom.get()); |
278 |
> |
// test bug: the following is not guaranteed and not true in JDK8 |
279 |
> |
// assertNotSame(current, threadLocalRandom.get()); |
280 |
|
rand.set(current.nextLong()); |
281 |
|
threadLocalRandom.set(current); |
282 |
|
}}; |