Home | History | Annotate | Download | only in calendar
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2005-2010, International Business Machines Corporation and    *
      7  * others. All Rights Reserved.                                                *
      8  *******************************************************************************
      9  */
     10 package android.icu.dev.test.calendar;
     11 
     12 import java.util.Date;
     13 import java.util.Locale;
     14 
     15 import org.junit.Test;
     16 import org.junit.runner.RunWith;
     17 import org.junit.runners.JUnit4;
     18 
     19 import android.icu.impl.LocaleUtility;
     20 import android.icu.text.DateFormat;
     21 import android.icu.text.SimpleDateFormat;
     22 import android.icu.util.Calendar;
     23 import android.icu.util.CopticCalendar;
     24 import android.icu.util.EthiopicCalendar;
     25 import android.icu.util.GregorianCalendar;
     26 import android.icu.util.TimeZone;
     27 import android.icu.util.ULocale;
     28 import android.icu.testsharding.MainTestShard;
     29 
     30 /**
     31  * Tests for the <code>CopticCalendar</code> class.
     32  */
     33 @MainTestShard
     34 @RunWith(JUnit4.class)
     35 public class CopticTest extends CalendarTestFmwk
     36 {
     37     /** Constants to save typing. */
     38     public static final int TOUT      = CopticCalendar.TOUT;
     39     public static final int BABA      = CopticCalendar.BABA;
     40     public static final int HATOR     = CopticCalendar.HATOR;
     41     public static final int KIAHK     = CopticCalendar.KIAHK;
     42     public static final int TOBA      = CopticCalendar.TOBA;
     43     public static final int AMSHIR    = CopticCalendar.AMSHIR;
     44     public static final int BARAMHAT  = CopticCalendar.BARAMHAT;
     45     public static final int BARAMOUDA = CopticCalendar.BARAMOUDA;
     46     public static final int BASHANS   = CopticCalendar.BASHANS;
     47     public static final int PAONA     = CopticCalendar.PAONA;
     48     public static final int EPEP      = CopticCalendar.EPEP;
     49     public static final int MESRA     = CopticCalendar.MESRA;
     50     public static final int NASIE     = CopticCalendar.NASIE;
     51 
     52     /* Test dates from:
     53      * "The Amharic Letters of Emperor Theodore of Ethiopia to Queen Victoria and
     54      * Her Special Envoy", David Appleyard, Girma Selasse Asfaw, Oxford University Press,
     55      * June 1 1979, ISBN: 0856726605, Longwood Pr Ltd
     56      *
     57      * Coptic         Gregorian    JD
     58      * 20/02/1579     29/10/1862  2401443
     59      * 29/10/1581     05/07/1865  2402423
     60      * 22/05/1582     29/01/1866  2402631
     61      * 10/08/1582     17/04/1866  2402709
     62      * 28/04/1583     05/01/1867  2402972
     63      * 05/05/1584     13/01/1868  2403345
     64      *
     65      * --------------------------------------------------
     66      *
     67      * From the Calendrica applet:  http://emr.cs.iit.edu/home/reingold/calendar-book/Calendrica.html
     68      *
     69      * Coptic         Gregorian    JD
     70      * 07/05/-284     01/01/0000  1721060
     71      * 08/05/-283     01/01/0001  1721426
     72      * 06/13/-1       29/08/0283  1824664
     73      *
     74      * 01/01/0000     30/08/0283  1824665
     75      * 01/01/0001     29/08/0284  1825030
     76      * 01/01/0002     29/08/0285  1825395
     77      * 01/01/0003     29/08/0286  1825760
     78      * 01/01/0004     30/08/0287  1826126
     79      * 05/13/0000     28/08/0284  1825029
     80      * 05/13/0001     28/08/0285  1825394
     81      * 05/13/0002     28/08/0286  1825759
     82      * 05/13/0003     28/08/0287  1826124
     83      * 06/13/0003     29/08/0287  1826125  first coptic leap year
     84      * 05/13/0004     28/08/0288  1826490
     85      *
     86      * 06/02/1299     13/10/1582  2299159
     87      * 07/02/1299     14/10/1582  2299160  Julian 04/10/1582
     88      * 08/02/1299     15/10/1582  2299161
     89      * 09/02/1299     16/10/1582  2299162
     90      *
     91      * 23/04/1616     01/01/1900  2415021
     92      * 23/04/1721     01/01/2005  2453372
     93      * 05/13/2000     12/09/2284  2555529
     94      */
     95 
     96 
     97     /** A huge list of test cases to make sure that computeTime and computeFields
     98      * work properly for a wide range of data in the civil calendar.
     99      */
    100     @Test
    101     public void TestCases()
    102     {
    103         final TestCase[] tests = {
    104             //
    105             // The months in this table are 1-based rather than 0-based,
    106             // because it's easier to edit that way.
    107             //                      Coptic
    108             //          Julian Day  Era  Year  Month Day  WkDay Hour Min Sec
    109             //
    110             // Dates from "Emporer Theodore..."
    111 
    112             new TestCase(2401442.5,  1,  1579,    2,  20,  WED,    0,  0,  0), // Gregorian: 20/10/1862
    113             new TestCase(2402422.5,  1,  1581,   10,  29,  WED,    0,  0,  0), // Gregorian: 05/07/1865
    114             new TestCase(2402630.5,  1,  1582,    5,  22,  MON,    0,  0,  0), // Gregorian: 29/01/1866
    115             new TestCase(2402708.5,  1,  1582,    8,  10,  TUE,    0,  0,  0), // Gregorian: 17/04/1866
    116             new TestCase(2402971.5,  1,  1583,    4,  28,  SAT,    0,  0,  0), // Gregorian: 05/01/1867
    117             new TestCase(2403344.5,  1,  1584,    5,   5,  MON,    0,  0,  0), // Gregorian: 13/01/1868
    118             new TestCase(1721059.5,  0,   285,    5,   7,  SAT,    0,  0,  0), // Gregorian: 01/01/0000
    119             new TestCase(1721425.5,  0,   284,    5,   8,  MON,    0,  0,  0), // Gregorian: 01/01/0001
    120             new TestCase(1824663.5,  0,     2,   13,   6,  WED,    0,  0,  0), // Gregorian: 29/08/0283
    121             new TestCase(1824664.5,  0,     1,    1,   1,  THU,    0,  0,  0), // Gregorian: 30/08/0283
    122             new TestCase(1825029.5,  1,     1,    1,   1,  FRI,    0,  0,  0), // Gregorian: 29/08/0284
    123             new TestCase(1825394.5,  1,     2,    1,   1,  SAT,    0,  0,  0), // Gregorian: 29/08/0285
    124             new TestCase(1825759.5,  1,     3,    1,   1,  SUN,    0,  0,  0), // Gregorian: 29/08/0286
    125             new TestCase(1826125.5,  1,     4,    1,   1,  TUE,    0,  0,  0), // Gregorian: 30/08/0287
    126             new TestCase(1825028.5,  0,     1,   13,   5,  THU,    0,  0,  0), // Gregorian: 28/08/0284
    127             new TestCase(1825393.5,  1,     1,   13,   5,  FRI,    0,  0,  0), // Gregorian: 28/08/0285
    128             new TestCase(1825758.5,  1,     2,   13,   5,  SAT,    0,  0,  0), // Gregorian: 28/08/0286
    129             new TestCase(1826123.5,  1,     3,   13,   5,  SUN,    0,  0,  0), // Gregorian: 28/08/0287
    130             new TestCase(1826124.5,  1,     3,   13,   6,  MON,    0,  0,  0), // Gregorian: 29/08/0287
    131                           // above is first coptic leap year
    132             new TestCase(1826489.5,  1,     4,   13,   5,  TUE,    0,  0,  0), // Gregorian: 28/08/0288
    133             new TestCase(2299158.5,  1,  1299,    2,   6,  WED,    0,  0,  0), // Gregorian: 13/10/1582
    134             new TestCase(2299159.5,  1,  1299,    2,   7,  THU,    0,  0,  0), // Gregorian: 14/10/1582
    135 
    136             new TestCase(2299160.5,  1,  1299,    2,   8,  FRI,    0,  0,  0), // Gregorian: 15/10/1582
    137             new TestCase(2299161.5,  1,  1299,    2,   9,  SAT,    0,  0,  0), // Gregorian: 16/10/1582
    138 
    139             new TestCase(2415020.5,  1,  1616,    4,  23,  MON,    0,  0,  0), // Gregorian: 01/01/1900
    140             new TestCase(2453371.5,  1,  1721,    4,  23,  SAT,    0,  0,  0), // Gregorian: 01/01/2005
    141             new TestCase(2555528.5,  1,  2000,   13,   5,  FRI,    0,  0,  0), // Gregorian: 12/09/2284
    142         };
    143 
    144         CopticCalendar testCalendar = new CopticCalendar();
    145         testCalendar.setLenient(true);
    146         doTestCases(tests, testCalendar);
    147     }
    148 
    149     // basic sanity check that the conversion algorithm round-trips
    150     @Test
    151     public void TestCopticToJD() {
    152         CopticCalendar cal = new CopticCalendar();
    153         cal.clear();
    154         for (int y = -2; y < 3; ++y) {
    155             for (int m = 0; m < 12; ++m) { // don't understand rules for 13th month
    156                 for (int d = 1; d < 25; d += 3) { // play it safe on days per month
    157                     int jd = CopticCalendar.copticToJD(y, m, d);
    158                     cal.set(Calendar.JULIAN_DAY, jd);
    159                     int eyear = cal.get(Calendar.EXTENDED_YEAR);
    160                     int month = cal.get(Calendar.MONTH);
    161                     int day = cal.get(Calendar.DAY_OF_MONTH);
    162                     if (!(y == eyear &&
    163                           m == month &&
    164                           d == day)) {
    165                         errln("y: " + y +
    166                               " m: " + m +
    167                               " d: " + d +
    168                               " --> jd: " + jd +
    169                               " --> y: " + eyear +
    170                               " m: " + month +
    171                               " d: " + day);
    172                     }
    173                 }
    174             }
    175         }
    176     }
    177 
    178     // basic check to see that we print out eras ok
    179     // eventually should modify to use locale strings and formatter appropriate to coptic calendar
    180     @Test
    181     public void TestEraStart() {
    182         SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd, yyyy GG");
    183         SimpleDateFormat copticFmt = new SimpleDateFormat("EEE MMM dd, yyyy GG");
    184         copticFmt.setCalendar(new CopticCalendar());
    185 
    186         CopticCalendar cal = new CopticCalendar(1, 0, 1);
    187         assertEquals("Coptic Date", "Fri Jan 01, 0001 AD", copticFmt.format(cal));
    188         assertEquals("Gregorian Date", "Fri Aug 29, 0284 AD", fmt.format(cal.getTime()));
    189 
    190         cal.set(Calendar.ERA, 0);
    191         cal.set(Calendar.YEAR, 1);
    192         assertEquals("Coptic Date", "Thu Jan 01, 0001 BC", copticFmt.format(cal));
    193         assertEquals("Gregorian Date", "Thu Aug 30, 0283 AD", fmt.format(cal.getTime()));
    194     }
    195 
    196     @Test
    197     public void TestBasic() {
    198         CopticCalendar cal = new CopticCalendar();
    199         cal.clear();
    200         cal.set(1000, 0, 30);
    201         logln("1000/0/30-> " +
    202               cal.get(YEAR) + "/" +
    203               cal.get(MONTH) + "/" +
    204               cal.get(DATE));
    205         cal.clear();
    206         cal.set(1, 0, 30);
    207         logln("1/0/30 -> " +
    208               cal.get(YEAR) + "/" +
    209               cal.get(MONTH) + "/" +
    210               cal.get(DATE));
    211     }
    212 
    213     /**
    214      * Test limits of the Coptic calendar
    215      */
    216     @Test
    217     public void TestLimits() {
    218         Calendar cal = Calendar.getInstance();
    219         cal.set(2007, Calendar.JANUARY, 1);
    220         CopticCalendar coptic = new CopticCalendar();
    221         doLimitsTest(coptic, null, cal.getTime());
    222         doTheoreticalLimitsTest(coptic, true);
    223     }
    224 
    225     /**
    226      * Test for track ticket 6379 - proper reporting of
    227      * maximum month lengths
    228      */
    229     @Test
    230     public void Test6379()
    231     {
    232         CopticCalendar copticCal = new CopticCalendar();
    233         copticCal.clear();
    234 
    235         for (int year = 1725; year < 1735; year++) {    // Coptic 1725-01-01 = Gregorian 2008-09-11
    236             boolean isLeap = ((year % 4) == 3);
    237             copticCal.set(Calendar.YEAR, year);
    238 
    239             int maxMonth = copticCal.getActualMaximum(Calendar.MONTH);
    240 
    241             for (int month = 0; month <= maxMonth; month++) {
    242                 copticCal.set(Calendar.MONTH, month);
    243                 int maxDayOfMonth = copticCal.getActualMaximum(Calendar.DAY_OF_MONTH);
    244 
    245                 int expected = (month != maxMonth) ? 30 : (isLeap ? 6 : 5);
    246                 if (maxDayOfMonth != expected) {
    247                     errln("FAIL: Expected maximum " + expected + " days for month #"
    248                             + (month + 1) + " - returned:" + maxDayOfMonth);
    249                 }
    250             }
    251         }
    252     }
    253 
    254     @Test
    255     public void TestCoverage() {
    256 
    257         {
    258             // new CopticCalendar(TimeZone)
    259             CopticCalendar cal = new CopticCalendar(TimeZone.getDefault());
    260             if(cal == null){
    261                 errln("could not create CopticCalendar with TimeZone");
    262             }
    263         }
    264 
    265         {
    266             // new CopticCalendar(ULocale)
    267             CopticCalendar cal = new CopticCalendar(ULocale.getDefault());
    268             if(cal == null){
    269                 errln("could not create CopticCalendar with ULocale");
    270             }
    271         }
    272 
    273         {
    274             // new CopticCalendar(Locale)
    275             CopticCalendar cal = new CopticCalendar(Locale.getDefault());
    276             if(cal == null){
    277                 errln("could not create CopticCalendar with Locale");
    278             }
    279         }
    280 
    281         {
    282             // new CopticCalendar(TimeZone, Locale)
    283             CopticCalendar cal = new CopticCalendar(TimeZone.getDefault(),Locale.getDefault());
    284             if(cal == null){
    285                 errln("could not create CopticCalendar with TimeZone, Locale");
    286             }
    287         }
    288 
    289         {
    290             // new CopticCalendar(TimeZone, ULocale)
    291             CopticCalendar cal = new CopticCalendar(TimeZone.getDefault(),ULocale.getDefault());
    292             if(cal == null){
    293                 errln("could not create CopticCalendar with TimeZone, ULocale");
    294             }
    295         }
    296 
    297         {
    298             // new CopticCalendar(Date)
    299             CopticCalendar cal = new CopticCalendar(new Date());
    300             if(cal == null){
    301                 errln("could not create CopticCalendar with Date");
    302             }
    303         }
    304 
    305         {
    306             // new CopticCalendar(int year, int month, int date)
    307             CopticCalendar cal = new CopticCalendar(1997, CopticCalendar.TOUT, 1);
    308             if(cal == null){
    309                 errln("could not create CopticCalendar with year,month,date");
    310             }
    311         }
    312 
    313         {
    314             // new CopticCalendar(int year, int month, int date, int hour, int minute, int second)
    315             CopticCalendar cal = new CopticCalendar(1997, CopticCalendar.TOUT, 1, 1, 1, 1);
    316             if(cal == null){
    317                 errln("could not create CopticCalendar with year,month,date,hour,minute,second");
    318             }
    319         }
    320 
    321         {
    322             // data
    323             CopticCalendar cal = new CopticCalendar(1997, CopticCalendar.TOUT, 1);
    324             Date time = cal.getTime();
    325 
    326             String[] calendarLocales = {
    327                 "am_ET", "gez_ET", "ti_ET"
    328             };
    329 
    330             String[] formatLocales = {
    331                 "en", "am", "am_ET", "gez", "ti"
    332             };
    333             for (int i = 0; i < calendarLocales.length; ++i) {
    334                 String calLocName = calendarLocales[i];
    335                 Locale calLocale = LocaleUtility.getLocaleFromName(calLocName);
    336                 cal = new CopticCalendar(calLocale);
    337 
    338                 for (int j = 0; j < formatLocales.length; ++j) {
    339                     String locName = formatLocales[j];
    340                     Locale formatLocale = LocaleUtility.getLocaleFromName(locName);
    341                     DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale);
    342                     logln(calLocName + "/" + locName + " --> " + format.format(time));
    343                 }
    344             }
    345         }
    346     }
    347 
    348     @Test
    349     public void TestYear() {
    350         // Gregorian Calendar
    351         Calendar gCal= new GregorianCalendar();
    352         Date gToday=gCal.getTime();
    353         gCal.add(GregorianCalendar.MONTH,2);
    354         Date gFuture=gCal.getTime();
    355         DateFormat gDF = DateFormat.getDateInstance(gCal,DateFormat.FULL);
    356         logln("gregorian calendar: " + gDF.format(gToday) +
    357               " + 2 months = " + gDF.format(gFuture));
    358 
    359         // Coptic Calendar
    360         CopticCalendar cCal= new CopticCalendar();
    361         Date cToday=cCal.getTime();
    362         cCal.add(CopticCalendar.MONTH,2);
    363         Date cFuture=cCal.getTime();
    364         DateFormat cDF = DateFormat.getDateInstance(cCal,DateFormat.FULL);
    365         logln("coptic calendar: " + cDF.format(cToday) +
    366               " + 2 months = " + cDF.format(cFuture));
    367 
    368         // EthiopicCalendar
    369         EthiopicCalendar eCal= new EthiopicCalendar();
    370         Date eToday=eCal.getTime();
    371         eCal.add(EthiopicCalendar.MONTH,2); // add 2 months
    372         eCal.setAmeteAlemEra(false);
    373         Date eFuture=eCal.getTime();
    374         DateFormat eDF = DateFormat.getDateInstance(eCal,DateFormat.FULL);
    375         logln("ethiopic calendar: " + eDF.format(eToday) +
    376               " + 2 months = " + eDF.format(eFuture));
    377     }
    378 
    379     @Test
    380     public void TestAddSet() {
    381         class TestAddSetItem {
    382             private int startYear;
    383             private int startMonth; // 0-based
    384             private int startDay; // 1-based
    385             private int fieldToChange;
    386             private int fieldDelta;
    387             private int endYear;
    388             private int endMonth;
    389             private int endDay;
    390             TestAddSetItem(int sYr, int sMo, int sDa, int field, int delta, int eYr, int eMo, int eDa) {
    391                 startYear = sYr;
    392                 startMonth = sMo;
    393                 startDay = sDa;
    394                 fieldToChange = field;
    395                 fieldDelta = delta;
    396                 endYear = eYr;
    397                 endMonth = eMo;
    398                 endDay = eDa;
    399             }
    400             public int getStartYear()  { return startYear; }
    401             public int getStartMonth() { return startMonth; }
    402             public int getStartDay()   { return startDay; }
    403             public int getField()      { return fieldToChange; }
    404             public int getDelta()      { return fieldDelta; }
    405             public int getEndYear()    { return endYear; }
    406             public int getEndMonth()   { return endMonth; }
    407             public int getEndDay()     { return endDay; }
    408         }
    409         final TestAddSetItem[] tests = {
    410             new TestAddSetItem( 1724, 12, 1, Calendar.MONTH, +1, 1725,  0, 1 ),
    411             new TestAddSetItem( 1724, 12, 1, Calendar.MONTH, +9, 1725,  8, 1 ),
    412             new TestAddSetItem( 1723, 12, 2, Calendar.MONTH, +1, 1724,  0, 2 ), // 1723 is a leap year
    413             new TestAddSetItem( 1723, 12, 2, Calendar.MONTH, +9, 1724,  8, 2 ),
    414             new TestAddSetItem( 1725,  0, 1, Calendar.MONTH, -1, 1724, 12, 1 ),
    415             new TestAddSetItem( 1725,  0, 1, Calendar.MONTH, -6, 1724,  7, 1 ),
    416             new TestAddSetItem( 1724, 12, 1, Calendar.DATE,  +8, 1725,  0, 4 ),
    417             new TestAddSetItem( 1723, 12, 1, Calendar.DATE,  +8, 1724,  0, 3 ), // 1723 is a leap year
    418             new TestAddSetItem( 1724,  0, 1, Calendar.DATE,  -1, 1723, 12, 6 ),
    419         };
    420         CopticCalendar testCalendar = new CopticCalendar();
    421         for ( int i = 0; i < tests.length; i++ ) {
    422             TestAddSetItem item = tests[i];
    423             testCalendar.set( item.getStartYear(), item.getStartMonth(), item.getStartDay(), 9, 0 );
    424             testCalendar.add( item.getField(), item.getDelta() );
    425             int endYear = testCalendar.get(Calendar.YEAR);
    426             int endMonth = testCalendar.get(Calendar.MONTH);
    427             int endDay = testCalendar.get(Calendar.DATE);
    428             if ( endYear != item.getEndYear() || endMonth != item.getEndMonth() || endDay != item.getEndDay() ) {
    429                 errln("CToJD FAILS: field " + item.getField() + " delta " + item.getDelta() +
    430                             " expected yr " + item.getEndYear() + " mo " + item.getEndMonth() +  " da " + item.getEndDay() +
    431                             " got yr " + endYear + " mo " + endMonth +  " da " + endDay);
    432             }
    433         }
    434     }
    435 }
    436