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.3 by jsr166, Tue Aug 4 10:04:47 2009 UTC vs.
Revision 1.17 by jsr166, Fri Aug 16 07:10:20 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.AtomicReference;
11  
12   public class ThreadLocalRandomTest extends JSR166TestCase {
13  
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18 <        return new TestSuite(ThreadLocalRandomTest.class);
18 >        return new TestSuite(ThreadLocalRandomTest.class);
19      }
20  
21 <    /**
21 >    /*
22       * Testing coverage notes:
23       *
24       * We don't test randomness properties, but only that repeated
# Line 26 | Line 27 | public class ThreadLocalRandomTest exten
27       * across multiples of primes.
28       */
29  
30 <    //
30 >    // max numbers of calls to detect getting stuck on one value
31      static final int NCALLS = 10000;
32  
33      // max sampled int bound
34      static final int MAX_INT_BOUND = (1 << 28);
35  
36 <    // Max sampled long bound
36 >    // max sampled long bound
37      static final long MAX_LONG_BOUND = (1L << 42);
38  
39 +    // Number of replications for other checks
40 +    static final int REPS = 20;
41 +
42      /**
43       * setSeed throws UnsupportedOperationException
44       */
# Line 42 | Line 46 | public class ThreadLocalRandomTest exten
46          try {
47              ThreadLocalRandom.current().setSeed(17);
48              shouldThrow();
49 <        } catch (UnsupportedOperationException success) {
46 <        }
49 >        } catch (UnsupportedOperationException success) {}
50      }
51  
52      /**
53 <     * Repeated calls to nextInt produce at least one different result
53 >     * Repeated calls to nextInt produce at least two distinct results
54       */
55      public void testNextInt() {
56          int f = ThreadLocalRandom.current().nextInt();
# Line 58 | Line 61 | public class ThreadLocalRandomTest exten
61      }
62  
63      /**
64 <     * Repeated calls to nextLong produce at least one different result
64 >     * Repeated calls to nextLong produce at least two distinct results
65       */
66      public void testNextLong() {
67          long f = ThreadLocalRandom.current().nextLong();
# Line 68 | Line 71 | public class ThreadLocalRandomTest exten
71          assertTrue(i < NCALLS);
72      }
73  
71
74      /**
75 <     * Repeated calls to nextBoolean produce at least one different result
75 >     * Repeated calls to nextBoolean produce at least two distinct results
76       */
77      public void testNextBoolean() {
78          boolean f = ThreadLocalRandom.current().nextBoolean();
# Line 81 | Line 83 | public class ThreadLocalRandomTest exten
83      }
84  
85      /**
86 <     * Repeated calls to nextFloat produce at least one different result
86 >     * Repeated calls to nextFloat produce at least two distinct results
87       */
88      public void testNextFloat() {
89          float f = ThreadLocalRandom.current().nextFloat();
# Line 92 | Line 94 | public class ThreadLocalRandomTest exten
94      }
95  
96      /**
97 <     * Repeated calls to nextDouble produce at least one different result
97 >     * Repeated calls to nextDouble produce at least two distinct results
98       */
99      public void testNextDouble() {
100          double f = ThreadLocalRandom.current().nextDouble();
101 <        double i = 0;
101 >        int i = 0;
102          while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
103              ++i;
104          assertTrue(i < NCALLS);
105      }
106  
107      /**
108 <     * Repeated calls to nextGaussian produce at least one different result
108 >     * Repeated calls to nextGaussian produce at least two distinct results
109       */
110      public void testNextGaussian() {
111          double f = ThreadLocalRandom.current().nextGaussian();
# Line 113 | Line 115 | public class ThreadLocalRandomTest exten
115          assertTrue(i < NCALLS);
116      }
117  
116
118      /**
119 <     * nextInt(negative) throws IllegalArgumentException;
119 >     * nextInt(negative) throws IllegalArgumentException
120       */
121      public void testNextIntBoundedNeg() {
122          try {
123 <            int  f = ThreadLocalRandom.current().nextInt(-17);
123 >            int f = ThreadLocalRandom.current().nextInt(-17);
124              shouldThrow();
125 <        } catch (IllegalArgumentException success) {
125 <        }
125 >        } catch (IllegalArgumentException success) {}
126      }
127  
128      /**
129 <     * nextInt(least >= bound) throws IllegalArgumentException;
129 >     * nextInt(least >= bound) throws IllegalArgumentException
130       */
131      public void testNextIntBadBounds() {
132          try {
133 <            int  f = ThreadLocalRandom.current().nextInt(17, 2);
133 >            int f = ThreadLocalRandom.current().nextInt(17, 2);
134              shouldThrow();
135 <        } catch (IllegalArgumentException success) {
136 <        }
135 >        } catch (IllegalArgumentException success) {}
136      }
137  
139
138      /**
139       * nextInt(bound) returns 0 <= value < bound;
140 <     * repeated calls produce at least one different result
140 >     * repeated calls produce at least two distinct results
141       */
142      public void testNextIntBounded() {
143          // sample bound space across prime number increments
# Line 157 | Line 155 | public class ThreadLocalRandomTest exten
155          }
156      }
157  
160
158      /**
159       * nextInt(least, bound) returns least <= value < bound;
160 <     * repeated calls produce at least one different result
160 >     * repeated calls produce at least two distinct results
161       */
162      public void testNextIntBounded2() {
163          for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
# Line 180 | Line 177 | public class ThreadLocalRandomTest exten
177      }
178  
179      /**
180 <     * nextLong(negative) throws IllegalArgumentException;
180 >     * nextLong(negative) throws IllegalArgumentException
181       */
182      public void testNextLongBoundedNeg() {
183          try {
184 <            long  f = ThreadLocalRandom.current().nextLong(-17);
184 >            long f = ThreadLocalRandom.current().nextLong(-17);
185              shouldThrow();
186 <        } catch (IllegalArgumentException success) {
190 <        }
186 >        } catch (IllegalArgumentException success) {}
187      }
188  
189      /**
190 <     * nextLong(least >= bound) throws IllegalArgumentException;
190 >     * nextLong(least >= bound) throws IllegalArgumentException
191       */
192      public void testNextLongBadBounds() {
193          try {
194 <            long  f = ThreadLocalRandom.current().nextLong(17, 2);
194 >            long f = ThreadLocalRandom.current().nextLong(17, 2);
195              shouldThrow();
196 <        } catch (IllegalArgumentException success) {
201 <        }
196 >        } catch (IllegalArgumentException success) {}
197      }
198  
199      /**
200       * nextLong(bound) returns 0 <= value < bound;
201 <     * repeated calls produce at least one different result
201 >     * repeated calls produce at least two distinct results
202       */
203      public void testNextLongBounded() {
204          for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
# Line 222 | Line 217 | public class ThreadLocalRandomTest exten
217  
218      /**
219       * nextLong(least, bound) returns least <= value < bound;
220 <     * repeated calls produce at least one different result
220 >     * repeated calls produce at least two distinct results
221       */
222      public void testNextLongBounded2() {
223          for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
# Line 241 | Line 236 | public class ThreadLocalRandomTest exten
236          }
237      }
238  
244
239      /**
240       * nextDouble(least, bound) returns least <= value < bound;
241 <     * repeated calls produce at least one different result
241 >     * repeated calls produce at least two distinct results
242       */
243      public void testNextDoubleBounded2() {
244          for (double least = 0.0001; least < 1.0e20; least *= 8) {
# Line 263 | Line 257 | public class ThreadLocalRandomTest exten
257          }
258      }
259  
260 +    /**
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 +                // test bug: the following is not guaranteed and not true in JDK8
278 +                //                assertNotSame(current, threadLocalRandom.get());
279 +                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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines