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