1/*
2 *******************************************************************************
3 * Copyright (C) 2012, International Business Machines Corporation and         *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.dev.test.calendar;
8import java.util.Date;
9
10import com.ibm.icu.util.Calendar;
11import com.ibm.icu.util.ULocale;
12
13public class PersianTest extends CalendarTest {
14
15    public static void main(String args[]) throws Exception {
16        new PersianTest().run(args);
17    }
18
19    /**
20     * Test basic mapping to and from Gregorian.
21     */
22    public void TestMapping() {
23        final int[] DATA = {
24            // (Note: months are 1-based)
25            2011, 1, 11, 1389, 10, 21,
26            1986, 2, 25, 1364, 12, 6,
27            1934, 3, 14, 1312, 12, 23,
28
29            2090, 3, 19, 1468, 12, 29,
30            2007, 2, 22, 1385, 12, 3,
31            1969, 12, 31, 1348, 10, 10,
32            1945, 11, 12, 1324, 8, 21,
33            1925, 3, 31, 1304, 1, 11,
34
35            1996, 3, 19, 1374, 12, 29,
36            1996, 3, 20, 1375, 1, 1,
37            1997, 3, 20, 1375, 12, 30,
38            1997, 3, 21, 1376, 1, 1,
39
40            2008, 3, 19, 1386, 12, 29,
41            2008, 3, 20, 1387, 1, 1,
42            2004, 3, 19, 1382, 12, 29,
43            2004, 3, 20, 1383, 1, 1,
44
45            2006, 3, 20, 1384, 12, 29,
46            2006, 3, 21, 1385, 1, 1,
47
48            2005, 4, 20, 1384, 1, 31,
49            2005, 4, 21, 1384, 2, 1,
50            2005, 5, 21, 1384, 2, 31,
51            2005, 5, 22, 1384, 3, 1,
52            2005, 6, 21, 1384, 3, 31,
53            2005, 6, 22, 1384, 4, 1,
54            2005, 7, 22, 1384, 4, 31,
55            2005, 7, 23, 1384, 5, 1,
56            2005, 8, 22, 1384, 5, 31,
57            2005, 8, 23, 1384, 6, 1,
58            2005, 9, 22, 1384, 6, 31,
59            2005, 9, 23, 1384, 7, 1,
60            2005, 10, 22, 1384, 7, 30,
61            2005, 10, 23, 1384, 8, 1,
62            2005, 11, 21, 1384, 8, 30,
63            2005, 11, 22, 1384, 9, 1,
64            2005, 12, 21, 1384, 9, 30,
65            2005, 12, 22, 1384, 10, 1,
66            2006, 1, 20, 1384, 10, 30,
67            2006, 1, 21, 1384, 11, 1,
68            2006, 2, 19, 1384, 11, 30,
69            2006, 2, 20, 1384, 12, 1,
70            2006, 3, 20, 1384, 12, 29,
71            2006, 3, 21, 1385, 1, 1,
72
73            // The 2820-year cycle arithmetical algorithm would fail this one.
74            2025, 3, 21, 1404, 1, 1,
75        };
76
77        Calendar cal = Calendar.getInstance(new ULocale("fa_IR@calendar=persian"));
78        StringBuilder buf = new StringBuilder();
79
80        logln("Gregorian -> Persian");
81
82        Calendar grego = Calendar.getInstance();
83        grego.clear();
84        for (int i = 0; i < DATA.length;) {
85            grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
86            Date date = grego.getTime();
87            cal.setTime(date);
88            int y = cal.get(Calendar.YEAR);
89            int m = cal.get(Calendar.MONTH) + 1; // 0-based -> 1-based
90            int d = cal.get(Calendar.DAY_OF_MONTH);
91            int yE = DATA[i++]; // Expected y, m, d
92            int mE = DATA[i++]; // 1-based
93            int dE = DATA[i++];
94            buf.setLength(0);
95            buf.append(date + " -> ");
96            buf.append(y + "/" + m + "/" + d);
97            if (y == yE && m == mE && d == dE) {
98                logln("OK: " + buf.toString());
99            } else {
100                errln("Fail: " + buf.toString() + ", expected " + yE + "/" + mE + "/" + dE);
101            }
102        }
103
104        logln("Persian -> Gregorian");
105        for (int i = 0; i < DATA.length;) {
106            grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
107            Date dexp = grego.getTime();
108            int cyear = DATA[i++];
109            int cmonth = DATA[i++];
110            int cdayofmonth = DATA[i++];
111            cal.clear();
112            cal.set(Calendar.YEAR, cyear);
113            cal.set(Calendar.MONTH, cmonth - 1);
114            cal.set(Calendar.DAY_OF_MONTH, cdayofmonth);
115            Date date = cal.getTime();
116            buf.setLength(0);
117            buf.append(cyear + "/" + cmonth + "/" + cdayofmonth);
118            buf.append(" -> " + date);
119            if (date.equals(dexp)) {
120                logln("OK: " + buf.toString());
121            } else {
122                errln("Fail: " + buf.toString() + ", expected " + dexp);
123            }
124        }
125    }
126}
127