14 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
|
17 |
– |
import java.time.temporal.ChronoUnit; |
17 |
|
import java.util.concurrent.CountDownLatch; |
18 |
|
import java.util.concurrent.TimeUnit; |
19 |
|
|
96 |
|
assertEquals(t, |
97 |
|
NANOSECONDS.convert(t, NANOSECONDS)); |
98 |
|
} |
99 |
+ |
|
100 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
101 |
+ |
long[] zs = { |
102 |
+ |
0, 1, -1, |
103 |
+ |
Integer.MAX_VALUE, Integer.MIN_VALUE, |
104 |
+ |
Long.MAX_VALUE, Long.MIN_VALUE, |
105 |
+ |
}; |
106 |
+ |
for (long z : zs) assertEquals(z, x.convert(z, x)); |
107 |
+ |
} |
108 |
|
} |
109 |
|
|
110 |
|
/** |
289 |
|
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); |
290 |
|
assertEquals(Long.MIN_VALUE, |
291 |
|
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); |
292 |
+ |
|
293 |
+ |
for (TimeUnit x : TimeUnit.values()) |
294 |
+ |
for (TimeUnit y : TimeUnit.values()) { |
295 |
+ |
long ratio = x.toNanos(1) / y.toNanos(1); |
296 |
+ |
if (ratio >= 1) { |
297 |
+ |
assertEquals(ratio, y.convert(1, x)); |
298 |
+ |
assertEquals(1, x.convert(ratio, y)); |
299 |
+ |
long max = Long.MAX_VALUE/ratio; |
300 |
+ |
assertEquals(max * ratio, y.convert(max, x)); |
301 |
+ |
assertEquals(-max * ratio, y.convert(-max, x)); |
302 |
+ |
assertEquals(max, x.convert(max * ratio, y)); |
303 |
+ |
assertEquals(-max, x.convert(-max * ratio, y)); |
304 |
+ |
if (max < Long.MAX_VALUE) { |
305 |
+ |
assertEquals(Long.MAX_VALUE, y.convert(max + 1, x)); |
306 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x)); |
307 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x)); |
308 |
+ |
} |
309 |
+ |
assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x)); |
310 |
+ |
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x)); |
311 |
+ |
} |
312 |
+ |
} |
313 |
|
} |
314 |
|
|
315 |
|
/** |
321 |
|
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
322 |
|
assertEquals(Long.MIN_VALUE, |
323 |
|
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
324 |
+ |
|
325 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
326 |
+ |
long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1); |
327 |
+ |
if (ratio >= 1) { |
328 |
+ |
long max = Long.MAX_VALUE/ratio; |
329 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
330 |
+ |
assertEquals(z * ratio, x.toNanos(z)); |
331 |
+ |
if (max < Long.MAX_VALUE) { |
332 |
+ |
assertEquals(Long.MAX_VALUE, x.toNanos(max + 1)); |
333 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1)); |
334 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1)); |
335 |
+ |
} |
336 |
+ |
assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE)); |
337 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE)); |
338 |
+ |
if (max < Integer.MAX_VALUE) { |
339 |
+ |
assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE)); |
340 |
+ |
assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE)); |
341 |
+ |
} |
342 |
+ |
} |
343 |
+ |
} |
344 |
+ |
} |
345 |
+ |
|
346 |
+ |
/** |
347 |
+ |
* toMicros saturates positive too-large values to Long.MAX_VALUE |
348 |
+ |
* and negative to LONG.MIN_VALUE |
349 |
+ |
*/ |
350 |
+ |
public void testToMicrosSaturate() { |
351 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
352 |
+ |
long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1); |
353 |
+ |
if (ratio >= 1) { |
354 |
+ |
long max = Long.MAX_VALUE/ratio; |
355 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
356 |
+ |
assertEquals(z * ratio, x.toMicros(z)); |
357 |
+ |
if (max < Long.MAX_VALUE) { |
358 |
+ |
assertEquals(Long.MAX_VALUE, x.toMicros(max + 1)); |
359 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1)); |
360 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1)); |
361 |
+ |
} |
362 |
+ |
assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE)); |
363 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE)); |
364 |
+ |
if (max < Integer.MAX_VALUE) { |
365 |
+ |
assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE)); |
366 |
+ |
assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE)); |
367 |
+ |
} |
368 |
+ |
} |
369 |
+ |
} |
370 |
+ |
} |
371 |
+ |
|
372 |
+ |
/** |
373 |
+ |
* toMillis saturates positive too-large values to Long.MAX_VALUE |
374 |
+ |
* and negative to LONG.MIN_VALUE |
375 |
+ |
*/ |
376 |
+ |
public void testToMillisSaturate() { |
377 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
378 |
+ |
long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1); |
379 |
+ |
if (ratio >= 1) { |
380 |
+ |
long max = Long.MAX_VALUE/ratio; |
381 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
382 |
+ |
assertEquals(z * ratio, x.toMillis(z)); |
383 |
+ |
if (max < Long.MAX_VALUE) { |
384 |
+ |
assertEquals(Long.MAX_VALUE, x.toMillis(max + 1)); |
385 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1)); |
386 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1)); |
387 |
+ |
} |
388 |
+ |
assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE)); |
389 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE)); |
390 |
+ |
if (max < Integer.MAX_VALUE) { |
391 |
+ |
assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE)); |
392 |
+ |
assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE)); |
393 |
+ |
} |
394 |
+ |
} |
395 |
+ |
} |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
/** |
399 |
+ |
* toSeconds saturates positive too-large values to Long.MAX_VALUE |
400 |
+ |
* and negative to LONG.MIN_VALUE |
401 |
+ |
*/ |
402 |
+ |
public void testToSecondsSaturate() { |
403 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
404 |
+ |
long ratio = x.toNanos(1) / SECONDS.toNanos(1); |
405 |
+ |
if (ratio >= 1) { |
406 |
+ |
long max = Long.MAX_VALUE/ratio; |
407 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
408 |
+ |
assertEquals(z * ratio, x.toSeconds(z)); |
409 |
+ |
if (max < Long.MAX_VALUE) { |
410 |
+ |
assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1)); |
411 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1)); |
412 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1)); |
413 |
+ |
} |
414 |
+ |
assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE)); |
415 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE)); |
416 |
+ |
if (max < Integer.MAX_VALUE) { |
417 |
+ |
assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE)); |
418 |
+ |
assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE)); |
419 |
+ |
} |
420 |
+ |
} |
421 |
+ |
} |
422 |
+ |
} |
423 |
+ |
|
424 |
+ |
/** |
425 |
+ |
* toMinutes saturates positive too-large values to Long.MAX_VALUE |
426 |
+ |
* and negative to LONG.MIN_VALUE |
427 |
+ |
*/ |
428 |
+ |
public void testToMinutesSaturate() { |
429 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
430 |
+ |
long ratio = x.toNanos(1) / MINUTES.toNanos(1); |
431 |
+ |
if (ratio > 1) { |
432 |
+ |
long max = Long.MAX_VALUE/ratio; |
433 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
434 |
+ |
assertEquals(z * ratio, x.toMinutes(z)); |
435 |
+ |
assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1)); |
436 |
+ |
assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1)); |
437 |
+ |
assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE)); |
438 |
+ |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE)); |
439 |
+ |
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1)); |
440 |
+ |
} |
441 |
+ |
} |
442 |
+ |
} |
443 |
+ |
|
444 |
+ |
/** |
445 |
+ |
* toHours saturates positive too-large values to Long.MAX_VALUE |
446 |
+ |
* and negative to LONG.MIN_VALUE |
447 |
+ |
*/ |
448 |
+ |
public void testToHoursSaturate() { |
449 |
+ |
for (TimeUnit x : TimeUnit.values()) { |
450 |
+ |
long ratio = x.toNanos(1) / HOURS.toNanos(1); |
451 |
+ |
if (ratio >= 1) { |
452 |
+ |
long max = Long.MAX_VALUE/ratio; |
453 |
+ |
for (long z : new long[] {0, 1, -1, max, -max}) |
454 |
+ |
assertEquals(z * ratio, x.toHours(z)); |
455 |
+ |
if (max < Long.MAX_VALUE) { |
456 |
+ |
assertEquals(Long.MAX_VALUE, x.toHours(max + 1)); |
457 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(-max - 1)); |
458 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1)); |
459 |
+ |
} |
460 |
+ |
assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE)); |
461 |
+ |
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE)); |
462 |
+ |
} |
463 |
+ |
} |
464 |
|
} |
465 |
|
|
466 |
|
/** |
526 |
|
}}); |
527 |
|
|
528 |
|
await(pleaseInterrupt); |
529 |
< |
assertThreadStaysAlive(t); |
529 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
530 |
|
t.interrupt(); |
531 |
|
awaitTermination(t); |
532 |
|
} |
559 |
|
}}); |
560 |
|
|
561 |
|
await(pleaseInterrupt); |
562 |
< |
assertThreadStaysAlive(t); |
562 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
563 |
|
t.interrupt(); |
564 |
|
awaitTermination(t); |
565 |
|
s.interrupt(); |
590 |
|
}}); |
591 |
|
|
592 |
|
await(pleaseInterrupt); |
593 |
< |
assertThreadStaysAlive(t); |
593 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
594 |
|
t.interrupt(); |
595 |
|
awaitTermination(t); |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* a deserialized serialized unit is the same instance |
599 |
> |
* a deserialized/reserialized unit is the same instance |
600 |
|
*/ |
601 |
|
public void testSerialization() throws Exception { |
433 |
– |
TimeUnit x = MILLISECONDS; |
434 |
– |
assertSame(x, serialClone(x)); |
435 |
– |
} |
436 |
– |
|
437 |
– |
/** |
438 |
– |
* tests for toChronoUnit. |
439 |
– |
*/ |
440 |
– |
public void testToChronoUnit() throws Exception { |
441 |
– |
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); |
442 |
– |
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); |
443 |
– |
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); |
444 |
– |
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); |
445 |
– |
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); |
446 |
– |
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); |
447 |
– |
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); |
448 |
– |
|
449 |
– |
// Every TimeUnit has a defined ChronoUnit equivalent |
602 |
|
for (TimeUnit x : TimeUnit.values()) |
603 |
< |
assertSame(x, TimeUnit.of(x.toChronoUnit())); |
452 |
< |
} |
453 |
< |
|
454 |
< |
/** |
455 |
< |
* tests for TimeUnit.of(ChronoUnit). |
456 |
< |
*/ |
457 |
< |
public void testTimeUnitOf() throws Exception { |
458 |
< |
assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); |
459 |
< |
assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); |
460 |
< |
assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); |
461 |
< |
assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); |
462 |
< |
assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); |
463 |
< |
assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); |
464 |
< |
assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); |
465 |
< |
|
466 |
< |
assertThrows(NullPointerException.class, |
467 |
< |
() -> TimeUnit.of((ChronoUnit)null)); |
468 |
< |
|
469 |
< |
// ChronoUnits either round trip to their TimeUnit |
470 |
< |
// equivalents, or throw IllegalArgumentException. |
471 |
< |
for (ChronoUnit cu : ChronoUnit.values()) { |
472 |
< |
final TimeUnit tu; |
473 |
< |
try { |
474 |
< |
tu = TimeUnit.of(cu); |
475 |
< |
} catch (IllegalArgumentException acceptable) { |
476 |
< |
continue; |
477 |
< |
} |
478 |
< |
assertSame(cu, tu.toChronoUnit()); |
479 |
< |
} |
603 |
> |
assertSame(x, serialClone(x)); |
604 |
|
} |
605 |
|
|
606 |
|
} |