ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandom8Test.java
Revision: 1.7
Committed: Wed Dec 31 19:05:43 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +4 -2 lines
Log Message:
no wildcard imports

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
7 import java.util.concurrent.ThreadLocalRandom;
8 import java.util.concurrent.atomic.AtomicInteger;
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) {
17 junit.textui.TestRunner.run(suite());
18 }
19 public static Test suite() {
20 return new TestSuite(ThreadLocalRandom8Test.class);
21 }
22
23 // max sampled int bound
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 =
31 Integer.getInteger("ThreadLocalRandom8Test.reps", 4);
32
33 /**
34 * Invoking sized ints, long, doubles, with negative sizes throws
35 * IllegalArgumentException
36 */
37 public void testBadStreamSize() {
38 ThreadLocalRandom r = ThreadLocalRandom.current();
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 /**
51 * Invoking bounded ints, long, doubles, with illegal bounds throws
52 * IllegalArgumentException
53 */
54 public void testBadStreamBounds() {
55 ThreadLocalRandom r = ThreadLocalRandom.current();
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 /**
68 * A parallel sized stream of ints generates the given number of values
69 */
70 public void testIntsCount() {
71 LongAdder counter = new LongAdder();
72 ThreadLocalRandom r = ThreadLocalRandom.current();
73 long size = 0;
74 for (int reps = 0; reps < REPS; ++reps) {
75 counter.reset();
76 r.ints(size).parallel().forEach(x -> counter.increment());
77 assertEquals(size, counter.sum());
78 size += 524959;
79 }
80 }
81
82 /**
83 * A parallel sized stream of longs generates the given number of values
84 */
85 public void testLongsCount() {
86 LongAdder counter = new LongAdder();
87 ThreadLocalRandom r = ThreadLocalRandom.current();
88 long size = 0;
89 for (int reps = 0; reps < REPS; ++reps) {
90 counter.reset();
91 r.longs(size).parallel().forEach(x -> counter.increment());
92 assertEquals(size, counter.sum());
93 size += 524959;
94 }
95 }
96
97 /**
98 * A parallel sized stream of doubles generates the given number of values
99 */
100 public void testDoublesCount() {
101 LongAdder counter = new LongAdder();
102 ThreadLocalRandom r = ThreadLocalRandom.current();
103 long size = 0;
104 for (int reps = 0; reps < REPS; ++reps) {
105 counter.reset();
106 r.doubles(size).parallel().forEach(x -> counter.increment());
107 assertEquals(size, counter.sum());
108 size += 524959;
109 }
110 }
111
112 /**
113 * Each of a parallel sized stream of bounded ints is within bounds
114 */
115 public void testBoundedInts() {
116 AtomicInteger fails = new AtomicInteger(0);
117 ThreadLocalRandom r = ThreadLocalRandom.current();
118 long size = 12345L;
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(); });
125 }
126 }
127 assertEquals(0, fails.get());
128 }
129
130 /**
131 * Each of a parallel sized stream of bounded longs is within bounds
132 */
133 public void testBoundedLongs() {
134 AtomicInteger fails = new AtomicInteger(0);
135 ThreadLocalRandom r = ThreadLocalRandom.current();
136 long size = 123L;
137 for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
138 for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
139 final long lo = least, hi = bound;
140 r.longs(size, lo, hi).parallel().
141 forEach(x -> {if (x < lo || x >= hi)
142 fails.getAndIncrement(); });
143 }
144 }
145 assertEquals(0, fails.get());
146 }
147
148 /**
149 * Each of a parallel sized stream of bounded doubles is within bounds
150 */
151 public void testBoundedDoubles() {
152 AtomicInteger fails = new AtomicInteger(0);
153 ThreadLocalRandom r = ThreadLocalRandom.current();
154 long size = 456;
155 for (double least = 0.00011; least < 1.0e20; least *= 9) {
156 for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
157 final double lo = least, hi = bound;
158 r.doubles(size, lo, hi).parallel().
159 forEach(x -> {if (x < lo || x >= hi)
160 fails.getAndIncrement(); });
161 }
162 }
163 assertEquals(0, fails.get());
164 }
165
166 /**
167 * A parallel unsized stream of ints generates at least 100 values
168 */
169 public void testUnsizedIntsCount() {
170 LongAdder counter = new LongAdder();
171 ThreadLocalRandom r = ThreadLocalRandom.current();
172 long size = 100;
173 r.ints().limit(size).parallel().forEach(x -> counter.increment());
174 assertEquals(size, counter.sum());
175 }
176
177 /**
178 * A parallel unsized stream of longs generates at least 100 values
179 */
180 public void testUnsizedLongsCount() {
181 LongAdder counter = new LongAdder();
182 ThreadLocalRandom r = ThreadLocalRandom.current();
183 long size = 100;
184 r.longs().limit(size).parallel().forEach(x -> counter.increment());
185 assertEquals(size, counter.sum());
186 }
187
188 /**
189 * A parallel unsized stream of doubles generates at least 100 values
190 */
191 public void testUnsizedDoublesCount() {
192 LongAdder counter = new LongAdder();
193 ThreadLocalRandom r = ThreadLocalRandom.current();
194 long size = 100;
195 r.doubles().limit(size).parallel().forEach(x -> counter.increment());
196 assertEquals(size, counter.sum());
197 }
198
199 /**
200 * A sequential unsized stream of ints generates at least 100 values
201 */
202 public void testUnsizedIntsCountSeq() {
203 LongAdder counter = new LongAdder();
204 ThreadLocalRandom r = ThreadLocalRandom.current();
205 long size = 100;
206 r.ints().limit(size).forEach(x -> counter.increment());
207 assertEquals(size, counter.sum());
208 }
209
210 /**
211 * A sequential unsized stream of longs generates at least 100 values
212 */
213 public void testUnsizedLongsCountSeq() {
214 LongAdder counter = new LongAdder();
215 ThreadLocalRandom r = ThreadLocalRandom.current();
216 long size = 100;
217 r.longs().limit(size).forEach(x -> counter.increment());
218 assertEquals(size, counter.sum());
219 }
220
221 /**
222 * A sequential unsized stream of doubles generates at least 100 values
223 */
224 public void testUnsizedDoublesCountSeq() {
225 LongAdder counter = new LongAdder();
226 ThreadLocalRandom r = ThreadLocalRandom.current();
227 long size = 100;
228 r.doubles().limit(size).forEach(x -> counter.increment());
229 assertEquals(size, counter.sum());
230 }
231
232 }