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