1 |
dl |
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 |
jsr166 |
1.8 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
import junit.framework.*; |
7 |
|
|
import java.util.concurrent.*; |
8 |
|
|
import java.util.*; |
9 |
|
|
|
10 |
|
|
public class ThreadLocalRandomTest extends JSR166TestCase { |
11 |
|
|
|
12 |
|
|
public static void main(String[] args) { |
13 |
jsr166 |
1.7 |
junit.textui.TestRunner.run(suite()); |
14 |
dl |
1.1 |
} |
15 |
|
|
public static Test suite() { |
16 |
jsr166 |
1.4 |
return new TestSuite(ThreadLocalRandomTest.class); |
17 |
dl |
1.1 |
} |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* Testing coverage notes: |
21 |
jsr166 |
1.2 |
* |
22 |
dl |
1.1 |
* We don't test randomness properties, but only that repeated |
23 |
|
|
* calls, up to NCALLS tries, produce at least one different |
24 |
|
|
* result. For bounded versions, we sample various intervals |
25 |
|
|
* across multiples of primes. |
26 |
|
|
*/ |
27 |
|
|
|
28 |
jsr166 |
1.2 |
// |
29 |
dl |
1.1 |
static final int NCALLS = 10000; |
30 |
|
|
|
31 |
|
|
// max sampled int bound |
32 |
|
|
static final int MAX_INT_BOUND = (1 << 28); |
33 |
|
|
|
34 |
|
|
// Max sampled long bound |
35 |
|
|
static final long MAX_LONG_BOUND = (1L << 42); |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* setSeed throws UnsupportedOperationException |
39 |
|
|
*/ |
40 |
|
|
public void testSetSeed() { |
41 |
|
|
try { |
42 |
|
|
ThreadLocalRandom.current().setSeed(17); |
43 |
jsr166 |
1.3 |
shouldThrow(); |
44 |
jsr166 |
1.5 |
} catch (UnsupportedOperationException success) {} |
45 |
dl |
1.1 |
} |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* Repeated calls to nextInt produce at least one different result |
49 |
|
|
*/ |
50 |
|
|
public void testNextInt() { |
51 |
|
|
int f = ThreadLocalRandom.current().nextInt(); |
52 |
|
|
int i = 0; |
53 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f) |
54 |
|
|
++i; |
55 |
|
|
assertTrue(i < NCALLS); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
/** |
59 |
|
|
* Repeated calls to nextLong produce at least one different result |
60 |
|
|
*/ |
61 |
|
|
public void testNextLong() { |
62 |
|
|
long f = ThreadLocalRandom.current().nextLong(); |
63 |
|
|
int i = 0; |
64 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f) |
65 |
|
|
++i; |
66 |
|
|
assertTrue(i < NCALLS); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* Repeated calls to nextBoolean produce at least one different result |
71 |
|
|
*/ |
72 |
|
|
public void testNextBoolean() { |
73 |
|
|
boolean f = ThreadLocalRandom.current().nextBoolean(); |
74 |
|
|
int i = 0; |
75 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f) |
76 |
|
|
++i; |
77 |
|
|
assertTrue(i < NCALLS); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* Repeated calls to nextFloat produce at least one different result |
82 |
|
|
*/ |
83 |
|
|
public void testNextFloat() { |
84 |
|
|
float f = ThreadLocalRandom.current().nextFloat(); |
85 |
|
|
int i = 0; |
86 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f) |
87 |
|
|
++i; |
88 |
|
|
assertTrue(i < NCALLS); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
/** |
92 |
|
|
* Repeated calls to nextDouble produce at least one different result |
93 |
|
|
*/ |
94 |
|
|
public void testNextDouble() { |
95 |
|
|
double f = ThreadLocalRandom.current().nextDouble(); |
96 |
|
|
double i = 0; |
97 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f) |
98 |
|
|
++i; |
99 |
|
|
assertTrue(i < NCALLS); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* Repeated calls to nextGaussian produce at least one different result |
104 |
|
|
*/ |
105 |
|
|
public void testNextGaussian() { |
106 |
|
|
double f = ThreadLocalRandom.current().nextGaussian(); |
107 |
|
|
int i = 0; |
108 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f) |
109 |
|
|
++i; |
110 |
|
|
assertTrue(i < NCALLS); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* nextInt(negative) throws IllegalArgumentException; |
115 |
|
|
*/ |
116 |
|
|
public void testNextIntBoundedNeg() { |
117 |
|
|
try { |
118 |
jsr166 |
1.6 |
int f = ThreadLocalRandom.current().nextInt(-17); |
119 |
jsr166 |
1.3 |
shouldThrow(); |
120 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
121 |
dl |
1.1 |
} |
122 |
|
|
|
123 |
|
|
/** |
124 |
|
|
* nextInt(least >= bound) throws IllegalArgumentException; |
125 |
|
|
*/ |
126 |
|
|
public void testNextIntBadBounds() { |
127 |
|
|
try { |
128 |
jsr166 |
1.6 |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
129 |
jsr166 |
1.3 |
shouldThrow(); |
130 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
131 |
dl |
1.1 |
} |
132 |
|
|
|
133 |
|
|
/** |
134 |
|
|
* nextInt(bound) returns 0 <= value < bound; |
135 |
|
|
* repeated calls produce at least one different result |
136 |
|
|
*/ |
137 |
|
|
public void testNextIntBounded() { |
138 |
|
|
// sample bound space across prime number increments |
139 |
|
|
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { |
140 |
|
|
int f = ThreadLocalRandom.current().nextInt(bound); |
141 |
|
|
assertTrue(0 <= f && f < bound); |
142 |
|
|
int i = 0; |
143 |
|
|
int j; |
144 |
jsr166 |
1.2 |
while (i < NCALLS && |
145 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextInt(bound)) == f) { |
146 |
|
|
assertTrue(0 <= j && j < bound); |
147 |
|
|
++i; |
148 |
|
|
} |
149 |
|
|
assertTrue(i < NCALLS); |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
/** |
154 |
|
|
* nextInt(least, bound) returns least <= value < bound; |
155 |
|
|
* repeated calls produce at least one different result |
156 |
|
|
*/ |
157 |
|
|
public void testNextIntBounded2() { |
158 |
|
|
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { |
159 |
|
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) { |
160 |
|
|
int f = ThreadLocalRandom.current().nextInt(least, bound); |
161 |
|
|
assertTrue(least <= f && f < bound); |
162 |
|
|
int i = 0; |
163 |
|
|
int j; |
164 |
jsr166 |
1.2 |
while (i < NCALLS && |
165 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextInt(least, bound)) == f) { |
166 |
|
|
assertTrue(least <= j && j < bound); |
167 |
|
|
++i; |
168 |
|
|
} |
169 |
|
|
assertTrue(i < NCALLS); |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
/** |
175 |
|
|
* nextLong(negative) throws IllegalArgumentException; |
176 |
|
|
*/ |
177 |
|
|
public void testNextLongBoundedNeg() { |
178 |
|
|
try { |
179 |
jsr166 |
1.6 |
long f = ThreadLocalRandom.current().nextLong(-17); |
180 |
jsr166 |
1.3 |
shouldThrow(); |
181 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
182 |
dl |
1.1 |
} |
183 |
|
|
|
184 |
|
|
/** |
185 |
|
|
* nextLong(least >= bound) throws IllegalArgumentException; |
186 |
|
|
*/ |
187 |
|
|
public void testNextLongBadBounds() { |
188 |
|
|
try { |
189 |
jsr166 |
1.6 |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
190 |
jsr166 |
1.3 |
shouldThrow(); |
191 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
192 |
dl |
1.1 |
} |
193 |
|
|
|
194 |
|
|
/** |
195 |
|
|
* nextLong(bound) returns 0 <= value < bound; |
196 |
|
|
* repeated calls produce at least one different result |
197 |
|
|
*/ |
198 |
|
|
public void testNextLongBounded() { |
199 |
|
|
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
200 |
|
|
long f = ThreadLocalRandom.current().nextLong(bound); |
201 |
|
|
assertTrue(0 <= f && f < bound); |
202 |
|
|
int i = 0; |
203 |
|
|
long j; |
204 |
jsr166 |
1.2 |
while (i < NCALLS && |
205 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextLong(bound)) == f) { |
206 |
|
|
assertTrue(0 <= j && j < bound); |
207 |
|
|
++i; |
208 |
|
|
} |
209 |
|
|
assertTrue(i < NCALLS); |
210 |
|
|
} |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
/** |
214 |
|
|
* nextLong(least, bound) returns least <= value < bound; |
215 |
|
|
* repeated calls produce at least one different result |
216 |
|
|
*/ |
217 |
|
|
public void testNextLongBounded2() { |
218 |
|
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { |
219 |
|
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
220 |
|
|
long f = ThreadLocalRandom.current().nextLong(least, bound); |
221 |
|
|
assertTrue(least <= f && f < bound); |
222 |
|
|
int i = 0; |
223 |
|
|
long j; |
224 |
jsr166 |
1.2 |
while (i < NCALLS && |
225 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextLong(least, bound)) == f) { |
226 |
|
|
assertTrue(least <= j && j < bound); |
227 |
|
|
++i; |
228 |
|
|
} |
229 |
|
|
assertTrue(i < NCALLS); |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
/** |
235 |
|
|
* nextDouble(least, bound) returns least <= value < bound; |
236 |
|
|
* repeated calls produce at least one different result |
237 |
|
|
*/ |
238 |
|
|
public void testNextDoubleBounded2() { |
239 |
|
|
for (double least = 0.0001; least < 1.0e20; least *= 8) { |
240 |
|
|
for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) { |
241 |
|
|
double f = ThreadLocalRandom.current().nextDouble(least, bound); |
242 |
|
|
assertTrue(least <= f && f < bound); |
243 |
|
|
int i = 0; |
244 |
|
|
double j; |
245 |
jsr166 |
1.2 |
while (i < NCALLS && |
246 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) { |
247 |
|
|
assertTrue(least <= j && j < bound); |
248 |
|
|
++i; |
249 |
|
|
} |
250 |
|
|
assertTrue(i < NCALLS); |
251 |
|
|
} |
252 |
|
|
} |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
} |