ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TimeUnitTest.java (file contents):
Revision 1.12 by jsr166, Mon Nov 16 05:30:08 2009 UTC vs.
Revision 1.29 by jsr166, Fri Mar 25 04:53:28 2016 UTC

# Line 1 | Line 1
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/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 + 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 + import java.time.temporal.ChronoUnit;
18 + import java.util.concurrent.CountDownLatch;
19 + import java.util.concurrent.TimeUnit;
20  
21 < import junit.framework.*;
22 < import java.util.concurrent.*;
12 < import java.io.*;
21 > import junit.framework.Test;
22 > import junit.framework.TestSuite;
23  
24   public class TimeUnitTest extends JSR166TestCase {
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28  
29      public static Test suite() {
30 <        return new TestSuite(TimeUnitTest.class);
30 >        return new TestSuite(TimeUnitTest.class);
31      }
32  
33      // (loops to 88888 check increments at all time divisions.)
# Line 28 | Line 38 | public class TimeUnitTest extends JSR166
38      public void testConvert() {
39          for (long t = 0; t < 88888; ++t) {
40              assertEquals(t*60*60*24,
41 <                         TimeUnit.SECONDS.convert(t,
32 <                                                  TimeUnit.DAYS));
41 >                         SECONDS.convert(t, DAYS));
42              assertEquals(t*60*60,
43 <                         TimeUnit.SECONDS.convert(t,
35 <                                                  TimeUnit.HOURS));
43 >                         SECONDS.convert(t, HOURS));
44              assertEquals(t*60,
45 <                         TimeUnit.SECONDS.convert(t,
38 <                                                  TimeUnit.MINUTES));
45 >                         SECONDS.convert(t, MINUTES));
46              assertEquals(t,
47 <                         TimeUnit.SECONDS.convert(t,
41 <                                                  TimeUnit.SECONDS));
47 >                         SECONDS.convert(t, SECONDS));
48              assertEquals(t,
49 <                         TimeUnit.SECONDS.convert(1000L*t,
44 <                                                  TimeUnit.MILLISECONDS));
49 >                         SECONDS.convert(1000L*t, MILLISECONDS));
50              assertEquals(t,
51 <                         TimeUnit.SECONDS.convert(1000000L*t,
47 <                                                  TimeUnit.MICROSECONDS));
51 >                         SECONDS.convert(1000000L*t, MICROSECONDS));
52              assertEquals(t,
53 <                         TimeUnit.SECONDS.convert(1000000000L*t,
50 <                                                  TimeUnit.NANOSECONDS));
51 <
53 >                         SECONDS.convert(1000000000L*t, NANOSECONDS));
54  
55              assertEquals(1000L*t*60*60*24,
56 <                         TimeUnit.MILLISECONDS.convert(t,
55 <                                                  TimeUnit.DAYS));
56 >                         MILLISECONDS.convert(t, DAYS));
57              assertEquals(1000L*t*60*60,
58 <                         TimeUnit.MILLISECONDS.convert(t,
58 <                                                  TimeUnit.HOURS));
58 >                         MILLISECONDS.convert(t, HOURS));
59              assertEquals(1000L*t*60,
60 <                         TimeUnit.MILLISECONDS.convert(t,
61 <                                                  TimeUnit.MINUTES));
60 >                         MILLISECONDS.convert(t, MINUTES));
61              assertEquals(1000L*t,
62 <                         TimeUnit.MILLISECONDS.convert(t,
64 <                                                  TimeUnit.SECONDS));
62 >                         MILLISECONDS.convert(t, SECONDS));
63              assertEquals(t,
64 <                         TimeUnit.MILLISECONDS.convert(t,
67 <                                                  TimeUnit.MILLISECONDS));
64 >                         MILLISECONDS.convert(t, MILLISECONDS));
65              assertEquals(t,
66 <                         TimeUnit.MILLISECONDS.convert(1000L*t,
70 <                                                  TimeUnit.MICROSECONDS));
66 >                         MILLISECONDS.convert(1000L*t, MICROSECONDS));
67              assertEquals(t,
68 <                         TimeUnit.MILLISECONDS.convert(1000000L*t,
73 <                                                  TimeUnit.NANOSECONDS));
68 >                         MILLISECONDS.convert(1000000L*t, NANOSECONDS));
69  
70              assertEquals(1000000L*t*60*60*24,
71 <                         TimeUnit.MICROSECONDS.convert(t,
77 <                                                  TimeUnit.DAYS));
71 >                         MICROSECONDS.convert(t, DAYS));
72              assertEquals(1000000L*t*60*60,
73 <                         TimeUnit.MICROSECONDS.convert(t,
80 <                                                  TimeUnit.HOURS));
73 >                         MICROSECONDS.convert(t, HOURS));
74              assertEquals(1000000L*t*60,
75 <                         TimeUnit.MICROSECONDS.convert(t,
83 <                                                  TimeUnit.MINUTES));
75 >                         MICROSECONDS.convert(t, MINUTES));
76              assertEquals(1000000L*t,
77 <                         TimeUnit.MICROSECONDS.convert(t,
86 <                                                  TimeUnit.SECONDS));
77 >                         MICROSECONDS.convert(t, SECONDS));
78              assertEquals(1000L*t,
79 <                         TimeUnit.MICROSECONDS.convert(t,
89 <                                                  TimeUnit.MILLISECONDS));
79 >                         MICROSECONDS.convert(t, MILLISECONDS));
80              assertEquals(t,
81 <                         TimeUnit.MICROSECONDS.convert(t,
92 <                                                  TimeUnit.MICROSECONDS));
81 >                         MICROSECONDS.convert(t, MICROSECONDS));
82              assertEquals(t,
83 <                         TimeUnit.MICROSECONDS.convert(1000L*t,
95 <                                                  TimeUnit.NANOSECONDS));
83 >                         MICROSECONDS.convert(1000L*t, NANOSECONDS));
84  
85              assertEquals(1000000000L*t*60*60*24,
86 <                         TimeUnit.NANOSECONDS.convert(t,
99 <                                                  TimeUnit.DAYS));
86 >                         NANOSECONDS.convert(t, DAYS));
87              assertEquals(1000000000L*t*60*60,
88 <                         TimeUnit.NANOSECONDS.convert(t,
102 <                                                  TimeUnit.HOURS));
88 >                         NANOSECONDS.convert(t, HOURS));
89              assertEquals(1000000000L*t*60,
90 <                         TimeUnit.NANOSECONDS.convert(t,
105 <                                                  TimeUnit.MINUTES));
90 >                         NANOSECONDS.convert(t, MINUTES));
91              assertEquals(1000000000L*t,
92 <                         TimeUnit.NANOSECONDS.convert(t,
108 <                                                  TimeUnit.SECONDS));
92 >                         NANOSECONDS.convert(t, SECONDS));
93              assertEquals(1000000L*t,
94 <                         TimeUnit.NANOSECONDS.convert(t,
111 <                                                  TimeUnit.MILLISECONDS));
94 >                         NANOSECONDS.convert(t, MILLISECONDS));
95              assertEquals(1000L*t,
96 <                         TimeUnit.NANOSECONDS.convert(t,
114 <                                                  TimeUnit.MICROSECONDS));
96 >                         NANOSECONDS.convert(t, MICROSECONDS));
97              assertEquals(t,
98 <                         TimeUnit.NANOSECONDS.convert(t,
99 <                                                  TimeUnit.NANOSECONDS));
98 >                         NANOSECONDS.convert(t, NANOSECONDS));
99 >        }
100 >
101 >        for (TimeUnit x : TimeUnit.values()) {
102 >            long[] zs = {
103 >                0, 1, -1,
104 >                Integer.MAX_VALUE, Integer.MIN_VALUE,
105 >                Long.MAX_VALUE, Long.MIN_VALUE,
106 >            };
107 >            for (long z : zs) assertEquals(z, x.convert(z, x));
108          }
109      }
110  
# Line 125 | Line 115 | public class TimeUnitTest extends JSR166
115      public void testToNanos() {
116          for (long t = 0; t < 88888; ++t) {
117              assertEquals(t*1000000000L*60*60*24,
118 <                         TimeUnit.DAYS.toNanos(t));
118 >                         DAYS.toNanos(t));
119              assertEquals(t*1000000000L*60*60,
120 <                         TimeUnit.HOURS.toNanos(t));
120 >                         HOURS.toNanos(t));
121              assertEquals(t*1000000000L*60,
122 <                         TimeUnit.MINUTES.toNanos(t));
122 >                         MINUTES.toNanos(t));
123              assertEquals(1000000000L*t,
124 <                         TimeUnit.SECONDS.toNanos(t));
124 >                         SECONDS.toNanos(t));
125              assertEquals(1000000L*t,
126 <                         TimeUnit.MILLISECONDS.toNanos(t));
126 >                         MILLISECONDS.toNanos(t));
127              assertEquals(1000L*t,
128 <                         TimeUnit.MICROSECONDS.toNanos(t));
128 >                         MICROSECONDS.toNanos(t));
129              assertEquals(t,
130 <                         TimeUnit.NANOSECONDS.toNanos(t));
130 >                         NANOSECONDS.toNanos(t));
131          }
132      }
133  
# Line 148 | Line 138 | public class TimeUnitTest extends JSR166
138      public void testToMicros() {
139          for (long t = 0; t < 88888; ++t) {
140              assertEquals(t*1000000L*60*60*24,
141 <                         TimeUnit.DAYS.toMicros(t));
141 >                         DAYS.toMicros(t));
142              assertEquals(t*1000000L*60*60,
143 <                         TimeUnit.HOURS.toMicros(t));
143 >                         HOURS.toMicros(t));
144              assertEquals(t*1000000L*60,
145 <                         TimeUnit.MINUTES.toMicros(t));
145 >                         MINUTES.toMicros(t));
146              assertEquals(1000000L*t,
147 <                         TimeUnit.SECONDS.toMicros(t));
147 >                         SECONDS.toMicros(t));
148              assertEquals(1000L*t,
149 <                         TimeUnit.MILLISECONDS.toMicros(t));
149 >                         MILLISECONDS.toMicros(t));
150              assertEquals(t,
151 <                         TimeUnit.MICROSECONDS.toMicros(t));
151 >                         MICROSECONDS.toMicros(t));
152              assertEquals(t,
153 <                         TimeUnit.NANOSECONDS.toMicros(t*1000L));
153 >                         NANOSECONDS.toMicros(t*1000L));
154          }
155      }
156  
# Line 171 | Line 161 | public class TimeUnitTest extends JSR166
161      public void testToMillis() {
162          for (long t = 0; t < 88888; ++t) {
163              assertEquals(t*1000L*60*60*24,
164 <                         TimeUnit.DAYS.toMillis(t));
164 >                         DAYS.toMillis(t));
165              assertEquals(t*1000L*60*60,
166 <                         TimeUnit.HOURS.toMillis(t));
166 >                         HOURS.toMillis(t));
167              assertEquals(t*1000L*60,
168 <                         TimeUnit.MINUTES.toMillis(t));
168 >                         MINUTES.toMillis(t));
169              assertEquals(1000L*t,
170 <                         TimeUnit.SECONDS.toMillis(t));
170 >                         SECONDS.toMillis(t));
171              assertEquals(t,
172 <                         TimeUnit.MILLISECONDS.toMillis(t));
172 >                         MILLISECONDS.toMillis(t));
173              assertEquals(t,
174 <                         TimeUnit.MICROSECONDS.toMillis(t*1000L));
174 >                         MICROSECONDS.toMillis(t*1000L));
175              assertEquals(t,
176 <                         TimeUnit.NANOSECONDS.toMillis(t*1000000L));
176 >                         NANOSECONDS.toMillis(t*1000000L));
177          }
178      }
179  
# Line 194 | Line 184 | public class TimeUnitTest extends JSR166
184      public void testToSeconds() {
185          for (long t = 0; t < 88888; ++t) {
186              assertEquals(t*60*60*24,
187 <                         TimeUnit.DAYS.toSeconds(t));
187 >                         DAYS.toSeconds(t));
188              assertEquals(t*60*60,
189 <                         TimeUnit.HOURS.toSeconds(t));
189 >                         HOURS.toSeconds(t));
190              assertEquals(t*60,
191 <                         TimeUnit.MINUTES.toSeconds(t));
191 >                         MINUTES.toSeconds(t));
192              assertEquals(t,
193 <                         TimeUnit.SECONDS.toSeconds(t));
193 >                         SECONDS.toSeconds(t));
194              assertEquals(t,
195 <                         TimeUnit.MILLISECONDS.toSeconds(t*1000L));
195 >                         MILLISECONDS.toSeconds(t*1000L));
196              assertEquals(t,
197 <                         TimeUnit.MICROSECONDS.toSeconds(t*1000000L));
197 >                         MICROSECONDS.toSeconds(t*1000000L));
198              assertEquals(t,
199 <                         TimeUnit.NANOSECONDS.toSeconds(t*1000000000L));
199 >                         NANOSECONDS.toSeconds(t*1000000000L));
200          }
201      }
202  
# Line 217 | Line 207 | public class TimeUnitTest extends JSR166
207      public void testToMinutes() {
208          for (long t = 0; t < 88888; ++t) {
209              assertEquals(t*60*24,
210 <                         TimeUnit.DAYS.toMinutes(t));
210 >                         DAYS.toMinutes(t));
211              assertEquals(t*60,
212 <                         TimeUnit.HOURS.toMinutes(t));
212 >                         HOURS.toMinutes(t));
213              assertEquals(t,
214 <                         TimeUnit.MINUTES.toMinutes(t));
214 >                         MINUTES.toMinutes(t));
215              assertEquals(t,
216 <                         TimeUnit.SECONDS.toMinutes(t*60));
216 >                         SECONDS.toMinutes(t*60));
217              assertEquals(t,
218 <                         TimeUnit.MILLISECONDS.toMinutes(t*1000L*60));
218 >                         MILLISECONDS.toMinutes(t*1000L*60));
219              assertEquals(t,
220 <                         TimeUnit.MICROSECONDS.toMinutes(t*1000000L*60));
220 >                         MICROSECONDS.toMinutes(t*1000000L*60));
221              assertEquals(t,
222 <                         TimeUnit.NANOSECONDS.toMinutes(t*1000000000L*60));
222 >                         NANOSECONDS.toMinutes(t*1000000000L*60));
223          }
224      }
225  
# Line 240 | Line 230 | public class TimeUnitTest extends JSR166
230      public void testToHours() {
231          for (long t = 0; t < 88888; ++t) {
232              assertEquals(t*24,
233 <                         TimeUnit.DAYS.toHours(t));
233 >                         DAYS.toHours(t));
234              assertEquals(t,
235 <                         TimeUnit.HOURS.toHours(t));
235 >                         HOURS.toHours(t));
236              assertEquals(t,
237 <                         TimeUnit.MINUTES.toHours(t*60));
237 >                         MINUTES.toHours(t*60));
238              assertEquals(t,
239 <                         TimeUnit.SECONDS.toHours(t*60*60));
239 >                         SECONDS.toHours(t*60*60));
240              assertEquals(t,
241 <                         TimeUnit.MILLISECONDS.toHours(t*1000L*60*60));
241 >                         MILLISECONDS.toHours(t*1000L*60*60));
242              assertEquals(t,
243 <                         TimeUnit.MICROSECONDS.toHours(t*1000000L*60*60));
243 >                         MICROSECONDS.toHours(t*1000000L*60*60));
244              assertEquals(t,
245 <                         TimeUnit.NANOSECONDS.toHours(t*1000000000L*60*60));
245 >                         NANOSECONDS.toHours(t*1000000000L*60*60));
246          }
247      }
248  
# Line 263 | Line 253 | public class TimeUnitTest extends JSR166
253      public void testToDays() {
254          for (long t = 0; t < 88888; ++t) {
255              assertEquals(t,
256 <                         TimeUnit.DAYS.toDays(t));
256 >                         DAYS.toDays(t));
257              assertEquals(t,
258 <                         TimeUnit.HOURS.toDays(t*24));
258 >                         HOURS.toDays(t*24));
259              assertEquals(t,
260 <                         TimeUnit.MINUTES.toDays(t*60*24));
260 >                         MINUTES.toDays(t*60*24));
261              assertEquals(t,
262 <                         TimeUnit.SECONDS.toDays(t*60*60*24));
262 >                         SECONDS.toDays(t*60*60*24));
263              assertEquals(t,
264 <                         TimeUnit.MILLISECONDS.toDays(t*1000L*60*60*24));
264 >                         MILLISECONDS.toDays(t*1000L*60*60*24));
265              assertEquals(t,
266 <                         TimeUnit.MICROSECONDS.toDays(t*1000000L*60*60*24));
266 >                         MICROSECONDS.toDays(t*1000000L*60*60*24));
267              assertEquals(t,
268 <                         TimeUnit.NANOSECONDS.toDays(t*1000000000L*60*60*24));
268 >                         NANOSECONDS.toDays(t*1000000000L*60*60*24));
269          }
270      }
271  
282
272      /**
273       * convert saturates positive too-large values to Long.MAX_VALUE
274       * and negative to LONG.MIN_VALUE
275       */
276      public void testConvertSaturate() {
277          assertEquals(Long.MAX_VALUE,
278 <                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
290 <                                                  TimeUnit.SECONDS));
278 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
279          assertEquals(Long.MIN_VALUE,
280 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
293 <                                                  TimeUnit.SECONDS));
280 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
281          assertEquals(Long.MAX_VALUE,
282 <                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
296 <                                                  TimeUnit.MINUTES));
282 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
283          assertEquals(Long.MIN_VALUE,
284 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
299 <                                                  TimeUnit.MINUTES));
284 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
285          assertEquals(Long.MAX_VALUE,
286 <                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
302 <                                                  TimeUnit.HOURS));
286 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
287          assertEquals(Long.MIN_VALUE,
288 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
305 <                                                  TimeUnit.HOURS));
288 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
289          assertEquals(Long.MAX_VALUE,
290 <                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
308 <                                                  TimeUnit.DAYS));
290 >                     NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
291          assertEquals(Long.MIN_VALUE,
292 <                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
311 <                                                  TimeUnit.DAYS));
292 >                     NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
293  
294 +        for (TimeUnit x : TimeUnit.values())
295 +            for (TimeUnit y : TimeUnit.values()) {
296 +                long ratio = x.toNanos(1) / y.toNanos(1);
297 +                if (ratio >= 1) {
298 +                    assertEquals(ratio, y.convert(1, x));
299 +                    assertEquals(1, x.convert(ratio, y));
300 +                    long max = Long.MAX_VALUE/ratio;
301 +                    assertEquals(max * ratio, y.convert(max, x));
302 +                    assertEquals(-max * ratio, y.convert(-max, x));
303 +                    assertEquals(max, x.convert(max * ratio, y));
304 +                    assertEquals(-max, x.convert(-max * ratio, y));
305 +                    if (max < Long.MAX_VALUE) {
306 +                        assertEquals(Long.MAX_VALUE, y.convert(max + 1, x));
307 +                        assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x));
308 +                        assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x));
309 +                    }
310 +                    assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x));
311 +                    assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x));
312 +                }
313 +            }
314      }
315  
316      /**
# Line 317 | Line 318 | public class TimeUnitTest extends JSR166
318       * and negative to LONG.MIN_VALUE
319       */
320      public void testToNanosSaturate() {
321 <            assertEquals(Long.MAX_VALUE,
322 <                         TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
323 <            assertEquals(Long.MIN_VALUE,
324 <                         TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
324 <    }
321 >        assertEquals(Long.MAX_VALUE,
322 >                     MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
323 >        assertEquals(Long.MIN_VALUE,
324 >                     MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
325  
326 +        for (TimeUnit x : TimeUnit.values()) {
327 +            long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1);
328 +            if (ratio >= 1) {
329 +                long max = Long.MAX_VALUE/ratio;
330 +                for (long z : new long[] {0, 1, -1, max, -max})
331 +                    assertEquals(z * ratio, x.toNanos(z));
332 +                if (max < Long.MAX_VALUE) {
333 +                    assertEquals(Long.MAX_VALUE, x.toNanos(max + 1));
334 +                    assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1));
335 +                    assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1));
336 +                }
337 +                assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE));
338 +                assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE));
339 +                if (max < Integer.MAX_VALUE) {
340 +                    assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE));
341 +                    assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE));
342 +                }
343 +            }
344 +        }
345 +    }
346  
347      /**
348 <     * toString returns string containing common name of unit
348 >     * toMicros saturates positive too-large values to Long.MAX_VALUE
349 >     * and negative to LONG.MIN_VALUE
350       */
351 <    public void testToString() {
352 <        String s = TimeUnit.SECONDS.toString();
353 <        assertTrue(s.indexOf("ECOND") >= 0);
351 >    public void testToMicrosSaturate() {
352 >        for (TimeUnit x : TimeUnit.values()) {
353 >            long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1);
354 >            if (ratio >= 1) {
355 >                long max = Long.MAX_VALUE/ratio;
356 >                for (long z : new long[] {0, 1, -1, max, -max})
357 >                    assertEquals(z * ratio, x.toMicros(z));
358 >                if (max < Long.MAX_VALUE) {
359 >                    assertEquals(Long.MAX_VALUE, x.toMicros(max + 1));
360 >                    assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1));
361 >                    assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1));
362 >                }
363 >                assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE));
364 >                assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE));
365 >                if (max < Integer.MAX_VALUE) {
366 >                    assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE));
367 >                    assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE));
368 >                }
369 >            }
370 >        }
371      }
372  
373 +    /**
374 +     * toMillis saturates positive too-large values to Long.MAX_VALUE
375 +     * and negative to LONG.MIN_VALUE
376 +     */
377 +    public void testToMillisSaturate() {
378 +        for (TimeUnit x : TimeUnit.values()) {
379 +            long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1);
380 +            if (ratio >= 1) {
381 +                long max = Long.MAX_VALUE/ratio;
382 +                for (long z : new long[] {0, 1, -1, max, -max})
383 +                    assertEquals(z * ratio, x.toMillis(z));
384 +                if (max < Long.MAX_VALUE) {
385 +                    assertEquals(Long.MAX_VALUE, x.toMillis(max + 1));
386 +                    assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1));
387 +                    assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1));
388 +                }
389 +                assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE));
390 +                assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE));
391 +                if (max < Integer.MAX_VALUE) {
392 +                    assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE));
393 +                    assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE));
394 +                }
395 +            }
396 +        }
397 +    }
398  
399      /**
400 <     *  Timed wait without holding lock throws
401 <     *  IllegalMonitorStateException
400 >     * toSeconds saturates positive too-large values to Long.MAX_VALUE
401 >     * and negative to LONG.MIN_VALUE
402       */
403 <    public void testTimedWait_IllegalMonitorException() {
404 <        //created a new thread with anonymous runnable
403 >    public void testToSecondsSaturate() {
404 >        for (TimeUnit x : TimeUnit.values()) {
405 >            long ratio = x.toNanos(1) / SECONDS.toNanos(1);
406 >            if (ratio >= 1) {
407 >                long max = Long.MAX_VALUE/ratio;
408 >                for (long z : new long[] {0, 1, -1, max, -max})
409 >                    assertEquals(z * ratio, x.toSeconds(z));
410 >                if (max < Long.MAX_VALUE) {
411 >                    assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1));
412 >                    assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1));
413 >                    assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1));
414 >                }
415 >                assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE));
416 >                assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE));
417 >                if (max < Integer.MAX_VALUE) {
418 >                    assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE));
419 >                    assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE));
420 >                }
421 >            }
422 >        }
423 >    }
424  
425 <        Thread t = new Thread(new Runnable() {
426 <                public void run() {
427 <                    Object o = new Object();
428 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
429 <                    try {
430 <                        tu.timedWait(o,LONG_DELAY_MS);
431 <                        threadShouldThrow();
432 <                    }
433 <                    catch (InterruptedException ie) {
434 <                        threadUnexpectedException();
435 <                    }
436 <                    catch (IllegalMonitorStateException success) {
437 <                    }
425 >    /**
426 >     * toMinutes saturates positive too-large values to Long.MAX_VALUE
427 >     * and negative to LONG.MIN_VALUE
428 >     */
429 >    public void testToMinutesSaturate() {
430 >        for (TimeUnit x : TimeUnit.values()) {
431 >            long ratio = x.toNanos(1) / MINUTES.toNanos(1);
432 >            if (ratio > 1) {
433 >                long max = Long.MAX_VALUE/ratio;
434 >                for (long z : new long[] {0, 1, -1, max, -max})
435 >                    assertEquals(z * ratio, x.toMinutes(z));
436 >                assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1));
437 >                assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1));
438 >                assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE));
439 >                assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE));
440 >                assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1));
441 >            }
442 >        }
443 >    }
444  
445 +    /**
446 +     * toHours saturates positive too-large values to Long.MAX_VALUE
447 +     * and negative to LONG.MIN_VALUE
448 +     */
449 +    public void testToHoursSaturate() {
450 +        for (TimeUnit x : TimeUnit.values()) {
451 +            long ratio = x.toNanos(1) / HOURS.toNanos(1);
452 +            if (ratio >= 1) {
453 +                long max = Long.MAX_VALUE/ratio;
454 +                for (long z : new long[] {0, 1, -1, max, -max})
455 +                    assertEquals(z * ratio, x.toHours(z));
456 +                if (max < Long.MAX_VALUE) {
457 +                    assertEquals(Long.MAX_VALUE, x.toHours(max + 1));
458 +                    assertEquals(Long.MIN_VALUE, x.toHours(-max - 1));
459 +                    assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1));
460                  }
461 <            });
462 <        t.start();
463 <        try {
361 <            Thread.sleep(SHORT_DELAY_MS);
362 <            t.interrupt();
363 <            t.join();
364 <        } catch (Exception e) {
365 <            unexpectedException();
461 >                assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE));
462 >                assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE));
463 >            }
464          }
465      }
466  
467      /**
468 <     * timedWait throws InterruptedException when interrupted
468 >     * toString returns name of unit
469       */
470 <    public void testTimedWait() {
471 <        Thread t = new Thread(new Runnable() {
472 <                public void run() {
473 <                    Object o = new Object();
474 <
475 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
476 <                    try {
477 <                        synchronized(o) {
478 <                            tu.timedWait(o,MEDIUM_DELAY_MS);
381 <                        }
382 <                        threadShouldThrow();
383 <                    }
384 <                    catch (InterruptedException success) {}
385 <                    catch (IllegalMonitorStateException failure) {
386 <                        threadUnexpectedException();
387 <                    }
388 <                }
389 <            });
390 <        t.start();
391 <        try {
392 <            Thread.sleep(SHORT_DELAY_MS);
393 <            t.interrupt();
394 <            t.join();
395 <        } catch (Exception e) {
396 <            unexpectedException();
397 <        }
470 >    public void testToString() {
471 >        assertEquals("SECONDS", SECONDS.toString());
472 >    }
473 >
474 >    /**
475 >     * name returns name of unit
476 >     */
477 >    public void testName() {
478 >        assertEquals("SECONDS", SECONDS.name());
479      }
480  
481 +    /**
482 +     * Timed wait without holding lock throws
483 +     * IllegalMonitorStateException
484 +     */
485 +    public void testTimedWait_IllegalMonitorException() {
486 +        Thread t = newStartedThread(new CheckedRunnable() {
487 +            public void realRun() throws InterruptedException {
488 +                Object o = new Object();
489 +                TimeUnit tu = MILLISECONDS;
490 +
491 +                try {
492 +                    tu.timedWait(o, LONG_DELAY_MS);
493 +                    threadShouldThrow();
494 +                } catch (IllegalMonitorStateException success) {}
495 +            }});
496 +
497 +        awaitTermination(t);
498 +    }
499 +
500 +    /**
501 +     * timedWait throws InterruptedException when interrupted
502 +     */
503 +    public void testTimedWait_Interruptible() {
504 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
505 +        Thread t = newStartedThread(new CheckedRunnable() {
506 +            public void realRun() throws InterruptedException {
507 +                Object o = new Object();
508 +                TimeUnit tu = MILLISECONDS;
509 +
510 +                Thread.currentThread().interrupt();
511 +                try {
512 +                    synchronized (o) {
513 +                        tu.timedWait(o, LONG_DELAY_MS);
514 +                    }
515 +                    shouldThrow();
516 +                } catch (InterruptedException success) {}
517 +                assertFalse(Thread.interrupted());
518 +
519 +                pleaseInterrupt.countDown();
520 +                try {
521 +                    synchronized (o) {
522 +                        tu.timedWait(o, LONG_DELAY_MS);
523 +                    }
524 +                    shouldThrow();
525 +                } catch (InterruptedException success) {}
526 +                assertFalse(Thread.interrupted());
527 +            }});
528 +
529 +        await(pleaseInterrupt);
530 +        assertThreadStaysAlive(t);
531 +        t.interrupt();
532 +        awaitTermination(t);
533 +    }
534  
535      /**
536       * timedJoin throws InterruptedException when interrupted
537       */
538 <    public void testTimedJoin() {
539 <        Thread t = new Thread(new Runnable() {
540 <                public void run() {
541 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
542 <                    try {
543 <                        Thread s = new Thread(new Runnable() {
544 <                                public void run() {
545 <                                    try {
546 <                                        Thread.sleep(MEDIUM_DELAY_MS);
547 <                                    } catch (InterruptedException success) {}
548 <                                }
549 <                            });
550 <                        s.start();
551 <                        tu.timedJoin(s,MEDIUM_DELAY_MS);
552 <                        threadShouldThrow();
553 <                    }
554 <                    catch (Exception e) {}
555 <                }
556 <            });
557 <        t.start();
558 <        try {
559 <            Thread.sleep(SHORT_DELAY_MS);
560 <            t.interrupt();
561 <            t.join();
562 <        } catch (Exception e) {
563 <            unexpectedException();
564 <        }
565 <    }
566 <
567 <    /**
568 <     *  timedSleep throws InterruptedException when interrupted
569 <     */
570 <    public void testTimedSleep() {
571 <        //created a new thread with anonymous runnable
572 <
573 <        Thread t = new Thread(new Runnable() {
574 <                public void run() {
575 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
576 <                    try {
577 <                        tu.sleep(MEDIUM_DELAY_MS);
578 <                        threadShouldThrow();
579 <                    }
580 <                    catch (InterruptedException success) {}
581 <                }
582 <            });
583 <        t.start();
584 <        try {
585 <            Thread.sleep(SHORT_DELAY_MS);
586 <            t.interrupt();
587 <            t.join();
588 <        } catch (Exception e) {
589 <            unexpectedException();
590 <        }
591 <    }
592 <
593 <    /**
594 <     * a deserialized serialized unit is equal
595 <     */
596 <    public void testSerialization() {
597 <        TimeUnit q = TimeUnit.MILLISECONDS;
598 <
599 <        try {
600 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
601 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
602 <            out.writeObject(q);
603 <            out.close();
604 <
605 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
606 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
607 <            TimeUnit r = (TimeUnit)in.readObject();
608 <
609 <            assertEquals(q.toString(), r.toString());
610 <        } catch (Exception e) {
611 <            e.printStackTrace();
612 <            unexpectedException();
538 >    public void testTimedJoin_Interruptible() {
539 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
540 >        final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
541 >            public void realRun() throws InterruptedException {
542 >                Thread.sleep(LONG_DELAY_MS);
543 >            }});
544 >        final Thread t = newStartedThread(new CheckedRunnable() {
545 >            public void realRun() throws InterruptedException {
546 >                TimeUnit tu = MILLISECONDS;
547 >                Thread.currentThread().interrupt();
548 >                try {
549 >                    tu.timedJoin(s, LONG_DELAY_MS);
550 >                    shouldThrow();
551 >                } catch (InterruptedException success) {}
552 >                assertFalse(Thread.interrupted());
553 >
554 >                pleaseInterrupt.countDown();
555 >                try {
556 >                    tu.timedJoin(s, LONG_DELAY_MS);
557 >                    shouldThrow();
558 >                } catch (InterruptedException success) {}
559 >                assertFalse(Thread.interrupted());
560 >            }});
561 >
562 >        await(pleaseInterrupt);
563 >        assertThreadStaysAlive(t);
564 >        t.interrupt();
565 >        awaitTermination(t);
566 >        s.interrupt();
567 >        awaitTermination(s);
568 >    }
569 >
570 >    /**
571 >     * timedSleep throws InterruptedException when interrupted
572 >     */
573 >    public void testTimedSleep_Interruptible() {
574 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
575 >        Thread t = newStartedThread(new CheckedRunnable() {
576 >            public void realRun() throws InterruptedException {
577 >                TimeUnit tu = MILLISECONDS;
578 >                Thread.currentThread().interrupt();
579 >                try {
580 >                    tu.sleep(LONG_DELAY_MS);
581 >                    shouldThrow();
582 >                } catch (InterruptedException success) {}
583 >                assertFalse(Thread.interrupted());
584 >
585 >                pleaseInterrupt.countDown();
586 >                try {
587 >                    tu.sleep(LONG_DELAY_MS);
588 >                    shouldThrow();
589 >                } catch (InterruptedException success) {}
590 >                assertFalse(Thread.interrupted());
591 >            }});
592 >
593 >        await(pleaseInterrupt);
594 >        assertThreadStaysAlive(t);
595 >        t.interrupt();
596 >        awaitTermination(t);
597 >    }
598 >
599 >    /**
600 >     * a deserialized serialized unit is the same instance
601 >     */
602 >    public void testSerialization() throws Exception {
603 >        for (TimeUnit x : TimeUnit.values())
604 >            assertSame(x, serialClone(x));
605 >    }
606 >
607 >    /**
608 >     * tests for toChronoUnit.
609 >     */
610 >    public void testToChronoUnit() throws Exception {
611 >        assertSame(ChronoUnit.NANOS,   NANOSECONDS.toChronoUnit());
612 >        assertSame(ChronoUnit.MICROS,  MICROSECONDS.toChronoUnit());
613 >        assertSame(ChronoUnit.MILLIS,  MILLISECONDS.toChronoUnit());
614 >        assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
615 >        assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
616 >        assertSame(ChronoUnit.HOURS,   HOURS.toChronoUnit());
617 >        assertSame(ChronoUnit.DAYS,    DAYS.toChronoUnit());
618 >
619 >        // Every TimeUnit has a defined ChronoUnit equivalent
620 >        for (TimeUnit x : TimeUnit.values())
621 >            assertSame(x, TimeUnit.of(x.toChronoUnit()));
622 >    }
623 >
624 >    /**
625 >     * tests for TimeUnit.of(ChronoUnit).
626 >     */
627 >    public void testTimeUnitOf() throws Exception {
628 >        assertSame(NANOSECONDS,  TimeUnit.of(ChronoUnit.NANOS));
629 >        assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS));
630 >        assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS));
631 >        assertSame(SECONDS,      TimeUnit.of(ChronoUnit.SECONDS));
632 >        assertSame(MINUTES,      TimeUnit.of(ChronoUnit.MINUTES));
633 >        assertSame(HOURS,        TimeUnit.of(ChronoUnit.HOURS));
634 >        assertSame(DAYS,         TimeUnit.of(ChronoUnit.DAYS));
635 >
636 >        assertThrows(NullPointerException.class,
637 >                     () -> TimeUnit.of((ChronoUnit)null));
638 >
639 >        // ChronoUnits either round trip to their TimeUnit
640 >        // equivalents, or throw IllegalArgumentException.
641 >        for (ChronoUnit cu : ChronoUnit.values()) {
642 >            final TimeUnit tu;
643 >            try {
644 >                tu = TimeUnit.of(cu);
645 >            } catch (IllegalArgumentException acceptable) {
646 >                continue;
647 >            }
648 >            assertSame(cu, tu.toChronoUnit());
649          }
650      }
651  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines