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