2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.calendar;
\r
9 import java.util.Date;
\r
10 import java.util.Locale;
\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
20 * Tests for the <code>IslamicCalendar</code> class.
\r
22 public class IslamicTest extends CalendarTest {
\r
23 public static void main(String args[]) throws Exception {
\r
24 new IslamicTest().run(args);
\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
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
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
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
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
60 IslamicCalendar cal = newCivil();
\r
62 doRollAdd(ROLL, cal, tests);
\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
69 public void TestCivilCases()
\r
71 final TestCase[] tests = {
\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
77 // The months in this table are 1-based rather than 0-based,
\r
78 // because it's easier to edit that way.
\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
116 IslamicCalendar civilCalendar = newCivil();
\r
117 civilCalendar.setLenient(true);
\r
118 doTestCases(tests, civilCalendar);
\r
121 public void TestBasic() {
\r
122 IslamicCalendar cal = newCivil();
\r
124 cal.set(1000, 0, 30);
\r
125 logln("1000/0/30 -> " +
\r
126 cal.get(YEAR) + "/" +
\r
127 cal.get(MONTH) + "/" +
\r
131 logln("1/0/30 -> " +
\r
132 cal.get(YEAR) + "/" +
\r
133 cal.get(MONTH) + "/" +
\r
138 * Test limits of the Islamic calendar
\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
148 // number of days to test - Islamic calendar starts to exhibit
\r
149 // rounding errors after year AH3954 - about 2500 years out.
\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
158 public void TestCoverage() {
\r
160 // new IslamicCalendar(TimeZone)
\r
161 IslamicCalendar cal = new IslamicCalendar(TimeZone.getDefault());
\r
163 errln("could not create IslamicCalendar with TimeZone");
\r
168 // new IslamicCalendar(ULocale)
\r
169 IslamicCalendar cal = new IslamicCalendar(ULocale.getDefault());
\r
171 errln("could not create IslamicCalendar with ULocale");
\r
176 // new IslamicCalendar(Locale)
\r
177 IslamicCalendar cal = new IslamicCalendar(Locale.getDefault());
\r
179 errln("could not create IslamicCalendar with Locale");
\r
184 // new IslamicCalendar(Date)
\r
185 IslamicCalendar cal = new IslamicCalendar(new Date());
\r
187 errln("could not create IslamicCalendar with Date");
\r
192 // new IslamicCalendar(int year, int month, int date)
\r
193 IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1);
\r
195 errln("could not create IslamicCalendar with year,month,date");
\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
203 errln("could not create IslamicCalendar with year,month,date,hour,minute,second");
\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
216 Date now = new Date();
\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
224 logln(then.toString());
\r
226 cal.add(Calendar.MONTH, 1);
\r
227 cal.add(Calendar.DAY_OF_MONTH, 1);
\r
228 cal.add(Calendar.YEAR, 1);
\r
230 logln(cal.getTime().toString());
\r
235 IslamicCalendar cal = new IslamicCalendar(800, IslamicCalendar.RAMADAN, 1);
\r
236 Date time = cal.getTime();
\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
243 String[] formatLocales = {
\r
244 "en", "ar", "fi", "fr", "hu", "iw", "nl"
\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
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
261 private static IslamicCalendar newCivil() {
\r
262 IslamicCalendar civilCalendar = new IslamicCalendar();
\r
263 civilCalendar.setCivil(true);
\r
264 return civilCalendar;
\r