1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
2479 assertEquals(Duration.ofSeconds(0).abs(), Duration.ofSeconds(0));
2480 assertEquals(Duration.ofSeconds(12).abs(), Duration.ofSeconds(12));
2481 assertEquals(Duration.ofSeconds(-12).abs(), Duration.ofSeconds(12));
2482 assertEquals(Duration.ofSeconds(12, 20).abs(), Duration.ofSeconds(12, 20));
2483 assertEquals(Duration.ofSeconds(12, -20).abs(), Duration.ofSeconds(12, -20));
2484 assertEquals(Duration.ofSeconds(-12, -20).abs(), Duration.ofSeconds(12, 20));
2485 assertEquals(Duration.ofSeconds(-12, 20).abs(), Duration.ofSeconds(12, -20));
2486 assertEquals(Duration.ofSeconds(Long.MAX_VALUE).abs(), Duration.ofSeconds(Long.MAX_VALUE));
2487 }
2488
2489 @Test(expectedExceptions=ArithmeticException.class)
2490 public void test_abs_overflow() {
2491 Duration.ofSeconds(Long.MIN_VALUE).abs();
2492 }
2493
2494 //-----------------------------------------------------------------------
2495 // toNanos()
2496 //-----------------------------------------------------------------------
2497 @Test
2498 public void test_toNanos() {
2499 Duration test = Duration.ofSeconds(321, 123456789);
2500 assertEquals(test.toNanos(), 321123456789L);
2501 }
2502
2503 @Test
2504 public void test_toNanos_max() {
2505 Duration test = Duration.ofSeconds(0, Long.MAX_VALUE);
2506 assertEquals(test.toNanos(), Long.MAX_VALUE);
2507 }
2508
2509 @Test(expectedExceptions=ArithmeticException.class)
2510 public void test_toNanos_tooBig() {
2511 Duration test = Duration.ofSeconds(0, Long.MAX_VALUE).plusNanos(1);
2512 test.toNanos();
2513 }
2514
2515 //-----------------------------------------------------------------------
2516 // toMillis()
2517 //-----------------------------------------------------------------------
2518 @Test
2519 public void test_toMillis() {
2520 Duration test = Duration.ofSeconds(321, 123456789);
2521 assertEquals(test.toMillis(), 321000 + 123);
2522 }
2523
2524 @Test
2525 public void test_toMillis_max() {
2526 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, (Long.MAX_VALUE % 1000) * 1000000);
2527 assertEquals(test.toMillis(), Long.MAX_VALUE);
2528 }
2529
2530 @Test(expectedExceptions=ArithmeticException.class)
2531 public void test_toMillis_tooBig() {
2532 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, ((Long.MAX_VALUE % 1000) + 1) * 1000000);
2533 test.toMillis();
2534 }
2535
2536 //-----------------------------------------------------------------------
2537 // toSeconds()
2538 //-----------------------------------------------------------------------
2539 @DataProvider(name="toSeconds_provider")
2540 Object[][] provider_toSeconds() {
2541 return new Object[][] {
2542 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 31556926L},
2543 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -31556927L},
2544 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, 123_456_789), -31556926L},
2545 {Duration.ofSeconds(0), 0L},
2546 {Duration.ofSeconds(0, 123_456_789), 0L},
2547 {Duration.ofSeconds(0, -123_456_789), -1L},
2548 {Duration.ofSeconds(Long.MAX_VALUE), 9223372036854775807L},
2549 {Duration.ofSeconds(Long.MIN_VALUE), -9223372036854775808L},
2550 };
2551 }
2552
2553 @Test(dataProvider="toSeconds_provider")
2554 public void test_toSeconds(Duration dur, long seconds) {
2555 assertEquals(dur.toSeconds(), seconds);
|
1 /*
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
2479 assertEquals(Duration.ofSeconds(0).abs(), Duration.ofSeconds(0));
2480 assertEquals(Duration.ofSeconds(12).abs(), Duration.ofSeconds(12));
2481 assertEquals(Duration.ofSeconds(-12).abs(), Duration.ofSeconds(12));
2482 assertEquals(Duration.ofSeconds(12, 20).abs(), Duration.ofSeconds(12, 20));
2483 assertEquals(Duration.ofSeconds(12, -20).abs(), Duration.ofSeconds(12, -20));
2484 assertEquals(Duration.ofSeconds(-12, -20).abs(), Duration.ofSeconds(12, 20));
2485 assertEquals(Duration.ofSeconds(-12, 20).abs(), Duration.ofSeconds(12, -20));
2486 assertEquals(Duration.ofSeconds(Long.MAX_VALUE).abs(), Duration.ofSeconds(Long.MAX_VALUE));
2487 }
2488
2489 @Test(expectedExceptions=ArithmeticException.class)
2490 public void test_abs_overflow() {
2491 Duration.ofSeconds(Long.MIN_VALUE).abs();
2492 }
2493
2494 //-----------------------------------------------------------------------
2495 // toNanos()
2496 //-----------------------------------------------------------------------
2497 @Test
2498 public void test_toNanos() {
2499 assertEquals(Duration.ofSeconds(321, 123456789).toNanos(), 321123456789L);
2500 assertEquals(Duration.ofNanos(Long.MAX_VALUE).toNanos(), 9223372036854775807L);
2501 assertEquals(Duration.ofNanos(Long.MIN_VALUE).toNanos(), -9223372036854775808L);
2502 }
2503
2504 @Test
2505 public void test_toNanos_max() {
2506 Duration test = Duration.ofSeconds(0, Long.MAX_VALUE);
2507 assertEquals(test.toNanos(), Long.MAX_VALUE);
2508 }
2509
2510 @Test(expectedExceptions=ArithmeticException.class)
2511 public void test_toNanos_tooBig() {
2512 Duration test = Duration.ofSeconds(0, Long.MAX_VALUE).plusNanos(1);
2513 test.toNanos();
2514 }
2515
2516 @Test
2517 public void test_toNanos_min() {
2518 Duration test = Duration.ofSeconds(0, Long.MIN_VALUE);
2519 assertEquals(test.toNanos(), Long.MIN_VALUE);
2520 }
2521
2522 @Test(expectedExceptions=ArithmeticException.class)
2523 public void test_toNanos_tooSmall() {
2524 Duration test = Duration.ofSeconds(0, Long.MIN_VALUE).minusNanos(1);
2525 test.toNanos();
2526 }
2527
2528 //-----------------------------------------------------------------------
2529 // toMillis()
2530 //-----------------------------------------------------------------------
2531 @Test
2532 public void test_toMillis() {
2533 assertEquals(Duration.ofSeconds(321, 123456789).toMillis(), 321000 + 123);
2534 assertEquals(Duration.ofMillis(Long.MAX_VALUE).toMillis(), 9223372036854775807L);
2535 assertEquals(Duration.ofMillis(Long.MIN_VALUE).toMillis(), -9223372036854775808L);
2536 }
2537
2538 @Test
2539 public void test_toMillis_max() {
2540 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, (Long.MAX_VALUE % 1000) * 1000000);
2541 assertEquals(test.toMillis(), Long.MAX_VALUE);
2542 }
2543
2544 @Test(expectedExceptions=ArithmeticException.class)
2545 public void test_toMillis_tooBig() {
2546 Duration test = Duration.ofSeconds(Long.MAX_VALUE / 1000, ((Long.MAX_VALUE % 1000) + 1) * 1000000);
2547 test.toMillis();
2548 }
2549
2550 @Test
2551 public void test_toMillis_min() {
2552 Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, (Long.MIN_VALUE % 1000) * 1000000);
2553 assertEquals(test.toMillis(), Long.MIN_VALUE);
2554 }
2555
2556 @Test(expectedExceptions=ArithmeticException.class)
2557 public void test_toMillis_tooSmall() {
2558 Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, ((Long.MIN_VALUE % 1000) - 1) * 1000000);
2559 test.toMillis();
2560 }
2561
2562 //-----------------------------------------------------------------------
2563 // toSeconds()
2564 //-----------------------------------------------------------------------
2565 @DataProvider(name="toSeconds_provider")
2566 Object[][] provider_toSeconds() {
2567 return new Object[][] {
2568 {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 31556926L},
2569 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -31556927L},
2570 {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, 123_456_789), -31556926L},
2571 {Duration.ofSeconds(0), 0L},
2572 {Duration.ofSeconds(0, 123_456_789), 0L},
2573 {Duration.ofSeconds(0, -123_456_789), -1L},
2574 {Duration.ofSeconds(Long.MAX_VALUE), 9223372036854775807L},
2575 {Duration.ofSeconds(Long.MIN_VALUE), -9223372036854775808L},
2576 };
2577 }
2578
2579 @Test(dataProvider="toSeconds_provider")
2580 public void test_toSeconds(Duration dur, long seconds) {
2581 assertEquals(dur.toSeconds(), seconds);
|