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.ChronoField.INSTANT_SECONDS; 63 import static java.time.temporal.ChronoField.MICRO_OF_SECOND; 64 import static java.time.temporal.ChronoField.MILLI_OF_SECOND; 65 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 66 import static java.time.temporal.ChronoUnit.DAYS; 67 import static java.time.temporal.ChronoUnit.HOURS; 68 import static java.time.temporal.ChronoUnit.MICROS; 69 import static java.time.temporal.ChronoUnit.MILLIS; 70 import static java.time.temporal.ChronoUnit.MINUTES; 71 import static java.time.temporal.ChronoUnit.MONTHS; 72 import static java.time.temporal.ChronoUnit.NANOS; 73 import static java.time.temporal.ChronoUnit.SECONDS; 74 import static java.time.temporal.ChronoUnit.WEEKS; 75 import static java.time.temporal.ChronoUnit.YEARS; 76 import static org.testng.Assert.assertEquals; 77 import static org.testng.Assert.assertTrue; 78 79 import java.io.ByteArrayOutputStream; 80 import java.io.DataOutputStream; 81 import java.time.Clock; 82 import java.time.DateTimeException; 83 import java.time.Duration; 84 import java.time.Instant; 85 import java.time.LocalDateTime; 86 import java.time.OffsetDateTime; 87 import java.time.ZoneOffset; 88 import java.time.ZonedDateTime; 89 import java.time.format.DateTimeParseException; 90 import java.time.temporal.ChronoField; 91 import java.time.temporal.JulianFields; 92 import java.time.temporal.Queries; 93 import java.time.temporal.Temporal; 94 import java.time.temporal.TemporalAccessor; 95 import java.time.temporal.TemporalAmount; 96 import java.time.temporal.TemporalField; 97 import java.time.temporal.TemporalQuery; 98 import java.time.temporal.TemporalUnit; 99 import java.util.ArrayList; 100 import java.util.Arrays; 101 import java.util.List; 102 import java.util.Locale; 103 104 import org.testng.annotations.BeforeMethod; 105 import org.testng.annotations.DataProvider; 106 import org.testng.annotations.Test; 107 108 /** 109 * Test Instant. 110 */ 111 @Test 112 public class TCKInstant extends AbstractDateTimeTest { 113 114 private static final long MIN_SECOND = Instant.MIN.getEpochSecond(); 115 private static final long MAX_SECOND = Instant.MAX.getEpochSecond(); 116 117 private Instant TEST_12345_123456789; 118 119 @BeforeMethod 120 public void setUp() { 121 TEST_12345_123456789 = Instant.ofEpochSecond(12345, 123456789); 122 } 123 124 //----------------------------------------------------------------------- 125 @Override 126 protected List<TemporalAccessor> samples() { 127 TemporalAccessor[] array = {TEST_12345_123456789, Instant.MIN, Instant.MAX, Instant.EPOCH}; 128 return Arrays.asList(array); 129 } 130 131 @Override 132 protected List<TemporalField> validFields() { 133 TemporalField[] array = { 134 NANO_OF_SECOND, 135 MICRO_OF_SECOND, 136 MILLI_OF_SECOND, 137 INSTANT_SECONDS, 138 }; 139 return Arrays.asList(array); 140 } 141 142 @Override 143 protected List<TemporalField> invalidFields() { 144 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 145 list.removeAll(validFields()); 146 list.add(JulianFields.JULIAN_DAY); 147 list.add(JulianFields.MODIFIED_JULIAN_DAY); 148 list.add(JulianFields.RATA_DIE); 149 return list; 150 } 151 152 //----------------------------------------------------------------------- 153 @Test 154 public void test_serialization() throws Exception { 155 assertSerializable(Instant.ofEpochMilli(134l)); 156 } 157 158 @Test 159 public void test_serialization_format() throws Exception { 160 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 161 try (DataOutputStream dos = new DataOutputStream(baos) ) { 162 dos.writeByte(2); 163 dos.writeLong(654321); 164 dos.writeInt(123456789); 165 } 166 byte[] bytes = baos.toByteArray(); 167 assertSerializedBySer(Instant.ofEpochSecond(654321, 123456789), bytes); 168 } 169 170 //----------------------------------------------------------------------- 171 private void check(Instant instant, long epochSecs, int nos) { 172 assertEquals(instant.getEpochSecond(), epochSecs); 173 assertEquals(instant.getNano(), nos); 174 assertEquals(instant, instant); 175 assertEquals(instant.hashCode(), instant.hashCode()); 176 } 177 178 //----------------------------------------------------------------------- 179 @Test 180 public void constant_EPOCH() { 181 check(Instant.EPOCH, 0, 0); 182 } 183 184 @Test 185 public void constant_MIN() { 186 check(Instant.MIN, -31557014167219200L, 0); 187 } 188 189 @Test 190 public void constant_MAX() { 191 check(Instant.MAX, 31556889864403199L, 999_999_999); 192 } 193 194 //----------------------------------------------------------------------- 195 // now() 196 //----------------------------------------------------------------------- 197 @Test 198 public void now() { 199 Instant expected = Instant.now(Clock.systemUTC()); 200 Instant test = Instant.now(); 201 long diff = Math.abs(test.toEpochMilli() - expected.toEpochMilli()); 202 assertTrue(diff < 100); // less than 0.1 secs 203 } 204 205 //----------------------------------------------------------------------- 206 // now(Clock) 207 //----------------------------------------------------------------------- 208 @Test(expectedExceptions=NullPointerException.class) 209 public void now_Clock_nullClock() { 210 Instant.now(null); 211 } 212 213 @Test 214 public void now_Clock_allSecsInDay_utc() { 215 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 216 Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L); 217 Clock clock = Clock.fixed(expected, ZoneOffset.UTC); 218 Instant test = Instant.now(clock); 219 assertEquals(test, expected); 220 } 221 } 222 223 @Test 224 public void now_Clock_allSecsInDay_beforeEpoch() { 225 for (int i =-1; i >= -(24 * 60 * 60); i--) { 226 Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L); 227 Clock clock = Clock.fixed(expected, ZoneOffset.UTC); 228 Instant test = Instant.now(clock); 229 assertEquals(test, expected); 230 } 231 } 232 233 //----------------------------------------------------------------------- 234 // ofEpochSecond(long) 235 //----------------------------------------------------------------------- 236 @Test 237 public void factory_seconds_long() { 238 for (long i = -2; i <= 2; i++) { 239 Instant t = Instant.ofEpochSecond(i); 240 assertEquals(t.getEpochSecond(), i); 241 assertEquals(t.getNano(), 0); 242 } 243 } 244 245 //----------------------------------------------------------------------- 246 // ofEpochSecond(long,long) 247 //----------------------------------------------------------------------- 248 @Test 249 public void factory_seconds_long_long() { 250 for (long i = -2; i <= 2; i++) { 251 for (int j = 0; j < 10; j++) { 252 Instant t = Instant.ofEpochSecond(i, j); 253 assertEquals(t.getEpochSecond(), i); 254 assertEquals(t.getNano(), j); 255 } 256 for (int j = -10; j < 0; j++) { 257 Instant t = Instant.ofEpochSecond(i, j); 258 assertEquals(t.getEpochSecond(), i - 1); 259 assertEquals(t.getNano(), j + 1000000000); 260 } 261 for (int j = 999999990; j < 1000000000; j++) { 262 Instant t = Instant.ofEpochSecond(i, j); 263 assertEquals(t.getEpochSecond(), i); 264 assertEquals(t.getNano(), j); 265 } 266 } 267 } 268 269 @Test 270 public void factory_seconds_long_long_nanosNegativeAdjusted() { 271 Instant test = Instant.ofEpochSecond(2L, -1); 272 assertEquals(test.getEpochSecond(), 1); 273 assertEquals(test.getNano(), 999999999); 274 } 275 276 @Test(expectedExceptions=DateTimeException.class) 277 public void factory_seconds_long_long_tooBig() { 278 Instant.ofEpochSecond(MAX_SECOND, 1000000000); 279 } 280 281 @Test(expectedExceptions=ArithmeticException.class) 282 public void factory_seconds_long_long_tooBigBig() { 283 Instant.ofEpochSecond(Long.MAX_VALUE, Long.MAX_VALUE); 284 } 285 286 //----------------------------------------------------------------------- 287 // ofEpochMilli(long) 288 //----------------------------------------------------------------------- 289 @DataProvider(name="MillisInstantNoNanos") 290 Object[][] provider_factory_millis_long() { 291 return new Object[][] { 292 {0, 0, 0}, 293 {1, 0, 1000000}, 294 {2, 0, 2000000}, 295 {999, 0, 999000000}, 296 {1000, 1, 0}, 297 {1001, 1, 1000000}, 298 {-1, -1, 999000000}, 299 {-2, -1, 998000000}, 300 {-999, -1, 1000000}, 301 {-1000, -1, 0}, 302 {-1001, -2, 999000000}, 303 }; 304 } 305 306 @Test(dataProvider="MillisInstantNoNanos") 307 public void factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond) { 308 Instant t = Instant.ofEpochMilli(millis); 309 assertEquals(t.getEpochSecond(), expectedSeconds); 310 assertEquals(t.getNano(), expectedNanoOfSecond); 311 } 312 313 //----------------------------------------------------------------------- 314 // parse(String) 315 //----------------------------------------------------------------------- 316 // see also parse tests under toString() 317 @DataProvider(name="Parse") 318 Object[][] provider_factory_parse() { 319 return new Object[][] { 320 {"1970-01-01T00:00:00Z", 0, 0}, 321 {"1970-01-01t00:00:00Z", 0, 0}, 322 {"1970-01-01T00:00:00z", 0, 0}, 323 {"1970-01-01T00:00:00.0Z", 0, 0}, 324 {"1970-01-01T00:00:00.000000000Z", 0, 0}, 325 326 {"1970-01-01T00:00:00.000000001Z", 0, 1}, 327 {"1970-01-01T00:00:00.100000000Z", 0, 100000000}, 328 {"1970-01-01T00:00:01Z", 1, 0}, 329 {"1970-01-01T00:01:00Z", 60, 0}, 330 {"1970-01-01T00:01:01Z", 61, 0}, 331 {"1970-01-01T00:01:01.000000001Z", 61, 1}, 332 {"1970-01-01T01:00:00.000000000Z", 3600, 0}, 333 {"1970-01-01T01:01:01.000000001Z", 3661, 1}, 334 {"1970-01-02T01:01:01.100000000Z", 90061, 100000000}, 335 }; 336 } 337 338 @Test(dataProvider="Parse") 339 public void factory_parse(String text, long expectedEpochSeconds, int expectedNanoOfSecond) { 340 Instant t = Instant.parse(text); 341 assertEquals(t.getEpochSecond(), expectedEpochSeconds); 342 assertEquals(t.getNano(), expectedNanoOfSecond); 343 } 344 345 @Test(dataProvider="Parse") 346 public void factory_parseLowercase(String text, long expectedEpochSeconds, int expectedNanoOfSecond) { 347 Instant t = Instant.parse(text.toLowerCase(Locale.ENGLISH)); 348 assertEquals(t.getEpochSecond(), expectedEpochSeconds); 349 assertEquals(t.getNano(), expectedNanoOfSecond); 350 } 351 352 // TODO: should comma be accepted? 353 // @Test(dataProvider="Parse") 354 // public void factory_parse_comma(String text, long expectedEpochSeconds, int expectedNanoOfSecond) { 355 // text = text.replace('.', ','); 356 // Instant t = Instant.parse(text); 357 // assertEquals(t.getEpochSecond(), expectedEpochSeconds); 358 // assertEquals(t.getNano(), expectedNanoOfSecond); 359 // } 360 361 @DataProvider(name="ParseFailures") 362 Object[][] provider_factory_parseFailures() { 363 return new Object[][] { 364 {""}, 365 {"Z"}, 366 {"1970-01-01T00:00:00"}, 367 {"1970-01-01T00:00:0Z"}, 368 {"1970-01-01T00:00:00.0000000000Z"}, 369 }; 370 } 371 372 @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class) 373 public void factory_parseFailures(String text) { 374 Instant.parse(text); 375 } 376 377 @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class) 378 public void factory_parseFailures_comma(String text) { 379 text = text.replace('.', ','); 380 Instant.parse(text); 381 } 382 383 @Test(expectedExceptions=NullPointerException.class) 384 public void factory_parse_nullText() { 385 Instant.parse(null); 386 } 387 388 //----------------------------------------------------------------------- 389 // get(TemporalField) 390 //----------------------------------------------------------------------- 391 @Test 392 public void test_get_TemporalField() { 393 Instant test = TEST_12345_123456789; 394 assertEquals(test.get(ChronoField.NANO_OF_SECOND), 123456789); 395 assertEquals(test.get(ChronoField.MICRO_OF_SECOND), 123456); 396 assertEquals(test.get(ChronoField.MILLI_OF_SECOND), 123); 397 } 398 399 @Test 400 public void test_getLong_TemporalField() { 401 Instant test = TEST_12345_123456789; 402 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 123456789); 403 assertEquals(test.getLong(ChronoField.MICRO_OF_SECOND), 123456); 404 assertEquals(test.getLong(ChronoField.MILLI_OF_SECOND), 123); 405 assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), 12345); 406 } 407 408 //----------------------------------------------------------------------- 409 // query(TemporalQuery) 410 //----------------------------------------------------------------------- 411 @DataProvider(name="query") 412 Object[][] data_query() { 413 return new Object[][] { 414 {TEST_12345_123456789, Queries.chronology(), null}, 415 {TEST_12345_123456789, Queries.zoneId(), null}, 416 {TEST_12345_123456789, Queries.precision(), NANOS}, 417 {TEST_12345_123456789, Queries.zone(), null}, 418 {TEST_12345_123456789, Queries.offset(), null}, 419 {TEST_12345_123456789, Queries.localDate(), null}, 420 {TEST_12345_123456789, Queries.localTime(), null}, 421 }; 422 } 423 424 @Test(dataProvider="query") 425 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 426 assertEquals(temporal.query(query), expected); 427 } 428 429 @Test(dataProvider="query") 430 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 431 assertEquals(query.queryFrom(temporal), expected); 432 } 433 434 @Test(expectedExceptions=NullPointerException.class) 435 public void test_query_null() { 436 TEST_12345_123456789.query(null); 437 } 438 439 //----------------------------------------------------------------------- 440 // truncated(TemporalUnit) 441 //----------------------------------------------------------------------- 442 TemporalUnit NINETY_MINS = new TemporalUnit() { 443 @Override 444 public String getName() { 445 return "NinetyMins"; 446 } 447 @Override 448 public Duration getDuration() { 449 return Duration.ofMinutes(90); 450 } 451 @Override 452 public boolean isDurationEstimated() { 453 return false; 454 } 455 @Override 456 public boolean isSupportedBy(Temporal temporal) { 457 return false; 458 } 459 @Override 460 public <R extends Temporal> R addTo(R temporal, long amount) { 461 throw new UnsupportedOperationException(); 462 } 463 @Override 464 public long between(Temporal temporal1, Temporal temporal2) { 465 throw new UnsupportedOperationException(); 466 } 467 }; 468 469 TemporalUnit NINETY_FIVE_MINS = new TemporalUnit() { 470 @Override 471 public String getName() { 472 return "NinetyFiveMins"; 473 } 474 @Override 475 public Duration getDuration() { 476 return Duration.ofMinutes(95); 477 } 478 @Override 479 public boolean isDurationEstimated() { 480 return false; 481 } 482 @Override 483 public boolean isSupportedBy(Temporal temporal) { 484 return false; 485 } 486 @Override 487 public <R extends Temporal> R addTo(R temporal, long amount) { 488 throw new UnsupportedOperationException(); 489 } 490 @Override 491 public long between(Temporal temporal1, Temporal temporal2) { 492 throw new UnsupportedOperationException(); 493 } 494 }; 495 496 @DataProvider(name="truncatedToValid") 497 Object[][] data_truncatedToValid() { 498 return new Object[][] { 499 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), NANOS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789)}, 500 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), MICROS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_000)}, 501 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), MILLIS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 1230_00_000)}, 502 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), SECONDS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 0)}, 503 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), MINUTES, Instant.ofEpochSecond(86400 + 3600 + 60, 0)}, 504 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), HOURS, Instant.ofEpochSecond(86400 + 3600, 0)}, 505 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), DAYS, Instant.ofEpochSecond(86400, 0)}, 506 507 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 0, 0)}, 508 {Instant.ofEpochSecond(86400 + 7200 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 5400, 0)}, 509 {Instant.ofEpochSecond(86400 + 10800 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 10800, 0)}, 510 }; 511 } 512 @Test(groups={"tck"}, dataProvider="truncatedToValid") 513 public void test_truncatedTo_valid(Instant input, TemporalUnit unit, Instant expected) { 514 assertEquals(input.truncatedTo(unit), expected); 515 } 516 517 @DataProvider(name="truncatedToInvalid") 518 Object[][] data_truncatedToInvalid() { 519 return new Object[][] { 520 {Instant.ofEpochSecond(1, 123_456_789), NINETY_FIVE_MINS}, 521 {Instant.ofEpochSecond(1, 123_456_789), WEEKS}, 522 {Instant.ofEpochSecond(1, 123_456_789), MONTHS}, 523 {Instant.ofEpochSecond(1, 123_456_789), YEARS}, 524 }; 525 } 526 527 @Test(groups={"tck"}, dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class) 528 public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) { 529 input.truncatedTo(unit); 530 } 531 532 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 533 public void test_truncatedTo_null() { 534 TEST_12345_123456789.truncatedTo(null); 535 } 536 537 //----------------------------------------------------------------------- 538 // plus(TemporalAmount) 539 //----------------------------------------------------------------------- 540 @DataProvider(name="plusTemporalAmount") 541 Object[][] data_plusTemporalAmount() { 542 return new Object[][] { 543 {DAYS, MockSimplePeriod.of(1, DAYS), 86401, 0}, 544 {HOURS, MockSimplePeriod.of(2, HOURS), 7201, 0}, 545 {MINUTES, MockSimplePeriod.of(4, MINUTES), 241, 0}, 546 {SECONDS, MockSimplePeriod.of(5, SECONDS), 6, 0}, 547 {NANOS, MockSimplePeriod.of(6, NANOS), 1, 6}, 548 {DAYS, MockSimplePeriod.of(10, DAYS), 864001, 0}, 549 {HOURS, MockSimplePeriod.of(11, HOURS), 39601, 0}, 550 {MINUTES, MockSimplePeriod.of(12, MINUTES), 721, 0}, 551 {SECONDS, MockSimplePeriod.of(13, SECONDS), 14, 0}, 552 {NANOS, MockSimplePeriod.of(14, NANOS), 1, 14}, 553 {SECONDS, Duration.ofSeconds(20, 40), 21, 40}, 554 {NANOS, Duration.ofSeconds(30, 300), 31, 300}, 555 }; 556 } 557 558 @Test(dataProvider="plusTemporalAmount") 559 public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) { 560 Instant inst = Instant.ofEpochMilli(1000); 561 Instant actual = inst.plus(amount); 562 Instant expected = Instant.ofEpochSecond(seconds, nanos); 563 assertEquals(actual, expected, "plus(TemporalAmount) failed"); 564 } 565 566 @DataProvider(name="badTemporalAmount") 567 Object[][] data_badPlusTemporalAmount() { 568 return new Object[][] { 569 {MockSimplePeriod.of(2, YEARS)}, 570 {MockSimplePeriod.of(2, MONTHS)}, 571 }; 572 } 573 574 @Test(dataProvider="badTemporalAmount",expectedExceptions=DateTimeException.class) 575 public void test_badPlusTemporalAmount(TemporalAmount amount) { 576 Instant inst = Instant.ofEpochMilli(1000); 577 inst.plus(amount); 578 } 579 580 //----------------------------------------------------------------------- 581 @DataProvider(name="Plus") 582 Object[][] provider_plus() { 583 return new Object[][] { 584 {MIN_SECOND, 0, -MIN_SECOND, 0, 0, 0}, 585 586 {MIN_SECOND, 0, 1, 0, MIN_SECOND + 1, 0}, 587 {MIN_SECOND, 0, 0, 500, MIN_SECOND, 500}, 588 {MIN_SECOND, 0, 0, 1000000000, MIN_SECOND + 1, 0}, 589 590 {MIN_SECOND + 1, 0, -1, 0, MIN_SECOND, 0}, 591 {MIN_SECOND + 1, 0, 0, -500, MIN_SECOND, 999999500}, 592 {MIN_SECOND + 1, 0, 0, -1000000000, MIN_SECOND, 0}, 593 594 {-4, 666666667, -4, 666666667, -7, 333333334}, 595 {-4, 666666667, -3, 0, -7, 666666667}, 596 {-4, 666666667, -2, 0, -6, 666666667}, 597 {-4, 666666667, -1, 0, -5, 666666667}, 598 {-4, 666666667, -1, 333333334, -4, 1}, 599 {-4, 666666667, -1, 666666667, -4, 333333334}, 600 {-4, 666666667, -1, 999999999, -4, 666666666}, 601 {-4, 666666667, 0, 0, -4, 666666667}, 602 {-4, 666666667, 0, 1, -4, 666666668}, 603 {-4, 666666667, 0, 333333333, -3, 0}, 604 {-4, 666666667, 0, 666666666, -3, 333333333}, 605 {-4, 666666667, 1, 0, -3, 666666667}, 606 {-4, 666666667, 2, 0, -2, 666666667}, 607 {-4, 666666667, 3, 0, -1, 666666667}, 608 {-4, 666666667, 3, 333333333, 0, 0}, 609 610 {-3, 0, -4, 666666667, -7, 666666667}, 611 {-3, 0, -3, 0, -6, 0}, 612 {-3, 0, -2, 0, -5, 0}, 613 {-3, 0, -1, 0, -4, 0}, 614 {-3, 0, -1, 333333334, -4, 333333334}, 615 {-3, 0, -1, 666666667, -4, 666666667}, 616 {-3, 0, -1, 999999999, -4, 999999999}, 617 {-3, 0, 0, 0, -3, 0}, 618 {-3, 0, 0, 1, -3, 1}, 619 {-3, 0, 0, 333333333, -3, 333333333}, 620 {-3, 0, 0, 666666666, -3, 666666666}, 621 {-3, 0, 1, 0, -2, 0}, 622 {-3, 0, 2, 0, -1, 0}, 623 {-3, 0, 3, 0, 0, 0}, 624 {-3, 0, 3, 333333333, 0, 333333333}, 625 626 {-2, 0, -4, 666666667, -6, 666666667}, 627 {-2, 0, -3, 0, -5, 0}, 628 {-2, 0, -2, 0, -4, 0}, 629 {-2, 0, -1, 0, -3, 0}, 630 {-2, 0, -1, 333333334, -3, 333333334}, 631 {-2, 0, -1, 666666667, -3, 666666667}, 632 {-2, 0, -1, 999999999, -3, 999999999}, 633 {-2, 0, 0, 0, -2, 0}, 634 {-2, 0, 0, 1, -2, 1}, 635 {-2, 0, 0, 333333333, -2, 333333333}, 636 {-2, 0, 0, 666666666, -2, 666666666}, 637 {-2, 0, 1, 0, -1, 0}, 638 {-2, 0, 2, 0, 0, 0}, 639 {-2, 0, 3, 0, 1, 0}, 640 {-2, 0, 3, 333333333, 1, 333333333}, 641 642 {-1, 0, -4, 666666667, -5, 666666667}, 643 {-1, 0, -3, 0, -4, 0}, 644 {-1, 0, -2, 0, -3, 0}, 645 {-1, 0, -1, 0, -2, 0}, 646 {-1, 0, -1, 333333334, -2, 333333334}, 647 {-1, 0, -1, 666666667, -2, 666666667}, 648 {-1, 0, -1, 999999999, -2, 999999999}, 649 {-1, 0, 0, 0, -1, 0}, 650 {-1, 0, 0, 1, -1, 1}, 651 {-1, 0, 0, 333333333, -1, 333333333}, 652 {-1, 0, 0, 666666666, -1, 666666666}, 653 {-1, 0, 1, 0, 0, 0}, 654 {-1, 0, 2, 0, 1, 0}, 655 {-1, 0, 3, 0, 2, 0}, 656 {-1, 0, 3, 333333333, 2, 333333333}, 657 658 {-1, 666666667, -4, 666666667, -4, 333333334}, 659 {-1, 666666667, -3, 0, -4, 666666667}, 660 {-1, 666666667, -2, 0, -3, 666666667}, 661 {-1, 666666667, -1, 0, -2, 666666667}, 662 {-1, 666666667, -1, 333333334, -1, 1}, 663 {-1, 666666667, -1, 666666667, -1, 333333334}, 664 {-1, 666666667, -1, 999999999, -1, 666666666}, 665 {-1, 666666667, 0, 0, -1, 666666667}, 666 {-1, 666666667, 0, 1, -1, 666666668}, 667 {-1, 666666667, 0, 333333333, 0, 0}, 668 {-1, 666666667, 0, 666666666, 0, 333333333}, 669 {-1, 666666667, 1, 0, 0, 666666667}, 670 {-1, 666666667, 2, 0, 1, 666666667}, 671 {-1, 666666667, 3, 0, 2, 666666667}, 672 {-1, 666666667, 3, 333333333, 3, 0}, 673 674 {0, 0, -4, 666666667, -4, 666666667}, 675 {0, 0, -3, 0, -3, 0}, 676 {0, 0, -2, 0, -2, 0}, 677 {0, 0, -1, 0, -1, 0}, 678 {0, 0, -1, 333333334, -1, 333333334}, 679 {0, 0, -1, 666666667, -1, 666666667}, 680 {0, 0, -1, 999999999, -1, 999999999}, 681 {0, 0, 0, 0, 0, 0}, 682 {0, 0, 0, 1, 0, 1}, 683 {0, 0, 0, 333333333, 0, 333333333}, 684 {0, 0, 0, 666666666, 0, 666666666}, 685 {0, 0, 1, 0, 1, 0}, 686 {0, 0, 2, 0, 2, 0}, 687 {0, 0, 3, 0, 3, 0}, 688 {0, 0, 3, 333333333, 3, 333333333}, 689 690 {0, 333333333, -4, 666666667, -3, 0}, 691 {0, 333333333, -3, 0, -3, 333333333}, 692 {0, 333333333, -2, 0, -2, 333333333}, 693 {0, 333333333, -1, 0, -1, 333333333}, 694 {0, 333333333, -1, 333333334, -1, 666666667}, 695 {0, 333333333, -1, 666666667, 0, 0}, 696 {0, 333333333, -1, 999999999, 0, 333333332}, 697 {0, 333333333, 0, 0, 0, 333333333}, 698 {0, 333333333, 0, 1, 0, 333333334}, 699 {0, 333333333, 0, 333333333, 0, 666666666}, 700 {0, 333333333, 0, 666666666, 0, 999999999}, 701 {0, 333333333, 1, 0, 1, 333333333}, 702 {0, 333333333, 2, 0, 2, 333333333}, 703 {0, 333333333, 3, 0, 3, 333333333}, 704 {0, 333333333, 3, 333333333, 3, 666666666}, 705 706 {1, 0, -4, 666666667, -3, 666666667}, 707 {1, 0, -3, 0, -2, 0}, 708 {1, 0, -2, 0, -1, 0}, 709 {1, 0, -1, 0, 0, 0}, 710 {1, 0, -1, 333333334, 0, 333333334}, 711 {1, 0, -1, 666666667, 0, 666666667}, 712 {1, 0, -1, 999999999, 0, 999999999}, 713 {1, 0, 0, 0, 1, 0}, 714 {1, 0, 0, 1, 1, 1}, 715 {1, 0, 0, 333333333, 1, 333333333}, 716 {1, 0, 0, 666666666, 1, 666666666}, 717 {1, 0, 1, 0, 2, 0}, 718 {1, 0, 2, 0, 3, 0}, 719 {1, 0, 3, 0, 4, 0}, 720 {1, 0, 3, 333333333, 4, 333333333}, 721 722 {2, 0, -4, 666666667, -2, 666666667}, 723 {2, 0, -3, 0, -1, 0}, 724 {2, 0, -2, 0, 0, 0}, 725 {2, 0, -1, 0, 1, 0}, 726 {2, 0, -1, 333333334, 1, 333333334}, 727 {2, 0, -1, 666666667, 1, 666666667}, 728 {2, 0, -1, 999999999, 1, 999999999}, 729 {2, 0, 0, 0, 2, 0}, 730 {2, 0, 0, 1, 2, 1}, 731 {2, 0, 0, 333333333, 2, 333333333}, 732 {2, 0, 0, 666666666, 2, 666666666}, 733 {2, 0, 1, 0, 3, 0}, 734 {2, 0, 2, 0, 4, 0}, 735 {2, 0, 3, 0, 5, 0}, 736 {2, 0, 3, 333333333, 5, 333333333}, 737 738 {3, 0, -4, 666666667, -1, 666666667}, 739 {3, 0, -3, 0, 0, 0}, 740 {3, 0, -2, 0, 1, 0}, 741 {3, 0, -1, 0, 2, 0}, 742 {3, 0, -1, 333333334, 2, 333333334}, 743 {3, 0, -1, 666666667, 2, 666666667}, 744 {3, 0, -1, 999999999, 2, 999999999}, 745 {3, 0, 0, 0, 3, 0}, 746 {3, 0, 0, 1, 3, 1}, 747 {3, 0, 0, 333333333, 3, 333333333}, 748 {3, 0, 0, 666666666, 3, 666666666}, 749 {3, 0, 1, 0, 4, 0}, 750 {3, 0, 2, 0, 5, 0}, 751 {3, 0, 3, 0, 6, 0}, 752 {3, 0, 3, 333333333, 6, 333333333}, 753 754 {3, 333333333, -4, 666666667, 0, 0}, 755 {3, 333333333, -3, 0, 0, 333333333}, 756 {3, 333333333, -2, 0, 1, 333333333}, 757 {3, 333333333, -1, 0, 2, 333333333}, 758 {3, 333333333, -1, 333333334, 2, 666666667}, 759 {3, 333333333, -1, 666666667, 3, 0}, 760 {3, 333333333, -1, 999999999, 3, 333333332}, 761 {3, 333333333, 0, 0, 3, 333333333}, 762 {3, 333333333, 0, 1, 3, 333333334}, 763 {3, 333333333, 0, 333333333, 3, 666666666}, 764 {3, 333333333, 0, 666666666, 3, 999999999}, 765 {3, 333333333, 1, 0, 4, 333333333}, 766 {3, 333333333, 2, 0, 5, 333333333}, 767 {3, 333333333, 3, 0, 6, 333333333}, 768 {3, 333333333, 3, 333333333, 6, 666666666}, 769 770 {MAX_SECOND - 1, 0, 1, 0, MAX_SECOND, 0}, 771 {MAX_SECOND - 1, 0, 0, 500, MAX_SECOND - 1, 500}, 772 {MAX_SECOND - 1, 0, 0, 1000000000, MAX_SECOND, 0}, 773 774 {MAX_SECOND, 0, -1, 0, MAX_SECOND - 1, 0}, 775 {MAX_SECOND, 0, 0, -500, MAX_SECOND - 1, 999999500}, 776 {MAX_SECOND, 0, 0, -1000000000, MAX_SECOND - 1, 0}, 777 778 {MAX_SECOND, 0, -MAX_SECOND, 0, 0, 0}, 779 }; 780 } 781 782 @Test(dataProvider="Plus") 783 public void plus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 784 Instant i = Instant.ofEpochSecond(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos)); 785 assertEquals(i.getEpochSecond(), expectedSeconds); 786 assertEquals(i.getNano(), expectedNanoOfSecond); 787 } 788 789 @Test(expectedExceptions=DateTimeException.class) 790 public void plus_Duration_overflowTooBig() { 791 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 792 i.plus(Duration.ofSeconds(0, 1)); 793 } 794 795 @Test(expectedExceptions=DateTimeException.class) 796 public void plus_Duration_overflowTooSmall() { 797 Instant i = Instant.ofEpochSecond(MIN_SECOND); 798 i.plus(Duration.ofSeconds(-1, 999999999)); 799 } 800 801 //-----------------------------------------------------------------------a 802 @Test(dataProvider="Plus") 803 public void plus_longTemporalUnit(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 804 Instant i = Instant.ofEpochSecond(seconds, nanos).plus(otherSeconds, SECONDS).plus(otherNanos, NANOS); 805 assertEquals(i.getEpochSecond(), expectedSeconds); 806 assertEquals(i.getNano(), expectedNanoOfSecond); 807 } 808 809 @Test(expectedExceptions=DateTimeException.class) 810 public void plus_longTemporalUnit_overflowTooBig() { 811 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 812 i.plus(1, NANOS); 813 } 814 815 @Test(expectedExceptions=DateTimeException.class) 816 public void plus_longTemporalUnit_overflowTooSmall() { 817 Instant i = Instant.ofEpochSecond(MIN_SECOND); 818 i.plus(999999999, NANOS); 819 i.plus(-1, SECONDS); 820 } 821 822 //----------------------------------------------------------------------- 823 @DataProvider(name="PlusSeconds") 824 Object[][] provider_plusSeconds_long() { 825 return new Object[][] { 826 {0, 0, 0, 0, 0}, 827 {0, 0, 1, 1, 0}, 828 {0, 0, -1, -1, 0}, 829 {0, 0, MAX_SECOND, MAX_SECOND, 0}, 830 {0, 0, MIN_SECOND, MIN_SECOND, 0}, 831 {1, 0, 0, 1, 0}, 832 {1, 0, 1, 2, 0}, 833 {1, 0, -1, 0, 0}, 834 {1, 0, MAX_SECOND - 1, MAX_SECOND, 0}, 835 {1, 0, MIN_SECOND, MIN_SECOND + 1, 0}, 836 {1, 1, 0, 1, 1}, 837 {1, 1, 1, 2, 1}, 838 {1, 1, -1, 0, 1}, 839 {1, 1, MAX_SECOND - 1, MAX_SECOND, 1}, 840 {1, 1, MIN_SECOND, MIN_SECOND + 1, 1}, 841 {-1, 1, 0, -1, 1}, 842 {-1, 1, 1, 0, 1}, 843 {-1, 1, -1, -2, 1}, 844 {-1, 1, MAX_SECOND, MAX_SECOND - 1, 1}, 845 {-1, 1, MIN_SECOND + 1, MIN_SECOND, 1}, 846 847 {MAX_SECOND, 2, -MAX_SECOND, 0, 2}, 848 {MIN_SECOND, 2, -MIN_SECOND, 0, 2}, 849 }; 850 } 851 852 @Test(dataProvider="PlusSeconds") 853 public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 854 Instant t = Instant.ofEpochSecond(seconds, nanos); 855 t = t.plusSeconds(amount); 856 assertEquals(t.getEpochSecond(), expectedSeconds); 857 assertEquals(t.getNano(), expectedNanoOfSecond); 858 } 859 860 @Test(expectedExceptions=ArithmeticException.class) 861 public void plusSeconds_long_overflowTooBig() { 862 Instant t = Instant.ofEpochSecond(1, 0); 863 t.plusSeconds(Long.MAX_VALUE); 864 } 865 866 @Test(expectedExceptions=ArithmeticException.class) 867 public void plusSeconds_long_overflowTooSmall() { 868 Instant t = Instant.ofEpochSecond(-1, 0); 869 t.plusSeconds(Long.MIN_VALUE); 870 } 871 872 //----------------------------------------------------------------------- 873 @DataProvider(name="PlusMillis") 874 Object[][] provider_plusMillis_long() { 875 return new Object[][] { 876 {0, 0, 0, 0, 0}, 877 {0, 0, 1, 0, 1000000}, 878 {0, 0, 999, 0, 999000000}, 879 {0, 0, 1000, 1, 0}, 880 {0, 0, 1001, 1, 1000000}, 881 {0, 0, 1999, 1, 999000000}, 882 {0, 0, 2000, 2, 0}, 883 {0, 0, -1, -1, 999000000}, 884 {0, 0, -999, -1, 1000000}, 885 {0, 0, -1000, -1, 0}, 886 {0, 0, -1001, -2, 999000000}, 887 {0, 0, -1999, -2, 1000000}, 888 889 {0, 1, 0, 0, 1}, 890 {0, 1, 1, 0, 1000001}, 891 {0, 1, 998, 0, 998000001}, 892 {0, 1, 999, 0, 999000001}, 893 {0, 1, 1000, 1, 1}, 894 {0, 1, 1998, 1, 998000001}, 895 {0, 1, 1999, 1, 999000001}, 896 {0, 1, 2000, 2, 1}, 897 {0, 1, -1, -1, 999000001}, 898 {0, 1, -2, -1, 998000001}, 899 {0, 1, -1000, -1, 1}, 900 {0, 1, -1001, -2, 999000001}, 901 902 {0, 1000000, 0, 0, 1000000}, 903 {0, 1000000, 1, 0, 2000000}, 904 {0, 1000000, 998, 0, 999000000}, 905 {0, 1000000, 999, 1, 0}, 906 {0, 1000000, 1000, 1, 1000000}, 907 {0, 1000000, 1998, 1, 999000000}, 908 {0, 1000000, 1999, 2, 0}, 909 {0, 1000000, 2000, 2, 1000000}, 910 {0, 1000000, -1, 0, 0}, 911 {0, 1000000, -2, -1, 999000000}, 912 {0, 1000000, -999, -1, 2000000}, 913 {0, 1000000, -1000, -1, 1000000}, 914 {0, 1000000, -1001, -1, 0}, 915 {0, 1000000, -1002, -2, 999000000}, 916 917 {0, 999999999, 0, 0, 999999999}, 918 {0, 999999999, 1, 1, 999999}, 919 {0, 999999999, 999, 1, 998999999}, 920 {0, 999999999, 1000, 1, 999999999}, 921 {0, 999999999, 1001, 2, 999999}, 922 {0, 999999999, -1, 0, 998999999}, 923 {0, 999999999, -1000, -1, 999999999}, 924 {0, 999999999, -1001, -1, 998999999}, 925 926 {0, 0, Long.MAX_VALUE, Long.MAX_VALUE / 1000, (int) (Long.MAX_VALUE % 1000) * 1000000}, 927 {0, 0, Long.MIN_VALUE, Long.MIN_VALUE / 1000 - 1, (int) (Long.MIN_VALUE % 1000) * 1000000 + 1000000000}, 928 }; 929 } 930 931 @Test(dataProvider="PlusMillis") 932 public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 933 Instant t = Instant.ofEpochSecond(seconds, nanos); 934 t = t.plusMillis(amount); 935 assertEquals(t.getEpochSecond(), expectedSeconds); 936 assertEquals(t.getNano(), expectedNanoOfSecond); 937 } 938 @Test(dataProvider="PlusMillis") 939 public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 940 Instant t = Instant.ofEpochSecond(seconds + 1, nanos); 941 t = t.plusMillis(amount); 942 assertEquals(t.getEpochSecond(), expectedSeconds + 1); 943 assertEquals(t.getNano(), expectedNanoOfSecond); 944 } 945 @Test(dataProvider="PlusMillis") 946 public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 947 Instant t = Instant.ofEpochSecond(seconds - 1, nanos); 948 t = t.plusMillis(amount); 949 assertEquals(t.getEpochSecond(), expectedSeconds - 1); 950 assertEquals(t.getNano(), expectedNanoOfSecond); 951 } 952 953 @Test 954 public void plusMillis_long_max() { 955 Instant t = Instant.ofEpochSecond(MAX_SECOND, 998999999); 956 t = t.plusMillis(1); 957 assertEquals(t.getEpochSecond(), MAX_SECOND); 958 assertEquals(t.getNano(), 999999999); 959 } 960 961 @Test(expectedExceptions=DateTimeException.class) 962 public void plusMillis_long_overflowTooBig() { 963 Instant t = Instant.ofEpochSecond(MAX_SECOND, 999000000); 964 t.plusMillis(1); 965 } 966 967 @Test 968 public void plusMillis_long_min() { 969 Instant t = Instant.ofEpochSecond(MIN_SECOND, 1000000); 970 t = t.plusMillis(-1); 971 assertEquals(t.getEpochSecond(), MIN_SECOND); 972 assertEquals(t.getNano(), 0); 973 } 974 975 @Test(expectedExceptions=DateTimeException.class) 976 public void plusMillis_long_overflowTooSmall() { 977 Instant t = Instant.ofEpochSecond(MIN_SECOND, 0); 978 t.plusMillis(-1); 979 } 980 981 //----------------------------------------------------------------------- 982 @DataProvider(name="PlusNanos") 983 Object[][] provider_plusNanos_long() { 984 return new Object[][] { 985 {0, 0, 0, 0, 0}, 986 {0, 0, 1, 0, 1}, 987 {0, 0, 999999999, 0, 999999999}, 988 {0, 0, 1000000000, 1, 0}, 989 {0, 0, 1000000001, 1, 1}, 990 {0, 0, 1999999999, 1, 999999999}, 991 {0, 0, 2000000000, 2, 0}, 992 {0, 0, -1, -1, 999999999}, 993 {0, 0, -999999999, -1, 1}, 994 {0, 0, -1000000000, -1, 0}, 995 {0, 0, -1000000001, -2, 999999999}, 996 {0, 0, -1999999999, -2, 1}, 997 998 {1, 0, 0, 1, 0}, 999 {1, 0, 1, 1, 1}, 1000 {1, 0, 999999999, 1, 999999999}, 1001 {1, 0, 1000000000, 2, 0}, 1002 {1, 0, 1000000001, 2, 1}, 1003 {1, 0, 1999999999, 2, 999999999}, 1004 {1, 0, 2000000000, 3, 0}, 1005 {1, 0, -1, 0, 999999999}, 1006 {1, 0, -999999999, 0, 1}, 1007 {1, 0, -1000000000, 0, 0}, 1008 {1, 0, -1000000001, -1, 999999999}, 1009 {1, 0, -1999999999, -1, 1}, 1010 1011 {-1, 0, 0, -1, 0}, 1012 {-1, 0, 1, -1, 1}, 1013 {-1, 0, 999999999, -1, 999999999}, 1014 {-1, 0, 1000000000, 0, 0}, 1015 {-1, 0, 1000000001, 0, 1}, 1016 {-1, 0, 1999999999, 0, 999999999}, 1017 {-1, 0, 2000000000, 1, 0}, 1018 {-1, 0, -1, -2, 999999999}, 1019 {-1, 0, -999999999, -2, 1}, 1020 {-1, 0, -1000000000, -2, 0}, 1021 {-1, 0, -1000000001, -3, 999999999}, 1022 {-1, 0, -1999999999, -3, 1}, 1023 1024 {1, 1, 0, 1, 1}, 1025 {1, 1, 1, 1, 2}, 1026 {1, 1, 999999998, 1, 999999999}, 1027 {1, 1, 999999999, 2, 0}, 1028 {1, 1, 1000000000, 2, 1}, 1029 {1, 1, 1999999998, 2, 999999999}, 1030 {1, 1, 1999999999, 3, 0}, 1031 {1, 1, 2000000000, 3, 1}, 1032 {1, 1, -1, 1, 0}, 1033 {1, 1, -2, 0, 999999999}, 1034 {1, 1, -1000000000, 0, 1}, 1035 {1, 1, -1000000001, 0, 0}, 1036 {1, 1, -1000000002, -1, 999999999}, 1037 {1, 1, -2000000000, -1, 1}, 1038 1039 {1, 999999999, 0, 1, 999999999}, 1040 {1, 999999999, 1, 2, 0}, 1041 {1, 999999999, 999999999, 2, 999999998}, 1042 {1, 999999999, 1000000000, 2, 999999999}, 1043 {1, 999999999, 1000000001, 3, 0}, 1044 {1, 999999999, -1, 1, 999999998}, 1045 {1, 999999999, -1000000000, 0, 999999999}, 1046 {1, 999999999, -1000000001, 0, 999999998}, 1047 {1, 999999999, -1999999999, 0, 0}, 1048 {1, 999999999, -2000000000, -1, 999999999}, 1049 1050 {MAX_SECOND, 0, 999999999, MAX_SECOND, 999999999}, 1051 {MAX_SECOND - 1, 0, 1999999999, MAX_SECOND, 999999999}, 1052 {MIN_SECOND, 1, -1, MIN_SECOND, 0}, 1053 {MIN_SECOND + 1, 1, -1000000001, MIN_SECOND, 0}, 1054 1055 {0, 0, MAX_SECOND, MAX_SECOND / 1000000000, (int) (MAX_SECOND % 1000000000)}, 1056 {0, 0, MIN_SECOND, MIN_SECOND / 1000000000 - 1, (int) (MIN_SECOND % 1000000000) + 1000000000}, 1057 }; 1058 } 1059 1060 @Test(dataProvider="PlusNanos") 1061 public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1062 Instant t = Instant.ofEpochSecond(seconds, nanos); 1063 t = t.plusNanos(amount); 1064 assertEquals(t.getEpochSecond(), expectedSeconds); 1065 assertEquals(t.getNano(), expectedNanoOfSecond); 1066 } 1067 1068 @Test(expectedExceptions=DateTimeException.class) 1069 public void plusNanos_long_overflowTooBig() { 1070 Instant t = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1071 t.plusNanos(1); 1072 } 1073 1074 @Test(expectedExceptions=DateTimeException.class) 1075 public void plusNanos_long_overflowTooSmall() { 1076 Instant t = Instant.ofEpochSecond(MIN_SECOND, 0); 1077 t.plusNanos(-1); 1078 } 1079 1080 //----------------------------------------------------------------------- 1081 @DataProvider(name="Minus") 1082 Object[][] provider_minus() { 1083 return new Object[][] { 1084 {MIN_SECOND, 0, MIN_SECOND, 0, 0, 0}, 1085 1086 {MIN_SECOND, 0, -1, 0, MIN_SECOND + 1, 0}, 1087 {MIN_SECOND, 0, 0, -500, MIN_SECOND, 500}, 1088 {MIN_SECOND, 0, 0, -1000000000, MIN_SECOND + 1, 0}, 1089 1090 {MIN_SECOND + 1, 0, 1, 0, MIN_SECOND, 0}, 1091 {MIN_SECOND + 1, 0, 0, 500, MIN_SECOND, 999999500}, 1092 {MIN_SECOND + 1, 0, 0, 1000000000, MIN_SECOND, 0}, 1093 1094 {-4, 666666667, -4, 666666667, 0, 0}, 1095 {-4, 666666667, -3, 0, -1, 666666667}, 1096 {-4, 666666667, -2, 0, -2, 666666667}, 1097 {-4, 666666667, -1, 0, -3, 666666667}, 1098 {-4, 666666667, -1, 333333334, -3, 333333333}, 1099 {-4, 666666667, -1, 666666667, -3, 0}, 1100 {-4, 666666667, -1, 999999999, -4, 666666668}, 1101 {-4, 666666667, 0, 0, -4, 666666667}, 1102 {-4, 666666667, 0, 1, -4, 666666666}, 1103 {-4, 666666667, 0, 333333333, -4, 333333334}, 1104 {-4, 666666667, 0, 666666666, -4, 1}, 1105 {-4, 666666667, 1, 0, -5, 666666667}, 1106 {-4, 666666667, 2, 0, -6, 666666667}, 1107 {-4, 666666667, 3, 0, -7, 666666667}, 1108 {-4, 666666667, 3, 333333333, -7, 333333334}, 1109 1110 {-3, 0, -4, 666666667, 0, 333333333}, 1111 {-3, 0, -3, 0, 0, 0}, 1112 {-3, 0, -2, 0, -1, 0}, 1113 {-3, 0, -1, 0, -2, 0}, 1114 {-3, 0, -1, 333333334, -3, 666666666}, 1115 {-3, 0, -1, 666666667, -3, 333333333}, 1116 {-3, 0, -1, 999999999, -3, 1}, 1117 {-3, 0, 0, 0, -3, 0}, 1118 {-3, 0, 0, 1, -4, 999999999}, 1119 {-3, 0, 0, 333333333, -4, 666666667}, 1120 {-3, 0, 0, 666666666, -4, 333333334}, 1121 {-3, 0, 1, 0, -4, 0}, 1122 {-3, 0, 2, 0, -5, 0}, 1123 {-3, 0, 3, 0, -6, 0}, 1124 {-3, 0, 3, 333333333, -7, 666666667}, 1125 1126 {-2, 0, -4, 666666667, 1, 333333333}, 1127 {-2, 0, -3, 0, 1, 0}, 1128 {-2, 0, -2, 0, 0, 0}, 1129 {-2, 0, -1, 0, -1, 0}, 1130 {-2, 0, -1, 333333334, -2, 666666666}, 1131 {-2, 0, -1, 666666667, -2, 333333333}, 1132 {-2, 0, -1, 999999999, -2, 1}, 1133 {-2, 0, 0, 0, -2, 0}, 1134 {-2, 0, 0, 1, -3, 999999999}, 1135 {-2, 0, 0, 333333333, -3, 666666667}, 1136 {-2, 0, 0, 666666666, -3, 333333334}, 1137 {-2, 0, 1, 0, -3, 0}, 1138 {-2, 0, 2, 0, -4, 0}, 1139 {-2, 0, 3, 0, -5, 0}, 1140 {-2, 0, 3, 333333333, -6, 666666667}, 1141 1142 {-1, 0, -4, 666666667, 2, 333333333}, 1143 {-1, 0, -3, 0, 2, 0}, 1144 {-1, 0, -2, 0, 1, 0}, 1145 {-1, 0, -1, 0, 0, 0}, 1146 {-1, 0, -1, 333333334, -1, 666666666}, 1147 {-1, 0, -1, 666666667, -1, 333333333}, 1148 {-1, 0, -1, 999999999, -1, 1}, 1149 {-1, 0, 0, 0, -1, 0}, 1150 {-1, 0, 0, 1, -2, 999999999}, 1151 {-1, 0, 0, 333333333, -2, 666666667}, 1152 {-1, 0, 0, 666666666, -2, 333333334}, 1153 {-1, 0, 1, 0, -2, 0}, 1154 {-1, 0, 2, 0, -3, 0}, 1155 {-1, 0, 3, 0, -4, 0}, 1156 {-1, 0, 3, 333333333, -5, 666666667}, 1157 1158 {-1, 666666667, -4, 666666667, 3, 0}, 1159 {-1, 666666667, -3, 0, 2, 666666667}, 1160 {-1, 666666667, -2, 0, 1, 666666667}, 1161 {-1, 666666667, -1, 0, 0, 666666667}, 1162 {-1, 666666667, -1, 333333334, 0, 333333333}, 1163 {-1, 666666667, -1, 666666667, 0, 0}, 1164 {-1, 666666667, -1, 999999999, -1, 666666668}, 1165 {-1, 666666667, 0, 0, -1, 666666667}, 1166 {-1, 666666667, 0, 1, -1, 666666666}, 1167 {-1, 666666667, 0, 333333333, -1, 333333334}, 1168 {-1, 666666667, 0, 666666666, -1, 1}, 1169 {-1, 666666667, 1, 0, -2, 666666667}, 1170 {-1, 666666667, 2, 0, -3, 666666667}, 1171 {-1, 666666667, 3, 0, -4, 666666667}, 1172 {-1, 666666667, 3, 333333333, -4, 333333334}, 1173 1174 {0, 0, -4, 666666667, 3, 333333333}, 1175 {0, 0, -3, 0, 3, 0}, 1176 {0, 0, -2, 0, 2, 0}, 1177 {0, 0, -1, 0, 1, 0}, 1178 {0, 0, -1, 333333334, 0, 666666666}, 1179 {0, 0, -1, 666666667, 0, 333333333}, 1180 {0, 0, -1, 999999999, 0, 1}, 1181 {0, 0, 0, 0, 0, 0}, 1182 {0, 0, 0, 1, -1, 999999999}, 1183 {0, 0, 0, 333333333, -1, 666666667}, 1184 {0, 0, 0, 666666666, -1, 333333334}, 1185 {0, 0, 1, 0, -1, 0}, 1186 {0, 0, 2, 0, -2, 0}, 1187 {0, 0, 3, 0, -3, 0}, 1188 {0, 0, 3, 333333333, -4, 666666667}, 1189 1190 {0, 333333333, -4, 666666667, 3, 666666666}, 1191 {0, 333333333, -3, 0, 3, 333333333}, 1192 {0, 333333333, -2, 0, 2, 333333333}, 1193 {0, 333333333, -1, 0, 1, 333333333}, 1194 {0, 333333333, -1, 333333334, 0, 999999999}, 1195 {0, 333333333, -1, 666666667, 0, 666666666}, 1196 {0, 333333333, -1, 999999999, 0, 333333334}, 1197 {0, 333333333, 0, 0, 0, 333333333}, 1198 {0, 333333333, 0, 1, 0, 333333332}, 1199 {0, 333333333, 0, 333333333, 0, 0}, 1200 {0, 333333333, 0, 666666666, -1, 666666667}, 1201 {0, 333333333, 1, 0, -1, 333333333}, 1202 {0, 333333333, 2, 0, -2, 333333333}, 1203 {0, 333333333, 3, 0, -3, 333333333}, 1204 {0, 333333333, 3, 333333333, -3, 0}, 1205 1206 {1, 0, -4, 666666667, 4, 333333333}, 1207 {1, 0, -3, 0, 4, 0}, 1208 {1, 0, -2, 0, 3, 0}, 1209 {1, 0, -1, 0, 2, 0}, 1210 {1, 0, -1, 333333334, 1, 666666666}, 1211 {1, 0, -1, 666666667, 1, 333333333}, 1212 {1, 0, -1, 999999999, 1, 1}, 1213 {1, 0, 0, 0, 1, 0}, 1214 {1, 0, 0, 1, 0, 999999999}, 1215 {1, 0, 0, 333333333, 0, 666666667}, 1216 {1, 0, 0, 666666666, 0, 333333334}, 1217 {1, 0, 1, 0, 0, 0}, 1218 {1, 0, 2, 0, -1, 0}, 1219 {1, 0, 3, 0, -2, 0}, 1220 {1, 0, 3, 333333333, -3, 666666667}, 1221 1222 {2, 0, -4, 666666667, 5, 333333333}, 1223 {2, 0, -3, 0, 5, 0}, 1224 {2, 0, -2, 0, 4, 0}, 1225 {2, 0, -1, 0, 3, 0}, 1226 {2, 0, -1, 333333334, 2, 666666666}, 1227 {2, 0, -1, 666666667, 2, 333333333}, 1228 {2, 0, -1, 999999999, 2, 1}, 1229 {2, 0, 0, 0, 2, 0}, 1230 {2, 0, 0, 1, 1, 999999999}, 1231 {2, 0, 0, 333333333, 1, 666666667}, 1232 {2, 0, 0, 666666666, 1, 333333334}, 1233 {2, 0, 1, 0, 1, 0}, 1234 {2, 0, 2, 0, 0, 0}, 1235 {2, 0, 3, 0, -1, 0}, 1236 {2, 0, 3, 333333333, -2, 666666667}, 1237 1238 {3, 0, -4, 666666667, 6, 333333333}, 1239 {3, 0, -3, 0, 6, 0}, 1240 {3, 0, -2, 0, 5, 0}, 1241 {3, 0, -1, 0, 4, 0}, 1242 {3, 0, -1, 333333334, 3, 666666666}, 1243 {3, 0, -1, 666666667, 3, 333333333}, 1244 {3, 0, -1, 999999999, 3, 1}, 1245 {3, 0, 0, 0, 3, 0}, 1246 {3, 0, 0, 1, 2, 999999999}, 1247 {3, 0, 0, 333333333, 2, 666666667}, 1248 {3, 0, 0, 666666666, 2, 333333334}, 1249 {3, 0, 1, 0, 2, 0}, 1250 {3, 0, 2, 0, 1, 0}, 1251 {3, 0, 3, 0, 0, 0}, 1252 {3, 0, 3, 333333333, -1, 666666667}, 1253 1254 {3, 333333333, -4, 666666667, 6, 666666666}, 1255 {3, 333333333, -3, 0, 6, 333333333}, 1256 {3, 333333333, -2, 0, 5, 333333333}, 1257 {3, 333333333, -1, 0, 4, 333333333}, 1258 {3, 333333333, -1, 333333334, 3, 999999999}, 1259 {3, 333333333, -1, 666666667, 3, 666666666}, 1260 {3, 333333333, -1, 999999999, 3, 333333334}, 1261 {3, 333333333, 0, 0, 3, 333333333}, 1262 {3, 333333333, 0, 1, 3, 333333332}, 1263 {3, 333333333, 0, 333333333, 3, 0}, 1264 {3, 333333333, 0, 666666666, 2, 666666667}, 1265 {3, 333333333, 1, 0, 2, 333333333}, 1266 {3, 333333333, 2, 0, 1, 333333333}, 1267 {3, 333333333, 3, 0, 0, 333333333}, 1268 {3, 333333333, 3, 333333333, 0, 0}, 1269 1270 {MAX_SECOND - 1, 0, -1, 0, MAX_SECOND, 0}, 1271 {MAX_SECOND - 1, 0, 0, -500, MAX_SECOND - 1, 500}, 1272 {MAX_SECOND - 1, 0, 0, -1000000000, MAX_SECOND, 0}, 1273 1274 {MAX_SECOND, 0, 1, 0, MAX_SECOND - 1, 0}, 1275 {MAX_SECOND, 0, 0, 500, MAX_SECOND - 1, 999999500}, 1276 {MAX_SECOND, 0, 0, 1000000000, MAX_SECOND - 1, 0}, 1277 1278 {MAX_SECOND, 0, MAX_SECOND, 0, 0, 0}, 1279 }; 1280 } 1281 1282 @Test(dataProvider="Minus") 1283 public void minus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1284 Instant i = Instant.ofEpochSecond(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos)); 1285 assertEquals(i.getEpochSecond(), expectedSeconds); 1286 assertEquals(i.getNano(), expectedNanoOfSecond); 1287 } 1288 1289 @Test(expectedExceptions=DateTimeException.class) 1290 public void minus_Duration_overflowTooSmall() { 1291 Instant i = Instant.ofEpochSecond(MIN_SECOND); 1292 i.minus(Duration.ofSeconds(0, 1)); 1293 } 1294 1295 @Test(expectedExceptions=DateTimeException.class) 1296 public void minus_Duration_overflowTooBig() { 1297 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1298 i.minus(Duration.ofSeconds(-1, 999999999)); 1299 } 1300 1301 //----------------------------------------------------------------------- 1302 @Test(dataProvider="Minus") 1303 public void minus_longTemporalUnit(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1304 Instant i = Instant.ofEpochSecond(seconds, nanos).minus(otherSeconds, SECONDS).minus(otherNanos, NANOS); 1305 assertEquals(i.getEpochSecond(), expectedSeconds); 1306 assertEquals(i.getNano(), expectedNanoOfSecond); 1307 } 1308 1309 @Test(expectedExceptions=DateTimeException.class) 1310 public void minus_longTemporalUnit_overflowTooSmall() { 1311 Instant i = Instant.ofEpochSecond(MIN_SECOND); 1312 i.minus(1, NANOS); 1313 } 1314 1315 @Test(expectedExceptions=DateTimeException.class) 1316 public void minus_longTemporalUnit_overflowTooBig() { 1317 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1318 i.minus(999999999, NANOS); 1319 i.minus(-1, SECONDS); 1320 } 1321 1322 //----------------------------------------------------------------------- 1323 @DataProvider(name="MinusSeconds") 1324 Object[][] provider_minusSeconds_long() { 1325 return new Object[][] { 1326 {0, 0, 0, 0, 0}, 1327 {0, 0, 1, -1, 0}, 1328 {0, 0, -1, 1, 0}, 1329 {0, 0, -MIN_SECOND, MIN_SECOND, 0}, 1330 {1, 0, 0, 1, 0}, 1331 {1, 0, 1, 0, 0}, 1332 {1, 0, -1, 2, 0}, 1333 {1, 0, -MIN_SECOND + 1, MIN_SECOND, 0}, 1334 {1, 1, 0, 1, 1}, 1335 {1, 1, 1, 0, 1}, 1336 {1, 1, -1, 2, 1}, 1337 {1, 1, -MIN_SECOND, MIN_SECOND + 1, 1}, 1338 {1, 1, -MIN_SECOND + 1, MIN_SECOND, 1}, 1339 {-1, 1, 0, -1, 1}, 1340 {-1, 1, 1, -2, 1}, 1341 {-1, 1, -1, 0, 1}, 1342 {-1, 1, -MAX_SECOND, MAX_SECOND - 1, 1}, 1343 {-1, 1, -(MAX_SECOND + 1), MAX_SECOND, 1}, 1344 1345 {MIN_SECOND, 2, MIN_SECOND, 0, 2}, 1346 {MIN_SECOND + 1, 2, MIN_SECOND, 1, 2}, 1347 {MAX_SECOND - 1, 2, MAX_SECOND, -1, 2}, 1348 {MAX_SECOND, 2, MAX_SECOND, 0, 2}, 1349 }; 1350 } 1351 1352 @Test(dataProvider="MinusSeconds") 1353 public void minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1354 Instant i = Instant.ofEpochSecond(seconds, nanos); 1355 i = i.minusSeconds(amount); 1356 assertEquals(i.getEpochSecond(), expectedSeconds); 1357 assertEquals(i.getNano(), expectedNanoOfSecond); 1358 } 1359 1360 @Test(expectedExceptions = {ArithmeticException.class}) 1361 public void minusSeconds_long_overflowTooBig() { 1362 Instant i = Instant.ofEpochSecond(1, 0); 1363 i.minusSeconds(Long.MIN_VALUE + 1); 1364 } 1365 1366 @Test(expectedExceptions = {ArithmeticException.class}) 1367 public void minusSeconds_long_overflowTooSmall() { 1368 Instant i = Instant.ofEpochSecond(-2, 0); 1369 i.minusSeconds(Long.MAX_VALUE); 1370 } 1371 1372 //----------------------------------------------------------------------- 1373 @DataProvider(name="MinusMillis") 1374 Object[][] provider_minusMillis_long() { 1375 return new Object[][] { 1376 {0, 0, 0, 0, 0}, 1377 {0, 0, 1, -1, 999000000}, 1378 {0, 0, 999, -1, 1000000}, 1379 {0, 0, 1000, -1, 0}, 1380 {0, 0, 1001, -2, 999000000}, 1381 {0, 0, 1999, -2, 1000000}, 1382 {0, 0, 2000, -2, 0}, 1383 {0, 0, -1, 0, 1000000}, 1384 {0, 0, -999, 0, 999000000}, 1385 {0, 0, -1000, 1, 0}, 1386 {0, 0, -1001, 1, 1000000}, 1387 {0, 0, -1999, 1, 999000000}, 1388 1389 {0, 1, 0, 0, 1}, 1390 {0, 1, 1, -1, 999000001}, 1391 {0, 1, 998, -1, 2000001}, 1392 {0, 1, 999, -1, 1000001}, 1393 {0, 1, 1000, -1, 1}, 1394 {0, 1, 1998, -2, 2000001}, 1395 {0, 1, 1999, -2, 1000001}, 1396 {0, 1, 2000, -2, 1}, 1397 {0, 1, -1, 0, 1000001}, 1398 {0, 1, -2, 0, 2000001}, 1399 {0, 1, -1000, 1, 1}, 1400 {0, 1, -1001, 1, 1000001}, 1401 1402 {0, 1000000, 0, 0, 1000000}, 1403 {0, 1000000, 1, 0, 0}, 1404 {0, 1000000, 998, -1, 3000000}, 1405 {0, 1000000, 999, -1, 2000000}, 1406 {0, 1000000, 1000, -1, 1000000}, 1407 {0, 1000000, 1998, -2, 3000000}, 1408 {0, 1000000, 1999, -2, 2000000}, 1409 {0, 1000000, 2000, -2, 1000000}, 1410 {0, 1000000, -1, 0, 2000000}, 1411 {0, 1000000, -2, 0, 3000000}, 1412 {0, 1000000, -999, 1, 0}, 1413 {0, 1000000, -1000, 1, 1000000}, 1414 {0, 1000000, -1001, 1, 2000000}, 1415 {0, 1000000, -1002, 1, 3000000}, 1416 1417 {0, 999999999, 0, 0, 999999999}, 1418 {0, 999999999, 1, 0, 998999999}, 1419 {0, 999999999, 999, 0, 999999}, 1420 {0, 999999999, 1000, -1, 999999999}, 1421 {0, 999999999, 1001, -1, 998999999}, 1422 {0, 999999999, -1, 1, 999999}, 1423 {0, 999999999, -1000, 1, 999999999}, 1424 {0, 999999999, -1001, 2, 999999}, 1425 1426 {0, 0, Long.MAX_VALUE, -(Long.MAX_VALUE / 1000) - 1, (int) -(Long.MAX_VALUE % 1000) * 1000000 + 1000000000}, 1427 {0, 0, Long.MIN_VALUE, -(Long.MIN_VALUE / 1000), (int) -(Long.MIN_VALUE % 1000) * 1000000}, 1428 }; 1429 } 1430 1431 @Test(dataProvider="MinusMillis") 1432 public void minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1433 Instant i = Instant.ofEpochSecond(seconds, nanos); 1434 i = i.minusMillis(amount); 1435 assertEquals(i.getEpochSecond(), expectedSeconds); 1436 assertEquals(i.getNano(), expectedNanoOfSecond); 1437 } 1438 1439 @Test(dataProvider="MinusMillis") 1440 public void minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1441 Instant i = Instant.ofEpochSecond(seconds + 1, nanos); 1442 i = i.minusMillis(amount); 1443 assertEquals(i.getEpochSecond(), expectedSeconds + 1); 1444 assertEquals(i.getNano(), expectedNanoOfSecond); 1445 } 1446 1447 @Test(dataProvider="MinusMillis") 1448 public void minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1449 Instant i = Instant.ofEpochSecond(seconds - 1, nanos); 1450 i = i.minusMillis(amount); 1451 assertEquals(i.getEpochSecond(), expectedSeconds - 1); 1452 assertEquals(i.getNano(), expectedNanoOfSecond); 1453 } 1454 1455 @Test 1456 public void minusMillis_long_max() { 1457 Instant i = Instant.ofEpochSecond(MAX_SECOND, 998999999); 1458 i = i.minusMillis(-1); 1459 assertEquals(i.getEpochSecond(), MAX_SECOND); 1460 assertEquals(i.getNano(), 999999999); 1461 } 1462 1463 @Test(expectedExceptions=DateTimeException.class) 1464 public void minusMillis_long_overflowTooBig() { 1465 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999000000); 1466 i.minusMillis(-1); 1467 } 1468 1469 @Test 1470 public void minusMillis_long_min() { 1471 Instant i = Instant.ofEpochSecond(MIN_SECOND, 1000000); 1472 i = i.minusMillis(1); 1473 assertEquals(i.getEpochSecond(), MIN_SECOND); 1474 assertEquals(i.getNano(), 0); 1475 } 1476 1477 @Test(expectedExceptions=DateTimeException.class) 1478 public void minusMillis_long_overflowTooSmall() { 1479 Instant i = Instant.ofEpochSecond(MIN_SECOND, 0); 1480 i.minusMillis(1); 1481 } 1482 1483 //----------------------------------------------------------------------- 1484 @DataProvider(name="MinusNanos") 1485 Object[][] provider_minusNanos_long() { 1486 return new Object[][] { 1487 {0, 0, 0, 0, 0}, 1488 {0, 0, 1, -1, 999999999}, 1489 {0, 0, 999999999, -1, 1}, 1490 {0, 0, 1000000000, -1, 0}, 1491 {0, 0, 1000000001, -2, 999999999}, 1492 {0, 0, 1999999999, -2, 1}, 1493 {0, 0, 2000000000, -2, 0}, 1494 {0, 0, -1, 0, 1}, 1495 {0, 0, -999999999, 0, 999999999}, 1496 {0, 0, -1000000000, 1, 0}, 1497 {0, 0, -1000000001, 1, 1}, 1498 {0, 0, -1999999999, 1, 999999999}, 1499 1500 {1, 0, 0, 1, 0}, 1501 {1, 0, 1, 0, 999999999}, 1502 {1, 0, 999999999, 0, 1}, 1503 {1, 0, 1000000000, 0, 0}, 1504 {1, 0, 1000000001, -1, 999999999}, 1505 {1, 0, 1999999999, -1, 1}, 1506 {1, 0, 2000000000, -1, 0}, 1507 {1, 0, -1, 1, 1}, 1508 {1, 0, -999999999, 1, 999999999}, 1509 {1, 0, -1000000000, 2, 0}, 1510 {1, 0, -1000000001, 2, 1}, 1511 {1, 0, -1999999999, 2, 999999999}, 1512 1513 {-1, 0, 0, -1, 0}, 1514 {-1, 0, 1, -2, 999999999}, 1515 {-1, 0, 999999999, -2, 1}, 1516 {-1, 0, 1000000000, -2, 0}, 1517 {-1, 0, 1000000001, -3, 999999999}, 1518 {-1, 0, 1999999999, -3, 1}, 1519 {-1, 0, 2000000000, -3, 0}, 1520 {-1, 0, -1, -1, 1}, 1521 {-1, 0, -999999999, -1, 999999999}, 1522 {-1, 0, -1000000000, 0, 0}, 1523 {-1, 0, -1000000001, 0, 1}, 1524 {-1, 0, -1999999999, 0, 999999999}, 1525 1526 {1, 1, 0, 1, 1}, 1527 {1, 1, 1, 1, 0}, 1528 {1, 1, 999999998, 0, 3}, 1529 {1, 1, 999999999, 0, 2}, 1530 {1, 1, 1000000000, 0, 1}, 1531 {1, 1, 1999999998, -1, 3}, 1532 {1, 1, 1999999999, -1, 2}, 1533 {1, 1, 2000000000, -1, 1}, 1534 {1, 1, -1, 1, 2}, 1535 {1, 1, -2, 1, 3}, 1536 {1, 1, -1000000000, 2, 1}, 1537 {1, 1, -1000000001, 2, 2}, 1538 {1, 1, -1000000002, 2, 3}, 1539 {1, 1, -2000000000, 3, 1}, 1540 1541 {1, 999999999, 0, 1, 999999999}, 1542 {1, 999999999, 1, 1, 999999998}, 1543 {1, 999999999, 999999999, 1, 0}, 1544 {1, 999999999, 1000000000, 0, 999999999}, 1545 {1, 999999999, 1000000001, 0, 999999998}, 1546 {1, 999999999, -1, 2, 0}, 1547 {1, 999999999, -1000000000, 2, 999999999}, 1548 {1, 999999999, -1000000001, 3, 0}, 1549 {1, 999999999, -1999999999, 3, 999999998}, 1550 {1, 999999999, -2000000000, 3, 999999999}, 1551 1552 {MAX_SECOND, 0, -999999999, MAX_SECOND, 999999999}, 1553 {MAX_SECOND - 1, 0, -1999999999, MAX_SECOND, 999999999}, 1554 {MIN_SECOND, 1, 1, MIN_SECOND, 0}, 1555 {MIN_SECOND + 1, 1, 1000000001, MIN_SECOND, 0}, 1556 1557 {0, 0, Long.MAX_VALUE, -(Long.MAX_VALUE / 1000000000) - 1, (int) -(Long.MAX_VALUE % 1000000000) + 1000000000}, 1558 {0, 0, Long.MIN_VALUE, -(Long.MIN_VALUE / 1000000000), (int) -(Long.MIN_VALUE % 1000000000)}, 1559 }; 1560 } 1561 1562 @Test(dataProvider="MinusNanos") 1563 public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1564 Instant i = Instant.ofEpochSecond(seconds, nanos); 1565 i = i.minusNanos(amount); 1566 assertEquals(i.getEpochSecond(), expectedSeconds); 1567 assertEquals(i.getNano(), expectedNanoOfSecond); 1568 } 1569 1570 @Test(expectedExceptions=DateTimeException.class) 1571 public void minusNanos_long_overflowTooBig() { 1572 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1573 i.minusNanos(-1); 1574 } 1575 1576 @Test(expectedExceptions=DateTimeException.class) 1577 public void minusNanos_long_overflowTooSmall() { 1578 Instant i = Instant.ofEpochSecond(MIN_SECOND, 0); 1579 i.minusNanos(1); 1580 } 1581 1582 //----------------------------------------------------------------------- 1583 // atOffset() 1584 //----------------------------------------------------------------------- 1585 @Test 1586 public void test_atOffset() { 1587 for (int i = 0; i < (24 * 60 * 60); i++) { 1588 Instant instant = Instant.ofEpochSecond(i); 1589 OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1)); 1590 assertEquals(test.getYear(), 1970); 1591 assertEquals(test.getMonthValue(), 1); 1592 assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0)); 1593 assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24); 1594 assertEquals(test.getMinute(), (i / 60) % 60); 1595 assertEquals(test.getSecond(), i % 60); 1596 } 1597 } 1598 1599 @Test(expectedExceptions=NullPointerException.class) 1600 public void test_atOffset_null() { 1601 TEST_12345_123456789.atOffset(null); 1602 } 1603 1604 //----------------------------------------------------------------------- 1605 // atZone() 1606 //----------------------------------------------------------------------- 1607 @Test 1608 public void test_atZone() { 1609 for (int i = 0; i < (24 * 60 * 60); i++) { 1610 Instant instant = Instant.ofEpochSecond(i); 1611 ZonedDateTime test = instant.atZone(ZoneOffset.ofHours(1)); 1612 assertEquals(test.getYear(), 1970); 1613 assertEquals(test.getMonthValue(), 1); 1614 assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0)); 1615 assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24); 1616 assertEquals(test.getMinute(), (i / 60) % 60); 1617 assertEquals(test.getSecond(), i % 60); 1618 } 1619 } 1620 1621 @Test(expectedExceptions=NullPointerException.class) 1622 public void test_atZone_null() { 1623 TEST_12345_123456789.atZone(null); 1624 } 1625 1626 //----------------------------------------------------------------------- 1627 // toEpochMilli() 1628 //----------------------------------------------------------------------- 1629 @Test 1630 public void test_toEpochMilli() { 1631 assertEquals(Instant.ofEpochSecond(1L, 1000000).toEpochMilli(), 1001L); 1632 assertEquals(Instant.ofEpochSecond(1L, 2000000).toEpochMilli(), 1002L); 1633 assertEquals(Instant.ofEpochSecond(1L, 567).toEpochMilli(), 1000L); 1634 assertEquals(Instant.ofEpochSecond(Long.MAX_VALUE / 1000).toEpochMilli(), (Long.MAX_VALUE / 1000) * 1000); 1635 assertEquals(Instant.ofEpochSecond(Long.MIN_VALUE / 1000).toEpochMilli(), (Long.MIN_VALUE / 1000) * 1000); 1636 assertEquals(Instant.ofEpochSecond(0L, -1000000).toEpochMilli(), -1L); 1637 assertEquals(Instant.ofEpochSecond(0L, 1000000).toEpochMilli(), 1); 1638 assertEquals(Instant.ofEpochSecond(0L, 999999).toEpochMilli(), 0); 1639 assertEquals(Instant.ofEpochSecond(0L, 1).toEpochMilli(), 0); 1640 assertEquals(Instant.ofEpochSecond(0L, 0).toEpochMilli(), 0); 1641 assertEquals(Instant.ofEpochSecond(0L, -1).toEpochMilli(), -1L); 1642 assertEquals(Instant.ofEpochSecond(0L, -999999).toEpochMilli(), -1L); 1643 assertEquals(Instant.ofEpochSecond(0L, -1000000).toEpochMilli(), -1L); 1644 assertEquals(Instant.ofEpochSecond(0L, -1000001).toEpochMilli(), -2L); 1645 } 1646 1647 @Test(expectedExceptions=ArithmeticException.class) 1648 public void test_toEpochMilli_tooBig() { 1649 Instant.ofEpochSecond(Long.MAX_VALUE / 1000 + 1).toEpochMilli(); 1650 } 1651 1652 @Test(expectedExceptions=ArithmeticException.class) 1653 public void test_toEpochMilli_tooSmall() { 1654 Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1).toEpochMilli(); 1655 } 1656 1657 //----------------------------------------------------------------------- 1658 // compareTo() 1659 //----------------------------------------------------------------------- 1660 @Test 1661 public void test_comparisons() { 1662 doTest_comparisons_Instant( 1663 Instant.ofEpochSecond(-2L, 0), 1664 Instant.ofEpochSecond(-2L, 999999998), 1665 Instant.ofEpochSecond(-2L, 999999999), 1666 Instant.ofEpochSecond(-1L, 0), 1667 Instant.ofEpochSecond(-1L, 1), 1668 Instant.ofEpochSecond(-1L, 999999998), 1669 Instant.ofEpochSecond(-1L, 999999999), 1670 Instant.ofEpochSecond(0L, 0), 1671 Instant.ofEpochSecond(0L, 1), 1672 Instant.ofEpochSecond(0L, 2), 1673 Instant.ofEpochSecond(0L, 999999999), 1674 Instant.ofEpochSecond(1L, 0), 1675 Instant.ofEpochSecond(2L, 0) 1676 ); 1677 } 1678 1679 void doTest_comparisons_Instant(Instant... instants) { 1680 for (int i = 0; i < instants.length; i++) { 1681 Instant a = instants[i]; 1682 for (int j = 0; j < instants.length; j++) { 1683 Instant b = instants[j]; 1684 if (i < j) { 1685 assertEquals(a.compareTo(b) < 0, true, a + " <=> " + b); 1686 assertEquals(a.isBefore(b), true, a + " <=> " + b); 1687 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1688 assertEquals(a.equals(b), false, a + " <=> " + b); 1689 } else if (i > j) { 1690 assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b); 1691 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1692 assertEquals(a.isAfter(b), true, a + " <=> " + b); 1693 assertEquals(a.equals(b), false, a + " <=> " + b); 1694 } else { 1695 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 1696 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1697 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1698 assertEquals(a.equals(b), true, a + " <=> " + b); 1699 } 1700 } 1701 } 1702 } 1703 1704 @Test(expectedExceptions=NullPointerException.class) 1705 public void test_compareTo_ObjectNull() { 1706 Instant a = Instant.ofEpochSecond(0L, 0); 1707 a.compareTo(null); 1708 } 1709 1710 @Test(expectedExceptions=NullPointerException.class) 1711 public void test_isBefore_ObjectNull() { 1712 Instant a = Instant.ofEpochSecond(0L, 0); 1713 a.isBefore(null); 1714 } 1715 1716 @Test(expectedExceptions=NullPointerException.class) 1717 public void test_isAfter_ObjectNull() { 1718 Instant a = Instant.ofEpochSecond(0L, 0); 1719 a.isAfter(null); 1720 } 1721 1722 @Test(expectedExceptions=ClassCastException.class) 1723 @SuppressWarnings({"unchecked", "rawtypes"}) 1724 public void compareToNonInstant() { 1725 Comparable c = Instant.ofEpochSecond(0L); 1726 c.compareTo(new Object()); 1727 } 1728 1729 //----------------------------------------------------------------------- 1730 // equals() 1731 //----------------------------------------------------------------------- 1732 @Test 1733 public void test_equals() { 1734 Instant test5a = Instant.ofEpochSecond(5L, 20); 1735 Instant test5b = Instant.ofEpochSecond(5L, 20); 1736 Instant test5n = Instant.ofEpochSecond(5L, 30); 1737 Instant test6 = Instant.ofEpochSecond(6L, 20); 1738 1739 assertEquals(test5a.equals(test5a), true); 1740 assertEquals(test5a.equals(test5b), true); 1741 assertEquals(test5a.equals(test5n), false); 1742 assertEquals(test5a.equals(test6), false); 1743 1744 assertEquals(test5b.equals(test5a), true); 1745 assertEquals(test5b.equals(test5b), true); 1746 assertEquals(test5b.equals(test5n), false); 1747 assertEquals(test5b.equals(test6), false); 1748 1749 assertEquals(test5n.equals(test5a), false); 1750 assertEquals(test5n.equals(test5b), false); 1751 assertEquals(test5n.equals(test5n), true); 1752 assertEquals(test5n.equals(test6), false); 1753 1754 assertEquals(test6.equals(test5a), false); 1755 assertEquals(test6.equals(test5b), false); 1756 assertEquals(test6.equals(test5n), false); 1757 assertEquals(test6.equals(test6), true); 1758 } 1759 1760 @Test 1761 public void test_equals_null() { 1762 Instant test5 = Instant.ofEpochSecond(5L, 20); 1763 assertEquals(test5.equals(null), false); 1764 } 1765 1766 @Test 1767 public void test_equals_otherClass() { 1768 Instant test5 = Instant.ofEpochSecond(5L, 20); 1769 assertEquals(test5.equals(""), false); 1770 } 1771 1772 //----------------------------------------------------------------------- 1773 // hashCode() 1774 //----------------------------------------------------------------------- 1775 @Test 1776 public void test_hashCode() { 1777 Instant test5a = Instant.ofEpochSecond(5L, 20); 1778 Instant test5b = Instant.ofEpochSecond(5L, 20); 1779 Instant test5n = Instant.ofEpochSecond(5L, 30); 1780 Instant test6 = Instant.ofEpochSecond(6L, 20); 1781 1782 assertEquals(test5a.hashCode() == test5a.hashCode(), true); 1783 assertEquals(test5a.hashCode() == test5b.hashCode(), true); 1784 assertEquals(test5b.hashCode() == test5b.hashCode(), true); 1785 1786 assertEquals(test5a.hashCode() == test5n.hashCode(), false); 1787 assertEquals(test5a.hashCode() == test6.hashCode(), false); 1788 } 1789 1790 //----------------------------------------------------------------------- 1791 // toString() 1792 //----------------------------------------------------------------------- 1793 @DataProvider(name="toStringParse") 1794 Object[][] data_toString() { 1795 return new Object[][] { 1796 {Instant.ofEpochSecond(65L, 567), "1970-01-01T00:01:05.000000567Z"}, 1797 {Instant.ofEpochSecond(1, 0), "1970-01-01T00:00:01Z"}, 1798 {Instant.ofEpochSecond(60, 0), "1970-01-01T00:01Z"}, 1799 {Instant.ofEpochSecond(3600, 0), "1970-01-01T01:00Z"}, 1800 {Instant.ofEpochSecond(-1, 0), "1969-12-31T23:59:59Z"}, 1801 1802 {LocalDateTime.of(0, 1, 2, 0, 0).toInstant(ZoneOffset.UTC), "0000-01-02T00:00Z"}, 1803 {LocalDateTime.of(0, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "0000-01-01T12:30Z"}, 1804 {LocalDateTime.of(0, 1, 1, 0, 0, 0, 1).toInstant(ZoneOffset.UTC), "0000-01-01T00:00:00.000000001Z"}, 1805 {LocalDateTime.of(0, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "0000-01-01T00:00Z"}, 1806 1807 {LocalDateTime.of(-1, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "-0001-12-31T23:59:59.999999999Z"}, 1808 {LocalDateTime.of(-1, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-0001-12-31T12:30Z"}, 1809 {LocalDateTime.of(-1, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-0001-12-30T12:30Z"}, 1810 1811 {LocalDateTime.of(-9999, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "-9999-01-02T12:30Z"}, 1812 {LocalDateTime.of(-9999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "-9999-01-01T12:30Z"}, 1813 {LocalDateTime.of(-9999, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "-9999-01-01T00:00Z"}, 1814 1815 {LocalDateTime.of(-10000, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "-10000-12-31T23:59:59.999999999Z"}, 1816 {LocalDateTime.of(-10000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-10000-12-31T12:30Z"}, 1817 {LocalDateTime.of(-10000, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-10000-12-30T12:30Z"}, 1818 {LocalDateTime.of(-15000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-15000-12-31T12:30Z"}, 1819 1820 {LocalDateTime.of(-19999, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "-19999-01-02T12:30Z"}, 1821 {LocalDateTime.of(-19999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "-19999-01-01T12:30Z"}, 1822 {LocalDateTime.of(-19999, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "-19999-01-01T00:00Z"}, 1823 1824 {LocalDateTime.of(-20000, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "-20000-12-31T23:59:59.999999999Z"}, 1825 {LocalDateTime.of(-20000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-20000-12-31T12:30Z"}, 1826 {LocalDateTime.of(-20000, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-20000-12-30T12:30Z"}, 1827 {LocalDateTime.of(-25000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-25000-12-31T12:30Z"}, 1828 1829 {LocalDateTime.of(9999, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "9999-12-30T12:30Z"}, 1830 {LocalDateTime.of(9999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "9999-12-31T12:30Z"}, 1831 {LocalDateTime.of(9999, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "9999-12-31T23:59:59.999999999Z"}, 1832 1833 {LocalDateTime.of(10000, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "+10000-01-01T00:00Z"}, 1834 {LocalDateTime.of(10000, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "+10000-01-01T12:30Z"}, 1835 {LocalDateTime.of(10000, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "+10000-01-02T12:30Z"}, 1836 {LocalDateTime.of(15000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+15000-12-31T12:30Z"}, 1837 1838 {LocalDateTime.of(19999, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "+19999-12-30T12:30Z"}, 1839 {LocalDateTime.of(19999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+19999-12-31T12:30Z"}, 1840 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.999999999Z"}, 1841 1842 {LocalDateTime.of(20000, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "+20000-01-01T00:00Z"}, 1843 {LocalDateTime.of(20000, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "+20000-01-01T12:30Z"}, 1844 {LocalDateTime.of(20000, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "+20000-01-02T12:30Z"}, 1845 {LocalDateTime.of(25000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+25000-12-31T12:30Z"}, 1846 1847 {LocalDateTime.of(-999_999_999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC).minus(1, DAYS), "-1000000000-12-31T12:30Z"}, 1848 {LocalDateTime.of(999_999_999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC).plus(1, DAYS), "+1000000000-01-01T12:30Z"}, 1849 1850 {Instant.MIN, "-1000000000-01-01T00:00Z"}, 1851 {Instant.MAX, "+1000000000-12-31T23:59:59.999999999Z"}, 1852 }; 1853 } 1854 1855 @Test(dataProvider="toStringParse") 1856 public void test_toString(Instant instant, String expected) { 1857 assertEquals(instant.toString(), expected); 1858 } 1859 1860 @Test(dataProvider="toStringParse") 1861 public void test_parse(Instant instant, String text) { 1862 assertEquals(Instant.parse(text), instant); 1863 } 1864 1865 @Test(dataProvider="toStringParse") 1866 public void test_parseLowercase(Instant instant, String text) { 1867 assertEquals(Instant.parse(text.toLowerCase(Locale.ENGLISH)), instant); 1868 } 1869 1870 }