1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package libcore.java.time.chrono; 17 18import org.junit.Test; 19import java.time.LocalDate; 20import java.time.chrono.ThaiBuddhistDate; 21import java.time.chrono.ThaiBuddhistChronology; 22import java.time.chrono.ThaiBuddhistEra; 23import java.time.temporal.ChronoField; 24import java.time.temporal.ValueRange; 25 26import static org.junit.Assert.assertEquals; 27import static org.junit.Assert.assertSame; 28 29/** 30 * Additional tests for {@link ThaiBuddhistDate}. 31 * 32 * @see tck.java.time.chrono.TCKThaiBuddhistChronology 33 */ 34public class ThaiBuddhistChronologyTest { 35 36 // year 2543 in Thai Buddhist calendar is 2000 in ISO calendar. 37 private static final int YEARS_AHEAD = 543; 38 39 @Test 40 public void test_range() { 41 for (ChronoField field : ChronoField.values()) { 42 ValueRange expected; 43 switch (field) { 44 case PROLEPTIC_MONTH: 45 // Proleptic month values are shifted by YEARS_AHEAD * 12. 46 expected = ValueRange.of( 47 ChronoField.PROLEPTIC_MONTH.range().getMinimum() + YEARS_AHEAD * 12L, 48 ChronoField.PROLEPTIC_MONTH.range().getMaximum() + YEARS_AHEAD * 12L); 49 break; 50 case YEAR_OF_ERA: 51 // range for era BE is 1..<yearRange.max - OFFSET> 52 // range for era before BE is 1..<-yearRange.min + 1 + OFFSET> 53 expected = ValueRange 54 .of(1, -ChronoField.YEAR.range().getMinimum() + 1 - YEARS_AHEAD, 55 ChronoField.YEAR.range().getMaximum() + YEARS_AHEAD); 56 break; 57 case YEAR: 58 // Proleptic year values are shifted by YEAR. 59 expected = ValueRange.of(ChronoField.YEAR.range().getMinimum() + YEARS_AHEAD, 60 ChronoField.YEAR.range().getMaximum() + YEARS_AHEAD); 61 break; 62 default: 63 // All other fields have the same ranges as ISO. 64 expected = field.range(); 65 break; 66 } 67 assertEquals("Range of " + field, expected, 68 ThaiBuddhistChronology.INSTANCE.range(field)); 69 } 70 } 71 72 @Test 73 public void test_ThaiBuddhistDate_getChronology() { 74 assertSame(ThaiBuddhistChronology.INSTANCE, ThaiBuddhistDate.now().getChronology()); 75 } 76 77 @Test 78 public void test_ThaiBuddhistDate_getEra() { 79 assertEquals(ThaiBuddhistEra.BEFORE_BE, ThaiBuddhistDate.of(-1, 1, 1).getEra()); 80 assertEquals(ThaiBuddhistEra.BE, ThaiBuddhistDate.of(1, 1, 1).getEra()); 81 } 82 83 @Test 84 public void test_ThaiBuddhistDate_range() { 85 ThaiBuddhistDate dates[] = new ThaiBuddhistDate[] { 86 ThaiBuddhistDate.from(LocalDate.of(2000, 2, 1)), //February of a leap year 87 ThaiBuddhistDate.from(LocalDate.of(2001, 2, 1)), //February of a non-leap year 88 ThaiBuddhistDate.of(1, 2, 3), 89 ThaiBuddhistDate.of(4, 5, 6), 90 ThaiBuddhistDate.of(-7, 8, 9) 91 }; 92 93 for (ThaiBuddhistDate date : dates) { 94 // only these three ChronoFields and YEAR_OF_ERA (below) have date-dependent ranges. 95 assertEquals(LocalDate.from(date).range(ChronoField.DAY_OF_MONTH), 96 date.range(ChronoField.DAY_OF_MONTH)); 97 assertEquals(LocalDate.from(date).range(ChronoField.DAY_OF_YEAR), 98 date.range(ChronoField.DAY_OF_YEAR)); 99 assertEquals(LocalDate.from(date).range(ChronoField.ALIGNED_WEEK_OF_MONTH), 100 date.range(ChronoField.ALIGNED_WEEK_OF_MONTH)); 101 } 102 } 103 104 @Test 105 public void test_ThaiBuddhistDate_range_yeaOfEra() { 106 // YEAR_OF_ERA is the big difference to a LocalDate, all other ranges are the same. 107 assertEquals(ValueRange.of(1, ChronoField.YEAR.range().getMaximum() + YEARS_AHEAD), 108 ThaiBuddhistDate.of(1, 1, 1).range(ChronoField.YEAR_OF_ERA)); 109 assertEquals(ValueRange.of(1, -ChronoField.YEAR.range().getMinimum() + 1 - YEARS_AHEAD), 110 ThaiBuddhistDate.of(-1, 1, 1).range(ChronoField.YEAR_OF_ERA)); 111 } 112 113 @Test 114 public void test_ThaiBuddhistDate_getLong() { 115 ThaiBuddhistDate date = ThaiBuddhistDate.of(10, 2, 5); 116 assertEquals(10, date.getLong(ChronoField.YEAR_OF_ERA)); 117 assertEquals(10, date.getLong(ChronoField.YEAR)); 118 assertEquals(2, date.getLong(ChronoField.MONTH_OF_YEAR)); 119 assertEquals(10 * 12 + 2 - 1, date.getLong(ChronoField.PROLEPTIC_MONTH)); 120 assertEquals(5, date.getLong(ChronoField.DAY_OF_MONTH)); 121 assertEquals(31 + 5, date.getLong(ChronoField.DAY_OF_YEAR)); 122 assertEquals(date.toEpochDay(), date.getLong(ChronoField.EPOCH_DAY)); 123 } 124} 125