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