2 *******************************************************************************
3 * Copyright (C) 1996-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.calendar;
10 import java.util.Locale;
12 import com.ibm.icu.impl.LocaleUtility;
13 import com.ibm.icu.text.DateFormat;
14 import com.ibm.icu.util.Calendar;
15 import com.ibm.icu.util.IslamicCalendar;
16 import com.ibm.icu.util.TimeZone;
17 import com.ibm.icu.util.ULocale;
20 * Tests for the <code>IslamicCalendar</code> class.
22 public class IslamicTest extends CalendarTest {
23 public static void main(String args[]) throws Exception {
24 new IslamicTest().run(args);
27 /** Constants to save typing. */
28 public static final int MUHARRAM = IslamicCalendar.MUHARRAM;
29 public static final int SAFAR = IslamicCalendar.SAFAR;
30 public static final int RABI_1 = IslamicCalendar.RABI_1;
31 public static final int RABI_2 = IslamicCalendar.RABI_2;
32 public static final int JUMADA_1 = IslamicCalendar.JUMADA_1;
33 public static final int JUMADA_2 = IslamicCalendar.JUMADA_2;
34 public static final int RAJAB = IslamicCalendar.RAJAB;
35 public static final int SHABAN = IslamicCalendar.SHABAN;
36 public static final int RAMADAN = IslamicCalendar.RAMADAN;
37 public static final int SHAWWAL = IslamicCalendar.SHAWWAL;
38 public static final int QIDAH = IslamicCalendar.DHU_AL_QIDAH;
39 public static final int HIJJAH = IslamicCalendar.DHU_AL_HIJJAH;
41 public void TestRoll() {
42 int[][] tests = new int[][] {
43 // input roll by output
44 // year month day field amount year month day
46 { 0001, QIDAH, 2, MONTH, 1, 0001, HIJJAH, 2 }, // non-leap years
47 { 0001, QIDAH, 2, MONTH, 2, 0001, MUHARRAM, 2 },
48 { 0001, QIDAH, 2, MONTH, -1, 0001, SHAWWAL, 2 },
49 { 0001, MUHARRAM, 2, MONTH, 12, 0001, MUHARRAM, 2 },
50 { 0001, MUHARRAM, 2, MONTH, 13, 0001, SAFAR, 2 },
52 { 0001, HIJJAH, 1, DATE, 30, 0001, HIJJAH, 2 }, // 29-day month
53 { 0002, HIJJAH, 1, DATE, 31, 0002, HIJJAH, 2 }, // 30-day month
55 // Try some rolls that require other fields to be adjusted
56 { 0001, MUHARRAM, 30, MONTH, 1, 0001, SAFAR, 29 },
57 { 0002, HIJJAH, 30, YEAR, -1, 0001, HIJJAH, 29 },
60 IslamicCalendar cal = newCivil();
62 doRollAdd(ROLL, cal, tests);
66 * A huge list of test cases to make sure that computeTime and computeFields
67 * work properly for a wide range of data in the civil calendar.
69 public void TestCivilCases()
71 final TestCase[] tests = {
73 // Most of these test cases were taken from the back of
74 // "Calendrical Calculations", with some extras added to help
75 // debug a few of the problems that cropped up in development.
77 // The months in this table are 1-based rather than 0-based,
78 // because it's easier to edit that way.
80 // Julian Day Era Year Month Day WkDay Hour Min Sec
81 new TestCase(1507231.5, 0, -1245, 12, 9, SUN, 0, 0, 0),
82 new TestCase(1660037.5, 0, -813, 2, 23, WED, 0, 0, 0),
83 new TestCase(1746893.5, 0, -568, 4, 1, WED, 0, 0, 0),
84 new TestCase(1770641.5, 0, -501, 4, 6, SUN, 0, 0, 0),
85 new TestCase(1892731.5, 0, -157, 10, 17, WED, 0, 0, 0),
86 new TestCase(1931579.5, 0, -47, 6, 3, MON, 0, 0, 0),
87 new TestCase(1974851.5, 0, 75, 7, 13, SAT, 0, 0, 0),
88 new TestCase(2091164.5, 0, 403, 10, 5, SUN, 0, 0, 0),
89 new TestCase(2121509.5, 0, 489, 5, 22, SUN, 0, 0, 0),
90 new TestCase(2155779.5, 0, 586, 2, 7, FRI, 0, 0, 0),
91 new TestCase(2174029.5, 0, 637, 8, 7, SAT, 0, 0, 0),
92 new TestCase(2191584.5, 0, 687, 2, 20, FRI, 0, 0, 0),
93 new TestCase(2195261.5, 0, 697, 7, 7, SUN, 0, 0, 0),
94 new TestCase(2229274.5, 0, 793, 7, 1, SUN, 0, 0, 0),
95 new TestCase(2245580.5, 0, 839, 7, 6, WED, 0, 0, 0),
96 new TestCase(2266100.5, 0, 897, 6, 1, SAT, 0, 0, 0),
97 new TestCase(2288542.5, 0, 960, 9, 30, SAT, 0, 0, 0),
98 new TestCase(2290901.5, 0, 967, 5, 27, SAT, 0, 0, 0),
99 new TestCase(2323140.5, 0, 1058, 5, 18, WED, 0, 0, 0),
100 new TestCase(2334848.5, 0, 1091, 6, 2, SUN, 0, 0, 0),
101 new TestCase(2348020.5, 0, 1128, 8, 4, FRI, 0, 0, 0),
102 new TestCase(2366978.5, 0, 1182, 2, 3, SUN, 0, 0, 0),
103 new TestCase(2385648.5, 0, 1234, 10, 10, MON, 0, 0, 0),
104 new TestCase(2392825.5, 0, 1255, 1, 11, WED, 0, 0, 0),
105 new TestCase(2416223.5, 0, 1321, 1, 21, SUN, 0, 0, 0),
106 new TestCase(2425848.5, 0, 1348, 3, 19, SUN, 0, 0, 0),
107 new TestCase(2430266.5, 0, 1360, 9, 8, MON, 0, 0, 0),
108 new TestCase(2430833.5, 0, 1362, 4, 13, MON, 0, 0, 0),
109 new TestCase(2431004.5, 0, 1362, 10, 7, THU, 0, 0, 0),
110 new TestCase(2448698.5, 0, 1412, 9, 13, TUE, 0, 0, 0),
111 new TestCase(2450138.5, 0, 1416, 10, 5, SUN, 0, 0, 0),
112 new TestCase(2465737.5, 0, 1460, 10, 12, WED, 0, 0, 0),
113 new TestCase(2486076.5, 0, 1518, 3, 5, SUN, 0, 0, 0),
116 IslamicCalendar civilCalendar = newCivil();
117 civilCalendar.setLenient(true);
118 doTestCases(tests, civilCalendar);
121 public void TestBasic() {
122 IslamicCalendar cal = newCivil();
124 cal.set(1000, 0, 30);
125 logln("1000/0/30 -> " +
126 cal.get(YEAR) + "/" +
127 cal.get(MONTH) + "/" +
132 cal.get(YEAR) + "/" +
133 cal.get(MONTH) + "/" +
138 * Test limits of the Islamic calendar
140 public void TestLimits() {
141 Calendar cal = Calendar.getInstance();
142 cal.set(2007, Calendar.JANUARY, 1);
143 IslamicCalendar islamic = newCivil();
144 doLimitsTest(islamic, null, cal.getTime());
145 doTheoreticalLimitsTest(islamic, true);
148 // number of days to test - Islamic calendar starts to exhibit
149 // rounding errors after year AH3954 - about 2500 years out.
151 IslamicCalendar islamic2 = new IslamicCalendar();
152 islamic2.setCivil(false);
153 int testTime = getInclusion() <= 5 ? 20000 : 800000;
154 doLimitsTest(islamic2, null, cal.getTime(), testTime);
155 doTheoreticalLimitsTest(islamic2, true);
158 public void Test7427() {
159 // Test the add month in a leap year problem as reported in ticket #7427
160 IslamicCalendar cal = new IslamicCalendar();
162 cal.set(IslamicCalendar.YEAR,1431);
163 cal.set(IslamicCalendar.MONTH, IslamicCalendar.DHU_AL_HIJJAH);
164 cal.add(IslamicCalendar.MONTH,1);
165 if ( cal.get(IslamicCalendar.MONTH) != IslamicCalendar.MUHARRAM ||
166 ( cal.get(IslamicCalendar.YEAR) != 1432 )) {
167 errln("Error incrementing month at the end of a leap year. Expected Month:0 Year:1432 - Got Month:" +
168 cal.get(IslamicCalendar.MONTH) + " Year:" + cal.get(IslamicCalendar.YEAR));
171 public void TestCoverage() {
173 // new IslamicCalendar(TimeZone)
174 IslamicCalendar cal = new IslamicCalendar(TimeZone.getDefault());
176 errln("could not create IslamicCalendar with TimeZone");
181 // new IslamicCalendar(ULocale)
182 IslamicCalendar cal = new IslamicCalendar(ULocale.getDefault());
184 errln("could not create IslamicCalendar with ULocale");
189 // new IslamicCalendar(Locale)
190 IslamicCalendar cal = new IslamicCalendar(Locale.getDefault());
192 errln("could not create IslamicCalendar with Locale");
197 // new IslamicCalendar(Date)
198 IslamicCalendar cal = new IslamicCalendar(new Date());
200 errln("could not create IslamicCalendar with Date");
205 // new IslamicCalendar(int year, int month, int date)
206 IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1);
208 errln("could not create IslamicCalendar with year,month,date");
213 // new IslamicCalendar(int year, int month, int date, int hour, int minute, int second)
214 IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1, 1, 1, 1);
216 errln("could not create IslamicCalendar with year,month,date,hour,minute,second");
222 // operations on non-civil calendar
223 IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1, 1, 1, 1);
226 errln("islamic calendar is civil");
229 Date now = new Date();
232 Date then = cal.getTime();
233 if (!now.equals(then)) {
234 errln("get/set time failed with non-civil islamic calendar");
237 logln(then.toString());
239 cal.add(Calendar.MONTH, 1);
240 cal.add(Calendar.DAY_OF_MONTH, 1);
241 cal.add(Calendar.YEAR, 1);
243 logln(cal.getTime().toString());
248 IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1);
249 Date time = cal.getTime();
251 String[] calendarLocales = {
252 "ar_AE", "ar_BH", "ar_DZ", "ar_EG", "ar_JO", "ar_KW", "ar_OM",
253 "ar_QA", "ar_SA", "ar_SY", "ar_YE", "ms_MY"
256 String[] formatLocales = {
257 "en", "ar", "fi", "fr", "hu", "iw", "nl"
259 for (int i = 0; i < calendarLocales.length; ++i) {
260 String calLocName = calendarLocales[i];
261 Locale calLocale = LocaleUtility.getLocaleFromName(calLocName);
262 cal = new IslamicCalendar(calLocale);
264 for (int j = 0; j < formatLocales.length; ++j) {
265 String locName = formatLocales[j];
266 Locale formatLocale = LocaleUtility.getLocaleFromName(locName);
267 DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.FULL, formatLocale);
268 logln(calLocName + "/" + locName + " --> " + format.format(time));
274 private static IslamicCalendar newCivil() {
275 IslamicCalendar civilCalendar = new IslamicCalendar();
276 civilCalendar.setCivil(true);
277 return civilCalendar;