ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandomTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadLocalRandomTest.java (file contents):
Revision 1.7 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.16 by jsr166, Fri Aug 16 07:07:01 2013 UTC

# Line 1 | Line 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 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6   import junit.framework.*;
7 import java.util.concurrent.*;
7   import java.util.*;
8 <
8 > import java.util.concurrent.ThreadLocalRandom;
9 > import java.util.concurrent.atomic.AtomicLong;
10 > import java.util.concurrent.atomic.AtomicInteger;
11 > import java.util.concurrent.atomic.AtomicReference;
12  
13   public class ThreadLocalRandomTest extends JSR166TestCase {
14  
# Line 17 | Line 19 | public class ThreadLocalRandomTest exten
19          return new TestSuite(ThreadLocalRandomTest.class);
20      }
21  
22 <    /**
22 >    /*
23       * Testing coverage notes:
24       *
25       * We don't test randomness properties, but only that repeated
# Line 26 | Line 28 | public class ThreadLocalRandomTest exten
28       * across multiples of primes.
29       */
30  
31 <    //
31 >    // max numbers of calls to detect getting stuck on one value
32      static final int NCALLS = 10000;
33  
34      // max sampled int bound
35      static final int MAX_INT_BOUND = (1 << 28);
36  
37 <    // Max sampled long bound
37 >    // max sampled long bound
38      static final long MAX_LONG_BOUND = (1L << 42);
39  
40 +    // Number of replications for other checks
41 +    static final int REPS = 20;
42 +
43      /**
44       * setSeed throws UnsupportedOperationException
45       */
# Line 46 | Line 51 | public class ThreadLocalRandomTest exten
51      }
52  
53      /**
54 <     * Repeated calls to nextInt produce at least one different result
54 >     * Repeated calls to nextInt produce at least two distinct results
55       */
56      public void testNextInt() {
57          int f = ThreadLocalRandom.current().nextInt();
# Line 57 | Line 62 | public class ThreadLocalRandomTest exten
62      }
63  
64      /**
65 <     * Repeated calls to nextLong produce at least one different result
65 >     * Repeated calls to nextLong produce at least two distinct results
66       */
67      public void testNextLong() {
68          long f = ThreadLocalRandom.current().nextLong();
# Line 67 | Line 72 | public class ThreadLocalRandomTest exten
72          assertTrue(i < NCALLS);
73      }
74  
70
75      /**
76 <     * Repeated calls to nextBoolean produce at least one different result
76 >     * Repeated calls to nextBoolean produce at least two distinct results
77       */
78      public void testNextBoolean() {
79          boolean f = ThreadLocalRandom.current().nextBoolean();
# Line 80 | Line 84 | public class ThreadLocalRandomTest exten
84      }
85  
86      /**
87 <     * Repeated calls to nextFloat produce at least one different result
87 >     * Repeated calls to nextFloat produce at least two distinct results
88       */
89      public void testNextFloat() {
90          float f = ThreadLocalRandom.current().nextFloat();
# Line 91 | Line 95 | public class ThreadLocalRandomTest exten
95      }
96  
97      /**
98 <     * Repeated calls to nextDouble produce at least one different result
98 >     * Repeated calls to nextDouble produce at least two distinct results
99       */
100      public void testNextDouble() {
101          double f = ThreadLocalRandom.current().nextDouble();
102 <        double i = 0;
102 >        int i = 0;
103          while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
104              ++i;
105          assertTrue(i < NCALLS);
106      }
107  
108      /**
109 <     * Repeated calls to nextGaussian produce at least one different result
109 >     * Repeated calls to nextGaussian produce at least two distinct results
110       */
111      public void testNextGaussian() {
112          double f = ThreadLocalRandom.current().nextGaussian();
# Line 112 | Line 116 | public class ThreadLocalRandomTest exten
116          assertTrue(i < NCALLS);
117      }
118  
115
119      /**
120 <     * nextInt(negative) throws IllegalArgumentException;
120 >     * nextInt(negative) throws IllegalArgumentException
121       */
122      public void testNextIntBoundedNeg() {
123          try {
# Line 124 | Line 127 | public class ThreadLocalRandomTest exten
127      }
128  
129      /**
130 <     * nextInt(least >= bound) throws IllegalArgumentException;
130 >     * nextInt(least >= bound) throws IllegalArgumentException
131       */
132      public void testNextIntBadBounds() {
133          try {
# Line 133 | Line 136 | public class ThreadLocalRandomTest exten
136          } catch (IllegalArgumentException success) {}
137      }
138  
136
139      /**
140       * nextInt(bound) returns 0 <= value < bound;
141 <     * repeated calls produce at least one different result
141 >     * repeated calls produce at least two distinct results
142       */
143      public void testNextIntBounded() {
144          // sample bound space across prime number increments
# Line 154 | Line 156 | public class ThreadLocalRandomTest exten
156          }
157      }
158  
157
159      /**
160       * nextInt(least, bound) returns least <= value < bound;
161 <     * repeated calls produce at least one different result
161 >     * repeated calls produce at least two distinct results
162       */
163      public void testNextIntBounded2() {
164          for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
# Line 177 | Line 178 | public class ThreadLocalRandomTest exten
178      }
179  
180      /**
181 <     * nextLong(negative) throws IllegalArgumentException;
181 >     * nextLong(negative) throws IllegalArgumentException
182       */
183      public void testNextLongBoundedNeg() {
184          try {
# Line 187 | Line 188 | public class ThreadLocalRandomTest exten
188      }
189  
190      /**
191 <     * nextLong(least >= bound) throws IllegalArgumentException;
191 >     * nextLong(least >= bound) throws IllegalArgumentException
192       */
193      public void testNextLongBadBounds() {
194          try {
# Line 198 | Line 199 | public class ThreadLocalRandomTest exten
199  
200      /**
201       * nextLong(bound) returns 0 <= value < bound;
202 <     * repeated calls produce at least one different result
202 >     * repeated calls produce at least two distinct results
203       */
204      public void testNextLongBounded() {
205          for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
# Line 217 | Line 218 | public class ThreadLocalRandomTest exten
218  
219      /**
220       * nextLong(least, bound) returns least <= value < bound;
221 <     * repeated calls produce at least one different result
221 >     * repeated calls produce at least two distinct results
222       */
223      public void testNextLongBounded2() {
224          for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
# Line 236 | Line 237 | public class ThreadLocalRandomTest exten
237          }
238      }
239  
239
240      /**
241       * nextDouble(least, bound) returns least <= value < bound;
242 <     * repeated calls produce at least one different result
242 >     * repeated calls produce at least two distinct results
243       */
244      public void testNextDoubleBounded2() {
245          for (double least = 0.0001; least < 1.0e20; least *= 8) {
# Line 258 | Line 258 | public class ThreadLocalRandomTest exten
258          }
259      }
260  
261 +    /**
262 +     * Different threads produce different pseudo-random sequences
263 +     */
264 +    public void testDifferentSequences() {
265 +        // Don't use main thread's ThreadLocalRandom - it is likely to
266 +        // be polluted by previous tests.
267 +        final AtomicReference<ThreadLocalRandom> threadLocalRandom =
268 +            new AtomicReference<ThreadLocalRandom>();
269 +        final AtomicLong rand = new AtomicLong();
270 +
271 +        long firstRand = 0;
272 +        ThreadLocalRandom firstThreadLocalRandom = null;
273 +
274 +        final CheckedRunnable getRandomState = new CheckedRunnable() {
275 +            public void realRun() {
276 +                ThreadLocalRandom current = ThreadLocalRandom.current();
277 +                assertSame(current, ThreadLocalRandom.current());
278 +                // test bug: the following is not guaranteed and not true in JDK8
279 +                //                assertNotSame(current, threadLocalRandom.get());
280 +                rand.set(current.nextLong());
281 +                threadLocalRandom.set(current);
282 +            }};
283 +
284 +        Thread first = newStartedThread(getRandomState);
285 +        awaitTermination(first);
286 +        firstRand = rand.get();
287 +        firstThreadLocalRandom = threadLocalRandom.get();
288 +
289 +        for (int i = 0; i < NCALLS; i++) {
290 +            Thread t = newStartedThread(getRandomState);
291 +            awaitTermination(t);
292 +            if (firstRand != rand.get())
293 +                return;
294 +        }
295 +        fail("all threads generate the same pseudo-random sequence");
296 +    }
297  
298   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines