ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandomTest.java
Revision: 1.2
Committed: Fri Jul 31 23:37:31 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +13 -13 lines
Log Message:
whitespace

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 } catch (UnsupportedOperationException success) {
45 }
46 }
47
48 /**
49 * Repeated calls to nextInt produce at least one different result
50 */
51 public void testNextInt() {
52 int f = ThreadLocalRandom.current().nextInt();
53 int i = 0;
54 while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f)
55 ++i;
56 assertTrue(i < NCALLS);
57 }
58
59 /**
60 * Repeated calls to nextLong produce at least one different result
61 */
62 public void testNextLong() {
63 long f = ThreadLocalRandom.current().nextLong();
64 int i = 0;
65 while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f)
66 ++i;
67 assertTrue(i < NCALLS);
68 }
69
70
71 /**
72 * Repeated calls to nextBoolean produce at least one different result
73 */
74 public void testNextBoolean() {
75 boolean f = ThreadLocalRandom.current().nextBoolean();
76 int i = 0;
77 while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f)
78 ++i;
79 assertTrue(i < NCALLS);
80 }
81
82 /**
83 * Repeated calls to nextFloat produce at least one different result
84 */
85 public void testNextFloat() {
86 float f = ThreadLocalRandom.current().nextFloat();
87 int i = 0;
88 while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f)
89 ++i;
90 assertTrue(i < NCALLS);
91 }
92
93 /**
94 * Repeated calls to nextDouble produce at least one different result
95 */
96 public void testNextDouble() {
97 double f = ThreadLocalRandom.current().nextDouble();
98 double i = 0;
99 while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
100 ++i;
101 assertTrue(i < NCALLS);
102 }
103
104 /**
105 * Repeated calls to nextGaussian produce at least one different result
106 */
107 public void testNextGaussian() {
108 double f = ThreadLocalRandom.current().nextGaussian();
109 int i = 0;
110 while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f)
111 ++i;
112 assertTrue(i < NCALLS);
113 }
114
115
116 /**
117 * nextInt(negative) throws IllegalArgumentException;
118 */
119 public void testNextIntBoundedNeg() {
120 try {
121 int f = ThreadLocalRandom.current().nextInt(-17);
122 } catch (IllegalArgumentException success) {
123 }
124 }
125
126 /**
127 * nextInt(least >= bound) throws IllegalArgumentException;
128 */
129 public void testNextIntBadBounds() {
130 try {
131 int f = ThreadLocalRandom.current().nextInt(17, 2);
132 } catch (IllegalArgumentException success) {
133 }
134 }
135
136
137 /**
138 * nextInt(bound) returns 0 <= value < bound;
139 * repeated calls produce at least one different result
140 */
141 public void testNextIntBounded() {
142 // sample bound space across prime number increments
143 for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
144 int f = ThreadLocalRandom.current().nextInt(bound);
145 assertTrue(0 <= f && f < bound);
146 int i = 0;
147 int j;
148 while (i < NCALLS &&
149 (j = ThreadLocalRandom.current().nextInt(bound)) == f) {
150 assertTrue(0 <= j && j < bound);
151 ++i;
152 }
153 assertTrue(i < NCALLS);
154 }
155 }
156
157
158 /**
159 * nextInt(least, bound) returns least <= value < bound;
160 * repeated calls produce at least one different result
161 */
162 public void testNextIntBounded2() {
163 for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
164 for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
165 int f = ThreadLocalRandom.current().nextInt(least, bound);
166 assertTrue(least <= f && f < bound);
167 int i = 0;
168 int j;
169 while (i < NCALLS &&
170 (j = ThreadLocalRandom.current().nextInt(least, bound)) == f) {
171 assertTrue(least <= j && j < bound);
172 ++i;
173 }
174 assertTrue(i < NCALLS);
175 }
176 }
177 }
178
179 /**
180 * nextLong(negative) throws IllegalArgumentException;
181 */
182 public void testNextLongBoundedNeg() {
183 try {
184 long f = ThreadLocalRandom.current().nextLong(-17);
185 } catch (IllegalArgumentException success) {
186 }
187 }
188
189 /**
190 * nextLong(least >= bound) throws IllegalArgumentException;
191 */
192 public void testNextLongBadBounds() {
193 try {
194 long f = ThreadLocalRandom.current().nextLong(17, 2);
195 } catch (IllegalArgumentException success) {
196 }
197 }
198
199 /**
200 * nextLong(bound) returns 0 <= value < bound;
201 * repeated calls produce at least one different result
202 */
203 public void testNextLongBounded() {
204 for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
205 long f = ThreadLocalRandom.current().nextLong(bound);
206 assertTrue(0 <= f && f < bound);
207 int i = 0;
208 long j;
209 while (i < NCALLS &&
210 (j = ThreadLocalRandom.current().nextLong(bound)) == f) {
211 assertTrue(0 <= j && j < bound);
212 ++i;
213 }
214 assertTrue(i < NCALLS);
215 }
216 }
217
218 /**
219 * nextLong(least, bound) returns least <= value < bound;
220 * repeated calls produce at least one different result
221 */
222 public void testNextLongBounded2() {
223 for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
224 for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
225 long f = ThreadLocalRandom.current().nextLong(least, bound);
226 assertTrue(least <= f && f < bound);
227 int i = 0;
228 long j;
229 while (i < NCALLS &&
230 (j = ThreadLocalRandom.current().nextLong(least, bound)) == f) {
231 assertTrue(least <= j && j < bound);
232 ++i;
233 }
234 assertTrue(i < NCALLS);
235 }
236 }
237 }
238
239
240 /**
241 * nextDouble(least, bound) returns least <= value < bound;
242 * repeated calls produce at least one different result
243 */
244 public void testNextDoubleBounded2() {
245 for (double least = 0.0001; least < 1.0e20; least *= 8) {
246 for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
247 double f = ThreadLocalRandom.current().nextDouble(least, bound);
248 assertTrue(least <= f && f < bound);
249 int i = 0;
250 double j;
251 while (i < NCALLS &&
252 (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
253 assertTrue(least <= j && j < bound);
254 ++i;
255 }
256 assertTrue(i < NCALLS);
257 }
258 }
259 }
260
261
262 }