ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandomTest.java
Revision: 1.3
Committed: Tue Aug 4 10:04:47 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +5 -0 lines
Log Message:
add missing shouldThrow()

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/licenses/publicdomain
5 */
6 import junit.framework.*;
7 import java.util.concurrent.*;
8 import java.util.*;
9
10
11 public class ThreadLocalRandomTest extends JSR166TestCase {
12
13 public static void main(String[] args) {
14 junit.textui.TestRunner.run (suite());
15 }
16 public static Test suite() {
17 return new TestSuite(ThreadLocalRandomTest.class);
18 }
19
20 /**
21 * Testing coverage notes:
22 *
23 * We don't test randomness properties, but only that repeated
24 * calls, up to NCALLS tries, produce at least one different
25 * result. For bounded versions, we sample various intervals
26 * across multiples of primes.
27 */
28
29 //
30 static final int NCALLS = 10000;
31
32 // max sampled int bound
33 static final int MAX_INT_BOUND = (1 << 28);
34
35 // Max sampled long bound
36 static final long MAX_LONG_BOUND = (1L << 42);
37
38 /**
39 * setSeed throws UnsupportedOperationException
40 */
41 public void testSetSeed() {
42 try {
43 ThreadLocalRandom.current().setSeed(17);
44 shouldThrow();
45 } catch (UnsupportedOperationException success) {
46 }
47 }
48
49 /**
50 * Repeated calls to nextInt produce at least one different result
51 */
52 public void testNextInt() {
53 int f = ThreadLocalRandom.current().nextInt();
54 int i = 0;
55 while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f)
56 ++i;
57 assertTrue(i < NCALLS);
58 }
59
60 /**
61 * Repeated calls to nextLong produce at least one different result
62 */
63 public void testNextLong() {
64 long f = ThreadLocalRandom.current().nextLong();
65 int i = 0;
66 while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f)
67 ++i;
68 assertTrue(i < NCALLS);
69 }
70
71
72 /**
73 * Repeated calls to nextBoolean produce at least one different result
74 */
75 public void testNextBoolean() {
76 boolean f = ThreadLocalRandom.current().nextBoolean();
77 int i = 0;
78 while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f)
79 ++i;
80 assertTrue(i < NCALLS);
81 }
82
83 /**
84 * Repeated calls to nextFloat produce at least one different result
85 */
86 public void testNextFloat() {
87 float f = ThreadLocalRandom.current().nextFloat();
88 int i = 0;
89 while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f)
90 ++i;
91 assertTrue(i < NCALLS);
92 }
93
94 /**
95 * Repeated calls to nextDouble produce at least one different result
96 */
97 public void testNextDouble() {
98 double f = ThreadLocalRandom.current().nextDouble();
99 double i = 0;
100 while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
101 ++i;
102 assertTrue(i < NCALLS);
103 }
104
105 /**
106 * Repeated calls to nextGaussian produce at least one different result
107 */
108 public void testNextGaussian() {
109 double f = ThreadLocalRandom.current().nextGaussian();
110 int i = 0;
111 while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f)
112 ++i;
113 assertTrue(i < NCALLS);
114 }
115
116
117 /**
118 * nextInt(negative) throws IllegalArgumentException;
119 */
120 public void testNextIntBoundedNeg() {
121 try {
122 int f = ThreadLocalRandom.current().nextInt(-17);
123 shouldThrow();
124 } catch (IllegalArgumentException success) {
125 }
126 }
127
128 /**
129 * nextInt(least >= bound) throws IllegalArgumentException;
130 */
131 public void testNextIntBadBounds() {
132 try {
133 int f = ThreadLocalRandom.current().nextInt(17, 2);
134 shouldThrow();
135 } catch (IllegalArgumentException success) {
136 }
137 }
138
139
140 /**
141 * nextInt(bound) returns 0 <= value < bound;
142 * repeated calls produce at least one different result
143 */
144 public void testNextIntBounded() {
145 // sample bound space across prime number increments
146 for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
147 int f = ThreadLocalRandom.current().nextInt(bound);
148 assertTrue(0 <= f && f < bound);
149 int i = 0;
150 int j;
151 while (i < NCALLS &&
152 (j = ThreadLocalRandom.current().nextInt(bound)) == f) {
153 assertTrue(0 <= j && j < bound);
154 ++i;
155 }
156 assertTrue(i < NCALLS);
157 }
158 }
159
160
161 /**
162 * nextInt(least, bound) returns least <= value < bound;
163 * repeated calls produce at least one different result
164 */
165 public void testNextIntBounded2() {
166 for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
167 for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
168 int f = ThreadLocalRandom.current().nextInt(least, bound);
169 assertTrue(least <= f && f < bound);
170 int i = 0;
171 int j;
172 while (i < NCALLS &&
173 (j = ThreadLocalRandom.current().nextInt(least, bound)) == f) {
174 assertTrue(least <= j && j < bound);
175 ++i;
176 }
177 assertTrue(i < NCALLS);
178 }
179 }
180 }
181
182 /**
183 * nextLong(negative) throws IllegalArgumentException;
184 */
185 public void testNextLongBoundedNeg() {
186 try {
187 long f = ThreadLocalRandom.current().nextLong(-17);
188 shouldThrow();
189 } catch (IllegalArgumentException success) {
190 }
191 }
192
193 /**
194 * nextLong(least >= bound) throws IllegalArgumentException;
195 */
196 public void testNextLongBadBounds() {
197 try {
198 long f = ThreadLocalRandom.current().nextLong(17, 2);
199 shouldThrow();
200 } catch (IllegalArgumentException success) {
201 }
202 }
203
204 /**
205 * nextLong(bound) returns 0 <= value < bound;
206 * repeated calls produce at least one different result
207 */
208 public void testNextLongBounded() {
209 for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
210 long f = ThreadLocalRandom.current().nextLong(bound);
211 assertTrue(0 <= f && f < bound);
212 int i = 0;
213 long j;
214 while (i < NCALLS &&
215 (j = ThreadLocalRandom.current().nextLong(bound)) == f) {
216 assertTrue(0 <= j && j < bound);
217 ++i;
218 }
219 assertTrue(i < NCALLS);
220 }
221 }
222
223 /**
224 * nextLong(least, bound) returns least <= value < bound;
225 * repeated calls produce at least one different result
226 */
227 public void testNextLongBounded2() {
228 for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
229 for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
230 long f = ThreadLocalRandom.current().nextLong(least, bound);
231 assertTrue(least <= f && f < bound);
232 int i = 0;
233 long j;
234 while (i < NCALLS &&
235 (j = ThreadLocalRandom.current().nextLong(least, bound)) == f) {
236 assertTrue(least <= j && j < bound);
237 ++i;
238 }
239 assertTrue(i < NCALLS);
240 }
241 }
242 }
243
244
245 /**
246 * nextDouble(least, bound) returns least <= value < bound;
247 * repeated calls produce at least one different result
248 */
249 public void testNextDoubleBounded2() {
250 for (double least = 0.0001; least < 1.0e20; least *= 8) {
251 for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
252 double f = ThreadLocalRandom.current().nextDouble(least, bound);
253 assertTrue(least <= f && f < bound);
254 int i = 0;
255 double j;
256 while (i < NCALLS &&
257 (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
258 assertTrue(least <= j && j < bound);
259 ++i;
260 }
261 assertTrue(i < NCALLS);
262 }
263 }
264 }
265
266
267 }