1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
import static java.util.concurrent.TimeUnit.DAYS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.HOURS; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MINUTES; |
14 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
+ |
|
17 |
+ |
import java.time.temporal.ChronoUnit; |
18 |
+ |
import java.util.concurrent.CountDownLatch; |
19 |
+ |
import java.util.concurrent.TimeUnit; |
20 |
|
|
21 |
< |
import junit.framework.*; |
22 |
< |
import java.util.concurrent.*; |
11 |
< |
import java.io.*; |
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class TimeUnitTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
26 |
< |
junit.textui.TestRunner.run(suite()); |
26 |
> |
main(suite(), args); |
27 |
|
} |
28 |
< |
|
28 |
> |
|
29 |
|
public static Test suite() { |
30 |
< |
return new TestSuite(TimeUnitTest.class); |
30 |
> |
return new TestSuite(TimeUnitTest.class); |
31 |
|
} |
32 |
|
|
33 |
+ |
// (loops to 88888 check increments at all time divisions.) |
34 |
+ |
|
35 |
+ |
/** |
36 |
+ |
* convert correctly converts sample values across the units |
37 |
+ |
*/ |
38 |
|
public void testConvert() { |
39 |
< |
for (long t = 0; t < 10; ++t) { |
40 |
< |
assertEquals(t, |
41 |
< |
TimeUnit.SECONDS.convert(t, |
42 |
< |
TimeUnit.SECONDS)); |
43 |
< |
assertEquals(t, |
44 |
< |
TimeUnit.SECONDS.convert(1000 * t, |
45 |
< |
TimeUnit.MILLISECONDS)); |
46 |
< |
assertEquals(t, |
47 |
< |
TimeUnit.SECONDS.convert(1000000 * t, |
48 |
< |
TimeUnit.MICROSECONDS)); |
49 |
< |
assertEquals(t, |
50 |
< |
TimeUnit.SECONDS.convert(1000000000 * t, |
51 |
< |
TimeUnit.NANOSECONDS)); |
52 |
< |
assertEquals(1000 * t, |
53 |
< |
TimeUnit.MILLISECONDS.convert(t, |
54 |
< |
TimeUnit.SECONDS)); |
55 |
< |
assertEquals(t, |
56 |
< |
TimeUnit.MILLISECONDS.convert(t, |
57 |
< |
TimeUnit.MILLISECONDS)); |
58 |
< |
assertEquals(t, |
59 |
< |
TimeUnit.MILLISECONDS.convert(1000 * t, |
60 |
< |
TimeUnit.MICROSECONDS)); |
61 |
< |
assertEquals(t, |
62 |
< |
TimeUnit.MILLISECONDS.convert(1000000 * t, |
63 |
< |
TimeUnit.NANOSECONDS)); |
64 |
< |
assertEquals(1000000 * t, |
65 |
< |
TimeUnit.MICROSECONDS.convert(t, |
66 |
< |
TimeUnit.SECONDS)); |
67 |
< |
assertEquals(1000 * t, |
68 |
< |
TimeUnit.MICROSECONDS.convert(t, |
69 |
< |
TimeUnit.MILLISECONDS)); |
70 |
< |
assertEquals(t, |
71 |
< |
TimeUnit.MICROSECONDS.convert(t, |
72 |
< |
TimeUnit.MICROSECONDS)); |
73 |
< |
assertEquals(t, |
74 |
< |
TimeUnit.MICROSECONDS.convert(1000 * t, |
75 |
< |
TimeUnit.NANOSECONDS)); |
76 |
< |
assertEquals(1000000000 * t, |
77 |
< |
TimeUnit.NANOSECONDS.convert(t, |
78 |
< |
TimeUnit.SECONDS)); |
79 |
< |
assertEquals(1000000 * t, |
80 |
< |
TimeUnit.NANOSECONDS.convert(t, |
81 |
< |
TimeUnit.MILLISECONDS)); |
82 |
< |
assertEquals(1000 * t, |
83 |
< |
TimeUnit.NANOSECONDS.convert(t, |
84 |
< |
TimeUnit.MICROSECONDS)); |
85 |
< |
assertEquals(t, |
86 |
< |
TimeUnit.NANOSECONDS.convert(t, |
87 |
< |
TimeUnit.NANOSECONDS)); |
39 |
> |
for (long t = 0; t < 88888; ++t) { |
40 |
> |
assertEquals(t*60*60*24, |
41 |
> |
SECONDS.convert(t, DAYS)); |
42 |
> |
assertEquals(t*60*60, |
43 |
> |
SECONDS.convert(t, HOURS)); |
44 |
> |
assertEquals(t*60, |
45 |
> |
SECONDS.convert(t, MINUTES)); |
46 |
> |
assertEquals(t, |
47 |
> |
SECONDS.convert(t, SECONDS)); |
48 |
> |
assertEquals(t, |
49 |
> |
SECONDS.convert(1000L*t, MILLISECONDS)); |
50 |
> |
assertEquals(t, |
51 |
> |
SECONDS.convert(1000000L*t, MICROSECONDS)); |
52 |
> |
assertEquals(t, |
53 |
> |
SECONDS.convert(1000000000L*t, NANOSECONDS)); |
54 |
> |
|
55 |
> |
assertEquals(1000L*t*60*60*24, |
56 |
> |
MILLISECONDS.convert(t, DAYS)); |
57 |
> |
assertEquals(1000L*t*60*60, |
58 |
> |
MILLISECONDS.convert(t, HOURS)); |
59 |
> |
assertEquals(1000L*t*60, |
60 |
> |
MILLISECONDS.convert(t, MINUTES)); |
61 |
> |
assertEquals(1000L*t, |
62 |
> |
MILLISECONDS.convert(t, SECONDS)); |
63 |
> |
assertEquals(t, |
64 |
> |
MILLISECONDS.convert(t, MILLISECONDS)); |
65 |
> |
assertEquals(t, |
66 |
> |
MILLISECONDS.convert(1000L*t, MICROSECONDS)); |
67 |
> |
assertEquals(t, |
68 |
> |
MILLISECONDS.convert(1000000L*t, NANOSECONDS)); |
69 |
> |
|
70 |
> |
assertEquals(1000000L*t*60*60*24, |
71 |
> |
MICROSECONDS.convert(t, DAYS)); |
72 |
> |
assertEquals(1000000L*t*60*60, |
73 |
> |
MICROSECONDS.convert(t, HOURS)); |
74 |
> |
assertEquals(1000000L*t*60, |
75 |
> |
MICROSECONDS.convert(t, MINUTES)); |
76 |
> |
assertEquals(1000000L*t, |
77 |
> |
MICROSECONDS.convert(t, SECONDS)); |
78 |
> |
assertEquals(1000L*t, |
79 |
> |
MICROSECONDS.convert(t, MILLISECONDS)); |
80 |
> |
assertEquals(t, |
81 |
> |
MICROSECONDS.convert(t, MICROSECONDS)); |
82 |
> |
assertEquals(t, |
83 |
> |
MICROSECONDS.convert(1000L*t, NANOSECONDS)); |
84 |
> |
|
85 |
> |
assertEquals(1000000000L*t*60*60*24, |
86 |
> |
NANOSECONDS.convert(t, DAYS)); |
87 |
> |
assertEquals(1000000000L*t*60*60, |
88 |
> |
NANOSECONDS.convert(t, HOURS)); |
89 |
> |
assertEquals(1000000000L*t*60, |
90 |
> |
NANOSECONDS.convert(t, MINUTES)); |
91 |
> |
assertEquals(1000000000L*t, |
92 |
> |
NANOSECONDS.convert(t, SECONDS)); |
93 |
> |
assertEquals(1000000L*t, |
94 |
> |
NANOSECONDS.convert(t, MILLISECONDS)); |
95 |
> |
assertEquals(1000L*t, |
96 |
> |
NANOSECONDS.convert(t, MICROSECONDS)); |
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 |
+ |
/** |
112 |
+ |
* toNanos correctly converts sample values in different units to |
113 |
+ |
* nanoseconds |
114 |
+ |
*/ |
115 |
|
public void testToNanos() { |
116 |
< |
for (long t = 0; t < 10; ++t) { |
117 |
< |
assertEquals(1000000000 * t, |
118 |
< |
TimeUnit.SECONDS.toNanos(t)); |
119 |
< |
|
120 |
< |
assertEquals(1000000 * t, |
121 |
< |
TimeUnit.MILLISECONDS.toNanos(t)); |
122 |
< |
assertEquals(1000 * t, |
123 |
< |
TimeUnit.MICROSECONDS.toNanos(t)); |
124 |
< |
assertEquals(t, |
125 |
< |
TimeUnit.NANOSECONDS.toNanos(t)); |
116 |
> |
for (long t = 0; t < 88888; ++t) { |
117 |
> |
assertEquals(t*1000000000L*60*60*24, |
118 |
> |
DAYS.toNanos(t)); |
119 |
> |
assertEquals(t*1000000000L*60*60, |
120 |
> |
HOURS.toNanos(t)); |
121 |
> |
assertEquals(t*1000000000L*60, |
122 |
> |
MINUTES.toNanos(t)); |
123 |
> |
assertEquals(1000000000L*t, |
124 |
> |
SECONDS.toNanos(t)); |
125 |
> |
assertEquals(1000000L*t, |
126 |
> |
MILLISECONDS.toNanos(t)); |
127 |
> |
assertEquals(1000L*t, |
128 |
> |
MICROSECONDS.toNanos(t)); |
129 |
> |
assertEquals(t, |
130 |
> |
NANOSECONDS.toNanos(t)); |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
+ |
/** |
135 |
+ |
* toMicros correctly converts sample values in different units to |
136 |
+ |
* microseconds |
137 |
+ |
*/ |
138 |
|
public void testToMicros() { |
139 |
< |
for (long t = 0; t < 10; ++t) { |
140 |
< |
assertEquals(1000000 * t, |
141 |
< |
TimeUnit.SECONDS.toMicros(t)); |
142 |
< |
|
143 |
< |
assertEquals(1000 * t, |
144 |
< |
TimeUnit.MILLISECONDS.toMicros(t)); |
145 |
< |
assertEquals(t, |
146 |
< |
TimeUnit.MICROSECONDS.toMicros(t)); |
147 |
< |
assertEquals(t, |
148 |
< |
TimeUnit.NANOSECONDS.toMicros(t * 1000)); |
139 |
> |
for (long t = 0; t < 88888; ++t) { |
140 |
> |
assertEquals(t*1000000L*60*60*24, |
141 |
> |
DAYS.toMicros(t)); |
142 |
> |
assertEquals(t*1000000L*60*60, |
143 |
> |
HOURS.toMicros(t)); |
144 |
> |
assertEquals(t*1000000L*60, |
145 |
> |
MINUTES.toMicros(t)); |
146 |
> |
assertEquals(1000000L*t, |
147 |
> |
SECONDS.toMicros(t)); |
148 |
> |
assertEquals(1000L*t, |
149 |
> |
MILLISECONDS.toMicros(t)); |
150 |
> |
assertEquals(t, |
151 |
> |
MICROSECONDS.toMicros(t)); |
152 |
> |
assertEquals(t, |
153 |
> |
NANOSECONDS.toMicros(t*1000L)); |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
+ |
/** |
158 |
+ |
* toMillis correctly converts sample values in different units to |
159 |
+ |
* milliseconds |
160 |
+ |
*/ |
161 |
|
public void testToMillis() { |
162 |
< |
for (long t = 0; t < 10; ++t) { |
163 |
< |
assertEquals(1000 * t, |
164 |
< |
TimeUnit.SECONDS.toMillis(t)); |
165 |
< |
|
166 |
< |
assertEquals(t, |
167 |
< |
TimeUnit.MILLISECONDS.toMillis(t)); |
168 |
< |
assertEquals(t, |
169 |
< |
TimeUnit.MICROSECONDS.toMillis(t * 1000)); |
170 |
< |
assertEquals(t, |
171 |
< |
TimeUnit.NANOSECONDS.toMillis(t * 1000000)); |
162 |
> |
for (long t = 0; t < 88888; ++t) { |
163 |
> |
assertEquals(t*1000L*60*60*24, |
164 |
> |
DAYS.toMillis(t)); |
165 |
> |
assertEquals(t*1000L*60*60, |
166 |
> |
HOURS.toMillis(t)); |
167 |
> |
assertEquals(t*1000L*60, |
168 |
> |
MINUTES.toMillis(t)); |
169 |
> |
assertEquals(1000L*t, |
170 |
> |
SECONDS.toMillis(t)); |
171 |
> |
assertEquals(t, |
172 |
> |
MILLISECONDS.toMillis(t)); |
173 |
> |
assertEquals(t, |
174 |
> |
MICROSECONDS.toMillis(t*1000L)); |
175 |
> |
assertEquals(t, |
176 |
> |
NANOSECONDS.toMillis(t*1000000L)); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
+ |
/** |
181 |
+ |
* toSeconds correctly converts sample values in different units to |
182 |
+ |
* seconds |
183 |
+ |
*/ |
184 |
|
public void testToSeconds() { |
185 |
< |
for (long t = 0; t < 10; ++t) { |
186 |
< |
assertEquals(t, |
187 |
< |
TimeUnit.SECONDS.toSeconds(t)); |
188 |
< |
|
189 |
< |
assertEquals(t, |
190 |
< |
TimeUnit.MILLISECONDS.toSeconds(t * 1000)); |
191 |
< |
assertEquals(t, |
192 |
< |
TimeUnit.MICROSECONDS.toSeconds(t * 1000000)); |
193 |
< |
assertEquals(t, |
194 |
< |
TimeUnit.NANOSECONDS.toSeconds(t * 1000000000)); |
185 |
> |
for (long t = 0; t < 88888; ++t) { |
186 |
> |
assertEquals(t*60*60*24, |
187 |
> |
DAYS.toSeconds(t)); |
188 |
> |
assertEquals(t*60*60, |
189 |
> |
HOURS.toSeconds(t)); |
190 |
> |
assertEquals(t*60, |
191 |
> |
MINUTES.toSeconds(t)); |
192 |
> |
assertEquals(t, |
193 |
> |
SECONDS.toSeconds(t)); |
194 |
> |
assertEquals(t, |
195 |
> |
MILLISECONDS.toSeconds(t*1000L)); |
196 |
> |
assertEquals(t, |
197 |
> |
MICROSECONDS.toSeconds(t*1000000L)); |
198 |
> |
assertEquals(t, |
199 |
> |
NANOSECONDS.toSeconds(t*1000000000L)); |
200 |
|
} |
201 |
|
} |
202 |
|
|
203 |
+ |
/** |
204 |
+ |
* toMinutes correctly converts sample values in different units to |
205 |
+ |
* minutes |
206 |
+ |
*/ |
207 |
+ |
public void testToMinutes() { |
208 |
+ |
for (long t = 0; t < 88888; ++t) { |
209 |
+ |
assertEquals(t*60*24, |
210 |
+ |
DAYS.toMinutes(t)); |
211 |
+ |
assertEquals(t*60, |
212 |
+ |
HOURS.toMinutes(t)); |
213 |
+ |
assertEquals(t, |
214 |
+ |
MINUTES.toMinutes(t)); |
215 |
+ |
assertEquals(t, |
216 |
+ |
SECONDS.toMinutes(t*60)); |
217 |
+ |
assertEquals(t, |
218 |
+ |
MILLISECONDS.toMinutes(t*1000L*60)); |
219 |
+ |
assertEquals(t, |
220 |
+ |
MICROSECONDS.toMinutes(t*1000000L*60)); |
221 |
+ |
assertEquals(t, |
222 |
+ |
NANOSECONDS.toMinutes(t*1000000000L*60)); |
223 |
+ |
} |
224 |
+ |
} |
225 |
|
|
226 |
+ |
/** |
227 |
+ |
* toHours correctly converts sample values in different units to |
228 |
+ |
* hours |
229 |
+ |
*/ |
230 |
+ |
public void testToHours() { |
231 |
+ |
for (long t = 0; t < 88888; ++t) { |
232 |
+ |
assertEquals(t*24, |
233 |
+ |
DAYS.toHours(t)); |
234 |
+ |
assertEquals(t, |
235 |
+ |
HOURS.toHours(t)); |
236 |
+ |
assertEquals(t, |
237 |
+ |
MINUTES.toHours(t*60)); |
238 |
+ |
assertEquals(t, |
239 |
+ |
SECONDS.toHours(t*60*60)); |
240 |
+ |
assertEquals(t, |
241 |
+ |
MILLISECONDS.toHours(t*1000L*60*60)); |
242 |
+ |
assertEquals(t, |
243 |
+ |
MICROSECONDS.toHours(t*1000000L*60*60)); |
244 |
+ |
assertEquals(t, |
245 |
+ |
NANOSECONDS.toHours(t*1000000000L*60*60)); |
246 |
+ |
} |
247 |
+ |
} |
248 |
+ |
|
249 |
+ |
/** |
250 |
+ |
* toDays correctly converts sample values in different units to |
251 |
+ |
* days |
252 |
+ |
*/ |
253 |
+ |
public void testToDays() { |
254 |
+ |
for (long t = 0; t < 88888; ++t) { |
255 |
+ |
assertEquals(t, |
256 |
+ |
DAYS.toDays(t)); |
257 |
+ |
assertEquals(t, |
258 |
+ |
HOURS.toDays(t*24)); |
259 |
+ |
assertEquals(t, |
260 |
+ |
MINUTES.toDays(t*60*24)); |
261 |
+ |
assertEquals(t, |
262 |
+ |
SECONDS.toDays(t*60*60*24)); |
263 |
+ |
assertEquals(t, |
264 |
+ |
MILLISECONDS.toDays(t*1000L*60*60*24)); |
265 |
+ |
assertEquals(t, |
266 |
+ |
MICROSECONDS.toDays(t*1000000L*60*60*24)); |
267 |
+ |
assertEquals(t, |
268 |
+ |
NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
269 |
+ |
} |
270 |
+ |
} |
271 |
+ |
|
272 |
+ |
/** |
273 |
+ |
* convert saturates positive too-large values to Long.MAX_VALUE |
274 |
+ |
* and negative to LONG.MIN_VALUE |
275 |
+ |
*/ |
276 |
|
public void testConvertSaturate() { |
277 |
|
assertEquals(Long.MAX_VALUE, |
278 |
< |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
135 |
< |
TimeUnit.SECONDS)); |
278 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS)); |
279 |
|
assertEquals(Long.MIN_VALUE, |
280 |
< |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
281 |
< |
TimeUnit.SECONDS)); |
280 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS)); |
281 |
> |
assertEquals(Long.MAX_VALUE, |
282 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES)); |
283 |
> |
assertEquals(Long.MIN_VALUE, |
284 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES)); |
285 |
> |
assertEquals(Long.MAX_VALUE, |
286 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS)); |
287 |
> |
assertEquals(Long.MIN_VALUE, |
288 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS)); |
289 |
> |
assertEquals(Long.MAX_VALUE, |
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 |
+ |
assertEquals(Long.MAX_VALUE, y.convert(max + 1, x)); |
306 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x)); |
307 |
+ |
assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x)); |
308 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x)); |
309 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x)); |
310 |
+ |
} |
311 |
+ |
} |
312 |
|
} |
313 |
|
|
314 |
+ |
/** |
315 |
+ |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
316 |
+ |
* and negative to LONG.MIN_VALUE |
317 |
+ |
*/ |
318 |
|
public void testToNanosSaturate() { |
319 |
< |
assertEquals(Long.MAX_VALUE, |
320 |
< |
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
321 |
< |
assertEquals(Long.MIN_VALUE, |
322 |
< |
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
323 |
< |
|
319 |
> |
assertEquals(Long.MAX_VALUE, |
320 |
> |
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
321 |
> |
assertEquals(Long.MIN_VALUE, |
322 |
> |
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
323 |
> |
|
324 |
> |
for (TimeUnit x : TimeUnit.values()) { |
325 |
> |
long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1); |
326 |
> |
if (ratio > 1) { |
327 |
> |
long max = Long.MAX_VALUE/ratio; |
328 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
329 |
> |
assertEquals(z * ratio, x.toNanos(z)); |
330 |
> |
assertEquals(Long.MAX_VALUE, x.toNanos(max + 1)); |
331 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1)); |
332 |
> |
assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE)); |
333 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE)); |
334 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1)); |
335 |
> |
if (max < Integer.MAX_VALUE) { |
336 |
> |
assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE)); |
337 |
> |
assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE)); |
338 |
> |
} |
339 |
> |
} |
340 |
> |
} |
341 |
> |
} |
342 |
> |
|
343 |
> |
/** |
344 |
> |
* toMicros saturates positive too-large values to Long.MAX_VALUE |
345 |
> |
* and negative to LONG.MIN_VALUE |
346 |
> |
*/ |
347 |
> |
public void testToMicrosSaturate() { |
348 |
> |
for (TimeUnit x : TimeUnit.values()) { |
349 |
> |
long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1); |
350 |
> |
if (ratio > 1) { |
351 |
> |
long max = Long.MAX_VALUE/ratio; |
352 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
353 |
> |
assertEquals(z * ratio, x.toMicros(z)); |
354 |
> |
assertEquals(Long.MAX_VALUE, x.toMicros(max + 1)); |
355 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1)); |
356 |
> |
assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE)); |
357 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE)); |
358 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1)); |
359 |
> |
if (max < Integer.MAX_VALUE) { |
360 |
> |
assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE)); |
361 |
> |
assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE)); |
362 |
> |
} |
363 |
> |
} |
364 |
> |
} |
365 |
> |
} |
366 |
> |
|
367 |
> |
/** |
368 |
> |
* toMillis saturates positive too-large values to Long.MAX_VALUE |
369 |
> |
* and negative to LONG.MIN_VALUE |
370 |
> |
*/ |
371 |
> |
public void testToMillisSaturate() { |
372 |
> |
for (TimeUnit x : TimeUnit.values()) { |
373 |
> |
long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1); |
374 |
> |
if (ratio > 1) { |
375 |
> |
long max = Long.MAX_VALUE/ratio; |
376 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
377 |
> |
assertEquals(z * ratio, x.toMillis(z)); |
378 |
> |
assertEquals(Long.MAX_VALUE, x.toMillis(max + 1)); |
379 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1)); |
380 |
> |
assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE)); |
381 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE)); |
382 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1)); |
383 |
> |
if (max < Integer.MAX_VALUE) { |
384 |
> |
assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE)); |
385 |
> |
assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE)); |
386 |
> |
} |
387 |
> |
} |
388 |
> |
} |
389 |
> |
} |
390 |
> |
|
391 |
> |
/** |
392 |
> |
* toSeconds saturates positive too-large values to Long.MAX_VALUE |
393 |
> |
* and negative to LONG.MIN_VALUE |
394 |
> |
*/ |
395 |
> |
public void testToSecondsSaturate() { |
396 |
> |
for (TimeUnit x : TimeUnit.values()) { |
397 |
> |
long ratio = x.toNanos(1) / SECONDS.toNanos(1); |
398 |
> |
if (ratio > 1) { |
399 |
> |
long max = Long.MAX_VALUE/ratio; |
400 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
401 |
> |
assertEquals(z * ratio, x.toSeconds(z)); |
402 |
> |
assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1)); |
403 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1)); |
404 |
> |
assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE)); |
405 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE)); |
406 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1)); |
407 |
> |
if (max < Integer.MAX_VALUE) { |
408 |
> |
assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE)); |
409 |
> |
assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE)); |
410 |
> |
} |
411 |
> |
} |
412 |
> |
} |
413 |
> |
} |
414 |
> |
|
415 |
> |
/** |
416 |
> |
* toMinutes saturates positive too-large values to Long.MAX_VALUE |
417 |
> |
* and negative to LONG.MIN_VALUE |
418 |
> |
*/ |
419 |
> |
public void testToMinutesSaturate() { |
420 |
> |
for (TimeUnit x : TimeUnit.values()) { |
421 |
> |
long ratio = x.toNanos(1) / MINUTES.toNanos(1); |
422 |
> |
if (ratio > 1) { |
423 |
> |
long max = Long.MAX_VALUE/ratio; |
424 |
> |
for (long z : new long[] {0, 1, -1, max, -max}) |
425 |
> |
assertEquals(z * ratio, x.toMinutes(z)); |
426 |
> |
assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1)); |
427 |
> |
assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1)); |
428 |
> |
assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE)); |
429 |
> |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE)); |
430 |
> |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1)); |
431 |
> |
} |
432 |
> |
} |
433 |
|
} |
434 |
|
|
435 |
+ |
/** |
436 |
+ |
* toHours saturates positive too-large values to Long.MAX_VALUE |
437 |
+ |
* and negative to LONG.MIN_VALUE |
438 |
+ |
*/ |
439 |
+ |
public void testToHoursSaturate() { |
440 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
441 |
+ |
long ratio = x.toNanos(1) / HOURS.toNanos(1); |
442 |
+ |
if (ratio > 1) { |
443 |
+ |
long max = Long.MAX_VALUE/ratio; |
444 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
445 |
+ |
assertEquals(z * ratio, x.toHours(z)); |
446 |
+ |
assertEquals(Long.MAX_VALUE, x.toHours(max + 1)); |
447 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(-max - 1)); |
448 |
+ |
assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE)); |
449 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE)); |
450 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1)); |
451 |
+ |
} |
452 |
+ |
} |
453 |
+ |
} |
454 |
|
|
455 |
+ |
/** |
456 |
+ |
* toString returns name of unit |
457 |
+ |
*/ |
458 |
|
public void testToString() { |
459 |
< |
String s = TimeUnit.SECONDS.toString(); |
460 |
< |
assertTrue(s.indexOf("econd") >= 0); |
459 |
> |
assertEquals("SECONDS", SECONDS.toString()); |
460 |
> |
} |
461 |
> |
|
462 |
> |
/** |
463 |
> |
* name returns name of unit |
464 |
> |
*/ |
465 |
> |
public void testName() { |
466 |
> |
assertEquals("SECONDS", SECONDS.name()); |
467 |
|
} |
468 |
|
|
156 |
– |
|
469 |
|
/** |
470 |
< |
* Timed wait without holding lock throws |
471 |
< |
* IllegalMonitorStateException |
470 |
> |
* Timed wait without holding lock throws |
471 |
> |
* IllegalMonitorStateException |
472 |
|
*/ |
473 |
|
public void testTimedWait_IllegalMonitorException() { |
474 |
< |
//created a new thread with anonymous runnable |
474 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
475 |
> |
public void realRun() throws InterruptedException { |
476 |
> |
Object o = new Object(); |
477 |
> |
TimeUnit tu = MILLISECONDS; |
478 |
> |
|
479 |
> |
try { |
480 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
481 |
> |
threadShouldThrow(); |
482 |
> |
} catch (IllegalMonitorStateException success) {} |
483 |
> |
}}); |
484 |
|
|
485 |
< |
Thread t = new Thread(new Runnable() { |
486 |
< |
public void run() { |
487 |
< |
Object o = new Object(); |
488 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
489 |
< |
try { |
490 |
< |
tu.timedWait(o,LONG_DELAY_MS); |
491 |
< |
fail("should throw"); |
485 |
> |
awaitTermination(t); |
486 |
> |
} |
487 |
> |
|
488 |
> |
/** |
489 |
> |
* timedWait throws InterruptedException when interrupted |
490 |
> |
*/ |
491 |
> |
public void testTimedWait_Interruptible() { |
492 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
493 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
494 |
> |
public void realRun() throws InterruptedException { |
495 |
> |
Object o = new Object(); |
496 |
> |
TimeUnit tu = MILLISECONDS; |
497 |
> |
|
498 |
> |
Thread.currentThread().interrupt(); |
499 |
> |
try { |
500 |
> |
synchronized (o) { |
501 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
502 |
|
} |
503 |
< |
catch (InterruptedException ie) { |
504 |
< |
fail("should not throw IE here"); |
505 |
< |
} |
506 |
< |
catch(IllegalMonitorStateException success) { |
503 |
> |
shouldThrow(); |
504 |
> |
} catch (InterruptedException success) {} |
505 |
> |
assertFalse(Thread.interrupted()); |
506 |
> |
|
507 |
> |
pleaseInterrupt.countDown(); |
508 |
> |
try { |
509 |
> |
synchronized (o) { |
510 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
511 |
|
} |
512 |
< |
|
513 |
< |
} |
514 |
< |
}); |
515 |
< |
t.start(); |
516 |
< |
try { |
517 |
< |
Thread.sleep(SHORT_DELAY_MS); |
518 |
< |
t.interrupt(); |
519 |
< |
t.join(); |
520 |
< |
} catch(Exception e) { |
521 |
< |
fail("Unexpected exception"); |
522 |
< |
} |
523 |
< |
} |
524 |
< |
|
525 |
< |
/** |
526 |
< |
* timedWait will throw InterruptedException. |
527 |
< |
* Thread t waits on timedWait while the main thread interrupts it. |
528 |
< |
* Note: This does not throw IllegalMonitorException since timeWait |
529 |
< |
* is synchronized on o |
530 |
< |
*/ |
531 |
< |
public void testTimedWait() { |
532 |
< |
Thread t = new Thread(new Runnable() { |
533 |
< |
public void run() { |
534 |
< |
Object o = new Object(); |
535 |
< |
|
536 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
537 |
< |
try { |
538 |
< |
synchronized(o) { |
539 |
< |
tu.timedWait(o,MEDIUM_DELAY_MS); |
540 |
< |
} |
541 |
< |
fail("should throw"); |
542 |
< |
} |
543 |
< |
catch(InterruptedException success) {} |
544 |
< |
catch(IllegalMonitorStateException failure) { |
545 |
< |
fail("should not throw"); |
546 |
< |
} |
547 |
< |
} |
548 |
< |
}); |
549 |
< |
t.start(); |
550 |
< |
try { |
551 |
< |
Thread.sleep(SHORT_DELAY_MS); |
552 |
< |
t.interrupt(); |
553 |
< |
t.join(); |
554 |
< |
} catch(Exception e) { |
555 |
< |
fail("Unexpected exception"); |
556 |
< |
} |
557 |
< |
} |
558 |
< |
|
559 |
< |
|
560 |
< |
/** |
561 |
< |
* timedJoin will throw InterruptedException. |
562 |
< |
* Thread t waits on timedJoin while the main thread interrupts it. |
563 |
< |
*/ |
564 |
< |
public void testTimedJoin() { |
565 |
< |
Thread t = new Thread(new Runnable() { |
566 |
< |
public void run() { |
567 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
568 |
< |
try { |
569 |
< |
Thread s = new Thread(new Runnable() { |
570 |
< |
public void run() { |
571 |
< |
try{ |
572 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
573 |
< |
}catch(InterruptedException success){} |
574 |
< |
} |
575 |
< |
}); |
576 |
< |
s.start(); |
577 |
< |
tu.timedJoin(s,MEDIUM_DELAY_MS); |
578 |
< |
fail("should throw"); |
579 |
< |
} |
580 |
< |
catch(Exception e) {} |
581 |
< |
} |
582 |
< |
}); |
583 |
< |
t.start(); |
584 |
< |
try { |
585 |
< |
Thread.sleep(SHORT_DELAY_MS); |
586 |
< |
t.interrupt(); |
587 |
< |
t.join(); |
588 |
< |
} catch(Exception e) { |
589 |
< |
fail("Unexpected exception"); |
590 |
< |
} |
591 |
< |
} |
592 |
< |
|
593 |
< |
/** |
594 |
< |
* timedSleep will throw InterruptedException. |
595 |
< |
* Thread t waits on timedSleep while the main thread interrupts it. |
596 |
< |
*/ |
597 |
< |
public void testTimedSleep() { |
598 |
< |
//created a new thread with anonymous runnable |
599 |
< |
|
600 |
< |
Thread t = new Thread(new Runnable() { |
601 |
< |
public void run() { |
602 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
603 |
< |
try { |
604 |
< |
tu.sleep(MEDIUM_DELAY_MS); |
605 |
< |
fail("should throw"); |
606 |
< |
} |
607 |
< |
catch(InterruptedException success) {} |
608 |
< |
} |
609 |
< |
}); |
610 |
< |
t.start(); |
611 |
< |
try { |
612 |
< |
Thread.sleep(SHORT_DELAY_MS); |
613 |
< |
t.interrupt(); |
614 |
< |
t.join(); |
615 |
< |
} catch(Exception e) { |
616 |
< |
fail("Unexpected exception"); |
617 |
< |
} |
618 |
< |
} |
619 |
< |
|
620 |
< |
public void testSerialization() { |
621 |
< |
TimeUnit q = TimeUnit.MILLISECONDS; |
622 |
< |
|
623 |
< |
try { |
624 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
625 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
626 |
< |
out.writeObject(q); |
627 |
< |
out.close(); |
628 |
< |
|
629 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
630 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
631 |
< |
TimeUnit r = (TimeUnit)in.readObject(); |
632 |
< |
|
633 |
< |
assertEquals(q.toString(), r.toString()); |
634 |
< |
} catch(Exception e){ |
635 |
< |
e.printStackTrace(); |
636 |
< |
fail("unexpected exception"); |
512 |
> |
shouldThrow(); |
513 |
> |
} catch (InterruptedException success) {} |
514 |
> |
assertFalse(Thread.interrupted()); |
515 |
> |
}}); |
516 |
> |
|
517 |
> |
await(pleaseInterrupt); |
518 |
> |
assertThreadStaysAlive(t); |
519 |
> |
t.interrupt(); |
520 |
> |
awaitTermination(t); |
521 |
> |
} |
522 |
> |
|
523 |
> |
/** |
524 |
> |
* timedJoin throws InterruptedException when interrupted |
525 |
> |
*/ |
526 |
> |
public void testTimedJoin_Interruptible() { |
527 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
528 |
> |
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
529 |
> |
public void realRun() throws InterruptedException { |
530 |
> |
Thread.sleep(LONG_DELAY_MS); |
531 |
> |
}}); |
532 |
> |
final Thread t = newStartedThread(new CheckedRunnable() { |
533 |
> |
public void realRun() throws InterruptedException { |
534 |
> |
TimeUnit tu = MILLISECONDS; |
535 |
> |
Thread.currentThread().interrupt(); |
536 |
> |
try { |
537 |
> |
tu.timedJoin(s, LONG_DELAY_MS); |
538 |
> |
shouldThrow(); |
539 |
> |
} catch (InterruptedException success) {} |
540 |
> |
assertFalse(Thread.interrupted()); |
541 |
> |
|
542 |
> |
pleaseInterrupt.countDown(); |
543 |
> |
try { |
544 |
> |
tu.timedJoin(s, LONG_DELAY_MS); |
545 |
> |
shouldThrow(); |
546 |
> |
} catch (InterruptedException success) {} |
547 |
> |
assertFalse(Thread.interrupted()); |
548 |
> |
}}); |
549 |
> |
|
550 |
> |
await(pleaseInterrupt); |
551 |
> |
assertThreadStaysAlive(t); |
552 |
> |
t.interrupt(); |
553 |
> |
awaitTermination(t); |
554 |
> |
s.interrupt(); |
555 |
> |
awaitTermination(s); |
556 |
> |
} |
557 |
> |
|
558 |
> |
/** |
559 |
> |
* timedSleep throws InterruptedException when interrupted |
560 |
> |
*/ |
561 |
> |
public void testTimedSleep_Interruptible() { |
562 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
563 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
564 |
> |
public void realRun() throws InterruptedException { |
565 |
> |
TimeUnit tu = MILLISECONDS; |
566 |
> |
Thread.currentThread().interrupt(); |
567 |
> |
try { |
568 |
> |
tu.sleep(LONG_DELAY_MS); |
569 |
> |
shouldThrow(); |
570 |
> |
} catch (InterruptedException success) {} |
571 |
> |
assertFalse(Thread.interrupted()); |
572 |
> |
|
573 |
> |
pleaseInterrupt.countDown(); |
574 |
> |
try { |
575 |
> |
tu.sleep(LONG_DELAY_MS); |
576 |
> |
shouldThrow(); |
577 |
> |
} catch (InterruptedException success) {} |
578 |
> |
assertFalse(Thread.interrupted()); |
579 |
> |
}}); |
580 |
> |
|
581 |
> |
await(pleaseInterrupt); |
582 |
> |
assertThreadStaysAlive(t); |
583 |
> |
t.interrupt(); |
584 |
> |
awaitTermination(t); |
585 |
> |
} |
586 |
> |
|
587 |
> |
/** |
588 |
> |
* a deserialized serialized unit is the same instance |
589 |
> |
*/ |
590 |
> |
public void testSerialization() throws Exception { |
591 |
> |
for (TimeUnit x : TimeUnit.values()) |
592 |
> |
assertSame(x, serialClone(x)); |
593 |
> |
} |
594 |
> |
|
595 |
> |
/** |
596 |
> |
* tests for toChronoUnit. |
597 |
> |
*/ |
598 |
> |
public void testToChronoUnit() throws Exception { |
599 |
> |
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); |
600 |
> |
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); |
601 |
> |
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); |
602 |
> |
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); |
603 |
> |
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); |
604 |
> |
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); |
605 |
> |
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); |
606 |
> |
|
607 |
> |
// Every TimeUnit has a defined ChronoUnit equivalent |
608 |
> |
for (TimeUnit x : TimeUnit.values()) |
609 |
> |
assertSame(x, TimeUnit.of(x.toChronoUnit())); |
610 |
> |
} |
611 |
> |
|
612 |
> |
/** |
613 |
> |
* tests for TimeUnit.of(ChronoUnit). |
614 |
> |
*/ |
615 |
> |
public void testTimeUnitOf() throws Exception { |
616 |
> |
assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); |
617 |
> |
assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); |
618 |
> |
assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); |
619 |
> |
assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); |
620 |
> |
assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); |
621 |
> |
assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); |
622 |
> |
assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); |
623 |
> |
|
624 |
> |
assertThrows(NullPointerException.class, |
625 |
> |
() -> TimeUnit.of((ChronoUnit)null)); |
626 |
> |
|
627 |
> |
// ChronoUnits either round trip to their TimeUnit |
628 |
> |
// equivalents, or throw IllegalArgumentException. |
629 |
> |
for (ChronoUnit cu : ChronoUnit.values()) { |
630 |
> |
final TimeUnit tu; |
631 |
> |
try { |
632 |
> |
tu = TimeUnit.of(cu); |
633 |
> |
} catch (IllegalArgumentException acceptable) { |
634 |
> |
continue; |
635 |
> |
} |
636 |
> |
assertSame(cu, tu.toChronoUnit()); |
637 |
|
} |
638 |
|
} |
639 |
|
|