]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/calendar/IslamicTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / calendar / IslamicTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, 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.IslamicCalendar;\r
16 import com.ibm.icu.util.TimeZone;\r
17 import com.ibm.icu.util.ULocale;\r
18 \r
19 /**\r
20  * Tests for the <code>IslamicCalendar</code> class.\r
21  */\r
22 public class IslamicTest extends CalendarTest {\r
23     public static void main(String args[]) throws Exception {\r
24         new IslamicTest().run(args);\r
25     }\r
26 \r
27     /** Constants to save typing. */\r
28     public static final int MUHARRAM = IslamicCalendar.MUHARRAM;\r
29     public static final int SAFAR =  IslamicCalendar.SAFAR;\r
30     public static final int RABI_1 =  IslamicCalendar.RABI_1;\r
31     public static final int RABI_2 =  IslamicCalendar.RABI_2;\r
32     public static final int JUMADA_1 =  IslamicCalendar.JUMADA_1;\r
33     public static final int JUMADA_2 =  IslamicCalendar.JUMADA_2;\r
34     public static final int RAJAB =  IslamicCalendar.RAJAB;\r
35     public static final int SHABAN =  IslamicCalendar.SHABAN;\r
36     public static final int RAMADAN =  IslamicCalendar.RAMADAN;\r
37     public static final int SHAWWAL =  IslamicCalendar.SHAWWAL;\r
38     public static final int QIDAH =  IslamicCalendar.DHU_AL_QIDAH;\r
39     public static final int HIJJAH =  IslamicCalendar.DHU_AL_HIJJAH;\r
40 \r
41     public void TestRoll() {\r
42         int[][] tests = new int[][] {\r
43             //       input                roll by          output\r
44             //  year  month     day     field amount    year  month     day\r
45     \r
46             {   0001, QIDAH,     2,     MONTH,   1,     0001, HIJJAH,    2 },   // non-leap years\r
47             {   0001, QIDAH,     2,     MONTH,   2,     0001, MUHARRAM,  2 },\r
48             {   0001, QIDAH,     2,     MONTH,  -1,     0001, SHAWWAL,   2 },\r
49             {   0001, MUHARRAM,  2,     MONTH,  12,     0001, MUHARRAM,  2 },\r
50             {   0001, MUHARRAM,  2,     MONTH,  13,     0001, SAFAR,     2 },\r
51 \r
52             {   0001, HIJJAH,    1,     DATE,   30,     0001, HIJJAH,    2 },   // 29-day month\r
53             {   0002, HIJJAH,    1,     DATE,   31,     0002, HIJJAH,    2 },   // 30-day month\r
54 \r
55             // Try some rolls that require other fields to be adjusted\r
56             {   0001, MUHARRAM, 30,     MONTH,   1,     0001, SAFAR,    29 },\r
57             {   0002, HIJJAH,   30,     YEAR,   -1,     0001, HIJJAH,   29 },\r
58         };\r
59        \r
60         IslamicCalendar cal = newCivil();\r
61 \r
62         doRollAdd(ROLL, cal, tests);\r
63     }\r
64 \r
65     /**\r
66      * A huge list of test cases to make sure that computeTime and computeFields\r
67      * work properly for a wide range of data in the civil calendar.\r
68      */\r
69     public void TestCivilCases()\r
70     {\r
71         final TestCase[] tests = {\r
72             //\r
73             // Most of these test cases were taken from the back of\r
74             // "Calendrical Calculations", with some extras added to help\r
75             // debug a few of the problems that cropped up in development.\r
76             //\r
77             // The months in this table are 1-based rather than 0-based,\r
78             // because it's easier to edit that way.\r
79             //                       Islamic\r
80             //          Julian Day  Era  Year  Month Day  WkDay Hour Min Sec\r
81             new TestCase(1507231.5,  0, -1245,   12,   9,  SUN,   0,  0,  0),\r
82             new TestCase(1660037.5,  0,  -813,    2,  23,  WED,   0,  0,  0),\r
83             new TestCase(1746893.5,  0,  -568,    4,   1,  WED,   0,  0,  0),\r
84             new TestCase(1770641.5,  0,  -501,    4,   6,  SUN,   0,  0,  0),\r
85             new TestCase(1892731.5,  0,  -157,   10,  17,  WED,   0,  0,  0),\r
86             new TestCase(1931579.5,  0,   -47,    6,   3,  MON,   0,  0,  0),\r
87             new TestCase(1974851.5,  0,    75,    7,  13,  SAT,   0,  0,  0),\r
88             new TestCase(2091164.5,  0,   403,   10,   5,  SUN,   0,  0,  0),\r
89             new TestCase(2121509.5,  0,   489,    5,  22,  SUN,   0,  0,  0),\r
90             new TestCase(2155779.5,  0,   586,    2,   7,  FRI,   0,  0,  0),\r
91             new TestCase(2174029.5,  0,   637,    8,   7,  SAT,   0,  0,  0),\r
92             new TestCase(2191584.5,  0,   687,    2,  20,  FRI,   0,  0,  0),\r
93             new TestCase(2195261.5,  0,   697,    7,   7,  SUN,   0,  0,  0),\r
94             new TestCase(2229274.5,  0,   793,    7,   1,  SUN,   0,  0,  0),\r
95             new TestCase(2245580.5,  0,   839,    7,   6,  WED,   0,  0,  0),\r
96             new TestCase(2266100.5,  0,   897,    6,   1,  SAT,   0,  0,  0),\r
97             new TestCase(2288542.5,  0,   960,    9,  30,  SAT,   0,  0,  0),\r
98             new TestCase(2290901.5,  0,   967,    5,  27,  SAT,   0,  0,  0),\r
99             new TestCase(2323140.5,  0,  1058,    5,  18,  WED,   0,  0,  0),\r
100             new TestCase(2334848.5,  0,  1091,    6,   2,  SUN,   0,  0,  0),\r
101             new TestCase(2348020.5,  0,  1128,    8,   4,  FRI,   0,  0,  0),\r
102             new TestCase(2366978.5,  0,  1182,    2,   3,  SUN,   0,  0,  0),\r
103             new TestCase(2385648.5,  0,  1234,   10,  10,  MON,   0,  0,  0),\r
104             new TestCase(2392825.5,  0,  1255,    1,  11,  WED,   0,  0,  0),\r
105             new TestCase(2416223.5,  0,  1321,    1,  21,  SUN,   0,  0,  0),\r
106             new TestCase(2425848.5,  0,  1348,    3,  19,  SUN,   0,  0,  0),\r
107             new TestCase(2430266.5,  0,  1360,    9,   8,  MON,   0,  0,  0),\r
108             new TestCase(2430833.5,  0,  1362,    4,  13,  MON,   0,  0,  0),\r
109             new TestCase(2431004.5,  0,  1362,   10,   7,  THU,   0,  0,  0),\r
110             new TestCase(2448698.5,  0,  1412,    9,  13,  TUE,   0,  0,  0),\r
111             new TestCase(2450138.5,  0,  1416,   10,   5,  SUN,   0,  0,  0),\r
112             new TestCase(2465737.5,  0,  1460,   10,  12,  WED,   0,  0,  0),\r
113             new TestCase(2486076.5,  0,  1518,    3,   5,  SUN,   0,  0,  0),\r
114         };\r
115         \r
116         IslamicCalendar civilCalendar = newCivil();\r
117         civilCalendar.setLenient(true);\r
118         doTestCases(tests, civilCalendar);\r
119     }\r
120 \r
121     public void TestBasic() {\r
122         IslamicCalendar cal = newCivil();\r
123         cal.clear();\r
124         cal.set(1000, 0, 30);\r
125         logln("1000/0/30 -> " +\r
126               cal.get(YEAR) + "/" +\r
127               cal.get(MONTH) + "/" + \r
128               cal.get(DATE));\r
129         cal.clear();\r
130         cal.set(1, 0, 30);\r
131         logln("1/0/30 -> " +\r
132               cal.get(YEAR) + "/" +\r
133               cal.get(MONTH) + "/" + \r
134               cal.get(DATE));\r
135     }\r
136 \r
137     /**\r
138      * Test limits of the Islamic calendar\r
139      */\r
140     public void TestLimits() {\r
141         Calendar cal = Calendar.getInstance();\r
142         cal.set(2007, Calendar.JANUARY, 1);\r
143         IslamicCalendar islamic = newCivil();\r
144         doLimitsTest(islamic, null, cal.getTime());\r
145         doTheoreticalLimitsTest(islamic, true);\r
146 \r
147 \r
148         // number of days to test - Islamic calendar starts to exhibit \r
149         // rounding errors after year AH3954 - about 2500 years out.\r
150 \r
151         IslamicCalendar islamic2 = new IslamicCalendar();\r
152         islamic2.setCivil(false);\r
153         int testTime = getInclusion() <= 5 ? 20000 : 800000;\r
154         doLimitsTest(islamic2, null, cal.getTime(), testTime);\r
155         doTheoreticalLimitsTest(islamic2, true);\r
156     }\r
157 \r
158     public void TestCoverage() {\r
159     {\r
160         // new IslamicCalendar(TimeZone)\r
161         IslamicCalendar cal = new IslamicCalendar(TimeZone.getDefault());\r
162         if(cal == null){\r
163             errln("could not create IslamicCalendar with TimeZone");\r
164         }\r
165     }\r
166 \r
167     {\r
168         // new IslamicCalendar(ULocale)\r
169         IslamicCalendar cal = new IslamicCalendar(ULocale.getDefault());\r
170         if(cal == null){\r
171             errln("could not create IslamicCalendar with ULocale");\r
172         }\r
173     }\r
174         \r
175     {\r
176         // new IslamicCalendar(Locale)\r
177         IslamicCalendar cal = new IslamicCalendar(Locale.getDefault());\r
178         if(cal == null){\r
179             errln("could not create IslamicCalendar with Locale");\r
180         }\r
181     }\r
182 \r
183     {\r
184         // new IslamicCalendar(Date)\r
185         IslamicCalendar cal = new IslamicCalendar(new Date());\r
186         if(cal == null){\r
187             errln("could not create IslamicCalendar with Date");\r
188         }\r
189     }\r
190 \r
191     {\r
192         // new IslamicCalendar(int year, int month, int date)\r
193         IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1);\r
194         if(cal == null){\r
195             errln("could not create IslamicCalendar with year,month,date");\r
196         }\r
197     }\r
198 \r
199     {\r
200         // new IslamicCalendar(int year, int month, int date, int hour, int minute, int second)\r
201         IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1, 1, 1, 1);\r
202         if(cal == null){\r
203             errln("could not create IslamicCalendar with year,month,date,hour,minute,second");\r
204         }\r
205     }\r
206 \r
207     {\r
208         // setCivil/isCivil\r
209         // operations on non-civil calendar\r
210         IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1, 1, 1, 1);\r
211         cal.setCivil(false);\r
212         if (cal.isCivil()) {\r
213         errln("islamic calendar is civil");\r
214         }\r
215 \r
216         Date now = new Date();\r
217         cal.setTime(now);\r
218 \r
219         Date then = cal.getTime();\r
220         if (!now.equals(then)) {\r
221         errln("get/set time failed with non-civil islamic calendar");\r
222         }\r
223 \r
224         logln(then.toString());\r
225 \r
226         cal.add(Calendar.MONTH, 1);\r
227         cal.add(Calendar.DAY_OF_MONTH, 1);\r
228         cal.add(Calendar.YEAR, 1);\r
229 \r
230         logln(cal.getTime().toString());\r
231     }\r
232     \r
233     {\r
234         // data\r
235         IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1);\r
236         Date time = cal.getTime();\r
237 \r
238         String[] calendarLocales = {\r
239         "ar_AE", "ar_BH", "ar_DZ", "ar_EG", "ar_JO", "ar_KW", "ar_OM", \r
240         "ar_QA", "ar_SA", "ar_SY", "ar_YE", "ms_MY"\r
241         };\r
242 \r
243         String[] formatLocales = {\r
244         "en", "ar", "fi", "fr", "hu", "iw", "nl"\r
245         };\r
246         for (int i = 0; i < calendarLocales.length; ++i) {\r
247         String calLocName = calendarLocales[i];\r
248         Locale calLocale = LocaleUtility.getLocaleFromName(calLocName);\r
249         cal = new IslamicCalendar(calLocale);\r
250 \r
251         for (int j = 0; j < formatLocales.length; ++j) {\r
252             String locName = formatLocales[j];\r
253             Locale formatLocale = LocaleUtility.getLocaleFromName(locName);\r
254             DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale);\r
255             logln(calLocName + "/" + locName + " --> " + format.format(time));\r
256         }\r
257         }\r
258     }\r
259     }\r
260 \r
261     private static IslamicCalendar newCivil() {\r
262         IslamicCalendar civilCalendar = new IslamicCalendar();\r
263         civilCalendar.setCivil(true);\r
264         return civilCalendar;\r
265     }\r
266     \r
267 }\r