ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandom8Test.java
Revision: 1.3
Committed: Mon Sep 23 17:05:52 2013 UTC (10 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +9 -9 lines
Log Message:
use nicer lambda syntax

File Contents

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