ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.19
Committed: Tue Mar 15 19:47:07 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.18: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

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