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