38 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
38 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
39 import static java.util.concurrent.TimeUnit.MINUTES; |
39 import static java.util.concurrent.TimeUnit.MINUTES; |
40 import static java.util.concurrent.TimeUnit.NANOSECONDS; |
40 import static java.util.concurrent.TimeUnit.NANOSECONDS; |
41 import static java.util.concurrent.TimeUnit.SECONDS; |
41 import static java.util.concurrent.TimeUnit.SECONDS; |
42 |
42 |
|
43 import java.time.Duration; |
43 import java.time.temporal.ChronoUnit; |
44 import java.time.temporal.ChronoUnit; |
|
45 import java.util.Arrays; |
|
46 import java.util.concurrent.ThreadLocalRandom; |
44 import java.util.concurrent.TimeUnit; |
47 import java.util.concurrent.TimeUnit; |
|
48 import java.util.stream.LongStream; |
45 |
49 |
46 import junit.framework.Test; |
50 import junit.framework.Test; |
47 import junit.framework.TestSuite; |
51 import junit.framework.TestSuite; |
48 |
52 |
49 public class TimeUnit8Test extends JSR166TestCase { |
53 public class TimeUnit8Test extends JSR166TestCase { |
98 } |
102 } |
99 assertSame(cu, tu.toChronoUnit()); |
103 assertSame(cu, tu.toChronoUnit()); |
100 } |
104 } |
101 } |
105 } |
102 |
106 |
|
107 /** |
|
108 * convert(Duration) roundtrips with Duration.ofXXXX and Duration.of(long, ChronoUnit) |
|
109 */ |
|
110 public void testConvertDuration_roundtripDurationOf() { |
|
111 long n = ThreadLocalRandom.current().nextLong(); |
|
112 |
|
113 assertEquals(n, NANOSECONDS.convert(Duration.ofNanos(n))); |
|
114 assertEquals(n, NANOSECONDS.convert(Duration.of(n, ChronoUnit.NANOS))); |
|
115 assertEquals(n, MILLISECONDS.convert(Duration.ofMillis(n))); |
|
116 assertEquals(n, MILLISECONDS.convert(Duration.of(n, ChronoUnit.MILLIS))); |
|
117 assertEquals(n, SECONDS.convert(Duration.ofSeconds(n))); |
|
118 assertEquals(n, SECONDS.convert(Duration.of(n, ChronoUnit.SECONDS))); |
|
119 n /= 60; |
|
120 assertEquals(n, MINUTES.convert(Duration.ofMinutes(n))); |
|
121 assertEquals(n, MINUTES.convert(Duration.of(n, ChronoUnit.MINUTES))); |
|
122 n /= 60; |
|
123 assertEquals(n, HOURS.convert(Duration.ofHours(n))); |
|
124 assertEquals(n, HOURS.convert(Duration.of(n, ChronoUnit.HOURS))); |
|
125 n /= 24; |
|
126 assertEquals(n, DAYS.convert(Duration.ofDays(n))); |
|
127 assertEquals(n, DAYS.convert(Duration.of(n, ChronoUnit.DAYS))); |
|
128 } |
|
129 |
|
130 /** |
|
131 * convert(Duration.ofNanos(n)) agrees with convert(n, NANOSECONDS) |
|
132 */ |
|
133 public void testConvertDuration_roundtripDurationOfNanos() { |
|
134 // Test values near unit transitions and near overflow. |
|
135 LongStream.concat( |
|
136 Arrays.stream(TimeUnit.values()).mapToLong(u -> u.toNanos(1)), |
|
137 LongStream.of(Long.MAX_VALUE, Long.MIN_VALUE)) |
|
138 .flatMap(n -> LongStream.of(n, n + 1, n - 1)) |
|
139 .flatMap(n -> LongStream.of(n, n + 1_000_000_000, n - 1_000_000_000)) |
|
140 .flatMap(n -> LongStream.of(n, -n)) |
|
141 // .peek(System.err::println) |
|
142 .forEach(n -> Arrays.stream(TimeUnit.values()).forEach( |
|
143 u -> assertEquals(u.convert(n, NANOSECONDS), |
|
144 u.convert(Duration.ofNanos(n))))); |
|
145 } |
|
146 |
|
147 /** |
|
148 * convert(Duration) doesn't misbehave near Long.MAX_VALUE and Long.MIN_VALUE. |
|
149 */ |
|
150 public void testConvertDuration_nearOverflow() { |
|
151 ChronoUnit NANOS = ChronoUnit.NANOS; |
|
152 Duration maxDuration = Duration.ofSeconds(Long.MAX_VALUE, 999_999_999); |
|
153 Duration minDuration = Duration.ofSeconds(Long.MIN_VALUE, 0); |
|
154 |
|
155 for (TimeUnit u : TimeUnit.values()) { |
|
156 ChronoUnit cu = u.toChronoUnit(); |
|
157 long r; |
|
158 if (u.toNanos(1) > SECONDS.toNanos(1)) { |
|
159 r = u.toNanos(1) / SECONDS.toNanos(1); |
|
160 |
|
161 assertThrows(ArithmeticException.class, |
|
162 () -> Duration.of(Long.MAX_VALUE, cu), |
|
163 () -> Duration.of(Long.MIN_VALUE, cu)); |
|
164 } else { |
|
165 r = 1; |
|
166 |
|
167 Duration max = Duration.of(Long.MAX_VALUE, cu); |
|
168 Duration min = Duration.of(Long.MIN_VALUE, cu); |
|
169 assertEquals(Long.MAX_VALUE, u.convert(max)); |
|
170 assertEquals(Long.MAX_VALUE - 1, u.convert(max.minus(1, NANOS))); |
|
171 assertEquals(Long.MAX_VALUE - 1, u.convert(max.minus(1, cu))); |
|
172 assertEquals(Long.MIN_VALUE, u.convert(min)); |
|
173 assertEquals(Long.MIN_VALUE + 1, u.convert(min.plus(1, NANOS))); |
|
174 assertEquals(Long.MIN_VALUE + 1, u.convert(min.plus(1, cu))); |
|
175 assertEquals(Long.MAX_VALUE, u.convert(max.plus(1, NANOS))); |
|
176 if (u != SECONDS) { |
|
177 assertEquals(Long.MAX_VALUE, u.convert(max.plus(1, cu))); |
|
178 assertEquals(Long.MIN_VALUE, u.convert(min.minus(1, NANOS))); |
|
179 assertEquals(Long.MIN_VALUE, u.convert(min.minus(1, cu))); |
|
180 } |
|
181 } |
|
182 |
|
183 assertEquals(Long.MAX_VALUE / r, u.convert(maxDuration)); |
|
184 assertEquals(Long.MIN_VALUE / r, u.convert(minDuration)); |
|
185 } |
|
186 } |
|
187 |
103 } |
188 } |