/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tck.java.time;
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.HOURS;
import static java.time.temporal.ChronoUnit.MICROS;
import static java.time.temporal.ChronoUnit.MILLIS;
import static java.time.temporal.ChronoUnit.MINUTES;
import static java.time.temporal.ChronoUnit.MONTHS;
import static java.time.temporal.ChronoUnit.NANOS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.time.temporal.ChronoUnit.WEEKS;
import static java.time.temporal.ChronoUnit.YEARS;
import static org.testng.Assert .assertEquals;
import static org.testng.Assert .assertTrue;
import static org.testng.Assert .fail;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.time.Clock;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.JulianFields;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Test Instant.
*
* @bug 8133022
*/
@Test
public class TCKInstant extends AbstractDateTimeTest {
private static final long MIN_SECOND = Instant.MIN.getEpochSecond();
private static final long MAX_SECOND = Instant.MAX.getEpochSecond();
private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris" );
private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
private Instant TEST_12345_123456789;
@BeforeMethod
public void setUp() {
TEST_12345_123456789 = Instant.ofEpochSecond(12345, 123456789);
}
//-----------------------------------------------------------------------
@Override
protected List<TemporalAccessor> samples() {
TemporalAccessor[] array = {TEST_12345_123456789, Instant.MIN, Instant.MAX, Instant.EPOCH};
return Arrays.asList(array);
}
@Override
protected List<TemporalField> validFields() {
TemporalField[] array = {
NANO_OF_SECOND,
MICRO_OF_SECOND,
MILLI_OF_SECOND,
INSTANT_SECONDS,
};
return Arrays.asList(array);
}
@Override
protected List<TemporalField> invalidFields() {
List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
list.removeAll(validFields());
list.add(JulianFields.JULIAN_DAY);
list.add(JulianFields.MODIFIED_JULIAN_DAY);
list.add(JulianFields.RATA_DIE);
return list;
}
//-----------------------------------------------------------------------
private void check(Instant instant, long epochSecs, int nos) {
assertEquals(instant.getEpochSecond(), epochSecs);
assertEquals(instant.getNano(), nos);
assertEquals(instant, instant);
assertEquals(instant.hashCode(), instant.hashCode());
}
//-----------------------------------------------------------------------
@Test
public void constant_EPOCH() {
check(Instant.EPOCH, 0, 0);
}
@Test
public void constant_MIN() {
check(Instant.MIN, -31557014167219200L, 0);
}
@Test
public void constant_MAX() {
check(Instant.MAX, 31556889864403199L, 999_999_999);
}
//-----------------------------------------------------------------------
// now()
//-----------------------------------------------------------------------
@Test
public void now() {
Instant expected = Instant.now(Clock.systemUTC());
Instant test = Instant.now();
long diff = Math.abs(test.toEpochMilli() - expected.toEpochMilli());
assertTrue(diff < 100); // less than 0.1 secs
}
//-----------------------------------------------------------------------
// now(Clock)
//-----------------------------------------------------------------------
@Test(expectedExceptions=NullPointerException.class )
public void now_Clock_nullClock() {
Instant.now(null );
}
@Test
public void now_Clock_allSecsInDay_utc() {
for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L);
Clock clock = Clock.fixed(expected, ZoneOffset.UTC);
Instant test = Instant.now(clock);
assertEquals(test, expected);
}
}
@Test
public void now_Clock_allSecsInDay_beforeEpoch() {
for (int i =-1; i >= -(24 * 60 * 60); i--) {
Instant expected = Instant.ofEpochSecond(i).plusNanos(123456789L);
Clock clock = Clock.fixed(expected, ZoneOffset.UTC);
Instant test = Instant.now(clock);
assertEquals(test, expected);
}
}
//-----------------------------------------------------------------------
// ofEpochSecond(long)
//-----------------------------------------------------------------------
@Test
public void factory_seconds_long() {
for (long i = -2; i <= 2; i++) {
Instant t = Instant.ofEpochSecond(i);
assertEquals(t.getEpochSecond(), i);
assertEquals(t.getNano(), 0);
}
}
//-----------------------------------------------------------------------
// ofEpochSecond(long,long)
//-----------------------------------------------------------------------
@Test
public void factory_seconds_long_long() {
for (long i = -2; i <= 2; i++) {
for (int j = 0; j < 10; j++) {
Instant t = Instant.ofEpochSecond(i, j);
assertEquals(t.getEpochSecond(), i);
assertEquals(t.getNano(), j);
}
for (int j = -10; j < 0; j++) {
Instant t = Instant.ofEpochSecond(i, j);
assertEquals(t.getEpochSecond(), i - 1);
assertEquals(t.getNano(), j + 1000000000);
}
for (int j = 999999990; j < 1000000000; j++) {
Instant t = Instant.ofEpochSecond(i, j);
assertEquals(t.getEpochSecond(), i);
assertEquals(t.getNano(), j);
}
}
}
@Test
public void factory_seconds_long_long_nanosNegativeAdjusted() {
Instant test = Instant.ofEpochSecond(2L, -1);
assertEquals(test.getEpochSecond(), 1);
assertEquals(test.getNano(), 999999999);
}
@Test(expectedExceptions=DateTimeException.class )
public void factory_seconds_long_long_tooBig() {
Instant.ofEpochSecond(MAX_SECOND, 1000000000);
}
@Test(expectedExceptions=ArithmeticException.class )
public void factory_seconds_long_long_tooBigBig() {
Instant.ofEpochSecond(Long .MAX_VALUE, Long .MAX_VALUE);
}
//-----------------------------------------------------------------------
// ofEpochMilli(long)
//-----------------------------------------------------------------------
@DataProvider(name="MillisInstantNoNanos" )
Object[][] provider_factory_millis_long() {
return new Object[][] {
{0, 0, 0},
{1, 0, 1000000},
{2, 0, 2000000},
{999, 0, 999000000},
{1000, 1, 0},
{1001, 1, 1000000},
{-1, -1, 999000000},
{-2, -1, 998000000},
{-999, -1, 1000000},
{-1000, -1, 0},
{-1001, -2, 999000000},
};
}
@Test(dataProvider="MillisInstantNoNanos" )
public void factory_millis_long(long millis, long expectedSeconds, int expectedNanoOfSecond) {
Instant t = Instant.ofEpochMilli(millis);
assertEquals(t.getEpochSecond(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
//-----------------------------------------------------------------------
// parse(String)
//-----------------------------------------------------------------------
// see also parse tests under toString()
@DataProvider(name="Parse" )
Object[][] provider_factory_parse() {
return new Object[][] {
{"1970-01-01T00:00:00Z" , 0, 0},
{"1970-01-01t00:00:00Z" , 0, 0},
{"1970-01-01T00:00:00z" , 0, 0},
{"1970-01-01T00:00:00.0Z" , 0, 0},
{"1970-01-01T00:00:00.000000000Z" , 0, 0},
{"1970-01-01T00:00:00.000000001Z" , 0, 1},
{"1970-01-01T00:00:00.100000000Z" , 0, 100000000},
{"1970-01-01T00:00:01Z" , 1, 0},
{"1970-01-01T00:01:00Z" , 60, 0},
{"1970-01-01T00:01:01Z" , 61, 0},
{"1970-01-01T00:01:01.000000001Z" , 61, 1},
{"1970-01-01T01:00:00.000000000Z" , 3600, 0},
{"1970-01-01T01:01:01.000000001Z" , 3661, 1},
{"1970-01-02T01:01:01.100000000Z" , 90061, 100000000},
};
}
@Test(dataProvider="Parse" )
public void factory_parse(String text, long expectedEpochSeconds, int expectedNanoOfSecond) {
Instant t = Instant.parse(text);
assertEquals(t.getEpochSecond(), expectedEpochSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="Parse" )
public void factory_parseLowercase(String text, long expectedEpochSeconds, int expectedNanoOfSecond) {
Instant t = Instant.parse(text.toLowerCase(Locale.ENGLISH));
assertEquals(t.getEpochSecond(), expectedEpochSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
// TODO: should comma be accepted?
// @Test(dataProvider="Parse")
// public void factory_parse_comma(String text, long expectedEpochSeconds, int expectedNanoOfSecond) {
// text = text.replace('.', ',');
// Instant t = Instant.parse(text);
// assertEquals(t.getEpochSecond(), expectedEpochSeconds);
// assertEquals(t.getNano(), expectedNanoOfSecond);
// }
@DataProvider(name="ParseFailures" )
Object[][] provider_factory_parseFailures() {
return new Object[][] {
{"" },
{"Z" },
{"1970-01-01T00:00:00" },
{"1970-01-01T00:00:0Z" },
{"1970-01-01T00:0:00Z" },
{"1970-01-01T0:00:00Z" },
{"1970-01-01T00:00:00.0000000000Z" },
};
}
@Test(dataProvider="ParseFailures" , expectedExceptions=DateTimeParseException.class )
public void factory_parseFailures(String text) {
Instant.parse(text);
}
@Test(dataProvider="ParseFailures" , expectedExceptions=DateTimeParseException.class )
public void factory_parseFailures_comma(String text) {
text = text.replace('.' , ',' );
Instant.parse(text);
}
@Test(expectedExceptions=NullPointerException.class )
public void factory_parse_nullText() {
Instant.parse(null );
}
//-----------------------------------------------------------------------
// get(TemporalField)
//-----------------------------------------------------------------------
@Test
public void test_get_TemporalField() {
Instant test = TEST_12345_123456789;
assertEquals(test.get(ChronoField.NANO_OF_SECOND), 123456789);
assertEquals(test.get(ChronoField.MICRO_OF_SECOND), 123456);
assertEquals(test.get(ChronoField.MILLI_OF_SECOND), 123);
}
@Test
public void test_getLong_TemporalField() {
Instant test = TEST_12345_123456789;
assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 123456789);
assertEquals(test.getLong(ChronoField.MICRO_OF_SECOND), 123456);
assertEquals(test.getLong(ChronoField.MILLI_OF_SECOND), 123);
assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), 12345);
}
//-----------------------------------------------------------------------
// query(TemporalQuery)
//-----------------------------------------------------------------------
@DataProvider(name="query" )
Object[][] data_query() {
return new Object[][] {
{TEST_12345_123456789, TemporalQueries.chronology(), null },
{TEST_12345_123456789, TemporalQueries.zoneId(), null },
{TEST_12345_123456789, TemporalQueries.precision(), NANOS},
{TEST_12345_123456789, TemporalQueries.zone(), null },
{TEST_12345_123456789, TemporalQueries.offset(), null },
{TEST_12345_123456789, TemporalQueries.localDate(), null },
{TEST_12345_123456789, TemporalQueries.localTime(), null },
};
}
@Test(dataProvider="query" )
public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
assertEquals(temporal.query(query), expected);
}
@Test(dataProvider="query" )
public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
assertEquals(query.queryFrom(temporal), expected);
}
@Test(expectedExceptions=NullPointerException.class )
public void test_query_null() {
TEST_12345_123456789.query(null );
}
//-----------------------------------------------------------------------
// adjustInto(Temporal)
//-----------------------------------------------------------------------
@DataProvider(name="adjustInto" )
Object[][] data_adjustInto() {
return new Object[][]{
{Instant.ofEpochSecond(10, 200), Instant.ofEpochSecond(20), Instant.ofEpochSecond(10, 200), null },
{Instant.ofEpochSecond(10, -200), Instant.now(), Instant.ofEpochSecond(10, -200), null },
{Instant.ofEpochSecond(-10), Instant.EPOCH, Instant.ofEpochSecond(-10), null },
{Instant.ofEpochSecond(10), Instant.MIN, Instant.ofEpochSecond(10), null },
{Instant.ofEpochSecond(10), Instant.MAX, Instant.ofEpochSecond(10), null },
{Instant.ofEpochSecond(10, 200), LocalDateTime.of(1970, 1, 1, 0, 0, 20).toInstant(ZoneOffset.UTC), Instant.ofEpochSecond(10, 200), null },
{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 },
{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 },
{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 },
{Instant.ofEpochSecond(10, 200), LocalDateTime.of(1970, 1, 1, 0, 0, 20), null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), null , null , NullPointerException.class },
};
}
@Test(dataProvider="adjustInto" )
public void test_adjustInto(Instant test, Temporal temporal, Temporal expected, Class <?> expectedEx) {
if (expectedEx == null ) {
Temporal result = test.adjustInto(temporal);
assertEquals(result, expected);
} else {
try {
Temporal result = test.adjustInto(temporal);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
//-----------------------------------------------------------------------
// with(TemporalAdjuster)
//-----------------------------------------------------------------------
@DataProvider(name="with" )
Object[][] data_with() {
return new Object[][]{
{Instant.ofEpochSecond(10, 200), Instant.ofEpochSecond(20), Instant.ofEpochSecond(20), null },
{Instant.ofEpochSecond(10), Instant.ofEpochSecond(20, -100), Instant.ofEpochSecond(20, -100), null },
{Instant.ofEpochSecond(-10), Instant.EPOCH, Instant.ofEpochSecond(0), null },
{Instant.ofEpochSecond(10), Instant.MIN, Instant.MIN, null },
{Instant.ofEpochSecond(10), Instant.MAX, Instant.MAX, null },
{Instant.ofEpochSecond(10, 200), LocalDateTime.of(1970, 1, 1, 0, 0, 20).toInstant(ZoneOffset.UTC), Instant.ofEpochSecond(20), null },
{Instant.ofEpochSecond(10, 200), LocalDateTime.of(1970, 1, 1, 0, 0, 20), null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), null , null , NullPointerException.class },
};
}
@Test(dataProvider="with" )
public void test_with_temporalAdjuster(Instant test, TemporalAdjuster adjuster, Instant expected, Class <?> expectedEx) {
if (expectedEx == null ) {
Instant result = test.with(adjuster);
assertEquals(result, expected);
} else {
try {
Instant result = test.with(adjuster);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
//-----------------------------------------------------------------------
// with(TemporalField, long)
//-----------------------------------------------------------------------
@DataProvider(name="with_longTemporalField" )
Object[][] data_with_longTemporalField() {
return new Object[][]{
{Instant.ofEpochSecond(10, 200), ChronoField.INSTANT_SECONDS, 100, Instant.ofEpochSecond(100, 200), null },
{Instant.ofEpochSecond(10, 200), ChronoField.INSTANT_SECONDS, 0, Instant.ofEpochSecond(0, 200), null },
{Instant.ofEpochSecond(10, 200), ChronoField.INSTANT_SECONDS, -100, Instant.ofEpochSecond(-100, 200), null },
{Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_SECOND, 100, Instant.ofEpochSecond(10, 100), null },
{Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_SECOND, 0, Instant.ofEpochSecond(10), null },
{Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_SECOND, 100, Instant.ofEpochSecond(10, 100*1000), null },
{Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_SECOND, 0, Instant.ofEpochSecond(10), null },
{Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_SECOND, 100, Instant.ofEpochSecond(10, 100*1000*1000), null },
{Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_SECOND, 0, Instant.ofEpochSecond(10), null },
{Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_SECOND, 1000000000L, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_SECOND, 1000000, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_SECOND, 1000, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.SECOND_OF_MINUTE, 1, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.SECOND_OF_DAY, 1, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.OFFSET_SECONDS, 1, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_DAY, 1, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.MINUTE_OF_HOUR, 1, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.MINUTE_OF_DAY, 1, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_DAY, 1, null , DateTimeException.class },
{Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_DAY, 1, null , DateTimeException.class },
};
}
@Test(dataProvider="with_longTemporalField" )
public void test_with_longTemporalField(Instant test, TemporalField field, long value, Instant expected, Class <?> expectedEx) {
if (expectedEx == null ) {
Instant result = test.with(field, value);
assertEquals(result, expected);
} else {
try {
Instant result = test.with(field, value);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
//-----------------------------------------------------------------------
// truncated(TemporalUnit)
//-----------------------------------------------------------------------
TemporalUnit NINETY_MINS = new TemporalUnit() {
@Override
public Duration getDuration() {
return Duration.ofMinutes(90);
}
@Override
public boolean isDurationEstimated() {
return false ;
}
@Override
public boolean isDateBased() {
return false ;
}
@Override
public boolean isTimeBased() {
return true ;
}
@Override
public boolean isSupportedBy(Temporal temporal) {
return false ;
}
@Override
public <R extends Temporal> R addTo(R temporal, long amount) {
throw new UnsupportedOperationException();
}
@Override
public long between(Temporal temporal1, Temporal temporal2) {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return "NinetyMins" ;
}
};
TemporalUnit NINETY_FIVE_MINS = new TemporalUnit() {
@Override
public Duration getDuration() {
return Duration.ofMinutes(95);
}
@Override
public boolean isDurationEstimated() {
return false ;
}
@Override
public boolean isDateBased() {
return false ;
}
@Override
public boolean isTimeBased() {
return false ;
}
@Override
public boolean isSupportedBy(Temporal temporal) {
return false ;
}
@Override
public <R extends Temporal> R addTo(R temporal, long amount) {
throw new UnsupportedOperationException();
}
@Override
public long between(Temporal temporal1, Temporal temporal2) {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return "NinetyFiveMins" ;
}
};
@DataProvider(name="truncatedToValid" )
Object[][] data_truncatedToValid() {
return new Object[][] {
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), NANOS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), MICROS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_000)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), MILLIS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 1230_00_000)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), SECONDS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 0)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), MINUTES, Instant.ofEpochSecond(86400 + 3600 + 60, 0)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), HOURS, Instant.ofEpochSecond(86400 + 3600, 0)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), DAYS, Instant.ofEpochSecond(86400, 0)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 0, 0)},
{Instant.ofEpochSecond(86400 + 7200 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 5400, 0)},
{Instant.ofEpochSecond(86400 + 10800 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 10800, 0)},
{Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_789), MINUTES, Instant.ofEpochSecond(-86400 - 3600 - 120, 0 )},
{Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_789), MICROS, Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_000)},
{Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 0), SECONDS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 0)},
{Instant.ofEpochSecond(-86400 - 3600 - 120, 0), MINUTES, Instant.ofEpochSecond(-86400 - 3600 - 120, 0)},
};
}
@Test(dataProvider="truncatedToValid" )
public void test_truncatedTo_valid(Instant input, TemporalUnit unit, Instant expected) {
assertEquals(input.truncatedTo(unit), expected);
}
@DataProvider(name="truncatedToInvalid" )
Object[][] data_truncatedToInvalid() {
return new Object[][] {
{Instant.ofEpochSecond(1, 123_456_789), NINETY_FIVE_MINS},
{Instant.ofEpochSecond(1, 123_456_789), WEEKS},
{Instant.ofEpochSecond(1, 123_456_789), MONTHS},
{Instant.ofEpochSecond(1, 123_456_789), YEARS},
};
}
@Test(dataProvider="truncatedToInvalid" , expectedExceptions=DateTimeException.class )
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
input.truncatedTo(unit);
}
@Test(expectedExceptions=NullPointerException.class )
public void test_truncatedTo_null() {
TEST_12345_123456789.truncatedTo(null );
}
//-----------------------------------------------------------------------
// plus(TemporalAmount)
//-----------------------------------------------------------------------
@DataProvider(name="plusTemporalAmount" )
Object[][] data_plusTemporalAmount() {
return new Object[][] {
{DAYS, MockSimplePeriod.of(1, DAYS), 86401, 0},
{HOURS, MockSimplePeriod.of(2, HOURS), 7201, 0},
{MINUTES, MockSimplePeriod.of(4, MINUTES), 241, 0},
{SECONDS, MockSimplePeriod.of(5, SECONDS), 6, 0},
{NANOS, MockSimplePeriod.of(6, NANOS), 1, 6},
{DAYS, MockSimplePeriod.of(10, DAYS), 864001, 0},
{HOURS, MockSimplePeriod.of(11, HOURS), 39601, 0},
{MINUTES, MockSimplePeriod.of(12, MINUTES), 721, 0},
{SECONDS, MockSimplePeriod.of(13, SECONDS), 14, 0},
{NANOS, MockSimplePeriod.of(14, NANOS), 1, 14},
{SECONDS, Duration.ofSeconds(20, 40), 21, 40},
{NANOS, Duration.ofSeconds(30, 300), 31, 300},
};
}
@Test(dataProvider="plusTemporalAmount" )
public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) {
Instant inst = Instant.ofEpochMilli(1000);
Instant actual = inst.plus(amount);
Instant expected = Instant.ofEpochSecond(seconds, nanos);
assertEquals(actual, expected, "plus(TemporalAmount) failed" );
}
@DataProvider(name="badTemporalAmount" )
Object[][] data_badPlusTemporalAmount() {
return new Object[][] {
{MockSimplePeriod.of(2, YEARS)},
{MockSimplePeriod.of(2, MONTHS)},
};
}
@Test(dataProvider="badTemporalAmount" ,expectedExceptions=DateTimeException.class )
public void test_badPlusTemporalAmount(TemporalAmount amount) {
Instant inst = Instant.ofEpochMilli(1000);
inst.plus(amount);
}
//-----------------------------------------------------------------------
@DataProvider(name="Plus" )
Object[][] provider_plus() {
return new Object[][] {
{MIN_SECOND, 0, -MIN_SECOND, 0, 0, 0},
{MIN_SECOND, 0, 1, 0, MIN_SECOND + 1, 0},
{MIN_SECOND, 0, 0, 500, MIN_SECOND, 500},
{MIN_SECOND, 0, 0, 1000000000, MIN_SECOND + 1, 0},
{MIN_SECOND + 1, 0, -1, 0, MIN_SECOND, 0},
{MIN_SECOND + 1, 0, 0, -500, MIN_SECOND, 999999500},
{MIN_SECOND + 1, 0, 0, -1000000000, MIN_SECOND, 0},
{-4, 666666667, -4, 666666667, -7, 333333334},
{-4, 666666667, -3, 0, -7, 666666667},
{-4, 666666667, -2, 0, -6, 666666667},
{-4, 666666667, -1, 0, -5, 666666667},
{-4, 666666667, -1, 333333334, -4, 1},
{-4, 666666667, -1, 666666667, -4, 333333334},
{-4, 666666667, -1, 999999999, -4, 666666666},
{-4, 666666667, 0, 0, -4, 666666667},
{-4, 666666667, 0, 1, -4, 666666668},
{-4, 666666667, 0, 333333333, -3, 0},
{-4, 666666667, 0, 666666666, -3, 333333333},
{-4, 666666667, 1, 0, -3, 666666667},
{-4, 666666667, 2, 0, -2, 666666667},
{-4, 666666667, 3, 0, -1, 666666667},
{-4, 666666667, 3, 333333333, 0, 0},
{-3, 0, -4, 666666667, -7, 666666667},
{-3, 0, -3, 0, -6, 0},
{-3, 0, -2, 0, -5, 0},
{-3, 0, -1, 0, -4, 0},
{-3, 0, -1, 333333334, -4, 333333334},
{-3, 0, -1, 666666667, -4, 666666667},
{-3, 0, -1, 999999999, -4, 999999999},
{-3, 0, 0, 0, -3, 0},
{-3, 0, 0, 1, -3, 1},
{-3, 0, 0, 333333333, -3, 333333333},
{-3, 0, 0, 666666666, -3, 666666666},
{-3, 0, 1, 0, -2, 0},
{-3, 0, 2, 0, -1, 0},
{-3, 0, 3, 0, 0, 0},
{-3, 0, 3, 333333333, 0, 333333333},
{-2, 0, -4, 666666667, -6, 666666667},
{-2, 0, -3, 0, -5, 0},
{-2, 0, -2, 0, -4, 0},
{-2, 0, -1, 0, -3, 0},
{-2, 0, -1, 333333334, -3, 333333334},
{-2, 0, -1, 666666667, -3, 666666667},
{-2, 0, -1, 999999999, -3, 999999999},
{-2, 0, 0, 0, -2, 0},
{-2, 0, 0, 1, -2, 1},
{-2, 0, 0, 333333333, -2, 333333333},
{-2, 0, 0, 666666666, -2, 666666666},
{-2, 0, 1, 0, -1, 0},
{-2, 0, 2, 0, 0, 0},
{-2, 0, 3, 0, 1, 0},
{-2, 0, 3, 333333333, 1, 333333333},
{-1, 0, -4, 666666667, -5, 666666667},
{-1, 0, -3, 0, -4, 0},
{-1, 0, -2, 0, -3, 0},
{-1, 0, -1, 0, -2, 0},
{-1, 0, -1, 333333334, -2, 333333334},
{-1, 0, -1, 666666667, -2, 666666667},
{-1, 0, -1, 999999999, -2, 999999999},
{-1, 0, 0, 0, -1, 0},
{-1, 0, 0, 1, -1, 1},
{-1, 0, 0, 333333333, -1, 333333333},
{-1, 0, 0, 666666666, -1, 666666666},
{-1, 0, 1, 0, 0, 0},
{-1, 0, 2, 0, 1, 0},
{-1, 0, 3, 0, 2, 0},
{-1, 0, 3, 333333333, 2, 333333333},
{-1, 666666667, -4, 666666667, -4, 333333334},
{-1, 666666667, -3, 0, -4, 666666667},
{-1, 666666667, -2, 0, -3, 666666667},
{-1, 666666667, -1, 0, -2, 666666667},
{-1, 666666667, -1, 333333334, -1, 1},
{-1, 666666667, -1, 666666667, -1, 333333334},
{-1, 666666667, -1, 999999999, -1, 666666666},
{-1, 666666667, 0, 0, -1, 666666667},
{-1, 666666667, 0, 1, -1, 666666668},
{-1, 666666667, 0, 333333333, 0, 0},
{-1, 666666667, 0, 666666666, 0, 333333333},
{-1, 666666667, 1, 0, 0, 666666667},
{-1, 666666667, 2, 0, 1, 666666667},
{-1, 666666667, 3, 0, 2, 666666667},
{-1, 666666667, 3, 333333333, 3, 0},
{0, 0, -4, 666666667, -4, 666666667},
{0, 0, -3, 0, -3, 0},
{0, 0, -2, 0, -2, 0},
{0, 0, -1, 0, -1, 0},
{0, 0, -1, 333333334, -1, 333333334},
{0, 0, -1, 666666667, -1, 666666667},
{0, 0, -1, 999999999, -1, 999999999},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 1},
{0, 0, 0, 333333333, 0, 333333333},
{0, 0, 0, 666666666, 0, 666666666},
{0, 0, 1, 0, 1, 0},
{0, 0, 2, 0, 2, 0},
{0, 0, 3, 0, 3, 0},
{0, 0, 3, 333333333, 3, 333333333},
{0, 333333333, -4, 666666667, -3, 0},
{0, 333333333, -3, 0, -3, 333333333},
{0, 333333333, -2, 0, -2, 333333333},
{0, 333333333, -1, 0, -1, 333333333},
{0, 333333333, -1, 333333334, -1, 666666667},
{0, 333333333, -1, 666666667, 0, 0},
{0, 333333333, -1, 999999999, 0, 333333332},
{0, 333333333, 0, 0, 0, 333333333},
{0, 333333333, 0, 1, 0, 333333334},
{0, 333333333, 0, 333333333, 0, 666666666},
{0, 333333333, 0, 666666666, 0, 999999999},
{0, 333333333, 1, 0, 1, 333333333},
{0, 333333333, 2, 0, 2, 333333333},
{0, 333333333, 3, 0, 3, 333333333},
{0, 333333333, 3, 333333333, 3, 666666666},
{1, 0, -4, 666666667, -3, 666666667},
{1, 0, -3, 0, -2, 0},
{1, 0, -2, 0, -1, 0},
{1, 0, -1, 0, 0, 0},
{1, 0, -1, 333333334, 0, 333333334},
{1, 0, -1, 666666667, 0, 666666667},
{1, 0, -1, 999999999, 0, 999999999},
{1, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 1, 1},
{1, 0, 0, 333333333, 1, 333333333},
{1, 0, 0, 666666666, 1, 666666666},
{1, 0, 1, 0, 2, 0},
{1, 0, 2, 0, 3, 0},
{1, 0, 3, 0, 4, 0},
{1, 0, 3, 333333333, 4, 333333333},
{2, 0, -4, 666666667, -2, 666666667},
{2, 0, -3, 0, -1, 0},
{2, 0, -2, 0, 0, 0},
{2, 0, -1, 0, 1, 0},
{2, 0, -1, 333333334, 1, 333333334},
{2, 0, -1, 666666667, 1, 666666667},
{2, 0, -1, 999999999, 1, 999999999},
{2, 0, 0, 0, 2, 0},
{2, 0, 0, 1, 2, 1},
{2, 0, 0, 333333333, 2, 333333333},
{2, 0, 0, 666666666, 2, 666666666},
{2, 0, 1, 0, 3, 0},
{2, 0, 2, 0, 4, 0},
{2, 0, 3, 0, 5, 0},
{2, 0, 3, 333333333, 5, 333333333},
{3, 0, -4, 666666667, -1, 666666667},
{3, 0, -3, 0, 0, 0},
{3, 0, -2, 0, 1, 0},
{3, 0, -1, 0, 2, 0},
{3, 0, -1, 333333334, 2, 333333334},
{3, 0, -1, 666666667, 2, 666666667},
{3, 0, -1, 999999999, 2, 999999999},
{3, 0, 0, 0, 3, 0},
{3, 0, 0, 1, 3, 1},
{3, 0, 0, 333333333, 3, 333333333},
{3, 0, 0, 666666666, 3, 666666666},
{3, 0, 1, 0, 4, 0},
{3, 0, 2, 0, 5, 0},
{3, 0, 3, 0, 6, 0},
{3, 0, 3, 333333333, 6, 333333333},
{3, 333333333, -4, 666666667, 0, 0},
{3, 333333333, -3, 0, 0, 333333333},
{3, 333333333, -2, 0, 1, 333333333},
{3, 333333333, -1, 0, 2, 333333333},
{3, 333333333, -1, 333333334, 2, 666666667},
{3, 333333333, -1, 666666667, 3, 0},
{3, 333333333, -1, 999999999, 3, 333333332},
{3, 333333333, 0, 0, 3, 333333333},
{3, 333333333, 0, 1, 3, 333333334},
{3, 333333333, 0, 333333333, 3, 666666666},
{3, 333333333, 0, 666666666, 3, 999999999},
{3, 333333333, 1, 0, 4, 333333333},
{3, 333333333, 2, 0, 5, 333333333},
{3, 333333333, 3, 0, 6, 333333333},
{3, 333333333, 3, 333333333, 6, 666666666},
{MAX_SECOND - 1, 0, 1, 0, MAX_SECOND, 0},
{MAX_SECOND - 1, 0, 0, 500, MAX_SECOND - 1, 500},
{MAX_SECOND - 1, 0, 0, 1000000000, MAX_SECOND, 0},
{MAX_SECOND, 0, -1, 0, MAX_SECOND - 1, 0},
{MAX_SECOND, 0, 0, -500, MAX_SECOND - 1, 999999500},
{MAX_SECOND, 0, 0, -1000000000, MAX_SECOND - 1, 0},
{MAX_SECOND, 0, -MAX_SECOND, 0, 0, 0},
};
}
@Test(dataProvider="Plus" )
public void plus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
Instant i = Instant.ofEpochSecond(seconds, nanos).plus(Duration.ofSeconds(otherSeconds, otherNanos));
assertEquals(i.getEpochSecond(), expectedSeconds);
assertEquals(i.getNano(), expectedNanoOfSecond);
}
@Test(expectedExceptions=DateTimeException.class )
public void plus_Duration_overflowTooBig() {
Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
i.plus(Duration.ofSeconds(0, 1));
}
@Test(expectedExceptions=DateTimeException.class )
public void plus_Duration_overflowTooSmall() {
Instant i = Instant.ofEpochSecond(MIN_SECOND);
i.plus(Duration.ofSeconds(-1, 999999999));
}
//-----------------------------------------------------------------------a
@Test(dataProvider="Plus" )
public void plus_longTemporalUnit(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
Instant i = Instant.ofEpochSecond(seconds, nanos).plus(otherSeconds, SECONDS).plus(otherNanos, NANOS);
assertEquals(i.getEpochSecond(), expectedSeconds);
assertEquals(i.getNano(), expectedNanoOfSecond);
}
@Test(expectedExceptions=DateTimeException.class )
public void plus_longTemporalUnit_overflowTooBig() {
Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
i.plus(1, NANOS);
}
@Test(expectedExceptions=DateTimeException.class )
public void plus_longTemporalUnit_overflowTooSmall() {
Instant i = Instant.ofEpochSecond(MIN_SECOND);
i.plus(999999999, NANOS);
i.plus(-1, SECONDS);
}
//-----------------------------------------------------------------------
@DataProvider(name="PlusSeconds" )
Object[][] provider_plusSeconds_long() {
return new Object[][] {
{0, 0, 0, 0, 0},
{0, 0, 1, 1, 0},
{0, 0, -1, -1, 0},
{0, 0, MAX_SECOND, MAX_SECOND, 0},
{0, 0, MIN_SECOND, MIN_SECOND, 0},
{1, 0, 0, 1, 0},
{1, 0, 1, 2, 0},
{1, 0, -1, 0, 0},
{1, 0, MAX_SECOND - 1, MAX_SECOND, 0},
{1, 0, MIN_SECOND, MIN_SECOND + 1, 0},
{1, 1, 0, 1, 1},
{1, 1, 1, 2, 1},
{1, 1, -1, 0, 1},
{1, 1, MAX_SECOND - 1, MAX_SECOND, 1},
{1, 1, MIN_SECOND, MIN_SECOND + 1, 1},
{-1, 1, 0, -1, 1},
{-1, 1, 1, 0, 1},
{-1, 1, -1, -2, 1},
{-1, 1, MAX_SECOND, MAX_SECOND - 1, 1},
{-1, 1, MIN_SECOND + 1, MIN_SECOND, 1},
{MAX_SECOND, 2, -MAX_SECOND, 0, 2},
{MIN_SECOND, 2, -MIN_SECOND, 0, 2},
};
}
@Test(dataProvider="PlusSeconds" )
public void plusSeconds_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
Instant t = Instant.ofEpochSecond(seconds, nanos);
t = t.plusSeconds(amount);
assertEquals(t.getEpochSecond(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(expectedExceptions=ArithmeticException.class )
public void plusSeconds_long_overflowTooBig() {
Instant t = Instant.ofEpochSecond(1, 0);
t.plusSeconds(Long .MAX_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class )
public void plusSeconds_long_overflowTooSmall() {
Instant t = Instant.ofEpochSecond(-1, 0);
t.plusSeconds(Long .MIN_VALUE);
}
//-----------------------------------------------------------------------
@DataProvider(name="PlusMillis" )
Object[][] provider_plusMillis_long() {
return new Object[][] {
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 1000000},
{0, 0, 999, 0, 999000000},
{0, 0, 1000, 1, 0},
{0, 0, 1001, 1, 1000000},
{0, 0, 1999, 1, 999000000},
{0, 0, 2000, 2, 0},
{0, 0, -1, -1, 999000000},
{0, 0, -999, -1, 1000000},
{0, 0, -1000, -1, 0},
{0, 0, -1001, -2, 999000000},
{0, 0, -1999, -2, 1000000},
{0, 1, 0, 0, 1},
{0, 1, 1, 0, 1000001},
{0, 1, 998, 0, 998000001},
{0, 1, 999, 0, 999000001},
{0, 1, 1000, 1, 1},
{0, 1, 1998, 1, 998000001},
{0, 1, 1999, 1, 999000001},
{0, 1, 2000, 2, 1},
{0, 1, -1, -1, 999000001},
{0, 1, -2, -1, 998000001},
{0, 1, -1000, -1, 1},
{0, 1, -1001, -2, 999000001},
{0, 1000000, 0, 0, 1000000},
{0, 1000000, 1, 0, 2000000},
{0, 1000000, 998, 0, 999000000},
{0, 1000000, 999, 1, 0},
{0, 1000000, 1000, 1, 1000000},
{0, 1000000, 1998, 1, 999000000},
{0, 1000000, 1999, 2, 0},
{0, 1000000, 2000, 2, 1000000},
{0, 1000000, -1, 0, 0},
{0, 1000000, -2, -1, 999000000},
{0, 1000000, -999, -1, 2000000},
{0, 1000000, -1000, -1, 1000000},
{0, 1000000, -1001, -1, 0},
{0, 1000000, -1002, -2, 999000000},
{0, 999999999, 0, 0, 999999999},
{0, 999999999, 1, 1, 999999},
{0, 999999999, 999, 1, 998999999},
{0, 999999999, 1000, 1, 999999999},
{0, 999999999, 1001, 2, 999999},
{0, 999999999, -1, 0, 998999999},
{0, 999999999, -1000, -1, 999999999},
{0, 999999999, -1001, -1, 998999999},
{0, 0, Long .MAX_VALUE, Long .MAX_VALUE / 1000, (int ) (Long .MAX_VALUE % 1000) * 1000000},
{0, 0, Long .MIN_VALUE, Long .MIN_VALUE / 1000 - 1, (int ) (Long .MIN_VALUE % 1000) * 1000000 + 1000000000},
};
}
@Test(dataProvider="PlusMillis" )
public void plusMillis_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
Instant t = Instant.ofEpochSecond(seconds, nanos);
t = t.plusMillis(amount);
assertEquals(t.getEpochSecond(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="PlusMillis" )
public void plusMillis_long_oneMore(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
Instant t = Instant.ofEpochSecond(seconds + 1, nanos);
t = t.plusMillis(amount);
assertEquals(t.getEpochSecond(), expectedSeconds + 1);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(dataProvider="PlusMillis" )
public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
Instant t = Instant.ofEpochSecond(seconds - 1, nanos);
t = t.plusMillis(amount);
assertEquals(t.getEpochSecond(), expectedSeconds - 1);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test
public void plusMillis_long_max() {
Instant t = Instant.ofEpochSecond(MAX_SECOND, 998999999);
t = t.plusMillis(1);
assertEquals(t.getEpochSecond(), MAX_SECOND);
assertEquals(t.getNano(), 999999999);
}
@Test(expectedExceptions=DateTimeException.class )
public void plusMillis_long_overflowTooBig() {
Instant t = Instant.ofEpochSecond(MAX_SECOND, 999000000);
t.plusMillis(1);
}
@Test
public void plusMillis_long_min() {
Instant t = Instant.ofEpochSecond(MIN_SECOND, 1000000);
t = t.plusMillis(-1);
assertEquals(t.getEpochSecond(), MIN_SECOND);
assertEquals(t.getNano(), 0);
}
@Test(expectedExceptions=DateTimeException.class )
public void plusMillis_long_overflowTooSmall() {
Instant t = Instant.ofEpochSecond(MIN_SECOND, 0);
t.plusMillis(-1);
}
//-----------------------------------------------------------------------
@DataProvider(name="PlusNanos" )
Object[][] provider_plusNanos_long() {
return new Object[][] {
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 1},
{0, 0, 999999999, 0, 999999999},
{0, 0, 1000000000, 1, 0},
{0, 0, 1000000001, 1, 1},
{0, 0, 1999999999, 1, 999999999},
{0, 0, 2000000000, 2, 0},
{0, 0, -1, -1, 999999999},
{0, 0, -999999999, -1, 1},
{0, 0, -1000000000, -1, 0},
{0, 0, -1000000001, -2, 999999999},
{0, 0, -1999999999, -2, 1},
{1, 0, 0, 1, 0},
{1, 0, 1, 1, 1},
{1, 0, 999999999, 1, 999999999},
{1, 0, 1000000000, 2, 0},
{1, 0, 1000000001, 2, 1},
{1, 0, 1999999999, 2, 999999999},
{1, 0, 2000000000, 3, 0},
{1, 0, -1, 0, 999999999},
{1, 0, -999999999, 0, 1},
{1, 0, -1000000000, 0, 0},
{1, 0, -1000000001, -1, 999999999},
{1, 0, -1999999999, -1, 1},
{-1, 0, 0, -1, 0},
{-1, 0, 1, -1, 1},
{-1, 0, 999999999, -1, 999999999},
{-1, 0, 1000000000, 0, 0},
{-1, 0, 1000000001, 0, 1},
{-1, 0, 1999999999, 0, 999999999},
{-1, 0, 2000000000, 1, 0},
{-1, 0, -1, -2, 999999999},
{-1, 0, -999999999, -2, 1},
{-1, 0, -1000000000, -2, 0},
{-1, 0, -1000000001, -3, 999999999},
{-1, 0, -1999999999, -3, 1},
{1, 1, 0, 1, 1},
{1, 1, 1, 1, 2},
{1, 1, 999999998, 1, 999999999},
{1, 1, 999999999, 2, 0},
{1, 1, 1000000000, 2, 1},
{1, 1, 1999999998, 2, 999999999},
{1, 1, 1999999999, 3, 0},
{1, 1, 2000000000, 3, 1},
{1, 1, -1, 1, 0},
{1, 1, -2, 0, 999999999},
{1, 1, -1000000000, 0, 1},
{1, 1, -1000000001, 0, 0},
{1, 1, -1000000002, -1, 999999999},
{1, 1, -2000000000, -1, 1},
{1, 999999999, 0, 1, 999999999},
{1, 999999999, 1, 2, 0},
{1, 999999999, 999999999, 2, 999999998},
{1, 999999999, 1000000000, 2, 999999999},
{1, 999999999, 1000000001, 3, 0},
{1, 999999999, -1, 1, 999999998},
{1, 999999999, -1000000000, 0, 999999999},
{1, 999999999, -1000000001, 0, 999999998},
{1, 999999999, -1999999999, 0, 0},
{1, 999999999, -2000000000, -1, 999999999},
{MAX_SECOND, 0, 999999999, MAX_SECOND, 999999999},
{MAX_SECOND - 1, 0, 1999999999, MAX_SECOND, 999999999},
{MIN_SECOND, 1, -1, MIN_SECOND, 0},
{MIN_SECOND + 1, 1, -1000000001, MIN_SECOND, 0},
{0, 0, MAX_SECOND, MAX_SECOND / 1000000000, (int ) (MAX_SECOND % 1000000000)},
{0, 0, MIN_SECOND, MIN_SECOND / 1000000000 - 1, (int ) (MIN_SECOND % 1000000000) + 1000000000},
};
}
@Test(dataProvider="PlusNanos" )
public void plusNanos_long(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
Instant t = Instant.ofEpochSecond(seconds, nanos);
t = t.plusNanos(amount);
assertEquals(t.getEpochSecond(), expectedSeconds);
assertEquals(t.getNano(), expectedNanoOfSecond);
}
@Test(expectedExceptions=DateTimeException.class )
public void plusNanos_long_overflowTooBig() {
Instant t = Instant.ofEpochSecond(MAX_SECOND, 999999999);
t.plusNanos(1);
}
@Test(expectedExceptions=DateTimeException.class )
public void plusNanos_long_overflowTooSmall() {
Instant t = Instant.ofEpochSecond(MIN_SECOND, 0);
t.plusNanos(-1);
}
//-----------------------------------------------------------------------
@DataProvider(name="Minus" )
Object[][] provider_minus() {
return new Object[][] {
{MIN_SECOND, 0, MIN_SECOND, 0, 0, 0},
{MIN_SECOND, 0, -1, 0, MIN_SECOND + 1, 0},
{MIN_SECOND, 0, 0, -500, MIN_SECOND, 500},
{MIN_SECOND, 0, 0, -1000000000, MIN_SECOND + 1, 0},
{MIN_SECOND + 1, 0, 1, 0, MIN_SECOND, 0},
{MIN_SECOND + 1, 0, 0, 500, MIN_SECOND, 999999500},
{MIN_SECOND + 1, 0, 0, 1000000000, MIN_SECOND, 0},
{-4, 666666667, -4, 666666667, 0, 0},
{-4, 666666667, -3, 0, -1, 666666667},
{-4, 666666667, -2, 0, -2, 666666667},
{-4, 666666667, -1, 0, -3, 666666667},
{-4, 666666667, -1, 333333334, -3, 333333333},
{-4, 666666667, -1, 666666667, -3, 0},
{-4, 666666667, -1, 999999999, -4, 666666668},
{-4, 666666667, 0, 0, -4, 666666667},
{-4, 666666667, 0, 1, -4, 666666666},
{-4, 666666667, 0, 333333333, -4, 333333334},
{-4, 666666667, 0, 666666666, -4, 1},
{-4, 666666667, 1, 0, -5, 666666667},
{-4, 666666667, 2, 0, -6, 666666667},
{-4, 666666667, 3, 0, -7, 666666667},
{-4, 666666667, 3, 333333333, -7, 333333334},
{-3, 0, -4, 666666667, 0, 333333333},
{-3, 0, -3, 0, 0, 0},
{-3, 0, -2, 0, -1, 0},
{-3, 0, -1, 0, -2, 0},
{-3, 0, -1, 333333334, -3, 666666666},
{-3, 0, -1, 666666667, -3, 333333333},
{-3, 0, -1, 999999999, -3, 1},
{-3, 0, 0, 0, -3, 0},
{-3, 0, 0, 1, -4, 999999999},
{-3, 0, 0, 333333333, -4, 666666667},
{-3, 0, 0, 666666666, -4, 333333334},
{-3, 0, 1, 0, -4, 0},
{-3, 0, 2, 0, -5, 0},
{-3, 0, 3, 0, -6, 0},
{-3, 0, 3, 333333333, -7, 666666667},
{-2, 0, -4, 666666667, 1, 333333333},
{-2, 0, -3, 0, 1, 0},
{-2, 0, -2, 0, 0, 0},
{-2, 0, -1, 0, -1, 0},
{-2, 0, -1, 333333334, -2, 666666666},
{-2, 0, -1, 666666667, -2, 333333333},
{-2, 0, -1, 999999999, -2, 1},
{-2, 0, 0, 0, -2, 0},
{-2, 0, 0, 1, -3, 999999999},
{-2, 0, 0, 333333333, -3, 666666667},
{-2, 0, 0, 666666666, -3, 333333334},
{-2, 0, 1, 0, -3, 0},
{-2, 0, 2, 0, -4, 0},
{-2, 0, 3, 0, -5, 0},
{-2, 0, 3, 333333333, -6, 666666667},
{-1, 0, -4, 666666667, 2, 333333333},
{-1, 0, -3, 0, 2, 0},
{-1, 0, -2, 0, 1, 0},
{-1, 0, -1, 0, 0, 0},
{-1, 0, -1, 333333334, -1, 666666666},
{-1, 0, -1, 666666667, -1, 333333333},
{-1, 0, -1, 999999999, -1, 1},
{-1, 0, 0, 0, -1, 0},
{-1, 0, 0, 1, -2, 999999999},
{-1, 0, 0, 333333333, -2, 666666667},
{-1, 0, 0, 666666666, -2, 333333334},
{-1, 0, 1, 0, -2, 0},
{-1, 0, 2, 0, -3, 0},
{-1, 0, 3, 0, -4, 0},
{-1, 0, 3, 333333333, -5, 666666667},
{-1, 666666667, -4, 666666667, 3, 0},
{-1, 666666667, -3, 0, 2, 666666667},
{-1, 666666667, -2, 0, 1, 666666667},
{-1, 666666667, -1, 0, 0, 666666667},
{-1, 666666667, -1, 333333334, 0, 333333333},
{-1, 666666667, -1, 666666667, 0, 0},
{-1, 666666667, -1, 999999999, -1, 666666668},
{-1, 666666667, 0, 0, -1, 666666667},
{-1, 666666667, 0, 1, -1, 666666666},
{-1, 666666667, 0, 333333333, -1, 333333334},
{-1, 666666667, 0, 666666666, -1, 1},
{-1, 666666667, 1, 0, -2, 666666667},
{-1, 666666667, 2, 0, -3, 666666667},
{-1, 666666667, 3, 0, -4, 666666667},
{-1, 666666667, 3, 333333333, -4, 333333334},
{0, 0, -4, 666666667, 3, 333333333},
{0, 0, -3, 0, 3, 0},
{0, 0, -2, 0, 2, 0},
{0, 0, -1, 0, 1, 0},
{0, 0, -1, 333333334, 0, 666666666},
{0, 0, -1, 666666667, 0, 333333333},
{0, 0, -1, 999999999, 0, 1},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, -1, 999999999},
{0, 0, 0, 333333333, -1, 666666667},
{0, 0, 0, 666666666, -1, 333333334},
{0, 0, 1, 0, -1, 0},
{0, 0, 2, 0, -2, 0},
{0, 0, 3, 0, -3, 0},
{0, 0, 3, 333333333, -4, 666666667},
{0, 333333333, -4, 666666667, 3, 666666666},
{0, 333333333, -3, 0, 3, 333333333},
{0, 333333333, -2, 0, 2, 333333333},
{0, 333333333, -1, 0, 1, 333333333},
{0, 333333333, -1, 333333334, 0, 999999999},
{0, 333333333, -1, 666666667, 0, 666666666},
{0, 333333333, -1, 999999999, 0, 333333334},
{0, 333333333, 0, 0, 0, 333333333},
{0, 333333333, 0, 1, 0, 333333332},
{0, 333333333, 0, 333333333, 0, 0},
{0, 333333333, 0, 666666666, -1, 666666667},
{0, 333333333, 1, 0, -1, 333333333},
{0, 333333333, 2, 0, -2, 333333333},
{0, 333333333, 3, 0, -3, 333333333},
{0, 333333333, 3, 333333333, -3, 0},
{1, 0, -4, 666666667, 4, 333333333},
{1, 0, -3, 0, 4, 0},
{1, 0, -2, 0, 3, 0},
{1, 0, -1, 0, 2, 0},
{1, 0, -1, 333333334, 1, 666666666},
{1, 0, -1, 666666667, 1, 333333333},
{1, 0, -1, 999999999, 1, 1},
{1, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 0, 999999999},
{1, 0, 0, 333333333, 0, 666666667},
{1, 0, 0, 666666666, 0, 333333334},
{1, 0, 1, 0, 0, 0},
{1, 0, 2, 0, -1, 0},
{1, 0, 3, 0, -2, 0},
{1, 0, 3, 333333333, -3, 666666667},
{2, 0, -4, 666666667, 5, 333333333},
{2, 0, -3, 0, 5, 0},
{2, 0, -2, 0, 4, 0},
{2, 0, -1, 0, 3, 0},
{2, 0, -1, 333333334, 2, 666666666},
{2, 0, -1, 666666667, 2, 333333333},
{2, 0, -1, 999999999, 2, 1},
{2, 0, 0, 0, 2, 0},
{2, 0, 0, 1, 1, 999999999},
{2, 0, 0, 333333333, 1, 666666667},
{2, 0, 0, 666666666, 1, 333333334},
{2, 0, 1, 0, 1, 0},
{2, 0, 2, 0, 0, 0},
{2, 0, 3, 0, -1, 0},
{2, 0, 3, 333333333, -2, 666666667},
{3, 0, -4, 666666667, 6, 333333333},
{3, 0, -3, 0, 6, 0},
{3, 0, -2, 0, 5, 0},
{3, 0, -1, 0, 4, 0},
{3, 0, -1, 333333334, 3, 666666666},
{3, 0, -1, 666666667, 3, 333333333},
{3, 0, -1, 999999999, 3, 1},
{3, 0, 0, 0, 3, 0},
{3, 0, 0, 1, 2, 999999999},
{3, 0, 0, 333333333, 2, 666666667},
{3, 0, 0, 666666666, 2, 333333334},
{3, 0, 1, 0, 2, 0},
{3, 0, 2, 0, 1, 0},
{3, 0, 3, 0, 0, 0},
{3, 0, 3, 333333333, -1, 666666667},
{3, 333333333, -4, 666666667, 6, 666666666},
{3, 333333333, -3, 0, 6, 333333333},
{3, 333333333, -2, 0, 5, 333333333},
{3, 333333333, -1, 0, 4, 333333333},
{3, 333333333, -1, 333333334, 3, 999999999},
{3, 333333333, -1, 666666667, 3, 666666666},
{3, 333333333, -1, 999999999, 3, 333333334},
{3, 333333333, 0, 0, 3, 333333333},
{3, 333333333, 0, 1, 3, 333333332},
{3, 333333333, 0, 333333333, 3, 0},
{3, 333333333, 0, 666666666, 2, 666666667},
{3, 333333333, 1, 0, 2, 333333333},
{3, 333333333, 2, 0, 1, 333333333},
{3, 333333333, 3, 0, 0, 333333333},
{3, 333333333, 3, 333333333, 0, 0},
{MAX_SECOND - 1, 0, -1, 0, MAX_SECOND, 0},
{MAX_SECOND - 1, 0, 0, -500, MAX_SECOND - 1, 500},
{MAX_SECOND - 1, 0, 0, -1000000000, MAX_SECOND, 0},
{MAX_SECOND, 0, 1, 0, MAX_SECOND - 1, 0},
{MAX_SECOND, 0, 0, 500, MAX_SECOND - 1, 999999500},
{MAX_SECOND, 0, 0, 1000000000, MAX_SECOND - 1, 0},
{MAX_SECOND, 0, MAX_SECOND, 0, 0, 0},
};
}
@Test(dataProvider="Minus" )
public void minus_Duration(long seconds, int nanos, long otherSeconds, int otherNanos, long expectedSeconds, int expectedNanoOfSecond) {
Instant i = Instant.ofEpochSecond(seconds, nanos).minus(Duration.ofSeconds(otherSeconds, otherNanos));
assertEquals(i.getEpochSecond(), expectedSeconds);
assertEquals(i.getNano(), expectedNanoOfSecond);
}
@Test(expectedExceptions=DateTimeException.class )
public void minus_Duration_overflowTooSmall() {
Instant i = Instant.ofEpochSecond(MIN_SECOND);
--> --------------------
--> maximum size reached
--> --------------------
Messung V0.5 C=95 H=92 G=93