2458 //----------------------------------------------------------------------- 2459 @Test 2460 public void test_toMillis() { 2461 Duration test = Duration.ofSeconds(321, 123456789); 2462 assertEquals(test.toMillis(), 321000 + 123); 2463 } 2464 2465 @Test 2466 public void test_toMillis_max() { 2467 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, (Long.MAX_VALUE % 1000) * 1000000); 2468 assertEquals(test.toMillis(), Long.MAX_VALUE); 2469 } 2470 2471 @Test(expectedExceptions=ArithmeticException.class) 2472 public void test_toMillis_tooBig() { 2473 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, ((Long.MAX_VALUE % 1000) + 1) * 1000000); 2474 test.toMillis(); 2475 } 2476 2477 //----------------------------------------------------------------------- 2478 // compareTo() 2479 //----------------------------------------------------------------------- 2480 @Test 2481 public void test_comparisons() { 2482 doTest_comparisons_Duration( 2483 Duration.ofSeconds(-2L, 0), 2484 Duration.ofSeconds(-2L, 999999998), 2485 Duration.ofSeconds(-2L, 999999999), 2486 Duration.ofSeconds(-1L, 0), 2487 Duration.ofSeconds(-1L, 1), 2488 Duration.ofSeconds(-1L, 999999998), 2489 Duration.ofSeconds(-1L, 999999999), 2490 Duration.ofSeconds(0L, 0), 2491 Duration.ofSeconds(0L, 1), 2492 Duration.ofSeconds(0L, 2), 2493 Duration.ofSeconds(0L, 999999999), 2494 Duration.ofSeconds(1L, 0), 2495 Duration.ofSeconds(2L, 0) 2496 ); 2497 } | 2458 //----------------------------------------------------------------------- 2459 @Test 2460 public void test_toMillis() { 2461 Duration test = Duration.ofSeconds(321, 123456789); 2462 assertEquals(test.toMillis(), 321000 + 123); 2463 } 2464 2465 @Test 2466 public void test_toMillis_max() { 2467 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, (Long.MAX_VALUE % 1000) * 1000000); 2468 assertEquals(test.toMillis(), Long.MAX_VALUE); 2469 } 2470 2471 @Test(expectedExceptions=ArithmeticException.class) 2472 public void test_toMillis_tooBig() { 2473 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, ((Long.MAX_VALUE % 1000) + 1) * 1000000); 2474 test.toMillis(); 2475 } 2476 2477 //----------------------------------------------------------------------- 2478 // toSeconds() 2479 //----------------------------------------------------------------------- 2480 @DataProvider(name="toSeconds_provider") 2481 Object[][] provider_toSeconds() { 2482 return new Object[][] { 2483 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 31556926L}, 2484 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -31556927L}, 2485 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, 123_456_789), -31556926L}, 2486 {Duration.ofSeconds(0), 0L}, 2487 {Duration.ofSeconds(0, 123_456_789), 0L}, 2488 {Duration.ofSeconds(0, -123_456_789), -1L}, 2489 {Duration.ofSeconds(Long.MAX_VALUE), 9223372036854775807L}, 2490 {Duration.ofSeconds(Long.MIN_VALUE), -9223372036854775808L}, 2491 }; 2492 } 2493 2494 @Test(dataProvider="toSeconds_provider") 2495 public void test_toSeconds(Duration dur, long seconds) { 2496 assertEquals(dur.toSeconds(), seconds); 2497 } 2498 2499 //----------------------------------------------------------------------- 2500 // toDaysPart() 2501 //----------------------------------------------------------------------- 2502 @DataProvider(name="toDaysPart_provider") 2503 Object[][] provider_toDaysPart() { 2504 return new Object[][] { 2505 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 365L}, 2506 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -365L}, 2507 {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 123_456_789), 0L}, 2508 {Duration.ofDays(365), 365L}, 2509 {Duration.ofHours(2), 0L}, 2510 {Duration.ofHours(-2), 0L}, 2511 }; 2512 } 2513 2514 @Test(dataProvider="toDaysPart_provider") 2515 public void test_toDaysPart(Duration dur, long days) { 2516 assertEquals(dur.toDaysPart(), days); 2517 } 2518 2519 //----------------------------------------------------------------------- 2520 // toHoursPart() 2521 //----------------------------------------------------------------------- 2522 @DataProvider(name="toHoursPart_provider") 2523 Object[][] provider_toHoursPart() { 2524 return new Object[][] { 2525 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 5}, 2526 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -5}, 2527 {Duration.ofSeconds(48 * 60 + 46, 123_456_789), 0}, 2528 {Duration.ofHours(2), 2}, 2529 {Duration.ofHours(-2), -2}, 2530 }; 2531 } 2532 2533 @Test(dataProvider="toHoursPart_provider") 2534 public void test_toHoursPart(Duration dur, int hours) { 2535 assertEquals(dur.toHoursPart(), hours); 2536 } 2537 2538 //----------------------------------------------------------------------- 2539 // toMinutesPart() 2540 //----------------------------------------------------------------------- 2541 @DataProvider(name="toMinutesPart_provider") 2542 Object[][] provider_toMinutesPart() { 2543 return new Object[][] { 2544 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 48}, 2545 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -48}, 2546 {Duration.ofSeconds(46, 123_456_789),0}, 2547 {Duration.ofMinutes(48), 48}, 2548 {Duration.ofHours(2), 0}, 2549 {Duration.ofHours(-2),0}, 2550 }; 2551 } 2552 2553 @Test(dataProvider="toMinutesPart_provider") 2554 public void test_toMinutesPart(Duration dur, int minutes) { 2555 assertEquals(dur.toMinutesPart(), minutes); 2556 } 2557 2558 //----------------------------------------------------------------------- 2559 // toSecondsPart() 2560 //----------------------------------------------------------------------- 2561 @DataProvider(name="toSecondsPart_provider") 2562 Object[][] provider_toSecondsPart() { 2563 return new Object[][] { 2564 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 46}, 2565 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -47}, 2566 {Duration.ofSeconds(0, 123_456_789), 0}, 2567 {Duration.ofSeconds(46), 46}, 2568 {Duration.ofHours(2), 0}, 2569 {Duration.ofHours(-2), 0}, 2570 }; 2571 } 2572 2573 @Test(dataProvider="toSecondsPart_provider") 2574 public void test_toSecondsPart(Duration dur, int seconds) { 2575 assertEquals(dur.toSecondsPart(), seconds); 2576 } 2577 2578 //----------------------------------------------------------------------- 2579 // toMillisPart() 2580 //----------------------------------------------------------------------- 2581 @DataProvider(name="toMillisPart_provider") 2582 Object[][] provider_toMillisPart() { 2583 return new Object[][] { 2584 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123}, 2585 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876}, 2586 {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0}, 2587 {Duration.ofMillis(123), 123}, 2588 {Duration.ofHours(2), 0}, 2589 {Duration.ofHours(-2), 0}, 2590 }; 2591 } 2592 2593 @Test(dataProvider="toMillisPart_provider") 2594 public void test_toMillisPart(Duration dur, int millis) { 2595 assertEquals(dur.toMillisPart(), millis); 2596 } 2597 2598 //----------------------------------------------------------------------- 2599 // toNanosPart() 2600 //----------------------------------------------------------------------- 2601 @DataProvider(name="toNanosPart_provider") 2602 Object[][] provider_toNanosPart() { 2603 return new Object[][] { 2604 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123_456_789}, 2605 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876_543_211}, 2606 {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0}, 2607 {Duration.ofNanos(123_456_789), 123_456_789}, 2608 {Duration.ofHours(2), 0}, 2609 {Duration.ofHours(-2), 0}, 2610 }; 2611 } 2612 2613 @Test(dataProvider="toNanosPart_provider") 2614 public void test_toNanosPart(Duration dur, int nanos) { 2615 assertEquals(dur.toNanosPart(), nanos); 2616 } 2617 2618 //----------------------------------------------------------------------- 2619 // compareTo() 2620 //----------------------------------------------------------------------- 2621 @Test 2622 public void test_comparisons() { 2623 doTest_comparisons_Duration( 2624 Duration.ofSeconds(-2L, 0), 2625 Duration.ofSeconds(-2L, 999999998), 2626 Duration.ofSeconds(-2L, 999999999), 2627 Duration.ofSeconds(-1L, 0), 2628 Duration.ofSeconds(-1L, 1), 2629 Duration.ofSeconds(-1L, 999999998), 2630 Duration.ofSeconds(-1L, 999999999), 2631 Duration.ofSeconds(0L, 0), 2632 Duration.ofSeconds(0L, 1), 2633 Duration.ofSeconds(0L, 2), 2634 Duration.ofSeconds(0L, 999999999), 2635 Duration.ofSeconds(1L, 0), 2636 Duration.ofSeconds(2L, 0) 2637 ); 2638 } |