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