ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandomTest.java
Revision: 1.19
Committed: Fri Sep 27 20:22:26 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +62 -20 lines
Log Message:
more "bound" tests

File Contents

# User Rev Content
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.*;
8 jsr166 1.10 import java.util.concurrent.ThreadLocalRandom;
9 jsr166 1.11 import java.util.concurrent.atomic.AtomicLong;
10     import java.util.concurrent.atomic.AtomicReference;
11 dl 1.1
12     public class ThreadLocalRandomTest extends JSR166TestCase {
13    
14     public static void main(String[] args) {
15 jsr166 1.7 junit.textui.TestRunner.run(suite());
16 dl 1.1 }
17     public static Test suite() {
18 jsr166 1.4 return new TestSuite(ThreadLocalRandomTest.class);
19 dl 1.1 }
20    
21 jsr166 1.13 /*
22 dl 1.1 * Testing coverage notes:
23 jsr166 1.2 *
24 dl 1.1 * We don't test randomness properties, but only that repeated
25     * calls, up to NCALLS tries, produce at least one different
26     * result. For bounded versions, we sample various intervals
27     * across multiples of primes.
28     */
29    
30 jsr166 1.14 // max numbers of calls to detect getting stuck on one value
31 dl 1.1 static final int NCALLS = 10000;
32    
33     // max sampled int bound
34     static final int MAX_INT_BOUND = (1 << 28);
35    
36 jsr166 1.14 // max sampled long bound
37 dl 1.1 static final long MAX_LONG_BOUND = (1L << 42);
38    
39 dl 1.15 // Number of replications for other checks
40     static final int REPS = 20;
41    
42 dl 1.1 /**
43     * setSeed throws UnsupportedOperationException
44     */
45     public void testSetSeed() {
46     try {
47     ThreadLocalRandom.current().setSeed(17);
48 jsr166 1.3 shouldThrow();
49 jsr166 1.5 } catch (UnsupportedOperationException success) {}
50 dl 1.1 }
51    
52     /**
53 jsr166 1.14 * Repeated calls to nextInt produce at least two distinct results
54 dl 1.1 */
55     public void testNextInt() {
56     int f = ThreadLocalRandom.current().nextInt();
57     int i = 0;
58     while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f)
59     ++i;
60     assertTrue(i < NCALLS);
61     }
62    
63     /**
64 jsr166 1.14 * Repeated calls to nextLong produce at least two distinct results
65 dl 1.1 */
66     public void testNextLong() {
67     long f = ThreadLocalRandom.current().nextLong();
68     int i = 0;
69     while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f)
70     ++i;
71     assertTrue(i < NCALLS);
72     }
73    
74     /**
75 jsr166 1.14 * Repeated calls to nextBoolean produce at least two distinct results
76 dl 1.1 */
77     public void testNextBoolean() {
78     boolean f = ThreadLocalRandom.current().nextBoolean();
79     int i = 0;
80     while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f)
81     ++i;
82     assertTrue(i < NCALLS);
83     }
84    
85     /**
86 jsr166 1.14 * Repeated calls to nextFloat produce at least two distinct results
87 dl 1.1 */
88     public void testNextFloat() {
89     float f = ThreadLocalRandom.current().nextFloat();
90     int i = 0;
91     while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f)
92     ++i;
93     assertTrue(i < NCALLS);
94     }
95    
96     /**
97 jsr166 1.14 * Repeated calls to nextDouble produce at least two distinct results
98 dl 1.1 */
99     public void testNextDouble() {
100     double f = ThreadLocalRandom.current().nextDouble();
101 jsr166 1.14 int i = 0;
102 dl 1.1 while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
103     ++i;
104     assertTrue(i < NCALLS);
105     }
106    
107     /**
108 jsr166 1.14 * Repeated calls to nextGaussian produce at least two distinct results
109 dl 1.1 */
110     public void testNextGaussian() {
111     double f = ThreadLocalRandom.current().nextGaussian();
112     int i = 0;
113     while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f)
114     ++i;
115     assertTrue(i < NCALLS);
116     }
117    
118     /**
119 jsr166 1.19 * nextInt(non-positive) throws IllegalArgumentException
120 dl 1.1 */
121 jsr166 1.19 public void testNextIntBoundNonPositive() {
122     ThreadLocalRandom rnd = ThreadLocalRandom.current();
123     for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) {
124     try {
125     rnd.nextInt(bound);
126     shouldThrow();
127     } catch (IllegalArgumentException success) {}
128     }
129 dl 1.1 }
130    
131     /**
132 jsr166 1.14 * nextInt(least >= bound) throws IllegalArgumentException
133 dl 1.1 */
134     public void testNextIntBadBounds() {
135 jsr166 1.19 int[][] badBoundss = {
136     { 17, 2 },
137     { -42, -42 },
138     { Integer.MAX_VALUE, Integer.MIN_VALUE },
139     };
140     ThreadLocalRandom rnd = ThreadLocalRandom.current();
141     for (int[] badBounds : badBoundss) {
142     try {
143     rnd.nextInt(badBounds[0], badBounds[1]);
144     shouldThrow();
145     } catch (IllegalArgumentException success) {}
146     }
147 dl 1.1 }
148    
149     /**
150     * nextInt(bound) returns 0 <= value < bound;
151 jsr166 1.14 * repeated calls produce at least two distinct results
152 dl 1.1 */
153     public void testNextIntBounded() {
154     // sample bound space across prime number increments
155     for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
156     int f = ThreadLocalRandom.current().nextInt(bound);
157     assertTrue(0 <= f && f < bound);
158     int i = 0;
159     int j;
160 jsr166 1.2 while (i < NCALLS &&
161 dl 1.1 (j = ThreadLocalRandom.current().nextInt(bound)) == f) {
162     assertTrue(0 <= j && j < bound);
163     ++i;
164     }
165     assertTrue(i < NCALLS);
166     }
167     }
168    
169     /**
170     * nextInt(least, bound) returns least <= value < bound;
171 jsr166 1.14 * repeated calls produce at least two distinct results
172 dl 1.1 */
173     public void testNextIntBounded2() {
174     for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
175     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
176     int f = ThreadLocalRandom.current().nextInt(least, bound);
177     assertTrue(least <= f && f < bound);
178     int i = 0;
179     int j;
180 jsr166 1.2 while (i < NCALLS &&
181 dl 1.1 (j = ThreadLocalRandom.current().nextInt(least, bound)) == f) {
182     assertTrue(least <= j && j < bound);
183     ++i;
184     }
185     assertTrue(i < NCALLS);
186     }
187     }
188     }
189    
190     /**
191 jsr166 1.19 * nextLong(non-positive) throws IllegalArgumentException
192 dl 1.1 */
193 jsr166 1.19 public void testNextLongBoundNonPositive() {
194     ThreadLocalRandom rnd = ThreadLocalRandom.current();
195     for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) {
196     try {
197     rnd.nextLong(bound);
198     shouldThrow();
199     } catch (IllegalArgumentException success) {}
200     }
201 dl 1.1 }
202    
203     /**
204 jsr166 1.14 * nextLong(least >= bound) throws IllegalArgumentException
205 dl 1.1 */
206     public void testNextLongBadBounds() {
207 jsr166 1.19 long[][] badBoundss = {
208     { 17L, 2L },
209     { -42L, -42L },
210     { Long.MAX_VALUE, Long.MIN_VALUE },
211     };
212     ThreadLocalRandom rnd = ThreadLocalRandom.current();
213     for (long[] badBounds : badBoundss) {
214     try {
215     rnd.nextLong(badBounds[0], badBounds[1]);
216     shouldThrow();
217     } catch (IllegalArgumentException success) {}
218     }
219 dl 1.1 }
220    
221     /**
222     * nextLong(bound) returns 0 <= value < bound;
223 jsr166 1.14 * repeated calls produce at least two distinct results
224 dl 1.1 */
225     public void testNextLongBounded() {
226     for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
227     long f = ThreadLocalRandom.current().nextLong(bound);
228     assertTrue(0 <= f && f < bound);
229     int i = 0;
230     long j;
231 jsr166 1.2 while (i < NCALLS &&
232 dl 1.1 (j = ThreadLocalRandom.current().nextLong(bound)) == f) {
233     assertTrue(0 <= j && j < bound);
234     ++i;
235     }
236     assertTrue(i < NCALLS);
237     }
238     }
239    
240     /**
241     * nextLong(least, bound) returns least <= value < bound;
242 jsr166 1.14 * repeated calls produce at least two distinct results
243 dl 1.1 */
244     public void testNextLongBounded2() {
245     for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
246     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
247     long f = ThreadLocalRandom.current().nextLong(least, bound);
248     assertTrue(least <= f && f < bound);
249     int i = 0;
250     long j;
251 jsr166 1.2 while (i < NCALLS &&
252 dl 1.1 (j = ThreadLocalRandom.current().nextLong(least, bound)) == f) {
253     assertTrue(least <= j && j < bound);
254     ++i;
255     }
256     assertTrue(i < NCALLS);
257     }
258     }
259     }
260    
261     /**
262 jsr166 1.19 * nextDouble(non-positive) throws IllegalArgumentException
263     */
264     public void testNextDoubleBoundNonPositive() {
265     ThreadLocalRandom rnd = ThreadLocalRandom.current();
266     double[] badBounds = {
267     0.0d,
268     -17.0d,
269     -Double.MIN_VALUE,
270     Double.NEGATIVE_INFINITY,
271     Double.NaN,
272     };
273     for (double bound : badBounds) {
274     try {
275     rnd.nextDouble(bound);
276     shouldThrow();
277     } catch (IllegalArgumentException success) {}
278     }
279     }
280    
281     /**
282 dl 1.1 * nextDouble(least, bound) returns least <= value < bound;
283 jsr166 1.14 * repeated calls produce at least two distinct results
284 dl 1.1 */
285     public void testNextDoubleBounded2() {
286     for (double least = 0.0001; least < 1.0e20; least *= 8) {
287     for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
288     double f = ThreadLocalRandom.current().nextDouble(least, bound);
289     assertTrue(least <= f && f < bound);
290     int i = 0;
291     double j;
292 jsr166 1.2 while (i < NCALLS &&
293 dl 1.1 (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
294     assertTrue(least <= j && j < bound);
295     ++i;
296     }
297     assertTrue(i < NCALLS);
298     }
299     }
300     }
301    
302 jsr166 1.11 /**
303     * Different threads produce different pseudo-random sequences
304     */
305     public void testDifferentSequences() {
306     // Don't use main thread's ThreadLocalRandom - it is likely to
307     // be polluted by previous tests.
308     final AtomicReference<ThreadLocalRandom> threadLocalRandom =
309     new AtomicReference<ThreadLocalRandom>();
310     final AtomicLong rand = new AtomicLong();
311    
312     long firstRand = 0;
313     ThreadLocalRandom firstThreadLocalRandom = null;
314    
315 jsr166 1.18 Runnable getRandomState = new CheckedRunnable() {
316 jsr166 1.11 public void realRun() {
317     ThreadLocalRandom current = ThreadLocalRandom.current();
318     assertSame(current, ThreadLocalRandom.current());
319 dl 1.12 // test bug: the following is not guaranteed and not true in JDK8
320     // assertNotSame(current, threadLocalRandom.get());
321 jsr166 1.11 rand.set(current.nextLong());
322     threadLocalRandom.set(current);
323     }};
324    
325     Thread first = newStartedThread(getRandomState);
326     awaitTermination(first);
327     firstRand = rand.get();
328     firstThreadLocalRandom = threadLocalRandom.get();
329    
330     for (int i = 0; i < NCALLS; i++) {
331     Thread t = newStartedThread(getRandomState);
332     awaitTermination(t);
333     if (firstRand != rand.get())
334     return;
335     }
336     fail("all threads generate the same pseudo-random sequence");
337     }
338    
339 dl 1.1 }