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