ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandomTest.java
Revision: 1.14
Committed: Sun Jul 14 16:55:01 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +18 -18 lines
Log Message:
actually test SplittableRandom; tidying pass; coding style consistency

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     /**
40     * setSeed throws UnsupportedOperationException
41     */
42     public void testSetSeed() {
43     try {
44     ThreadLocalRandom.current().setSeed(17);
45 jsr166 1.3 shouldThrow();
46 jsr166 1.5 } catch (UnsupportedOperationException success) {}
47 dl 1.1 }
48    
49     /**
50 jsr166 1.14 * Repeated calls to nextInt produce at least two distinct results
51 dl 1.1 */
52     public void testNextInt() {
53     int f = ThreadLocalRandom.current().nextInt();
54     int i = 0;
55     while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f)
56     ++i;
57     assertTrue(i < NCALLS);
58     }
59    
60     /**
61 jsr166 1.14 * Repeated calls to nextLong produce at least two distinct results
62 dl 1.1 */
63     public void testNextLong() {
64     long f = ThreadLocalRandom.current().nextLong();
65     int i = 0;
66     while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f)
67     ++i;
68     assertTrue(i < NCALLS);
69     }
70    
71     /**
72 jsr166 1.14 * Repeated calls to nextBoolean produce at least two distinct results
73 dl 1.1 */
74     public void testNextBoolean() {
75     boolean f = ThreadLocalRandom.current().nextBoolean();
76     int i = 0;
77     while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f)
78     ++i;
79     assertTrue(i < NCALLS);
80     }
81    
82     /**
83 jsr166 1.14 * Repeated calls to nextFloat produce at least two distinct results
84 dl 1.1 */
85     public void testNextFloat() {
86     float f = ThreadLocalRandom.current().nextFloat();
87     int i = 0;
88     while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f)
89     ++i;
90     assertTrue(i < NCALLS);
91     }
92    
93     /**
94 jsr166 1.14 * Repeated calls to nextDouble produce at least two distinct results
95 dl 1.1 */
96     public void testNextDouble() {
97     double f = ThreadLocalRandom.current().nextDouble();
98 jsr166 1.14 int i = 0;
99 dl 1.1 while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
100     ++i;
101     assertTrue(i < NCALLS);
102     }
103    
104     /**
105 jsr166 1.14 * Repeated calls to nextGaussian produce at least two distinct results
106 dl 1.1 */
107     public void testNextGaussian() {
108     double f = ThreadLocalRandom.current().nextGaussian();
109     int i = 0;
110     while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f)
111     ++i;
112     assertTrue(i < NCALLS);
113     }
114    
115     /**
116 jsr166 1.14 * nextInt(negative) throws IllegalArgumentException
117 dl 1.1 */
118     public void testNextIntBoundedNeg() {
119     try {
120 jsr166 1.6 int f = ThreadLocalRandom.current().nextInt(-17);
121 jsr166 1.3 shouldThrow();
122 jsr166 1.5 } catch (IllegalArgumentException success) {}
123 dl 1.1 }
124    
125     /**
126 jsr166 1.14 * nextInt(least >= bound) throws IllegalArgumentException
127 dl 1.1 */
128     public void testNextIntBadBounds() {
129     try {
130 jsr166 1.6 int f = ThreadLocalRandom.current().nextInt(17, 2);
131 jsr166 1.3 shouldThrow();
132 jsr166 1.5 } catch (IllegalArgumentException success) {}
133 dl 1.1 }
134    
135     /**
136     * nextInt(bound) returns 0 <= value < bound;
137 jsr166 1.14 * repeated calls produce at least two distinct results
138 dl 1.1 */
139     public void testNextIntBounded() {
140     // sample bound space across prime number increments
141     for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
142     int f = ThreadLocalRandom.current().nextInt(bound);
143     assertTrue(0 <= f && f < bound);
144     int i = 0;
145     int j;
146 jsr166 1.2 while (i < NCALLS &&
147 dl 1.1 (j = ThreadLocalRandom.current().nextInt(bound)) == f) {
148     assertTrue(0 <= j && j < bound);
149     ++i;
150     }
151     assertTrue(i < NCALLS);
152     }
153     }
154    
155     /**
156     * nextInt(least, bound) returns least <= value < bound;
157 jsr166 1.14 * repeated calls produce at least two distinct results
158 dl 1.1 */
159     public void testNextIntBounded2() {
160     for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
161     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
162     int f = ThreadLocalRandom.current().nextInt(least, bound);
163     assertTrue(least <= f && f < bound);
164     int i = 0;
165     int j;
166 jsr166 1.2 while (i < NCALLS &&
167 dl 1.1 (j = ThreadLocalRandom.current().nextInt(least, bound)) == f) {
168     assertTrue(least <= j && j < bound);
169     ++i;
170     }
171     assertTrue(i < NCALLS);
172     }
173     }
174     }
175    
176     /**
177 jsr166 1.14 * nextLong(negative) throws IllegalArgumentException
178 dl 1.1 */
179     public void testNextLongBoundedNeg() {
180     try {
181 jsr166 1.6 long f = ThreadLocalRandom.current().nextLong(-17);
182 jsr166 1.3 shouldThrow();
183 jsr166 1.5 } catch (IllegalArgumentException success) {}
184 dl 1.1 }
185    
186     /**
187 jsr166 1.14 * nextLong(least >= bound) throws IllegalArgumentException
188 dl 1.1 */
189     public void testNextLongBadBounds() {
190     try {
191 jsr166 1.6 long f = ThreadLocalRandom.current().nextLong(17, 2);
192 jsr166 1.3 shouldThrow();
193 jsr166 1.5 } catch (IllegalArgumentException success) {}
194 dl 1.1 }
195    
196     /**
197     * nextLong(bound) returns 0 <= value < bound;
198 jsr166 1.14 * repeated calls produce at least two distinct results
199 dl 1.1 */
200     public void testNextLongBounded() {
201     for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
202     long f = ThreadLocalRandom.current().nextLong(bound);
203     assertTrue(0 <= f && f < bound);
204     int i = 0;
205     long j;
206 jsr166 1.2 while (i < NCALLS &&
207 dl 1.1 (j = ThreadLocalRandom.current().nextLong(bound)) == f) {
208     assertTrue(0 <= j && j < bound);
209     ++i;
210     }
211     assertTrue(i < NCALLS);
212     }
213     }
214    
215     /**
216     * nextLong(least, bound) returns least <= value < bound;
217 jsr166 1.14 * repeated calls produce at least two distinct results
218 dl 1.1 */
219     public void testNextLongBounded2() {
220     for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
221     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
222     long f = ThreadLocalRandom.current().nextLong(least, bound);
223     assertTrue(least <= f && f < bound);
224     int i = 0;
225     long j;
226 jsr166 1.2 while (i < NCALLS &&
227 dl 1.1 (j = ThreadLocalRandom.current().nextLong(least, bound)) == f) {
228     assertTrue(least <= j && j < bound);
229     ++i;
230     }
231     assertTrue(i < NCALLS);
232     }
233     }
234     }
235    
236     /**
237     * nextDouble(least, bound) returns least <= value < bound;
238 jsr166 1.14 * repeated calls produce at least two distinct results
239 dl 1.1 */
240     public void testNextDoubleBounded2() {
241     for (double least = 0.0001; least < 1.0e20; least *= 8) {
242     for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
243     double f = ThreadLocalRandom.current().nextDouble(least, bound);
244     assertTrue(least <= f && f < bound);
245     int i = 0;
246     double j;
247 jsr166 1.2 while (i < NCALLS &&
248 dl 1.1 (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
249     assertTrue(least <= j && j < bound);
250     ++i;
251     }
252     assertTrue(i < NCALLS);
253     }
254     }
255     }
256    
257 jsr166 1.11 /**
258     * Different threads produce different pseudo-random sequences
259     */
260     public void testDifferentSequences() {
261     // Don't use main thread's ThreadLocalRandom - it is likely to
262     // be polluted by previous tests.
263     final AtomicReference<ThreadLocalRandom> threadLocalRandom =
264     new AtomicReference<ThreadLocalRandom>();
265     final AtomicLong rand = new AtomicLong();
266    
267     long firstRand = 0;
268     ThreadLocalRandom firstThreadLocalRandom = null;
269    
270     final CheckedRunnable getRandomState = new CheckedRunnable() {
271     public void realRun() {
272     ThreadLocalRandom current = ThreadLocalRandom.current();
273     assertSame(current, ThreadLocalRandom.current());
274 dl 1.12 // test bug: the following is not guaranteed and not true in JDK8
275     // assertNotSame(current, threadLocalRandom.get());
276 jsr166 1.11 rand.set(current.nextLong());
277     threadLocalRandom.set(current);
278     }};
279    
280     Thread first = newStartedThread(getRandomState);
281     awaitTermination(first);
282     firstRand = rand.get();
283     firstThreadLocalRandom = threadLocalRandom.get();
284    
285     for (int i = 0; i < NCALLS; i++) {
286     Thread t = newStartedThread(getRandomState);
287     awaitTermination(t);
288     if (firstRand != rand.get())
289     return;
290     }
291     fail("all threads generate the same pseudo-random sequence");
292     }
293    
294 dl 1.1 }