• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 
8 #include "unicode/calendar.h"
9 #include "unicode/localpointer.h"
10 #include "unicode/unistr.h"
11 #include "unicode/timezone.h"
12 #include "erarules.h"
13 #include "erarulestest.h"
14 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)15 void EraRulesTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/)
16 {
17     if (exec) {
18         logln("TestSuite EraRulesTest");
19     }
20     TESTCASE_AUTO_BEGIN;
21     TESTCASE_AUTO(testAPIs);
22     TESTCASE_AUTO(testJapanese);
23     TESTCASE_AUTO_END;
24 }
25 
testAPIs()26 void EraRulesTest::testAPIs() {
27     const char * calTypes[] = {
28         "gregorian",
29         //"iso8601",
30         "buddhist",
31         "chinese",
32         "coptic",
33         "dangi",
34         "ethiopic",
35         "ethiopic-amete-alem",
36         "hebrew",
37         "indian",
38         "islamic",
39         "islamic-civil",
40         "islamic-rgsa",
41         "islamic-tbla",
42         "islamic-umalqura",
43         "japanese",
44         "persian",
45         "roc",
46         //"unknown",
47         NULL
48     };
49 
50     for (int32_t i = 0; calTypes[i] != NULL; i++) {
51         UErrorCode status = U_ZERO_ERROR;
52         const char *calId = calTypes[i];
53 
54         LocalPointer<EraRules> rules1(EraRules::createInstance(calId, false, status));
55         if (U_FAILURE(status)) {
56             errln(UnicodeString("Era rules for ") + calId + " is not available.");
57             continue;
58         }
59 
60         LocalPointer<EraRules> rules2(EraRules::createInstance(calId, true, status));
61         if (U_FAILURE(status)) {
62             errln(UnicodeString("Era rules for ") + calId + " (including tentative eras) is not available.");
63             continue;
64         }
65 
66         int32_t numEras1 = rules1->getNumberOfEras();
67         if (numEras1 <= 0) {
68             errln(UnicodeString("Number of era rules for ") + calId + " is " + numEras1);
69         }
70 
71         int32_t numEras2 = rules2->getNumberOfEras();
72         if (numEras2 < numEras1) {
73             errln(UnicodeString("Number of era including tentative eras is fewer than one without tentative eras in calendar: ")
74                     + calId);
75         }
76 
77         LocalPointer<Calendar> cal(Calendar::createInstance(*TimeZone::getGMT(), "en", status));
78         if (U_FAILURE(status)) {
79             errln("Failed to create a Calendar instance.");
80             continue;
81         }
82         int32_t currentIdx = rules1->getCurrentEraIndex();
83         int32_t currentYear = cal->get(UCAL_YEAR, status);
84         int32_t idx = rules1->getEraIndex(
85                 currentYear, cal->get(UCAL_MONTH, status) + 1,
86                 cal->get(UCAL_DATE, status), status);
87         if (U_FAILURE(status)) {
88             errln("Error while getting index of era.");
89             continue;
90         }
91         if (idx != currentIdx) {
92             errln(UnicodeString("Current era index:") + currentIdx + " is different from era index of now:" + idx
93                     + " in calendar:" + calId);
94         }
95 
96         int32_t eraStartYear = rules1->getStartYear(currentIdx, status);
97         if (U_FAILURE(status)) {
98             errln(UnicodeString("Failed to get the start year of era index: ") + currentIdx + " in calendar: " + calId);
99         }
100         if (currentYear < eraStartYear) {
101             errln(UnicodeString("Current era's start year is after the current year in calendar:") + calId);
102         }
103     }
104 }
105 
testJapanese()106 void EraRulesTest::testJapanese() {
107     const int32_t HEISEI = 235; // ICU4C does not define constants for eras
108 
109     UErrorCode status = U_ZERO_ERROR;
110     LocalPointer<EraRules> rules(EraRules::createInstance("japanese", true, status));
111     if (U_FAILURE(status)) {
112         errln("Failed to get era rules for Japanese calendar.");
113         return;
114     }
115     // Rules should have an era after Heisei
116     int32_t numRules = rules->getNumberOfEras();
117     if (numRules <= HEISEI) {
118         errln("Era after Heisei is not available.");
119         return;
120     }
121     int postHeiseiStartYear = rules->getStartYear(HEISEI + 1, status);
122     if (U_FAILURE(status)) {
123         errln("Failed to get the start year of era after Heisei.");
124     }
125     if (postHeiseiStartYear != 2019) {
126         errln(UnicodeString("Era after Heisei should start in 2019, but got ") + postHeiseiStartYear);
127     }
128 }
129 
130 #endif /* #if !UCONFIG_NO_FORMATTING */
131 
132