ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandom8Test.java
Revision: 1.5
Committed: Tue Sep 24 16:39:38 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
smaller MAX_INT_BOUND to reduce test case runtimes

File Contents

# User Rev Content
1 jsr166 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/publicdomain/zero/1.0/
5     */
6     import junit.framework.*;
7     import java.util.*;
8     import java.util.concurrent.ThreadLocalRandom;
9     import java.util.concurrent.atomic.AtomicInteger;
10     import java.util.concurrent.atomic.LongAdder;
11    
12     public class ThreadLocalRandom8Test extends JSR166TestCase {
13    
14     public static void main(String[] args) {
15     junit.textui.TestRunner.run(suite());
16     }
17     public static Test suite() {
18     return new TestSuite(ThreadLocalRandom8Test.class);
19     }
20    
21     // max sampled int bound
22 jsr166 1.5 static final int MAX_INT_BOUND = (1 << 26);
23 jsr166 1.1
24     // max sampled long bound
25     static final long MAX_LONG_BOUND = (1L << 42);
26    
27     // Number of replications for other checks
28 jsr166 1.4 static final int REPS =
29     Integer.getInteger("ThreadLocalRandom8Test.reps", 4);
30 jsr166 1.1
31     /**
32     * Invoking sized ints, long, doubles, with negative sizes throws
33     * IllegalArgumentException
34     */
35     public void testBadStreamSize() {
36     ThreadLocalRandom r = ThreadLocalRandom.current();
37     try {
38     java.util.stream.IntStream x = r.ints(-1L);
39     shouldThrow();
40     } catch (IllegalArgumentException success) {}
41     try {
42     java.util.stream.IntStream x = r.ints(-1L, 2, 3);
43     shouldThrow();
44     } catch (IllegalArgumentException success) {}
45     try {
46     java.util.stream.LongStream x = r.longs(-1L);
47     shouldThrow();
48     } catch (IllegalArgumentException success) {}
49     try {
50     java.util.stream.LongStream x = r.longs(-1L, -1L, 1L);
51     shouldThrow();
52     } catch (IllegalArgumentException success) {}
53     try {
54     java.util.stream.DoubleStream x = r.doubles(-1L);
55     shouldThrow();
56     } catch (IllegalArgumentException success) {}
57     try {
58     java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6);
59     shouldThrow();
60     } catch (IllegalArgumentException success) {}
61     }
62    
63     /**
64     * Invoking bounded ints, long, doubles, with illegal bounds throws
65     * IllegalArgumentException
66     */
67     public void testBadStreamBounds() {
68     ThreadLocalRandom r = ThreadLocalRandom.current();
69     try {
70     java.util.stream.IntStream x = r.ints(2, 1);
71     shouldThrow();
72     } catch (IllegalArgumentException success) {}
73     try {
74     java.util.stream.IntStream x = r.ints(10, 42, 42);
75     shouldThrow();
76     } catch (IllegalArgumentException success) {}
77     try {
78     java.util.stream.LongStream x = r.longs(-1L, -1L);
79     shouldThrow();
80     } catch (IllegalArgumentException success) {}
81     try {
82     java.util.stream.LongStream x = r.longs(10, 1L, -2L);
83     shouldThrow();
84     } catch (IllegalArgumentException success) {}
85     try {
86     java.util.stream.DoubleStream x = r.doubles(0.0, 0.0);
87     shouldThrow();
88     } catch (IllegalArgumentException success) {}
89     try {
90     java.util.stream.DoubleStream x = r.doubles(10, .5, .4);
91     shouldThrow();
92     } catch (IllegalArgumentException success) {}
93     }
94    
95     /**
96     * A parallel sized stream of ints generates the given number of values
97     */
98     public void testIntsCount() {
99     LongAdder counter = new LongAdder();
100     ThreadLocalRandom r = ThreadLocalRandom.current();
101     long size = 0;
102     for (int reps = 0; reps < REPS; ++reps) {
103     counter.reset();
104 jsr166 1.3 r.ints(size).parallel().forEach(x -> counter.increment());
105 jsr166 1.1 assertEquals(size, counter.sum());
106     size += 524959;
107     }
108     }
109    
110     /**
111     * A parallel sized stream of longs generates the given number of values
112     */
113     public void testLongsCount() {
114     LongAdder counter = new LongAdder();
115     ThreadLocalRandom r = ThreadLocalRandom.current();
116     long size = 0;
117     for (int reps = 0; reps < REPS; ++reps) {
118     counter.reset();
119 jsr166 1.3 r.longs(size).parallel().forEach(x -> counter.increment());
120 jsr166 1.1 assertEquals(size, counter.sum());
121     size += 524959;
122     }
123     }
124    
125     /**
126     * A parallel sized stream of doubles generates the given number of values
127     */
128     public void testDoublesCount() {
129     LongAdder counter = new LongAdder();
130     ThreadLocalRandom r = ThreadLocalRandom.current();
131     long size = 0;
132     for (int reps = 0; reps < REPS; ++reps) {
133     counter.reset();
134 jsr166 1.3 r.doubles(size).parallel().forEach(x -> counter.increment());
135 jsr166 1.1 assertEquals(size, counter.sum());
136     size += 524959;
137     }
138     }
139    
140     /**
141     * Each of a parallel sized stream of bounded ints is within bounds
142     */
143     public void testBoundedInts() {
144     AtomicInteger fails = new AtomicInteger(0);
145     ThreadLocalRandom r = ThreadLocalRandom.current();
146     long size = 12345L;
147     for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
148     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
149     final int lo = least, hi = bound;
150     r.ints(size, lo, hi).parallel().
151     forEach(x -> {if (x < lo || x >= hi)
152     fails.getAndIncrement(); });
153     }
154     }
155     assertEquals(0, fails.get());
156     }
157    
158     /**
159     * Each of a parallel sized stream of bounded longs is within bounds
160     */
161     public void testBoundedLongs() {
162     AtomicInteger fails = new AtomicInteger(0);
163     ThreadLocalRandom r = ThreadLocalRandom.current();
164     long size = 123L;
165     for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
166     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
167     final long lo = least, hi = bound;
168     r.longs(size, lo, hi).parallel().
169     forEach(x -> {if (x < lo || x >= hi)
170     fails.getAndIncrement(); });
171     }
172     }
173     assertEquals(0, fails.get());
174     }
175    
176     /**
177     * Each of a parallel sized stream of bounded doubles is within bounds
178     */
179     public void testBoundedDoubles() {
180     AtomicInteger fails = new AtomicInteger(0);
181     ThreadLocalRandom r = ThreadLocalRandom.current();
182     long size = 456;
183     for (double least = 0.00011; least < 1.0e20; least *= 9) {
184     for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
185     final double lo = least, hi = bound;
186     r.doubles(size, lo, hi).parallel().
187     forEach(x -> {if (x < lo || x >= hi)
188     fails.getAndIncrement(); });
189     }
190     }
191     assertEquals(0, fails.get());
192     }
193    
194     /**
195     * A parallel unsized stream of ints generates at least 100 values
196     */
197     public void testUnsizedIntsCount() {
198     LongAdder counter = new LongAdder();
199     ThreadLocalRandom r = ThreadLocalRandom.current();
200     long size = 100;
201 jsr166 1.3 r.ints().limit(size).parallel().forEach(x -> counter.increment());
202 jsr166 1.1 assertEquals(size, counter.sum());
203     }
204    
205     /**
206     * A parallel unsized stream of longs generates at least 100 values
207     */
208     public void testUnsizedLongsCount() {
209     LongAdder counter = new LongAdder();
210     ThreadLocalRandom r = ThreadLocalRandom.current();
211     long size = 100;
212 jsr166 1.3 r.longs().limit(size).parallel().forEach(x -> counter.increment());
213 jsr166 1.1 assertEquals(size, counter.sum());
214     }
215    
216     /**
217     * A parallel unsized stream of doubles generates at least 100 values
218     */
219     public void testUnsizedDoublesCount() {
220     LongAdder counter = new LongAdder();
221     ThreadLocalRandom r = ThreadLocalRandom.current();
222     long size = 100;
223 jsr166 1.3 r.doubles().limit(size).parallel().forEach(x -> counter.increment());
224 jsr166 1.1 assertEquals(size, counter.sum());
225     }
226    
227     /**
228     * A sequential unsized stream of ints generates at least 100 values
229     */
230     public void testUnsizedIntsCountSeq() {
231     LongAdder counter = new LongAdder();
232     ThreadLocalRandom r = ThreadLocalRandom.current();
233     long size = 100;
234 jsr166 1.3 r.ints().limit(size).forEach(x -> counter.increment());
235 jsr166 1.1 assertEquals(size, counter.sum());
236     }
237    
238     /**
239     * A sequential unsized stream of longs generates at least 100 values
240     */
241     public void testUnsizedLongsCountSeq() {
242     LongAdder counter = new LongAdder();
243     ThreadLocalRandom r = ThreadLocalRandom.current();
244     long size = 100;
245 jsr166 1.3 r.longs().limit(size).forEach(x -> counter.increment());
246 jsr166 1.1 assertEquals(size, counter.sum());
247     }
248    
249     /**
250     * A sequential unsized stream of doubles generates at least 100 values
251     */
252     public void testUnsizedDoublesCountSeq() {
253     LongAdder counter = new LongAdder();
254     ThreadLocalRandom r = ThreadLocalRandom.current();
255     long size = 100;
256 jsr166 1.3 r.doubles().limit(size).forEach(x -> counter.increment());
257 jsr166 1.1 assertEquals(size, counter.sum());
258     }
259    
260     }