--- jsr166/src/test/tck/TimeUnitTest.java 2016/01/30 22:24:32 1.27 +++ jsr166/src/test/tck/TimeUnitTest.java 2016/03/18 16:55:09 1.28 @@ -97,6 +97,15 @@ public class TimeUnitTest extends JSR166 assertEquals(t, NANOSECONDS.convert(t, NANOSECONDS)); } + + for (TimeUnit x : TimeUnit.values()) { + long[] zs = { + 0, 1, -1, + Integer.MAX_VALUE, Integer.MIN_VALUE, + Long.MAX_VALUE, Long.MIN_VALUE, + }; + for (long z : zs) assertEquals(z, x.convert(z, x)); + } } /** @@ -281,6 +290,25 @@ public class TimeUnitTest extends JSR166 NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); assertEquals(Long.MIN_VALUE, NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); + + for (TimeUnit x : TimeUnit.values()) + for (TimeUnit y : TimeUnit.values()) { + long ratio = x.toNanos(1) / y.toNanos(1); + if (ratio > 1) { + assertEquals(ratio, y.convert(1, x)); + assertEquals(1, x.convert(ratio, y)); + long max = Long.MAX_VALUE/ratio; + assertEquals(max * ratio, y.convert(max, x)); + assertEquals(-max * ratio, y.convert(-max, x)); + assertEquals(max, x.convert(max * ratio, y)); + assertEquals(-max, x.convert(-max * ratio, y)); + assertEquals(Long.MAX_VALUE, y.convert(max + 1, x)); + assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x)); + assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x)); + assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x)); + assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x)); + } + } } /** @@ -292,6 +320,136 @@ public class TimeUnitTest extends JSR166 MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); assertEquals(Long.MIN_VALUE, MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); + + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toNanos(z)); + assertEquals(Long.MAX_VALUE, x.toNanos(max + 1)); + assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toMicros saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToMicrosSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toMicros(z)); + assertEquals(Long.MAX_VALUE, x.toMicros(max + 1)); + assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toMillis saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToMillisSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toMillis(z)); + assertEquals(Long.MAX_VALUE, x.toMillis(max + 1)); + assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toSeconds saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToSecondsSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / SECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toSeconds(z)); + assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1)); + assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toMinutes saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToMinutesSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / MINUTES.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toMinutes(z)); + assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1)); + assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1)); + } + } + } + + /** + * toHours saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToHoursSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / HOURS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toHours(z)); + assertEquals(Long.MAX_VALUE, x.toHours(max + 1)); + assertEquals(Long.MIN_VALUE, x.toHours(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1)); + } + } } /**