1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertTrue; 36 import static org.threeten.bp.temporal.ChronoField.INSTANT_SECONDS; 37 import static org.threeten.bp.temporal.ChronoField.MICRO_OF_SECOND; 38 import static org.threeten.bp.temporal.ChronoField.MILLI_OF_SECOND; 39 import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND; 40 import static org.threeten.bp.temporal.ChronoUnit.DAYS; 41 import static org.threeten.bp.temporal.ChronoUnit.NANOS; 42 import static org.threeten.bp.temporal.ChronoUnit.SECONDS; 43 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.List; 47 import java.util.Locale; 48 49 import org.testng.annotations.BeforeMethod; 50 import org.testng.annotations.DataProvider; 51 import org.testng.annotations.Test; 52 import org.threeten.bp.format.DateTimeParseException; 53 import org.threeten.bp.temporal.ChronoField; 54 import org.threeten.bp.temporal.ChronoUnit; 55 import org.threeten.bp.temporal.JulianFields; 56 import org.threeten.bp.temporal.TemporalAccessor; 57 import org.threeten.bp.temporal.TemporalField; 58 import org.threeten.bp.temporal.TemporalQueries; 59 60 /** 61 * Test Instant. 62 */ 63 @Test 64 public class TestInstant extends AbstractDateTimeTest { 65 66 private static final long MIN_SECOND = Instant.MIN.getEpochSecond(); 67 private static final long MAX_SECOND = Instant.MAX.getEpochSecond(); 68 69 private Instant TEST_12345_123456789; 70 71 @BeforeMethod setUp()72 public void setUp() { 73 TEST_12345_123456789 = Instant.ofEpochSecond(12345, 123456789); 74 } 75 76 //----------------------------------------------------------------------- 77 @Override samples()78 protected List<TemporalAccessor> samples() { 79 TemporalAccessor[] array = {TEST_12345_123456789, Instant.MIN, Instant.MAX, Instant.EPOCH}; 80 return Arrays.asList(array); 81 } 82 83 @Override validFields()84 protected List<TemporalField> validFields() { 85 TemporalField[] array = { 86 NANO_OF_SECOND, 87 MICRO_OF_SECOND, 88 MILLI_OF_SECOND, 89 INSTANT_SECONDS, 90 }; 91 return Arrays.asList(array); 92 } 93 94 @Override invalidFields()95 protected List<TemporalField> invalidFields() { 96 List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values())); 97 list.removeAll(validFields()); 98 list.add(JulianFields.JULIAN_DAY); 99 list.add(JulianFields.MODIFIED_JULIAN_DAY); 100 list.add(JulianFields.RATA_DIE); 101 return list; 102 } 103 104 //----------------------------------------------------------------------- 105 @Test test_serialization()106 public void test_serialization() throws Exception { 107 assertSerializable(Instant.ofEpochMilli(134l)); 108 } 109 110 @Test test_serialization_format()111 public void test_serialization_format() throws Exception { 112 assertEqualsSerialisedForm(Instant.ofEpochMilli(1347830279338l)); 113 } 114 115 //----------------------------------------------------------------------- check(Instant instant, long epochSecs, int nos)116 private void check(Instant instant, long epochSecs, int nos) { 117 assertEquals(instant.getEpochSecond(), epochSecs); 118 assertEquals(instant.getNano(), nos); 119 assertEquals(instant, instant); 120 assertEquals(instant.hashCode(), instant.hashCode()); 121 } 122 123 //----------------------------------------------------------------------- 124 @Test constant_EPOCH()125 public void constant_EPOCH() { 126 check(Instant.EPOCH, 0, 0); 127 } 128 129 @Test constant_MIN()130 public void constant_MIN() { 131 check(Instant.MIN, -31557014167219200L, 0); 132 } 133 134 @Test constant_MAX()135 public void constant_MAX() { 136 check(Instant.MAX, 31556889864403199L, 999999999); 137 } 138 139 //----------------------------------------------------------------------- 140 // now() 141 //----------------------------------------------------------------------- 142 @Test now()143 public void now() { 144 Instant expected = Instant.now(Clock.systemUTC()); 145 Instant test = Instant.now(); 146 long diff = Math.abs(test.toEpochMilli() - expected.toEpochMilli()); 147 assertTrue(diff < 100); // less than 0.1 secs 148 } 149 150 //----------------------------------------------------------------------- 151 // now(Clock) 152 //----------------------------------------------------------------------- 153 @Test(expectedExceptions=NullPointerException.class) 154 public void now_Clock_nullClock() { 155 Instant.now(null); 156 } 157 158 @Test 159 public void now_Clock_allSecsInDay_utc() { 160 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 161 Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L); 162 Clock clock = Clock.fixed(expected, ZoneOffset.UTC); 163 Instant test = Instant.now(clock); 164 assertEquals(test, expected); 165 } 166 } 167 168 @Test 169 public void now_Clock_allSecsInDay_beforeEpoch() { 170 for (int i =-1; i >= -(24 * 60 * 60); i--) { 171 Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L); 172 Clock clock = Clock.fixed(expected, ZoneOffset.UTC); 173 Instant test = Instant.now(clock); 174 assertEquals(test, expected); 175 } 176 } 177 178 //----------------------------------------------------------------------- 179 // ofEpochSecond(long) 180 //----------------------------------------------------------------------- 181 @Test 182 public void factory_seconds_long() { 183 for (long i = -2; i <= 2; i++) { 184 Instant t = Instant.ofEpochSecond(i); 185 assertEquals(t.getEpochSecond(), i); 186 assertEquals(t.getNano(), 0); 187 } 188 } 189 190 //----------------------------------------------------------------------- 191 // ofEpochSecond(long,long) 192 //----------------------------------------------------------------------- 193 @Test 194 public void factory_seconds_long_long() { 195 for (long i = -2; i <= 2; i++) { 196 for (int j = 0; j < 10; j++) { 197 Instant t = Instant.ofEpochSecond(i, j); 198 assertEquals(t.getEpochSecond(), i); 199 assertEquals(t.getNano(), j); 200 } 201 for (int j = -10; j < 0; j++) { 202 Instant t = Instant.ofEpochSecond(i, j); 203 assertEquals(t.getEpochSecond(), i - 1); 204 assertEquals(t.getNano(), j + 1000000000); 205 } 206 for (int j = 999999990; j < 1000000000; j++) { 207 Instant t = Instant.ofEpochSecond(i, j); 208 assertEquals(t.getEpochSecond(), i); 209 assertEquals(t.getNano(), j); 210 } 211 } 212 } 213 214 @Test 215 public void factory_seconds_long_long_nanosNegativeAdjusted() { 216 Instant test = Instant.ofEpochSecond(2L, -1); 217 assertEquals(test.getEpochSecond(), 1); 218 assertEquals(test.getNano(), 999999999); 219 } 220 221 @Test(expectedExceptions=DateTimeException.class) 222 public void factory_seconds_long_long_tooBig() { 223 Instant.ofEpochSecond(MAX_SECOND, 1000000000); 224 } 225 226 @Test(expectedExceptions=ArithmeticException.class) 227 public void factory_seconds_long_long_tooBigBig() { 228 Instant.ofEpochSecond(Long.MAX_VALUE, Long.MAX_VALUE); 229 } 230 231 //----------------------------------------------------------------------- 232 // ofEpochMilli(long) 233 //----------------------------------------------------------------------- 234 @DataProvider(name="MillisInstantNoNanos") 235 Object[][] provider_factory_millis_long() { 236 return new Object[][] { 237 {0, 0, 0, 0}, 238 {0, 999999, 0, 999999}, 239 {1, 0, 0, 1000000}, 240 {1, 1, 0, 1000001}, 241 {2, 0, 0, 2000000}, 242 {999, 0, 0, 999000000}, 243 {1000, 0, 1, 0}, 244 {1001, 0, 1, 1000000}, 245 {-1, 1, -1, 999000001}, 246 {-1, 0, -1, 999000000}, 247 {-2, 999999, -1, 998999999}, 248 {-2, 0, -1, 998000000}, 249 {-999, 0, -1, 1000000}, 250 {-1000, 0, -1, 0}, 251 {-1001, 0, -2, 999000000}, 252 {Long.MAX_VALUE, 0, Long.MAX_VALUE / 1000, (int) (Long.MAX_VALUE % 1000) * 1000000}, 253 {Long.MAX_VALUE - 1, 0, (Long.MAX_VALUE - 1) / 1000, (int) ((Long.MAX_VALUE - 1) % 1000) * 1000000}, 254 {Long.MIN_VALUE, 0, (Long.MIN_VALUE / 1000) - 1, (int) (Long.MIN_VALUE % 1000) * 1000000 + 1000000000}, 255 {Long.MIN_VALUE, 1, (Long.MIN_VALUE / 1000) - 1, (int) (Long.MIN_VALUE % 1000) * 1000000 + 1000000000 + 1}, 256 {Long.MIN_VALUE + 1, 0, ((Long.MIN_VALUE + 1) / 1000) - 1, (int) ((Long.MIN_VALUE + 1) % 1000) * 1000000 + 1000000000}, 257 {Long.MIN_VALUE + 1, 1, ((Long.MIN_VALUE + 1) / 1000) - 1, (int) ((Long.MIN_VALUE + 1) % 1000) * 1000000 + 1000000000 + 1}, 258 }; 259 } 260 261 @Test(dataProvider="MillisInstantNoNanos") 262 public void factory_millis_long(long millis, int nanos, long expectedSeconds, int expectedNanoOfSecond) { 263 Instant t = Instant.ofEpochMilli(millis).plusNanos(nanos); 264 assertEquals(t.getEpochSecond(), expectedSeconds); 265 assertEquals(t.getNano(), expectedNanoOfSecond); 266 assertEquals(t.toEpochMilli(), millis); 267 } 268 269 //----------------------------------------------------------------------- 270 // parse(String) 271 //----------------------------------------------------------------------- 272 // see also parse tests under toString() 273 @DataProvider(name="Parse") 274 Object[][] provider_factory_parse() { 275 return new Object[][] { 276 {"1970-01-01T00:00:00Z", 0, 0}, 277 {"1970-01-01t00:00:00Z", 0, 0}, 278 {"1970-01-01T00:00:00z", 0, 0}, 279 {"1970-01-01T00:00:00.0Z", 0, 0}, 280 {"1970-01-01T00:00:00.000000000Z", 0, 0}, 281 282 {"1970-01-01T00:00:00.000000001Z", 0, 1}, 283 {"1970-01-01T00:00:00.100000000Z", 0, 100000000}, 284 {"1970-01-01T00:00:01Z", 1, 0}, 285 {"1970-01-01T00:01:00Z", 60, 0}, 286 {"1970-01-01T00:01:01Z", 61, 0}, 287 {"1970-01-01T00:01:01.000000001Z", 61, 1}, 288 {"1970-01-01T01:00:00.000000000Z", 3600, 0}, 289 {"1970-01-01T01:01:01.000000001Z", 3661, 1}, 290 {"1970-01-02T01:01:01.100000000Z", 90061, 100000000}, 291 }; 292 } 293 294 @Test(dataProvider="Parse") 295 public void factory_parse(String text, long expectedEpochSeconds, int expectedNanoOfSecond) { 296 Instant t = Instant.parse(text); 297 assertEquals(t.getEpochSecond(), expectedEpochSeconds); 298 assertEquals(t.getNano(), expectedNanoOfSecond); 299 } 300 301 @Test(dataProvider="Parse") 302 public void factory_parseLowercase(String text, long expectedEpochSeconds, int expectedNanoOfSecond) { 303 Instant t = Instant.parse(text.toLowerCase(Locale.ENGLISH)); 304 assertEquals(t.getEpochSecond(), expectedEpochSeconds); 305 assertEquals(t.getNano(), expectedNanoOfSecond); 306 } 307 308 // TODO: should comma be accepted? 309 // @Test(dataProvider="Parse") 310 // public void factory_parse_comma(String text, long expectedEpochSeconds, int expectedNanoOfSecond) { 311 // text = text.replace('.', ','); 312 // Instant t = Instant.parse(text); 313 // assertEquals(t.getEpochSecond(), expectedEpochSeconds); 314 // assertEquals(t.getNano(), expectedNanoOfSecond); 315 // } 316 317 @DataProvider(name="ParseFailures") 318 Object[][] provider_factory_parseFailures() { 319 return new Object[][] { 320 {""}, 321 {"Z"}, 322 {"1970-01-01T00:00:00"}, 323 {"1970-01-01T00:00:0Z"}, 324 {"1970-01-01T00:00:00.0000000000Z"}, 325 }; 326 } 327 328 @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class) 329 public void factory_parseFailures(String text) { 330 Instant.parse(text); 331 } 332 333 @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class) 334 public void factory_parseFailures_comma(String text) { 335 text = text.replace('.', ','); 336 Instant.parse(text); 337 } 338 339 @Test(expectedExceptions=NullPointerException.class) 340 public void factory_parse_nullText() { 341 Instant.parse(null); 342 } 343 344 //----------------------------------------------------------------------- 345 // get(TemporalField) 346 //----------------------------------------------------------------------- 347 @Test 348 public void test_get_TemporalField() { 349 Instant test = TEST_12345_123456789; 350 assertEquals(test.get(ChronoField.NANO_OF_SECOND), 123456789); 351 assertEquals(test.get(ChronoField.MICRO_OF_SECOND), 123456); 352 assertEquals(test.get(ChronoField.MILLI_OF_SECOND), 123); 353 } 354 355 @Test 356 public void test_getLong_TemporalField() { 357 Instant test = TEST_12345_123456789; 358 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 123456789); 359 assertEquals(test.getLong(ChronoField.MICRO_OF_SECOND), 123456); 360 assertEquals(test.getLong(ChronoField.MILLI_OF_SECOND), 123); 361 assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), 12345); 362 } 363 364 //----------------------------------------------------------------------- 365 // query(TemporalQuery) 366 //----------------------------------------------------------------------- 367 @Test 368 public void test_query() { 369 assertEquals(TEST_12345_123456789.query(TemporalQueries.chronology()), null); 370 assertEquals(TEST_12345_123456789.query(TemporalQueries.localDate()), null); 371 assertEquals(TEST_12345_123456789.query(TemporalQueries.localTime()), null); 372 assertEquals(TEST_12345_123456789.query(TemporalQueries.offset()), null); 373 assertEquals(TEST_12345_123456789.query(TemporalQueries.precision()), ChronoUnit.NANOS); 374 assertEquals(TEST_12345_123456789.query(TemporalQueries.zone()), null); 375 assertEquals(TEST_12345_123456789.query(TemporalQueries.zoneId()), null); 376 } 377 378 @Test(expectedExceptions=NullPointerException.class) 379 public void test_query_null() { 380 TEST_12345_123456789.query(null); 381 } 382 383 //----------------------------------------------------------------------- 384 @DataProvider(name="Plus") 385 Object[][] provider_plus() { 386 return new Object[][] { 387 {MIN_SECOND, 0, -MIN_SECOND, 0, 0, 0}, 388 389 {MIN_SECOND, 0, 1, 0, MIN_SECOND + 1, 0}, 390 {MIN_SECOND, 0, 0, 500, MIN_SECOND, 500}, 391 {MIN_SECOND, 0, 0, 1000000000, MIN_SECOND + 1, 0}, 392 393 {MIN_SECOND + 1, 0, -1, 0, MIN_SECOND, 0}, 394 {MIN_SECOND + 1, 0, 0, -500, MIN_SECOND, 999999500}, 395 {MIN_SECOND + 1, 0, 0, -1000000000, MIN_SECOND, 0}, 396 397 {-4, 666666667, -4, 666666667, -7, 333333334}, 398 {-4, 666666667, -3, 0, -7, 666666667}, 399 {-4, 666666667, -2, 0, -6, 666666667}, 400 {-4, 666666667, -1, 0, -5, 666666667}, 401 {-4, 666666667, -1, 333333334, -4, 1}, 402 {-4, 666666667, -1, 666666667, -4, 333333334}, 403 {-4, 666666667, -1, 999999999, -4, 666666666}, 404 {-4, 666666667, 0, 0, -4, 666666667}, 405 {-4, 666666667, 0, 1, -4, 666666668}, 406 {-4, 666666667, 0, 333333333, -3, 0}, 407 {-4, 666666667, 0, 666666666, -3, 333333333}, 408 {-4, 666666667, 1, 0, -3, 666666667}, 409 {-4, 666666667, 2, 0, -2, 666666667}, 410 {-4, 666666667, 3, 0, -1, 666666667}, 411 {-4, 666666667, 3, 333333333, 0, 0}, 412 413 {-3, 0, -4, 666666667, -7, 666666667}, 414 {-3, 0, -3, 0, -6, 0}, 415 {-3, 0, -2, 0, -5, 0}, 416 {-3, 0, -1, 0, -4, 0}, 417 {-3, 0, -1, 333333334, -4, 333333334}, 418 {-3, 0, -1, 666666667, -4, 666666667}, 419 {-3, 0, -1, 999999999, -4, 999999999}, 420 {-3, 0, 0, 0, -3, 0}, 421 {-3, 0, 0, 1, -3, 1}, 422 {-3, 0, 0, 333333333, -3, 333333333}, 423 {-3, 0, 0, 666666666, -3, 666666666}, 424 {-3, 0, 1, 0, -2, 0}, 425 {-3, 0, 2, 0, -1, 0}, 426 {-3, 0, 3, 0, 0, 0}, 427 {-3, 0, 3, 333333333, 0, 333333333}, 428 429 {-2, 0, -4, 666666667, -6, 666666667}, 430 {-2, 0, -3, 0, -5, 0}, 431 {-2, 0, -2, 0, -4, 0}, 432 {-2, 0, -1, 0, -3, 0}, 433 {-2, 0, -1, 333333334, -3, 333333334}, 434 {-2, 0, -1, 666666667, -3, 666666667}, 435 {-2, 0, -1, 999999999, -3, 999999999}, 436 {-2, 0, 0, 0, -2, 0}, 437 {-2, 0, 0, 1, -2, 1}, 438 {-2, 0, 0, 333333333, -2, 333333333}, 439 {-2, 0, 0, 666666666, -2, 666666666}, 440 {-2, 0, 1, 0, -1, 0}, 441 {-2, 0, 2, 0, 0, 0}, 442 {-2, 0, 3, 0, 1, 0}, 443 {-2, 0, 3, 333333333, 1, 333333333}, 444 445 {-1, 0, -4, 666666667, -5, 666666667}, 446 {-1, 0, -3, 0, -4, 0}, 447 {-1, 0, -2, 0, -3, 0}, 448 {-1, 0, -1, 0, -2, 0}, 449 {-1, 0, -1, 333333334, -2, 333333334}, 450 {-1, 0, -1, 666666667, -2, 666666667}, 451 {-1, 0, -1, 999999999, -2, 999999999}, 452 {-1, 0, 0, 0, -1, 0}, 453 {-1, 0, 0, 1, -1, 1}, 454 {-1, 0, 0, 333333333, -1, 333333333}, 455 {-1, 0, 0, 666666666, -1, 666666666}, 456 {-1, 0, 1, 0, 0, 0}, 457 {-1, 0, 2, 0, 1, 0}, 458 {-1, 0, 3, 0, 2, 0}, 459 {-1, 0, 3, 333333333, 2, 333333333}, 460 461 {-1, 666666667, -4, 666666667, -4, 333333334}, 462 {-1, 666666667, -3, 0, -4, 666666667}, 463 {-1, 666666667, -2, 0, -3, 666666667}, 464 {-1, 666666667, -1, 0, -2, 666666667}, 465 {-1, 666666667, -1, 333333334, -1, 1}, 466 {-1, 666666667, -1, 666666667, -1, 333333334}, 467 {-1, 666666667, -1, 999999999, -1, 666666666}, 468 {-1, 666666667, 0, 0, -1, 666666667}, 469 {-1, 666666667, 0, 1, -1, 666666668}, 470 {-1, 666666667, 0, 333333333, 0, 0}, 471 {-1, 666666667, 0, 666666666, 0, 333333333}, 472 {-1, 666666667, 1, 0, 0, 666666667}, 473 {-1, 666666667, 2, 0, 1, 666666667}, 474 {-1, 666666667, 3, 0, 2, 666666667}, 475 {-1, 666666667, 3, 333333333, 3, 0}, 476 477 {0, 0, -4, 666666667, -4, 666666667}, 478 {0, 0, -3, 0, -3, 0}, 479 {0, 0, -2, 0, -2, 0}, 480 {0, 0, -1, 0, -1, 0}, 481 {0, 0, -1, 333333334, -1, 333333334}, 482 {0, 0, -1, 666666667, -1, 666666667}, 483 {0, 0, -1, 999999999, -1, 999999999}, 484 {0, 0, 0, 0, 0, 0}, 485 {0, 0, 0, 1, 0, 1}, 486 {0, 0, 0, 333333333, 0, 333333333}, 487 {0, 0, 0, 666666666, 0, 666666666}, 488 {0, 0, 1, 0, 1, 0}, 489 {0, 0, 2, 0, 2, 0}, 490 {0, 0, 3, 0, 3, 0}, 491 {0, 0, 3, 333333333, 3, 333333333}, 492 493 {0, 333333333, -4, 666666667, -3, 0}, 494 {0, 333333333, -3, 0, -3, 333333333}, 495 {0, 333333333, -2, 0, -2, 333333333}, 496 {0, 333333333, -1, 0, -1, 333333333}, 497 {0, 333333333, -1, 333333334, -1, 666666667}, 498 {0, 333333333, -1, 666666667, 0, 0}, 499 {0, 333333333, -1, 999999999, 0, 333333332}, 500 {0, 333333333, 0, 0, 0, 333333333}, 501 {0, 333333333, 0, 1, 0, 333333334}, 502 {0, 333333333, 0, 333333333, 0, 666666666}, 503 {0, 333333333, 0, 666666666, 0, 999999999}, 504 {0, 333333333, 1, 0, 1, 333333333}, 505 {0, 333333333, 2, 0, 2, 333333333}, 506 {0, 333333333, 3, 0, 3, 333333333}, 507 {0, 333333333, 3, 333333333, 3, 666666666}, 508 509 {1, 0, -4, 666666667, -3, 666666667}, 510 {1, 0, -3, 0, -2, 0}, 511 {1, 0, -2, 0, -1, 0}, 512 {1, 0, -1, 0, 0, 0}, 513 {1, 0, -1, 333333334, 0, 333333334}, 514 {1, 0, -1, 666666667, 0, 666666667}, 515 {1, 0, -1, 999999999, 0, 999999999}, 516 {1, 0, 0, 0, 1, 0}, 517 {1, 0, 0, 1, 1, 1}, 518 {1, 0, 0, 333333333, 1, 333333333}, 519 {1, 0, 0, 666666666, 1, 666666666}, 520 {1, 0, 1, 0, 2, 0}, 521 {1, 0, 2, 0, 3, 0}, 522 {1, 0, 3, 0, 4, 0}, 523 {1, 0, 3, 333333333, 4, 333333333}, 524 525 {2, 0, -4, 666666667, -2, 666666667}, 526 {2, 0, -3, 0, -1, 0}, 527 {2, 0, -2, 0, 0, 0}, 528 {2, 0, -1, 0, 1, 0}, 529 {2, 0, -1, 333333334, 1, 333333334}, 530 {2, 0, -1, 666666667, 1, 666666667}, 531 {2, 0, -1, 999999999, 1, 999999999}, 532 {2, 0, 0, 0, 2, 0}, 533 {2, 0, 0, 1, 2, 1}, 534 {2, 0, 0, 333333333, 2, 333333333}, 535 {2, 0, 0, 666666666, 2, 666666666}, 536 {2, 0, 1, 0, 3, 0}, 537 {2, 0, 2, 0, 4, 0}, 538 {2, 0, 3, 0, 5, 0}, 539 {2, 0, 3, 333333333, 5, 333333333}, 540 541 {3, 0, -4, 666666667, -1, 666666667}, 542 {3, 0, -3, 0, 0, 0}, 543 {3, 0, -2, 0, 1, 0}, 544 {3, 0, -1, 0, 2, 0}, 545 {3, 0, -1, 333333334, 2, 333333334}, 546 {3, 0, -1, 666666667, 2, 666666667}, 547 {3, 0, -1, 999999999, 2, 999999999}, 548 {3, 0, 0, 0, 3, 0}, 549 {3, 0, 0, 1, 3, 1}, 550 {3, 0, 0, 333333333, 3, 333333333}, 551 {3, 0, 0, 666666666, 3, 666666666}, 552 {3, 0, 1, 0, 4, 0}, 553 {3, 0, 2, 0, 5, 0}, 554 {3, 0, 3, 0, 6, 0}, 555 {3, 0, 3, 333333333, 6, 333333333}, 556 557 {3, 333333333, -4, 666666667, 0, 0}, 558 {3, 333333333, -3, 0, 0, 333333333}, 559 {3, 333333333, -2, 0, 1, 333333333}, 560 {3, 333333333, -1, 0, 2, 333333333}, 561 {3, 333333333, -1, 333333334, 2, 666666667}, 562 {3, 333333333, -1, 666666667, 3, 0}, 563 {3, 333333333, -1, 999999999, 3, 333333332}, 564 {3, 333333333, 0, 0, 3, 333333333}, 565 {3, 333333333, 0, 1, 3, 333333334}, 566 {3, 333333333, 0, 333333333, 3, 666666666}, 567 {3, 333333333, 0, 666666666, 3, 999999999}, 568 {3, 333333333, 1, 0, 4, 333333333}, 569 {3, 333333333, 2, 0, 5, 333333333}, 570 {3, 333333333, 3, 0, 6, 333333333}, 571 {3, 333333333, 3, 333333333, 6, 666666666}, 572 573 {MAX_SECOND - 1, 0, 1, 0, MAX_SECOND, 0}, 574 {MAX_SECOND - 1, 0, 0, 500, MAX_SECOND - 1, 500}, 575 {MAX_SECOND - 1, 0, 0, 1000000000, MAX_SECOND, 0}, 576 577 {MAX_SECOND, 0, -1, 0, MAX_SECOND - 1, 0}, 578 {MAX_SECOND, 0, 0, -500, MAX_SECOND - 1, 999999500}, 579 {MAX_SECOND, 0, 0, -1000000000, MAX_SECOND - 1, 0}, 580 581 {MAX_SECOND, 0, -MAX_SECOND, 0, 0, 0}, 582 }; 583 } 584 585 @Test(dataProvider="Plus") 586 public void plus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 587 Instant i = Instant.ofEpochSecond(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos)); 588 assertEquals(i.getEpochSecond(), expectedSeconds); 589 assertEquals(i.getNano(), expectedNanoOfSecond); 590 } 591 592 @Test(expectedExceptions=DateTimeException.class) 593 public void plus_Duration_overflowTooBig() { 594 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 595 i.plus(Duration.ofSeconds(0, 1)); 596 } 597 598 @Test(expectedExceptions=DateTimeException.class) 599 public void plus_Duration_overflowTooSmall() { 600 Instant i = Instant.ofEpochSecond(MIN_SECOND); 601 i.plus(Duration.ofSeconds(-1, 999999999)); 602 } 603 604 //-----------------------------------------------------------------------a 605 @Test(dataProvider="Plus") 606 public void plus_longTemporalUnit(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 607 Instant i = Instant.ofEpochSecond(seconds, nanos).plus(otherSeconds, SECONDS).plus(otherNanos, NANOS); 608 assertEquals(i.getEpochSecond(), expectedSeconds); 609 assertEquals(i.getNano(), expectedNanoOfSecond); 610 } 611 612 @Test(expectedExceptions=DateTimeException.class) 613 public void plus_longTemporalUnit_overflowTooBig() { 614 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 615 i.plus(1, NANOS); 616 } 617 618 @Test(expectedExceptions=DateTimeException.class) 619 public void plus_longTemporalUnit_overflowTooSmall() { 620 Instant i = Instant.ofEpochSecond(MIN_SECOND); 621 i.plus(999999999, NANOS); 622 i.plus(-1, SECONDS); 623 } 624 625 //----------------------------------------------------------------------- 626 @DataProvider(name="PlusSeconds") 627 Object[][] provider_plusSeconds_long() { 628 return new Object[][] { 629 {0, 0, 0, 0, 0}, 630 {0, 0, 1, 1, 0}, 631 {0, 0, -1, -1, 0}, 632 {0, 0, MAX_SECOND, MAX_SECOND, 0}, 633 {0, 0, MIN_SECOND, MIN_SECOND, 0}, 634 {1, 0, 0, 1, 0}, 635 {1, 0, 1, 2, 0}, 636 {1, 0, -1, 0, 0}, 637 {1, 0, MAX_SECOND - 1, MAX_SECOND, 0}, 638 {1, 0, MIN_SECOND, MIN_SECOND + 1, 0}, 639 {1, 1, 0, 1, 1}, 640 {1, 1, 1, 2, 1}, 641 {1, 1, -1, 0, 1}, 642 {1, 1, MAX_SECOND - 1, MAX_SECOND, 1}, 643 {1, 1, MIN_SECOND, MIN_SECOND + 1, 1}, 644 {-1, 1, 0, -1, 1}, 645 {-1, 1, 1, 0, 1}, 646 {-1, 1, -1, -2, 1}, 647 {-1, 1, MAX_SECOND, MAX_SECOND - 1, 1}, 648 {-1, 1, MIN_SECOND + 1, MIN_SECOND, 1}, 649 650 {MAX_SECOND, 2, -MAX_SECOND, 0, 2}, 651 {MIN_SECOND, 2, -MIN_SECOND, 0, 2}, 652 }; 653 } 654 655 @Test(dataProvider="PlusSeconds") 656 public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 657 Instant t = Instant.ofEpochSecond(seconds, nanos); 658 t = t.plusSeconds(amount); 659 assertEquals(t.getEpochSecond(), expectedSeconds); 660 assertEquals(t.getNano(), expectedNanoOfSecond); 661 } 662 663 @Test(expectedExceptions=ArithmeticException.class) 664 public void plusSeconds_long_overflowTooBig() { 665 Instant t = Instant.ofEpochSecond(1, 0); 666 t.plusSeconds(Long.MAX_VALUE); 667 } 668 669 @Test(expectedExceptions=ArithmeticException.class) 670 public void plusSeconds_long_overflowTooSmall() { 671 Instant t = Instant.ofEpochSecond(-1, 0); 672 t.plusSeconds(Long.MIN_VALUE); 673 } 674 675 //----------------------------------------------------------------------- 676 @DataProvider(name="PlusMillis") 677 Object[][] provider_plusMillis_long() { 678 return new Object[][] { 679 {0, 0, 0, 0, 0}, 680 {0, 0, 1, 0, 1000000}, 681 {0, 0, 999, 0, 999000000}, 682 {0, 0, 1000, 1, 0}, 683 {0, 0, 1001, 1, 1000000}, 684 {0, 0, 1999, 1, 999000000}, 685 {0, 0, 2000, 2, 0}, 686 {0, 0, -1, -1, 999000000}, 687 {0, 0, -999, -1, 1000000}, 688 {0, 0, -1000, -1, 0}, 689 {0, 0, -1001, -2, 999000000}, 690 {0, 0, -1999, -2, 1000000}, 691 692 {0, 1, 0, 0, 1}, 693 {0, 1, 1, 0, 1000001}, 694 {0, 1, 998, 0, 998000001}, 695 {0, 1, 999, 0, 999000001}, 696 {0, 1, 1000, 1, 1}, 697 {0, 1, 1998, 1, 998000001}, 698 {0, 1, 1999, 1, 999000001}, 699 {0, 1, 2000, 2, 1}, 700 {0, 1, -1, -1, 999000001}, 701 {0, 1, -2, -1, 998000001}, 702 {0, 1, -1000, -1, 1}, 703 {0, 1, -1001, -2, 999000001}, 704 705 {0, 1000000, 0, 0, 1000000}, 706 {0, 1000000, 1, 0, 2000000}, 707 {0, 1000000, 998, 0, 999000000}, 708 {0, 1000000, 999, 1, 0}, 709 {0, 1000000, 1000, 1, 1000000}, 710 {0, 1000000, 1998, 1, 999000000}, 711 {0, 1000000, 1999, 2, 0}, 712 {0, 1000000, 2000, 2, 1000000}, 713 {0, 1000000, -1, 0, 0}, 714 {0, 1000000, -2, -1, 999000000}, 715 {0, 1000000, -999, -1, 2000000}, 716 {0, 1000000, -1000, -1, 1000000}, 717 {0, 1000000, -1001, -1, 0}, 718 {0, 1000000, -1002, -2, 999000000}, 719 720 {0, 999999999, 0, 0, 999999999}, 721 {0, 999999999, 1, 1, 999999}, 722 {0, 999999999, 999, 1, 998999999}, 723 {0, 999999999, 1000, 1, 999999999}, 724 {0, 999999999, 1001, 2, 999999}, 725 {0, 999999999, -1, 0, 998999999}, 726 {0, 999999999, -1000, -1, 999999999}, 727 {0, 999999999, -1001, -1, 998999999}, 728 729 {0, 0, Long.MAX_VALUE, Long.MAX_VALUE / 1000, (int) (Long.MAX_VALUE % 1000) * 1000000}, 730 {0, 0, Long.MIN_VALUE, Long.MIN_VALUE / 1000 - 1, (int) (Long.MIN_VALUE % 1000) * 1000000 + 1000000000}, 731 }; 732 } 733 734 @Test(dataProvider="PlusMillis") 735 public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 736 Instant t = Instant.ofEpochSecond(seconds, nanos); 737 t = t.plusMillis(amount); 738 assertEquals(t.getEpochSecond(), expectedSeconds); 739 assertEquals(t.getNano(), expectedNanoOfSecond); 740 } 741 @Test(dataProvider="PlusMillis") 742 public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 743 Instant t = Instant.ofEpochSecond(seconds + 1, nanos); 744 t = t.plusMillis(amount); 745 assertEquals(t.getEpochSecond(), expectedSeconds + 1); 746 assertEquals(t.getNano(), expectedNanoOfSecond); 747 } 748 @Test(dataProvider="PlusMillis") 749 public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 750 Instant t = Instant.ofEpochSecond(seconds - 1, nanos); 751 t = t.plusMillis(amount); 752 assertEquals(t.getEpochSecond(), expectedSeconds - 1); 753 assertEquals(t.getNano(), expectedNanoOfSecond); 754 } 755 756 @Test 757 public void plusMillis_long_max() { 758 Instant t = Instant.ofEpochSecond(MAX_SECOND, 998999999); 759 t = t.plusMillis(1); 760 assertEquals(t.getEpochSecond(), MAX_SECOND); 761 assertEquals(t.getNano(), 999999999); 762 } 763 764 @Test(expectedExceptions=DateTimeException.class) 765 public void plusMillis_long_overflowTooBig() { 766 Instant t = Instant.ofEpochSecond(MAX_SECOND, 999000000); 767 t.plusMillis(1); 768 } 769 770 @Test 771 public void plusMillis_long_min() { 772 Instant t = Instant.ofEpochSecond(MIN_SECOND, 1000000); 773 t = t.plusMillis(-1); 774 assertEquals(t.getEpochSecond(), MIN_SECOND); 775 assertEquals(t.getNano(), 0); 776 } 777 778 @Test(expectedExceptions=DateTimeException.class) 779 public void plusMillis_long_overflowTooSmall() { 780 Instant t = Instant.ofEpochSecond(MIN_SECOND, 0); 781 t.plusMillis(-1); 782 } 783 784 //----------------------------------------------------------------------- 785 @DataProvider(name="PlusNanos") 786 Object[][] provider_plusNanos_long() { 787 return new Object[][] { 788 {0, 0, 0, 0, 0}, 789 {0, 0, 1, 0, 1}, 790 {0, 0, 999999999, 0, 999999999}, 791 {0, 0, 1000000000, 1, 0}, 792 {0, 0, 1000000001, 1, 1}, 793 {0, 0, 1999999999, 1, 999999999}, 794 {0, 0, 2000000000, 2, 0}, 795 {0, 0, -1, -1, 999999999}, 796 {0, 0, -999999999, -1, 1}, 797 {0, 0, -1000000000, -1, 0}, 798 {0, 0, -1000000001, -2, 999999999}, 799 {0, 0, -1999999999, -2, 1}, 800 801 {1, 0, 0, 1, 0}, 802 {1, 0, 1, 1, 1}, 803 {1, 0, 999999999, 1, 999999999}, 804 {1, 0, 1000000000, 2, 0}, 805 {1, 0, 1000000001, 2, 1}, 806 {1, 0, 1999999999, 2, 999999999}, 807 {1, 0, 2000000000, 3, 0}, 808 {1, 0, -1, 0, 999999999}, 809 {1, 0, -999999999, 0, 1}, 810 {1, 0, -1000000000, 0, 0}, 811 {1, 0, -1000000001, -1, 999999999}, 812 {1, 0, -1999999999, -1, 1}, 813 814 {-1, 0, 0, -1, 0}, 815 {-1, 0, 1, -1, 1}, 816 {-1, 0, 999999999, -1, 999999999}, 817 {-1, 0, 1000000000, 0, 0}, 818 {-1, 0, 1000000001, 0, 1}, 819 {-1, 0, 1999999999, 0, 999999999}, 820 {-1, 0, 2000000000, 1, 0}, 821 {-1, 0, -1, -2, 999999999}, 822 {-1, 0, -999999999, -2, 1}, 823 {-1, 0, -1000000000, -2, 0}, 824 {-1, 0, -1000000001, -3, 999999999}, 825 {-1, 0, -1999999999, -3, 1}, 826 827 {1, 1, 0, 1, 1}, 828 {1, 1, 1, 1, 2}, 829 {1, 1, 999999998, 1, 999999999}, 830 {1, 1, 999999999, 2, 0}, 831 {1, 1, 1000000000, 2, 1}, 832 {1, 1, 1999999998, 2, 999999999}, 833 {1, 1, 1999999999, 3, 0}, 834 {1, 1, 2000000000, 3, 1}, 835 {1, 1, -1, 1, 0}, 836 {1, 1, -2, 0, 999999999}, 837 {1, 1, -1000000000, 0, 1}, 838 {1, 1, -1000000001, 0, 0}, 839 {1, 1, -1000000002, -1, 999999999}, 840 {1, 1, -2000000000, -1, 1}, 841 842 {1, 999999999, 0, 1, 999999999}, 843 {1, 999999999, 1, 2, 0}, 844 {1, 999999999, 999999999, 2, 999999998}, 845 {1, 999999999, 1000000000, 2, 999999999}, 846 {1, 999999999, 1000000001, 3, 0}, 847 {1, 999999999, -1, 1, 999999998}, 848 {1, 999999999, -1000000000, 0, 999999999}, 849 {1, 999999999, -1000000001, 0, 999999998}, 850 {1, 999999999, -1999999999, 0, 0}, 851 {1, 999999999, -2000000000, -1, 999999999}, 852 853 {MAX_SECOND, 0, 999999999, MAX_SECOND, 999999999}, 854 {MAX_SECOND - 1, 0, 1999999999, MAX_SECOND, 999999999}, 855 {MIN_SECOND, 1, -1, MIN_SECOND, 0}, 856 {MIN_SECOND + 1, 1, -1000000001, MIN_SECOND, 0}, 857 858 {0, 0, MAX_SECOND, MAX_SECOND / 1000000000, (int) (MAX_SECOND % 1000000000)}, 859 {0, 0, MIN_SECOND, MIN_SECOND / 1000000000 - 1, (int) (MIN_SECOND % 1000000000) + 1000000000}, 860 }; 861 } 862 863 @Test(dataProvider="PlusNanos") 864 public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 865 Instant t = Instant.ofEpochSecond(seconds, nanos); 866 t = t.plusNanos(amount); 867 assertEquals(t.getEpochSecond(), expectedSeconds); 868 assertEquals(t.getNano(), expectedNanoOfSecond); 869 } 870 871 @Test(expectedExceptions=DateTimeException.class) 872 public void plusNanos_long_overflowTooBig() { 873 Instant t = Instant.ofEpochSecond(MAX_SECOND, 999999999); 874 t.plusNanos(1); 875 } 876 877 @Test(expectedExceptions=DateTimeException.class) 878 public void plusNanos_long_overflowTooSmall() { 879 Instant t = Instant.ofEpochSecond(MIN_SECOND, 0); 880 t.plusNanos(-1); 881 } 882 883 //----------------------------------------------------------------------- 884 @DataProvider(name="Minus") 885 Object[][] provider_minus() { 886 return new Object[][] { 887 {MIN_SECOND, 0, MIN_SECOND, 0, 0, 0}, 888 889 {MIN_SECOND, 0, -1, 0, MIN_SECOND + 1, 0}, 890 {MIN_SECOND, 0, 0, -500, MIN_SECOND, 500}, 891 {MIN_SECOND, 0, 0, -1000000000, MIN_SECOND + 1, 0}, 892 893 {MIN_SECOND + 1, 0, 1, 0, MIN_SECOND, 0}, 894 {MIN_SECOND + 1, 0, 0, 500, MIN_SECOND, 999999500}, 895 {MIN_SECOND + 1, 0, 0, 1000000000, MIN_SECOND, 0}, 896 897 {-4, 666666667, -4, 666666667, 0, 0}, 898 {-4, 666666667, -3, 0, -1, 666666667}, 899 {-4, 666666667, -2, 0, -2, 666666667}, 900 {-4, 666666667, -1, 0, -3, 666666667}, 901 {-4, 666666667, -1, 333333334, -3, 333333333}, 902 {-4, 666666667, -1, 666666667, -3, 0}, 903 {-4, 666666667, -1, 999999999, -4, 666666668}, 904 {-4, 666666667, 0, 0, -4, 666666667}, 905 {-4, 666666667, 0, 1, -4, 666666666}, 906 {-4, 666666667, 0, 333333333, -4, 333333334}, 907 {-4, 666666667, 0, 666666666, -4, 1}, 908 {-4, 666666667, 1, 0, -5, 666666667}, 909 {-4, 666666667, 2, 0, -6, 666666667}, 910 {-4, 666666667, 3, 0, -7, 666666667}, 911 {-4, 666666667, 3, 333333333, -7, 333333334}, 912 913 {-3, 0, -4, 666666667, 0, 333333333}, 914 {-3, 0, -3, 0, 0, 0}, 915 {-3, 0, -2, 0, -1, 0}, 916 {-3, 0, -1, 0, -2, 0}, 917 {-3, 0, -1, 333333334, -3, 666666666}, 918 {-3, 0, -1, 666666667, -3, 333333333}, 919 {-3, 0, -1, 999999999, -3, 1}, 920 {-3, 0, 0, 0, -3, 0}, 921 {-3, 0, 0, 1, -4, 999999999}, 922 {-3, 0, 0, 333333333, -4, 666666667}, 923 {-3, 0, 0, 666666666, -4, 333333334}, 924 {-3, 0, 1, 0, -4, 0}, 925 {-3, 0, 2, 0, -5, 0}, 926 {-3, 0, 3, 0, -6, 0}, 927 {-3, 0, 3, 333333333, -7, 666666667}, 928 929 {-2, 0, -4, 666666667, 1, 333333333}, 930 {-2, 0, -3, 0, 1, 0}, 931 {-2, 0, -2, 0, 0, 0}, 932 {-2, 0, -1, 0, -1, 0}, 933 {-2, 0, -1, 333333334, -2, 666666666}, 934 {-2, 0, -1, 666666667, -2, 333333333}, 935 {-2, 0, -1, 999999999, -2, 1}, 936 {-2, 0, 0, 0, -2, 0}, 937 {-2, 0, 0, 1, -3, 999999999}, 938 {-2, 0, 0, 333333333, -3, 666666667}, 939 {-2, 0, 0, 666666666, -3, 333333334}, 940 {-2, 0, 1, 0, -3, 0}, 941 {-2, 0, 2, 0, -4, 0}, 942 {-2, 0, 3, 0, -5, 0}, 943 {-2, 0, 3, 333333333, -6, 666666667}, 944 945 {-1, 0, -4, 666666667, 2, 333333333}, 946 {-1, 0, -3, 0, 2, 0}, 947 {-1, 0, -2, 0, 1, 0}, 948 {-1, 0, -1, 0, 0, 0}, 949 {-1, 0, -1, 333333334, -1, 666666666}, 950 {-1, 0, -1, 666666667, -1, 333333333}, 951 {-1, 0, -1, 999999999, -1, 1}, 952 {-1, 0, 0, 0, -1, 0}, 953 {-1, 0, 0, 1, -2, 999999999}, 954 {-1, 0, 0, 333333333, -2, 666666667}, 955 {-1, 0, 0, 666666666, -2, 333333334}, 956 {-1, 0, 1, 0, -2, 0}, 957 {-1, 0, 2, 0, -3, 0}, 958 {-1, 0, 3, 0, -4, 0}, 959 {-1, 0, 3, 333333333, -5, 666666667}, 960 961 {-1, 666666667, -4, 666666667, 3, 0}, 962 {-1, 666666667, -3, 0, 2, 666666667}, 963 {-1, 666666667, -2, 0, 1, 666666667}, 964 {-1, 666666667, -1, 0, 0, 666666667}, 965 {-1, 666666667, -1, 333333334, 0, 333333333}, 966 {-1, 666666667, -1, 666666667, 0, 0}, 967 {-1, 666666667, -1, 999999999, -1, 666666668}, 968 {-1, 666666667, 0, 0, -1, 666666667}, 969 {-1, 666666667, 0, 1, -1, 666666666}, 970 {-1, 666666667, 0, 333333333, -1, 333333334}, 971 {-1, 666666667, 0, 666666666, -1, 1}, 972 {-1, 666666667, 1, 0, -2, 666666667}, 973 {-1, 666666667, 2, 0, -3, 666666667}, 974 {-1, 666666667, 3, 0, -4, 666666667}, 975 {-1, 666666667, 3, 333333333, -4, 333333334}, 976 977 {0, 0, -4, 666666667, 3, 333333333}, 978 {0, 0, -3, 0, 3, 0}, 979 {0, 0, -2, 0, 2, 0}, 980 {0, 0, -1, 0, 1, 0}, 981 {0, 0, -1, 333333334, 0, 666666666}, 982 {0, 0, -1, 666666667, 0, 333333333}, 983 {0, 0, -1, 999999999, 0, 1}, 984 {0, 0, 0, 0, 0, 0}, 985 {0, 0, 0, 1, -1, 999999999}, 986 {0, 0, 0, 333333333, -1, 666666667}, 987 {0, 0, 0, 666666666, -1, 333333334}, 988 {0, 0, 1, 0, -1, 0}, 989 {0, 0, 2, 0, -2, 0}, 990 {0, 0, 3, 0, -3, 0}, 991 {0, 0, 3, 333333333, -4, 666666667}, 992 993 {0, 333333333, -4, 666666667, 3, 666666666}, 994 {0, 333333333, -3, 0, 3, 333333333}, 995 {0, 333333333, -2, 0, 2, 333333333}, 996 {0, 333333333, -1, 0, 1, 333333333}, 997 {0, 333333333, -1, 333333334, 0, 999999999}, 998 {0, 333333333, -1, 666666667, 0, 666666666}, 999 {0, 333333333, -1, 999999999, 0, 333333334}, 1000 {0, 333333333, 0, 0, 0, 333333333}, 1001 {0, 333333333, 0, 1, 0, 333333332}, 1002 {0, 333333333, 0, 333333333, 0, 0}, 1003 {0, 333333333, 0, 666666666, -1, 666666667}, 1004 {0, 333333333, 1, 0, -1, 333333333}, 1005 {0, 333333333, 2, 0, -2, 333333333}, 1006 {0, 333333333, 3, 0, -3, 333333333}, 1007 {0, 333333333, 3, 333333333, -3, 0}, 1008 1009 {1, 0, -4, 666666667, 4, 333333333}, 1010 {1, 0, -3, 0, 4, 0}, 1011 {1, 0, -2, 0, 3, 0}, 1012 {1, 0, -1, 0, 2, 0}, 1013 {1, 0, -1, 333333334, 1, 666666666}, 1014 {1, 0, -1, 666666667, 1, 333333333}, 1015 {1, 0, -1, 999999999, 1, 1}, 1016 {1, 0, 0, 0, 1, 0}, 1017 {1, 0, 0, 1, 0, 999999999}, 1018 {1, 0, 0, 333333333, 0, 666666667}, 1019 {1, 0, 0, 666666666, 0, 333333334}, 1020 {1, 0, 1, 0, 0, 0}, 1021 {1, 0, 2, 0, -1, 0}, 1022 {1, 0, 3, 0, -2, 0}, 1023 {1, 0, 3, 333333333, -3, 666666667}, 1024 1025 {2, 0, -4, 666666667, 5, 333333333}, 1026 {2, 0, -3, 0, 5, 0}, 1027 {2, 0, -2, 0, 4, 0}, 1028 {2, 0, -1, 0, 3, 0}, 1029 {2, 0, -1, 333333334, 2, 666666666}, 1030 {2, 0, -1, 666666667, 2, 333333333}, 1031 {2, 0, -1, 999999999, 2, 1}, 1032 {2, 0, 0, 0, 2, 0}, 1033 {2, 0, 0, 1, 1, 999999999}, 1034 {2, 0, 0, 333333333, 1, 666666667}, 1035 {2, 0, 0, 666666666, 1, 333333334}, 1036 {2, 0, 1, 0, 1, 0}, 1037 {2, 0, 2, 0, 0, 0}, 1038 {2, 0, 3, 0, -1, 0}, 1039 {2, 0, 3, 333333333, -2, 666666667}, 1040 1041 {3, 0, -4, 666666667, 6, 333333333}, 1042 {3, 0, -3, 0, 6, 0}, 1043 {3, 0, -2, 0, 5, 0}, 1044 {3, 0, -1, 0, 4, 0}, 1045 {3, 0, -1, 333333334, 3, 666666666}, 1046 {3, 0, -1, 666666667, 3, 333333333}, 1047 {3, 0, -1, 999999999, 3, 1}, 1048 {3, 0, 0, 0, 3, 0}, 1049 {3, 0, 0, 1, 2, 999999999}, 1050 {3, 0, 0, 333333333, 2, 666666667}, 1051 {3, 0, 0, 666666666, 2, 333333334}, 1052 {3, 0, 1, 0, 2, 0}, 1053 {3, 0, 2, 0, 1, 0}, 1054 {3, 0, 3, 0, 0, 0}, 1055 {3, 0, 3, 333333333, -1, 666666667}, 1056 1057 {3, 333333333, -4, 666666667, 6, 666666666}, 1058 {3, 333333333, -3, 0, 6, 333333333}, 1059 {3, 333333333, -2, 0, 5, 333333333}, 1060 {3, 333333333, -1, 0, 4, 333333333}, 1061 {3, 333333333, -1, 333333334, 3, 999999999}, 1062 {3, 333333333, -1, 666666667, 3, 666666666}, 1063 {3, 333333333, -1, 999999999, 3, 333333334}, 1064 {3, 333333333, 0, 0, 3, 333333333}, 1065 {3, 333333333, 0, 1, 3, 333333332}, 1066 {3, 333333333, 0, 333333333, 3, 0}, 1067 {3, 333333333, 0, 666666666, 2, 666666667}, 1068 {3, 333333333, 1, 0, 2, 333333333}, 1069 {3, 333333333, 2, 0, 1, 333333333}, 1070 {3, 333333333, 3, 0, 0, 333333333}, 1071 {3, 333333333, 3, 333333333, 0, 0}, 1072 1073 {MAX_SECOND - 1, 0, -1, 0, MAX_SECOND, 0}, 1074 {MAX_SECOND - 1, 0, 0, -500, MAX_SECOND - 1, 500}, 1075 {MAX_SECOND - 1, 0, 0, -1000000000, MAX_SECOND, 0}, 1076 1077 {MAX_SECOND, 0, 1, 0, MAX_SECOND - 1, 0}, 1078 {MAX_SECOND, 0, 0, 500, MAX_SECOND - 1, 999999500}, 1079 {MAX_SECOND, 0, 0, 1000000000, MAX_SECOND - 1, 0}, 1080 1081 {MAX_SECOND, 0, MAX_SECOND, 0, 0, 0}, 1082 }; 1083 } 1084 1085 @Test(dataProvider="Minus") 1086 public void minus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1087 Instant i = Instant.ofEpochSecond(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos)); 1088 assertEquals(i.getEpochSecond(), expectedSeconds); 1089 assertEquals(i.getNano(), expectedNanoOfSecond); 1090 } 1091 1092 @Test(expectedExceptions=DateTimeException.class) 1093 public void minus_Duration_overflowTooSmall() { 1094 Instant i = Instant.ofEpochSecond(MIN_SECOND); 1095 i.minus(Duration.ofSeconds(0, 1)); 1096 } 1097 1098 @Test(expectedExceptions=DateTimeException.class) 1099 public void minus_Duration_overflowTooBig() { 1100 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1101 i.minus(Duration.ofSeconds(-1, 999999999)); 1102 } 1103 1104 //----------------------------------------------------------------------- 1105 @Test(dataProvider="Minus") 1106 public void minus_longTemporalUnit(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) { 1107 Instant i = Instant.ofEpochSecond(seconds, nanos).minus(otherSeconds, SECONDS).minus(otherNanos, NANOS); 1108 assertEquals(i.getEpochSecond(), expectedSeconds); 1109 assertEquals(i.getNano(), expectedNanoOfSecond); 1110 } 1111 1112 @Test(expectedExceptions=DateTimeException.class) 1113 public void minus_longTemporalUnit_overflowTooSmall() { 1114 Instant i = Instant.ofEpochSecond(MIN_SECOND); 1115 i.minus(1, NANOS); 1116 } 1117 1118 @Test(expectedExceptions=DateTimeException.class) 1119 public void minus_longTemporalUnit_overflowTooBig() { 1120 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1121 i.minus(999999999, NANOS); 1122 i.minus(-1, SECONDS); 1123 } 1124 1125 //----------------------------------------------------------------------- 1126 @DataProvider(name="MinusSeconds") 1127 Object[][] provider_minusSeconds_long() { 1128 return new Object[][] { 1129 {0, 0, 0, 0, 0}, 1130 {0, 0, 1, -1, 0}, 1131 {0, 0, -1, 1, 0}, 1132 {0, 0, -MIN_SECOND, MIN_SECOND, 0}, 1133 {1, 0, 0, 1, 0}, 1134 {1, 0, 1, 0, 0}, 1135 {1, 0, -1, 2, 0}, 1136 {1, 0, -MIN_SECOND + 1, MIN_SECOND, 0}, 1137 {1, 1, 0, 1, 1}, 1138 {1, 1, 1, 0, 1}, 1139 {1, 1, -1, 2, 1}, 1140 {1, 1, -MIN_SECOND, MIN_SECOND + 1, 1}, 1141 {1, 1, -MIN_SECOND + 1, MIN_SECOND, 1}, 1142 {-1, 1, 0, -1, 1}, 1143 {-1, 1, 1, -2, 1}, 1144 {-1, 1, -1, 0, 1}, 1145 {-1, 1, -MAX_SECOND, MAX_SECOND - 1, 1}, 1146 {-1, 1, -(MAX_SECOND + 1), MAX_SECOND, 1}, 1147 1148 {MIN_SECOND, 2, MIN_SECOND, 0, 2}, 1149 {MIN_SECOND + 1, 2, MIN_SECOND, 1, 2}, 1150 {MAX_SECOND - 1, 2, MAX_SECOND, -1, 2}, 1151 {MAX_SECOND, 2, MAX_SECOND, 0, 2}, 1152 }; 1153 } 1154 1155 @Test(dataProvider="MinusSeconds") 1156 public void minusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1157 Instant i = Instant.ofEpochSecond(seconds, nanos); 1158 i = i.minusSeconds(amount); 1159 assertEquals(i.getEpochSecond(), expectedSeconds); 1160 assertEquals(i.getNano(), expectedNanoOfSecond); 1161 } 1162 1163 @Test(expectedExceptions = {ArithmeticException.class}) 1164 public void minusSeconds_long_overflowTooBig() { 1165 Instant i = Instant.ofEpochSecond(1, 0); 1166 i.minusSeconds(Long.MIN_VALUE + 1); 1167 } 1168 1169 @Test(expectedExceptions = {ArithmeticException.class}) 1170 public void minusSeconds_long_overflowTooSmall() { 1171 Instant i = Instant.ofEpochSecond(-2, 0); 1172 i.minusSeconds(Long.MAX_VALUE); 1173 } 1174 1175 //----------------------------------------------------------------------- 1176 @DataProvider(name="MinusMillis") 1177 Object[][] provider_minusMillis_long() { 1178 return new Object[][] { 1179 {0, 0, 0, 0, 0}, 1180 {0, 0, 1, -1, 999000000}, 1181 {0, 0, 999, -1, 1000000}, 1182 {0, 0, 1000, -1, 0}, 1183 {0, 0, 1001, -2, 999000000}, 1184 {0, 0, 1999, -2, 1000000}, 1185 {0, 0, 2000, -2, 0}, 1186 {0, 0, -1, 0, 1000000}, 1187 {0, 0, -999, 0, 999000000}, 1188 {0, 0, -1000, 1, 0}, 1189 {0, 0, -1001, 1, 1000000}, 1190 {0, 0, -1999, 1, 999000000}, 1191 1192 {0, 1, 0, 0, 1}, 1193 {0, 1, 1, -1, 999000001}, 1194 {0, 1, 998, -1, 2000001}, 1195 {0, 1, 999, -1, 1000001}, 1196 {0, 1, 1000, -1, 1}, 1197 {0, 1, 1998, -2, 2000001}, 1198 {0, 1, 1999, -2, 1000001}, 1199 {0, 1, 2000, -2, 1}, 1200 {0, 1, -1, 0, 1000001}, 1201 {0, 1, -2, 0, 2000001}, 1202 {0, 1, -1000, 1, 1}, 1203 {0, 1, -1001, 1, 1000001}, 1204 1205 {0, 1000000, 0, 0, 1000000}, 1206 {0, 1000000, 1, 0, 0}, 1207 {0, 1000000, 998, -1, 3000000}, 1208 {0, 1000000, 999, -1, 2000000}, 1209 {0, 1000000, 1000, -1, 1000000}, 1210 {0, 1000000, 1998, -2, 3000000}, 1211 {0, 1000000, 1999, -2, 2000000}, 1212 {0, 1000000, 2000, -2, 1000000}, 1213 {0, 1000000, -1, 0, 2000000}, 1214 {0, 1000000, -2, 0, 3000000}, 1215 {0, 1000000, -999, 1, 0}, 1216 {0, 1000000, -1000, 1, 1000000}, 1217 {0, 1000000, -1001, 1, 2000000}, 1218 {0, 1000000, -1002, 1, 3000000}, 1219 1220 {0, 999999999, 0, 0, 999999999}, 1221 {0, 999999999, 1, 0, 998999999}, 1222 {0, 999999999, 999, 0, 999999}, 1223 {0, 999999999, 1000, -1, 999999999}, 1224 {0, 999999999, 1001, -1, 998999999}, 1225 {0, 999999999, -1, 1, 999999}, 1226 {0, 999999999, -1000, 1, 999999999}, 1227 {0, 999999999, -1001, 2, 999999}, 1228 1229 {0, 0, Long.MAX_VALUE, -(Long.MAX_VALUE / 1000) - 1, (int) -(Long.MAX_VALUE % 1000) * 1000000 + 1000000000}, 1230 {0, 0, Long.MIN_VALUE, -(Long.MIN_VALUE / 1000), (int) -(Long.MIN_VALUE % 1000) * 1000000}, 1231 }; 1232 } 1233 1234 @Test(dataProvider="MinusMillis") 1235 public void minusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1236 Instant i = Instant.ofEpochSecond(seconds, nanos); 1237 i = i.minusMillis(amount); 1238 assertEquals(i.getEpochSecond(), expectedSeconds); 1239 assertEquals(i.getNano(), expectedNanoOfSecond); 1240 } 1241 1242 @Test(dataProvider="MinusMillis") 1243 public void minusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1244 Instant i = Instant.ofEpochSecond(seconds + 1, nanos); 1245 i = i.minusMillis(amount); 1246 assertEquals(i.getEpochSecond(), expectedSeconds + 1); 1247 assertEquals(i.getNano(), expectedNanoOfSecond); 1248 } 1249 1250 @Test(dataProvider="MinusMillis") 1251 public void minusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1252 Instant i = Instant.ofEpochSecond(seconds - 1, nanos); 1253 i = i.minusMillis(amount); 1254 assertEquals(i.getEpochSecond(), expectedSeconds - 1); 1255 assertEquals(i.getNano(), expectedNanoOfSecond); 1256 } 1257 1258 @Test 1259 public void minusMillis_long_max() { 1260 Instant i = Instant.ofEpochSecond(MAX_SECOND, 998999999); 1261 i = i.minusMillis(-1); 1262 assertEquals(i.getEpochSecond(), MAX_SECOND); 1263 assertEquals(i.getNano(), 999999999); 1264 } 1265 1266 @Test(expectedExceptions=DateTimeException.class) 1267 public void minusMillis_long_overflowTooBig() { 1268 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999000000); 1269 i.minusMillis(-1); 1270 } 1271 1272 @Test 1273 public void minusMillis_long_min() { 1274 Instant i = Instant.ofEpochSecond(MIN_SECOND, 1000000); 1275 i = i.minusMillis(1); 1276 assertEquals(i.getEpochSecond(), MIN_SECOND); 1277 assertEquals(i.getNano(), 0); 1278 } 1279 1280 @Test(expectedExceptions=DateTimeException.class) 1281 public void minusMillis_long_overflowTooSmall() { 1282 Instant i = Instant.ofEpochSecond(MIN_SECOND, 0); 1283 i.minusMillis(1); 1284 } 1285 1286 //----------------------------------------------------------------------- 1287 @DataProvider(name="MinusNanos") 1288 Object[][] provider_minusNanos_long() { 1289 return new Object[][] { 1290 {0, 0, 0, 0, 0}, 1291 {0, 0, 1, -1, 999999999}, 1292 {0, 0, 999999999, -1, 1}, 1293 {0, 0, 1000000000, -1, 0}, 1294 {0, 0, 1000000001, -2, 999999999}, 1295 {0, 0, 1999999999, -2, 1}, 1296 {0, 0, 2000000000, -2, 0}, 1297 {0, 0, -1, 0, 1}, 1298 {0, 0, -999999999, 0, 999999999}, 1299 {0, 0, -1000000000, 1, 0}, 1300 {0, 0, -1000000001, 1, 1}, 1301 {0, 0, -1999999999, 1, 999999999}, 1302 1303 {1, 0, 0, 1, 0}, 1304 {1, 0, 1, 0, 999999999}, 1305 {1, 0, 999999999, 0, 1}, 1306 {1, 0, 1000000000, 0, 0}, 1307 {1, 0, 1000000001, -1, 999999999}, 1308 {1, 0, 1999999999, -1, 1}, 1309 {1, 0, 2000000000, -1, 0}, 1310 {1, 0, -1, 1, 1}, 1311 {1, 0, -999999999, 1, 999999999}, 1312 {1, 0, -1000000000, 2, 0}, 1313 {1, 0, -1000000001, 2, 1}, 1314 {1, 0, -1999999999, 2, 999999999}, 1315 1316 {-1, 0, 0, -1, 0}, 1317 {-1, 0, 1, -2, 999999999}, 1318 {-1, 0, 999999999, -2, 1}, 1319 {-1, 0, 1000000000, -2, 0}, 1320 {-1, 0, 1000000001, -3, 999999999}, 1321 {-1, 0, 1999999999, -3, 1}, 1322 {-1, 0, 2000000000, -3, 0}, 1323 {-1, 0, -1, -1, 1}, 1324 {-1, 0, -999999999, -1, 999999999}, 1325 {-1, 0, -1000000000, 0, 0}, 1326 {-1, 0, -1000000001, 0, 1}, 1327 {-1, 0, -1999999999, 0, 999999999}, 1328 1329 {1, 1, 0, 1, 1}, 1330 {1, 1, 1, 1, 0}, 1331 {1, 1, 999999998, 0, 3}, 1332 {1, 1, 999999999, 0, 2}, 1333 {1, 1, 1000000000, 0, 1}, 1334 {1, 1, 1999999998, -1, 3}, 1335 {1, 1, 1999999999, -1, 2}, 1336 {1, 1, 2000000000, -1, 1}, 1337 {1, 1, -1, 1, 2}, 1338 {1, 1, -2, 1, 3}, 1339 {1, 1, -1000000000, 2, 1}, 1340 {1, 1, -1000000001, 2, 2}, 1341 {1, 1, -1000000002, 2, 3}, 1342 {1, 1, -2000000000, 3, 1}, 1343 1344 {1, 999999999, 0, 1, 999999999}, 1345 {1, 999999999, 1, 1, 999999998}, 1346 {1, 999999999, 999999999, 1, 0}, 1347 {1, 999999999, 1000000000, 0, 999999999}, 1348 {1, 999999999, 1000000001, 0, 999999998}, 1349 {1, 999999999, -1, 2, 0}, 1350 {1, 999999999, -1000000000, 2, 999999999}, 1351 {1, 999999999, -1000000001, 3, 0}, 1352 {1, 999999999, -1999999999, 3, 999999998}, 1353 {1, 999999999, -2000000000, 3, 999999999}, 1354 1355 {MAX_SECOND, 0, -999999999, MAX_SECOND, 999999999}, 1356 {MAX_SECOND - 1, 0, -1999999999, MAX_SECOND, 999999999}, 1357 {MIN_SECOND, 1, 1, MIN_SECOND, 0}, 1358 {MIN_SECOND + 1, 1, 1000000001, MIN_SECOND, 0}, 1359 1360 {0, 0, Long.MAX_VALUE, -(Long.MAX_VALUE / 1000000000) - 1, (int) -(Long.MAX_VALUE % 1000000000) + 1000000000}, 1361 {0, 0, Long.MIN_VALUE, -(Long.MIN_VALUE / 1000000000), (int) -(Long.MIN_VALUE % 1000000000)}, 1362 }; 1363 } 1364 1365 @Test(dataProvider="MinusNanos") 1366 public void minusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) { 1367 Instant i = Instant.ofEpochSecond(seconds, nanos); 1368 i = i.minusNanos(amount); 1369 assertEquals(i.getEpochSecond(), expectedSeconds); 1370 assertEquals(i.getNano(), expectedNanoOfSecond); 1371 } 1372 1373 @Test(expectedExceptions=DateTimeException.class) 1374 public void minusNanos_long_overflowTooBig() { 1375 Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999); 1376 i.minusNanos(-1); 1377 } 1378 1379 @Test(expectedExceptions=DateTimeException.class) 1380 public void minusNanos_long_overflowTooSmall() { 1381 Instant i = Instant.ofEpochSecond(MIN_SECOND, 0); 1382 i.minusNanos(1); 1383 } 1384 1385 //----------------------------------------------------------------------- 1386 // truncatedTo() 1387 //----------------------------------------------------------------------- 1388 @Test 1389 public void test_truncatedTo() { 1390 assertEquals(Instant.ofEpochSecond(2L, 1000000).truncatedTo(ChronoUnit.SECONDS), Instant.ofEpochSecond(2L)); 1391 assertEquals(Instant.ofEpochSecond(2L, -1000000).truncatedTo(ChronoUnit.SECONDS), Instant.ofEpochSecond(1L)); 1392 assertEquals(Instant.ofEpochSecond(0L, -1000000).truncatedTo(ChronoUnit.SECONDS), Instant.ofEpochSecond(-1L)); 1393 assertEquals(Instant.ofEpochSecond(-1L).truncatedTo(ChronoUnit.SECONDS), Instant.ofEpochSecond(-1L)); 1394 assertEquals(Instant.ofEpochSecond(-1L, -1000000).truncatedTo(ChronoUnit.SECONDS), Instant.ofEpochSecond(-2L)); 1395 assertEquals(Instant.ofEpochSecond(-2L).truncatedTo(ChronoUnit.SECONDS), Instant.ofEpochSecond(-2L)); 1396 } 1397 1398 //----------------------------------------------------------------------- 1399 // toEpochMilli() 1400 //----------------------------------------------------------------------- 1401 @Test 1402 public void test_toEpochMilli() { 1403 assertEquals(Instant.ofEpochSecond(1L, 1000000).toEpochMilli(), 1001L); 1404 assertEquals(Instant.ofEpochSecond(1L, 2000000).toEpochMilli(), 1002L); 1405 assertEquals(Instant.ofEpochSecond(1L, 567).toEpochMilli(), 1000L); 1406 assertEquals(Instant.ofEpochSecond(Long.MAX_VALUE / 1000).toEpochMilli(), (Long.MAX_VALUE / 1000) * 1000); 1407 assertEquals(Instant.ofEpochSecond(Long.MIN_VALUE / 1000).toEpochMilli(), (Long.MIN_VALUE / 1000) * 1000); 1408 assertEquals(Instant.ofEpochSecond(0L, -1000000).toEpochMilli(), -1L); 1409 assertEquals(Instant.ofEpochSecond(0L, 1000000).toEpochMilli(), 1); 1410 assertEquals(Instant.ofEpochSecond(0L, 999999).toEpochMilli(), 0); 1411 assertEquals(Instant.ofEpochSecond(0L, 1).toEpochMilli(), 0); 1412 assertEquals(Instant.ofEpochSecond(0L, 0).toEpochMilli(), 0); 1413 assertEquals(Instant.ofEpochSecond(0L, -1).toEpochMilli(), -1L); 1414 assertEquals(Instant.ofEpochSecond(0L, -999999).toEpochMilli(), -1L); 1415 assertEquals(Instant.ofEpochSecond(0L, -1000000).toEpochMilli(), -1L); 1416 assertEquals(Instant.ofEpochSecond(0L, -1000001).toEpochMilli(), -2L); 1417 } 1418 1419 @Test(expectedExceptions=ArithmeticException.class) 1420 public void test_toEpochMilli_tooBig() { 1421 Instant.ofEpochSecond(Long.MAX_VALUE / 1000 + 1).toEpochMilli(); 1422 } 1423 1424 @Test(expectedExceptions=ArithmeticException.class) 1425 public void test_toEpochMilli_tooBigDueToNanos() { 1426 Instant.ofEpochMilli(Long.MAX_VALUE).plusMillis(1).toEpochMilli(); 1427 } 1428 1429 @Test(expectedExceptions=ArithmeticException.class) 1430 public void test_toEpochMilli_tooSmall() { 1431 Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1).toEpochMilli(); 1432 } 1433 1434 @Test(expectedExceptions=ArithmeticException.class) 1435 public void test_toEpochMilli_tooSmallDueToNanos() { 1436 Instant.ofEpochMilli(Long.MIN_VALUE).minusMillis(1).toEpochMilli(); 1437 } 1438 1439 //----------------------------------------------------------------------- 1440 // compareTo() 1441 //----------------------------------------------------------------------- 1442 @Test 1443 public void test_comparisons() { 1444 doTest_comparisons_Instant( 1445 Instant.ofEpochSecond(-2L, 0), 1446 Instant.ofEpochSecond(-2L, 999999998), 1447 Instant.ofEpochSecond(-2L, 999999999), 1448 Instant.ofEpochSecond(-1L, 0), 1449 Instant.ofEpochSecond(-1L, 1), 1450 Instant.ofEpochSecond(-1L, 999999998), 1451 Instant.ofEpochSecond(-1L, 999999999), 1452 Instant.ofEpochSecond(0L, 0), 1453 Instant.ofEpochSecond(0L, 1), 1454 Instant.ofEpochSecond(0L, 2), 1455 Instant.ofEpochSecond(0L, 999999999), 1456 Instant.ofEpochSecond(1L, 0), 1457 Instant.ofEpochSecond(2L, 0) 1458 ); 1459 } 1460 1461 void doTest_comparisons_Instant(Instant... instants) { 1462 for (int i = 0; i < instants.length; i++) { 1463 Instant a = instants[i]; 1464 for (int j = 0; j < instants.length; j++) { 1465 Instant b = instants[j]; 1466 if (i < j) { 1467 assertEquals(a.compareTo(b) < 0, true, a + " <=> " + b); 1468 assertEquals(a.isBefore(b), true, a + " <=> " + b); 1469 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1470 assertEquals(a.equals(b), false, a + " <=> " + b); 1471 } else if (i > j) { 1472 assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b); 1473 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1474 assertEquals(a.isAfter(b), true, a + " <=> " + b); 1475 assertEquals(a.equals(b), false, a + " <=> " + b); 1476 } else { 1477 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 1478 assertEquals(a.isBefore(b), false, a + " <=> " + b); 1479 assertEquals(a.isAfter(b), false, a + " <=> " + b); 1480 assertEquals(a.equals(b), true, a + " <=> " + b); 1481 } 1482 } 1483 } 1484 } 1485 1486 @Test(expectedExceptions=NullPointerException.class) 1487 public void test_compareTo_ObjectNull() { 1488 Instant a = Instant.ofEpochSecond(0L, 0); 1489 a.compareTo(null); 1490 } 1491 1492 @Test(expectedExceptions=NullPointerException.class) 1493 public void test_isBefore_ObjectNull() { 1494 Instant a = Instant.ofEpochSecond(0L, 0); 1495 a.isBefore(null); 1496 } 1497 1498 @Test(expectedExceptions=NullPointerException.class) 1499 public void test_isAfter_ObjectNull() { 1500 Instant a = Instant.ofEpochSecond(0L, 0); 1501 a.isAfter(null); 1502 } 1503 1504 @Test(expectedExceptions=ClassCastException.class) 1505 @SuppressWarnings({"unchecked", "rawtypes"}) 1506 public void compareToNonInstant() { 1507 Comparable c = Instant.ofEpochSecond(0L); 1508 c.compareTo(new Object()); 1509 } 1510 1511 //----------------------------------------------------------------------- 1512 // equals() 1513 //----------------------------------------------------------------------- 1514 @Test 1515 public void test_equals() { 1516 Instant test5a = Instant.ofEpochSecond(5L, 20); 1517 Instant test5b = Instant.ofEpochSecond(5L, 20); 1518 Instant test5n = Instant.ofEpochSecond(5L, 30); 1519 Instant test6 = Instant.ofEpochSecond(6L, 20); 1520 1521 assertEquals(test5a.equals(test5a), true); 1522 assertEquals(test5a.equals(test5b), true); 1523 assertEquals(test5a.equals(test5n), false); 1524 assertEquals(test5a.equals(test6), false); 1525 1526 assertEquals(test5b.equals(test5a), true); 1527 assertEquals(test5b.equals(test5b), true); 1528 assertEquals(test5b.equals(test5n), false); 1529 assertEquals(test5b.equals(test6), false); 1530 1531 assertEquals(test5n.equals(test5a), false); 1532 assertEquals(test5n.equals(test5b), false); 1533 assertEquals(test5n.equals(test5n), true); 1534 assertEquals(test5n.equals(test6), false); 1535 1536 assertEquals(test6.equals(test5a), false); 1537 assertEquals(test6.equals(test5b), false); 1538 assertEquals(test6.equals(test5n), false); 1539 assertEquals(test6.equals(test6), true); 1540 } 1541 1542 @Test 1543 public void test_equals_null() { 1544 Instant test5 = Instant.ofEpochSecond(5L, 20); 1545 assertEquals(test5.equals(null), false); 1546 } 1547 1548 @Test 1549 public void test_equals_otherClass() { 1550 Instant test5 = Instant.ofEpochSecond(5L, 20); 1551 assertEquals(test5.equals(""), false); 1552 } 1553 1554 //----------------------------------------------------------------------- 1555 // hashCode() 1556 //----------------------------------------------------------------------- 1557 @Test 1558 public void test_hashCode() { 1559 Instant test5a = Instant.ofEpochSecond(5L, 20); 1560 Instant test5b = Instant.ofEpochSecond(5L, 20); 1561 Instant test5n = Instant.ofEpochSecond(5L, 30); 1562 Instant test6 = Instant.ofEpochSecond(6L, 20); 1563 1564 assertEquals(test5a.hashCode() == test5a.hashCode(), true); 1565 assertEquals(test5a.hashCode() == test5b.hashCode(), true); 1566 assertEquals(test5b.hashCode() == test5b.hashCode(), true); 1567 1568 assertEquals(test5a.hashCode() == test5n.hashCode(), false); 1569 assertEquals(test5a.hashCode() == test6.hashCode(), false); 1570 } 1571 1572 //----------------------------------------------------------------------- 1573 // toString() 1574 //----------------------------------------------------------------------- 1575 @DataProvider(name="toStringParse") 1576 Object[][] data_toString() { 1577 return new Object[][] { 1578 {Instant.ofEpochSecond(65L, 567), "1970-01-01T00:01:05.000000567Z"}, 1579 {Instant.ofEpochSecond(1, 0), "1970-01-01T00:00:01Z"}, 1580 {Instant.ofEpochSecond(60, 0), "1970-01-01T00:01:00Z"}, 1581 {Instant.ofEpochSecond(3600, 0), "1970-01-01T01:00:00Z"}, 1582 {Instant.ofEpochSecond(-1, 0), "1969-12-31T23:59:59Z"}, 1583 1584 {LocalDateTime.of(0, 1, 2, 0, 0).toInstant(ZoneOffset.UTC), "0000-01-02T00:00:00Z"}, 1585 {LocalDateTime.of(0, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "0000-01-01T12:30:00Z"}, 1586 {LocalDateTime.of(0, 1, 1, 0, 0, 0, 1).toInstant(ZoneOffset.UTC), "0000-01-01T00:00:00.000000001Z"}, 1587 {LocalDateTime.of(0, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "0000-01-01T00:00:00Z"}, 1588 1589 {LocalDateTime.of(-1, 12, 31, 23, 59, 59, 999999999).toInstant(ZoneOffset.UTC), "-0001-12-31T23:59:59.999999999Z"}, 1590 {LocalDateTime.of(-1, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-0001-12-31T12:30:00Z"}, 1591 {LocalDateTime.of(-1, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-0001-12-30T12:30:00Z"}, 1592 1593 {LocalDateTime.of(-9999, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "-9999-01-02T12:30:00Z"}, 1594 {LocalDateTime.of(-9999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "-9999-01-01T12:30:00Z"}, 1595 {LocalDateTime.of(-9999, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "-9999-01-01T00:00:00Z"}, 1596 1597 {LocalDateTime.of(-10000, 12, 31, 23, 59, 59, 999999999).toInstant(ZoneOffset.UTC), "-10000-12-31T23:59:59.999999999Z"}, 1598 {LocalDateTime.of(-10000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-10000-12-31T12:30:00Z"}, 1599 {LocalDateTime.of(-10000, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-10000-12-30T12:30:00Z"}, 1600 {LocalDateTime.of(-15000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-15000-12-31T12:30:00Z"}, 1601 1602 {LocalDateTime.of(-19999, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "-19999-01-02T12:30:00Z"}, 1603 {LocalDateTime.of(-19999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "-19999-01-01T12:30:00Z"}, 1604 {LocalDateTime.of(-19999, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "-19999-01-01T00:00:00Z"}, 1605 1606 {LocalDateTime.of(-20000, 12, 31, 23, 59, 59, 999999999).toInstant(ZoneOffset.UTC), "-20000-12-31T23:59:59.999999999Z"}, 1607 {LocalDateTime.of(-20000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-20000-12-31T12:30:00Z"}, 1608 {LocalDateTime.of(-20000, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "-20000-12-30T12:30:00Z"}, 1609 {LocalDateTime.of(-25000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "-25000-12-31T12:30:00Z"}, 1610 1611 {LocalDateTime.of(9999, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "9999-12-30T12:30:00Z"}, 1612 {LocalDateTime.of(9999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "9999-12-31T12:30:00Z"}, 1613 {LocalDateTime.of(9999, 12, 31, 23, 59, 59, 999999999).toInstant(ZoneOffset.UTC), "9999-12-31T23:59:59.999999999Z"}, 1614 1615 {LocalDateTime.of(10000, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "+10000-01-01T00:00:00Z"}, 1616 {LocalDateTime.of(10000, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "+10000-01-01T12:30:00Z"}, 1617 {LocalDateTime.of(10000, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "+10000-01-02T12:30:00Z"}, 1618 {LocalDateTime.of(15000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+15000-12-31T12:30:00Z"}, 1619 1620 {LocalDateTime.of(19999, 12, 30, 12, 30).toInstant(ZoneOffset.UTC), "+19999-12-30T12:30:00Z"}, 1621 {LocalDateTime.of(19999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+19999-12-31T12:30:00Z"}, 1622 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 999999999).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.999999999Z"}, 1623 1624 {LocalDateTime.of(20000, 1, 1, 0, 0).toInstant(ZoneOffset.UTC), "+20000-01-01T00:00:00Z"}, 1625 {LocalDateTime.of(20000, 1, 1, 12, 30).toInstant(ZoneOffset.UTC), "+20000-01-01T12:30:00Z"}, 1626 {LocalDateTime.of(20000, 1, 2, 12, 30).toInstant(ZoneOffset.UTC), "+20000-01-02T12:30:00Z"}, 1627 {LocalDateTime.of(25000, 12, 31, 12, 30).toInstant(ZoneOffset.UTC), "+25000-12-31T12:30:00Z"}, 1628 1629 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 9999999).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.009999999Z"}, 1630 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 999999000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.999999Z"}, 1631 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 9999000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.009999Z"}, 1632 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 123000000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.123Z"}, 1633 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 100000000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.100Z"}, 1634 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 20000000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.020Z"}, 1635 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 3000000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.003Z"}, 1636 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 400000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.000400Z"}, 1637 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 50000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.000050Z"}, 1638 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 6000).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.000006Z"}, 1639 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 700).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.000000700Z"}, 1640 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 80).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.000000080Z"}, 1641 {LocalDateTime.of(19999, 12, 31, 23, 59, 59, 9).toInstant(ZoneOffset.UTC), "+19999-12-31T23:59:59.000000009Z"}, 1642 {LocalDateTime.of(-999999999, 1, 1, 12, 30).toInstant(ZoneOffset.UTC).minus(1, DAYS), "-1000000000-12-31T12:30:00Z"}, 1643 1644 {LocalDateTime.of(999999999, 12, 31, 12, 30).toInstant(ZoneOffset.UTC).plus(1, DAYS), "+1000000000-01-01T12:30:00Z"}, 1645 1646 {Instant.MIN, "-1000000000-01-01T00:00:00Z"}, 1647 {Instant.MAX, "+1000000000-12-31T23:59:59.999999999Z"}, 1648 }; 1649 } 1650 1651 @Test(dataProvider="toStringParse") 1652 public void test_toString(Instant instant, String expected) { 1653 assertEquals(instant.toString(), expected); 1654 } 1655 1656 @Test(dataProvider="toStringParse") 1657 public void test_parse(Instant instant, String text) { 1658 assertEquals(Instant.parse(text), instant); 1659 } 1660 1661 @Test(dataProvider="toStringParse") 1662 public void test_parseLowercase(Instant instant, String text) { 1663 assertEquals(Instant.parse(text.toLowerCase(Locale.ENGLISH)), instant); 1664 } 1665 1666 } 1667