[cvs] / jsr166 / src / test / tck / TimeUnitTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/TimeUnitTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (view) (download)

1 : dl 1.1 /*
2 : dl 1.6 * 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 : jsr166 1.19 * http://creativecommons.org/publicdomain/zero/1.0/
5 : jsr166 1.10 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 :     * Pat Fisher, Mike Judd.
7 : dl 1.1 */
8 :    
9 :     import junit.framework.*;
10 : jsr166 1.22 import java.util.concurrent.CountDownLatch;
11 :     import java.util.concurrent.TimeUnit;
12 : jsr166 1.23 import static java.util.concurrent.TimeUnit.*;
13 : dl 1.1
14 : dl 1.3 public class TimeUnitTest extends JSR166TestCase {
15 : dl 1.1 public static void main(String[] args) {
16 : jsr166 1.14 junit.textui.TestRunner.run(suite());
17 : dl 1.1 }
18 : jsr166 1.10
19 : dl 1.1 public static Test suite() {
20 : jsr166 1.14 return new TestSuite(TimeUnitTest.class);
21 : dl 1.1 }
22 :    
23 : dl 1.9 // (loops to 88888 check increments at all time divisions.)
24 :    
25 : dl 1.4 /**
26 : dl 1.9 * convert correctly converts sample values across the units
27 : dl 1.4 */
28 : dl 1.1 public void testConvert() {
29 : dl 1.9 for (long t = 0; t < 88888; ++t) {
30 : jsr166 1.10 assertEquals(t*60*60*24,
31 : jsr166 1.23 SECONDS.convert(t, DAYS));
32 : jsr166 1.10 assertEquals(t*60*60,
33 : jsr166 1.23 SECONDS.convert(t, HOURS));
34 : jsr166 1.10 assertEquals(t*60,
35 : jsr166 1.23 SECONDS.convert(t, MINUTES));
36 : jsr166 1.10 assertEquals(t,
37 : jsr166 1.23 SECONDS.convert(t, SECONDS));
38 : jsr166 1.10 assertEquals(t,
39 : jsr166 1.23 SECONDS.convert(1000L*t, MILLISECONDS));
40 : jsr166 1.10 assertEquals(t,
41 : jsr166 1.23 SECONDS.convert(1000000L*t, MICROSECONDS));
42 : jsr166 1.10 assertEquals(t,
43 : jsr166 1.23 SECONDS.convert(1000000000L*t, NANOSECONDS));
44 : dl 1.9
45 : jsr166 1.10 assertEquals(1000L*t*60*60*24,
46 : jsr166 1.23 MILLISECONDS.convert(t, DAYS));
47 : jsr166 1.10 assertEquals(1000L*t*60*60,
48 : jsr166 1.23 MILLISECONDS.convert(t, HOURS));
49 : jsr166 1.10 assertEquals(1000L*t*60,
50 : jsr166 1.23 MILLISECONDS.convert(t, MINUTES));
51 : jsr166 1.10 assertEquals(1000L*t,
52 : jsr166 1.23 MILLISECONDS.convert(t, SECONDS));
53 : jsr166 1.10 assertEquals(t,
54 : jsr166 1.23 MILLISECONDS.convert(t, MILLISECONDS));
55 : jsr166 1.10 assertEquals(t,
56 : jsr166 1.23 MILLISECONDS.convert(1000L*t, MICROSECONDS));
57 : jsr166 1.10 assertEquals(t,
58 : jsr166 1.23 MILLISECONDS.convert(1000000L*t, NANOSECONDS));
59 : dl 1.9
60 : jsr166 1.10 assertEquals(1000000L*t*60*60*24,
61 : jsr166 1.23 MICROSECONDS.convert(t, DAYS));
62 : jsr166 1.10 assertEquals(1000000L*t*60*60,
63 : jsr166 1.23 MICROSECONDS.convert(t, HOURS));
64 : jsr166 1.10 assertEquals(1000000L*t*60,
65 : jsr166 1.23 MICROSECONDS.convert(t, MINUTES));
66 : jsr166 1.10 assertEquals(1000000L*t,
67 : jsr166 1.23 MICROSECONDS.convert(t, SECONDS));
68 : jsr166 1.10 assertEquals(1000L*t,
69 : jsr166 1.23 MICROSECONDS.convert(t, MILLISECONDS));
70 : jsr166 1.10 assertEquals(t,
71 : jsr166 1.23 MICROSECONDS.convert(t, MICROSECONDS));
72 : jsr166 1.10 assertEquals(t,
73 : jsr166 1.23 MICROSECONDS.convert(1000L*t, NANOSECONDS));
74 : dl 1.9
75 : jsr166 1.10 assertEquals(1000000000L*t*60*60*24,
76 : jsr166 1.23 NANOSECONDS.convert(t, DAYS));
77 : jsr166 1.10 assertEquals(1000000000L*t*60*60,
78 : jsr166 1.23 NANOSECONDS.convert(t, HOURS));
79 : jsr166 1.10 assertEquals(1000000000L*t*60,
80 : jsr166 1.23 NANOSECONDS.convert(t, MINUTES));
81 : jsr166 1.10 assertEquals(1000000000L*t,
82 : jsr166 1.23 NANOSECONDS.convert(t, SECONDS));
83 : jsr166 1.10 assertEquals(1000000L*t,
84 : jsr166 1.23 NANOSECONDS.convert(t, MILLISECONDS));
85 : jsr166 1.10 assertEquals(1000L*t,
86 : jsr166 1.23 NANOSECONDS.convert(t, MICROSECONDS));
87 : jsr166 1.10 assertEquals(t,
88 : jsr166 1.23 NANOSECONDS.convert(t, NANOSECONDS));
89 : dl 1.1 }
90 :     }
91 :    
92 : dl 1.4 /**
93 : dl 1.5 * toNanos correctly converts sample values in different units to
94 :     * nanoseconds
95 : dl 1.4 */
96 : dl 1.1 public void testToNanos() {
97 : dl 1.9 for (long t = 0; t < 88888; ++t) {
98 : jsr166 1.10 assertEquals(t*1000000000L*60*60*24,
99 : jsr166 1.23 DAYS.toNanos(t));
100 : jsr166 1.10 assertEquals(t*1000000000L*60*60,
101 : jsr166 1.23 HOURS.toNanos(t));
102 : jsr166 1.10 assertEquals(t*1000000000L*60,
103 : jsr166 1.23 MINUTES.toNanos(t));
104 : jsr166 1.10 assertEquals(1000000000L*t,
105 : jsr166 1.23 SECONDS.toNanos(t));
106 : jsr166 1.10 assertEquals(1000000L*t,
107 : jsr166 1.23 MILLISECONDS.toNanos(t));
108 : jsr166 1.10 assertEquals(1000L*t,
109 : jsr166 1.23 MICROSECONDS.toNanos(t));
110 : jsr166 1.10 assertEquals(t,
111 : jsr166 1.23 NANOSECONDS.toNanos(t));
112 : dl 1.3 }
113 :     }
114 :    
115 : dl 1.4 /**
116 : dl 1.5 * toMicros correctly converts sample values in different units to
117 :     * microseconds
118 : dl 1.4 */
119 : dl 1.3 public void testToMicros() {
120 : dl 1.9 for (long t = 0; t < 88888; ++t) {
121 : jsr166 1.10 assertEquals(t*1000000L*60*60*24,
122 : jsr166 1.23 DAYS.toMicros(t));
123 : jsr166 1.10 assertEquals(t*1000000L*60*60,
124 : jsr166 1.23 HOURS.toMicros(t));
125 : jsr166 1.10 assertEquals(t*1000000L*60,
126 : jsr166 1.23 MINUTES.toMicros(t));
127 : jsr166 1.10 assertEquals(1000000L*t,
128 : jsr166 1.23 SECONDS.toMicros(t));
129 : jsr166 1.10 assertEquals(1000L*t,
130 : jsr166 1.23 MILLISECONDS.toMicros(t));
131 : jsr166 1.10 assertEquals(t,
132 : jsr166 1.23 MICROSECONDS.toMicros(t));
133 : jsr166 1.10 assertEquals(t,
134 : jsr166 1.23 NANOSECONDS.toMicros(t*1000L));
135 : dl 1.3 }
136 :     }
137 :    
138 : dl 1.4 /**
139 : dl 1.5 * toMillis correctly converts sample values in different units to
140 :     * milliseconds
141 : dl 1.4 */
142 : dl 1.3 public void testToMillis() {
143 : dl 1.9 for (long t = 0; t < 88888; ++t) {
144 : jsr166 1.10 assertEquals(t*1000L*60*60*24,
145 : jsr166 1.23 DAYS.toMillis(t));
146 : jsr166 1.10 assertEquals(t*1000L*60*60,
147 : jsr166 1.23 HOURS.toMillis(t));
148 : jsr166 1.10 assertEquals(t*1000L*60,
149 : jsr166 1.23 MINUTES.toMillis(t));
150 : jsr166 1.10 assertEquals(1000L*t,
151 : jsr166 1.23 SECONDS.toMillis(t));
152 : jsr166 1.10 assertEquals(t,
153 : jsr166 1.23 MILLISECONDS.toMillis(t));
154 : jsr166 1.10 assertEquals(t,
155 : jsr166 1.23 MICROSECONDS.toMillis(t*1000L));
156 : jsr166 1.10 assertEquals(t,
157 : jsr166 1.23 NANOSECONDS.toMillis(t*1000000L));
158 : dl 1.3 }
159 :     }
160 :    
161 : dl 1.4 /**
162 : dl 1.5 * toSeconds correctly converts sample values in different units to
163 :     * seconds
164 : dl 1.4 */
165 : dl 1.3 public void testToSeconds() {
166 : dl 1.9 for (long t = 0; t < 88888; ++t) {
167 : jsr166 1.10 assertEquals(t*60*60*24,
168 : jsr166 1.23 DAYS.toSeconds(t));
169 : jsr166 1.10 assertEquals(t*60*60,
170 : jsr166 1.23 HOURS.toSeconds(t));
171 : jsr166 1.10 assertEquals(t*60,
172 : jsr166 1.23 MINUTES.toSeconds(t));
173 : jsr166 1.10 assertEquals(t,
174 : jsr166 1.23 SECONDS.toSeconds(t));
175 : jsr166 1.10 assertEquals(t,
176 : jsr166 1.23 MILLISECONDS.toSeconds(t*1000L));
177 : jsr166 1.10 assertEquals(t,
178 : jsr166 1.23 MICROSECONDS.toSeconds(t*1000000L));
179 : jsr166 1.10 assertEquals(t,
180 : jsr166 1.23 NANOSECONDS.toSeconds(t*1000000000L));
181 : dl 1.9 }
182 :     }
183 : dl 1.3
184 : dl 1.9 /**
185 :     * toMinutes correctly converts sample values in different units to
186 :     * minutes
187 :     */
188 :     public void testToMinutes() {
189 :     for (long t = 0; t < 88888; ++t) {
190 : jsr166 1.10 assertEquals(t*60*24,
191 : jsr166 1.23 DAYS.toMinutes(t));
192 : jsr166 1.10 assertEquals(t*60,
193 : jsr166 1.23 HOURS.toMinutes(t));
194 : jsr166 1.10 assertEquals(t,
195 : jsr166 1.23 MINUTES.toMinutes(t));
196 : jsr166 1.10 assertEquals(t,
197 : jsr166 1.23 SECONDS.toMinutes(t*60));
198 : jsr166 1.10 assertEquals(t,
199 : jsr166 1.23 MILLISECONDS.toMinutes(t*1000L*60));
200 : jsr166 1.10 assertEquals(t,
201 : jsr166 1.23 MICROSECONDS.toMinutes(t*1000000L*60));
202 : jsr166 1.10 assertEquals(t,
203 : jsr166 1.23 NANOSECONDS.toMinutes(t*1000000000L*60));
204 : dl 1.9 }
205 :     }
206 :    
207 :     /**
208 :     * toHours correctly converts sample values in different units to
209 :     * hours
210 :     */
211 :     public void testToHours() {
212 :     for (long t = 0; t < 88888; ++t) {
213 : jsr166 1.10 assertEquals(t*24,
214 : jsr166 1.23 DAYS.toHours(t));
215 : jsr166 1.10 assertEquals(t,
216 : jsr166 1.23 HOURS.toHours(t));
217 : jsr166 1.10 assertEquals(t,
218 : jsr166 1.23 MINUTES.toHours(t*60));
219 : jsr166 1.10 assertEquals(t,
220 : jsr166 1.23 SECONDS.toHours(t*60*60));
221 : jsr166 1.10 assertEquals(t,
222 : jsr166 1.23 MILLISECONDS.toHours(t*1000L*60*60));
223 : jsr166 1.10 assertEquals(t,
224 : jsr166 1.23 MICROSECONDS.toHours(t*1000000L*60*60));
225 : jsr166 1.10 assertEquals(t,
226 : jsr166 1.23 NANOSECONDS.toHours(t*1000000000L*60*60));
227 : dl 1.9 }
228 :     }
229 :    
230 :     /**
231 :     * toDays correctly converts sample values in different units to
232 :     * days
233 :     */
234 :     public void testToDays() {
235 :     for (long t = 0; t < 88888; ++t) {
236 : jsr166 1.10 assertEquals(t,
237 : jsr166 1.23 DAYS.toDays(t));
238 : jsr166 1.10 assertEquals(t,
239 : jsr166 1.23 HOURS.toDays(t*24));
240 : jsr166 1.10 assertEquals(t,
241 : jsr166 1.23 MINUTES.toDays(t*60*24));
242 : jsr166 1.10 assertEquals(t,
243 : jsr166 1.23 SECONDS.toDays(t*60*60*24));
244 : jsr166 1.10 assertEquals(t,
245 : jsr166 1.23 MILLISECONDS.toDays(t*1000L*60*60*24));
246 : jsr166 1.10 assertEquals(t,
247 : jsr166 1.23 MICROSECONDS.toDays(t*1000000L*60*60*24));
248 : jsr166 1.10 assertEquals(t,
249 : jsr166 1.23 NANOSECONDS.toDays(t*1000000000L*60*60*24));
250 : dl 1.1 }
251 :     }
252 :    
253 : dl 1.4 /**
254 : jsr166 1.10 * convert saturates positive too-large values to Long.MAX_VALUE
255 : dl 1.5 * and negative to LONG.MIN_VALUE
256 : dl 1.4 */
257 : dl 1.1 public void testConvertSaturate() {
258 :     assertEquals(Long.MAX_VALUE,
259 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
260 : dl 1.1 assertEquals(Long.MIN_VALUE,
261 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
262 : dl 1.9 assertEquals(Long.MAX_VALUE,
263 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
264 : dl 1.9 assertEquals(Long.MIN_VALUE,
265 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
266 : dl 1.9 assertEquals(Long.MAX_VALUE,
267 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
268 : dl 1.9 assertEquals(Long.MIN_VALUE,
269 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
270 : dl 1.9 assertEquals(Long.MAX_VALUE,
271 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
272 : dl 1.9 assertEquals(Long.MIN_VALUE,
273 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
274 : dl 1.1 }
275 :    
276 : dl 1.4 /**
277 : jsr166 1.10 * toNanos saturates positive too-large values to Long.MAX_VALUE
278 : dl 1.5 * and negative to LONG.MIN_VALUE
279 : dl 1.4 */
280 : dl 1.1 public void testToNanosSaturate() {
281 : jsr166 1.13 assertEquals(Long.MAX_VALUE,
282 : jsr166 1.23 MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
283 : jsr166 1.13 assertEquals(Long.MIN_VALUE,
284 : jsr166 1.23 MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
285 : dl 1.1 }
286 :    
287 : dl 1.4 /**
288 : jsr166 1.20 * toString returns name of unit
289 : dl 1.4 */
290 : dl 1.1 public void testToString() {
291 : jsr166 1.23 assertEquals("SECONDS", SECONDS.toString());
292 : dl 1.1 }
293 :    
294 : jsr166 1.20 /**
295 :     * name returns name of unit
296 :     */
297 :     public void testName() {
298 : jsr166 1.23 assertEquals("SECONDS", SECONDS.name());
299 : jsr166 1.20 }
300 : jsr166 1.10
301 : dl 1.1 /**
302 : jsr166 1.17 * Timed wait without holding lock throws
303 :     * IllegalMonitorStateException
304 : dl 1.1 */
305 : jsr166 1.21 public void testTimedWait_IllegalMonitorException() {
306 :     Thread t = newStartedThread(new CheckedRunnable() {
307 : jsr166 1.13 public void realRun() throws InterruptedException {
308 :     Object o = new Object();
309 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
310 : jsr166 1.21
311 : jsr166 1.13 try {
312 : jsr166 1.21 tu.timedWait(o, LONG_DELAY_MS);
313 : jsr166 1.13 threadShouldThrow();
314 : jsr166 1.21 } catch (IllegalMonitorStateException success) {}
315 :     }});
316 : dl 1.1
317 : jsr166 1.21 awaitTermination(t);
318 : dl 1.1 }
319 : jsr166 1.10
320 : dl 1.1 /**
321 : dl 1.5 * timedWait throws InterruptedException when interrupted
322 : dl 1.4 */
323 : jsr166 1.21 public void testTimedWait_Interruptible() {
324 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
325 :     Thread t = newStartedThread(new CheckedRunnable() {
326 : jsr166 1.14 public void realRun() throws InterruptedException {
327 : jsr166 1.13 Object o = new Object();
328 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
329 : jsr166 1.13
330 : jsr166 1.21 Thread.currentThread().interrupt();
331 :     try {
332 :     synchronized (o) {
333 :     tu.timedWait(o, LONG_DELAY_MS);
334 :     }
335 :     shouldThrow();
336 :     } catch (InterruptedException success) {}
337 :     assertFalse(Thread.interrupted());
338 :    
339 :     pleaseInterrupt.countDown();
340 :     try {
341 :     synchronized (o) {
342 :     tu.timedWait(o, LONG_DELAY_MS);
343 :     }
344 :     shouldThrow();
345 :     } catch (InterruptedException success) {}
346 :     assertFalse(Thread.interrupted());
347 : jsr166 1.13 }});
348 : jsr166 1.21
349 :     await(pleaseInterrupt);
350 :     assertThreadStaysAlive(t);
351 : jsr166 1.13 t.interrupt();
352 : jsr166 1.21 awaitTermination(t);
353 : dl 1.1 }
354 : jsr166 1.10
355 : dl 1.1 /**
356 : dl 1.5 * timedJoin throws InterruptedException when interrupted
357 : dl 1.4 */
358 : jsr166 1.21 public void testTimedJoin_Interruptible() {
359 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
360 :     final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
361 : jsr166 1.13 public void realRun() throws InterruptedException {
362 : jsr166 1.21 Thread.sleep(LONG_DELAY_MS);
363 : jsr166 1.13 }});
364 : jsr166 1.21 final Thread t = newStartedThread(new CheckedRunnable() {
365 : jsr166 1.14 public void realRun() throws InterruptedException {
366 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
367 : jsr166 1.21 Thread.currentThread().interrupt();
368 :     try {
369 :     tu.timedJoin(s, LONG_DELAY_MS);
370 :     shouldThrow();
371 :     } catch (InterruptedException success) {}
372 :     assertFalse(Thread.interrupted());
373 :    
374 :     pleaseInterrupt.countDown();
375 :     try {
376 :     tu.timedJoin(s, LONG_DELAY_MS);
377 :     shouldThrow();
378 :     } catch (InterruptedException success) {}
379 :     assertFalse(Thread.interrupted());
380 : jsr166 1.18 }});
381 : jsr166 1.21
382 :     await(pleaseInterrupt);
383 :     assertThreadStaysAlive(t);
384 : jsr166 1.13 t.interrupt();
385 : jsr166 1.21 awaitTermination(t);
386 : jsr166 1.13 s.interrupt();
387 : jsr166 1.21 awaitTermination(s);
388 : dl 1.1 }
389 : jsr166 1.10
390 : dl 1.1 /**
391 : jsr166 1.17 * timedSleep throws InterruptedException when interrupted
392 : dl 1.4 */
393 : jsr166 1.21 public void testTimedSleep_Interruptible() {
394 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
395 :     Thread t = newStartedThread(new CheckedRunnable() {
396 : jsr166 1.14 public void realRun() throws InterruptedException {
397 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
398 : jsr166 1.21 Thread.currentThread().interrupt();
399 :     try {
400 :     tu.sleep(LONG_DELAY_MS);
401 :     shouldThrow();
402 :     } catch (InterruptedException success) {}
403 :     assertFalse(Thread.interrupted());
404 :    
405 :     pleaseInterrupt.countDown();
406 :     try {
407 :     tu.sleep(LONG_DELAY_MS);
408 :     shouldThrow();
409 :     } catch (InterruptedException success) {}
410 :     assertFalse(Thread.interrupted());
411 : jsr166 1.13 }});
412 : dl 1.1
413 : jsr166 1.21 await(pleaseInterrupt);
414 :     assertThreadStaysAlive(t);
415 : jsr166 1.13 t.interrupt();
416 : jsr166 1.21 awaitTermination(t);
417 : dl 1.1 }
418 : dl 1.2
419 : dl 1.4 /**
420 : jsr166 1.13 * a deserialized serialized unit is the same instance
421 : dl 1.4 */
422 : jsr166 1.13 public void testSerialization() throws Exception {
423 : jsr166 1.23 TimeUnit x = MILLISECONDS;
424 : jsr166 1.22 assertSame(x, serialClone(x));
425 : dl 1.2 }
426 :    
427 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8