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.27 by jsr166, Sat Jan 30 22:24:32 2016 UTC vs.
Revision 1.29 by jsr166, Fri Mar 25 04:53:28 2016 UTC

# Line 97 | Line 97 | public class TimeUnitTest extends JSR166
97              assertEquals(t,
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  
111      /**
# Line 281 | Line 290 | public class TimeUnitTest extends JSR166
290                       NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
291          assertEquals(Long.MIN_VALUE,
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 292 | Line 322 | public class TimeUnitTest extends JSR166
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 +     * toMicros saturates positive too-large values to Long.MAX_VALUE
349 +     * and negative to LONG.MIN_VALUE
350 +     */
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 +     * toSeconds saturates positive too-large values to Long.MAX_VALUE
401 +     * and negative to LONG.MIN_VALUE
402 +     */
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 +    /**
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 +                assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE));
462 +                assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE));
463 +            }
464 +        }
465      }
466  
467      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines