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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8