001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.calendar;
008:
009: import java.util.Date;
010: import java.util.Locale;
011:
012: import com.ibm.icu.impl.LocaleUtility;
013: import com.ibm.icu.text.DateFormat;
014: import com.ibm.icu.util.Calendar;
015: import com.ibm.icu.util.IslamicCalendar;
016: import com.ibm.icu.util.TimeZone;
017: import com.ibm.icu.util.ULocale;
018:
019: /**
020: * Tests for the <code>IslamicCalendar</code> class.
021: */
022: public class IslamicTest extends CalendarTest {
023: public static void main(String args[]) throws Exception {
024: new IslamicTest().run(args);
025: }
026:
027: /** Constants to save typing. */
028: public static final int MUHARRAM = IslamicCalendar.MUHARRAM;
029: public static final int SAFAR = IslamicCalendar.SAFAR;
030: public static final int RABI_1 = IslamicCalendar.RABI_1;
031: public static final int RABI_2 = IslamicCalendar.RABI_2;
032: public static final int JUMADA_1 = IslamicCalendar.JUMADA_1;
033: public static final int JUMADA_2 = IslamicCalendar.JUMADA_2;
034: public static final int RAJAB = IslamicCalendar.RAJAB;
035: public static final int SHABAN = IslamicCalendar.SHABAN;
036: public static final int RAMADAN = IslamicCalendar.RAMADAN;
037: public static final int SHAWWAL = IslamicCalendar.SHAWWAL;
038: public static final int QIDAH = IslamicCalendar.DHU_AL_QIDAH;
039: public static final int HIJJAH = IslamicCalendar.DHU_AL_HIJJAH;
040:
041: public void TestRoll() {
042: int[][] tests = new int[][] {
043: // input roll by output
044: // year month day field amount year month day
045:
046: { 0001, QIDAH, 2, MONTH, 1, 0001, HIJJAH, 2 }, // non-leap years
047: { 0001, QIDAH, 2, MONTH, 2, 0001, MUHARRAM, 2 },
048: { 0001, QIDAH, 2, MONTH, -1, 0001, SHAWWAL, 2 },
049: { 0001, MUHARRAM, 2, MONTH, 12, 0001, MUHARRAM, 2 },
050: { 0001, MUHARRAM, 2, MONTH, 13, 0001, SAFAR, 2 },
051:
052: { 0001, HIJJAH, 1, DATE, 30, 0001, HIJJAH, 2 }, // 29-day month
053: { 0002, HIJJAH, 1, DATE, 31, 0002, HIJJAH, 2 }, // 30-day month
054:
055: // Try some rolls that require other fields to be adjusted
056: { 0001, MUHARRAM, 30, MONTH, 1, 0001, SAFAR, 29 },
057: { 0002, HIJJAH, 30, YEAR, -1, 0001, HIJJAH, 29 }, };
058:
059: IslamicCalendar cal = newCivil();
060:
061: doRollAdd(ROLL, cal, tests);
062: }
063:
064: /**
065: * A huge list of test cases to make sure that computeTime and computeFields
066: * work properly for a wide range of data in the civil calendar.
067: */
068: public void TestCivilCases() {
069: final TestCase[] tests = {
070: //
071: // Most of these test cases were taken from the back of
072: // "Calendrical Calculations", with some extras added to help
073: // debug a few of the problems that cropped up in development.
074: //
075: // The months in this table are 1-based rather than 0-based,
076: // because it's easier to edit that way.
077: // Islamic
078: // Julian Day Era Year Month Day WkDay Hour Min Sec
079: new TestCase(1507231.5, 0, -1245, 12, 9, SUN, 0, 0, 0),
080: new TestCase(1660037.5, 0, -813, 2, 23, WED, 0, 0, 0),
081: new TestCase(1746893.5, 0, -568, 4, 1, WED, 0, 0, 0),
082: new TestCase(1770641.5, 0, -501, 4, 6, SUN, 0, 0, 0),
083: new TestCase(1892731.5, 0, -157, 10, 17, WED, 0, 0, 0),
084: new TestCase(1931579.5, 0, -47, 6, 3, MON, 0, 0, 0),
085: new TestCase(1974851.5, 0, 75, 7, 13, SAT, 0, 0, 0),
086: new TestCase(2091164.5, 0, 403, 10, 5, SUN, 0, 0, 0),
087: new TestCase(2121509.5, 0, 489, 5, 22, SUN, 0, 0, 0),
088: new TestCase(2155779.5, 0, 586, 2, 7, FRI, 0, 0, 0),
089: new TestCase(2174029.5, 0, 637, 8, 7, SAT, 0, 0, 0),
090: new TestCase(2191584.5, 0, 687, 2, 20, FRI, 0, 0, 0),
091: new TestCase(2195261.5, 0, 697, 7, 7, SUN, 0, 0, 0),
092: new TestCase(2229274.5, 0, 793, 7, 1, SUN, 0, 0, 0),
093: new TestCase(2245580.5, 0, 839, 7, 6, WED, 0, 0, 0),
094: new TestCase(2266100.5, 0, 897, 6, 1, SAT, 0, 0, 0),
095: new TestCase(2288542.5, 0, 960, 9, 30, SAT, 0, 0, 0),
096: new TestCase(2290901.5, 0, 967, 5, 27, SAT, 0, 0, 0),
097: new TestCase(2323140.5, 0, 1058, 5, 18, WED, 0, 0, 0),
098: new TestCase(2334848.5, 0, 1091, 6, 2, SUN, 0, 0, 0),
099: new TestCase(2348020.5, 0, 1128, 8, 4, FRI, 0, 0, 0),
100: new TestCase(2366978.5, 0, 1182, 2, 3, SUN, 0, 0, 0),
101: new TestCase(2385648.5, 0, 1234, 10, 10, MON, 0, 0, 0),
102: new TestCase(2392825.5, 0, 1255, 1, 11, WED, 0, 0, 0),
103: new TestCase(2416223.5, 0, 1321, 1, 21, SUN, 0, 0, 0),
104: new TestCase(2425848.5, 0, 1348, 3, 19, SUN, 0, 0, 0),
105: new TestCase(2430266.5, 0, 1360, 9, 8, MON, 0, 0, 0),
106: new TestCase(2430833.5, 0, 1362, 4, 13, MON, 0, 0, 0),
107: new TestCase(2431004.5, 0, 1362, 10, 7, THU, 0, 0, 0),
108: new TestCase(2448698.5, 0, 1412, 9, 13, TUE, 0, 0, 0),
109: new TestCase(2450138.5, 0, 1416, 10, 5, SUN, 0, 0, 0),
110: new TestCase(2465737.5, 0, 1460, 10, 12, WED, 0, 0, 0),
111: new TestCase(2486076.5, 0, 1518, 3, 5, SUN, 0, 0, 0), };
112:
113: IslamicCalendar civilCalendar = newCivil();
114: civilCalendar.setLenient(true);
115: doTestCases(tests, civilCalendar);
116: }
117:
118: public void TestBasic() {
119: IslamicCalendar cal = newCivil();
120: cal.clear();
121: cal.set(1000, 0, 30);
122: logln("1000/0/30 -> " + cal.get(YEAR) + "/" + cal.get(MONTH)
123: + "/" + cal.get(DATE));
124: cal.clear();
125: cal.set(1, 0, 30);
126: logln("1/0/30 -> " + cal.get(YEAR) + "/" + cal.get(MONTH) + "/"
127: + cal.get(DATE));
128: }
129:
130: public void TestCoverage() {
131: {
132: // new IslamicCalendar(TimeZone)
133: IslamicCalendar cal = new IslamicCalendar(TimeZone
134: .getDefault());
135: if (cal == null) {
136: errln("could not create IslamicCalendar with TimeZone");
137: }
138: }
139:
140: {
141: // new IslamicCalendar(ULocale)
142: IslamicCalendar cal = new IslamicCalendar(ULocale
143: .getDefault());
144: if (cal == null) {
145: errln("could not create IslamicCalendar with ULocale");
146: }
147: }
148:
149: {
150: // new IslamicCalendar(Locale)
151: IslamicCalendar cal = new IslamicCalendar(Locale
152: .getDefault());
153: if (cal == null) {
154: errln("could not create IslamicCalendar with Locale");
155: }
156: }
157:
158: {
159: // new IslamicCalendar(Date)
160: IslamicCalendar cal = new IslamicCalendar(new Date());
161: if (cal == null) {
162: errln("could not create IslamicCalendar with Date");
163: }
164: }
165:
166: {
167: // new IslamicCalendar(int year, int month, int date)
168: IslamicCalendar cal = new IslamicCalendar(800,
169: IslamicCalendar.RAMADAN, 1);
170: if (cal == null) {
171: errln("could not create IslamicCalendar with year,month,date");
172: }
173: }
174:
175: {
176: // new IslamicCalendar(int year, int month, int date, int hour, int minute, int second)
177: IslamicCalendar cal = new IslamicCalendar(800,
178: IslamicCalendar.RAMADAN, 1, 1, 1, 1);
179: if (cal == null) {
180: errln("could not create IslamicCalendar with year,month,date,hour,minute,second");
181: }
182: }
183:
184: {
185: // setCivil/isCivil
186: // operations on non-civil calendar
187: IslamicCalendar cal = new IslamicCalendar(800,
188: IslamicCalendar.RAMADAN, 1, 1, 1, 1);
189: cal.setCivil(false);
190: if (cal.isCivil()) {
191: errln("islamic calendar is civil");
192: }
193:
194: Date now = new Date();
195: cal.setTime(now);
196:
197: Date then = cal.getTime();
198: if (!now.equals(then)) {
199: errln("get/set time failed with non-civil islamic calendar");
200: }
201:
202: logln(then.toString());
203:
204: cal.add(Calendar.MONTH, 1);
205: cal.add(Calendar.DAY_OF_MONTH, 1);
206: cal.add(Calendar.YEAR, 1);
207:
208: logln(cal.getTime().toString());
209: }
210:
211: {
212: // data
213: IslamicCalendar cal = new IslamicCalendar(800,
214: IslamicCalendar.RAMADAN, 1);
215: Date time = cal.getTime();
216:
217: String[] calendarLocales = { "ar_AE", "ar_BH", "ar_DZ",
218: "ar_EG", "ar_JO", "ar_KW", "ar_OM", "ar_QA",
219: "ar_SA", "ar_SY", "ar_YE", "ms_MY" };
220:
221: String[] formatLocales = { "en", "ar", "fi", "fr", "hu",
222: "iw", "nl" };
223: for (int i = 0; i < calendarLocales.length; ++i) {
224: String calLocName = calendarLocales[i];
225: Locale calLocale = LocaleUtility
226: .getLocaleFromName(calLocName);
227: cal = new IslamicCalendar(calLocale);
228:
229: for (int j = 0; j < formatLocales.length; ++j) {
230: String locName = formatLocales[j];
231: Locale formatLocale = LocaleUtility
232: .getLocaleFromName(locName);
233: DateFormat format = DateFormat.getDateTimeInstance(
234: cal, DateFormat.FULL, DateFormat.FULL,
235: formatLocale);
236: logln(calLocName + "/" + locName + " --> "
237: + format.format(time));
238: }
239: }
240: }
241: }
242:
243: private static IslamicCalendar newCivil() {
244: IslamicCalendar civilCalendar = new IslamicCalendar();
245: civilCalendar.setCivil(true);
246: return civilCalendar;
247: }
248:
249: }
|