ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandom8Test.java
Revision: 1.6
Committed: Fri Sep 27 20:28:04 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +18 -48 lines
Log Message:
use assertThrows

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 jsr166 1.6 Runnable[] throwingActions = {
38     () -> r.ints(-1L),
39     () -> r.ints(-1L, 2, 3),
40     () -> r.longs(-1L),
41     () -> r.longs(-1L, -1L, 1L),
42     () -> r.doubles(-1L),
43     () -> r.doubles(-1L, .5, .6),
44     };
45     assertThrows(IllegalArgumentException.class, throwingActions);
46 jsr166 1.1 }
47    
48     /**
49     * Invoking bounded ints, long, doubles, with illegal bounds throws
50     * IllegalArgumentException
51     */
52     public void testBadStreamBounds() {
53     ThreadLocalRandom r = ThreadLocalRandom.current();
54 jsr166 1.6 Runnable[] throwingActions = {
55     () -> r.ints(2, 1),
56     () -> r.ints(10, 42, 42),
57     () -> r.longs(-1L, -1L),
58     () -> r.longs(10, 1L, -2L),
59     () -> r.doubles(0.0, 0.0),
60     () -> r.doubles(10, .5, .4),
61     };
62     assertThrows(IllegalArgumentException.class, throwingActions);
63 jsr166 1.1 }
64    
65     /**
66     * A parallel sized stream of ints generates the given number of values
67     */
68     public void testIntsCount() {
69     LongAdder counter = new LongAdder();
70     ThreadLocalRandom r = ThreadLocalRandom.current();
71     long size = 0;
72     for (int reps = 0; reps < REPS; ++reps) {
73     counter.reset();
74 jsr166 1.3 r.ints(size).parallel().forEach(x -> counter.increment());
75 jsr166 1.1 assertEquals(size, counter.sum());
76     size += 524959;
77     }
78     }
79    
80     /**
81     * A parallel sized stream of longs generates the given number of values
82     */
83     public void testLongsCount() {
84     LongAdder counter = new LongAdder();
85     ThreadLocalRandom r = ThreadLocalRandom.current();
86     long size = 0;
87     for (int reps = 0; reps < REPS; ++reps) {
88     counter.reset();
89 jsr166 1.3 r.longs(size).parallel().forEach(x -> counter.increment());
90 jsr166 1.1 assertEquals(size, counter.sum());
91     size += 524959;
92     }
93     }
94    
95     /**
96     * A parallel sized stream of doubles generates the given number of values
97     */
98     public void testDoublesCount() {
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.doubles(size).parallel().forEach(x -> counter.increment());
105 jsr166 1.1 assertEquals(size, counter.sum());
106     size += 524959;
107     }
108     }
109    
110     /**
111     * Each of a parallel sized stream of bounded ints is within bounds
112     */
113     public void testBoundedInts() {
114     AtomicInteger fails = new AtomicInteger(0);
115     ThreadLocalRandom r = ThreadLocalRandom.current();
116     long size = 12345L;
117     for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
118     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
119     final int lo = least, hi = bound;
120     r.ints(size, lo, hi).parallel().
121     forEach(x -> {if (x < lo || x >= hi)
122     fails.getAndIncrement(); });
123     }
124     }
125     assertEquals(0, fails.get());
126     }
127    
128     /**
129     * Each of a parallel sized stream of bounded longs is within bounds
130     */
131     public void testBoundedLongs() {
132     AtomicInteger fails = new AtomicInteger(0);
133     ThreadLocalRandom r = ThreadLocalRandom.current();
134     long size = 123L;
135     for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
136     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
137     final long lo = least, hi = bound;
138     r.longs(size, lo, hi).parallel().
139     forEach(x -> {if (x < lo || x >= hi)
140     fails.getAndIncrement(); });
141     }
142     }
143     assertEquals(0, fails.get());
144     }
145    
146     /**
147     * Each of a parallel sized stream of bounded doubles is within bounds
148     */
149     public void testBoundedDoubles() {
150     AtomicInteger fails = new AtomicInteger(0);
151     ThreadLocalRandom r = ThreadLocalRandom.current();
152     long size = 456;
153     for (double least = 0.00011; least < 1.0e20; least *= 9) {
154     for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
155     final double lo = least, hi = bound;
156     r.doubles(size, lo, hi).parallel().
157     forEach(x -> {if (x < lo || x >= hi)
158     fails.getAndIncrement(); });
159     }
160     }
161     assertEquals(0, fails.get());
162     }
163    
164     /**
165     * A parallel unsized stream of ints generates at least 100 values
166     */
167     public void testUnsizedIntsCount() {
168     LongAdder counter = new LongAdder();
169     ThreadLocalRandom r = ThreadLocalRandom.current();
170     long size = 100;
171 jsr166 1.3 r.ints().limit(size).parallel().forEach(x -> counter.increment());
172 jsr166 1.1 assertEquals(size, counter.sum());
173     }
174    
175     /**
176     * A parallel unsized stream of longs generates at least 100 values
177     */
178     public void testUnsizedLongsCount() {
179     LongAdder counter = new LongAdder();
180     ThreadLocalRandom r = ThreadLocalRandom.current();
181     long size = 100;
182 jsr166 1.3 r.longs().limit(size).parallel().forEach(x -> counter.increment());
183 jsr166 1.1 assertEquals(size, counter.sum());
184     }
185    
186     /**
187     * A parallel unsized stream of doubles generates at least 100 values
188     */
189     public void testUnsizedDoublesCount() {
190     LongAdder counter = new LongAdder();
191     ThreadLocalRandom r = ThreadLocalRandom.current();
192     long size = 100;
193 jsr166 1.3 r.doubles().limit(size).parallel().forEach(x -> counter.increment());
194 jsr166 1.1 assertEquals(size, counter.sum());
195     }
196    
197     /**
198     * A sequential unsized stream of ints generates at least 100 values
199     */
200     public void testUnsizedIntsCountSeq() {
201     LongAdder counter = new LongAdder();
202     ThreadLocalRandom r = ThreadLocalRandom.current();
203     long size = 100;
204 jsr166 1.3 r.ints().limit(size).forEach(x -> counter.increment());
205 jsr166 1.1 assertEquals(size, counter.sum());
206     }
207    
208     /**
209     * A sequential unsized stream of longs generates at least 100 values
210     */
211     public void testUnsizedLongsCountSeq() {
212     LongAdder counter = new LongAdder();
213     ThreadLocalRandom r = ThreadLocalRandom.current();
214     long size = 100;
215 jsr166 1.3 r.longs().limit(size).forEach(x -> counter.increment());
216 jsr166 1.1 assertEquals(size, counter.sum());
217     }
218    
219     /**
220     * A sequential unsized stream of doubles generates at least 100 values
221     */
222     public void testUnsizedDoublesCountSeq() {
223     LongAdder counter = new LongAdder();
224     ThreadLocalRandom r = ThreadLocalRandom.current();
225     long size = 100;
226 jsr166 1.3 r.doubles().limit(size).forEach(x -> counter.increment());
227 jsr166 1.1 assertEquals(size, counter.sum());
228     }
229    
230     }