1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
|
|
* members of JCP JSR-166 Expert Group and released to the public |
4 |
|
|
* domain, as explained at |
5 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import static java.util.concurrent.TimeUnit.DAYS; |
9 |
|
|
import static java.util.concurrent.TimeUnit.HOURS; |
10 |
|
|
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
11 |
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
|
import static java.util.concurrent.TimeUnit.MINUTES; |
13 |
|
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
14 |
|
|
import static java.util.concurrent.TimeUnit.SECONDS; |
15 |
|
|
|
16 |
jsr166 |
1.2 |
import java.time.Duration; |
17 |
jsr166 |
1.1 |
import java.time.temporal.ChronoUnit; |
18 |
jsr166 |
1.2 |
import java.util.Arrays; |
19 |
|
|
import java.util.concurrent.ThreadLocalRandom; |
20 |
jsr166 |
1.1 |
import java.util.concurrent.TimeUnit; |
21 |
jsr166 |
1.2 |
import java.util.stream.LongStream; |
22 |
jsr166 |
1.1 |
|
23 |
|
|
import junit.framework.Test; |
24 |
|
|
import junit.framework.TestSuite; |
25 |
|
|
|
26 |
|
|
public class TimeUnit8Test extends JSR166TestCase { |
27 |
|
|
public static void main(String[] args) { |
28 |
|
|
main(suite(), args); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
public static Test suite() { |
32 |
|
|
return new TestSuite(TimeUnit8Test.class); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* tests for toChronoUnit. |
37 |
|
|
*/ |
38 |
|
|
public void testToChronoUnit() throws Exception { |
39 |
|
|
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); |
40 |
|
|
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); |
41 |
|
|
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); |
42 |
|
|
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); |
43 |
|
|
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); |
44 |
|
|
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); |
45 |
|
|
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); |
46 |
|
|
|
47 |
|
|
// Every TimeUnit has a defined ChronoUnit equivalent |
48 |
|
|
for (TimeUnit x : TimeUnit.values()) |
49 |
|
|
assertSame(x, TimeUnit.of(x.toChronoUnit())); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
/** |
53 |
|
|
* tests for TimeUnit.of(ChronoUnit). |
54 |
|
|
*/ |
55 |
|
|
public void testTimeUnitOf() throws Exception { |
56 |
|
|
assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); |
57 |
|
|
assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); |
58 |
|
|
assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); |
59 |
|
|
assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); |
60 |
|
|
assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); |
61 |
|
|
assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); |
62 |
|
|
assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); |
63 |
|
|
|
64 |
|
|
assertThrows(NullPointerException.class, |
65 |
|
|
() -> TimeUnit.of((ChronoUnit)null)); |
66 |
|
|
|
67 |
|
|
// ChronoUnits either round trip to their TimeUnit |
68 |
|
|
// equivalents, or throw IllegalArgumentException. |
69 |
|
|
for (ChronoUnit cu : ChronoUnit.values()) { |
70 |
|
|
final TimeUnit tu; |
71 |
|
|
try { |
72 |
|
|
tu = TimeUnit.of(cu); |
73 |
|
|
} catch (IllegalArgumentException acceptable) { |
74 |
|
|
continue; |
75 |
|
|
} |
76 |
|
|
assertSame(cu, tu.toChronoUnit()); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
jsr166 |
1.2 |
/** |
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 |
jsr166 |
1.1 |
} |