13 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
14 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
15 |
|
|
16 |
+ |
import java.time.Duration; |
17 |
|
import java.time.temporal.ChronoUnit; |
18 |
+ |
import java.util.Arrays; |
19 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
20 |
|
import java.util.concurrent.TimeUnit; |
21 |
+ |
import java.util.stream.LongStream; |
22 |
|
|
23 |
|
import junit.framework.Test; |
24 |
|
import junit.framework.TestSuite; |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
+ |
/** |
81 |
+ |
* convert(Duration) roundtrips with Duration.ofXXXX and Duration.of(long, ChronoUnit) |
82 |
+ |
*/ |
83 |
+ |
public void testConvertDuration_roundtripDurationOf() { |
84 |
+ |
long n = ThreadLocalRandom.current().nextLong(); |
85 |
+ |
|
86 |
+ |
assertEquals(n, NANOSECONDS.convert(Duration.ofNanos(n))); |
87 |
+ |
assertEquals(n, NANOSECONDS.convert(Duration.of(n, ChronoUnit.NANOS))); |
88 |
+ |
assertEquals(n, MILLISECONDS.convert(Duration.ofMillis(n))); |
89 |
+ |
assertEquals(n, MILLISECONDS.convert(Duration.of(n, ChronoUnit.MILLIS))); |
90 |
+ |
assertEquals(n, SECONDS.convert(Duration.ofSeconds(n))); |
91 |
+ |
assertEquals(n, SECONDS.convert(Duration.of(n, ChronoUnit.SECONDS))); |
92 |
+ |
n /= 60; |
93 |
+ |
assertEquals(n, MINUTES.convert(Duration.ofMinutes(n))); |
94 |
+ |
assertEquals(n, MINUTES.convert(Duration.of(n, ChronoUnit.MINUTES))); |
95 |
+ |
n /= 60; |
96 |
+ |
assertEquals(n, HOURS.convert(Duration.ofHours(n))); |
97 |
+ |
assertEquals(n, HOURS.convert(Duration.of(n, ChronoUnit.HOURS))); |
98 |
+ |
n /= 24; |
99 |
+ |
assertEquals(n, DAYS.convert(Duration.ofDays(n))); |
100 |
+ |
assertEquals(n, DAYS.convert(Duration.of(n, ChronoUnit.DAYS))); |
101 |
+ |
} |
102 |
+ |
|
103 |
+ |
/** |
104 |
+ |
* convert(Duration.ofNanos(n)) agrees with convert(n, NANOSECONDS) |
105 |
+ |
*/ |
106 |
+ |
public void testConvertDuration_roundtripDurationOfNanos() { |
107 |
+ |
// Test values near unit transitions and near overflow. |
108 |
+ |
LongStream.concat( |
109 |
+ |
Arrays.stream(TimeUnit.values()).mapToLong(u -> u.toNanos(1)), |
110 |
+ |
LongStream.of(Long.MAX_VALUE, Long.MIN_VALUE)) |
111 |
+ |
.flatMap(n -> LongStream.of(n, n + 1, n - 1)) |
112 |
+ |
.flatMap(n -> LongStream.of(n, n + 1_000_000_000, n - 1_000_000_000)) |
113 |
+ |
.flatMap(n -> LongStream.of(n, -n)) |
114 |
+ |
// .peek(System.err::println) |
115 |
+ |
.forEach(n -> Arrays.stream(TimeUnit.values()).forEach( |
116 |
+ |
u -> assertEquals(u.convert(n, NANOSECONDS), |
117 |
+ |
u.convert(Duration.ofNanos(n))))); |
118 |
+ |
} |
119 |
+ |
|
120 |
+ |
/** |
121 |
+ |
* convert(Duration) doesn't misbehave near Long.MAX_VALUE and Long.MIN_VALUE. |
122 |
+ |
*/ |
123 |
+ |
public void testConvertDuration_nearOverflow() { |
124 |
+ |
ChronoUnit NANOS = ChronoUnit.NANOS; |
125 |
+ |
Duration maxDuration = Duration.ofSeconds(Long.MAX_VALUE, 999_999_999); |
126 |
+ |
Duration minDuration = Duration.ofSeconds(Long.MIN_VALUE, 0); |
127 |
+ |
|
128 |
+ |
for (TimeUnit u : TimeUnit.values()) { |
129 |
+ |
ChronoUnit cu = u.toChronoUnit(); |
130 |
+ |
long r; |
131 |
+ |
if (u.toNanos(1) > SECONDS.toNanos(1)) { |
132 |
+ |
r = u.toNanos(1) / SECONDS.toNanos(1); |
133 |
+ |
|
134 |
+ |
assertThrows(ArithmeticException.class, |
135 |
+ |
() -> Duration.of(Long.MAX_VALUE, cu), |
136 |
+ |
() -> Duration.of(Long.MIN_VALUE, cu)); |
137 |
+ |
} else { |
138 |
+ |
r = 1; |
139 |
+ |
|
140 |
+ |
Duration max = Duration.of(Long.MAX_VALUE, cu); |
141 |
+ |
Duration min = Duration.of(Long.MIN_VALUE, cu); |
142 |
+ |
assertEquals(Long.MAX_VALUE, u.convert(max)); |
143 |
+ |
assertEquals(Long.MAX_VALUE - 1, u.convert(max.minus(1, NANOS))); |
144 |
+ |
assertEquals(Long.MAX_VALUE - 1, u.convert(max.minus(1, cu))); |
145 |
+ |
assertEquals(Long.MIN_VALUE, u.convert(min)); |
146 |
+ |
assertEquals(Long.MIN_VALUE + 1, u.convert(min.plus(1, NANOS))); |
147 |
+ |
assertEquals(Long.MIN_VALUE + 1, u.convert(min.plus(1, cu))); |
148 |
+ |
assertEquals(Long.MAX_VALUE, u.convert(max.plus(1, NANOS))); |
149 |
+ |
if (u != SECONDS) { |
150 |
+ |
assertEquals(Long.MAX_VALUE, u.convert(max.plus(1, cu))); |
151 |
+ |
assertEquals(Long.MIN_VALUE, u.convert(min.minus(1, NANOS))); |
152 |
+ |
assertEquals(Long.MIN_VALUE, u.convert(min.minus(1, cu))); |
153 |
+ |
} |
154 |
+ |
} |
155 |
+ |
|
156 |
+ |
assertEquals(Long.MAX_VALUE / r, u.convert(maxDuration)); |
157 |
+ |
assertEquals(Long.MIN_VALUE / r, u.convert(minDuration)); |
158 |
+ |
} |
159 |
+ |
} |
160 |
+ |
|
161 |
|
} |