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.*; |
6 |
> |
|
7 |
|
import java.util.concurrent.ThreadLocalRandom; |
8 |
|
import java.util.concurrent.atomic.AtomicLong; |
9 |
|
import java.util.concurrent.atomic.AtomicReference; |
10 |
|
|
11 |
+ |
import junit.framework.Test; |
12 |
+ |
import junit.framework.TestSuite; |
13 |
+ |
|
14 |
|
public class ThreadLocalRandomTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* nextInt(negative) throws IllegalArgumentException |
121 |
> |
* nextInt(non-positive) throws IllegalArgumentException |
122 |
|
*/ |
123 |
< |
public void testNextIntBoundedNeg() { |
124 |
< |
try { |
125 |
< |
int f = ThreadLocalRandom.current().nextInt(-17); |
126 |
< |
shouldThrow(); |
127 |
< |
} catch (IllegalArgumentException success) {} |
123 |
> |
public void testNextIntBoundNonPositive() { |
124 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
125 |
> |
for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) { |
126 |
> |
try { |
127 |
> |
rnd.nextInt(bound); |
128 |
> |
shouldThrow(); |
129 |
> |
} catch (IllegalArgumentException success) {} |
130 |
> |
} |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
|
* nextInt(least >= bound) throws IllegalArgumentException |
135 |
|
*/ |
136 |
|
public void testNextIntBadBounds() { |
137 |
< |
try { |
138 |
< |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
139 |
< |
shouldThrow(); |
140 |
< |
} catch (IllegalArgumentException success) {} |
137 |
> |
int[][] badBoundss = { |
138 |
> |
{ 17, 2 }, |
139 |
> |
{ -42, -42 }, |
140 |
> |
{ Integer.MAX_VALUE, Integer.MIN_VALUE }, |
141 |
> |
}; |
142 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
143 |
> |
for (int[] badBounds : badBoundss) { |
144 |
> |
try { |
145 |
> |
rnd.nextInt(badBounds[0], badBounds[1]); |
146 |
> |
shouldThrow(); |
147 |
> |
} catch (IllegalArgumentException success) {} |
148 |
> |
} |
149 |
|
} |
150 |
|
|
151 |
|
/** |
190 |
|
} |
191 |
|
|
192 |
|
/** |
193 |
< |
* nextLong(negative) throws IllegalArgumentException |
193 |
> |
* nextLong(non-positive) throws IllegalArgumentException |
194 |
|
*/ |
195 |
< |
public void testNextLongBoundedNeg() { |
196 |
< |
try { |
197 |
< |
long f = ThreadLocalRandom.current().nextLong(-17); |
198 |
< |
shouldThrow(); |
199 |
< |
} catch (IllegalArgumentException success) {} |
195 |
> |
public void testNextLongBoundNonPositive() { |
196 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
197 |
> |
for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) { |
198 |
> |
try { |
199 |
> |
rnd.nextLong(bound); |
200 |
> |
shouldThrow(); |
201 |
> |
} catch (IllegalArgumentException success) {} |
202 |
> |
} |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
|
* nextLong(least >= bound) throws IllegalArgumentException |
207 |
|
*/ |
208 |
|
public void testNextLongBadBounds() { |
209 |
< |
try { |
210 |
< |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
211 |
< |
shouldThrow(); |
212 |
< |
} catch (IllegalArgumentException success) {} |
209 |
> |
long[][] badBoundss = { |
210 |
> |
{ 17L, 2L }, |
211 |
> |
{ -42L, -42L }, |
212 |
> |
{ Long.MAX_VALUE, Long.MIN_VALUE }, |
213 |
> |
}; |
214 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
215 |
> |
for (long[] badBounds : badBoundss) { |
216 |
> |
try { |
217 |
> |
rnd.nextLong(badBounds[0], badBounds[1]); |
218 |
> |
shouldThrow(); |
219 |
> |
} catch (IllegalArgumentException success) {} |
220 |
> |
} |
221 |
|
} |
222 |
|
|
223 |
|
/** |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
+ |
/** |
264 |
+ |
* nextDouble(non-positive) throws IllegalArgumentException |
265 |
+ |
*/ |
266 |
+ |
public void testNextDoubleBoundNonPositive() { |
267 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
268 |
+ |
double[] badBounds = { |
269 |
+ |
0.0d, |
270 |
+ |
-17.0d, |
271 |
+ |
-Double.MIN_VALUE, |
272 |
+ |
Double.NEGATIVE_INFINITY, |
273 |
+ |
Double.NaN, |
274 |
+ |
}; |
275 |
+ |
for (double bound : badBounds) { |
276 |
+ |
try { |
277 |
+ |
rnd.nextDouble(bound); |
278 |
+ |
shouldThrow(); |
279 |
+ |
} catch (IllegalArgumentException success) {} |
280 |
+ |
} |
281 |
+ |
} |
282 |
+ |
|
283 |
|
/** |
284 |
|
* nextDouble(least, bound) returns least <= value < bound; |
285 |
|
* repeated calls produce at least two distinct results |