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