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