]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/calendar/IndianTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / calendar / IndianTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2005-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.calendar;\r
8 \r
9 import java.util.Date;\r
10 import java.util.Locale;\r
11 \r
12 import com.ibm.icu.impl.LocaleUtility;\r
13 import com.ibm.icu.text.DateFormat;\r
14 import com.ibm.icu.util.Calendar;\r
15 import com.ibm.icu.util.GregorianCalendar;\r
16 import com.ibm.icu.util.IndianCalendar;\r
17 import com.ibm.icu.util.TimeZone;\r
18 import com.ibm.icu.util.ULocale;\r
19 \r
20 /**\r
21  * Tests for the <code>IndianCalendar</code> class.\r
22  */\r
23 public class IndianTest extends CalendarTest \r
24 {\r
25     public static void main(String args[]) throws Exception {\r
26         new IndianTest().run(args);\r
27     }\r
28     // Months in indian calendar are 0-based. Here taking 1-based names:\r
29     public static final int CHAITRA = IndianCalendar.CHAITRA + 1; \r
30     public static final int VAISAKHA = IndianCalendar.VAISAKHA + 1;\r
31     public static final int JYAISTHA = IndianCalendar.JYAISTHA + 1;\r
32     public static final int ASADHA = IndianCalendar.ASADHA + 1;\r
33     public static final int SRAVANA = IndianCalendar.SRAVANA + 1 ;\r
34     public static final int BHADRA = IndianCalendar.BHADRA + 1 ;\r
35     public static final int  ASVINA = IndianCalendar.ASVINA  + 1 ;\r
36     public static final int KARTIKA = IndianCalendar.KARTIKA + 1 ;\r
37     public static final int AGRAHAYANA = IndianCalendar.AGRAHAYANA + 1 ;\r
38     public static final int PAUSA = IndianCalendar.PAUSA + 1 ;\r
39     public static final int MAGHA = IndianCalendar.MAGHA + 1 ;\r
40     public static final int PHALGUNA = IndianCalendar.PHALGUNA + 1 ;\r
41 \r
42     /** Constants to save typing. */\r
43     /* Test dates generated from:\r
44      * http://www.fourmilab.ch/documents/calendar/ \r
45     \r
46     /** A huge list of test cases to make sure that computeTime and computeFields\r
47      * work properly for a wide range of data in the Indian civil calendar.\r
48      */\r
49     public void TestCases()\r
50     {\r
51         final TestCase[] tests = {\r
52             //\r
53             // The months in this table are 1-based rather than 0-based,\r
54             // because it's easier to edit that way.\r
55             //                      Indian\r
56             //          Julian Day  Era  Year   Month Day    WkDay Hour Min Sec\r
57            new TestCase(1770641.5,  0,    57,    ASVINA,       10,  SUN,   0,  0,  0),\r
58            new TestCase(1892731.5,  0,   391,    PAUSA,        18,  WED,   0,  0,  0),\r
59            new TestCase(1931579.5,  0,   498,    VAISAKHA,     30,  MON,   0,  0,  0),\r
60            new TestCase(1974851.5,  0,   616,    KARTIKA,      19,  SAT,   0,  0,  0),\r
61            new TestCase(2091164.5,  0,   935,    VAISAKHA,      5,  SUN,   0,  0,  0),\r
62            new TestCase(2121509.5,  0,  1018,    JYAISTHA,      3,  SUN,   0,  0,  0),\r
63            new TestCase(2155779.5,  0,  1112,    CHAITRA,       2,  FRI,   0,  0,  0),\r
64            new TestCase(2174029.5,  0,  1161,    PHALGUNA,     20,  SAT,   0,  0,  0),\r
65            new TestCase(2191584.5,  0,  1210,    CHAITRA,      13,  FRI,   0,  0,  0),\r
66            new TestCase(2195261.5,  0,  1220,    VAISAKHA,      7,  SUN,   0,  0,  0),\r
67            new TestCase(2229274.5,  0,  1313,    JYAISTHA,     22,  SUN,   0,  0,  0),\r
68            new TestCase(2245580.5,  0,  1357,    MAGHA,        14,  WED,   0,  0,  0),\r
69            new TestCase(2266100.5,  0,  1414,    CHAITRA,      20,  SAT,   0,  0,  0),\r
70            new TestCase(2288542.5,  0,  1475,    BHADRA,       28,  SAT,   0,  0,  0),\r
71            new TestCase(2290901.5,  0,  1481,    PHALGUNA,     15,  SAT,   0,  0,  0),\r
72            new TestCase(2323140.5,  0,  1570,    JYAISTHA,     20,  WED,   0,  0,  0),\r
73            new TestCase(2334551.5,  0,  1601,    BHADRA,       16,  THU,   0,  0,  0),\r
74            new TestCase(2334581.5,  0,  1601,    ASVINA,       15,  SAT,   0,  0,  0),\r
75            new TestCase(2334610.5,  0,  1601,    KARTIKA,      14,  SUN,   0,  0,  0),\r
76            new TestCase(2334639.5,  0,  1601,    AGRAHAYANA,   13,  MON,   0,  0,  0),\r
77            new TestCase(2334668.5,  0,  1601,    PAUSA,        12,  TUE,   0,  0,  0),\r
78            new TestCase(2334698.5,  0,  1601,    MAGHA,        12,  THU,   0,  0,  0),\r
79            new TestCase(2334728.5,  0,  1601,    PHALGUNA,     12,  SAT,   0,  0,  0),\r
80            new TestCase(2334757.5,  0,  1602,    CHAITRA,      11,  SUN,   0,  0,  0),\r
81            new TestCase(2334787.5,  0,  1602,    VAISAKHA,     10,  TUE,   0,  0,  0),\r
82            new TestCase(2334816.5,  0,  1602,    JYAISTHA,      8,  WED,   0,  0,  0),\r
83            new TestCase(2334846.5,  0,  1602,    ASADHA,        7,  FRI,   0,  0,  0),\r
84            new TestCase(2334848.5,  0,  1602,    ASADHA,        9,  SUN,   0,  0,  0),\r
85            new TestCase(2348020.5,  0,  1638,    SRAVANA,       2,  FRI,   0,  0,  0),\r
86            new TestCase(2334934.5,  0,  1602,    ASVINA,        2,  TUE,   0,  0,  0),\r
87            new TestCase(2366978.5,  0,  1690,    JYAISTHA,     29,  SUN,   0,  0,  0),\r
88            new TestCase(2385648.5,  0,  1741,    SRAVANA,      11,  MON,   0,  0,  0),\r
89            new TestCase(2392825.5,  0,  1761,    CHAITRA,       6,  WED,   0,  0,  0),\r
90            new TestCase(2416223.5,  0,  1825,    CHAITRA,      29,  SUN,   0,  0,  0),\r
91            new TestCase(2425848.5,  0,  1851,    BHADRA,        3,  SUN,   0,  0,  0),\r
92            new TestCase(2430266.5,  0,  1863,    ASVINA,        7,  MON,   0,  0,  0),\r
93            new TestCase(2430833.5,  0,  1865,    CHAITRA,      29,  MON,   0,  0,  0),\r
94            new TestCase(2431004.5,  0,  1865,    ASVINA,       15,  THU,   0,  0,  0),\r
95            new TestCase(2448698.5,  0,  1913,    PHALGUNA,     27,  TUE,   0,  0,  0),\r
96            new TestCase(2450138.5,  0,  1917,    PHALGUNA,      6,  SUN,   0,  0,  0),\r
97            new TestCase(2465737.5,  0,  1960,    KARTIKA,      19,  WED,   0,  0,  0),\r
98            new TestCase(2486076.5,  0,  2016,    ASADHA,       27,  SUN,   0,  0,  0),\r
99         };\r
100         \r
101         IndianCalendar testCalendar = new IndianCalendar();\r
102         testCalendar.setLenient(true);\r
103         doTestCases(tests, testCalendar);\r
104     }\r
105 \r
106     public void TestBasic() {\r
107         IndianCalendar cal = new IndianCalendar();\r
108         cal.clear();\r
109         cal.set(1000, 0, 30);\r
110         logln("1000/0/30-> " +\r
111               cal.get(YEAR) + "/" +\r
112               cal.get(MONTH) + "/" + \r
113               cal.get(DATE));\r
114         cal.clear();\r
115         cal.set(1, 0, 30);\r
116         logln("1/0/30 -> " +\r
117               cal.get(YEAR) + "/" +\r
118               cal.get(MONTH) + "/" + \r
119               cal.get(DATE));\r
120     }\r
121 \r
122     public void TestCoverage() {\r
123 \r
124         {\r
125             // new IndianCalendar(TimeZone)\r
126             IndianCalendar cal = new IndianCalendar(TimeZone.getDefault()); \r
127             if(cal == null){\r
128                 errln("could not create IndianCalendar with TimeZone");\r
129             }\r
130         }\r
131 \r
132         {\r
133             // new IndianCalendar(ULocale)\r
134             IndianCalendar cal = new IndianCalendar(ULocale.getDefault());\r
135             if(cal == null){\r
136                 errln("could not create IndianCalendar with ULocale");\r
137             }\r
138         }\r
139         \r
140         {\r
141             // new IndianCalendar(Locale)\r
142             IndianCalendar cal = new IndianCalendar(Locale.getDefault());\r
143             if(cal == null){\r
144                 errln("could not create IndianCalendar with Locale");\r
145             }\r
146         }\r
147         \r
148         {                                                                                       \r
149             // new IndianCalendar(TimeZone, Locale)                                             \r
150             IndianCalendar cal = new IndianCalendar(TimeZone.getDefault(),Locale.getDefault()); \r
151             if(cal == null){                                                                    \r
152                 errln("could not create IndianCalendar with TimeZone, Locale");                 \r
153             }                                                                                   \r
154         }                                                                                       \r
155                                                                                                 \r
156         {                                                                                       \r
157             // new IndianCalendar(TimeZone, ULocale)                                            \r
158             IndianCalendar cal = new IndianCalendar(TimeZone.getDefault(),ULocale.getDefault());\r
159             if(cal == null){                                                                    \r
160                 errln("could not create IndianCalendar with TimeZone, ULocale");                \r
161             }                                                                                   \r
162         }                                                                                       \r
163         \r
164         {\r
165             // new IndianCalendar(Date)\r
166             IndianCalendar cal = new IndianCalendar(new Date());\r
167             if(cal == null){\r
168                 errln("could not create IndianCalendar with Date");\r
169             }\r
170         }\r
171         {\r
172             // new IndianCalendar(int year, int month, int date)\r
173             IndianCalendar cal = new IndianCalendar(1917, IndianCalendar.CHAITRA, 1);\r
174             if(cal == null){\r
175                 errln("could not create IndianCalendar with year,month,date");\r
176             }\r
177         }\r
178         {\r
179             // new IndianCalendar(int year, int month, int date, int hour, int minute, int second)\r
180             IndianCalendar cal = new IndianCalendar(1917, IndianCalendar.CHAITRA, 1, 1, 1, 1);\r
181             if(cal == null){\r
182                 errln("could not create IndianCalendar with year,month,date,hour,minute,second");\r
183             }\r
184         }\r
185     \r
186         {\r
187             // data\r
188             String[] calendarLocales = {\r
189                 "bn_IN", "gu_IN", "hi_IN", "kn_IN", "ml_IN", "or_IN", "pa_IN", "ta_IN", "te_IN"\r
190             };\r
191 \r
192             String[] formatLocales = {\r
193                 "en", "fr", "bn", "gu", "hi", "kn", "ml", "or", "pa", "ta", "te"\r
194             };\r
195 \r
196             for (int i = 0; i < calendarLocales.length; ++i) {\r
197                 String calLocName = calendarLocales[i];\r
198                 Locale calLocale = LocaleUtility.getLocaleFromName(calLocName);\r
199                 IndianCalendar cal = new IndianCalendar(calLocale);\r
200                 cal.set(-1039, 9, 21);\r
201 \r
202                 for (int j = 0; j < formatLocales.length; j++  ) {\r
203                     String locName = formatLocales[j];\r
204                     Locale formatLocale = LocaleUtility.getLocaleFromName(locName);\r
205                     DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale);\r
206                     logln(calLocName + "/" + locName + " --> " + format.format(cal));\r
207                 }\r
208             }\r
209         }\r
210     }\r
211 \r
212     public void TestYear() {\r
213         // Gregorian Calendar\r
214         Calendar gCal= new GregorianCalendar();\r
215         Date gToday=gCal.getTime();\r
216         gCal.add(GregorianCalendar.MONTH,2);\r
217         Date gFuture=gCal.getTime();\r
218         DateFormat gDF = DateFormat.getDateInstance(gCal,DateFormat.FULL);\r
219         logln("gregorian calendar: " + gDF.format(gToday) +\r
220               " + 2 months = " + gDF.format(gFuture));\r
221 \r
222         // Indian Calendar\r
223         IndianCalendar iCal= new IndianCalendar();\r
224         Date iToday=iCal.getTime();\r
225         iCal.add(IndianCalendar.MONTH,2);\r
226         Date iFuture=iCal.getTime();\r
227         DateFormat iDF = DateFormat.getDateInstance(iCal,DateFormat.FULL);\r
228         logln("Indian calendar: " + iDF.format(iToday) +\r
229               " + 2 months = " + iDF.format(iFuture));\r
230 \r
231     }\r
232 \r
233     /**\r
234      * Test limits of the Indian calendar\r
235      */\r
236     public void TestLimits() {\r
237         Calendar cal = Calendar.getInstance();\r
238         cal.set(2007, Calendar.JANUARY, 1);\r
239         IndianCalendar indian = new IndianCalendar();\r
240         doLimitsTest(indian, null, cal.getTime());\r
241         doTheoreticalLimitsTest(indian, true);\r
242     }\r
243 }\r