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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (show annotations)
Tue May 31 16:16:24 2011 UTC (8 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +4 -4 lines
use serialClone in serialization tests; update imports

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

dl@cs.oswego.edu
ViewVC Help
Powered by ViewVC 1.1.27