ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandomTest.java
Revision: 1.17
Committed: Fri Aug 16 07:10:20 2013 UTC (10 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.16: +0 -1 lines
Log Message:
remove unused imports

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.14 * nextInt(negative) throws IllegalArgumentException
120 dl 1.1 */
121     public void testNextIntBoundedNeg() {
122     try {
123 jsr166 1.6 int f = ThreadLocalRandom.current().nextInt(-17);
124 jsr166 1.3 shouldThrow();
125 jsr166 1.5 } catch (IllegalArgumentException success) {}
126 dl 1.1 }
127    
128     /**
129 jsr166 1.14 * nextInt(least >= bound) throws IllegalArgumentException
130 dl 1.1 */
131     public void testNextIntBadBounds() {
132     try {
133 jsr166 1.6 int f = ThreadLocalRandom.current().nextInt(17, 2);
134 jsr166 1.3 shouldThrow();
135 jsr166 1.5 } catch (IllegalArgumentException success) {}
136 dl 1.1 }
137    
138     /**
139     * nextInt(bound) returns 0 <= value < bound;
140 jsr166 1.14 * repeated calls produce at least two distinct results
141 dl 1.1 */
142     public void testNextIntBounded() {
143     // sample bound space across prime number increments
144     for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
145     int f = ThreadLocalRandom.current().nextInt(bound);
146     assertTrue(0 <= f && f < bound);
147     int i = 0;
148     int j;
149 jsr166 1.2 while (i < NCALLS &&
150 dl 1.1 (j = ThreadLocalRandom.current().nextInt(bound)) == f) {
151     assertTrue(0 <= j && j < bound);
152     ++i;
153     }
154     assertTrue(i < NCALLS);
155     }
156     }
157    
158     /**
159     * nextInt(least, bound) returns least <= value < bound;
160 jsr166 1.14 * repeated calls produce at least two distinct results
161 dl 1.1 */
162     public void testNextIntBounded2() {
163     for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
164     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
165     int f = ThreadLocalRandom.current().nextInt(least, bound);
166     assertTrue(least <= f && f < bound);
167     int i = 0;
168     int j;
169 jsr166 1.2 while (i < NCALLS &&
170 dl 1.1 (j = ThreadLocalRandom.current().nextInt(least, bound)) == f) {
171     assertTrue(least <= j && j < bound);
172     ++i;
173     }
174     assertTrue(i < NCALLS);
175     }
176     }
177     }
178    
179     /**
180 jsr166 1.14 * nextLong(negative) throws IllegalArgumentException
181 dl 1.1 */
182     public void testNextLongBoundedNeg() {
183     try {
184 jsr166 1.6 long f = ThreadLocalRandom.current().nextLong(-17);
185 jsr166 1.3 shouldThrow();
186 jsr166 1.5 } catch (IllegalArgumentException success) {}
187 dl 1.1 }
188    
189     /**
190 jsr166 1.14 * nextLong(least >= bound) throws IllegalArgumentException
191 dl 1.1 */
192     public void testNextLongBadBounds() {
193     try {
194 jsr166 1.6 long f = ThreadLocalRandom.current().nextLong(17, 2);
195 jsr166 1.3 shouldThrow();
196 jsr166 1.5 } catch (IllegalArgumentException success) {}
197 dl 1.1 }
198    
199     /**
200     * nextLong(bound) returns 0 <= value < bound;
201 jsr166 1.14 * repeated calls produce at least two distinct results
202 dl 1.1 */
203     public void testNextLongBounded() {
204     for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
205     long f = ThreadLocalRandom.current().nextLong(bound);
206     assertTrue(0 <= f && f < bound);
207     int i = 0;
208     long j;
209 jsr166 1.2 while (i < NCALLS &&
210 dl 1.1 (j = ThreadLocalRandom.current().nextLong(bound)) == f) {
211     assertTrue(0 <= j && j < bound);
212     ++i;
213     }
214     assertTrue(i < NCALLS);
215     }
216     }
217    
218     /**
219     * nextLong(least, bound) returns least <= value < bound;
220 jsr166 1.14 * repeated calls produce at least two distinct results
221 dl 1.1 */
222     public void testNextLongBounded2() {
223     for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
224     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
225     long f = ThreadLocalRandom.current().nextLong(least, bound);
226     assertTrue(least <= f && f < bound);
227     int i = 0;
228     long j;
229 jsr166 1.2 while (i < NCALLS &&
230 dl 1.1 (j = ThreadLocalRandom.current().nextLong(least, bound)) == f) {
231     assertTrue(least <= j && j < bound);
232     ++i;
233     }
234     assertTrue(i < NCALLS);
235     }
236     }
237     }
238    
239     /**
240     * nextDouble(least, bound) returns least <= value < bound;
241 jsr166 1.14 * repeated calls produce at least two distinct results
242 dl 1.1 */
243     public void testNextDoubleBounded2() {
244     for (double least = 0.0001; least < 1.0e20; least *= 8) {
245     for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
246     double f = ThreadLocalRandom.current().nextDouble(least, bound);
247     assertTrue(least <= f && f < bound);
248     int i = 0;
249     double j;
250 jsr166 1.2 while (i < NCALLS &&
251 dl 1.1 (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
252     assertTrue(least <= j && j < bound);
253     ++i;
254     }
255     assertTrue(i < NCALLS);
256     }
257     }
258     }
259    
260 jsr166 1.11 /**
261     * Different threads produce different pseudo-random sequences
262     */
263     public void testDifferentSequences() {
264     // Don't use main thread's ThreadLocalRandom - it is likely to
265     // be polluted by previous tests.
266     final AtomicReference<ThreadLocalRandom> threadLocalRandom =
267     new AtomicReference<ThreadLocalRandom>();
268     final AtomicLong rand = new AtomicLong();
269    
270     long firstRand = 0;
271     ThreadLocalRandom firstThreadLocalRandom = null;
272    
273     final CheckedRunnable getRandomState = new CheckedRunnable() {
274     public void realRun() {
275     ThreadLocalRandom current = ThreadLocalRandom.current();
276     assertSame(current, ThreadLocalRandom.current());
277 dl 1.12 // test bug: the following is not guaranteed and not true in JDK8
278     // assertNotSame(current, threadLocalRandom.get());
279 jsr166 1.11 rand.set(current.nextLong());
280     threadLocalRandom.set(current);
281     }};
282    
283     Thread first = newStartedThread(getRandomState);
284     awaitTermination(first);
285     firstRand = rand.get();
286     firstThreadLocalRandom = threadLocalRandom.get();
287    
288     for (int i = 0; i < NCALLS; i++) {
289     Thread t = newStartedThread(getRandomState);
290     awaitTermination(t);
291     if (firstRand != rand.get())
292     return;
293     }
294     fail("all threads generate the same pseudo-random sequence");
295     }
296    
297 dl 1.1 }