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 */ 23 24 /* 25 * This file is available under and governed by the GNU General Public 26 * License version 2 only, as published by the Free Software Foundation. 27 * However, the following notice accompanied the original version of this 28 * file: 29 * 30 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos 31 * 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions are met: 36 * 37 * * Redistributions of source code must retain the above copyright notice, 38 * this list of conditions and the following disclaimer. 39 * 40 * * Redistributions in binary form must reproduce the above copyright notice, 41 * this list of conditions and the following disclaimer in the documentation 42 * and/or other materials provided with the distribution. 43 * 44 * * Neither the name of JSR-310 nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package tck.java.time; 61 62 import static java.time.temporal.ChronoUnit.DAYS; 63 import static java.time.temporal.ChronoUnit.HALF_DAYS; 64 import static java.time.temporal.ChronoUnit.HOURS; 65 import static java.time.temporal.ChronoUnit.MICROS; 66 import static java.time.temporal.ChronoUnit.MILLIS; 67 import static java.time.temporal.ChronoUnit.MINUTES; 68 import static java.time.temporal.ChronoUnit.NANOS; 69 import static java.time.temporal.ChronoUnit.SECONDS; 70 import static java.time.temporal.ChronoUnit.WEEKS; 71 import static org.testng.Assert.assertEquals; 72 import static org.testng.Assert.assertTrue; 73 import static org.testng.Assert.fail; 74 75 import java.io.ByteArrayOutputStream; 76 import java.io.DataOutputStream; 77 import java.time.DateTimeException; 78 import java.time.Duration; 79 import java.time.Instant; 80 import java.time.LocalDate; 81 import java.time.LocalDateTime; 82 import java.time.LocalTime; 83 import java.time.Period; 84 import java.time.ZoneOffset; 85 import java.time.ZonedDateTime; 86 import java.time.format.DateTimeParseException; 87 import java.time.temporal.ChronoUnit; 88 import java.time.temporal.Temporal; 89 import java.time.temporal.TemporalAmount; 90 import java.time.temporal.TemporalUnit; 91 import java.util.ArrayList; 92 import java.util.Collections; 93 import java.util.List; 94 import java.util.Locale; 95 96 import org.testng.annotations.DataProvider; 97 import org.testng.annotations.Test; 98 99 /** 100 * Test Duration. 101 */ 102 @Test 103 public class TCKDuration extends AbstractTCKTest { 104 105 private static final long CYCLE_SECS = 146097L * 86400L; 106 107 //----------------------------------------------------------------------- 108 // constants 109 //----------------------------------------------------------------------- 110 @Test 111 public void test_zero() { 112 assertEquals(Duration.ZERO.getSeconds(), 0L); 113 assertEquals(Duration.ZERO.getNano(), 0); 114 } 115 116 //----------------------------------------------------------------------- 117 // ofSeconds(long) 118 //----------------------------------------------------------------------- 119 @Test 120 public void factory_seconds_long() { 121 for (long i = -2; i <= 2; i++) { 122 Duration t = Duration.ofSeconds(i); 123 assertEquals(t.getSeconds(), i); 124 assertEquals(t.getNano(), 0); 125 } 126 } 127 128 //----------------------------------------------------------------------- 129 // ofSeconds(long,long) 130 //----------------------------------------------------------------------- 131 @Test 132 public void factory_seconds_long_long() { 133 for (long i = -2; i <= 2; i++) { 134 for (int j = 0; j < 10; j++) { 135 Duration t = Duration.ofSeconds(i, j); 136 assertEquals(t.getSeconds(), i); 137 assertEquals(t.getNano(), j); 138 } 139 for (int j = -10; j < 0; j++) { 140 Duration t = Duration.ofSeconds(i, j); 141 assertEquals(t.getSeconds(), i - 1); 142 assertEquals(t.getNano(), j + 1000000000); 143 } 144 for (int j = 999999990; j < 1000000000; j++) { 145 Duration t = Duration.ofSeconds(i, j); 146 assertEquals(t.getSeconds(), i); 147 assertEquals(t.getNano(), j); 148 } 149 } 150 } 151 152 @Test 153 public void factory_seconds_long_long_nanosNegativeAdjusted() { 154 Duration test = Duration.ofSeconds(2L, -1); 155 assertEquals(test.getSeconds(), 1); 156 assertEquals(test.getNano(), 999999999); 157 } 158 159 @Test(expectedExceptions=ArithmeticException.class) 160 public void factory_seconds_long_long_tooBig() { 161 Duration.ofSeconds(Long.MAX_VALUE, 1000000000); 162 } 163 164 //----------------------------------------------------------------------- 165 // ofMillis(long) 166 //----------------------------------------------------------------------- 167 @DataProvider(name="MillisDurationNoNanos") 168 Object[][] provider_factory_millis_long() { 169 return new Object[][] { 170 {0, 0, 0}, 171 {1, 0, 1000000}, 172 {2, 0, 2000000}, 173 {999, 0, 999000000}, 174 {1000, 1, 0}, 175 {1001, 1, 1000000}, 176 {-1, -1, 999000000}, 177 {-2, -1, 998000000}, 178 {-999, -1, 1000000}, 179 {-1000, -1, 0}, 180 {-1001, -2, 999000000}, 181 }; 182 } 183 184 @Test(dataProvider="MillisDurationNoNanos") 185 public void factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond) { 186 Duration test = Duration.ofMillis(millis); 187 assertEquals(test.getSeconds(), expectedSeconds); 188 assertEquals(test.getNano(), expectedNanoOfSecond); 189 } 190 191 //----------------------------------------------------------------------- 192 // ofNanos(long) 193 //----------------------------------------------------------------------- 194 @Test 195 public void factory_nanos_nanos() { 196 Duration test = Duration.ofNanos(1); 197 assertEquals(test.getSeconds(), 0); 198 assertEquals(test.getNano(), 1); 199 } 200 201 @Test 202 public void factory_nanos_nanosSecs() { 203 Duration test = Duration.ofNanos(1000000002); 204 assertEquals(test.getSeconds(), 1); 205 assertEquals(test.getNano(), 2); 206 } 207 208 @Test 209 public void factory_nanos_negative() { 210 Duration test = Duration.ofNanos(-2000000001); 211 assertEquals(test.getSeconds(), -3); 212 assertEquals(test.getNano(), 999999999); 213 } 214 215 @Test 216 public void factory_nanos_max() { 217 Duration test = Duration.ofNanos(Long.MAX_VALUE); 218 assertEquals(test.getSeconds(), Long.MAX_VALUE / 1000000000); 219 assertEquals(test.getNano(), Long.MAX_VALUE % 1000000000); 220 } 221 222 @Test 223 public void factory_nanos_min() { 224 Duration test = Duration.ofNanos(Long.MIN_VALUE); 225 assertEquals(test.getSeconds(), Long.MIN_VALUE / 1000000000 - 1); 226 assertEquals(test.getNano(), Long.MIN_VALUE % 1000000000 + 1000000000); 227 } 228 229 //----------------------------------------------------------------------- 230 // ofMinutes() 231 //----------------------------------------------------------------------- 232 @Test 233 public void factory_minutes() { 234 Duration test = Duration.ofMinutes(2); 235 assertEquals(test.getSeconds(), 120); 236 assertEquals(test.getNano(), 0); 237 } 238 239 @Test 240 public void factory_minutes_max() { 241 Duration test = Duration.ofMinutes(Long.MAX_VALUE / 60); 242 assertEquals(test.getSeconds(), (Long.MAX_VALUE / 60) * 60); 243 assertEquals(test.getNano(), 0); 244 } 245 246 @Test 247 public void factory_minutes_min() { 248 Duration test = Duration.ofMinutes(Long.MIN_VALUE / 60); 249 assertEquals(test.getSeconds(), (Long.MIN_VALUE / 60) * 60); 250 assertEquals(test.getNano(), 0); 251 } 252 253 @Test(expectedExceptions=ArithmeticException.class) 254 public void factory_minutes_tooBig() { 255 Duration.ofMinutes(Long.MAX_VALUE / 60 + 1); 256 } 257 258 @Test(expectedExceptions=ArithmeticException.class) 259 public void factory_minutes_tooSmall() { 260 Duration.ofMinutes(Long.MIN_VALUE / 60 - 1); 261 } 262 263 //----------------------------------------------------------------------- 264 // ofHours() 265 //----------------------------------------------------------------------- 266 @Test 267 public void factory_hours() { 268 Duration test = Duration.ofHours(2); 269 assertEquals(test.getSeconds(), 2 * 3600); 270 assertEquals(test.getNano(), 0); 271 } 272 273 @Test 274 public void factory_hours_max() { 275 Duration test = Duration.ofHours(Long.MAX_VALUE / 3600); 276 assertEquals(test.getSeconds(), (Long.MAX_VALUE / 3600) * 3600); 277 assertEquals(test.getNano(), 0); 278 } 279 280 @Test 281 public void factory_hours_min() { 282 Duration test = Duration.ofHours(Long.MIN_VALUE / 3600); 283 assertEquals(test.getSeconds(), (Long.MIN_VALUE / 3600) * 3600); 284 assertEquals(test.getNano(), 0); 285 } 286 287 @Test(expectedExceptions=ArithmeticException.class) 288 public void factory_hours_tooBig() { 289 Duration.ofHours(Long.MAX_VALUE / 3600 + 1); 290 } 291 292 @Test(expectedExceptions=ArithmeticException.class) 293 public void factory_hours_tooSmall() { 294 Duration.ofHours(Long.MIN_VALUE / 3600 - 1); 295 } 296 297 //----------------------------------------------------------------------- 298 // ofDays() 299 //----------------------------------------------------------------------- 300 @Test 301 public void factory_days() { 302 Duration test = Duration.ofDays(2); 303 assertEquals(test.getSeconds(), 2 * 86400); 304 assertEquals(test.getNano(), 0); 305 } 306 307 @Test 308 public void factory_days_max() { 309 Duration test = Duration.ofDays(Long.MAX_VALUE / 86400); 310 assertEquals(test.getSeconds(), (Long.MAX_VALUE / 86400) * 86400); 311 assertEquals(test.getNano(), 0); 312 } 313 314 @Test 315 public void factory_days_min() { 316 Duration test = Duration.ofDays(Long.MIN_VALUE / 86400); 317 assertEquals(test.getSeconds(), (Long.MIN_VALUE / 86400) * 86400); 318 assertEquals(test.getNano(), 0); 319 } 320 321 @Test(expectedExceptions=ArithmeticException.class) 322 public void factory_days_tooBig() { 323 Duration.ofDays(Long.MAX_VALUE / 86400 + 1); 324 } 325 326 @Test(expectedExceptions=ArithmeticException.class) 327 public void factory_days_tooSmall() { 328 Duration.ofDays(Long.MIN_VALUE / 86400 - 1); 329 } 330 331 //----------------------------------------------------------------------- 332 // of(long,TemporalUnit) 333 //----------------------------------------------------------------------- 334 @DataProvider(name="OfTemporalUnit") 335 Object[][] provider_factory_of_longTemporalUnit() { 336 return new Object[][] { 337 {0, NANOS, 0, 0}, 338 {0, MICROS, 0, 0}, 339 {0, MILLIS, 0, 0}, 340 {0, SECONDS, 0, 0}, 341 {0, MINUTES, 0, 0}, 342 {0, HOURS, 0, 0}, 343 {0, HALF_DAYS, 0, 0}, 344 {0, DAYS, 0, 0}, 345 {1, NANOS, 0, 1}, 346 {1, MICROS, 0, 1000}, 347 {1, MILLIS, 0, 1000000}, 348 {1, SECONDS, 1, 0}, 349 {1, MINUTES, 60, 0}, 350 {1, HOURS, 3600, 0}, 351 {1, HALF_DAYS, 43200, 0}, 352 {1, DAYS, 86400, 0}, 353 {3, NANOS, 0, 3}, 354 {3, MICROS, 0, 3000}, 355 {3, MILLIS, 0, 3000000}, 356 {3, SECONDS, 3, 0}, 357 {3, MINUTES, 3 * 60, 0}, 358 {3, HOURS, 3 * 3600, 0}, 359 {3, HALF_DAYS, 3 * 43200, 0}, 360 {3, DAYS, 3 * 86400, 0}, 361 {-1, NANOS, -1, 999999999}, 362 {-1, MICROS, -1, 999999000}, 363 {-1, MILLIS, -1, 999000000}, 364 {-1, SECONDS, -1, 0}, 365 {-1, MINUTES, -60, 0}, 366 {-1, HOURS, -3600, 0}, 367 {-1, HALF_DAYS, -43200, 0}, 368 {-1, DAYS, -86400, 0}, 369 {-3, NANOS, -1, 999999997}, 370 {-3, MICROS, -1, 999997000}, 371 {-3, MILLIS, -1, 997000000}, 372 {-3, SECONDS, -3, 0}, 373 {-3, MINUTES, -3 * 60, 0}, 374 {-3, HOURS, -3 * 3600, 0}, 375 {-3, HALF_DAYS, -3 * 43200, 0}, 376 {-3, DAYS, -3 * 86400, 0}, 377 {Long.MAX_VALUE, NANOS, Long.MAX_VALUE / 1000000000, (int) (Long.MAX_VALUE % 1000000000)}, 378 {Long.MIN_VALUE, NANOS, Long.MIN_VALUE / 1000000000 - 1, (int) (Long.MIN_VALUE % 1000000000 + 1000000000)}, 379 {Long.MAX_VALUE, MICROS, Long.MAX_VALUE / 1000000, (int) ((Long.MAX_VALUE % 1000000) * 1000)}, 380 {Long.MIN_VALUE, MICROS, Long.MIN_VALUE / 1000000 - 1, (int) ((Long.MIN_VALUE % 1000000 + 1000000) * 1000)}, 381 {Long.MAX_VALUE, MILLIS, Long.MAX_VALUE / 1000, (int) ((Long.MAX_VALUE % 1000) * 1000000)}, 382 {Long.MIN_VALUE, MILLIS, Long.MIN_VALUE / 1000 - 1, (int) ((Long.MIN_VALUE % 1000 + 1000) * 1000000)}, 383 {Long.MAX_VALUE, SECONDS, Long.MAX_VALUE, 0}, 384 {Long.MIN_VALUE, SECONDS, Long.MIN_VALUE, 0}, 385 {Long.MAX_VALUE / 60, MINUTES, (Long.MAX_VALUE / 60) * 60, 0}, 386 {Long.MIN_VALUE / 60, MINUTES, (Long.MIN_VALUE / 60) * 60, 0}, 387 {Long.MAX_VALUE / 3600, HOURS, (Long.MAX_VALUE / 3600) * 3600, 0}, 388 {Long.MIN_VALUE / 3600, HOURS, (Long.MIN_VALUE / 3600) * 3600, 0}, 389 {Long.MAX_VALUE / 43200, HALF_DAYS, (Long.MAX_VALUE / 43200) * 43200, 0}, 390 {Long.MIN_VALUE / 43200, HALF_DAYS, (Long.MIN_VALUE / 43200) * 43200, 0}, 391 }; 392 } 393 394 @Test(dataProvider="OfTemporalUnit") 395 public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) { 396 Duration t = Duration.of(amount, unit); 397 assertEquals(t.getSeconds(), expectedSeconds); 398 assertEquals(t.getNano(), expectedNanoOfSecond); 399 } 400 401 @DataProvider(name="OfTemporalUnitOutOfRange") 402 Object[][] provider_factory_of_longTemporalUnit_outOfRange() { 403 return new Object[][] { 404 {Long.MAX_VALUE / 60 + 1, MINUTES}, 405 {Long.MIN_VALUE / 60 - 1, MINUTES}, 406 {Long.MAX_VALUE / 3600 + 1, HOURS}, 407 {Long.MIN_VALUE / 3600 - 1, HOURS}, 408 {Long.MAX_VALUE / 43200 + 1, HALF_DAYS}, 409 {Long.MIN_VALUE / 43200 - 1, HALF_DAYS}, 410 }; 411 } 412 413 @Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class) 414 public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) { 415 Duration.of(amount, unit); 416 } 417 418 @Test(expectedExceptions=DateTimeException.class) 419 public void factory_of_longTemporalUnit_estimatedUnit() { 420 Duration.of(2, WEEKS); 421 } 422 423 @Test(expectedExceptions=NullPointerException.class) 424 public void factory_of_longTemporalUnit_null() { 425 Duration.of(1, (TemporalUnit) null); 426 } 427 428 //----------------------------------------------------------------------- 429 // from(TemporalAmount) 430 //----------------------------------------------------------------------- 431 @Test 432 public void factory_from_TemporalAmount_Duration() { 433 TemporalAmount amount = Duration.ofHours(3); 434 assertEquals(Duration.from(amount), Duration.ofHours(3)); 435 } 436 437 @Test 438 public void factory_from_TemporalAmount_DaysNanos() { 439 TemporalAmount amount = new TemporalAmount() { 440 @Override 441 public long get(TemporalUnit unit) { 442 if (unit == DAYS) { 443 return 23; 444 } else { 445 return 45; 446 } 447 } 448 @Override 449 public List<TemporalUnit> getUnits() { 450 List<TemporalUnit> list = new ArrayList<>(); 451 list.add(DAYS); 452 list.add(NANOS); 453 return list; 454 } 455 @Override 456 public Temporal addTo(Temporal temporal) { 457 throw new UnsupportedOperationException(); 458 } 459 @Override 460 public Temporal subtractFrom(Temporal temporal) { 461 throw new UnsupportedOperationException(); 462 } 463 }; 464 Duration t = Duration.from(amount); 465 assertEquals(t.getSeconds(), 23 * 86400); 466 assertEquals(t.getNano(), 45); 467 } 468 469 @Test(expectedExceptions = ArithmeticException.class) 470 public void factory_from_TemporalAmount_Minutes_tooBig() { 471 TemporalAmount amount = new TemporalAmount() { 472 @Override 473 public long get(TemporalUnit unit) { 474 return (Long.MAX_VALUE / 60) + 2; 475 } 476 @Override 477 public List<TemporalUnit> getUnits() { 478 return Collections.<TemporalUnit>singletonList(MINUTES); 479 } 480 @Override 481 public Temporal addTo(Temporal temporal) { 482 throw new UnsupportedOperationException(); 483 } 484 @Override 485 public Temporal subtractFrom(Temporal temporal) { 486 throw new UnsupportedOperationException(); 487 } 488 }; 489 Duration.from(amount); 490 } 491 492 @Test(expectedExceptions = DateTimeException.class) 493 public void factory_from_TemporalAmount_Period() { 494 Duration.from(Period.ZERO); 495 } 496 497 @Test(expectedExceptions = NullPointerException.class) 498 public void factory_from_TemporalAmount_null() { 499 Duration.from(null); 500 } 501 502 //----------------------------------------------------------------------- 503 // parse(String) 504 //----------------------------------------------------------------------- 505 @DataProvider(name="parseSuccess") 506 Object[][] data_parseSuccess() { 507 return new Object[][] { 508 {"PT0S", 0, 0}, 509 {"PT1S", 1, 0}, 510 {"PT12S", 12, 0}, 511 {"PT123456789S", 123456789, 0}, 512 {"PT" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0}, 513 514 {"PT+1S", 1, 0}, 515 {"PT+12S", 12, 0}, 516 {"PT+123456789S", 123456789, 0}, 517 {"PT+" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0}, 518 519 {"PT-1S", -1, 0}, 520 {"PT-12S", -12, 0}, 521 {"PT-123456789S", -123456789, 0}, 522 {"PT" + Long.MIN_VALUE + "S", Long.MIN_VALUE, 0}, 523 524 525 {"PT0.1S", 0, 100000000}, 526 {"PT1.1S", 1, 100000000}, 527 {"PT1.12S", 1, 120000000}, 528 {"PT1.123S", 1, 123000000}, 529 {"PT1.1234S", 1, 123400000}, 530 {"PT1.12345S", 1, 123450000}, 531 {"PT1.123456S", 1, 123456000}, 532 {"PT1.1234567S", 1, 123456700}, 533 {"PT1.12345678S", 1, 123456780}, 534 {"PT1.123456789S", 1, 123456789}, 535 536 {"PT-0.1S", -1, 1000000000 - 100000000}, 537 {"PT-1.1S", -2, 1000000000 - 100000000}, 538 {"PT-1.12S", -2, 1000000000 - 120000000}, 539 {"PT-1.123S", -2, 1000000000 - 123000000}, 540 {"PT-1.1234S", -2, 1000000000 - 123400000}, 541 {"PT-1.12345S", -2, 1000000000 - 123450000}, 542 {"PT-1.123456S", -2, 1000000000 - 123456000}, 543 {"PT-1.1234567S", -2, 1000000000 - 123456700}, 544 {"PT-1.12345678S", -2, 1000000000 - 123456780}, 545 {"PT-1.123456789S", -2, 1000000000 - 123456789}, 546 547 {"PT" + Long.MAX_VALUE + ".123456789S", Long.MAX_VALUE, 123456789}, 548 {"PT" + Long.MIN_VALUE + ".000000000S", Long.MIN_VALUE, 0}, 549 550 {"PT12M", 12 * 60, 0}, 551 {"PT12M0.35S", 12 * 60, 350000000}, 552 {"PT12M1.35S", 12 * 60 + 1, 350000000}, 553 {"PT12M-0.35S", 12 * 60 - 1, 1000000000 - 350000000}, 554 {"PT12M-1.35S", 12 * 60 - 2, 1000000000 - 350000000}, 555 556 {"PT12H", 12 * 3600, 0}, 557 {"PT12H0.35S", 12 * 3600, 350000000}, 558 {"PT12H1.35S", 12 * 3600 + 1, 350000000}, 559 {"PT12H-0.35S", 12 * 3600 - 1, 1000000000 - 350000000}, 560 {"PT12H-1.35S", 12 * 3600 - 2, 1000000000 - 350000000}, 561 562 {"P12D", 12 * 24 * 3600, 0}, 563 {"P12DT0.35S", 12 * 24 * 3600, 350000000}, 564 {"P12DT1.35S", 12 * 24 * 3600 + 1, 350000000}, 565 {"P12DT-0.35S", 12 * 24 * 3600 - 1, 1000000000 - 350000000}, 566 {"P12DT-1.35S", 12 * 24 * 3600 - 2, 1000000000 - 350000000}, 567 568 {"PT01S", 1, 0}, 569 {"PT001S", 1, 0}, 570 {"PT000S", 0, 0}, 571 {"PT+01S", 1, 0}, 572 {"PT-01S", -1, 0}, 573 574 {"PT1.S", 1, 0}, 575 {"PT+1.S", 1, 0}, 576 {"PT-1.S", -1, 0}, 577 578 {"P0D", 0, 0}, 579 {"P0DT0H", 0, 0}, 580 {"P0DT0M", 0, 0}, 581 {"P0DT0S", 0, 0}, 582 {"P0DT0H0S", 0, 0}, 583 {"P0DT0M0S", 0, 0}, 584 {"P0DT0H0M0S", 0, 0}, 585 586 {"P1D", 86400, 0}, 587 {"P1DT0H", 86400, 0}, 588 {"P1DT0M", 86400, 0}, 589 {"P1DT0S", 86400, 0}, 590 {"P1DT0H0S", 86400, 0}, 591 {"P1DT0M0S", 86400, 0}, 592 {"P1DT0H0M0S", 86400, 0}, 593 594 {"P3D", 86400 * 3, 0}, 595 {"P3DT2H", 86400 * 3 + 3600 * 2, 0}, 596 {"P3DT2M", 86400 * 3 + 60 * 2, 0}, 597 {"P3DT2S", 86400 * 3 + 2, 0}, 598 {"P3DT2H1S", 86400 * 3 + 3600 * 2 + 1, 0}, 599 {"P3DT2M1S", 86400 * 3 + 60 * 2 + 1, 0}, 600 {"P3DT2H1M1S", 86400 * 3 + 3600 * 2 + 60 + 1, 0}, 601 602 {"P-3D", -86400 * 3, 0}, 603 {"P-3DT2H", -86400 * 3 + 3600 * 2, 0}, 604 {"P-3DT2M", -86400 * 3 + 60 * 2, 0}, 605 {"P-3DT2S", -86400 * 3 + 2, 0}, 606 {"P-3DT2H1S", -86400 * 3 + 3600 * 2 + 1, 0}, 607 {"P-3DT2M1S", -86400 * 3 + 60 * 2 + 1, 0}, 608 {"P-3DT2H1M1S", -86400 * 3 + 3600 * 2 + 60 + 1, 0}, 609 610 {"P-3DT-2H", -86400 * 3 - 3600 * 2, 0}, 611 {"P-3DT-2M", -86400 * 3 - 60 * 2, 0}, 612 {"P-3DT-2S", -86400 * 3 - 2, 0}, 613 {"P-3DT-2H1S", -86400 * 3 - 3600 * 2 + 1, 0}, 614 {"P-3DT-2M1S", -86400 * 3 - 60 * 2 + 1, 0}, 615 {"P-3DT-2H1M1S", -86400 * 3 - 3600 * 2 + 60 + 1, 0}, 616 617 {"PT0H", 0, 0}, 618 {"PT0H0M", 0, 0}, 619 {"PT0H0S", 0, 0}, 620 {"PT0H0M0S", 0, 0}, 621 622 {"PT1H", 3600, 0}, 623 {"PT3H", 3600 * 3, 0}, 624 {"PT-1H", -3600, 0}, 625 {"PT-3H", -3600 * 3, 0}, 626 627 {"PT2H5M", 3600 * 2 + 60 * 5, 0}, 628 {"PT2H5S", 3600 * 2 + 5, 0}, 629 {"PT2H5M8S", 3600 * 2 + 60 * 5 + 8, 0}, 630 {"PT-2H5M", -3600 * 2 + 60 * 5, 0}, 631 {"PT-2H5S", -3600 * 2 + 5, 0}, 632 {"PT-2H5M8S", -3600 * 2 + 60 * 5 + 8, 0}, 633 {"PT-2H-5M", -3600 * 2 - 60 * 5, 0}, 634 {"PT-2H-5S", -3600 * 2 - 5, 0}, 635 {"PT-2H-5M8S", -3600 * 2 - 60 * 5 + 8, 0}, 636 {"PT-2H-5M-8S", -3600 * 2 - 60 * 5 - 8, 0}, 637 638 {"PT0M", 0, 0}, 639 {"PT1M", 60, 0}, 640 {"PT3M", 60 * 3, 0}, 641 {"PT-1M", -60, 0}, 642 {"PT-3M", -60 * 3, 0}, 643 {"P0DT3M", 60 * 3, 0}, 644 {"P0DT-3M", -60 * 3, 0}, 645 }; 646 } 647 648 @Test(dataProvider="parseSuccess") 649 public void factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond) { 650 Duration test = Duration.parse(text); 651 assertEquals(test.getSeconds(), expectedSeconds); 652 assertEquals(test.getNano(), expectedNanoOfSecond); 653 } 654 655 @Test(dataProvider="parseSuccess") 656 public void factory_parse_plus(String text, long expectedSeconds, int expectedNanoOfSecond) { 657 Duration test = Duration.parse("+" + text); 658 assertEquals(test.getSeconds(), expectedSeconds); 659 assertEquals(test.getNano(), expectedNanoOfSecond); 660 } 661 662 @Test(dataProvider="parseSuccess") 663 public void factory_parse_minus(String text, long expectedSeconds, int expectedNanoOfSecond) { 664 Duration test; 665 try { 666 test = Duration.parse("-" + text); 667 } catch (DateTimeParseException ex) { 668 assertEquals(expectedSeconds == Long.MIN_VALUE, true); 669 return; 670 } 671 // not inside try/catch or it breaks test 672 assertEquals(test, Duration.ofSeconds(expectedSeconds, expectedNanoOfSecond).negated()); 673 } 674 675 @Test(dataProvider="parseSuccess") 676 public void factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond) { 677 text = text.replace('.', ','); 678 Duration test = Duration.parse(text); 679 assertEquals(test.getSeconds(), expectedSeconds); 680 assertEquals(test.getNano(), expectedNanoOfSecond); 681 } 682 683 @Test(dataProvider="parseSuccess") 684 public void factory_parse_lowerCase(String text, long expectedSeconds, int expectedNanoOfSecond) { 685 Duration test = Duration.parse(text.toLowerCase(Locale.ENGLISH)); 686 assertEquals(test.getSeconds(), expectedSeconds); 687 assertEquals(test.getNano(), expectedNanoOfSecond); 688 } 689 690 @DataProvider(name="parseFailure") 691 Object[][] data_parseFailure() { 692 return new Object[][] { 693 {""}, 694 {"ABCDEF"}, 695 {" PT0S"}, 696 {"PT0S "}, 697 698 {"PTS"}, 699 {"AT0S"}, 700 {"PA0S"}, 701 {"PT0A"}, 702 703 {"P0Y"}, 704 {"P1Y"}, 705 {"P-2Y"}, 706 {"P0M"}, 707 {"P1M"}, 708 {"P-2M"}, 709 {"P3Y2D"}, 710 {"P3M2D"}, 711 {"P3W"}, 712 {"P-3W"}, 713 {"P2YT30S"}, 714 {"P2MT30S"}, 715 716 {"P1DT"}, 717 718 {"PT+S"}, 719 {"PT-S"}, 720 {"PT.S"}, 721 {"PTAS"}, 722 723 {"PT-.S"}, 724 {"PT+.S"}, 725 726 {"PT1ABC2S"}, 727 {"PT1.1ABC2S"}, 728 729 {"PT123456789123456789123456789S"}, 730 {"PT0.1234567891S"}, 731 732 {"PT2.-3"}, 733 {"PT-2.-3"}, 734 {"PT2.+3"}, 735 {"PT-2.+3"}, 736 }; 737 } 738 739 @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class) 740 public void factory_parseFailures(String text) { 741 Duration.parse(text); 742 } 743 744 @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class) 745 public void factory_parseFailures_comma(String text) { 746 text = text.replace('.', ','); 747 Duration.parse(text); 748 } 749 750 @Test(expectedExceptions=DateTimeParseException.class) 751 public void factory_parse_tooBig() { 752 Duration.parse("PT" + Long.MAX_VALUE + "1S"); 753 } 754 755 @Test(expectedExceptions=DateTimeParseException.class) 756 public void factory_parse_tooBig_decimal() { 757 Duration.parse("PT" + Long.MAX_VALUE + "1.1S"); 758 } 759 760 @Test(expectedExceptions=DateTimeParseException.class) 761 public void factory_parse_tooSmall() { 762 Duration.parse("PT" + Long.MIN_VALUE + "1S"); 763 } 764 765 @Test(expectedExceptions=DateTimeParseException.class) 766 public void factory_parse_tooSmall_decimal() { 767 Duration.parse("PT" + Long.MIN_VALUE + ".1S"); 768 } 769 770 @Test(expectedExceptions=NullPointerException.class) 771 public void factory_parse_nullText() { 772 Duration.parse(null); 773 } 774 775 //----------------------------------------------------------------------- 776 // between() 777 //----------------------------------------------------------------------- 778 @DataProvider(name="durationBetweenInstant") 779 Object[][] data_durationBetweenInstant() { 780 return new Object[][] { 781 {0, 0, 0, 0, 0, 0}, 782 {3, 0, 7, 0, 4, 0}, 783 {7, 0, 3, 0, -4, 0}, 784 785 {3, 20, 7, 50, 4, 30}, 786 {3, 80, 7, 50, 3, 999999970}, 787 {3, 80, 7, 79, 3, 999999999}, 788 {3, 80, 7, 80, 4, 0}, 789 {3, 80, 7, 81, 4, 1}, 790 }; 791 } 792 793 @Test(dataProvider="durationBetweenInstant") 794 public void factory_between_TemporalTemporal_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) { 795 Instant start = Instant.ofEpochSecond(secs1, nanos1); 796 Instant end = Instant.ofEpochSecond(secs2, nanos2); 797 Duration t = Duration.between(start, end); 798 assertEquals(t.getSeconds(), expectedSeconds); 799 assertEquals(t.getNano(), expectedNanoOfSecond); 800 } 801 802 @Test(dataProvider="durationBetweenInstant") 803 public void factory_between_TemporalTemporal_Instant_negated(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) { 804 Instant start = Instant.ofEpochSecond(secs1, nanos1); 805 Instant end = Instant.ofEpochSecond(secs2, nanos2); 806 assertEquals(Duration.between(end, start), Duration.between(start, end).negated()); 807 } 808 809 @DataProvider(name="durationBetweenLocalTime") 810 Object[][] data_durationBetweenLocalTime() { 811 return new Object[][] { 812 {LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 45), 15L, 0}, 813 {LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 25), -5L, 0}, 814 }; 815 } 816 817 @Test(dataProvider="durationBetweenLocalTime") 818 public void factory_between_TemporalTemporal_LT(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond) { 819 Duration t = Duration.between(start, end); 820 assertEquals(t.getSeconds(), expectedSeconds); 821 assertEquals(t.getNano(), expectedNanoOfSecond); 822 } 823 824 @Test(dataProvider="durationBetweenLocalTime") 825 public void factory_between_TemporalTemporal_LT_negated(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond) { 826 assertEquals(Duration.between(end, start), Duration.between(start, end).negated()); 827 } 828 829 @DataProvider(name="durationBetweenLocalDateTime") 830 Object[][] data_durationBetweenLocalDateTime() { 831 return new Object[][] { 832 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 30, 65_000_000), -2L, 500_000_000}, 833 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), -1L, 500_000_000}, 834 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 32, 65_000_000), 0L, 500_000_000}, 835 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 33, 65_000_000), 1L, 500_000_000}, 836 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 34, 65_000_000), 2L, 500_000_000}, 837 838 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 30, 565_000_000), -1L, 500_000_000}, 839 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 31, 565_000_000), 0L, 500_000_000}, 840 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 32, 565_000_000), 1L, 500_000_000}, 841 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 33, 565_000_000), 2L, 500_000_000}, 842 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 34, 565_000_000), 3L, 500_000_000}, 843 844 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 30, 65_000_000), -1L, 0}, 845 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), 0L, 0}, 846 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 32, 65_000_000), 1L, 0}, 847 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 33, 65_000_000), 2L, 0}, 848 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2013, 3, 24, 0, 44, 34, 65_000_000), 3L, 0}, 849 850 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2813, 3, 24, 0, 44, 30, 565_000_000), 2 * CYCLE_SECS - 1L, 500_000_000}, 851 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2813, 3, 24, 0, 44, 31, 565_000_000), 2 * CYCLE_SECS + 0L, 500_000_000}, 852 {LocalDateTime.of(2013, 3, 24, 0, 44, 31, 65_000_000), LocalDateTime.of(2813, 3, 24, 0, 44, 32, 565_000_000), 2 * CYCLE_SECS + 1L, 500_000_000}, 853 }; 854 } 855 856 @Test(dataProvider="durationBetweenLocalDateTime") 857 public void factory_between_TemporalTemporal_LDT(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond) { 858 Duration t = Duration.between(start, end); 859 assertEquals(t.getSeconds(), expectedSeconds); 860 assertEquals(t.getNano(), expectedNanoOfSecond); 861 } 862 863 @Test(dataProvider="durationBetweenLocalDateTime") 864 public void factory_between_TemporalTemporal_LDT_negated(LocalDateTime start, LocalDateTime end, long expectedSeconds, int expectedNanoOfSecond) { 865 assertEquals(Duration.between(end, start), Duration.between(start, end).negated()); 866 } 867 868 @Test 869 public void factory_between_TemporalTemporal_mixedTypes() { 870 Instant start = Instant.ofEpochSecond(1); 871 ZonedDateTime end = Instant.ofEpochSecond(4).atZone(ZoneOffset.UTC); 872 assertEquals(Duration.between(start, end), Duration.ofSeconds(3)); 873 } 874 875 @Test(expectedExceptions=DateTimeException.class) 876 public void factory_between_TemporalTemporal_invalidMixedTypes() { 877 Instant start = Instant.ofEpochSecond(1); 878 LocalDate end = LocalDate.of(2010, 6, 20); 879 Duration.between(start, end); 880 } 881 882 @Test(expectedExceptions=NullPointerException.class) 883 public void factory_between__TemporalTemporal_startNull() { 884 Instant end = Instant.ofEpochSecond(1); 885 Duration.between(null, end); 886 } 887 888 @Test(expectedExceptions=NullPointerException.class) 889 public void factory_between__TemporalTemporal_endNull() { 890 Instant start = Instant.ofEpochSecond(1); 891 Duration.between(start, null); 892 } 893 894 //----------------------------------------------------------------------- 895 // isZero(), isPositive(), isPositiveOrZero(), isNegative(), isNegativeOrZero() 896 //----------------------------------------------------------------------- 897 @Test 898 public void test_isZero() { 899 assertEquals(Duration.ofNanos(0).isZero(), true); 900 assertEquals(Duration.ofSeconds(0).isZero(), true); 901 assertEquals(Duration.ofNanos(1).isZero(), false); 902 assertEquals(Duration.ofSeconds(1).isZero(), false); 903 assertEquals(Duration.ofSeconds(1, 1).isZero(), false); 904 assertEquals(Duration.ofNanos(-1).isZero(), false); 905 assertEquals(Duration.ofSeconds(-1).isZero(), false); 906 assertEquals(Duration.ofSeconds(-1, -1).isZero(), false); 907 } 908 909 @Test 910 public void test_isNegative() { 911 assertEquals(Duration.ofNanos(0).isNegative(), false); 912 assertEquals(Duration.ofSeconds(0).isNegative(), false); 913 assertEquals(Duration.ofNanos(1).isNegative(), false); 914 assertEquals(Duration.ofSeconds(1).isNegative(), false); 915 assertEquals(Duration.ofSeconds(1, 1).isNegative(), false); 916 assertEquals(Duration.ofNanos(-1).isNegative(), true); 917 assertEquals(Duration.ofSeconds(-1).isNegative(), true); 918 assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true); 919 } 920 921 //----------------------------------------------------------------------- 922 // plus() 923 //----------------------------------------------------------------------- 924 @DataProvider(name="Plus") 925 Object[][] provider_plus() { 926 return new Object[][] { 927 {Long.MIN_VALUE, 0, Long.MAX_VALUE, 0, -1, 0}, 928 929 {-4, 666666667, -4, 666666667, -7, 333333334}, 930 {-4, 666666667, -3, 0, -7, 666666667}, 931 {-4, 666666667, -2, 0, -6, 666666667}, 932 {-4, 666666667, -1, 0, -5, 666666667}, 933 {-4, 666666667, -1, 333333334, -4, 1}, 934 {-4, 666666667, -1, 666666667, -4, 333333334}, 935 {-4, 666666667, -1, 999999999, -4, 666666666}, 936 {-4, 666666667, 0, 0, -4, 666666667}, 937 {-4, 666666667, 0, 1, -4, 666666668}, 938 {-4, 666666667, 0, 333333333, -3, 0}, 939 {-4, 666666667, 0, 666666666, -3, 333333333}, 940 {-4, 666666667, 1, 0, -3, 666666667}, 941 {-4, 666666667, 2, 0, -2, 666666667}, 942 {-4, 666666667, 3, 0, -1, 666666667}, 943 {-4, 666666667, 3, 333333333, 0, 0}, 944 945 {-3, 0, -4, 666666667, -7, 666666667}, 946 {-3, 0, -3, 0, -6, 0}, 947 {-3, 0, -2, 0, -5, 0}, 948 {-3, 0, -1, 0, -4, 0}, 949 {-3, 0, -1, 333333334, -4, 333333334}, 950 {-3, 0, -1, 666666667, -4, 666666667}, 951 {-3, 0, -1, 999999999, -4, 999999999}, 952 {-3, 0, 0, 0, -3, 0}, 953 {-3, 0, 0, 1, -3, 1}, 954 {-3, 0, 0, 333333333, -3, 333333333}, 955 {-3, 0, 0, 666666666, -3, 666666666}, 956 {-3, 0, 1, 0, -2, 0}, 957 {-3, 0, 2, 0, -1, 0}, 958 {-3, 0, 3, 0, 0, 0}, 959 {-3, 0, 3, 333333333, 0, 333333333}, 960 961 {-2, 0, -4, 666666667, -6, 666666667}, 962 {-2, 0, -3, 0, -5, 0}, 963 {-2, 0, -2, 0, -4, 0}, 964 {-2, 0, -1, 0, -3, 0}, 965 {-2, 0, -1, 333333334, -3, 333333334}, 966 {-2, 0, -1, 666666667, -3, 666666667}, 967 {-2, 0, -1, 999999999, -3, 999999999}, 968 {-2, 0, 0, 0, -2, 0}, 969 {-2, 0, 0, 1, -2, 1}, 970 {-2, 0, 0, 333333333, -2, 333333333}, 971 {-2, 0, 0, 666666666, -2, 666666666}, 972 {-2, 0, 1, 0, -1, 0}, 973 {-2, 0, 2, 0, 0, 0}, 974 {-2, 0, 3, 0, 1, 0}, 975 {-2, 0, 3, 333333333, 1, 333333333}, 976 977 {-1, 0, -4, 666666667, -5, 666666667}, 978 {-1, 0, -3, 0, -4, 0}, 979 {-1, 0, -2, 0, -3, 0}, 980 {-1, 0, -1, 0, -2, 0}, 981 {-1, 0, -1, 333333334, -2, 333333334}, 982 {-1, 0, -1, 666666667, -2, 666666667}, 983 {-1, 0, -1, 999999999, -2, 999999999}, 984 {-1, 0, 0, 0, -1, 0}, 985 {-1, 0, 0, 1, -1, 1}, 986 {-1, 0, 0, 333333333, -1, 333333333}, 987 {-1, 0, 0, 666666666, -1, 666666666}, 988 {-1, 0, 1, 0, 0, 0}, 989 {-1, 0, 2, 0, 1, 0}, 990 {-1, 0, 3, 0, 2, 0}, 991 {-1, 0, 3, 333333333, 2, 333333333}, 992 993 {-1, 666666667, -4, 666666667, -4, 333333334}, 994 {-1, 666666667, -3, 0, -4, 666666667}, 995 {-1, 666666667, -2, 0, -3, 666666667}, 996 {-1, 666666667, -1, 0, -2, 666666667}, 997 {-1, 666666667, -1, 333333334, -1, 1}, 998 {-1, 666666667, -1, 666666667, -1, 333333334}, 999 {-1, 666666667, -1, 999999999, -1, 666666666}, 1000 {-1, 666666667, 0, 0, -1, 666666667}, 1001 {-1, 666666667, 0, 1, -1, 666666668}, 1002 {-1, 666666667, 0, 333333333, 0, 0}, 1003 {-1, 666666667, 0, 666666666, 0, 333333333}, 1004 {-1, 666666667, 1, 0, 0, 666666667}, 1005 {-1, 666666667, 2, 0, 1, 666666667}, 1006 {-1, 666666667, 3, 0, 2, 666666667}, 1007 {-1, 666666667, 3, 333333333, 3, 0}, 1008 1009 {0, 0, -4, 666666667, -4, 666666667}, 1010 {0, 0, -3, 0, -3, 0}, 1011 {0, 0, -2, 0, -2, 0}, 1012 {0, 0, -1, 0, -1, 0}, 1013 {0, 0, -1, 333333334, -1, 333333334}, 1014 {0, 0, -1, 666666667, -1, 666666667}, 1015 {0, 0, -1, 999999999, -1, 999999999}, 1016 {0, 0, 0, 0, 0, 0}, 1017 {0, 0, 0, 1, 0, 1}, 1018 {0, 0, 0, 333333333, 0, 333333333}, 1019 {0, 0, 0, 666666666, 0, 666666666}, 1020 {0, 0, 1, 0, 1, 0}, 1021 {0, 0, 2, 0, 2, 0}, 1022 {0, 0, 3, 0, 3, 0}, 1023 {0, 0, 3, 333333333, 3, 333333333}, 1024 1025 {0, 333333333, -4, 666666667, -3, 0}, 1026 {0, 333333333, -3, 0, -3, 333333333}, 1027 {0, 333333333, -2, 0, -2, 333333333}, 1028 {0, 333333333, -1, 0, -1, 333333333}, 1029 {0, 333333333, -1, 333333334, -1, 666666667}, 1030 {0, 333333333, -1, 666666667, 0, 0}, 1031 {0, 333333333, -1, 999999999, 0, 333333332}, 1032 {0, 333333333, 0, 0, 0, 333333333}, 1033 {0, 333333333, 0, 1, 0, 333333334}, 1034 {0, 333333333, 0, 333333333, 0, 666666666}, 1035 {0, 333333333, 0, 666666666, 0, 999999999}, 1036 {0, 333333333, 1, 0, 1, 333333333}, 1037 {0, 333333333, 2, 0, 2, 333333333}, 1038 {0, 333333333, 3, 0, 3, 333333333}, 1039 {0, 333333333, 3, 333333333, 3, 666666666}, 1040 1041 {1, 0, -4, 666666667, -3, 666666667}, 1042 {1, 0, -3, 0, -2, 0}, 1043 {1, 0, -2, 0, -1, 0}, 1044 {1, 0, -1, 0, 0, 0}, 1045 {1, 0, -1, 333333334, 0, 333333334}, 1046 {1, 0, -1, 666666667, 0, 666666667}, 1047 {1, 0, -1, 999999999, 0, 999999999}, 1048 {1, 0, 0, 0, 1, 0}, 1049 {1, 0, 0, 1, 1, 1}, 1050 {1, 0, 0, 333333333, 1, 333333333}, 1051 {1, 0, 0, 666666666, 1, 666666666}, 1052 {1, 0, 1, 0, 2, 0}, 1053 {1, 0, 2, 0, 3, 0}, 1054 {1, 0, 3, 0, 4, 0}, 1055 {1, 0, 3, 333333333, 4, 333333333}, 1056 1057 {2, 0, -4, 666666667, -2, 666666667}, 1058 {2, 0, -3, 0, -1, 0}, 1059 {2, 0, -2, 0, 0, 0}, 1060 {2, 0, -1, 0, 1, 0}, 1061 {2, 0, -1, 333333334, 1, 333333334}, 1062 {2, 0, -1, 666666667, 1, 666666667}, 1063 {2, 0, -1, 999999999, 1, 999999999}, 1064 {2, 0, 0, 0, 2, 0}, 1065 {2, 0, 0, 1, 2, 1}, 1066 {2, 0, 0, 333333333, 2, 333333333}, 1067 {2, 0, 0, 666666666, 2, 666666666}, 1068 {2, 0, 1, 0, 3, 0}, 1069 {2, 0, 2, 0, 4, 0}, 1070 {2, 0, 3, 0, 5, 0}, 1071 {2, 0, 3, 333333333, 5, 333333333}, 1072 1073 {3, 0, -4, 666666667, -1, 666666667}, 1074 {3, 0, -3, 0, 0, 0}, 1075 {3, 0, -2, 0, 1, 0}, 1076 {3, 0, -1, 0, 2, 0}, 1077 {3, 0, -1, 333333334, 2, 333333334}, 1078 {3, 0, -1, 666666667, 2, 666666667}, 1079 {3, 0, -1, 999999999, 2, 999999999}, 1080 {3, 0, 0, 0, 3, 0}, 1081 {3, 0, 0, 1, 3, 1}, 1082 {3, 0, 0, 333333333, 3, 333333333}, 1083 {3, 0, 0, 666666666, 3, 666666666}, 1084 {3, 0, 1, 0, 4, 0}, 1085 {3, 0, 2, 0, 5, 0}, 1086 {3, 0, 3, 0, 6, 0}, 1087 {3, 0, 3, 333333333, 6, 333333333}, 1088 1089 {3, 333333333, -4, 666666667, 0, 0}, 1090 {3, 333333333, -3, 0, 0, 333333333}, 1091 {3, 333333333, -2, 0, 1, 333333333}, 1092 {3, 333333333, -1, 0, 2, 333333333}, 1093 {3, 333333333, -1, 333333334, 2, 666666667}, 1094 {3, 333333333, -1, 666666667, 3, 0}, 1095 {3, 333333333, -1, 999999999, 3, 333333332}, 1096 {3, 333333333, 0, 0, 3, 333333333}, 1097 {3, 333333333, 0, 1, 3, 333333334}, 1098 {3, 333333333, 0, 333333333, 3, 666666666}, 1099 {3, 333333333, 0, 666666666, 3, 999999999}, 1100 {3, 333333333, 1, 0, 4, 333333333}, 1101 {3, 333333333, 2, 0, 5, 333333333}, 1102 {3, 333333333, 3, 0, 6, 333333333}, 1103 {3, 333333333, 3, 333333333, 6, 666666666}, 1104 1105 {Long.MAX_VALUE, 0, Long.MIN_VALUE, 0, -1, 0}, 1106 }; 1107 } 1108 1109 @Test(dataProvider="Plus") 1110 public void plus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1111 Duration t = Duration.ofSeconds(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos)); 1112 assertEquals(t.getSeconds(), expectedSeconds); 1113 assertEquals(t.getNano(), expectedNanoOfSecond); 1114 } 1115 1116 @Test(expectedExceptions=ArithmeticException.class) 1117 public void plusOverflowTooBig() { 1118 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999); 1119 t.plus(Duration.ofSeconds(0, 1)); 1120 } 1121 1122 @Test(expectedExceptions=ArithmeticException.class) 1123 public void plusOverflowTooSmall() { 1124 Duration t = Duration.ofSeconds(Long.MIN_VALUE); 1125 t.plus(Duration.ofSeconds(-1, 999999999)); 1126 } 1127 1128 //----------------------------------------------------------------------- 1129 @Test 1130 public void plus_longTemporalUnit_seconds() { 1131 Duration t = Duration.ofSeconds(1); 1132 t = t.plus(1, SECONDS); 1133 assertEquals(2, t.getSeconds()); 1134 assertEquals(0, t.getNano()); 1135 } 1136 1137 @Test 1138 public void plus_longTemporalUnit_millis() { 1139 Duration t = Duration.ofSeconds(1); 1140 t = t.plus(1, MILLIS); 1141 assertEquals(1, t.getSeconds()); 1142 assertEquals(1000000, t.getNano()); 1143 } 1144 1145 @Test 1146 public void plus_longTemporalUnit_micros() { 1147 Duration t = Duration.ofSeconds(1); 1148 t = t.plus(1, MICROS); 1149 assertEquals(1, t.getSeconds()); 1150 assertEquals(1000, t.getNano()); 1151 } 1152 1153 @Test 1154 public void plus_longTemporalUnit_nanos() { 1155 Duration t = Duration.ofSeconds(1); 1156 t = t.plus(1, NANOS); 1157 assertEquals(1, t.getSeconds()); 1158 assertEquals(1, t.getNano()); 1159 } 1160 1161 @Test(expectedExceptions=NullPointerException.class) 1162 public void plus_longTemporalUnit_null() { 1163 Duration t = Duration.ofSeconds(1); 1164 t.plus(1, (TemporalUnit) null); 1165 } 1166 1167 //----------------------------------------------------------------------- 1168 @DataProvider(name="PlusDays") 1169 Object[][] provider_plusDays_long() { 1170 return new Object[][] { 1171 {0, 0, 0}, 1172 {0, 1, 1}, 1173 {0, -1, -1}, 1174 {Long.MAX_VALUE/3600/24, 0, Long.MAX_VALUE/3600/24}, 1175 {Long.MIN_VALUE/3600/24, 0, Long.MIN_VALUE/3600/24}, 1176 {1, 0, 1}, 1177 {1, 1, 2}, 1178 {1, -1, 0}, 1179 {1, Long.MIN_VALUE/3600/24, Long.MIN_VALUE/3600/24 + 1}, 1180 {1, 0, 1}, 1181 {1, 1, 2}, 1182 {1, -1, 0}, 1183 {-1, 0, -1}, 1184 {-1, 1, 0}, 1185 {-1, -1, -2}, 1186 {-1, Long.MAX_VALUE/3600/24, Long.MAX_VALUE/3600/24 - 1}, 1187 }; 1188 } 1189 1190 @Test(dataProvider="PlusDays") 1191 public void plusDays_long(long days, long amount, long expectedDays) { 1192 Duration t = Duration.ofDays(days); 1193 t = t.plusDays(amount); 1194 assertEquals(t.toDays(), expectedDays); 1195 } 1196 1197 @Test(expectedExceptions = {ArithmeticException.class}) 1198 public void plusDays_long_overflowTooBig() { 1199 Duration t = Duration.ofDays(1); 1200 t.plusDays(Long.MAX_VALUE/3600/24); 1201 } 1202 1203 @Test(expectedExceptions = {ArithmeticException.class}) 1204 public void plusDays_long_overflowTooSmall() { 1205 Duration t = Duration.ofDays(-1); 1206 t.plusDays(Long.MIN_VALUE/3600/24); 1207 } 1208 1209 //----------------------------------------------------------------------- 1210 @DataProvider(name="PlusHours") 1211 Object[][] provider_plusHours_long() { 1212 return new Object[][] { 1213 {0, 0, 0}, 1214 {0, 1, 1}, 1215 {0, -1, -1}, 1216 {Long.MAX_VALUE/3600, 0, Long.MAX_VALUE/3600}, 1217 {Long.MIN_VALUE/3600, 0, Long.MIN_VALUE/3600}, 1218 {1, 0, 1}, 1219 {1, 1, 2}, 1220 {1, -1, 0}, 1221 {1, Long.MIN_VALUE/3600, Long.MIN_VALUE/3600 + 1}, 1222 {1, 0, 1}, 1223 {1, 1, 2}, 1224 {1, -1, 0}, 1225 {-1, 0, -1}, 1226 {-1, 1, 0}, 1227 {-1, -1, -2}, 1228 {-1, Long.MAX_VALUE/3600, Long.MAX_VALUE/3600 - 1}, 1229 }; 1230 } 1231 1232 @Test(dataProvider="PlusHours") 1233 public void plusHours_long(long hours, long amount, long expectedHours) { 1234 Duration t = Duration.ofHours(hours); 1235 t = t.plusHours(amount); 1236 assertEquals(t.toHours(), expectedHours); 1237 } 1238 1239 @Test(expectedExceptions = {ArithmeticException.class}) 1240 public void plusHours_long_overflowTooBig() { 1241 Duration t = Duration.ofHours(1); 1242 t.plusHours(Long.MAX_VALUE/3600); 1243 } 1244 1245 @Test(expectedExceptions = {ArithmeticException.class}) 1246 public void plusHours_long_overflowTooSmall() { 1247 Duration t = Duration.ofHours(-1); 1248 t.plusHours(Long.MIN_VALUE/3600); 1249 } 1250 1251 //----------------------------------------------------------------------- 1252 @DataProvider(name="PlusMinutes") 1253 Object[][] provider_plusMinutes_long() { 1254 return new Object[][] { 1255 {0, 0, 0}, 1256 {0, 1, 1}, 1257 {0, -1, -1}, 1258 {Long.MAX_VALUE/60, 0, Long.MAX_VALUE/60}, 1259 {Long.MIN_VALUE/60, 0, Long.MIN_VALUE/60}, 1260 {1, 0, 1}, 1261 {1, 1, 2}, 1262 {1, -1, 0}, 1263 {1, Long.MIN_VALUE/60, Long.MIN_VALUE/60 + 1}, 1264 {1, 0, 1}, 1265 {1, 1, 2}, 1266 {1, -1, 0}, 1267 {-1, 0, -1}, 1268 {-1, 1, 0}, 1269 {-1, -1, -2}, 1270 {-1, Long.MAX_VALUE/60, Long.MAX_VALUE/60 - 1}, 1271 }; 1272 } 1273 1274 @Test(dataProvider="PlusMinutes") 1275 public void plusMinutes_long(long minutes, long amount, long expectedMinutes) { 1276 Duration t = Duration.ofMinutes(minutes); 1277 t = t.plusMinutes(amount); 1278 assertEquals(t.toMinutes(), expectedMinutes); 1279 } 1280 1281 @Test(expectedExceptions = {ArithmeticException.class}) 1282 public void plusMinutes_long_overflowTooBig() { 1283 Duration t = Duration.ofMinutes(1); 1284 t.plusMinutes(Long.MAX_VALUE/60); 1285 } 1286 1287 @Test(expectedExceptions = {ArithmeticException.class}) 1288 public void plusMinutes_long_overflowTooSmall() { 1289 Duration t = Duration.ofMinutes(-1); 1290 t.plusMinutes(Long.MIN_VALUE/60); 1291 } 1292 1293 //----------------------------------------------------------------------- 1294 @DataProvider(name="PlusSeconds") 1295 Object[][] provider_plusSeconds_long() { 1296 return new Object[][] { 1297 {0, 0, 0, 0, 0}, 1298 {0, 0, 1, 1, 0}, 1299 {0, 0, -1, -1, 0}, 1300 {0, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0}, 1301 {0, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0}, 1302 {1, 0, 0, 1, 0}, 1303 {1, 0, 1, 2, 0}, 1304 {1, 0, -1, 0, 0}, 1305 {1, 0, Long.MAX_VALUE - 1, Long.MAX_VALUE, 0}, 1306 {1, 0, Long.MIN_VALUE, Long.MIN_VALUE + 1, 0}, 1307 {1, 1, 0, 1, 1}, 1308 {1, 1, 1, 2, 1}, 1309 {1, 1, -1, 0, 1}, 1310 {1, 1, Long.MAX_VALUE - 1, Long.MAX_VALUE, 1}, 1311 {1, 1, Long.MIN_VALUE, Long.MIN_VALUE + 1, 1}, 1312 {-1, 1, 0, -1, 1}, 1313 {-1, 1, 1, 0, 1}, 1314 {-1, 1, -1, -2, 1}, 1315 {-1, 1, Long.MAX_VALUE, Long.MAX_VALUE - 1, 1}, 1316 {-1, 1, Long.MIN_VALUE + 1, Long.MIN_VALUE, 1}, 1317 }; 1318 } 1319 1320 @Test(dataProvider="PlusSeconds") 1321 public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1322 Duration t = Duration.ofSeconds(seconds, nanos); 1323 t = t.plusSeconds(amount); 1324 assertEquals(t.getSeconds(), expectedSeconds); 1325 assertEquals(t.getNano(), expectedNanoOfSecond); 1326 } 1327 1328 @Test(expectedExceptions = {ArithmeticException.class}) 1329 public void plusSeconds_long_overflowTooBig() { 1330 Duration t = Duration.ofSeconds(1, 0); 1331 t.plusSeconds(Long.MAX_VALUE); 1332 } 1333 1334 @Test(expectedExceptions = {ArithmeticException.class}) 1335 public void plusSeconds_long_overflowTooSmall() { 1336 Duration t = Duration.ofSeconds(-1, 0); 1337 t.plusSeconds(Long.MIN_VALUE); 1338 } 1339 1340 //----------------------------------------------------------------------- 1341 @DataProvider(name="PlusMillis") 1342 Object[][] provider_plusMillis_long() { 1343 return new Object[][] { 1344 {0, 0, 0, 0, 0}, 1345 {0, 0, 1, 0, 1000000}, 1346 {0, 0, 999, 0, 999000000}, 1347 {0, 0, 1000, 1, 0}, 1348 {0, 0, 1001, 1, 1000000}, 1349 {0, 0, 1999, 1, 999000000}, 1350 {0, 0, 2000, 2, 0}, 1351 {0, 0, -1, -1, 999000000}, 1352 {0, 0, -999, -1, 1000000}, 1353 {0, 0, -1000, -1, 0}, 1354 {0, 0, -1001, -2, 999000000}, 1355 {0, 0, -1999, -2, 1000000}, 1356 1357 {0, 1, 0, 0, 1}, 1358 {0, 1, 1, 0, 1000001}, 1359 {0, 1, 998, 0, 998000001}, 1360 {0, 1, 999, 0, 999000001}, 1361 {0, 1, 1000, 1, 1}, 1362 {0, 1, 1998, 1, 998000001}, 1363 {0, 1, 1999, 1, 999000001}, 1364 {0, 1, 2000, 2, 1}, 1365 {0, 1, -1, -1, 999000001}, 1366 {0, 1, -2, -1, 998000001}, 1367 {0, 1, -1000, -1, 1}, 1368 {0, 1, -1001, -2, 999000001}, 1369 1370 {0, 1000000, 0, 0, 1000000}, 1371 {0, 1000000, 1, 0, 2000000}, 1372 {0, 1000000, 998, 0, 999000000}, 1373 {0, 1000000, 999, 1, 0}, 1374 {0, 1000000, 1000, 1, 1000000}, 1375 {0, 1000000, 1998, 1, 999000000}, 1376 {0, 1000000, 1999, 2, 0}, 1377 {0, 1000000, 2000, 2, 1000000}, 1378 {0, 1000000, -1, 0, 0}, 1379 {0, 1000000, -2, -1, 999000000}, 1380 {0, 1000000, -999, -1, 2000000}, 1381 {0, 1000000, -1000, -1, 1000000}, 1382 {0, 1000000, -1001, -1, 0}, 1383 {0, 1000000, -1002, -2, 999000000}, 1384 1385 {0, 999999999, 0, 0, 999999999}, 1386 {0, 999999999, 1, 1, 999999}, 1387 {0, 999999999, 999, 1, 998999999}, 1388 {0, 999999999, 1000, 1, 999999999}, 1389 {0, 999999999, 1001, 2, 999999}, 1390 {0, 999999999, -1, 0, 998999999}, 1391 {0, 999999999, -1000, -1, 999999999}, 1392 {0, 999999999, -1001, -1, 998999999}, 1393 }; 1394 } 1395 1396 @Test(dataProvider="PlusMillis") 1397 public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1398 Duration t = Duration.ofSeconds(seconds, nanos); 1399 t = t.plusMillis(amount); 1400 assertEquals(t.getSeconds(), expectedSeconds); 1401 assertEquals(t.getNano(), expectedNanoOfSecond); 1402 } 1403 @Test(dataProvider="PlusMillis") 1404 public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1405 Duration t = Duration.ofSeconds(seconds + 1, nanos); 1406 t = t.plusMillis(amount); 1407 assertEquals(t.getSeconds(), expectedSeconds + 1); 1408 assertEquals(t.getNano(), expectedNanoOfSecond); 1409 } 1410 @Test(dataProvider="PlusMillis") 1411 public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1412 Duration t = Duration.ofSeconds(seconds - 1, nanos); 1413 t = t.plusMillis(amount); 1414 assertEquals(t.getSeconds(), expectedSeconds - 1); 1415 assertEquals(t.getNano(), expectedNanoOfSecond); 1416 } 1417 1418 @Test 1419 public void plusMillis_long_max() { 1420 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999); 1421 t = t.plusMillis(1); 1422 assertEquals(t.getSeconds(), Long.MAX_VALUE); 1423 assertEquals(t.getNano(), 999999999); 1424 } 1425 1426 @Test(expectedExceptions = {ArithmeticException.class}) 1427 public void plusMillis_long_overflowTooBig() { 1428 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000); 1429 t.plusMillis(1); 1430 } 1431 1432 @Test 1433 public void plusMillis_long_min() { 1434 Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000); 1435 t = t.plusMillis(-1); 1436 assertEquals(t.getSeconds(), Long.MIN_VALUE); 1437 assertEquals(t.getNano(), 0); 1438 } 1439 1440 @Test(expectedExceptions = {ArithmeticException.class}) 1441 public void plusMillis_long_overflowTooSmall() { 1442 Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0); 1443 t.plusMillis(-1); 1444 } 1445 1446 //----------------------------------------------------------------------- 1447 @DataProvider(name="PlusNanos") 1448 Object[][] provider_plusNanos_long() { 1449 return new Object[][] { 1450 {0, 0, 0, 0, 0}, 1451 {0, 0, 1, 0, 1}, 1452 {0, 0, 999999999, 0, 999999999}, 1453 {0, 0, 1000000000, 1, 0}, 1454 {0, 0, 1000000001, 1, 1}, 1455 {0, 0, 1999999999, 1, 999999999}, 1456 {0, 0, 2000000000, 2, 0}, 1457 {0, 0, -1, -1, 999999999}, 1458 {0, 0, -999999999, -1, 1}, 1459 {0, 0, -1000000000, -1, 0}, 1460 {0, 0, -1000000001, -2, 999999999}, 1461 {0, 0, -1999999999, -2, 1}, 1462 1463 {1, 0, 0, 1, 0}, 1464 {1, 0, 1, 1, 1}, 1465 {1, 0, 999999999, 1, 999999999}, 1466 {1, 0, 1000000000, 2, 0}, 1467 {1, 0, 1000000001, 2, 1}, 1468 {1, 0, 1999999999, 2, 999999999}, 1469 {1, 0, 2000000000, 3, 0}, 1470 {1, 0, -1, 0, 999999999}, 1471 {1, 0, -999999999, 0, 1}, 1472 {1, 0, -1000000000, 0, 0}, 1473 {1, 0, -1000000001, -1, 999999999}, 1474 {1, 0, -1999999999, -1, 1}, 1475 1476 {-1, 0, 0, -1, 0}, 1477 {-1, 0, 1, -1, 1}, 1478 {-1, 0, 999999999, -1, 999999999}, 1479 {-1, 0, 1000000000, 0, 0}, 1480 {-1, 0, 1000000001, 0, 1}, 1481 {-1, 0, 1999999999, 0, 999999999}, 1482 {-1, 0, 2000000000, 1, 0}, 1483 {-1, 0, -1, -2, 999999999}, 1484 {-1, 0, -999999999, -2, 1}, 1485 {-1, 0, -1000000000, -2, 0}, 1486 {-1, 0, -1000000001, -3, 999999999}, 1487 {-1, 0, -1999999999, -3, 1}, 1488 1489 {1, 1, 0, 1, 1}, 1490 {1, 1, 1, 1, 2}, 1491 {1, 1, 999999998, 1, 999999999}, 1492 {1, 1, 999999999, 2, 0}, 1493 {1, 1, 1000000000, 2, 1}, 1494 {1, 1, 1999999998, 2, 999999999}, 1495 {1, 1, 1999999999, 3, 0}, 1496 {1, 1, 2000000000, 3, 1}, 1497 {1, 1, -1, 1, 0}, 1498 {1, 1, -2, 0, 999999999}, 1499 {1, 1, -1000000000, 0, 1}, 1500 {1, 1, -1000000001, 0, 0}, 1501 {1, 1, -1000000002, -1, 999999999}, 1502 {1, 1, -2000000000, -1, 1}, 1503 1504 {1, 999999999, 0, 1, 999999999}, 1505 {1, 999999999, 1, 2, 0}, 1506 {1, 999999999, 999999999, 2, 999999998}, 1507 {1, 999999999, 1000000000, 2, 999999999}, 1508 {1, 999999999, 1000000001, 3, 0}, 1509 {1, 999999999, -1, 1, 999999998}, 1510 {1, 999999999, -1000000000, 0, 999999999}, 1511 {1, 999999999, -1000000001, 0, 999999998}, 1512 {1, 999999999, -1999999999, 0, 0}, 1513 {1, 999999999, -2000000000, -1, 999999999}, 1514 1515 {Long.MAX_VALUE, 0, 999999999, Long.MAX_VALUE, 999999999}, 1516 {Long.MAX_VALUE - 1, 0, 1999999999, Long.MAX_VALUE, 999999999}, 1517 {Long.MIN_VALUE, 1, -1, Long.MIN_VALUE, 0}, 1518 {Long.MIN_VALUE + 1, 1, -1000000001, Long.MIN_VALUE, 0}, 1519 }; 1520 } 1521 1522 @Test(dataProvider="PlusNanos") 1523 public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1524 Duration t = Duration.ofSeconds(seconds, nanos); 1525 t = t.plusNanos(amount); 1526 assertEquals(t.getSeconds(), expectedSeconds); 1527 assertEquals(t.getNano(), expectedNanoOfSecond); 1528 } 1529 1530 @Test(expectedExceptions = {ArithmeticException.class}) 1531 public void plusNanos_long_overflowTooBig() { 1532 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999); 1533 t.plusNanos(1); 1534 } 1535 1536 @Test(expectedExceptions = {ArithmeticException.class}) 1537 public void plusNanos_long_overflowTooSmall() { 1538 Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0); 1539 t.plusNanos(-1); 1540 } 1541 1542 //----------------------------------------------------------------------- 1543 @DataProvider(name="Minus") 1544 Object[][] provider_minus() { 1545 return new Object[][] { 1546 {Long.MIN_VALUE, 0, Long.MIN_VALUE + 1, 0, -1, 0}, 1547 1548 {-4, 666666667, -4, 666666667, 0, 0}, 1549 {-4, 666666667, -3, 0, -1, 666666667}, 1550 {-4, 666666667, -2, 0, -2, 666666667}, 1551 {-4, 666666667, -1, 0, -3, 666666667}, 1552 {-4, 666666667, -1, 333333334, -3, 333333333}, 1553 {-4, 666666667, -1, 666666667, -3, 0}, 1554 {-4, 666666667, -1, 999999999, -4, 666666668}, 1555 {-4, 666666667, 0, 0, -4, 666666667}, 1556 {-4, 666666667, 0, 1, -4, 666666666}, 1557 {-4, 666666667, 0, 333333333, -4, 333333334}, 1558 {-4, 666666667, 0, 666666666, -4, 1}, 1559 {-4, 666666667, 1, 0, -5, 666666667}, 1560 {-4, 666666667, 2, 0, -6, 666666667}, 1561 {-4, 666666667, 3, 0, -7, 666666667}, 1562 {-4, 666666667, 3, 333333333, -7, 333333334}, 1563 1564 {-3, 0, -4, 666666667, 0, 333333333}, 1565 {-3, 0, -3, 0, 0, 0}, 1566 {-3, 0, -2, 0, -1, 0}, 1567 {-3, 0, -1, 0, -2, 0}, 1568 {-3, 0, -1, 333333334, -3, 666666666}, 1569 {-3, 0, -1, 666666667, -3, 333333333}, 1570 {-3, 0, -1, 999999999, -3, 1}, 1571 {-3, 0, 0, 0, -3, 0}, 1572 {-3, 0, 0, 1, -4, 999999999}, 1573 {-3, 0, 0, 333333333, -4, 666666667}, 1574 {-3, 0, 0, 666666666, -4, 333333334}, 1575 {-3, 0, 1, 0, -4, 0}, 1576 {-3, 0, 2, 0, -5, 0}, 1577 {-3, 0, 3, 0, -6, 0}, 1578 {-3, 0, 3, 333333333, -7, 666666667}, 1579 1580 {-2, 0, -4, 666666667, 1, 333333333}, 1581 {-2, 0, -3, 0, 1, 0}, 1582 {-2, 0, -2, 0, 0, 0}, 1583 {-2, 0, -1, 0, -1, 0}, 1584 {-2, 0, -1, 333333334, -2, 666666666}, 1585 {-2, 0, -1, 666666667, -2, 333333333}, 1586 {-2, 0, -1, 999999999, -2, 1}, 1587 {-2, 0, 0, 0, -2, 0}, 1588 {-2, 0, 0, 1, -3, 999999999}, 1589 {-2, 0, 0, 333333333, -3, 666666667}, 1590 {-2, 0, 0, 666666666, -3, 333333334}, 1591 {-2, 0, 1, 0, -3, 0}, 1592 {-2, 0, 2, 0, -4, 0}, 1593 {-2, 0, 3, 0, -5, 0}, 1594 {-2, 0, 3, 333333333, -6, 666666667}, 1595 1596 {-1, 0, -4, 666666667, 2, 333333333}, 1597 {-1, 0, -3, 0, 2, 0}, 1598 {-1, 0, -2, 0, 1, 0}, 1599 {-1, 0, -1, 0, 0, 0}, 1600 {-1, 0, -1, 333333334, -1, 666666666}, 1601 {-1, 0, -1, 666666667, -1, 333333333}, 1602 {-1, 0, -1, 999999999, -1, 1}, 1603 {-1, 0, 0, 0, -1, 0}, 1604 {-1, 0, 0, 1, -2, 999999999}, 1605 {-1, 0, 0, 333333333, -2, 666666667}, 1606 {-1, 0, 0, 666666666, -2, 333333334}, 1607 {-1, 0, 1, 0, -2, 0}, 1608 {-1, 0, 2, 0, -3, 0}, 1609 {-1, 0, 3, 0, -4, 0}, 1610 {-1, 0, 3, 333333333, -5, 666666667}, 1611 1612 {-1, 666666667, -4, 666666667, 3, 0}, 1613 {-1, 666666667, -3, 0, 2, 666666667}, 1614 {-1, 666666667, -2, 0, 1, 666666667}, 1615 {-1, 666666667, -1, 0, 0, 666666667}, 1616 {-1, 666666667, -1, 333333334, 0, 333333333}, 1617 {-1, 666666667, -1, 666666667, 0, 0}, 1618 {-1, 666666667, -1, 999999999, -1, 666666668}, 1619 {-1, 666666667, 0, 0, -1, 666666667}, 1620 {-1, 666666667, 0, 1, -1, 666666666}, 1621 {-1, 666666667, 0, 333333333, -1, 333333334}, 1622 {-1, 666666667, 0, 666666666, -1, 1}, 1623 {-1, 666666667, 1, 0, -2, 666666667}, 1624 {-1, 666666667, 2, 0, -3, 666666667}, 1625 {-1, 666666667, 3, 0, -4, 666666667}, 1626 {-1, 666666667, 3, 333333333, -4, 333333334}, 1627 1628 {0, 0, -4, 666666667, 3, 333333333}, 1629 {0, 0, -3, 0, 3, 0}, 1630 {0, 0, -2, 0, 2, 0}, 1631 {0, 0, -1, 0, 1, 0}, 1632 {0, 0, -1, 333333334, 0, 666666666}, 1633 {0, 0, -1, 666666667, 0, 333333333}, 1634 {0, 0, -1, 999999999, 0, 1}, 1635 {0, 0, 0, 0, 0, 0}, 1636 {0, 0, 0, 1, -1, 999999999}, 1637 {0, 0, 0, 333333333, -1, 666666667}, 1638 {0, 0, 0, 666666666, -1, 333333334}, 1639 {0, 0, 1, 0, -1, 0}, 1640 {0, 0, 2, 0, -2, 0}, 1641 {0, 0, 3, 0, -3, 0}, 1642 {0, 0, 3, 333333333, -4, 666666667}, 1643 1644 {0, 333333333, -4, 666666667, 3, 666666666}, 1645 {0, 333333333, -3, 0, 3, 333333333}, 1646 {0, 333333333, -2, 0, 2, 333333333}, 1647 {0, 333333333, -1, 0, 1, 333333333}, 1648 {0, 333333333, -1, 333333334, 0, 999999999}, 1649 {0, 333333333, -1, 666666667, 0, 666666666}, 1650 {0, 333333333, -1, 999999999, 0, 333333334}, 1651 {0, 333333333, 0, 0, 0, 333333333}, 1652 {0, 333333333, 0, 1, 0, 333333332}, 1653 {0, 333333333, 0, 333333333, 0, 0}, 1654 {0, 333333333, 0, 666666666, -1, 666666667}, 1655 {0, 333333333, 1, 0, -1, 333333333}, 1656 {0, 333333333, 2, 0, -2, 333333333}, 1657 {0, 333333333, 3, 0, -3, 333333333}, 1658 {0, 333333333, 3, 333333333, -3, 0}, 1659 1660 {1, 0, -4, 666666667, 4, 333333333}, 1661 {1, 0, -3, 0, 4, 0}, 1662 {1, 0, -2, 0, 3, 0}, 1663 {1, 0, -1, 0, 2, 0}, 1664 {1, 0, -1, 333333334, 1, 666666666}, 1665 {1, 0, -1, 666666667, 1, 333333333}, 1666 {1, 0, -1, 999999999, 1, 1}, 1667 {1, 0, 0, 0, 1, 0}, 1668 {1, 0, 0, 1, 0, 999999999}, 1669 {1, 0, 0, 333333333, 0, 666666667}, 1670 {1, 0, 0, 666666666, 0, 333333334}, 1671 {1, 0, 1, 0, 0, 0}, 1672 {1, 0, 2, 0, -1, 0}, 1673 {1, 0, 3, 0, -2, 0}, 1674 {1, 0, 3, 333333333, -3, 666666667}, 1675 1676 {2, 0, -4, 666666667, 5, 333333333}, 1677 {2, 0, -3, 0, 5, 0}, 1678 {2, 0, -2, 0, 4, 0}, 1679 {2, 0, -1, 0, 3, 0}, 1680 {2, 0, -1, 333333334, 2, 666666666}, 1681 {2, 0, -1, 666666667, 2, 333333333}, 1682 {2, 0, -1, 999999999, 2, 1}, 1683 {2, 0, 0, 0, 2, 0}, 1684 {2, 0, 0, 1, 1, 999999999}, 1685 {2, 0, 0, 333333333, 1, 666666667}, 1686 {2, 0, 0, 666666666, 1, 333333334}, 1687 {2, 0, 1, 0, 1, 0}, 1688 {2, 0, 2, 0, 0, 0}, 1689 {2, 0, 3, 0, -1, 0}, 1690 {2, 0, 3, 333333333, -2, 666666667}, 1691 1692 {3, 0, -4, 666666667, 6, 333333333}, 1693 {3, 0, -3, 0, 6, 0}, 1694 {3, 0, -2, 0, 5, 0}, 1695 {3, 0, -1, 0, 4, 0}, 1696 {3, 0, -1, 333333334, 3, 666666666}, 1697 {3, 0, -1, 666666667, 3, 333333333}, 1698 {3, 0, -1, 999999999, 3, 1}, 1699 {3, 0, 0, 0, 3, 0}, 1700 {3, 0, 0, 1, 2, 999999999}, 1701 {3, 0, 0, 333333333, 2, 666666667}, 1702 {3, 0, 0, 666666666, 2, 333333334}, 1703 {3, 0, 1, 0, 2, 0}, 1704 {3, 0, 2, 0, 1, 0}, 1705 {3, 0, 3, 0, 0, 0}, 1706 {3, 0, 3, 333333333, -1, 666666667}, 1707 1708 {3, 333333333, -4, 666666667, 6, 666666666}, 1709 {3, 333333333, -3, 0, 6, 333333333}, 1710 {3, 333333333, -2, 0, 5, 333333333}, 1711 {3, 333333333, -1, 0, 4, 333333333}, 1712 {3, 333333333, -1, 333333334, 3, 999999999}, 1713 {3, 333333333, -1, 666666667, 3, 666666666}, 1714 {3, 333333333, -1, 999999999, 3, 333333334}, 1715 {3, 333333333, 0, 0, 3, 333333333}, 1716 {3, 333333333, 0, 1, 3, 333333332}, 1717 {3, 333333333, 0, 333333333, 3, 0}, 1718 {3, 333333333, 0, 666666666, 2, 666666667}, 1719 {3, 333333333, 1, 0, 2, 333333333}, 1720 {3, 333333333, 2, 0, 1, 333333333}, 1721 {3, 333333333, 3, 0, 0, 333333333}, 1722 {3, 333333333, 3, 333333333, 0, 0}, 1723 1724 {Long.MAX_VALUE, 0, Long.MAX_VALUE, 0, 0, 0}, 1725 }; 1726 } 1727 1728 @Test(dataProvider="Minus") 1729 public void minus(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1730 Duration t = Duration.ofSeconds(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos)); 1731 assertEquals(t.getSeconds(), expectedSeconds); 1732 assertEquals(t.getNano(), expectedNanoOfSecond); 1733 } 1734 1735 @Test(expectedExceptions=ArithmeticException.class) 1736 public void minusOverflowTooSmall() { 1737 Duration t = Duration.ofSeconds(Long.MIN_VALUE); 1738 t.minus(Duration.ofSeconds(0, 1)); 1739 } 1740 1741 @Test(expectedExceptions=ArithmeticException.class) 1742 public void minusOverflowTooBig() { 1743 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999); 1744 t.minus(Duration.ofSeconds(-1, 999999999)); 1745 } 1746 1747 //----------------------------------------------------------------------- 1748 @Test 1749 public void minus_longTemporalUnit_seconds() { 1750 Duration t = Duration.ofSeconds(1); 1751 t = t.minus(1, SECONDS); 1752 assertEquals(0, t.getSeconds()); 1753 assertEquals(0, t.getNano()); 1754 } 1755 1756 @Test 1757 public void minus_longTemporalUnit_millis() { 1758 Duration t = Duration.ofSeconds(1); 1759 t = t.minus(1, MILLIS); 1760 assertEquals(0, t.getSeconds()); 1761 assertEquals(999000000, t.getNano()); 1762 } 1763 1764 @Test 1765 public void minus_longTemporalUnit_micros() { 1766 Duration t = Duration.ofSeconds(1); 1767 t = t.minus(1, MICROS); 1768 assertEquals(0, t.getSeconds()); 1769 assertEquals(999999000, t.getNano()); 1770 } 1771 1772 @Test 1773 public void minus_longTemporalUnit_nanos() { 1774 Duration t = Duration.ofSeconds(1); 1775 t = t.minus(1, NANOS); 1776 assertEquals(0, t.getSeconds()); 1777 assertEquals(999999999, t.getNano()); 1778 } 1779 1780 @Test(expectedExceptions=NullPointerException.class) 1781 public void minus_longTemporalUnit_null() { 1782 Duration t = Duration.ofSeconds(1); 1783 t.minus(1, (TemporalUnit) null); 1784 } 1785 1786 //----------------------------------------------------------------------- 1787 @DataProvider(name="MinusDays") 1788 Object[][] provider_minusDays_long() { 1789 return new Object[][] { 1790 {0, 0, 0}, 1791 {0, 1, -1}, 1792 {0, -1, 1}, 1793 {Long.MAX_VALUE/3600/24, 0, Long.MAX_VALUE/3600/24}, 1794 {Long.MIN_VALUE/3600/24, 0, Long.MIN_VALUE/3600/24}, 1795 {1, 0, 1}, 1796 {1, 1, 0}, 1797 {1, -1, 2}, 1798 {Long.MAX_VALUE/3600/24, 1, Long.MAX_VALUE/3600/24 - 1}, 1799 {Long.MIN_VALUE/3600/24, -1, Long.MIN_VALUE/3600/24 + 1}, 1800 {1, 0, 1}, 1801 {1, 1, 0}, 1802 {1, -1, 2}, 1803 {-1, 0, -1}, 1804 {-1, 1, -2}, 1805 {-1, -1, 0}, 1806 }; 1807 } 1808 1809 @Test(dataProvider="MinusDays") 1810 public void minusDays_long(long days, long amount, long expectedDays) { 1811 Duration t = Duration.ofDays(days); 1812 t = t.minusDays(amount); 1813 assertEquals(t.toDays(), expectedDays); 1814 } 1815 1816 @Test(expectedExceptions = {ArithmeticException.class}) 1817 public void minusDays_long_overflowTooBig() { 1818 Duration t = Duration.ofDays(Long.MAX_VALUE/3600/24); 1819 t.minusDays(-1); 1820 } 1821 1822 @Test(expectedExceptions = {ArithmeticException.class}) 1823 public void minusDays_long_overflowTooSmall() { 1824 Duration t = Duration.ofDays(Long.MIN_VALUE/3600/24); 1825 t.minusDays(1); 1826 } 1827 1828 //----------------------------------------------------------------------- 1829 @DataProvider(name="MinusHours") 1830 Object[][] provider_minusHours_long() { 1831 return new Object[][] { 1832 {0, 0, 0}, 1833 {0, 1, -1}, 1834 {0, -1, 1}, 1835 {Long.MAX_VALUE/3600, 0, Long.MAX_VALUE/3600}, 1836 {Long.MIN_VALUE/3600, 0, Long.MIN_VALUE/3600}, 1837 {1, 0, 1}, 1838 {1, 1, 0}, 1839 {1, -1, 2}, 1840 {Long.MAX_VALUE/3600, 1, Long.MAX_VALUE/3600 - 1}, 1841 {Long.MIN_VALUE/3600, -1, Long.MIN_VALUE/3600 + 1}, 1842 {1, 0, 1}, 1843 {1, 1, 0}, 1844 {1, -1, 2}, 1845 {-1, 0, -1}, 1846 {-1, 1, -2}, 1847 {-1, -1, 0}, 1848 }; 1849 } 1850 1851 @Test(dataProvider="MinusHours") 1852 public void minusHours_long(long hours, long amount, long expectedHours) { 1853 Duration t = Duration.ofHours(hours); 1854 t = t.minusHours(amount); 1855 assertEquals(t.toHours(), expectedHours); 1856 } 1857 1858 @Test(expectedExceptions = {ArithmeticException.class}) 1859 public void minusHours_long_overflowTooBig() { 1860 Duration t = Duration.ofHours(Long.MAX_VALUE/3600); 1861 t.minusHours(-1); 1862 } 1863 1864 @Test(expectedExceptions = {ArithmeticException.class}) 1865 public void minusHours_long_overflowTooSmall() { 1866 Duration t = Duration.ofHours(Long.MIN_VALUE/3600); 1867 t.minusHours(1); 1868 } 1869 1870 //----------------------------------------------------------------------- 1871 @DataProvider(name="MinusMinutes") 1872 Object[][] provider_minusminutes_long() { 1873 return new Object[][] { 1874 {0, 0, 0}, 1875 {0, 1, -1}, 1876 {0, -1, 1}, 1877 {Long.MAX_VALUE/60, 0, Long.MAX_VALUE/60}, 1878 {Long.MIN_VALUE/60, 0, Long.MIN_VALUE/60}, 1879 {1, 0, 1}, 1880 {1, 1, 0}, 1881 {1, -1, 2}, 1882 {Long.MAX_VALUE/60, 1, Long.MAX_VALUE/60 - 1}, 1883 {Long.MIN_VALUE/60, -1, Long.MIN_VALUE/60 + 1}, 1884 {1, 0, 1}, 1885 {1, 1, 0}, 1886 {1, -1, 2}, 1887 {-1, 0, -1}, 1888 {-1, 1, -2}, 1889 {-1, -1, 0}, 1890 }; 1891 } 1892 1893 @Test(dataProvider="MinusMinutes") 1894 public void minusMinutes_long(long minutes, long amount, long expectedMinutes) { 1895 Duration t = Duration.ofMinutes(minutes); 1896 t = t.minusMinutes(amount); 1897 assertEquals(t.toMinutes(), expectedMinutes); 1898 } 1899 1900 @Test(expectedExceptions = {ArithmeticException.class}) 1901 public void minusMinutes_long_overflowTooBig() { 1902 Duration t = Duration.ofMinutes(Long.MAX_VALUE/60); 1903 t.minusMinutes(-1); 1904 } 1905 1906 @Test(expectedExceptions = {ArithmeticException.class}) 1907 public void minusMinutes_long_overflowTooSmall() { 1908 Duration t = Duration.ofMinutes(Long.MIN_VALUE/60); 1909 t.minusMinutes(1); 1910 } 1911 1912 //----------------------------------------------------------------------- 1913 @DataProvider(name="MinusSeconds") 1914 Object[][] provider_minusSeconds_long() { 1915 return new Object[][] { 1916 {0, 0, 0, 0, 0}, 1917 {0, 0, 1, -1, 0}, 1918 {0, 0, -1, 1, 0}, 1919 {0, 0, Long.MAX_VALUE, -Long.MAX_VALUE, 0}, 1920 {0, 0, Long.MIN_VALUE + 1, Long.MAX_VALUE, 0}, 1921 {1, 0, 0, 1, 0}, 1922 {1, 0, 1, 0, 0}, 1923 {1, 0, -1, 2, 0}, 1924 {1, 0, Long.MAX_VALUE - 1, -Long.MAX_VALUE + 2, 0}, 1925 {1, 0, Long.MIN_VALUE + 2, Long.MAX_VALUE, 0}, 1926 {1, 1, 0, 1, 1}, 1927 {1, 1, 1, 0, 1}, 1928 {1, 1, -1, 2, 1}, 1929 {1, 1, Long.MAX_VALUE, -Long.MAX_VALUE + 1, 1}, 1930 {1, 1, Long.MIN_VALUE + 2, Long.MAX_VALUE, 1}, 1931 {-1, 1, 0, -1, 1}, 1932 {-1, 1, 1, -2, 1}, 1933 {-1, 1, -1, 0, 1}, 1934 {-1, 1, Long.MAX_VALUE, Long.MIN_VALUE, 1}, 1935 {-1, 1, Long.MIN_VALUE + 1, Long.MAX_VALUE - 1, 1}, 1936 }; 1937 } 1938 1939 @Test(dataProvider="MinusSeconds") 1940 public void minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1941 Duration t = Duration.ofSeconds(seconds, nanos); 1942 t = t.minusSeconds(amount); 1943 assertEquals(t.getSeconds(), expectedSeconds); 1944 assertEquals(t.getNano(), expectedNanoOfSecond); 1945 } 1946 1947 @Test(expectedExceptions = {ArithmeticException.class}) 1948 public void minusSeconds_long_overflowTooBig() { 1949 Duration t = Duration.ofSeconds(1, 0); 1950 t.minusSeconds(Long.MIN_VALUE + 1); 1951 } 1952 1953 @Test(expectedExceptions = {ArithmeticException.class}) 1954 public void minusSeconds_long_overflowTooSmall() { 1955 Duration t = Duration.ofSeconds(-2, 0); 1956 t.minusSeconds(Long.MAX_VALUE); 1957 } 1958 1959 //----------------------------------------------------------------------- 1960 @DataProvider(name="MinusMillis") 1961 Object[][] provider_minusMillis_long() { 1962 return new Object[][] { 1963 {0, 0, 0, 0, 0}, 1964 {0, 0, 1, -1, 999000000}, 1965 {0, 0, 999, -1, 1000000}, 1966 {0, 0, 1000, -1, 0}, 1967 {0, 0, 1001, -2, 999000000}, 1968 {0, 0, 1999, -2, 1000000}, 1969 {0, 0, 2000, -2, 0}, 1970 {0, 0, -1, 0, 1000000}, 1971 {0, 0, -999, 0, 999000000}, 1972 {0, 0, -1000, 1, 0}, 1973 {0, 0, -1001, 1, 1000000}, 1974 {0, 0, -1999, 1, 999000000}, 1975 1976 {0, 1, 0, 0, 1}, 1977 {0, 1, 1, -1, 999000001}, 1978 {0, 1, 998, -1, 2000001}, 1979 {0, 1, 999, -1, 1000001}, 1980 {0, 1, 1000, -1, 1}, 1981 {0, 1, 1998, -2, 2000001}, 1982 {0, 1, 1999, -2, 1000001}, 1983 {0, 1, 2000, -2, 1}, 1984 {0, 1, -1, 0, 1000001}, 1985 {0, 1, -2, 0, 2000001}, 1986 {0, 1, -1000, 1, 1}, 1987 {0, 1, -1001, 1, 1000001}, 1988 1989 {0, 1000000, 0, 0, 1000000}, 1990 {0, 1000000, 1, 0, 0}, 1991 {0, 1000000, 998, -1, 3000000}, 1992 {0, 1000000, 999, -1, 2000000}, 1993 {0, 1000000, 1000, -1, 1000000}, 1994 {0, 1000000, 1998, -2, 3000000}, 1995 {0, 1000000, 1999, -2, 2000000}, 1996 {0, 1000000, 2000, -2, 1000000}, 1997 {0, 1000000, -1, 0, 2000000}, 1998 {0, 1000000, -2, 0, 3000000}, 1999 {0, 1000000, -999, 1, 0}, 2000 {0, 1000000, -1000, 1, 1000000}, 2001 {0, 1000000, -1001, 1, 2000000}, 2002 {0, 1000000, -1002, 1, 3000000}, 2003 2004 {0, 999999999, 0, 0, 999999999}, 2005 {0, 999999999, 1, 0, 998999999}, 2006 {0, 999999999, 999, 0, 999999}, 2007 {0, 999999999, 1000, -1, 999999999}, 2008 {0, 999999999, 1001, -1, 998999999}, 2009 {0, 999999999, -1, 1, 999999}, 2010 {0, 999999999, -1000, 1, 999999999}, 2011 {0, 999999999, -1001, 2, 999999}, 2012 }; 2013 } 2014 2015 @Test(dataProvider="MinusMillis") 2016 public void minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 2017 Duration t = Duration.ofSeconds(seconds, nanos); 2018 t = t.minusMillis(amount); 2019 assertEquals(t.getSeconds(), expectedSeconds); 2020 assertEquals(t.getNano(), expectedNanoOfSecond); 2021 } 2022 @Test(dataProvider="MinusMillis") 2023 public void minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 2024 Duration t = Duration.ofSeconds(seconds + 1, nanos); 2025 t = t.minusMillis(amount); 2026 assertEquals(t.getSeconds(), expectedSeconds + 1); 2027 assertEquals(t.getNano(), expectedNanoOfSecond); 2028 } 2029 @Test(dataProvider="MinusMillis") 2030 public void minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 2031 Duration t = Duration.ofSeconds(seconds - 1, nanos); 2032 t = t.minusMillis(amount); 2033 assertEquals(t.getSeconds(), expectedSeconds - 1); 2034 assertEquals(t.getNano(), expectedNanoOfSecond); 2035 } 2036 2037 @Test 2038 public void minusMillis_long_max() { 2039 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 998999999); 2040 t = t.minusMillis(-1); 2041 assertEquals(t.getSeconds(), Long.MAX_VALUE); 2042 assertEquals(t.getNano(), 999999999); 2043 } 2044 2045 @Test(expectedExceptions = {ArithmeticException.class}) 2046 public void minusMillis_long_overflowTooBig() { 2047 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999000000); 2048 t.minusMillis(-1); 2049 } 2050 2051 @Test 2052 public void minusMillis_long_min() { 2053 Duration t = Duration.ofSeconds(Long.MIN_VALUE, 1000000); 2054 t = t.minusMillis(1); 2055 assertEquals(t.getSeconds(), Long.MIN_VALUE); 2056 assertEquals(t.getNano(), 0); 2057 } 2058 2059 @Test(expectedExceptions = {ArithmeticException.class}) 2060 public void minusMillis_long_overflowTooSmall() { 2061 Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0); 2062 t.minusMillis(1); 2063 } 2064 2065 //----------------------------------------------------------------------- 2066 @DataProvider(name="MinusNanos") 2067 Object[][] provider_minusNanos_long() { 2068 return new Object[][] { 2069 {0, 0, 0, 0, 0}, 2070 {0, 0, 1, -1, 999999999}, 2071 {0, 0, 999999999, -1, 1}, 2072 {0, 0, 1000000000, -1, 0}, 2073 {0, 0, 1000000001, -2, 999999999}, 2074 {0, 0, 1999999999, -2, 1}, 2075 {0, 0, 2000000000, -2, 0}, 2076 {0, 0, -1, 0, 1}, 2077 {0, 0, -999999999, 0, 999999999}, 2078 {0, 0, -1000000000, 1, 0}, 2079 {0, 0, -1000000001, 1, 1}, 2080 {0, 0, -1999999999, 1, 999999999}, 2081 2082 {1, 0, 0, 1, 0}, 2083 {1, 0, 1, 0, 999999999}, 2084 {1, 0, 999999999, 0, 1}, 2085 {1, 0, 1000000000, 0, 0}, 2086 {1, 0, 1000000001, -1, 999999999}, 2087 {1, 0, 1999999999, -1, 1}, 2088 {1, 0, 2000000000, -1, 0}, 2089 {1, 0, -1, 1, 1}, 2090 {1, 0, -999999999, 1, 999999999}, 2091 {1, 0, -1000000000, 2, 0}, 2092 {1, 0, -1000000001, 2, 1}, 2093 {1, 0, -1999999999, 2, 999999999}, 2094 2095 {-1, 0, 0, -1, 0}, 2096 {-1, 0, 1, -2, 999999999}, 2097 {-1, 0, 999999999, -2, 1}, 2098 {-1, 0, 1000000000, -2, 0}, 2099 {-1, 0, 1000000001, -3, 999999999}, 2100 {-1, 0, 1999999999, -3, 1}, 2101 {-1, 0, 2000000000, -3, 0}, 2102 {-1, 0, -1, -1, 1}, 2103 {-1, 0, -999999999, -1, 999999999}, 2104 {-1, 0, -1000000000, 0, 0}, 2105 {-1, 0, -1000000001, 0, 1}, 2106 {-1, 0, -1999999999, 0, 999999999}, 2107 2108 {1, 1, 0, 1, 1}, 2109 {1, 1, 1, 1, 0}, 2110 {1, 1, 999999998, 0, 3}, 2111 {1, 1, 999999999, 0, 2}, 2112 {1, 1, 1000000000, 0, 1}, 2113 {1, 1, 1999999998, -1, 3}, 2114 {1, 1, 1999999999, -1, 2}, 2115 {1, 1, 2000000000, -1, 1}, 2116 {1, 1, -1, 1, 2}, 2117 {1, 1, -2, 1, 3}, 2118 {1, 1, -1000000000, 2, 1}, 2119 {1, 1, -1000000001, 2, 2}, 2120 {1, 1, -1000000002, 2, 3}, 2121 {1, 1, -2000000000, 3, 1}, 2122 2123 {1, 999999999, 0, 1, 999999999}, 2124 {1, 999999999, 1, 1, 999999998}, 2125 {1, 999999999, 999999999, 1, 0}, 2126 {1, 999999999, 1000000000, 0, 999999999}, 2127 {1, 999999999, 1000000001, 0, 999999998}, 2128 {1, 999999999, -1, 2, 0}, 2129 {1, 999999999, -1000000000, 2, 999999999}, 2130 {1, 999999999, -1000000001, 3, 0}, 2131 {1, 999999999, -1999999999, 3, 999999998}, 2132 {1, 999999999, -2000000000, 3, 999999999}, 2133 2134 {Long.MAX_VALUE, 0, -999999999, Long.MAX_VALUE, 999999999}, 2135 {Long.MAX_VALUE - 1, 0, -1999999999, Long.MAX_VALUE, 999999999}, 2136 {Long.MIN_VALUE, 1, 1, Long.MIN_VALUE, 0}, 2137 {Long.MIN_VALUE + 1, 1, 1000000001, Long.MIN_VALUE, 0}, 2138 }; 2139 } 2140 2141 @Test(dataProvider="MinusNanos") 2142 public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 2143 Duration t = Duration.ofSeconds(seconds, nanos); 2144 t = t.minusNanos(amount); 2145 assertEquals(t.getSeconds(), expectedSeconds); 2146 assertEquals(t.getNano(), expectedNanoOfSecond); 2147 } 2148 2149 @Test(expectedExceptions = {ArithmeticException.class}) 2150 public void minusNanos_long_overflowTooBig() { 2151 Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999); 2152 t.minusNanos(-1); 2153 } 2154 2155 @Test(expectedExceptions = {ArithmeticException.class}) 2156 public void minusNanos_long_overflowTooSmall() { 2157 Duration t = Duration.ofSeconds(Long.MIN_VALUE, 0); 2158 t.minusNanos(1); 2159 } 2160 2161 //----------------------------------------------------------------------- 2162 // multipliedBy() 2163 //----------------------------------------------------------------------- 2164 @DataProvider(name="MultipliedBy") 2165 Object[][] provider_multipliedBy() { 2166 return new Object[][] { 2167 {-4, 666666667, -3, 9, 999999999}, 2168 {-4, 666666667, -2, 6, 666666666}, 2169 {-4, 666666667, -1, 3, 333333333}, 2170 {-4, 666666667, 0, 0, 0}, 2171 {-4, 666666667, 1, -4, 666666667}, 2172 {-4, 666666667, 2, -7, 333333334}, 2173 {-4, 666666667, 3, -10, 000000001}, 2174 2175 {-3, 0, -3, 9, 0}, 2176 {-3, 0, -2, 6, 0}, 2177 {-3, 0, -1, 3, 0}, 2178 {-3, 0, 0, 0, 0}, 2179 {-3, 0, 1, -3, 0}, 2180 {-3, 0, 2, -6, 0}, 2181 {-3, 0, 3, -9, 0}, 2182 2183 {-2, 0, -3, 6, 0}, 2184 {-2, 0, -2, 4, 0}, 2185 {-2, 0, -1, 2, 0}, 2186 {-2, 0, 0, 0, 0}, 2187 {-2, 0, 1, -2, 0}, 2188 {-2, 0, 2, -4, 0}, 2189 {-2, 0, 3, -6, 0}, 2190 2191 {-1, 0, -3, 3, 0}, 2192 {-1, 0, -2, 2, 0}, 2193 {-1, 0, -1, 1, 0}, 2194 {-1, 0, 0, 0, 0}, 2195 {-1, 0, 1, -1, 0}, 2196 {-1, 0, 2, -2, 0}, 2197 {-1, 0, 3, -3, 0}, 2198 2199 {-1, 500000000, -3, 1, 500000000}, 2200 {-1, 500000000, -2, 1, 0}, 2201 {-1, 500000000, -1, 0, 500000000}, 2202 {-1, 500000000, 0, 0, 0}, 2203 {-1, 500000000, 1, -1, 500000000}, 2204 {-1, 500000000, 2, -1, 0}, 2205 {-1, 500000000, 3, -2, 500000000}, 2206 2207 {0, 0, -3, 0, 0}, 2208 {0, 0, -2, 0, 0}, 2209 {0, 0, -1, 0, 0}, 2210 {0, 0, 0, 0, 0}, 2211 {0, 0, 1, 0, 0}, 2212 {0, 0, 2, 0, 0}, 2213 {0, 0, 3, 0, 0}, 2214 2215 {0, 500000000, -3, -2, 500000000}, 2216 {0, 500000000, -2, -1, 0}, 2217 {0, 500000000, -1, -1, 500000000}, 2218 {0, 500000000, 0, 0, 0}, 2219 {0, 500000000, 1, 0, 500000000}, 2220 {0, 500000000, 2, 1, 0}, 2221 {0, 500000000, 3, 1, 500000000}, 2222 2223 {1, 0, -3, -3, 0}, 2224 {1, 0, -2, -2, 0}, 2225 {1, 0, -1, -1, 0}, 2226 {1, 0, 0, 0, 0}, 2227 {1, 0, 1, 1, 0}, 2228 {1, 0, 2, 2, 0}, 2229 {1, 0, 3, 3, 0}, 2230 2231 {2, 0, -3, -6, 0}, 2232 {2, 0, -2, -4, 0}, 2233 {2, 0, -1, -2, 0}, 2234 {2, 0, 0, 0, 0}, 2235 {2, 0, 1, 2, 0}, 2236 {2, 0, 2, 4, 0}, 2237 {2, 0, 3, 6, 0}, 2238 2239 {3, 0, -3, -9, 0}, 2240 {3, 0, -2, -6, 0}, 2241 {3, 0, -1, -3, 0}, 2242 {3, 0, 0, 0, 0}, 2243 {3, 0, 1, 3, 0}, 2244 {3, 0, 2, 6, 0}, 2245 {3, 0, 3, 9, 0}, 2246 2247 {3, 333333333, -3, -10, 000000001}, 2248 {3, 333333333, -2, -7, 333333334}, 2249 {3, 333333333, -1, -4, 666666667}, 2250 {3, 333333333, 0, 0, 0}, 2251 {3, 333333333, 1, 3, 333333333}, 2252 {3, 333333333, 2, 6, 666666666}, 2253 {3, 333333333, 3, 9, 999999999}, 2254 }; 2255 } 2256 2257 @Test(dataProvider="MultipliedBy") 2258 public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) { 2259 Duration t = Duration.ofSeconds(seconds, nanos); 2260 t = t.multipliedBy(multiplicand); 2261 assertEquals(t.getSeconds(), expectedSeconds); 2262 assertEquals(t.getNano(), expectedNanos); 2263 } 2264 2265 @Test 2266 public void multipliedBy_max() { 2267 Duration test = Duration.ofSeconds(1); 2268 assertEquals(test.multipliedBy(Long.MAX_VALUE), Duration.ofSeconds(Long.MAX_VALUE)); 2269 } 2270 2271 @Test 2272 public void multipliedBy_min() { 2273 Duration test = Duration.ofSeconds(1); 2274 assertEquals(test.multipliedBy(Long.MIN_VALUE), Duration.ofSeconds(Long.MIN_VALUE)); 2275 } 2276 2277 @Test(expectedExceptions=ArithmeticException.class) 2278 public void multipliedBy_tooBig() { 2279 Duration test = Duration.ofSeconds(1, 1); 2280 test.multipliedBy(Long.MAX_VALUE); 2281 } 2282 2283 @Test(expectedExceptions=ArithmeticException.class) 2284 public void multipliedBy_tooBig_negative() { 2285 Duration test = Duration.ofSeconds(1, 1); 2286 test.multipliedBy(Long.MIN_VALUE); 2287 } 2288 2289 //----------------------------------------------------------------------- 2290 // dividedBy() 2291 //----------------------------------------------------------------------- 2292 @DataProvider(name="DividedBy") 2293 Object[][] provider_dividedBy() { 2294 return new Object[][] { 2295 {-4, 666666667, -3, 1, 111111111}, 2296 {-4, 666666667, -2, 1, 666666666}, 2297 {-4, 666666667, -1, 3, 333333333}, 2298 {-4, 666666667, 1, -4, 666666667}, 2299 {-4, 666666667, 2, -2, 333333334}, 2300 {-4, 666666667, 3, -2, 888888889}, 2301 2302 {-3, 0, -3, 1, 0}, 2303 {-3, 0, -2, 1, 500000000}, 2304 {-3, 0, -1, 3, 0}, 2305 {-3, 0, 1, -3, 0}, 2306 {-3, 0, 2, -2, 500000000}, 2307 {-3, 0, 3, -1, 0}, 2308 2309 {-2, 0, -3, 0, 666666666}, 2310 {-2, 0, -2, 1, 0}, 2311 {-2, 0, -1, 2, 0}, 2312 {-2, 0, 1, -2, 0}, 2313 {-2, 0, 2, -1, 0}, 2314 {-2, 0, 3, -1, 333333334}, 2315 2316 {-1, 0, -3, 0, 333333333}, 2317 {-1, 0, -2, 0, 500000000}, 2318 {-1, 0, -1, 1, 0}, 2319 {-1, 0, 1, -1, 0}, 2320 {-1, 0, 2, -1, 500000000}, 2321 {-1, 0, 3, -1, 666666667}, 2322 2323 {-1, 500000000, -3, 0, 166666666}, 2324 {-1, 500000000, -2, 0, 250000000}, 2325 {-1, 500000000, -1, 0, 500000000}, 2326 {-1, 500000000, 1, -1, 500000000}, 2327 {-1, 500000000, 2, -1, 750000000}, 2328 {-1, 500000000, 3, -1, 833333334}, 2329 2330 {0, 0, -3, 0, 0}, 2331 {0, 0, -2, 0, 0}, 2332 {0, 0, -1, 0, 0}, 2333 {0, 0, 1, 0, 0}, 2334 {0, 0, 2, 0, 0}, 2335 {0, 0, 3, 0, 0}, 2336 2337 {0, 500000000, -3, -1, 833333334}, 2338 {0, 500000000, -2, -1, 750000000}, 2339 {0, 500000000, -1, -1, 500000000}, 2340 {0, 500000000, 1, 0, 500000000}, 2341 {0, 500000000, 2, 0, 250000000}, 2342 {0, 500000000, 3, 0, 166666666}, 2343 2344 {1, 0, -3, -1, 666666667}, 2345 {1, 0, -2, -1, 500000000}, 2346 {1, 0, -1, -1, 0}, 2347 {1, 0, 1, 1, 0}, 2348 {1, 0, 2, 0, 500000000}, 2349 {1, 0, 3, 0, 333333333}, 2350 2351 {2, 0, -3, -1, 333333334}, 2352 {2, 0, -2, -1, 0}, 2353 {2, 0, -1, -2, 0}, 2354 {2, 0, 1, 2, 0}, 2355 {2, 0, 2, 1, 0}, 2356 {2, 0, 3, 0, 666666666}, 2357 2358 {3, 0, -3, -1, 0}, 2359 {3, 0, -2, -2, 500000000}, 2360 {3, 0, -1, -3, 0}, 2361 {3, 0, 1, 3, 0}, 2362 {3, 0, 2, 1, 500000000}, 2363 {3, 0, 3, 1, 0}, 2364 2365 {3, 333333333, -3, -2, 888888889}, 2366 {3, 333333333, -2, -2, 333333334}, 2367 {3, 333333333, -1, -4, 666666667}, 2368 {3, 333333333, 1, 3, 333333333}, 2369 {3, 333333333, 2, 1, 666666666}, 2370 {3, 333333333, 3, 1, 111111111}, 2371 }; 2372 } 2373 2374 @Test(dataProvider="DividedBy") 2375 public void dividedBy(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) { 2376 Duration t = Duration.ofSeconds(seconds, nanos); 2377 t = t.dividedBy(divisor); 2378 assertEquals(t.getSeconds(), expectedSeconds); 2379 assertEquals(t.getNano(), expectedNanos); 2380 } 2381 2382 @Test(dataProvider="DividedBy", expectedExceptions=ArithmeticException.class) 2383 public void dividedByZero(long seconds, int nanos, int divisor, long expectedSeconds, int expectedNanos) { 2384 Duration t = Duration.ofSeconds(seconds, nanos); 2385 t.dividedBy(0); 2386 fail(t + " divided by zero did not throw ArithmeticException"); 2387 } 2388 2389 @Test 2390 public void dividedBy_max() { 2391 Duration test = Duration.ofSeconds(Long.MAX_VALUE); 2392 assertEquals(test.dividedBy(Long.MAX_VALUE), Duration.ofSeconds(1)); 2393 } 2394 2395 //----------------------------------------------------------------------- 2396 // negated() 2397 //----------------------------------------------------------------------- 2398 @Test 2399 public void test_negated() { 2400 assertEquals(Duration.ofSeconds(0).negated(), Duration.ofSeconds(0)); 2401 assertEquals(Duration.ofSeconds(12).negated(), Duration.ofSeconds(-12)); 2402 assertEquals(Duration.ofSeconds(-12).negated(), Duration.ofSeconds(12)); 2403 assertEquals(Duration.ofSeconds(12, 20).negated(), Duration.ofSeconds(-12, -20)); 2404 assertEquals(Duration.ofSeconds(12, -20).negated(), Duration.ofSeconds(-12, 20)); 2405 assertEquals(Duration.ofSeconds(-12, -20).negated(), Duration.ofSeconds(12, 20)); 2406 assertEquals(Duration.ofSeconds(-12, 20).negated(), Duration.ofSeconds(12, -20)); 2407 assertEquals(Duration.ofSeconds(Long.MAX_VALUE).negated(), Duration.ofSeconds(-Long.MAX_VALUE)); 2408 } 2409 2410 @Test(expectedExceptions=ArithmeticException.class) 2411 public void test_negated_overflow() { 2412 Duration.ofSeconds(Long.MIN_VALUE).negated(); 2413 } 2414 2415 //----------------------------------------------------------------------- 2416 // abs() 2417 //----------------------------------------------------------------------- 2418 @Test 2419 public void test_abs() { 2420 assertEquals(Duration.ofSeconds(0).abs(), Duration.ofSeconds(0)); 2421 assertEquals(Duration.ofSeconds(12).abs(), Duration.ofSeconds(12)); 2422 assertEquals(Duration.ofSeconds(-12).abs(), Duration.ofSeconds(12)); 2423 assertEquals(Duration.ofSeconds(12, 20).abs(), Duration.ofSeconds(12, 20)); 2424 assertEquals(Duration.ofSeconds(12, -20).abs(), Duration.ofSeconds(12, -20)); 2425 assertEquals(Duration.ofSeconds(-12, -20).abs(), Duration.ofSeconds(12, 20)); 2426 assertEquals(Duration.ofSeconds(-12, 20).abs(), Duration.ofSeconds(12, -20)); 2427 assertEquals(Duration.ofSeconds(Long.MAX_VALUE).abs(), Duration.ofSeconds(Long.MAX_VALUE)); 2428 } 2429 2430 @Test(expectedExceptions=ArithmeticException.class) 2431 public void test_abs_overflow() { 2432 Duration.ofSeconds(Long.MIN_VALUE).abs(); 2433 } 2434 2435 //----------------------------------------------------------------------- 2436 // toNanos() 2437 //----------------------------------------------------------------------- 2438 @Test 2439 public void test_toNanos() { 2440 Duration test = Duration.ofSeconds(321, 123456789); 2441 assertEquals(test.toNanos(), 321123456789L); 2442 } 2443 2444 @Test 2445 public void test_toNanos_max() { 2446 Duration test = Duration.ofSeconds(0, Long.MAX_VALUE); 2447 assertEquals(test.toNanos(), Long.MAX_VALUE); 2448 } 2449 2450 @Test(expectedExceptions=ArithmeticException.class) 2451 public void test_toNanos_tooBig() { 2452 Duration test = Duration.ofSeconds(0, Long.MAX_VALUE).plusNanos(1); 2453 test.toNanos(); 2454 } 2455 2456 //----------------------------------------------------------------------- 2457 // toMillis() 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 } 2639 2640 void doTest_comparisons_Duration(Duration... durations) { 2641 for (int i = 0; i < durations.length; i++) { 2642 Duration a = durations[i]; 2643 for (int j = 0; j < durations.length; j++) { 2644 Duration b = durations[j]; 2645 if (i < j) { 2646 assertEquals(a.compareTo(b)< 0, true, a + " <=> " + b); 2647 assertEquals(a.equals(b), false, a + " <=> " + b); 2648 } else if (i > j) { 2649 assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b); 2650 assertEquals(a.equals(b), false, a + " <=> " + b); 2651 } else { 2652 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 2653 assertEquals(a.equals(b), true, a + " <=> " + b); 2654 } 2655 } 2656 } 2657 } 2658 2659 @Test(expectedExceptions=NullPointerException.class) 2660 public void test_compareTo_ObjectNull() { 2661 Duration a = Duration.ofSeconds(0L, 0); 2662 a.compareTo(null); 2663 } 2664 2665 @Test(expectedExceptions=ClassCastException.class) 2666 @SuppressWarnings({ "unchecked", "rawtypes" }) 2667 public void compareToNonDuration() { 2668 Comparable c = Duration.ofSeconds(0L); 2669 c.compareTo(new Object()); 2670 } 2671 2672 //----------------------------------------------------------------------- 2673 // equals() 2674 //----------------------------------------------------------------------- 2675 @Test 2676 public void test_equals() { 2677 Duration test5a = Duration.ofSeconds(5L, 20); 2678 Duration test5b = Duration.ofSeconds(5L, 20); 2679 Duration test5n = Duration.ofSeconds(5L, 30); 2680 Duration test6 = Duration.ofSeconds(6L, 20); 2681 2682 assertEquals(test5a.equals(test5a), true); 2683 assertEquals(test5a.equals(test5b), true); 2684 assertEquals(test5a.equals(test5n), false); 2685 assertEquals(test5a.equals(test6), false); 2686 2687 assertEquals(test5b.equals(test5a), true); 2688 assertEquals(test5b.equals(test5b), true); 2689 assertEquals(test5b.equals(test5n), false); 2690 assertEquals(test5b.equals(test6), false); 2691 2692 assertEquals(test5n.equals(test5a), false); 2693 assertEquals(test5n.equals(test5b), false); 2694 assertEquals(test5n.equals(test5n), true); 2695 assertEquals(test5n.equals(test6), false); 2696 2697 assertEquals(test6.equals(test5a), false); 2698 assertEquals(test6.equals(test5b), false); 2699 assertEquals(test6.equals(test5n), false); 2700 assertEquals(test6.equals(test6), true); 2701 } 2702 2703 @Test 2704 public void test_equals_null() { 2705 Duration test5 = Duration.ofSeconds(5L, 20); 2706 assertEquals(test5.equals(null), false); 2707 } 2708 2709 @Test 2710 public void test_equals_otherClass() { 2711 Duration test5 = Duration.ofSeconds(5L, 20); 2712 assertEquals(test5.equals(""), false); 2713 } 2714 2715 //----------------------------------------------------------------------- 2716 // hashCode() 2717 //----------------------------------------------------------------------- 2718 @Test 2719 public void test_hashCode() { 2720 Duration test5a = Duration.ofSeconds(5L, 20); 2721 Duration test5b = Duration.ofSeconds(5L, 20); 2722 Duration test5n = Duration.ofSeconds(5L, 30); 2723 Duration test6 = Duration.ofSeconds(6L, 20); 2724 2725 assertEquals(test5a.hashCode() == test5a.hashCode(), true); 2726 assertEquals(test5a.hashCode() == test5b.hashCode(), true); 2727 assertEquals(test5b.hashCode() == test5b.hashCode(), true); 2728 2729 assertEquals(test5a.hashCode() == test5n.hashCode(), false); 2730 assertEquals(test5a.hashCode() == test6.hashCode(), false); 2731 } 2732 2733 //----------------------------------------------------------------------- 2734 @DataProvider(name="withNanos") 2735 Object[][] provider_withNanos_int() { 2736 return new Object[][] { 2737 {0, 0, 0, 0, 0}, 2738 {0, 0, 1, 0, 1}, 2739 {0, 0, 999999999, 0, 999999999}, 2740 2741 {1, 0, 0, 1, 0}, 2742 {1, 0, 1, 1, 1}, 2743 {1, 0, 999999999, 1, 999999999}, 2744 2745 {-1, 0, 0, -1, 0}, 2746 {-1, 0, 1, -1, 1}, 2747 {-1, 0, 999999999, -1, 999999999}, 2748 2749 {1, 999999999, 0, 1, 0}, 2750 {1, 999999999, 1, 1, 1}, 2751 {1, 999999998, 2, 1, 2}, 2752 2753 {Long.MAX_VALUE, 0, 999999999, Long.MAX_VALUE, 999999999}, 2754 {Long.MIN_VALUE, 0, 999999999, Long.MIN_VALUE, 999999999}, 2755 }; 2756 } 2757 2758 @Test(dataProvider="withNanos") 2759 public void withNanos_long(long seconds, int nanos, int amount, long expectedSeconds, int expectedNanoOfSecond) { 2760 Duration t = Duration.ofSeconds(seconds, nanos); 2761 t = t.withNanos(amount); 2762 assertEquals(t.getSeconds(), expectedSeconds); 2763 assertEquals(t.getNano(), expectedNanoOfSecond); 2764 } 2765 2766 //----------------------------------------------------------------------- 2767 @DataProvider(name="withSeconds") 2768 Object[][] provider_withSeconds_long() { 2769 return new Object[][] { 2770 {0, 0, 0, 0, 0}, 2771 {0, 0, 1, 1, 0}, 2772 {0, 0, -1, -1, 0}, 2773 {0, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0}, 2774 {0, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0}, 2775 2776 {1, 0, 0, 0, 0}, 2777 {1, 0, 2, 2, 0}, 2778 {1, 0, -1, -1, 0}, 2779 {1, 0, Long.MAX_VALUE, Long.MAX_VALUE, 0}, 2780 {1, 0, Long.MIN_VALUE, Long.MIN_VALUE, 0}, 2781 2782 {-1, 1, 0, 0, 1}, 2783 {-1, 1, 1, 1, 1}, 2784 {-1, 1, -1, -1, 1}, 2785 {-1, 1, Long.MAX_VALUE, Long.MAX_VALUE, 1}, 2786 {-1, 1, Long.MIN_VALUE, Long.MIN_VALUE, 1}, 2787 }; 2788 } 2789 2790 @Test(dataProvider="withSeconds") 2791 public void withSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 2792 Duration t = Duration.ofSeconds(seconds, nanos); 2793 t = t.withSeconds(amount); 2794 assertEquals(t.getSeconds(), expectedSeconds); 2795 assertEquals(t.getNano(), expectedNanoOfSecond); 2796 } 2797 2798 //----------------------------------------------------------------------- 2799 // toString() 2800 //----------------------------------------------------------------------- 2801 @DataProvider(name="toString") 2802 Object[][] provider_toString() { 2803 return new Object[][] { 2804 {0, 0, "PT0S"}, 2805 {0, 1, "PT0.000000001S"}, 2806 {0, 10, "PT0.00000001S"}, 2807 {0, 100, "PT0.0000001S"}, 2808 {0, 1000, "PT0.000001S"}, 2809 {0, 10000, "PT0.00001S"}, 2810 {0, 100000, "PT0.0001S"}, 2811 {0, 1000000, "PT0.001S"}, 2812 {0, 10000000, "PT0.01S"}, 2813 {0, 100000000, "PT0.1S"}, 2814 {0, 120000000, "PT0.12S"}, 2815 {0, 123000000, "PT0.123S"}, 2816 {0, 123400000, "PT0.1234S"}, 2817 {0, 123450000, "PT0.12345S"}, 2818 {0, 123456000, "PT0.123456S"}, 2819 {0, 123456700, "PT0.1234567S"}, 2820 {0, 123456780, "PT0.12345678S"}, 2821 {0, 123456789, "PT0.123456789S"}, 2822 {1, 0, "PT1S"}, 2823 {59, 0, "PT59S"}, 2824 {60, 0, "PT1M"}, 2825 {61, 0, "PT1M1S"}, 2826 {3599, 0, "PT59M59S"}, 2827 {3600, 0, "PT1H"}, 2828 {3601, 0, "PT1H1S"}, 2829 {3661, 0, "PT1H1M1S"}, 2830 {86399, 0, "PT23H59M59S"}, 2831 {86400, 0, "PT24H"}, 2832 {59, 0, "PT59S"}, 2833 {59, 0, "PT59S"}, 2834 {-1, 0, "PT-1S"}, 2835 {-1, 1000, "PT-0.999999S"}, 2836 {-1, 900000000, "PT-0.1S"}, 2837 {Long.MAX_VALUE, 0, "PT" + (Long.MAX_VALUE / 3600) + "H" + 2838 ((Long.MAX_VALUE % 3600) / 60) + "M" + (Long.MAX_VALUE % 60) + "S"}, 2839 {Long.MIN_VALUE, 0, "PT" + (Long.MIN_VALUE / 3600) + "H" + 2840 ((Long.MIN_VALUE % 3600) / 60) + "M" + (Long.MIN_VALUE % 60) + "S"}, 2841 }; 2842 } 2843 2844 @Test(dataProvider="toString") 2845 public void test_toString(long seconds, int nanos, String expected) { 2846 Duration t = Duration.ofSeconds(seconds, nanos); 2847 assertEquals(t.toString(), expected); 2848 } 2849 2850 //----------------------------------------------------------------------- 2851 @Test(groups="{tck}") 2852 public void test_duration_getUnits() { 2853 Duration duration = Duration.ofSeconds(5000, 1000); 2854 List<TemporalUnit> units = duration.getUnits(); 2855 assertEquals(units.size(), 2, "Period.getUnits length"); 2856 assertTrue(units.contains(ChronoUnit.SECONDS), "Period.getUnits contains ChronoUnit.SECONDS"); 2857 assertTrue(units.contains(ChronoUnit.NANOS), "contains ChronoUnit.NANOS"); 2858 } 2859 2860 @Test() 2861 public void test_getUnit() { 2862 Duration test = Duration.ofSeconds(2000, 1000); 2863 long seconds = test.get(ChronoUnit.SECONDS); 2864 assertEquals(seconds, 2000, "duration.get(SECONDS)"); 2865 long nanos = test.get(ChronoUnit.NANOS); 2866 assertEquals(nanos, 1000, "duration.get(NANOS)"); 2867 } 2868 2869 @DataProvider(name="BadTemporalUnit") 2870 Object[][] provider_factory_of_badTemporalUnit() { 2871 return new Object[][] { 2872 {0, MICROS}, 2873 {0, MILLIS}, 2874 {0, MINUTES}, 2875 {0, HOURS}, 2876 {0, HALF_DAYS}, 2877 {0, DAYS}, 2878 {0, ChronoUnit.MONTHS}, 2879 {0, ChronoUnit.YEARS}, 2880 {0, ChronoUnit.DECADES}, 2881 {0, ChronoUnit.CENTURIES}, 2882 {0, ChronoUnit.MILLENNIA}, 2883 }; 2884 } 2885 2886 @Test(dataProvider="BadTemporalUnit", expectedExceptions=DateTimeException.class) 2887 public void test_bad_getUnit(long amount, TemporalUnit unit) { 2888 Duration t = Duration.of(amount, unit); 2889 t.get(unit); 2890 } 2891 }