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