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.13 by jsr166, Sat Feb 16 20:50:29 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 42 | Line 43 | public class ThreadLocalRandomTest exten
43          try {
44              ThreadLocalRandom.current().setSeed(17);
45              shouldThrow();
46 <        } catch (UnsupportedOperationException success) {
46 <        }
46 >        } catch (UnsupportedOperationException success) {}
47      }
48  
49      /**
# Line 68 | Line 68 | public class ThreadLocalRandomTest exten
68          assertTrue(i < NCALLS);
69      }
70  
71
71      /**
72       * Repeated calls to nextBoolean produce at least one different result
73       */
# Line 113 | Line 112 | public class ThreadLocalRandomTest exten
112          assertTrue(i < NCALLS);
113      }
114  
116
115      /**
116       * nextInt(negative) throws IllegalArgumentException;
117       */
118      public void testNextIntBoundedNeg() {
119          try {
120 <            int  f = ThreadLocalRandom.current().nextInt(-17);
120 >            int f = ThreadLocalRandom.current().nextInt(-17);
121              shouldThrow();
122 <        } catch (IllegalArgumentException success) {
125 <        }
122 >        } catch (IllegalArgumentException success) {}
123      }
124  
125      /**
# Line 130 | Line 127 | public class ThreadLocalRandomTest exten
127       */
128      public void testNextIntBadBounds() {
129          try {
130 <            int  f = ThreadLocalRandom.current().nextInt(17, 2);
130 >            int f = ThreadLocalRandom.current().nextInt(17, 2);
131              shouldThrow();
132 <        } catch (IllegalArgumentException success) {
136 <        }
132 >        } catch (IllegalArgumentException success) {}
133      }
134  
139
135      /**
136       * nextInt(bound) returns 0 <= value < bound;
137       * repeated calls produce at least one different result
# Line 157 | Line 152 | public class ThreadLocalRandomTest exten
152          }
153      }
154  
160
155      /**
156       * nextInt(least, bound) returns least <= value < bound;
157       * repeated calls produce at least one different result
# Line 184 | Line 178 | public class ThreadLocalRandomTest exten
178       */
179      public void testNextLongBoundedNeg() {
180          try {
181 <            long  f = ThreadLocalRandom.current().nextLong(-17);
181 >            long f = ThreadLocalRandom.current().nextLong(-17);
182              shouldThrow();
183 <        } catch (IllegalArgumentException success) {
190 <        }
183 >        } catch (IllegalArgumentException success) {}
184      }
185  
186      /**
# Line 195 | Line 188 | public class ThreadLocalRandomTest exten
188       */
189      public void testNextLongBadBounds() {
190          try {
191 <            long  f = ThreadLocalRandom.current().nextLong(17, 2);
191 >            long f = ThreadLocalRandom.current().nextLong(17, 2);
192              shouldThrow();
193 <        } catch (IllegalArgumentException success) {
201 <        }
193 >        } catch (IllegalArgumentException success) {}
194      }
195  
196      /**
# Line 241 | Line 233 | public class ThreadLocalRandomTest exten
233          }
234      }
235  
244
236      /**
237       * nextDouble(least, bound) returns least <= value < bound;
238       * repeated calls produce at least one different result
# Line 263 | Line 254 | public class ThreadLocalRandomTest exten
254          }
255      }
256  
257 +    /**
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 +                // test bug: the following is not guaranteed and not true in JDK8
275 +                //                assertNotSame(current, threadLocalRandom.get());
276 +                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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines