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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8