test/java/time/tck/java/time/TCKPeriod.java

Print this page

        

*** 57,74 **** --- 57,81 ---- * 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.ChronoUnit.DAYS; + import static java.time.temporal.ChronoUnit.YEARS; import static org.testng.Assert.assertEquals; import java.time.DateTimeException; + import java.time.Duration; import java.time.LocalDate; import java.time.Period; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoUnit; + import java.time.temporal.Temporal; + import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalUnit; + import java.util.ArrayList; + import java.util.Collections; import java.util.List; import java.util.Locale; import org.testng.annotations.DataProvider; import org.testng.annotations.Test;
*** 137,146 **** --- 144,225 ---- assertPeriod(Period.of(0, 0, 0), 0, 0, 0); assertPeriod(Period.of(-1, -2, -3), -1, -2, -3); } //----------------------------------------------------------------------- + // from(TemporalAmount) + //----------------------------------------------------------------------- + @Test + public void factory_from_TemporalAmount_Period() { + TemporalAmount amount = Period.of(1, 2, 3); + assertPeriod(Period.from(amount), 1, 2, 3); + } + + @Test + public void factory_from_TemporalAmount_YearsDays() { + TemporalAmount amount = new TemporalAmount() { + @Override + public long get(TemporalUnit unit) { + if (unit == YEARS) { + return 23; + } else { + return 45; + } + } + @Override + public List<TemporalUnit> getUnits() { + List<TemporalUnit> list = new ArrayList<>(); + list.add(YEARS); + list.add(DAYS); + return list; + } + @Override + public Temporal addTo(Temporal temporal) { + throw new UnsupportedOperationException(); + } + @Override + public Temporal subtractFrom(Temporal temporal) { + throw new UnsupportedOperationException(); + } + }; + assertPeriod(Period.from(amount), 23, 0, 45); + } + + @Test(expectedExceptions = ArithmeticException.class) + public void factory_from_TemporalAmount_Years_tooBig() { + TemporalAmount amount = new TemporalAmount() { + @Override + public long get(TemporalUnit unit) { + return ((long) (Integer.MAX_VALUE)) + 1; + } + @Override + public List<TemporalUnit> getUnits() { + return Collections.<TemporalUnit>singletonList(YEARS); + } + @Override + public Temporal addTo(Temporal temporal) { + throw new UnsupportedOperationException(); + } + @Override + public Temporal subtractFrom(Temporal temporal) { + throw new UnsupportedOperationException(); + } + }; + Period.from(amount); + } + + @Test(expectedExceptions = DateTimeException.class) + public void factory_from_TemporalAmount_Duration() { + Period.from(Duration.ZERO); + } + + @Test(expectedExceptions = NullPointerException.class) + public void factory_from_TemporalAmount_null() { + Period.from(null); + } + + //----------------------------------------------------------------------- // parse(String) //----------------------------------------------------------------------- @DataProvider(name="parseSuccess") Object[][] data_factory_parseSuccess() { return new Object[][] {
*** 464,481 **** --- 543,591 ---- assertPeriod(Period.of(-1, -2, -3).withDays(10), -1, -2, 10); assertPeriod(Period.of(1, 2, 3).withDays(0), 1, 2, 0); } //----------------------------------------------------------------------- + // plus(Period) + //----------------------------------------------------------------------- + @DataProvider(name="plus") + Object[][] data_plus() { + return new Object[][] { + {pymd(0, 0, 0), pymd(0, 0, 0), pymd(0, 0, 0)}, + {pymd(0, 0, 0), pymd(5, 0, 0), pymd(5, 0, 0)}, + {pymd(0, 0, 0), pymd(-5, 0, 0), pymd(-5, 0, 0)}, + {pymd(0, 0, 0), pymd(0, 5, 0), pymd(0, 5, 0)}, + {pymd(0, 0, 0), pymd(0, -5, 0), pymd(0, -5, 0)}, + {pymd(0, 0, 0), pymd(0, 0, 5), pymd(0, 0, 5)}, + {pymd(0, 0, 0), pymd(0, 0, -5), pymd(0, 0, -5)}, + {pymd(0, 0, 0), pymd(2, 3, 4), pymd(2, 3, 4)}, + {pymd(0, 0, 0), pymd(-2, -3, -4), pymd(-2, -3, -4)}, + + {pymd(4, 5, 6), pymd(2, 3, 4), pymd(6, 8, 10)}, + {pymd(4, 5, 6), pymd(-2, -3, -4), pymd(2, 2, 2)}, + }; + } + + @Test(dataProvider="plus") + public void test_plus(Period base, Period add, Period expected) { + assertEquals(base.plus(add), expected); + } + + //----------------------------------------------------------------------- // plusYears() //----------------------------------------------------------------------- @Test public void test_plusYears() { assertPeriod(Period.of(1, 2, 3).plusYears(0), 1, 2, 3); assertPeriod(Period.of(1, 2, 3).plusYears(10), 11, 2, 3); assertPeriod(Period.of(1, 2, 3).plusYears(-10), -9, 2, 3); assertPeriod(Period.of(1, 2, 3).plusYears(-1), 0, 2, 3); + + assertPeriod(Period.of(1, 2, 3).plus(Period.ofYears(0)), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofYears(10)), 11, 2, 3); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofYears(-10)), -9, 2, 3); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofYears(-1)), 0, 2, 3); } @Test(expectedExceptions=ArithmeticException.class) public void test_plusYears_overflowTooBig() { Period test = Period.ofYears(Integer.MAX_VALUE);
*** 495,504 **** --- 605,619 ---- public void test_plusMonths() { assertPeriod(Period.of(1, 2, 3).plusMonths(0), 1, 2, 3); assertPeriod(Period.of(1, 2, 3).plusMonths(10), 1, 12, 3); assertPeriod(Period.of(1, 2, 3).plusMonths(-10), 1, -8, 3); assertPeriod(Period.of(1, 2, 3).plusMonths(-2), 1, 0, 3); + + assertPeriod(Period.of(1, 2, 3).plus(Period.ofMonths(0)), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofMonths(10)), 1, 12, 3); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofMonths(-10)), 1, -8, 3); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofMonths(-2)), 1, 0, 3); } @Test(expectedExceptions=ArithmeticException.class) public void test_plusMonths_overflowTooBig() { Period test = Period.ofMonths(Integer.MAX_VALUE);
*** 518,527 **** --- 633,647 ---- public void test_plusDays() { assertPeriod(Period.of(1, 2, 3).plusDays(0), 1, 2, 3); assertPeriod(Period.of(1, 2, 3).plusDays(10), 1, 2, 13); assertPeriod(Period.of(1, 2, 3).plusDays(-10), 1, 2, -7); assertPeriod(Period.of(1, 2, 3).plusDays(-3), 1, 2, 0); + + assertPeriod(Period.of(1, 2, 3).plus(Period.ofDays(0)), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofDays(10)), 1, 2, 13); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofDays(-10)), 1, 2, -7); + assertPeriod(Period.of(1, 2, 3).plus(Period.ofDays(-3)), 1, 2, 0); } @Test(expectedExceptions=ArithmeticException.class) public void test_plusDays_overflowTooBig() { Period test = Period.ofDays(Integer.MAX_VALUE);
*** 533,542 **** --- 653,772 ---- Period test = Period.ofDays(Integer.MIN_VALUE); test.plusDays(-1); } //----------------------------------------------------------------------- + // minus(Period) + //----------------------------------------------------------------------- + @DataProvider(name="minus") + Object[][] data_minus() { + return new Object[][] { + {pymd(0, 0, 0), pymd(0, 0, 0), pymd(0, 0, 0)}, + {pymd(0, 0, 0), pymd(5, 0, 0), pymd(-5, 0, 0)}, + {pymd(0, 0, 0), pymd(-5, 0, 0), pymd(5, 0, 0)}, + {pymd(0, 0, 0), pymd(0, 5, 0), pymd(0, -5, 0)}, + {pymd(0, 0, 0), pymd(0, -5, 0), pymd(0, 5, 0)}, + {pymd(0, 0, 0), pymd(0, 0, 5), pymd(0, 0, -5)}, + {pymd(0, 0, 0), pymd(0, 0, -5), pymd(0, 0, 5)}, + {pymd(0, 0, 0), pymd(2, 3, 4), pymd(-2, -3, -4)}, + {pymd(0, 0, 0), pymd(-2, -3, -4), pymd(2, 3, 4)}, + + {pymd(4, 5, 6), pymd(2, 3, 4), pymd(2, 2, 2)}, + {pymd(4, 5, 6), pymd(-2, -3, -4), pymd(6, 8, 10)}, + }; + } + + @Test(dataProvider="minus") + public void test_minus(Period base, Period subtract, Period expected) { + assertEquals(base.minus(subtract), expected); + } + + //----------------------------------------------------------------------- + // minusYears() + //----------------------------------------------------------------------- + @Test + public void test_minusYears() { + assertPeriod(Period.of(1, 2, 3).minusYears(0), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).minusYears(10), -9, 2, 3); + assertPeriod(Period.of(1, 2, 3).minusYears(-10), 11, 2, 3); + assertPeriod(Period.of(1, 2, 3).minusYears(-1), 2, 2, 3); + + assertPeriod(Period.of(1, 2, 3).minus(Period.ofYears(0)), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofYears(10)), -9, 2, 3); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofYears(-10)), 11, 2, 3); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofYears(-1)), 2, 2, 3); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_minusYears_overflowTooBig() { + Period test = Period.ofYears(Integer.MAX_VALUE); + test.minusYears(-1); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_minusYears_overflowTooSmall() { + Period test = Period.ofYears(Integer.MIN_VALUE); + test.minusYears(1); + } + + //----------------------------------------------------------------------- + // minusMonths() + //----------------------------------------------------------------------- + @Test + public void test_minusMonths() { + assertPeriod(Period.of(1, 2, 3).minusMonths(0), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).minusMonths(10), 1, -8, 3); + assertPeriod(Period.of(1, 2, 3).minusMonths(-10), 1, 12, 3); + assertPeriod(Period.of(1, 2, 3).minusMonths(-2), 1, 4, 3); + + assertPeriod(Period.of(1, 2, 3).minus(Period.ofMonths(0)), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofMonths(10)), 1, -8, 3); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofMonths(-10)), 1, 12, 3); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofMonths(-2)), 1, 4, 3); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_minusMonths_overflowTooBig() { + Period test = Period.ofMonths(Integer.MAX_VALUE); + test.minusMonths(-1); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_minusMonths_overflowTooSmall() { + Period test = Period.ofMonths(Integer.MIN_VALUE); + test.minusMonths(1); + } + + //----------------------------------------------------------------------- + // minusDays() + //----------------------------------------------------------------------- + @Test + public void test_minusDays() { + assertPeriod(Period.of(1, 2, 3).minusDays(0), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).minusDays(10), 1, 2, -7); + assertPeriod(Period.of(1, 2, 3).minusDays(-10), 1, 2, 13); + assertPeriod(Period.of(1, 2, 3).minusDays(-3), 1, 2, 6); + + assertPeriod(Period.of(1, 2, 3).minus(Period.ofDays(0)), 1, 2, 3); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofDays(10)), 1, 2, -7); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofDays(-10)), 1, 2, 13); + assertPeriod(Period.of(1, 2, 3).minus(Period.ofDays(-3)), 1, 2, 6); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_minusDays_overflowTooBig() { + Period test = Period.ofDays(Integer.MAX_VALUE); + test.minusDays(-1); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_minusDays_overflowTooSmall() { + Period test = Period.ofDays(Integer.MIN_VALUE); + test.minusDays(1); + } + + //----------------------------------------------------------------------- // multipliedBy() //----------------------------------------------------------------------- @Test public void test_multipliedBy() { Period test = Period.of(1, 2, 3);