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

Comparing jsr166/src/test/tck/ThreadLocalRandom8Test.java (file contents):
Revision 1.1 by jsr166, Fri Aug 16 07:07:01 2013 UTC vs.
Revision 1.8 by jsr166, Wed Dec 31 21:28:49 2014 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import junit.framework.*;
7 < import java.util.*;
6 >
7   import java.util.concurrent.ThreadLocalRandom;
9 import java.util.concurrent.atomic.AtomicLong;
8   import java.util.concurrent.atomic.AtomicInteger;
11 import java.util.concurrent.atomic.AtomicReference;
9   import java.util.concurrent.atomic.LongAdder;
10  
11 + import junit.framework.Test;
12 + import junit.framework.TestSuite;
13 +
14   public class ThreadLocalRandom8Test extends JSR166TestCase {
15  
16      public static void main(String[] args) {
# Line 21 | Line 21 | public class ThreadLocalRandom8Test exte
21      }
22  
23      // max sampled int bound
24 <    static final int MAX_INT_BOUND = (1 << 28);
24 >    static final int MAX_INT_BOUND = (1 << 26);
25  
26      // max sampled long bound
27      static final long MAX_LONG_BOUND = (1L << 42);
28  
29      // Number of replications for other checks
30 <    static final int REPS = 20;
30 >    static final int REPS =
31 >        Integer.getInteger("ThreadLocalRandom8Test.reps", 4);
32  
33      /**
34       * Invoking sized ints, long, doubles, with negative sizes throws
# Line 35 | Line 36 | public class ThreadLocalRandom8Test exte
36       */
37      public void testBadStreamSize() {
38          ThreadLocalRandom r = ThreadLocalRandom.current();
39 <        try {
40 <            java.util.stream.IntStream x = r.ints(-1L);
41 <            shouldThrow();
42 <        } catch (IllegalArgumentException success) {}
43 <        try {
44 <            java.util.stream.IntStream x = r.ints(-1L, 2, 3);
45 <            shouldThrow();
46 <        } catch (IllegalArgumentException success) {}
47 <        try {
47 <            java.util.stream.LongStream x = r.longs(-1L);
48 <            shouldThrow();
49 <        } catch (IllegalArgumentException success) {}
50 <        try {
51 <            java.util.stream.LongStream x = r.longs(-1L, -1L, 1L);
52 <            shouldThrow();
53 <        } catch (IllegalArgumentException success) {}
54 <        try {
55 <            java.util.stream.DoubleStream x = r.doubles(-1L);
56 <            shouldThrow();
57 <        } catch (IllegalArgumentException success) {}
58 <        try {
59 <            java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6);
60 <            shouldThrow();
61 <        } catch (IllegalArgumentException success) {}
39 >        Runnable[] throwingActions = {
40 >            () -> r.ints(-1L),
41 >            () -> r.ints(-1L, 2, 3),
42 >            () -> r.longs(-1L),
43 >            () -> r.longs(-1L, -1L, 1L),
44 >            () -> r.doubles(-1L),
45 >            () -> r.doubles(-1L, .5, .6),
46 >        };
47 >        assertThrows(IllegalArgumentException.class, throwingActions);
48      }
49  
50      /**
# Line 67 | Line 53 | public class ThreadLocalRandom8Test exte
53       */
54      public void testBadStreamBounds() {
55          ThreadLocalRandom r = ThreadLocalRandom.current();
56 <        try {
57 <            java.util.stream.IntStream x = r.ints(2, 1);
58 <            shouldThrow();
59 <        } catch (IllegalArgumentException success) {}
60 <        try {
61 <            java.util.stream.IntStream x = r.ints(10, 42, 42);
62 <            shouldThrow();
63 <        } catch (IllegalArgumentException success) {}
64 <        try {
79 <            java.util.stream.LongStream x = r.longs(-1L, -1L);
80 <            shouldThrow();
81 <        } catch (IllegalArgumentException success) {}
82 <        try {
83 <            java.util.stream.LongStream x = r.longs(10, 1L, -2L);
84 <            shouldThrow();
85 <        } catch (IllegalArgumentException success) {}
86 <        try {
87 <            java.util.stream.DoubleStream x = r.doubles(0.0, 0.0);
88 <            shouldThrow();
89 <        } catch (IllegalArgumentException success) {}
90 <        try {
91 <            java.util.stream.DoubleStream x = r.doubles(10, .5, .4);
92 <            shouldThrow();
93 <        } catch (IllegalArgumentException success) {}
56 >        Runnable[] throwingActions = {
57 >            () -> r.ints(2, 1),
58 >            () -> r.ints(10, 42, 42),
59 >            () -> r.longs(-1L, -1L),
60 >            () -> r.longs(10, 1L, -2L),
61 >            () -> r.doubles(0.0, 0.0),
62 >            () -> r.doubles(10, .5, .4),
63 >        };
64 >        assertThrows(IllegalArgumentException.class, throwingActions);
65      }
66  
67      /**
# Line 102 | Line 73 | public class ThreadLocalRandom8Test exte
73          long size = 0;
74          for (int reps = 0; reps < REPS; ++reps) {
75              counter.reset();
76 <            r.ints(size).parallel().forEach(x -> {counter.increment();});
76 >            r.ints(size).parallel().forEach(x -> counter.increment());
77              assertEquals(size, counter.sum());
78              size += 524959;
79          }
# Line 117 | Line 88 | public class ThreadLocalRandom8Test exte
88          long size = 0;
89          for (int reps = 0; reps < REPS; ++reps) {
90              counter.reset();
91 <            r.longs(size).parallel().forEach(x -> {counter.increment();});
91 >            r.longs(size).parallel().forEach(x -> counter.increment());
92              assertEquals(size, counter.sum());
93              size += 524959;
94          }
# Line 132 | Line 103 | public class ThreadLocalRandom8Test exte
103          long size = 0;
104          for (int reps = 0; reps < REPS; ++reps) {
105              counter.reset();
106 <            r.doubles(size).parallel().forEach(x -> {counter.increment();});
106 >            r.doubles(size).parallel().forEach(x -> counter.increment());
107              assertEquals(size, counter.sum());
108              size += 524959;
109          }
# Line 148 | Line 119 | public class ThreadLocalRandom8Test exte
119          for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
120              for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
121                  final int lo = least, hi = bound;
122 <                r.ints(size, lo, hi).parallel().
123 <                    forEach(x -> {if (x < lo || x >= hi)
124 <                                fails.getAndIncrement(); });
122 >                r.ints(size, lo, hi).parallel().forEach(
123 >                    x -> {
124 >                        if (x < lo || x >= hi)
125 >                            fails.getAndIncrement(); });
126              }
127          }
128          assertEquals(0, fails.get());
# Line 166 | Line 138 | public class ThreadLocalRandom8Test exte
138          for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
139              for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
140                  final long lo = least, hi = bound;
141 <                r.longs(size, lo, hi).parallel().
142 <                    forEach(x -> {if (x < lo || x >= hi)
143 <                                fails.getAndIncrement(); });
141 >                r.longs(size, lo, hi).parallel().forEach(
142 >                    x -> {
143 >                        if (x < lo || x >= hi)
144 >                            fails.getAndIncrement(); });
145              }
146          }
147          assertEquals(0, fails.get());
# Line 184 | Line 157 | public class ThreadLocalRandom8Test exte
157          for (double least = 0.00011; least < 1.0e20; least *= 9) {
158              for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
159                  final double lo = least, hi = bound;
160 <                r.doubles(size, lo, hi).parallel().
161 <                    forEach(x -> {if (x < lo || x >= hi)
162 <                                fails.getAndIncrement(); });
160 >                r.doubles(size, lo, hi).parallel().forEach(
161 >                    x -> {
162 >                        if (x < lo || x >= hi)
163 >                            fails.getAndIncrement(); });
164              }
165          }
166          assertEquals(0, fails.get());
# Line 199 | Line 173 | public class ThreadLocalRandom8Test exte
173          LongAdder counter = new LongAdder();
174          ThreadLocalRandom r = ThreadLocalRandom.current();
175          long size = 100;
176 <        r.ints().limit(size).parallel().forEach(x -> {counter.increment();});
176 >        r.ints().limit(size).parallel().forEach(x -> counter.increment());
177          assertEquals(size, counter.sum());
178      }
179  
# Line 210 | Line 184 | public class ThreadLocalRandom8Test exte
184          LongAdder counter = new LongAdder();
185          ThreadLocalRandom r = ThreadLocalRandom.current();
186          long size = 100;
187 <        r.longs().limit(size).parallel().forEach(x -> {counter.increment();});
187 >        r.longs().limit(size).parallel().forEach(x -> counter.increment());
188          assertEquals(size, counter.sum());
189      }
190  
# Line 221 | Line 195 | public class ThreadLocalRandom8Test exte
195          LongAdder counter = new LongAdder();
196          ThreadLocalRandom r = ThreadLocalRandom.current();
197          long size = 100;
198 <        r.doubles().limit(size).parallel().forEach(x -> {counter.increment();});
198 >        r.doubles().limit(size).parallel().forEach(x -> counter.increment());
199          assertEquals(size, counter.sum());
200      }
201  
# Line 232 | Line 206 | public class ThreadLocalRandom8Test exte
206          LongAdder counter = new LongAdder();
207          ThreadLocalRandom r = ThreadLocalRandom.current();
208          long size = 100;
209 <        r.ints().limit(size).forEach(x -> {counter.increment();});
209 >        r.ints().limit(size).forEach(x -> counter.increment());
210          assertEquals(size, counter.sum());
211      }
212  
# Line 243 | Line 217 | public class ThreadLocalRandom8Test exte
217          LongAdder counter = new LongAdder();
218          ThreadLocalRandom r = ThreadLocalRandom.current();
219          long size = 100;
220 <        r.longs().limit(size).forEach(x -> {counter.increment();});
220 >        r.longs().limit(size).forEach(x -> counter.increment());
221          assertEquals(size, counter.sum());
222      }
223  
# Line 254 | Line 228 | public class ThreadLocalRandom8Test exte
228          LongAdder counter = new LongAdder();
229          ThreadLocalRandom r = ThreadLocalRandom.current();
230          long size = 100;
231 <        r.doubles().limit(size).forEach(x -> {counter.increment();});
231 >        r.doubles().limit(size).forEach(x -> counter.increment());
232          assertEquals(size, counter.sum());
233      }
234  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines