1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2012-2016, International Business Machines Corporation and 7 * others. All Rights Reserved. 8 ******************************************************************************* 9 */ 10 package android.icu.dev.test.calendar; 11 import java.util.Date; 12 13 import org.junit.Test; 14 import org.junit.runner.RunWith; 15 import org.junit.runners.JUnit4; 16 17 import android.icu.util.Calendar; 18 import android.icu.util.PersianCalendar; 19 import android.icu.util.ULocale; 20 import android.icu.testsharding.MainTestShard; 21 22 @MainTestShard 23 @RunWith(JUnit4.class) 24 public class PersianTest extends CalendarTestFmwk { 25 /** 26 * Test basic mapping to and from Gregorian. 27 */ 28 @Test 29 public void TestMapping() { 30 final int[] DATA = { 31 // (Note: months are 1-based) 32 2011, 1, 11, 1389, 10, 21, 33 1986, 2, 25, 1364, 12, 6, 34 1934, 3, 14, 1312, 12, 23, 35 36 2090, 3, 19, 1468, 12, 29, 37 2007, 2, 22, 1385, 12, 3, 38 1969, 12, 31, 1348, 10, 10, 39 1945, 11, 12, 1324, 8, 21, 40 1925, 3, 31, 1304, 1, 11, 41 42 1996, 3, 19, 1374, 12, 29, 43 1996, 3, 20, 1375, 1, 1, 44 1997, 3, 20, 1375, 12, 30, 45 1997, 3, 21, 1376, 1, 1, 46 47 2008, 3, 19, 1386, 12, 29, 48 2008, 3, 20, 1387, 1, 1, 49 2004, 3, 19, 1382, 12, 29, 50 2004, 3, 20, 1383, 1, 1, 51 52 2006, 3, 20, 1384, 12, 29, 53 2006, 3, 21, 1385, 1, 1, 54 55 2005, 4, 20, 1384, 1, 31, 56 2005, 4, 21, 1384, 2, 1, 57 2005, 5, 21, 1384, 2, 31, 58 2005, 5, 22, 1384, 3, 1, 59 2005, 6, 21, 1384, 3, 31, 60 2005, 6, 22, 1384, 4, 1, 61 2005, 7, 22, 1384, 4, 31, 62 2005, 7, 23, 1384, 5, 1, 63 2005, 8, 22, 1384, 5, 31, 64 2005, 8, 23, 1384, 6, 1, 65 2005, 9, 22, 1384, 6, 31, 66 2005, 9, 23, 1384, 7, 1, 67 2005, 10, 22, 1384, 7, 30, 68 2005, 10, 23, 1384, 8, 1, 69 2005, 11, 21, 1384, 8, 30, 70 2005, 11, 22, 1384, 9, 1, 71 2005, 12, 21, 1384, 9, 30, 72 2005, 12, 22, 1384, 10, 1, 73 2006, 1, 20, 1384, 10, 30, 74 2006, 1, 21, 1384, 11, 1, 75 2006, 2, 19, 1384, 11, 30, 76 2006, 2, 20, 1384, 12, 1, 77 2006, 3, 20, 1384, 12, 29, 78 2006, 3, 21, 1385, 1, 1, 79 80 // The 2820-year cycle arithmetical algorithm would fail this one. 81 2025, 3, 21, 1404, 1, 1, 82 }; 83 84 Calendar cal = Calendar.getInstance(new ULocale("fa_IR@calendar=persian")); 85 StringBuilder buf = new StringBuilder(); 86 87 logln("Gregorian -> Persian"); 88 89 Calendar grego = Calendar.getInstance(); 90 grego.clear(); 91 for (int i = 0; i < DATA.length;) { 92 grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]); 93 Date date = grego.getTime(); 94 cal.setTime(date); 95 int y = cal.get(Calendar.YEAR); 96 int m = cal.get(Calendar.MONTH) + 1; // 0-based -> 1-based 97 int d = cal.get(Calendar.DAY_OF_MONTH); 98 int yE = DATA[i++]; // Expected y, m, d 99 int mE = DATA[i++]; // 1-based 100 int dE = DATA[i++]; 101 buf.setLength(0); 102 buf.append(date + " -> "); 103 buf.append(y + "/" + m + "/" + d); 104 if (y == yE && m == mE && d == dE) { 105 logln("OK: " + buf.toString()); 106 } else { 107 errln("Fail: " + buf.toString() + ", expected " + yE + "/" + mE + "/" + dE); 108 } 109 } 110 111 logln("Persian -> Gregorian"); 112 for (int i = 0; i < DATA.length;) { 113 grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]); 114 Date dexp = grego.getTime(); 115 int cyear = DATA[i++]; 116 int cmonth = DATA[i++]; 117 int cdayofmonth = DATA[i++]; 118 cal.clear(); 119 cal.set(Calendar.YEAR, cyear); 120 cal.set(Calendar.MONTH, cmonth - 1); 121 cal.set(Calendar.DAY_OF_MONTH, cdayofmonth); 122 Date date = cal.getTime(); 123 buf.setLength(0); 124 buf.append(cyear + "/" + cmonth + "/" + cdayofmonth); 125 buf.append(" -> " + date); 126 if (date.equals(dexp)) { 127 logln("OK: " + buf.toString()); 128 } else { 129 errln("Fail: " + buf.toString() + ", expected " + dexp); 130 } 131 } 132 } 133 134 @Test 135 public void TestCoverage12424() { 136 class StubCalendar extends PersianCalendar { 137 private static final long serialVersionUID = 1L; 138 public StubCalendar() { 139 assertEquals("Persian month 0 length", 31, handleGetMonthLength(1000, 0)); 140 assertEquals("Persian month 7 length", 30, handleGetMonthLength(1000, 7)); 141 142 int leastWeeks = handleGetLimit(Calendar.WEEK_OF_YEAR, Calendar.LEAST_MAXIMUM); 143 assertEquals("Persian Week of Year least maximum", 52, leastWeeks); 144 } 145 } 146 147 new StubCalendar(); 148 } 149 } 150