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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8