[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.17 - (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.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 :     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 : dl 1.1 }
313 :    
314 : dl 1.4 /**
315 : jsr166 1.10 * toNanos saturates positive too-large values to Long.MAX_VALUE
316 : dl 1.5 * and negative to LONG.MIN_VALUE
317 : dl 1.4 */
318 : dl 1.1 public void testToNanosSaturate() {
319 : jsr166 1.13 assertEquals(Long.MAX_VALUE,
320 :     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
321 :     assertEquals(Long.MIN_VALUE,
322 :     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
323 : dl 1.1 }
324 :    
325 :    
326 : dl 1.4 /**
327 : dl 1.7 * toString returns string containing common name of unit
328 : dl 1.4 */
329 : dl 1.1 public void testToString() {
330 :     String s = TimeUnit.SECONDS.toString();
331 : dl 1.8 assertTrue(s.indexOf("ECOND") >= 0);
332 : dl 1.1 }
333 :    
334 : jsr166 1.10
335 : dl 1.1 /**
336 : jsr166 1.17 * Timed wait without holding lock throws
337 :     * IllegalMonitorStateException
338 : dl 1.1 */
339 : jsr166 1.13 public void testTimedWait_IllegalMonitorException() throws Exception {
340 :     Thread t = new Thread(new CheckedRunnable() {
341 :     public void realRun() throws InterruptedException {
342 :     Object o = new Object();
343 :     TimeUnit tu = TimeUnit.MILLISECONDS;
344 :     try {
345 :     tu.timedWait(o,LONG_DELAY_MS);
346 :     threadShouldThrow();
347 :     } catch (IllegalMonitorStateException success) {}}});
348 : dl 1.1
349 :     t.start();
350 : jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
351 :     t.interrupt();
352 :     t.join();
353 : dl 1.1 }
354 : jsr166 1.10
355 : dl 1.1 /**
356 : dl 1.5 * timedWait throws InterruptedException when interrupted
357 : dl 1.4 */
358 : jsr166 1.13 public void testTimedWait() throws InterruptedException {
359 : jsr166 1.14 Thread t = new Thread(new CheckedInterruptedRunnable() {
360 :     public void realRun() throws InterruptedException {
361 : jsr166 1.13 Object o = new Object();
362 :    
363 :     TimeUnit tu = TimeUnit.MILLISECONDS;
364 : jsr166 1.16 synchronized (o) {
365 : jsr166 1.13 tu.timedWait(o,MEDIUM_DELAY_MS);
366 :     }
367 :     }});
368 : jsr166 1.14 t.start();
369 : jsr166 1.13 Thread.sleep(SHORT_DELAY_MS);
370 :     t.interrupt();
371 :     t.join();
372 : dl 1.1 }
373 : jsr166 1.10
374 :    
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 :     }});;
388 :     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