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.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 {Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_789), MINUTES, Instant.ofEpochSecond(-86400 - 3600 - 120, 0 )}, 643 {Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_789), MICROS, Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_000)}, 644 }; 645 } 646 @Test(dataProvider="truncatedToValid") 647 public void test_truncatedTo_valid(Instant input, TemporalUnit unit, Instant expected) { 648 assertEquals(input.truncatedTo(unit), expected); 649 } 650 651 @DataProvider(name="truncatedToInvalid") 652 Object[][] data_truncatedToInvalid() { 653 return new Object[][] { 654 {Instant.ofEpochSecond(1, 123_456_789), NINETY_FIVE_MINS}, 655 {Instant.ofEpochSecond(1, 123_456_789), WEEKS}, 656 {Instant.ofEpochSecond(1, 123_456_789), MONTHS}, 657 {Instant.ofEpochSecond(1, 123_456_789), YEARS}, 658 }; 659 } 660 661 @Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class) 662 public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) { 663 input.truncatedTo(unit); 664 } 665 666 @Test(expectedExceptions=NullPointerException.class) 667 public void test_truncatedTo_null() { 668 TEST_12345_123456789.truncatedTo(null); 669 } 670 671 //----------------------------------------------------------------------- 672 // plus(TemporalAmount) 673 //----------------------------------------------------------------------- 674 @DataProvider(name="plusTemporalAmount") 675 Object[][] data_plusTemporalAmount() { 676 return new Object[][] { 677 {DAYS, MockSimplePeriod.of(1, DAYS), 86401, 0}, 678 {HOURS, MockSimplePeriod.of(2, HOURS), 7201, 0}, 679 {MINUTES, MockSimplePeriod.of(4, MINUTES), 241, 0}, 680 {SECONDS, MockSimplePeriod.of(5, SECONDS), 6, 0}, 681 {NANOS, MockSimplePeriod.of(6, NANOS), 1, 6}, 682 {DAYS, MockSimplePeriod.of(10, DAYS), 864001, 0}, 683 {HOURS, MockSimplePeriod.of(11, HOURS), 39601, 0}, 684 {MINUTES, MockSimplePeriod.of(12, MINUTES), 721, 0}, 685 {SECONDS, MockSimplePeriod.of(13, SECONDS), 14, 0}, 686 {NANOS, MockSimplePeriod.of(14, NANOS), 1, 14}, 687 {SECONDS, Duration.ofSeconds(20, 40), 21, 40}, 688 {NANOS, Duration.ofSeconds(30, 300), 31, 300}, 689 }; 690 } 691 692 @Test(dataProvider="plusTemporalAmount") 693 public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) { 694 Instant inst = Instant.ofEpochMilli(1000); 695 Instant actual = inst.plus(amount); 696 Instant expected = Instant.ofEpochSecond(seconds, nanos); 697 assertEquals(actual, expected, "plus(TemporalAmount) failed"); 698 } 699 700 @DataProvider(name="badTemporalAmount") 701 Object[][] data_badPlusTemporalAmount() { 702 return new Object[][] { 703 {MockSimplePeriod.of(2, YEARS)}, 704 {MockSimplePeriod.of(2, MONTHS)}, 705 }; 706 } 707 708 @Test(dataProvider="badTemporalAmount",expectedExceptions=DateTimeException.class) 709 public void test_badPlusTemporalAmount(TemporalAmount amount) { 710 Instant inst = Instant.ofEpochMilli(1000); 711 inst.plus(amount); 712 } 713 714 //----------------------------------------------------------------------- 715 @DataProvider(name="Plus") 716 Object[][] provider_plus() { 717 return new Object[][] { 718 {MIN_SECOND, 0, -MIN_SECOND, 0, 0, 0}, 719 720 {MIN_SECOND, 0, 1, 0, MIN_SECOND + 1, 0}, 721 {MIN_SECOND, 0, 0, 500, MIN_SECOND, 500}, 722 {MIN_SECOND, 0, 0, 1000000000, MIN_SECOND + 1, 0}, 723 724 {MIN_SECOND + 1, 0, -1, 0, MIN_SECOND, 0}, 725 {MIN_SECOND + 1, 0, 0, -500, MIN_SECOND, 999999500}, 726 {MIN_SECOND + 1, 0, 0, -1000000000, MIN_SECOND, 0}, 727 728 {-4, 666666667, -4, 666666667, -7, 333333334}, 729 {-4, 666666667, -3, 0, -7, 666666667}, 730 {-4, 666666667, -2, 0, -6, 666666667}, 731 {-4, 666666667, -1, 0, -5, 666666667}, 732 {-4, 666666667, -1, 333333334, -4, 1}, 733 {-4, 666666667, -1, 666666667, -4, 333333334}, 734 {-4, 666666667, -1, 999999999, -4, 666666666}, 735 {-4, 666666667, 0, 0, -4, 666666667}, 736 {-4, 666666667, 0, 1, -4, 666666668}, 737 {-4, 666666667, 0, 333333333, -3, 0}, 738 {-4, 666666667, 0, 666666666, -3, 333333333}, 739 {-4, 666666667, 1, 0, -3, 666666667}, 740 {-4, 666666667, 2, 0, -2, 666666667}, 741 {-4, 666666667, 3, 0, -1, 666666667}, 742 {-4, 666666667, 3, 333333333, 0, 0}, 743 744 {-3, 0, -4, 666666667, -7, 666666667}, 745 {-3, 0, -3, 0, -6, 0}, 746 {-3, 0, -2, 0, -5, 0}, 747 {-3, 0, -1, 0, -4, 0}, 748 {-3, 0, -1, 333333334, -4, 333333334}, 749 {-3, 0, -1, 666666667, -4, 666666667}, 750 {-3, 0, -1, 999999999, -4, 999999999}, 751 {-3, 0, 0, 0, -3, 0}, 752 {-3, 0, 0, 1, -3, 1}, 753 {-3, 0, 0, 333333333, -3, 333333333}, 754 {-3, 0, 0, 666666666, -3, 666666666}, 755 {-3, 0, 1, 0, -2, 0}, 756 {-3, 0, 2, 0, -1, 0}, 757 {-3, 0, 3, 0, 0, 0}, 758 {-3, 0, 3, 333333333, 0, 333333333}, 759 760 {-2, 0, -4, 666666667, -6, 666666667}, 761 {-2, 0, -3, 0, -5, 0}, 762 {-2, 0, -2, 0, -4, 0}, 763 {-2, 0, -1, 0, -3, 0}, 764 {-2, 0, -1, 333333334, -3, 333333334}, 765 {-2, 0, -1, 666666667, -3, 666666667}, 766 {-2, 0, -1, 999999999, -3, 999999999}, 767 {-2, 0, 0, 0, -2, 0}, 768 {-2, 0, 0, 1, -2, 1}, 769 {-2, 0, 0, 333333333, -2, 333333333}, 770 {-2, 0, 0, 666666666, -2, 666666666}, 771 {-2, 0, 1, 0, -1, 0}, 772 {-2, 0, 2, 0, 0, 0}, 773 {-2, 0, 3, 0, 1, 0}, 774 {-2, 0, 3, 333333333, 1, 333333333}, 775 776 {-1, 0, -4, 666666667, -5, 666666667}, 777 {-1, 0, -3, 0, -4, 0}, 778 {-1, 0, -2, 0, -3, 0}, 779 {-1, 0, -1, 0, -2, 0}, 780 {-1, 0, -1, 333333334, -2, 333333334}, 781 {-1, 0, -1, 666666667, -2, 666666667}, 782 {-1, 0, -1, 999999999, -2, 999999999}, 783 {-1, 0, 0, 0, -1, 0}, 784 {-1, 0, 0, 1, -1, 1}, 785 {-1, 0, 0, 333333333, -1, 333333333}, 786 {-1, 0, 0, 666666666, -1, 666666666}, 787 {-1, 0, 1, 0, 0, 0}, 788 {-1, 0, 2, 0, 1, 0}, 789 {-1, 0, 3, 0, 2, 0}, 790 {-1, 0, 3, 333333333, 2, 333333333}, 791 792 {-1, 666666667, -4, 666666667, -4, 333333334}, 793 {-1, 666666667, -3, 0, -4, 666666667}, 794 {-1, 666666667, -2, 0, -3, 666666667}, 795 {-1, 666666667, -1, 0, -2, 666666667}, 796 {-1, 666666667, -1, 333333334, -1, 1}, 797 {-1, 666666667, -1, 666666667, -1, 333333334}, 798 {-1, 666666667, -1, 999999999, -1, 666666666}, 799 {-1, 666666667, 0, 0, -1, 666666667}, 800 {-1, 666666667, 0, 1, -1, 666666668}, 801 {-1, 666666667, 0, 333333333, 0, 0}, 802 {-1, 666666667, 0, 666666666, 0, 333333333}, 803 {-1, 666666667, 1, 0, 0, 666666667}, 804 {-1, 666666667, 2, 0, 1, 666666667}, 805 {-1, 666666667, 3, 0, 2, 666666667}, 806 {-1, 666666667, 3, 333333333, 3, 0}, 807 808 {0, 0, -4, 666666667, -4, 666666667}, 809 {0, 0, -3, 0, -3, 0}, 810 {0, 0, -2, 0, -2, 0}, 811 {0, 0, -1, 0, -1, 0}, 812 {0, 0, -1, 333333334, -1, 333333334}, 813 {0, 0, -1, 666666667, -1, 666666667}, 814 {0, 0, -1, 999999999, -1, 999999999}, 815 {0, 0, 0, 0, 0, 0}, 816 {0, 0, 0, 1, 0, 1}, 817 {0, 0, 0, 333333333, 0, 333333333}, 818 {0, 0, 0, 666666666, 0, 666666666}, 819 {0, 0, 1, 0, 1, 0}, 820 {0, 0, 2, 0, 2, 0}, 821 {0, 0, 3, 0, 3, 0}, 822 {0, 0, 3, 333333333, 3, 333333333}, 823 824 {0, 333333333, -4, 666666667, -3, 0}, 825 {0, 333333333, -3, 0, -3, 333333333}, 826 {0, 333333333, -2, 0, -2, 333333333}, 827 {0, 333333333, -1, 0, -1, 333333333}, 828 {0, 333333333, -1, 333333334, -1, 666666667}, 829 {0, 333333333, -1, 666666667, 0, 0}, 830 {0, 333333333, -1, 999999999, 0, 333333332}, 831 {0, 333333333, 0, 0, 0, 333333333}, 832 {0, 333333333, 0, 1, 0, 333333334}, 833 {0, 333333333, 0, 333333333, 0, 666666666}, 834 {0, 333333333, 0, 666666666, 0, 999999999}, 835 {0, 333333333, 1, 0, 1, 333333333}, 836 {0, 333333333, 2, 0, 2, 333333333}, 837 {0, 333333333, 3, 0, 3, 333333333}, 838 {0, 333333333, 3, 333333333, 3, 666666666}, 839 840 {1, 0, -4, 666666667, -3, 666666667}, 841 {1, 0, -3, 0, -2, 0}, 842 {1, 0, -2, 0, -1, 0}, 843 {1, 0, -1, 0, 0, 0}, 844 {1, 0, -1, 333333334, 0, 333333334}, 845 {1, 0, -1, 666666667, 0, 666666667}, 846 {1, 0, -1, 999999999, 0, 999999999}, 847 {1, 0, 0, 0, 1, 0}, 848 {1, 0, 0, 1, 1, 1}, 849 {1, 0, 0, 333333333, 1, 333333333}, 850 {1, 0, 0, 666666666, 1, 666666666}, 851 {1, 0, 1, 0, 2, 0}, 852 {1, 0, 2, 0, 3, 0}, 853 {1, 0, 3, 0, 4, 0}, 854 {1, 0, 3, 333333333, 4, 333333333}, 855 856 {2, 0, -4, 666666667, -2, 666666667}, 857 {2, 0, -3, 0, -1, 0}, 858 {2, 0, -2, 0, 0, 0}, 859 {2, 0, -1, 0, 1, 0}, 860 {2, 0, -1, 333333334, 1, 333333334}, 861 {2, 0, -1, 666666667, 1, 666666667}, 862 {2, 0, -1, 999999999, 1, 999999999}, 863 {2, 0, 0, 0, 2, 0}, 864 {2, 0, 0, 1, 2, 1}, 865 {2, 0, 0, 333333333, 2, 333333333}, 866 {2, 0, 0, 666666666, 2, 666666666}, 867 {2, 0, 1, 0, 3, 0}, 868 {2, 0, 2, 0, 4, 0}, 869 {2, 0, 3, 0, 5, 0}, 870 {2, 0, 3, 333333333, 5, 333333333}, 871 872 {3, 0, -4, 666666667, -1, 666666667}, 873 {3, 0, -3, 0, 0, 0}, 874 {3, 0, -2, 0, 1, 0}, 875 {3, 0, -1, 0, 2, 0}, 876 {3, 0, -1, 333333334, 2, 333333334}, 877 {3, 0, -1, 666666667, 2, 666666667}, 878 {3, 0, -1, 999999999, 2, 999999999}, 879 {3, 0, 0, 0, 3, 0}, 880 {3, 0, 0, 1, 3, 1}, 881 {3, 0, 0, 333333333, 3, 333333333}, 882 {3, 0, 0, 666666666, 3, 666666666}, 883 {3, 0, 1, 0, 4, 0}, 884 {3, 0, 2, 0, 5, 0}, 885 {3, 0, 3, 0, 6, 0}, 886 {3, 0, 3, 333333333, 6, 333333333}, 887 888 {3, 333333333, -4, 666666667, 0, 0}, 889 {3, 333333333, -3, 0, 0, 333333333}, 890 {3, 333333333, -2, 0, 1, 333333333}, 891 {3, 333333333, -1, 0, 2, 333333333}, 892 {3, 333333333, -1, 333333334, 2, 666666667}, 893 {3, 333333333, -1, 666666667, 3, 0}, 894 {3, 333333333, -1, 999999999, 3, 333333332}, 895 {3, 333333333, 0, 0, 3, 333333333}, 896 {3, 333333333, 0, 1, 3, 333333334}, 897 {3, 333333333, 0, 333333333, 3, 666666666}, 898 {3, 333333333, 0, 666666666, 3, 999999999}, 899 {3, 333333333, 1, 0, 4, 333333333}, 900 {3, 333333333, 2, 0, 5, 333333333}, 901 {3, 333333333, 3, 0, 6, 333333333}, 902 {3, 333333333, 3, 333333333, 6, 666666666}, 903 904 {MAX_SECOND - 1, 0, 1, 0, MAX_SECOND, 0}, 905 {MAX_SECOND - 1, 0, 0, 500, MAX_SECOND - 1, 500}, 906 {MAX_SECOND - 1, 0, 0, 1000000000, MAX_SECOND, 0}, 907 908 {MAX_SECOND, 0, -1, 0, MAX_SECOND - 1, 0}, 909 {MAX_SECOND, 0, 0, -500, MAX_SECOND - 1, 999999500}, 910 {MAX_SECOND, 0, 0, -1000000000, MAX_SECOND - 1, 0}, 911 912 {MAX_SECOND, 0, -MAX_SECOND, 0, 0, 0}, 913 }; 914 } 915 916 @Test(dataProvider="Plus") 917 public void plus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 918 Instant i = Instant.ofEpochSecond(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos)); 919 assertEquals(i.getEpochSecond(), expectedSeconds); 920 assertEquals(i.getNano(), expectedNanoOfSecond); 921 } 922 923 @Test(expectedExceptions=DateTimeException.class) 924 public void plus_Duration_overflowTooBig() { 925 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 926 i.plus(Duration.ofSeconds(0, 1)); 927 } 928 929 @Test(expectedExceptions=DateTimeException.class) 930 public void plus_Duration_overflowTooSmall() { 931 Instant i = Instant.ofEpochSecond(MIN_SECOND); 932 i.plus(Duration.ofSeconds(-1, 999999999)); 933 } 934 935 //-----------------------------------------------------------------------a 936 @Test(dataProvider="Plus") 937 public void plus_longTemporalUnit(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 938 Instant i = Instant.ofEpochSecond(seconds, nanos).plus(otherSeconds, SECONDS).plus(otherNanos, NANOS); 939 assertEquals(i.getEpochSecond(), expectedSeconds); 940 assertEquals(i.getNano(), expectedNanoOfSecond); 941 } 942 943 @Test(expectedExceptions=DateTimeException.class) 944 public void plus_longTemporalUnit_overflowTooBig() { 945 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 946 i.plus(1, NANOS); 947 } 948 949 @Test(expectedExceptions=DateTimeException.class) 950 public void plus_longTemporalUnit_overflowTooSmall() { 951 Instant i = Instant.ofEpochSecond(MIN_SECOND); 952 i.plus(999999999, NANOS); 953 i.plus(-1, SECONDS); 954 } 955 956 //----------------------------------------------------------------------- 957 @DataProvider(name="PlusSeconds") 958 Object[][] provider_plusSeconds_long() { 959 return new Object[][] { 960 {0, 0, 0, 0, 0}, 961 {0, 0, 1, 1, 0}, 962 {0, 0, -1, -1, 0}, 963 {0, 0, MAX_SECOND, MAX_SECOND, 0}, 964 {0, 0, MIN_SECOND, MIN_SECOND, 0}, 965 {1, 0, 0, 1, 0}, 966 {1, 0, 1, 2, 0}, 967 {1, 0, -1, 0, 0}, 968 {1, 0, MAX_SECOND - 1, MAX_SECOND, 0}, 969 {1, 0, MIN_SECOND, MIN_SECOND + 1, 0}, 970 {1, 1, 0, 1, 1}, 971 {1, 1, 1, 2, 1}, 972 {1, 1, -1, 0, 1}, 973 {1, 1, MAX_SECOND - 1, MAX_SECOND, 1}, 974 {1, 1, MIN_SECOND, MIN_SECOND + 1, 1}, 975 {-1, 1, 0, -1, 1}, 976 {-1, 1, 1, 0, 1}, 977 {-1, 1, -1, -2, 1}, 978 {-1, 1, MAX_SECOND, MAX_SECOND - 1, 1}, 979 {-1, 1, MIN_SECOND + 1, MIN_SECOND, 1}, 980 981 {MAX_SECOND, 2, -MAX_SECOND, 0, 2}, 982 {MIN_SECOND, 2, -MIN_SECOND, 0, 2}, 983 }; 984 } 985 986 @Test(dataProvider="PlusSeconds") 987 public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 988 Instant t = Instant.ofEpochSecond(seconds, nanos); 989 t = t.plusSeconds(amount); 990 assertEquals(t.getEpochSecond(), expectedSeconds); 991 assertEquals(t.getNano(), expectedNanoOfSecond); 992 } 993 994 @Test(expectedExceptions=ArithmeticException.class) 995 public void plusSeconds_long_overflowTooBig() { 996 Instant t = Instant.ofEpochSecond(1, 0); 997 t.plusSeconds(Long.MAX_VALUE); 998 } 999 1000 @Test(expectedExceptions=ArithmeticException.class) 1001 public void plusSeconds_long_overflowTooSmall() { 1002 Instant t = Instant.ofEpochSecond(-1, 0); 1003 t.plusSeconds(Long.MIN_VALUE); 1004 } 1005 1006 //----------------------------------------------------------------------- 1007 @DataProvider(name="PlusMillis") 1008 Object[][] provider_plusMillis_long() { 1009 return new Object[][] { 1010 {0, 0, 0, 0, 0}, 1011 {0, 0, 1, 0, 1000000}, 1012 {0, 0, 999, 0, 999000000}, 1013 {0, 0, 1000, 1, 0}, 1014 {0, 0, 1001, 1, 1000000}, 1015 {0, 0, 1999, 1, 999000000}, 1016 {0, 0, 2000, 2, 0}, 1017 {0, 0, -1, -1, 999000000}, 1018 {0, 0, -999, -1, 1000000}, 1019 {0, 0, -1000, -1, 0}, 1020 {0, 0, -1001, -2, 999000000}, 1021 {0, 0, -1999, -2, 1000000}, 1022 1023 {0, 1, 0, 0, 1}, 1024 {0, 1, 1, 0, 1000001}, 1025 {0, 1, 998, 0, 998000001}, 1026 {0, 1, 999, 0, 999000001}, 1027 {0, 1, 1000, 1, 1}, 1028 {0, 1, 1998, 1, 998000001}, 1029 {0, 1, 1999, 1, 999000001}, 1030 {0, 1, 2000, 2, 1}, 1031 {0, 1, -1, -1, 999000001}, 1032 {0, 1, -2, -1, 998000001}, 1033 {0, 1, -1000, -1, 1}, 1034 {0, 1, -1001, -2, 999000001}, 1035 1036 {0, 1000000, 0, 0, 1000000}, 1037 {0, 1000000, 1, 0, 2000000}, 1038 {0, 1000000, 998, 0, 999000000}, 1039 {0, 1000000, 999, 1, 0}, 1040 {0, 1000000, 1000, 1, 1000000}, 1041 {0, 1000000, 1998, 1, 999000000}, 1042 {0, 1000000, 1999, 2, 0}, 1043 {0, 1000000, 2000, 2, 1000000}, 1044 {0, 1000000, -1, 0, 0}, 1045 {0, 1000000, -2, -1, 999000000}, 1046 {0, 1000000, -999, -1, 2000000}, 1047 {0, 1000000, -1000, -1, 1000000}, 1048 {0, 1000000, -1001, -1, 0}, 1049 {0, 1000000, -1002, -2, 999000000}, 1050 1051 {0, 999999999, 0, 0, 999999999}, 1052 {0, 999999999, 1, 1, 999999}, 1053 {0, 999999999, 999, 1, 998999999}, 1054 {0, 999999999, 1000, 1, 999999999}, 1055 {0, 999999999, 1001, 2, 999999}, 1056 {0, 999999999, -1, 0, 998999999}, 1057 {0, 999999999, -1000, -1, 999999999}, 1058 {0, 999999999, -1001, -1, 998999999}, 1059 1060 {0, 0, Long.MAX_VALUE, Long.MAX_VALUE / 1000, (int) (Long.MAX_VALUE % 1000) * 1000000}, 1061 {0, 0, Long.MIN_VALUE, Long.MIN_VALUE / 1000 - 1, (int) (Long.MIN_VALUE % 1000) * 1000000 + 1000000000}, 1062 }; 1063 } 1064 1065 @Test(dataProvider="PlusMillis") 1066 public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1067 Instant t = Instant.ofEpochSecond(seconds, nanos); 1068 t = t.plusMillis(amount); 1069 assertEquals(t.getEpochSecond(), expectedSeconds); 1070 assertEquals(t.getNano(), expectedNanoOfSecond); 1071 } 1072 @Test(dataProvider="PlusMillis") 1073 public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1074 Instant t = Instant.ofEpochSecond(seconds + 1, nanos); 1075 t = t.plusMillis(amount); 1076 assertEquals(t.getEpochSecond(), expectedSeconds + 1); 1077 assertEquals(t.getNano(), expectedNanoOfSecond); 1078 } 1079 @Test(dataProvider="PlusMillis") 1080 public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1081 Instant t = Instant.ofEpochSecond(seconds - 1, nanos); 1082 t = t.plusMillis(amount); 1083 assertEquals(t.getEpochSecond(), expectedSeconds - 1); 1084 assertEquals(t.getNano(), expectedNanoOfSecond); 1085 } 1086 1087 @Test 1088 public void plusMillis_long_max() { 1089 Instant t = Instant.ofEpochSecond(MAX_SECOND, 998999999); 1090 t = t.plusMillis(1); 1091 assertEquals(t.getEpochSecond(), MAX_SECOND); 1092 assertEquals(t.getNano(), 999999999); 1093 } 1094 1095 @Test(expectedExceptions=DateTimeException.class) 1096 public void plusMillis_long_overflowTooBig() { 1097 Instant t = Instant.ofEpochSecond(MAX_SECOND, 999000000); 1098 t.plusMillis(1); 1099 } 1100 1101 @Test 1102 public void plusMillis_long_min() { 1103 Instant t = Instant.ofEpochSecond(MIN_SECOND, 1000000); 1104 t = t.plusMillis(-1); 1105 assertEquals(t.getEpochSecond(), MIN_SECOND); 1106 assertEquals(t.getNano(), 0); 1107 } 1108 1109 @Test(expectedExceptions=DateTimeException.class) 1110 public void plusMillis_long_overflowTooSmall() { 1111 Instant t = Instant.ofEpochSecond(MIN_SECOND, 0); 1112 t.plusMillis(-1); 1113 } 1114 1115 //----------------------------------------------------------------------- 1116 @DataProvider(name="PlusNanos") 1117 Object[][] provider_plusNanos_long() { 1118 return new Object[][] { 1119 {0, 0, 0, 0, 0}, 1120 {0, 0, 1, 0, 1}, 1121 {0, 0, 999999999, 0, 999999999}, 1122 {0, 0, 1000000000, 1, 0}, 1123 {0, 0, 1000000001, 1, 1}, 1124 {0, 0, 1999999999, 1, 999999999}, 1125 {0, 0, 2000000000, 2, 0}, 1126 {0, 0, -1, -1, 999999999}, 1127 {0, 0, -999999999, -1, 1}, 1128 {0, 0, -1000000000, -1, 0}, 1129 {0, 0, -1000000001, -2, 999999999}, 1130 {0, 0, -1999999999, -2, 1}, 1131 1132 {1, 0, 0, 1, 0}, 1133 {1, 0, 1, 1, 1}, 1134 {1, 0, 999999999, 1, 999999999}, 1135 {1, 0, 1000000000, 2, 0}, 1136 {1, 0, 1000000001, 2, 1}, 1137 {1, 0, 1999999999, 2, 999999999}, 1138 {1, 0, 2000000000, 3, 0}, 1139 {1, 0, -1, 0, 999999999}, 1140 {1, 0, -999999999, 0, 1}, 1141 {1, 0, -1000000000, 0, 0}, 1142 {1, 0, -1000000001, -1, 999999999}, 1143 {1, 0, -1999999999, -1, 1}, 1144 1145 {-1, 0, 0, -1, 0}, 1146 {-1, 0, 1, -1, 1}, 1147 {-1, 0, 999999999, -1, 999999999}, 1148 {-1, 0, 1000000000, 0, 0}, 1149 {-1, 0, 1000000001, 0, 1}, 1150 {-1, 0, 1999999999, 0, 999999999}, 1151 {-1, 0, 2000000000, 1, 0}, 1152 {-1, 0, -1, -2, 999999999}, 1153 {-1, 0, -999999999, -2, 1}, 1154 {-1, 0, -1000000000, -2, 0}, 1155 {-1, 0, -1000000001, -3, 999999999}, 1156 {-1, 0, -1999999999, -3, 1}, 1157 1158 {1, 1, 0, 1, 1}, 1159 {1, 1, 1, 1, 2}, 1160 {1, 1, 999999998, 1, 999999999}, 1161 {1, 1, 999999999, 2, 0}, 1162 {1, 1, 1000000000, 2, 1}, 1163 {1, 1, 1999999998, 2, 999999999}, 1164 {1, 1, 1999999999, 3, 0}, 1165 {1, 1, 2000000000, 3, 1}, 1166 {1, 1, -1, 1, 0}, 1167 {1, 1, -2, 0, 999999999}, 1168 {1, 1, -1000000000, 0, 1}, 1169 {1, 1, -1000000001, 0, 0}, 1170 {1, 1, -1000000002, -1, 999999999}, 1171 {1, 1, -2000000000, -1, 1}, 1172 1173 {1, 999999999, 0, 1, 999999999}, 1174 {1, 999999999, 1, 2, 0}, 1175 {1, 999999999, 999999999, 2, 999999998}, 1176 {1, 999999999, 1000000000, 2, 999999999}, 1177 {1, 999999999, 1000000001, 3, 0}, 1178 {1, 999999999, -1, 1, 999999998}, 1179 {1, 999999999, -1000000000, 0, 999999999}, 1180 {1, 999999999, -1000000001, 0, 999999998}, 1181 {1, 999999999, -1999999999, 0, 0}, 1182 {1, 999999999, -2000000000, -1, 999999999}, 1183 1184 {MAX_SECOND, 0, 999999999, MAX_SECOND, 999999999}, 1185 {MAX_SECOND - 1, 0, 1999999999, MAX_SECOND, 999999999}, 1186 {MIN_SECOND, 1, -1, MIN_SECOND, 0}, 1187 {MIN_SECOND + 1, 1, -1000000001, MIN_SECOND, 0}, 1188 1189 {0, 0, MAX_SECOND, MAX_SECOND / 1000000000, (int) (MAX_SECOND % 1000000000)}, 1190 {0, 0, MIN_SECOND, MIN_SECOND / 1000000000 - 1, (int) (MIN_SECOND % 1000000000) + 1000000000}, 1191 }; 1192 } 1193 1194 @Test(dataProvider="PlusNanos") 1195 public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1196 Instant t = Instant.ofEpochSecond(seconds, nanos); 1197 t = t.plusNanos(amount); 1198 assertEquals(t.getEpochSecond(), expectedSeconds); 1199 assertEquals(t.getNano(), expectedNanoOfSecond); 1200 } 1201 1202 @Test(expectedExceptions=DateTimeException.class) 1203 public void plusNanos_long_overflowTooBig() { 1204 Instant t = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1205 t.plusNanos(1); 1206 } 1207 1208 @Test(expectedExceptions=DateTimeException.class) 1209 public void plusNanos_long_overflowTooSmall() { 1210 Instant t = Instant.ofEpochSecond(MIN_SECOND, 0); 1211 t.plusNanos(-1); 1212 } 1213 1214 //----------------------------------------------------------------------- 1215 @DataProvider(name="Minus") 1216 Object[][] provider_minus() { 1217 return new Object[][] { 1218 {MIN_SECOND, 0, MIN_SECOND, 0, 0, 0}, 1219 1220 {MIN_SECOND, 0, -1, 0, MIN_SECOND + 1, 0}, 1221 {MIN_SECOND, 0, 0, -500, MIN_SECOND, 500}, 1222 {MIN_SECOND, 0, 0, -1000000000, MIN_SECOND + 1, 0}, 1223 1224 {MIN_SECOND + 1, 0, 1, 0, MIN_SECOND, 0}, 1225 {MIN_SECOND + 1, 0, 0, 500, MIN_SECOND, 999999500}, 1226 {MIN_SECOND + 1, 0, 0, 1000000000, MIN_SECOND, 0}, 1227 1228 {-4, 666666667, -4, 666666667, 0, 0}, 1229 {-4, 666666667, -3, 0, -1, 666666667}, 1230 {-4, 666666667, -2, 0, -2, 666666667}, 1231 {-4, 666666667, -1, 0, -3, 666666667}, 1232 {-4, 666666667, -1, 333333334, -3, 333333333}, 1233 {-4, 666666667, -1, 666666667, -3, 0}, 1234 {-4, 666666667, -1, 999999999, -4, 666666668}, 1235 {-4, 666666667, 0, 0, -4, 666666667}, 1236 {-4, 666666667, 0, 1, -4, 666666666}, 1237 {-4, 666666667, 0, 333333333, -4, 333333334}, 1238 {-4, 666666667, 0, 666666666, -4, 1}, 1239 {-4, 666666667, 1, 0, -5, 666666667}, 1240 {-4, 666666667, 2, 0, -6, 666666667}, 1241 {-4, 666666667, 3, 0, -7, 666666667}, 1242 {-4, 666666667, 3, 333333333, -7, 333333334}, 1243 1244 {-3, 0, -4, 666666667, 0, 333333333}, 1245 {-3, 0, -3, 0, 0, 0}, 1246 {-3, 0, -2, 0, -1, 0}, 1247 {-3, 0, -1, 0, -2, 0}, 1248 {-3, 0, -1, 333333334, -3, 666666666}, 1249 {-3, 0, -1, 666666667, -3, 333333333}, 1250 {-3, 0, -1, 999999999, -3, 1}, 1251 {-3, 0, 0, 0, -3, 0}, 1252 {-3, 0, 0, 1, -4, 999999999}, 1253 {-3, 0, 0, 333333333, -4, 666666667}, 1254 {-3, 0, 0, 666666666, -4, 333333334}, 1255 {-3, 0, 1, 0, -4, 0}, 1256 {-3, 0, 2, 0, -5, 0}, 1257 {-3, 0, 3, 0, -6, 0}, 1258 {-3, 0, 3, 333333333, -7, 666666667}, 1259 1260 {-2, 0, -4, 666666667, 1, 333333333}, 1261 {-2, 0, -3, 0, 1, 0}, 1262 {-2, 0, -2, 0, 0, 0}, 1263 {-2, 0, -1, 0, -1, 0}, 1264 {-2, 0, -1, 333333334, -2, 666666666}, 1265 {-2, 0, -1, 666666667, -2, 333333333}, 1266 {-2, 0, -1, 999999999, -2, 1}, 1267 {-2, 0, 0, 0, -2, 0}, 1268 {-2, 0, 0, 1, -3, 999999999}, 1269 {-2, 0, 0, 333333333, -3, 666666667}, 1270 {-2, 0, 0, 666666666, -3, 333333334}, 1271 {-2, 0, 1, 0, -3, 0}, 1272 {-2, 0, 2, 0, -4, 0}, 1273 {-2, 0, 3, 0, -5, 0}, 1274 {-2, 0, 3, 333333333, -6, 666666667}, 1275 1276 {-1, 0, -4, 666666667, 2, 333333333}, 1277 {-1, 0, -3, 0, 2, 0}, 1278 {-1, 0, -2, 0, 1, 0}, 1279 {-1, 0, -1, 0, 0, 0}, 1280 {-1, 0, -1, 333333334, -1, 666666666}, 1281 {-1, 0, -1, 666666667, -1, 333333333}, 1282 {-1, 0, -1, 999999999, -1, 1}, 1283 {-1, 0, 0, 0, -1, 0}, 1284 {-1, 0, 0, 1, -2, 999999999}, 1285 {-1, 0, 0, 333333333, -2, 666666667}, 1286 {-1, 0, 0, 666666666, -2, 333333334}, 1287 {-1, 0, 1, 0, -2, 0}, 1288 {-1, 0, 2, 0, -3, 0}, 1289 {-1, 0, 3, 0, -4, 0}, 1290 {-1, 0, 3, 333333333, -5, 666666667}, 1291 1292 {-1, 666666667, -4, 666666667, 3, 0}, 1293 {-1, 666666667, -3, 0, 2, 666666667}, 1294 {-1, 666666667, -2, 0, 1, 666666667}, 1295 {-1, 666666667, -1, 0, 0, 666666667}, 1296 {-1, 666666667, -1, 333333334, 0, 333333333}, 1297 {-1, 666666667, -1, 666666667, 0, 0}, 1298 {-1, 666666667, -1, 999999999, -1, 666666668}, 1299 {-1, 666666667, 0, 0, -1, 666666667}, 1300 {-1, 666666667, 0, 1, -1, 666666666}, 1301 {-1, 666666667, 0, 333333333, -1, 333333334}, 1302 {-1, 666666667, 0, 666666666, -1, 1}, 1303 {-1, 666666667, 1, 0, -2, 666666667}, 1304 {-1, 666666667, 2, 0, -3, 666666667}, 1305 {-1, 666666667, 3, 0, -4, 666666667}, 1306 {-1, 666666667, 3, 333333333, -4, 333333334}, 1307 1308 {0, 0, -4, 666666667, 3, 333333333}, 1309 {0, 0, -3, 0, 3, 0}, 1310 {0, 0, -2, 0, 2, 0}, 1311 {0, 0, -1, 0, 1, 0}, 1312 {0, 0, -1, 333333334, 0, 666666666}, 1313 {0, 0, -1, 666666667, 0, 333333333}, 1314 {0, 0, -1, 999999999, 0, 1}, 1315 {0, 0, 0, 0, 0, 0}, 1316 {0, 0, 0, 1, -1, 999999999}, 1317 {0, 0, 0, 333333333, -1, 666666667}, 1318 {0, 0, 0, 666666666, -1, 333333334}, 1319 {0, 0, 1, 0, -1, 0}, 1320 {0, 0, 2, 0, -2, 0}, 1321 {0, 0, 3, 0, -3, 0}, 1322 {0, 0, 3, 333333333, -4, 666666667}, 1323 1324 {0, 333333333, -4, 666666667, 3, 666666666}, 1325 {0, 333333333, -3, 0, 3, 333333333}, 1326 {0, 333333333, -2, 0, 2, 333333333}, 1327 {0, 333333333, -1, 0, 1, 333333333}, 1328 {0, 333333333, -1, 333333334, 0, 999999999}, 1329 {0, 333333333, -1, 666666667, 0, 666666666}, 1330 {0, 333333333, -1, 999999999, 0, 333333334}, 1331 {0, 333333333, 0, 0, 0, 333333333}, 1332 {0, 333333333, 0, 1, 0, 333333332}, 1333 {0, 333333333, 0, 333333333, 0, 0}, 1334 {0, 333333333, 0, 666666666, -1, 666666667}, 1335 {0, 333333333, 1, 0, -1, 333333333}, 1336 {0, 333333333, 2, 0, -2, 333333333}, 1337 {0, 333333333, 3, 0, -3, 333333333}, 1338 {0, 333333333, 3, 333333333, -3, 0}, 1339 1340 {1, 0, -4, 666666667, 4, 333333333}, 1341 {1, 0, -3, 0, 4, 0}, 1342 {1, 0, -2, 0, 3, 0}, 1343 {1, 0, -1, 0, 2, 0}, 1344 {1, 0, -1, 333333334, 1, 666666666}, 1345 {1, 0, -1, 666666667, 1, 333333333}, 1346 {1, 0, -1, 999999999, 1, 1}, 1347 {1, 0, 0, 0, 1, 0}, 1348 {1, 0, 0, 1, 0, 999999999}, 1349 {1, 0, 0, 333333333, 0, 666666667}, 1350 {1, 0, 0, 666666666, 0, 333333334}, 1351 {1, 0, 1, 0, 0, 0}, 1352 {1, 0, 2, 0, -1, 0}, 1353 {1, 0, 3, 0, -2, 0}, 1354 {1, 0, 3, 333333333, -3, 666666667}, 1355 1356 {2, 0, -4, 666666667, 5, 333333333}, 1357 {2, 0, -3, 0, 5, 0}, 1358 {2, 0, -2, 0, 4, 0}, 1359 {2, 0, -1, 0, 3, 0}, 1360 {2, 0, -1, 333333334, 2, 666666666}, 1361 {2, 0, -1, 666666667, 2, 333333333}, 1362 {2, 0, -1, 999999999, 2, 1}, 1363 {2, 0, 0, 0, 2, 0}, 1364 {2, 0, 0, 1, 1, 999999999}, 1365 {2, 0, 0, 333333333, 1, 666666667}, 1366 {2, 0, 0, 666666666, 1, 333333334}, 1367 {2, 0, 1, 0, 1, 0}, 1368 {2, 0, 2, 0, 0, 0}, 1369 {2, 0, 3, 0, -1, 0}, 1370 {2, 0, 3, 333333333, -2, 666666667}, 1371 1372 {3, 0, -4, 666666667, 6, 333333333}, 1373 {3, 0, -3, 0, 6, 0}, 1374 {3, 0, -2, 0, 5, 0}, 1375 {3, 0, -1, 0, 4, 0}, 1376 {3, 0, -1, 333333334, 3, 666666666}, 1377 {3, 0, -1, 666666667, 3, 333333333}, 1378 {3, 0, -1, 999999999, 3, 1}, 1379 {3, 0, 0, 0, 3, 0}, 1380 {3, 0, 0, 1, 2, 999999999}, 1381 {3, 0, 0, 333333333, 2, 666666667}, 1382 {3, 0, 0, 666666666, 2, 333333334}, 1383 {3, 0, 1, 0, 2, 0}, 1384 {3, 0, 2, 0, 1, 0}, 1385 {3, 0, 3, 0, 0, 0}, 1386 {3, 0, 3, 333333333, -1, 666666667}, 1387 1388 {3, 333333333, -4, 666666667, 6, 666666666}, 1389 {3, 333333333, -3, 0, 6, 333333333}, 1390 {3, 333333333, -2, 0, 5, 333333333}, 1391 {3, 333333333, -1, 0, 4, 333333333}, 1392 {3, 333333333, -1, 333333334, 3, 999999999}, 1393 {3, 333333333, -1, 666666667, 3, 666666666}, 1394 {3, 333333333, -1, 999999999, 3, 333333334}, 1395 {3, 333333333, 0, 0, 3, 333333333}, 1396 {3, 333333333, 0, 1, 3, 333333332}, 1397 {3, 333333333, 0, 333333333, 3, 0}, 1398 {3, 333333333, 0, 666666666, 2, 666666667}, 1399 {3, 333333333, 1, 0, 2, 333333333}, 1400 {3, 333333333, 2, 0, 1, 333333333}, 1401 {3, 333333333, 3, 0, 0, 333333333}, 1402 {3, 333333333, 3, 333333333, 0, 0}, 1403 1404 {MAX_SECOND - 1, 0, -1, 0, MAX_SECOND, 0}, 1405 {MAX_SECOND - 1, 0, 0, -500, MAX_SECOND - 1, 500}, 1406 {MAX_SECOND - 1, 0, 0, -1000000000, MAX_SECOND, 0}, 1407 1408 {MAX_SECOND, 0, 1, 0, MAX_SECOND - 1, 0}, 1409 {MAX_SECOND, 0, 0, 500, MAX_SECOND - 1, 999999500}, 1410 {MAX_SECOND, 0, 0, 1000000000, MAX_SECOND - 1, 0}, 1411 1412 {MAX_SECOND, 0, MAX_SECOND, 0, 0, 0}, 1413 }; 1414 } 1415 1416 @Test(dataProvider="Minus") 1417 public void minus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1418 Instant i = Instant.ofEpochSecond(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos)); 1419 assertEquals(i.getEpochSecond(), expectedSeconds); 1420 assertEquals(i.getNano(), expectedNanoOfSecond); 1421 } 1422 1423 @Test(expectedExceptions=DateTimeException.class) 1424 public void minus_Duration_overflowTooSmall() { 1425 Instant i = Instant.ofEpochSecond(MIN_SECOND); 1426 i.minus(Duration.ofSeconds(0, 1)); 1427 } 1428 1429 @Test(expectedExceptions=DateTimeException.class) 1430 public void minus_Duration_overflowTooBig() { 1431 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1432 i.minus(Duration.ofSeconds(-1, 999999999)); 1433 } 1434 1435 //----------------------------------------------------------------------- 1436 @Test(dataProvider="Minus") 1437 public void minus_longTemporalUnit(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1438 Instant i = Instant.ofEpochSecond(seconds, nanos).minus(otherSeconds, SECONDS).minus(otherNanos, NANOS); 1439 assertEquals(i.getEpochSecond(), expectedSeconds); 1440 assertEquals(i.getNano(), expectedNanoOfSecond); 1441 } 1442 1443 @Test(expectedExceptions=DateTimeException.class) 1444 public void minus_longTemporalUnit_overflowTooSmall() { 1445 Instant i = Instant.ofEpochSecond(MIN_SECOND); 1446 i.minus(1, NANOS); 1447 } 1448 1449 @Test(expectedExceptions=DateTimeException.class) 1450 public void minus_longTemporalUnit_overflowTooBig() { 1451 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1452 i.minus(999999999, NANOS); 1453 i.minus(-1, SECONDS); 1454 } 1455 1456 //----------------------------------------------------------------------- 1457 @DataProvider(name="MinusSeconds") 1458 Object[][] provider_minusSeconds_long() { 1459 return new Object[][] { 1460 {0, 0, 0, 0, 0}, 1461 {0, 0, 1, -1, 0}, 1462 {0, 0, -1, 1, 0}, 1463 {0, 0, -MIN_SECOND, MIN_SECOND, 0}, 1464 {1, 0, 0, 1, 0}, 1465 {1, 0, 1, 0, 0}, 1466 {1, 0, -1, 2, 0}, 1467 {1, 0, -MIN_SECOND + 1, MIN_SECOND, 0}, 1468 {1, 1, 0, 1, 1}, 1469 {1, 1, 1, 0, 1}, 1470 {1, 1, -1, 2, 1}, 1471 {1, 1, -MIN_SECOND, MIN_SECOND + 1, 1}, 1472 {1, 1, -MIN_SECOND + 1, MIN_SECOND, 1}, 1473 {-1, 1, 0, -1, 1}, 1474 {-1, 1, 1, -2, 1}, 1475 {-1, 1, -1, 0, 1}, 1476 {-1, 1, -MAX_SECOND, MAX_SECOND - 1, 1}, 1477 {-1, 1, -(MAX_SECOND + 1), MAX_SECOND, 1}, 1478 1479 {MIN_SECOND, 2, MIN_SECOND, 0, 2}, 1480 {MIN_SECOND + 1, 2, MIN_SECOND, 1, 2}, 1481 {MAX_SECOND - 1, 2, MAX_SECOND, -1, 2}, 1482 {MAX_SECOND, 2, MAX_SECOND, 0, 2}, 1483 }; 1484 } 1485 1486 @Test(dataProvider="MinusSeconds") 1487 public void minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1488 Instant i = Instant.ofEpochSecond(seconds, nanos); 1489 i = i.minusSeconds(amount); 1490 assertEquals(i.getEpochSecond(), expectedSeconds); 1491 assertEquals(i.getNano(), expectedNanoOfSecond); 1492 } 1493 1494 @Test(expectedExceptions = {ArithmeticException.class}) 1495 public void minusSeconds_long_overflowTooBig() { 1496 Instant i = Instant.ofEpochSecond(1, 0); 1497 i.minusSeconds(Long.MIN_VALUE + 1); 1498 } 1499 1500 @Test(expectedExceptions = {ArithmeticException.class}) 1501 public void minusSeconds_long_overflowTooSmall() { 1502 Instant i = Instant.ofEpochSecond(-2, 0); 1503 i.minusSeconds(Long.MAX_VALUE); 1504 } 1505 1506 //----------------------------------------------------------------------- 1507 @DataProvider(name="MinusMillis") 1508 Object[][] provider_minusMillis_long() { 1509 return new Object[][] { 1510 {0, 0, 0, 0, 0}, 1511 {0, 0, 1, -1, 999000000}, 1512 {0, 0, 999, -1, 1000000}, 1513 {0, 0, 1000, -1, 0}, 1514 {0, 0, 1001, -2, 999000000}, 1515 {0, 0, 1999, -2, 1000000}, 1516 {0, 0, 2000, -2, 0}, 1517 {0, 0, -1, 0, 1000000}, 1518 {0, 0, -999, 0, 999000000}, 1519 {0, 0, -1000, 1, 0}, 1520 {0, 0, -1001, 1, 1000000}, 1521 {0, 0, -1999, 1, 999000000}, 1522 1523 {0, 1, 0, 0, 1}, 1524 {0, 1, 1, -1, 999000001}, 1525 {0, 1, 998, -1, 2000001}, 1526 {0, 1, 999, -1, 1000001}, 1527 {0, 1, 1000, -1, 1}, 1528 {0, 1, 1998, -2, 2000001}, 1529 {0, 1, 1999, -2, 1000001}, 1530 {0, 1, 2000, -2, 1}, 1531 {0, 1, -1, 0, 1000001}, 1532 {0, 1, -2, 0, 2000001}, 1533 {0, 1, -1000, 1, 1}, 1534 {0, 1, -1001, 1, 1000001}, 1535 1536 {0, 1000000, 0, 0, 1000000}, 1537 {0, 1000000, 1, 0, 0}, 1538 {0, 1000000, 998, -1, 3000000}, 1539 {0, 1000000, 999, -1, 2000000}, 1540 {0, 1000000, 1000, -1, 1000000}, 1541 {0, 1000000, 1998, -2, 3000000}, 1542 {0, 1000000, 1999, -2, 2000000}, 1543 {0, 1000000, 2000, -2, 1000000}, 1544 {0, 1000000, -1, 0, 2000000}, 1545 {0, 1000000, -2, 0, 3000000}, 1546 {0, 1000000, -999, 1, 0}, 1547 {0, 1000000, -1000, 1, 1000000}, 1548 {0, 1000000, -1001, 1, 2000000}, 1549 {0, 1000000, -1002, 1, 3000000}, 1550 1551 {0, 999999999, 0, 0, 999999999}, 1552 {0, 999999999, 1, 0, 998999999}, 1553 {0, 999999999, 999, 0, 999999}, 1554 {0, 999999999, 1000, -1, 999999999}, 1555 {0, 999999999, 1001, -1, 998999999}, 1556 {0, 999999999, -1, 1, 999999}, 1557 {0, 999999999, -1000, 1, 999999999}, 1558 {0, 999999999, -1001, 2, 999999}, 1559 1560 {0, 0, Long.MAX_VALUE, -(Long.MAX_VALUE / 1000) - 1, (int) -(Long.MAX_VALUE % 1000) * 1000000 + 1000000000}, 1561 {0, 0, Long.MIN_VALUE, -(Long.MIN_VALUE / 1000), (int) -(Long.MIN_VALUE % 1000) * 1000000}, 1562 }; 1563 } 1564 1565 @Test(dataProvider="MinusMillis") 1566 public void minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1567 Instant i = Instant.ofEpochSecond(seconds, nanos); 1568 i = i.minusMillis(amount); 1569 assertEquals(i.getEpochSecond(), expectedSeconds); 1570 assertEquals(i.getNano(), expectedNanoOfSecond); 1571 } 1572 1573 @Test(dataProvider="MinusMillis") 1574 public void minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1575 Instant i = Instant.ofEpochSecond(seconds + 1, nanos); 1576 i = i.minusMillis(amount); 1577 assertEquals(i.getEpochSecond(), expectedSeconds + 1); 1578 assertEquals(i.getNano(), expectedNanoOfSecond); 1579 } 1580 1581 @Test(dataProvider="MinusMillis") 1582 public void minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1583 Instant i = Instant.ofEpochSecond(seconds - 1, nanos); 1584 i = i.minusMillis(amount); 1585 assertEquals(i.getEpochSecond(), expectedSeconds - 1); 1586 assertEquals(i.getNano(), expectedNanoOfSecond); 1587 } 1588 1589 @Test 1590 public void minusMillis_long_max() { 1591 Instant i = Instant.ofEpochSecond(MAX_SECOND, 998999999); 1592 i = i.minusMillis(-1); 1593 assertEquals(i.getEpochSecond(), MAX_SECOND); 1594 assertEquals(i.getNano(), 999999999); 1595 } 1596 1597 @Test(expectedExceptions=DateTimeException.class) 1598 public void minusMillis_long_overflowTooBig() { 1599 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999000000); 1600 i.minusMillis(-1); 1601 } 1602 1603 @Test 1604 public void minusMillis_long_min() { 1605 Instant i = Instant.ofEpochSecond(MIN_SECOND, 1000000); 1606 i = i.minusMillis(1); 1607 assertEquals(i.getEpochSecond(), MIN_SECOND); 1608 assertEquals(i.getNano(), 0); 1609 } 1610 1611 @Test(expectedExceptions=DateTimeException.class) 1612 public void minusMillis_long_overflowTooSmall() { 1613 Instant i = Instant.ofEpochSecond(MIN_SECOND, 0); 1614 i.minusMillis(1); 1615 } 1616 1617 //----------------------------------------------------------------------- 1618 @DataProvider(name="MinusNanos") 1619 Object[][] provider_minusNanos_long() { 1620 return new Object[][] { 1621 {0, 0, 0, 0, 0}, 1622 {0, 0, 1, -1, 999999999}, 1623 {0, 0, 999999999, -1, 1}, 1624 {0, 0, 1000000000, -1, 0}, 1625 {0, 0, 1000000001, -2, 999999999}, 1626 {0, 0, 1999999999, -2, 1}, 1627 {0, 0, 2000000000, -2, 0}, 1628 {0, 0, -1, 0, 1}, 1629 {0, 0, -999999999, 0, 999999999}, 1630 {0, 0, -1000000000, 1, 0}, 1631 {0, 0, -1000000001, 1, 1}, 1632 {0, 0, -1999999999, 1, 999999999}, 1633 1634 {1, 0, 0, 1, 0}, 1635 {1, 0, 1, 0, 999999999}, 1636 {1, 0, 999999999, 0, 1}, 1637 {1, 0, 1000000000, 0, 0}, 1638 {1, 0, 1000000001, -1, 999999999}, 1639 {1, 0, 1999999999, -1, 1}, 1640 {1, 0, 2000000000, -1, 0}, 1641 {1, 0, -1, 1, 1}, 1642 {1, 0, -999999999, 1, 999999999}, 1643 {1, 0, -1000000000, 2, 0}, 1644 {1, 0, -1000000001, 2, 1}, 1645 {1, 0, -1999999999, 2, 999999999}, 1646 1647 {-1, 0, 0, -1, 0}, 1648 {-1, 0, 1, -2, 999999999}, 1649 {-1, 0, 999999999, -2, 1}, 1650 {-1, 0, 1000000000, -2, 0}, 1651 {-1, 0, 1000000001, -3, 999999999}, 1652 {-1, 0, 1999999999, -3, 1}, 1653 {-1, 0, 2000000000, -3, 0}, 1654 {-1, 0, -1, -1, 1}, 1655 {-1, 0, -999999999, -1, 999999999}, 1656 {-1, 0, -1000000000, 0, 0}, 1657 {-1, 0, -1000000001, 0, 1}, 1658 {-1, 0, -1999999999, 0, 999999999}, 1659 1660 {1, 1, 0, 1, 1}, 1661 {1, 1, 1, 1, 0}, 1662 {1, 1, 999999998, 0, 3}, 1663 {1, 1, 999999999, 0, 2}, 1664 {1, 1, 1000000000, 0, 1}, 1665 {1, 1, 1999999998, -1, 3}, 1666 {1, 1, 1999999999, -1, 2}, 1667 {1, 1, 2000000000, -1, 1}, 1668 {1, 1, -1, 1, 2}, 1669 {1, 1, -2, 1, 3}, 1670 {1, 1, -1000000000, 2, 1}, 1671 {1, 1, -1000000001, 2, 2}, 1672 {1, 1, -1000000002, 2, 3}, 1673 {1, 1, -2000000000, 3, 1}, 1674 1675 {1, 999999999, 0, 1, 999999999}, 1676 {1, 999999999, 1, 1, 999999998}, 1677 {1, 999999999, 999999999, 1, 0}, 1678 {1, 999999999, 1000000000, 0, 999999999}, 1679 {1, 999999999, 1000000001, 0, 999999998}, 1680 {1, 999999999, -1, 2, 0}, 1681 {1, 999999999, -1000000000, 2, 999999999}, 1682 {1, 999999999, -1000000001, 3, 0}, 1683 {1, 999999999, -1999999999, 3, 999999998}, 1684 {1, 999999999, -2000000000, 3, 999999999}, 1685 1686 {MAX_SECOND, 0, -999999999, MAX_SECOND, 999999999}, 1687 {MAX_SECOND - 1, 0, -1999999999, MAX_SECOND, 999999999}, 1688 {MIN_SECOND, 1, 1, MIN_SECOND, 0}, 1689 {MIN_SECOND + 1, 1, 1000000001, MIN_SECOND, 0}, 1690 1691 {0, 0, Long.MAX_VALUE, -(Long.MAX_VALUE / 1000000000) - 1, (int) -(Long.MAX_VALUE % 1000000000) + 1000000000}, 1692 {0, 0, Long.MIN_VALUE, -(Long.MIN_VALUE / 1000000000), (int) -(Long.MIN_VALUE % 1000000000)}, 1693 }; 1694 } 1695 1696 @Test(dataProvider="MinusNanos") 1697 public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1698 Instant i = Instant.ofEpochSecond(seconds, nanos); 1699 i = i.minusNanos(amount); 1700 assertEquals(i.getEpochSecond(), expectedSeconds); 1701 assertEquals(i.getNano(), expectedNanoOfSecond); 1702 } 1703 1704 @Test(expectedExceptions=DateTimeException.class) 1705 public void minusNanos_long_overflowTooBig() { 1706 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1707 i.minusNanos(-1); 1708 } 1709 1710 @Test(expectedExceptions=DateTimeException.class) 1711 public void minusNanos_long_overflowTooSmall() { 1712 Instant i = Instant.ofEpochSecond(MIN_SECOND, 0); 1713 i.minusNanos(1); 1714 } 1715 1716 //----------------------------------------------------------------------- 1717 // until(Temporal, TemporalUnit) 1718 //----------------------------------------------------------------------- 1719 @DataProvider(name="periodUntilUnit") 1720 Object[][] data_periodUntilUnit() { 1721 return new Object[][] { 1722 {5, 650, -1, 650, SECONDS, -6}, 1723 {5, 650, 0, 650, SECONDS, -5}, 1724 {5, 650, 3, 650, SECONDS, -2}, 1725 {5, 650, 4, 650, SECONDS, -1}, 1726 {5, 650, 5, 650, SECONDS, 0}, 1727 {5, 650, 6, 650, SECONDS, 1}, 1728 {5, 650, 7, 650, SECONDS, 2}, 1729 1730 {5, 650, -1, 0, SECONDS, -6}, 1731 {5, 650, 0, 0, SECONDS, -5}, 1732 {5, 650, 3, 0, SECONDS, -2}, 1733 {5, 650, 4, 0, SECONDS, -1}, 1734 {5, 650, 5, 0, SECONDS, 0}, 1735 {5, 650, 6, 0, SECONDS, 0}, 1736 {5, 650, 7, 0, SECONDS, 1}, 1737 1738 {5, 650, -1, 950, SECONDS, -5}, 1739 {5, 650, 0, 950, SECONDS, -4}, 1740 {5, 650, 3, 950, SECONDS, -1}, 1741 {5, 650, 4, 950, SECONDS, 0}, 1742 {5, 650, 5, 950, SECONDS, 0}, 1743 {5, 650, 6, 950, SECONDS, 1}, 1744 {5, 650, 7, 950, SECONDS, 2}, 1745 1746 {5, 650, -1, 50, SECONDS, -6}, 1747 {5, 650, 0, 50, SECONDS, -5}, 1748 {5, 650, 4, 50, SECONDS, -1}, 1749 {5, 650, 5, 50, SECONDS, 0}, 1750 {5, 650, 6, 50, SECONDS, 0}, 1751 {5, 650, 7, 50, SECONDS, 1}, 1752 {5, 650, 8, 50, SECONDS, 2}, 1753 1754 {5, 650_000_000, -1, 650_000_000, NANOS, -6_000_000_000L}, 1755 {5, 650_000_000, 0, 650_000_000, NANOS, -5_000_000_000L}, 1756 {5, 650_000_000, 3, 650_000_000, NANOS, -2_000_000_000L}, 1757 {5, 650_000_000, 4, 650_000_000, NANOS, -1_000_000_000L}, 1758 {5, 650_000_000, 5, 650_000_000, NANOS, 0}, 1759 {5, 650_000_000, 6, 650_000_000, NANOS, 1_000_000_000L}, 1760 {5, 650_000_000, 7, 650_000_000, NANOS, 2_000_000_000L}, 1761 1762 {5, 650_000_000, -1, 0, NANOS, -6_650_000_000L}, 1763 {5, 650_000_000, 0, 0, NANOS, -5_650_000_000L}, 1764 {5, 650_000_000, 3, 0, NANOS, -2_650_000_000L}, 1765 {5, 650_000_000, 4, 0, NANOS, -1_650_000_000L}, 1766 {5, 650_000_000, 5, 0, NANOS, -650_000_000L}, 1767 {5, 650_000_000, 6, 0, NANOS, 350_000_000L}, 1768 {5, 650_000_000, 7, 0, NANOS, 1_350_000_000L}, 1769 1770 {5, 650_000_000, -1, 950_000_000, NANOS, -5_700_000_000L}, 1771 {5, 650_000_000, 0, 950_000_000, NANOS, -4_700_000_000L}, 1772 {5, 650_000_000, 3, 950_000_000, NANOS, -1_700_000_000L}, 1773 {5, 650_000_000, 4, 950_000_000, NANOS, -700_000_000L}, 1774 {5, 650_000_000, 5, 950_000_000, NANOS, 300_000_000L}, 1775 {5, 650_000_000, 6, 950_000_000, NANOS, 1_300_000_000L}, 1776 {5, 650_000_000, 7, 950_000_000, NANOS, 2_300_000_000L}, 1777 1778 {5, 650_000_000, -1, 50_000_000, NANOS, -6_600_000_000L}, 1779 {5, 650_000_000, 0, 50_000_000, NANOS, -5_600_000_000L}, 1780 {5, 650_000_000, 4, 50_000_000, NANOS, -1_600_000_000L}, 1781 {5, 650_000_000, 5, 50_000_000, NANOS, -600_000_000L}, 1782 {5, 650_000_000, 6, 50_000_000, NANOS, 400_000_000L}, 1783 {5, 650_000_000, 7, 50_000_000, NANOS, 1_400_000_000L}, 1784 {5, 650_000_000, 8, 50_000_000, NANOS, 2_400_000_000L}, 1785 1786 {0, 0, -60, 0, MINUTES, -1L}, 1787 {0, 0, -1, 999_999_999, MINUTES, 0L}, 1788 {0, 0, 59, 0, MINUTES, 0L}, 1789 {0, 0, 59, 999_999_999, MINUTES, 0L}, 1790 {0, 0, 60, 0, MINUTES, 1L}, 1791 {0, 0, 61, 0, MINUTES, 1L}, 1792 1793 {0, 0, -3600, 0, HOURS, -1L}, 1794 {0, 0, -1, 999_999_999, HOURS, 0L}, 1795 {0, 0, 3599, 0, HOURS, 0L}, 1796 {0, 0, 3599, 999_999_999, HOURS, 0L}, 1797 {0, 0, 3600, 0, HOURS, 1L}, 1798 {0, 0, 3601, 0, HOURS, 1L}, 1799 1800 {0, 0, -86400, 0, DAYS, -1L}, 1801 {0, 0, -1, 999_999_999, DAYS, 0L}, 1802 {0, 0, 86399, 0, DAYS, 0L}, 1803 {0, 0, 86399, 999_999_999, DAYS, 0L}, 1804 {0, 0, 86400, 0, DAYS, 1L}, 1805 {0, 0, 86401, 0, DAYS, 1L}, 1806 }; 1807 } 1808 1809 @Test(dataProvider="periodUntilUnit") 1810 public void test_until_TemporalUnit(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) { 1811 Instant i1 = Instant.ofEpochSecond(seconds1, nanos1); 1812 Instant i2 = Instant.ofEpochSecond(seconds2, nanos2); 1813 long amount = i1.until(i2, unit); 1814 assertEquals(amount, expected); 1815 } 1816 1817 @Test(dataProvider="periodUntilUnit") 1818 public void test_until_TemporalUnit_negated(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) { 1819 Instant i1 = Instant.ofEpochSecond(seconds1, nanos1); 1820 Instant i2 = Instant.ofEpochSecond(seconds2, nanos2); 1821 long amount = i2.until(i1, unit); 1822 assertEquals(amount, -expected); 1823 } 1824 1825 @Test(dataProvider="periodUntilUnit") 1826 public void test_until_TemporalUnit_between(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) { 1827 Instant i1 = Instant.ofEpochSecond(seconds1, nanos1); 1828 Instant i2 = Instant.ofEpochSecond(seconds2, nanos2); 1829 long amount = unit.between(i1, i2); 1830 assertEquals(amount, expected); 1831 } 1832 1833 @Test 1834 public void test_until_convertedType() { 1835 Instant start = Instant.ofEpochSecond(12, 3000); 1836 OffsetDateTime end = start.plusSeconds(2).atOffset(ZoneOffset.ofHours(2)); 1837 assertEquals(start.until(end, SECONDS), 2); 1838 } 1839 1840 @Test(expectedExceptions=DateTimeException.class) 1841 public void test_until_invalidType() { 1842 Instant start = Instant.ofEpochSecond(12, 3000); 1843 start.until(LocalTime.of(11, 30), SECONDS); 1844 } 1845 1846 @Test(expectedExceptions = UnsupportedTemporalTypeException.class) 1847 public void test_until_TemporalUnit_unsupportedUnit() { 1848 TEST_12345_123456789.until(TEST_12345_123456789, MONTHS); 1849 } 1850 1851 @Test(expectedExceptions = NullPointerException.class) 1852 public void test_until_TemporalUnit_nullEnd() { 1853 TEST_12345_123456789.until(null, HOURS); 1854 } 1855 1856 @Test(expectedExceptions = NullPointerException.class) 1857 public void test_until_TemporalUnit_nullUnit() { 1858 TEST_12345_123456789.until(TEST_12345_123456789, null); 1859 } 1860 1861 //----------------------------------------------------------------------- 1862 // atOffset() 1863 //----------------------------------------------------------------------- 1864 @Test 1865 public void test_atOffset() { 1866 for (int i = 0; i < (24 * 60 * 60); i++) { 1867 Instant instant = Instant.ofEpochSecond(i); 1868 OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1)); 1869 assertEquals(test.getYear(), 1970); 1870 assertEquals(test.getMonthValue(), 1); 1871 assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0)); 1872 assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24); 1873 assertEquals(test.getMinute(), (i / 60) % 60); 1874 assertEquals(test.getSecond(), i % 60); 1875 } 1876 } 1877 1878 @Test(expectedExceptions=NullPointerException.class) 1879 public void test_atOffset_null() { 1880 TEST_12345_123456789.atOffset(null); 1881 } 1882 1883 //----------------------------------------------------------------------- 1884 // atZone() 1885 //----------------------------------------------------------------------- 1886 @Test 1887 public void test_atZone() { 1888 for (int i = 0; i < (24 * 60 * 60); i++) { 1889 Instant instant = Instant.ofEpochSecond(i); 1890 ZonedDateTime test = instant.atZone(ZoneOffset.ofHours(1)); 1891 assertEquals(test.getYear(), 1970); 1892 assertEquals(test.getMonthValue(), 1); 1893 assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0)); 1894 assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24); 1895 assertEquals(test.getMinute(), (i / 60) % 60); 1896 assertEquals(test.getSecond(), i % 60); 1897 } 1898 } 1899 1900 @Test(expectedExceptions=NullPointerException.class) 1901 public void test_atZone_null() { 1902 TEST_12345_123456789.atZone(null); 1903 } 1904 1905 //----------------------------------------------------------------------- 1906 // toEpochMilli() 1907 //----------------------------------------------------------------------- 1908 @Test 1909 public void test_toEpochMilli() { 1910 assertEquals(Instant.ofEpochSecond(1L, 1000000).toEpochMilli(), 1001L); 1911 assertEquals(Instant.ofEpochSecond(1L, 2000000).toEpochMilli(), 1002L); 1912 assertEquals(Instant.ofEpochSecond(1L, 567).toEpochMilli(), 1000L); 1913 assertEquals(Instant.ofEpochSecond(Long.MAX_VALUE / 1000).toEpochMilli(), (Long.MAX_VALUE / 1000) * 1000); 1914 assertEquals(Instant.ofEpochSecond(Long.MIN_VALUE / 1000).toEpochMilli(), (Long.MIN_VALUE / 1000) * 1000); 1915 assertEquals(Instant.ofEpochSecond(0L, -1000000).toEpochMilli(), -1L); 1916 assertEquals(Instant.ofEpochSecond(0L, 1000000).toEpochMilli(), 1); 1917 assertEquals(Instant.ofEpochSecond(0L, 999999).toEpochMilli(), 0); 1918 assertEquals(Instant.ofEpochSecond(0L, 1).toEpochMilli(), 0); 1919 assertEquals(Instant.ofEpochSecond(0L, 0).toEpochMilli(), 0); 1920 assertEquals(Instant.ofEpochSecond(0L, -1).toEpochMilli(), -1L); 1921 assertEquals(Instant.ofEpochSecond(0L, -999999).toEpochMilli(), -1L); 1922 assertEquals(Instant.ofEpochSecond(0L, -1000000).toEpochMilli(), -1L); 1923 assertEquals(Instant.ofEpochSecond(0L, -1000001).toEpochMilli(), -2L); 1924 } 1925 1926 @Test(expectedExceptions=ArithmeticException.class) 1927 public void test_toEpochMilli_tooBig() { 1928 Instant.ofEpochSecond(Long.MAX_VALUE / 1000 + 1).toEpochMilli(); 1929 } 1930 1931 @Test(expectedExceptions=ArithmeticException.class) 1932 public void test_toEpochMilli_tooSmall() { 1933 Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1).toEpochMilli(); 1934 } 1935 1936 @Test(expectedExceptions=ArithmeticException.class) 1937 public void test_toEpochMillis_overflow() { 1938 Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 809_000_000).toEpochMilli(); 1939 } 1940 1941 @Test(expectedExceptions=ArithmeticException.class) 1942 public void test_toEpochMillis_overflow2() { 1943 Instant.ofEpochSecond(-9223372036854776L, 1).toEpochMilli(); 1944 } 1945 1946 //----------------------------------------------------------------------- 1947 // compareTo() 1948 //----------------------------------------------------------------------- 1949 @Test 1950 public void test_comparisons() { 1951 doTest_comparisons_Instant( 1952 Instant.ofEpochSecond(-2L, 0), 1953 Instant.ofEpochSecond(-2L, 999999998), 1954 Instant.ofEpochSecond(-2L, 999999999), 1955 Instant.ofEpochSecond(-1L, 0), 1956 Instant.ofEpochSecond(-1L, 1), 1957 Instant.ofEpochSecond(-1L, 999999998), 1958 Instant.ofEpochSecond(-1L, 999999999), 1959 Instant.ofEpochSecond(0L, 0), 1960 Instant.ofEpochSecond(0L, 1), 1961 Instant.ofEpochSecond(0L, 2), 1962 Instant.ofEpochSecond(0L, 999999999), 1963 Instant.ofEpochSecond(1L, 0), 1964 Instant.ofEpochSecond(2L, 0) 1965 ); 1966 } 1967 1968 void doTest_comparisons_Instant(Instant... instants) { 1969 for (int i = 0; i < instants.length; i++) { 1970 Instant a = instants[i]; 1971 for (int j = 0; j < instants.length; j++) { 1972 Instant b = instants[j]; 1973 if (i < j) { 1974 assertEquals(a.compareTo(b) < 0, true, a + " <=> " + b); 1975 assertEquals(a.isBefore(b), true, a + " <=> " + b); 1976 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1977 assertEquals(a.equals(b), false, a + " <=> " + b); 1978 } else if (i > j) { 1979 assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b); 1980 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1981 assertEquals(a.isAfter(b), true, a + " <=> " + b); 1982 assertEquals(a.equals(b), false, a + " <=> " + b); 1983 } else { 1984 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 1985 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1986 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1987 assertEquals(a.equals(b), true, a + " <=> " + b); 1988 } 1989 } 1990 } 1991 } 1992 1993 @Test(expectedExceptions=NullPointerException.class) 1994 public void test_compareTo_ObjectNull() { 1995 Instant a = Instant.ofEpochSecond(0L, 0); 1996 a.compareTo(null); 1997 } 1998 1999 @Test(expectedExceptions=NullPointerException.class) 2000 public void test_isBefore_ObjectNull() { 2001 Instant a = Instant.ofEpochSecond(0L, 0); 2002 a.isBefore(null); 2003 } 2004 2005 @Test(expectedExceptions=NullPointerException.class) 2006 public void test_isAfter_ObjectNull() { 2007 Instant a = Instant.ofEpochSecond(0L, 0); 2008 a.isAfter(null); 2009 } 2010 2011 @Test(expectedExceptions=ClassCastException.class) 2012 @SuppressWarnings({"unchecked", "rawtypes"}) 2013 public void compareToNonInstant() { 2014 Comparable c = Instant.ofEpochSecond(0L); 2015 c.compareTo(new Object()); 2016 } 2017 2018 //----------------------------------------------------------------------- 2019 // equals() 2020 //----------------------------------------------------------------------- 2021 @Test 2022 public void test_equals() { 2023 Instant test5a = Instant.ofEpochSecond(5L, 20); 2024 Instant test5b = Instant.ofEpochSecond(5L, 20); 2025 Instant test5n = Instant.ofEpochSecond(5L, 30); 2026 Instant test6 = Instant.ofEpochSecond(6L, 20); 2027 2028 assertEquals(test5a.equals(test5a), true); 2029 assertEquals(test5a.equals(test5b), true); 2030 assertEquals(test5a.equals(test5n), false); 2031 assertEquals(test5a.equals(test6), false); 2032 2033 assertEquals(test5b.equals(test5a), true); 2034 assertEquals(test5b.equals(test5b), true); 2035 assertEquals(test5b.equals(test5n), false); 2036 assertEquals(test5b.equals(test6), false); 2037 2038 assertEquals(test5n.equals(test5a), false); 2039 assertEquals(test5n.equals(test5b), false); 2040 assertEquals(test5n.equals(test5n), true); 2041 assertEquals(test5n.equals(test6), false); 2042 2043 assertEquals(test6.equals(test5a), false); 2044 assertEquals(test6.equals(test5b), false); 2045 assertEquals(test6.equals(test5n), false); 2046 assertEquals(test6.equals(test6), true); 2047 } 2048 2049 @Test 2050 public void test_equals_null() { 2051 Instant test5 = Instant.ofEpochSecond(5L, 20); 2052 assertEquals(test5.equals(null), false); 2053 } 2054 2055 @Test 2056 public void test_equals_otherClass() { 2057 Instant test5 = Instant.ofEpochSecond(5L, 20); 2058 assertEquals(test5.equals(""), false); 2059 } 2060 2061 //----------------------------------------------------------------------- 2062 // hashCode() 2063 //----------------------------------------------------------------------- 2064 @Test 2065 public void test_hashCode() { 2066 Instant test5a = Instant.ofEpochSecond(5L, 20); 2067 Instant test5b = Instant.ofEpochSecond(5L, 20); 2068 Instant test5n = Instant.ofEpochSecond(5L, 30); 2069 Instant test6 = Instant.ofEpochSecond(6L, 20); 2070 2071 assertEquals(test5a.hashCode() == test5a.hashCode(), true); 2072 assertEquals(test5a.hashCode() == test5b.hashCode(), true); 2073 assertEquals(test5b.hashCode() == test5b.hashCode(), true); 2074 2075 assertEquals(test5a.hashCode() == test5n.hashCode(), false); 2076 assertEquals(test5a.hashCode() == test6.hashCode(), false); 2077 } 2078 2079 //----------------------------------------------------------------------- 2080 // toString() 2081 //----------------------------------------------------------------------- 2082 @DataProvider(name="toStringParse") 2083 Object[][] data_toString() { 2084 return new Object[][] { 2085 {Instant.ofEpochSecond(65L, 567), "1970-01-01T00:01:05.000000567Z"}, 2086 {Instant.ofEpochSecond(65L, 560), "1970-01-01T00:01:05.000000560Z"}, 2087 {Instant.ofEpochSecond(65L, 560000), "1970-01-01T00:01:05.000560Z"}, 2088 {Instant.ofEpochSecond(65L, 560000000), "1970-01-01T00:01:05.560Z"}, 2089 2090 {Instant.ofEpochSecond(1, 0), "1970-01-01T00:00:01Z"}, 2091 {Instant.ofEpochSecond(60, 0), "1970-01-01T00:01:00Z"}, 2092 {Instant.ofEpochSecond(3600, 0), "1970-01-01T01:00:00Z"}, 2093 {Instant.ofEpochSecond(-1, 0), "1969-12-31T23:59:59Z"}, 2094 2095 {LocalDateTime.of(0, 1, 2, 0, 0).toInstant(ZoneOffset.UTC), "0000-01-02T00:00:00Z"}, 2096 {LocalDateTime.of(0, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "0000-01-01T12:30:00Z"}, 2097 {LocalDateTime.of(0, 1, 1, 0, 0, 0, 1).toInstant(ZoneOffset.UTC), "0000-01-01T00:00:00.000000001Z"}, 2098 {LocalDateTime.of(0, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "0000-01-01T00:00:00Z"}, 2099 2100 {LocalDateTime.of(-1, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "-0001-12-31T23:59:59.999999999Z"}, 2101 {LocalDateTime.of(-1, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-0001-12-31T12:30:00Z"}, 2102 {LocalDateTime.of(-1, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-0001-12-30T12:30:00Z"}, 2103 2104 {LocalDateTime.of(-9999, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "-9999-01-02T12:30:00Z"}, 2105 {LocalDateTime.of(-9999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "-9999-01-01T12:30:00Z"}, 2106 {LocalDateTime.of(-9999, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "-9999-01-01T00:00:00Z"}, 2107 2108 {LocalDateTime.of(-10000, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "-10000-12-31T23:59:59.999999999Z"}, 2109 {LocalDateTime.of(-10000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-10000-12-31T12:30:00Z"}, 2110 {LocalDateTime.of(-10000, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-10000-12-30T12:30:00Z"}, 2111 {LocalDateTime.of(-15000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-15000-12-31T12:30:00Z"}, 2112 2113 {LocalDateTime.of(-19999, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "-19999-01-02T12:30:00Z"}, 2114 {LocalDateTime.of(-19999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "-19999-01-01T12:30:00Z"}, 2115 {LocalDateTime.of(-19999, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "-19999-01-01T00:00:00Z"}, 2116 2117 {LocalDateTime.of(-20000, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "-20000-12-31T23:59:59.999999999Z"}, 2118 {LocalDateTime.of(-20000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-20000-12-31T12:30:00Z"}, 2119 {LocalDateTime.of(-20000, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-20000-12-30T12:30:00Z"}, 2120 {LocalDateTime.of(-25000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-25000-12-31T12:30:00Z"}, 2121 2122 {LocalDateTime.of(9999, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "9999-12-30T12:30:00Z"}, 2123 {LocalDateTime.of(9999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "9999-12-31T12:30:00Z"}, 2124 {LocalDateTime.of(9999, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "9999-12-31T23:59:59.999999999Z"}, 2125 2126 {LocalDateTime.of(10000, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "+10000-01-01T00:00:00Z"}, 2127 {LocalDateTime.of(10000, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "+10000-01-01T12:30:00Z"}, 2128 {LocalDateTime.of(10000, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "+10000-01-02T12:30:00Z"}, 2129 {LocalDateTime.of(15000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+15000-12-31T12:30:00Z"}, 2130 2131 {LocalDateTime.of(19999, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "+19999-12-30T12:30:00Z"}, 2132 {LocalDateTime.of(19999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+19999-12-31T12:30:00Z"}, 2133 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 999_999_999).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.999999999Z"}, 2134 2135 {LocalDateTime.of(20000, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "+20000-01-01T00:00:00Z"}, 2136 {LocalDateTime.of(20000, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "+20000-01-01T12:30:00Z"}, 2137 {LocalDateTime.of(20000, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "+20000-01-02T12:30:00Z"}, 2138 {LocalDateTime.of(25000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+25000-12-31T12:30:00Z"}, 2139 2140 {LocalDateTime.of(-999_999_999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC).minus(1, DAYS), "-1000000000-12-31T12:30:00Z"}, 2141 {LocalDateTime.of(999_999_999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC).plus(1, DAYS), "+1000000000-01-01T12:30:00Z"}, 2142 2143 {Instant.MIN, "-1000000000-01-01T00:00:00Z"}, 2144 {Instant.MAX, "+1000000000-12-31T23:59:59.999999999Z"}, 2145 }; 2146 } 2147 2148 @Test(dataProvider="toStringParse") 2149 public void test_toString(Instant instant, String expected) { 2150 assertEquals(instant.toString(), expected); 2151 } 2152 2153 @Test(dataProvider="toStringParse") 2154 public void test_parse(Instant instant, String text) { 2155 assertEquals(Instant.parse(text), instant); 2156 } 2157 2158 @Test(dataProvider="toStringParse") 2159 public void test_parseLowercase(Instant instant, String text) { 2160 assertEquals(Instant.parse(text.toLowerCase(Locale.ENGLISH)), instant); 2161 } 2162 2163 }