[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.32 - (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 : jsr166 1.24 import static java.util.concurrent.TimeUnit.DAYS;
10 :     import static java.util.concurrent.TimeUnit.HOURS;
11 :     import static java.util.concurrent.TimeUnit.MICROSECONDS;
12 :     import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 :     import static java.util.concurrent.TimeUnit.MINUTES;
14 :     import static java.util.concurrent.TimeUnit.NANOSECONDS;
15 :     import static java.util.concurrent.TimeUnit.SECONDS;
16 :    
17 : jsr166 1.22 import java.util.concurrent.CountDownLatch;
18 :     import java.util.concurrent.TimeUnit;
19 : jsr166 1.24
20 :     import junit.framework.Test;
21 :     import junit.framework.TestSuite;
22 : dl 1.1
23 : dl 1.3 public class TimeUnitTest extends JSR166TestCase {
24 : dl 1.1 public static void main(String[] args) {
25 : jsr166 1.25 main(suite(), args);
26 : dl 1.1 }
27 : jsr166 1.10
28 : dl 1.1 public static Test suite() {
29 : jsr166 1.14 return new TestSuite(TimeUnitTest.class);
30 : dl 1.1 }
31 :    
32 : dl 1.9 // (loops to 88888 check increments at all time divisions.)
33 :    
34 : dl 1.4 /**
35 : dl 1.9 * convert correctly converts sample values across the units
36 : dl 1.4 */
37 : dl 1.1 public void testConvert() {
38 : dl 1.9 for (long t = 0; t < 88888; ++t) {
39 : jsr166 1.10 assertEquals(t*60*60*24,
40 : jsr166 1.23 SECONDS.convert(t, DAYS));
41 : jsr166 1.10 assertEquals(t*60*60,
42 : jsr166 1.23 SECONDS.convert(t, HOURS));
43 : jsr166 1.10 assertEquals(t*60,
44 : jsr166 1.23 SECONDS.convert(t, MINUTES));
45 : jsr166 1.10 assertEquals(t,
46 : jsr166 1.23 SECONDS.convert(t, SECONDS));
47 : jsr166 1.10 assertEquals(t,
48 : jsr166 1.23 SECONDS.convert(1000L*t, MILLISECONDS));
49 : jsr166 1.10 assertEquals(t,
50 : jsr166 1.23 SECONDS.convert(1000000L*t, MICROSECONDS));
51 : jsr166 1.10 assertEquals(t,
52 : jsr166 1.23 SECONDS.convert(1000000000L*t, NANOSECONDS));
53 : dl 1.9
54 : jsr166 1.10 assertEquals(1000L*t*60*60*24,
55 : jsr166 1.23 MILLISECONDS.convert(t, DAYS));
56 : jsr166 1.10 assertEquals(1000L*t*60*60,
57 : jsr166 1.23 MILLISECONDS.convert(t, HOURS));
58 : jsr166 1.10 assertEquals(1000L*t*60,
59 : jsr166 1.23 MILLISECONDS.convert(t, MINUTES));
60 : jsr166 1.10 assertEquals(1000L*t,
61 : jsr166 1.23 MILLISECONDS.convert(t, SECONDS));
62 : jsr166 1.10 assertEquals(t,
63 : jsr166 1.23 MILLISECONDS.convert(t, MILLISECONDS));
64 : jsr166 1.10 assertEquals(t,
65 : jsr166 1.23 MILLISECONDS.convert(1000L*t, MICROSECONDS));
66 : jsr166 1.10 assertEquals(t,
67 : jsr166 1.23 MILLISECONDS.convert(1000000L*t, NANOSECONDS));
68 : dl 1.9
69 : jsr166 1.10 assertEquals(1000000L*t*60*60*24,
70 : jsr166 1.23 MICROSECONDS.convert(t, DAYS));
71 : jsr166 1.10 assertEquals(1000000L*t*60*60,
72 : jsr166 1.23 MICROSECONDS.convert(t, HOURS));
73 : jsr166 1.10 assertEquals(1000000L*t*60,
74 : jsr166 1.23 MICROSECONDS.convert(t, MINUTES));
75 : jsr166 1.10 assertEquals(1000000L*t,
76 : jsr166 1.23 MICROSECONDS.convert(t, SECONDS));
77 : jsr166 1.10 assertEquals(1000L*t,
78 : jsr166 1.23 MICROSECONDS.convert(t, MILLISECONDS));
79 : jsr166 1.10 assertEquals(t,
80 : jsr166 1.23 MICROSECONDS.convert(t, MICROSECONDS));
81 : jsr166 1.10 assertEquals(t,
82 : jsr166 1.23 MICROSECONDS.convert(1000L*t, NANOSECONDS));
83 : dl 1.9
84 : jsr166 1.10 assertEquals(1000000000L*t*60*60*24,
85 : jsr166 1.23 NANOSECONDS.convert(t, DAYS));
86 : jsr166 1.10 assertEquals(1000000000L*t*60*60,
87 : jsr166 1.23 NANOSECONDS.convert(t, HOURS));
88 : jsr166 1.10 assertEquals(1000000000L*t*60,
89 : jsr166 1.23 NANOSECONDS.convert(t, MINUTES));
90 : jsr166 1.10 assertEquals(1000000000L*t,
91 : jsr166 1.23 NANOSECONDS.convert(t, SECONDS));
92 : jsr166 1.10 assertEquals(1000000L*t,
93 : jsr166 1.23 NANOSECONDS.convert(t, MILLISECONDS));
94 : jsr166 1.10 assertEquals(1000L*t,
95 : jsr166 1.23 NANOSECONDS.convert(t, MICROSECONDS));
96 : jsr166 1.10 assertEquals(t,
97 : jsr166 1.23 NANOSECONDS.convert(t, NANOSECONDS));
98 : dl 1.1 }
99 : jsr166 1.28
100 :     for (TimeUnit x : TimeUnit.values()) {
101 :     long[] zs = {
102 :     0, 1, -1,
103 :     Integer.MAX_VALUE, Integer.MIN_VALUE,
104 :     Long.MAX_VALUE, Long.MIN_VALUE,
105 :     };
106 :     for (long z : zs) assertEquals(z, x.convert(z, x));
107 :     }
108 : dl 1.1 }
109 :    
110 : dl 1.4 /**
111 : dl 1.5 * toNanos correctly converts sample values in different units to
112 :     * nanoseconds
113 : dl 1.4 */
114 : dl 1.1 public void testToNanos() {
115 : dl 1.9 for (long t = 0; t < 88888; ++t) {
116 : jsr166 1.10 assertEquals(t*1000000000L*60*60*24,
117 : jsr166 1.23 DAYS.toNanos(t));
118 : jsr166 1.10 assertEquals(t*1000000000L*60*60,
119 : jsr166 1.23 HOURS.toNanos(t));
120 : jsr166 1.10 assertEquals(t*1000000000L*60,
121 : jsr166 1.23 MINUTES.toNanos(t));
122 : jsr166 1.10 assertEquals(1000000000L*t,
123 : jsr166 1.23 SECONDS.toNanos(t));
124 : jsr166 1.10 assertEquals(1000000L*t,
125 : jsr166 1.23 MILLISECONDS.toNanos(t));
126 : jsr166 1.10 assertEquals(1000L*t,
127 : jsr166 1.23 MICROSECONDS.toNanos(t));
128 : jsr166 1.10 assertEquals(t,
129 : jsr166 1.23 NANOSECONDS.toNanos(t));
130 : dl 1.3 }
131 :     }
132 :    
133 : dl 1.4 /**
134 : dl 1.5 * toMicros correctly converts sample values in different units to
135 :     * microseconds
136 : dl 1.4 */
137 : dl 1.3 public void testToMicros() {
138 : dl 1.9 for (long t = 0; t < 88888; ++t) {
139 : jsr166 1.10 assertEquals(t*1000000L*60*60*24,
140 : jsr166 1.23 DAYS.toMicros(t));
141 : jsr166 1.10 assertEquals(t*1000000L*60*60,
142 : jsr166 1.23 HOURS.toMicros(t));
143 : jsr166 1.10 assertEquals(t*1000000L*60,
144 : jsr166 1.23 MINUTES.toMicros(t));
145 : jsr166 1.10 assertEquals(1000000L*t,
146 : jsr166 1.23 SECONDS.toMicros(t));
147 : jsr166 1.10 assertEquals(1000L*t,
148 : jsr166 1.23 MILLISECONDS.toMicros(t));
149 : jsr166 1.10 assertEquals(t,
150 : jsr166 1.23 MICROSECONDS.toMicros(t));
151 : jsr166 1.10 assertEquals(t,
152 : jsr166 1.23 NANOSECONDS.toMicros(t*1000L));
153 : dl 1.3 }
154 :     }
155 :    
156 : dl 1.4 /**
157 : dl 1.5 * toMillis correctly converts sample values in different units to
158 :     * milliseconds
159 : dl 1.4 */
160 : dl 1.3 public void testToMillis() {
161 : dl 1.9 for (long t = 0; t < 88888; ++t) {
162 : jsr166 1.10 assertEquals(t*1000L*60*60*24,
163 : jsr166 1.23 DAYS.toMillis(t));
164 : jsr166 1.10 assertEquals(t*1000L*60*60,
165 : jsr166 1.23 HOURS.toMillis(t));
166 : jsr166 1.10 assertEquals(t*1000L*60,
167 : jsr166 1.23 MINUTES.toMillis(t));
168 : jsr166 1.10 assertEquals(1000L*t,
169 : jsr166 1.23 SECONDS.toMillis(t));
170 : jsr166 1.10 assertEquals(t,
171 : jsr166 1.23 MILLISECONDS.toMillis(t));
172 : jsr166 1.10 assertEquals(t,
173 : jsr166 1.23 MICROSECONDS.toMillis(t*1000L));
174 : jsr166 1.10 assertEquals(t,
175 : jsr166 1.23 NANOSECONDS.toMillis(t*1000000L));
176 : dl 1.3 }
177 :     }
178 :    
179 : dl 1.4 /**
180 : dl 1.5 * toSeconds correctly converts sample values in different units to
181 :     * seconds
182 : dl 1.4 */
183 : dl 1.3 public void testToSeconds() {
184 : dl 1.9 for (long t = 0; t < 88888; ++t) {
185 : jsr166 1.10 assertEquals(t*60*60*24,
186 : jsr166 1.23 DAYS.toSeconds(t));
187 : jsr166 1.10 assertEquals(t*60*60,
188 : jsr166 1.23 HOURS.toSeconds(t));
189 : jsr166 1.10 assertEquals(t*60,
190 : jsr166 1.23 MINUTES.toSeconds(t));
191 : jsr166 1.10 assertEquals(t,
192 : jsr166 1.23 SECONDS.toSeconds(t));
193 : jsr166 1.10 assertEquals(t,
194 : jsr166 1.23 MILLISECONDS.toSeconds(t*1000L));
195 : jsr166 1.10 assertEquals(t,
196 : jsr166 1.23 MICROSECONDS.toSeconds(t*1000000L));
197 : jsr166 1.10 assertEquals(t,
198 : jsr166 1.23 NANOSECONDS.toSeconds(t*1000000000L));
199 : dl 1.9 }
200 :     }
201 : dl 1.3
202 : dl 1.9 /**
203 :     * toMinutes correctly converts sample values in different units to
204 :     * minutes
205 :     */
206 :     public void testToMinutes() {
207 :     for (long t = 0; t < 88888; ++t) {
208 : jsr166 1.10 assertEquals(t*60*24,
209 : jsr166 1.23 DAYS.toMinutes(t));
210 : jsr166 1.10 assertEquals(t*60,
211 : jsr166 1.23 HOURS.toMinutes(t));
212 : jsr166 1.10 assertEquals(t,
213 : jsr166 1.23 MINUTES.toMinutes(t));
214 : jsr166 1.10 assertEquals(t,
215 : jsr166 1.23 SECONDS.toMinutes(t*60));
216 : jsr166 1.10 assertEquals(t,
217 : jsr166 1.23 MILLISECONDS.toMinutes(t*1000L*60));
218 : jsr166 1.10 assertEquals(t,
219 : jsr166 1.23 MICROSECONDS.toMinutes(t*1000000L*60));
220 : jsr166 1.10 assertEquals(t,
221 : jsr166 1.23 NANOSECONDS.toMinutes(t*1000000000L*60));
222 : dl 1.9 }
223 :     }
224 :    
225 :     /**
226 :     * toHours correctly converts sample values in different units to
227 :     * hours
228 :     */
229 :     public void testToHours() {
230 :     for (long t = 0; t < 88888; ++t) {
231 : jsr166 1.10 assertEquals(t*24,
232 : jsr166 1.23 DAYS.toHours(t));
233 : jsr166 1.10 assertEquals(t,
234 : jsr166 1.23 HOURS.toHours(t));
235 : jsr166 1.10 assertEquals(t,
236 : jsr166 1.23 MINUTES.toHours(t*60));
237 : jsr166 1.10 assertEquals(t,
238 : jsr166 1.23 SECONDS.toHours(t*60*60));
239 : jsr166 1.10 assertEquals(t,
240 : jsr166 1.23 MILLISECONDS.toHours(t*1000L*60*60));
241 : jsr166 1.10 assertEquals(t,
242 : jsr166 1.23 MICROSECONDS.toHours(t*1000000L*60*60));
243 : jsr166 1.10 assertEquals(t,
244 : jsr166 1.23 NANOSECONDS.toHours(t*1000000000L*60*60));
245 : dl 1.9 }
246 :     }
247 :    
248 :     /**
249 :     * toDays correctly converts sample values in different units to
250 :     * days
251 :     */
252 :     public void testToDays() {
253 :     for (long t = 0; t < 88888; ++t) {
254 : jsr166 1.10 assertEquals(t,
255 : jsr166 1.23 DAYS.toDays(t));
256 : jsr166 1.10 assertEquals(t,
257 : jsr166 1.23 HOURS.toDays(t*24));
258 : jsr166 1.10 assertEquals(t,
259 : jsr166 1.23 MINUTES.toDays(t*60*24));
260 : jsr166 1.10 assertEquals(t,
261 : jsr166 1.23 SECONDS.toDays(t*60*60*24));
262 : jsr166 1.10 assertEquals(t,
263 : jsr166 1.23 MILLISECONDS.toDays(t*1000L*60*60*24));
264 : jsr166 1.10 assertEquals(t,
265 : jsr166 1.23 MICROSECONDS.toDays(t*1000000L*60*60*24));
266 : jsr166 1.10 assertEquals(t,
267 : jsr166 1.23 NANOSECONDS.toDays(t*1000000000L*60*60*24));
268 : dl 1.1 }
269 :     }
270 :    
271 : dl 1.4 /**
272 : jsr166 1.10 * convert saturates positive too-large values to Long.MAX_VALUE
273 : dl 1.5 * and negative to LONG.MIN_VALUE
274 : dl 1.4 */
275 : dl 1.1 public void testConvertSaturate() {
276 :     assertEquals(Long.MAX_VALUE,
277 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
278 : dl 1.1 assertEquals(Long.MIN_VALUE,
279 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
280 : dl 1.9 assertEquals(Long.MAX_VALUE,
281 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
282 : dl 1.9 assertEquals(Long.MIN_VALUE,
283 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
284 : dl 1.9 assertEquals(Long.MAX_VALUE,
285 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
286 : dl 1.9 assertEquals(Long.MIN_VALUE,
287 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
288 : dl 1.9 assertEquals(Long.MAX_VALUE,
289 : jsr166 1.23 NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
290 : dl 1.9 assertEquals(Long.MIN_VALUE,
291 : jsr166 1.23 NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
292 : jsr166 1.28
293 :     for (TimeUnit x : TimeUnit.values())
294 :     for (TimeUnit y : TimeUnit.values()) {
295 :     long ratio = x.toNanos(1) / y.toNanos(1);
296 : jsr166 1.29 if (ratio >= 1) {
297 : jsr166 1.28 assertEquals(ratio, y.convert(1, x));
298 :     assertEquals(1, x.convert(ratio, y));
299 :     long max = Long.MAX_VALUE/ratio;
300 :     assertEquals(max * ratio, y.convert(max, x));
301 :     assertEquals(-max * ratio, y.convert(-max, x));
302 :     assertEquals(max, x.convert(max * ratio, y));
303 :     assertEquals(-max, x.convert(-max * ratio, y));
304 : jsr166 1.29 if (max < Long.MAX_VALUE) {
305 :     assertEquals(Long.MAX_VALUE, y.convert(max + 1, x));
306 :     assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x));
307 :     assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x));
308 :     }
309 : jsr166 1.28 assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x));
310 :     assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x));
311 :     }
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 : jsr166 1.23 MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
322 : jsr166 1.13 assertEquals(Long.MIN_VALUE,
323 : jsr166 1.23 MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
324 : jsr166 1.28
325 :     for (TimeUnit x : TimeUnit.values()) {
326 :     long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1);
327 : jsr166 1.29 if (ratio >= 1) {
328 : jsr166 1.28 long max = Long.MAX_VALUE/ratio;
329 :     for (long z : new long[] {0, 1, -1, max, -max})
330 :     assertEquals(z * ratio, x.toNanos(z));
331 : jsr166 1.29 if (max < Long.MAX_VALUE) {
332 :     assertEquals(Long.MAX_VALUE, x.toNanos(max + 1));
333 :     assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1));
334 :     assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1));
335 :     }
336 : jsr166 1.28 assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE));
337 :     assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE));
338 :     if (max < Integer.MAX_VALUE) {
339 :     assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE));
340 :     assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE));
341 :     }
342 :     }
343 :     }
344 :     }
345 :    
346 :     /**
347 :     * toMicros saturates positive too-large values to Long.MAX_VALUE
348 :     * and negative to LONG.MIN_VALUE
349 :     */
350 :     public void testToMicrosSaturate() {
351 :     for (TimeUnit x : TimeUnit.values()) {
352 :     long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1);
353 : jsr166 1.29 if (ratio >= 1) {
354 : jsr166 1.28 long max = Long.MAX_VALUE/ratio;
355 :     for (long z : new long[] {0, 1, -1, max, -max})
356 :     assertEquals(z * ratio, x.toMicros(z));
357 : jsr166 1.29 if (max < Long.MAX_VALUE) {
358 :     assertEquals(Long.MAX_VALUE, x.toMicros(max + 1));
359 :     assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1));
360 :     assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1));
361 :     }
362 : jsr166 1.28 assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE));
363 :     assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE));
364 :     if (max < Integer.MAX_VALUE) {
365 :     assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE));
366 :     assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE));
367 :     }
368 :     }
369 :     }
370 :     }
371 :    
372 :     /**
373 :     * toMillis saturates positive too-large values to Long.MAX_VALUE
374 :     * and negative to LONG.MIN_VALUE
375 :     */
376 :     public void testToMillisSaturate() {
377 :     for (TimeUnit x : TimeUnit.values()) {
378 :     long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1);
379 : jsr166 1.29 if (ratio >= 1) {
380 : jsr166 1.28 long max = Long.MAX_VALUE/ratio;
381 :     for (long z : new long[] {0, 1, -1, max, -max})
382 :     assertEquals(z * ratio, x.toMillis(z));
383 : jsr166 1.29 if (max < Long.MAX_VALUE) {
384 :     assertEquals(Long.MAX_VALUE, x.toMillis(max + 1));
385 :     assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1));
386 :     assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1));
387 :     }
388 : jsr166 1.28 assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE));
389 :     assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE));
390 :     if (max < Integer.MAX_VALUE) {
391 :     assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE));
392 :     assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE));
393 :     }
394 :     }
395 :     }
396 :     }
397 :    
398 :     /**
399 :     * toSeconds saturates positive too-large values to Long.MAX_VALUE
400 :     * and negative to LONG.MIN_VALUE
401 :     */
402 :     public void testToSecondsSaturate() {
403 :     for (TimeUnit x : TimeUnit.values()) {
404 :     long ratio = x.toNanos(1) / SECONDS.toNanos(1);
405 : jsr166 1.29 if (ratio >= 1) {
406 : jsr166 1.28 long max = Long.MAX_VALUE/ratio;
407 :     for (long z : new long[] {0, 1, -1, max, -max})
408 :     assertEquals(z * ratio, x.toSeconds(z));
409 : jsr166 1.29 if (max < Long.MAX_VALUE) {
410 :     assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1));
411 :     assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1));
412 :     assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1));
413 :     }
414 : jsr166 1.28 assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE));
415 :     assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE));
416 :     if (max < Integer.MAX_VALUE) {
417 :     assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE));
418 :     assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE));
419 :     }
420 :     }
421 :     }
422 :     }
423 :    
424 :     /**
425 :     * toMinutes saturates positive too-large values to Long.MAX_VALUE
426 :     * and negative to LONG.MIN_VALUE
427 :     */
428 :     public void testToMinutesSaturate() {
429 :     for (TimeUnit x : TimeUnit.values()) {
430 :     long ratio = x.toNanos(1) / MINUTES.toNanos(1);
431 :     if (ratio > 1) {
432 :     long max = Long.MAX_VALUE/ratio;
433 :     for (long z : new long[] {0, 1, -1, max, -max})
434 :     assertEquals(z * ratio, x.toMinutes(z));
435 :     assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1));
436 :     assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1));
437 :     assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE));
438 :     assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE));
439 :     assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1));
440 :     }
441 :     }
442 :     }
443 :    
444 :     /**
445 :     * toHours saturates positive too-large values to Long.MAX_VALUE
446 :     * and negative to LONG.MIN_VALUE
447 :     */
448 :     public void testToHoursSaturate() {
449 :     for (TimeUnit x : TimeUnit.values()) {
450 :     long ratio = x.toNanos(1) / HOURS.toNanos(1);
451 : jsr166 1.29 if (ratio >= 1) {
452 : jsr166 1.28 long max = Long.MAX_VALUE/ratio;
453 :     for (long z : new long[] {0, 1, -1, max, -max})
454 :     assertEquals(z * ratio, x.toHours(z));
455 : jsr166 1.29 if (max < Long.MAX_VALUE) {
456 :     assertEquals(Long.MAX_VALUE, x.toHours(max + 1));
457 :     assertEquals(Long.MIN_VALUE, x.toHours(-max - 1));
458 :     assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1));
459 :     }
460 : jsr166 1.28 assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE));
461 :     assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE));
462 :     }
463 :     }
464 : dl 1.1 }
465 :    
466 : dl 1.4 /**
467 : jsr166 1.20 * toString returns name of unit
468 : dl 1.4 */
469 : dl 1.1 public void testToString() {
470 : jsr166 1.23 assertEquals("SECONDS", SECONDS.toString());
471 : dl 1.1 }
472 :    
473 : jsr166 1.20 /**
474 :     * name returns name of unit
475 :     */
476 :     public void testName() {
477 : jsr166 1.23 assertEquals("SECONDS", SECONDS.name());
478 : jsr166 1.20 }
479 : jsr166 1.10
480 : dl 1.1 /**
481 : jsr166 1.17 * Timed wait without holding lock throws
482 :     * IllegalMonitorStateException
483 : dl 1.1 */
484 : jsr166 1.21 public void testTimedWait_IllegalMonitorException() {
485 :     Thread t = newStartedThread(new CheckedRunnable() {
486 : jsr166 1.13 public void realRun() throws InterruptedException {
487 :     Object o = new Object();
488 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
489 : jsr166 1.21
490 : jsr166 1.13 try {
491 : jsr166 1.21 tu.timedWait(o, LONG_DELAY_MS);
492 : jsr166 1.13 threadShouldThrow();
493 : jsr166 1.21 } catch (IllegalMonitorStateException success) {}
494 :     }});
495 : dl 1.1
496 : jsr166 1.21 awaitTermination(t);
497 : dl 1.1 }
498 : jsr166 1.10
499 : dl 1.1 /**
500 : dl 1.5 * timedWait throws InterruptedException when interrupted
501 : dl 1.4 */
502 : jsr166 1.21 public void testTimedWait_Interruptible() {
503 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
504 :     Thread t = newStartedThread(new CheckedRunnable() {
505 : jsr166 1.14 public void realRun() throws InterruptedException {
506 : jsr166 1.13 Object o = new Object();
507 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
508 : jsr166 1.13
509 : jsr166 1.21 Thread.currentThread().interrupt();
510 :     try {
511 :     synchronized (o) {
512 :     tu.timedWait(o, LONG_DELAY_MS);
513 :     }
514 :     shouldThrow();
515 :     } catch (InterruptedException success) {}
516 :     assertFalse(Thread.interrupted());
517 :    
518 :     pleaseInterrupt.countDown();
519 :     try {
520 :     synchronized (o) {
521 :     tu.timedWait(o, LONG_DELAY_MS);
522 :     }
523 :     shouldThrow();
524 :     } catch (InterruptedException success) {}
525 :     assertFalse(Thread.interrupted());
526 : jsr166 1.13 }});
527 : jsr166 1.21
528 :     await(pleaseInterrupt);
529 : jsr166 1.31 assertThreadBlocks(t, Thread.State.TIMED_WAITING);
530 : jsr166 1.13 t.interrupt();
531 : jsr166 1.21 awaitTermination(t);
532 : dl 1.1 }
533 : jsr166 1.10
534 : dl 1.1 /**
535 : dl 1.5 * timedJoin throws InterruptedException when interrupted
536 : dl 1.4 */
537 : jsr166 1.21 public void testTimedJoin_Interruptible() {
538 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
539 :     final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
540 : jsr166 1.13 public void realRun() throws InterruptedException {
541 : jsr166 1.21 Thread.sleep(LONG_DELAY_MS);
542 : jsr166 1.13 }});
543 : jsr166 1.21 final Thread t = newStartedThread(new CheckedRunnable() {
544 : jsr166 1.14 public void realRun() throws InterruptedException {
545 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
546 : jsr166 1.21 Thread.currentThread().interrupt();
547 :     try {
548 :     tu.timedJoin(s, LONG_DELAY_MS);
549 :     shouldThrow();
550 :     } catch (InterruptedException success) {}
551 :     assertFalse(Thread.interrupted());
552 :    
553 :     pleaseInterrupt.countDown();
554 :     try {
555 :     tu.timedJoin(s, LONG_DELAY_MS);
556 :     shouldThrow();
557 :     } catch (InterruptedException success) {}
558 :     assertFalse(Thread.interrupted());
559 : jsr166 1.18 }});
560 : jsr166 1.21
561 :     await(pleaseInterrupt);
562 : jsr166 1.31 assertThreadBlocks(t, Thread.State.TIMED_WAITING);
563 : jsr166 1.13 t.interrupt();
564 : jsr166 1.21 awaitTermination(t);
565 : jsr166 1.13 s.interrupt();
566 : jsr166 1.21 awaitTermination(s);
567 : dl 1.1 }
568 : jsr166 1.10
569 : dl 1.1 /**
570 : jsr166 1.17 * timedSleep throws InterruptedException when interrupted
571 : dl 1.4 */
572 : jsr166 1.21 public void testTimedSleep_Interruptible() {
573 :     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
574 :     Thread t = newStartedThread(new CheckedRunnable() {
575 : jsr166 1.14 public void realRun() throws InterruptedException {
576 : jsr166 1.23 TimeUnit tu = MILLISECONDS;
577 : jsr166 1.21 Thread.currentThread().interrupt();
578 :     try {
579 :     tu.sleep(LONG_DELAY_MS);
580 :     shouldThrow();
581 :     } catch (InterruptedException success) {}
582 :     assertFalse(Thread.interrupted());
583 :    
584 :     pleaseInterrupt.countDown();
585 :     try {
586 :     tu.sleep(LONG_DELAY_MS);
587 :     shouldThrow();
588 :     } catch (InterruptedException success) {}
589 :     assertFalse(Thread.interrupted());
590 : jsr166 1.13 }});
591 : dl 1.1
592 : jsr166 1.21 await(pleaseInterrupt);
593 : jsr166 1.31 assertThreadBlocks(t, Thread.State.TIMED_WAITING);
594 : jsr166 1.13 t.interrupt();
595 : jsr166 1.21 awaitTermination(t);
596 : dl 1.1 }
597 : dl 1.2
598 : dl 1.4 /**
599 : jsr166 1.32 * a deserialized/reserialized unit is the same instance
600 : dl 1.4 */
601 : jsr166 1.13 public void testSerialization() throws Exception {
602 : jsr166 1.27 for (TimeUnit x : TimeUnit.values())
603 :     assertSame(x, serialClone(x));
604 : dl 1.2 }
605 :    
606 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8