]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/erarulestest.cpp
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
8 #include "unicode/calendar.h"
9 #include "unicode/localpointer.h"
10 #include "unicode/unistr.h"
11 #include "unicode/timezone.h"
13 #include "erarulestest.h"
15 void EraRulesTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
18 logln("TestSuite EraRulesTest");
21 TESTCASE_AUTO(testAPIs
);
22 TESTCASE_AUTO(testJapanese
);
26 void EraRulesTest::testAPIs() {
27 const char * calTypes
[] = {
35 "ethiopic-amete-alem",
50 for (int32_t i
= 0; calTypes
[i
] != NULL
; i
++) {
51 UErrorCode status
= U_ZERO_ERROR
;
52 const char *calId
= calTypes
[i
];
54 LocalPointer
<EraRules
> rules1(EraRules::createInstance(calId
, FALSE
, status
));
55 if (U_FAILURE(status
)) {
56 errln(UnicodeString("Era rules for ") + calId
+ " is not available.");
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.");
66 int32_t numEras1
= rules1
->getNumberOfEras();
68 errln(UnicodeString("Number of era rules for ") + calId
+ " is " + numEras1
);
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: ")
77 LocalPointer
<Calendar
> cal(Calendar::createInstance(*TimeZone::getGMT(), "en", status
));
78 if (U_FAILURE(status
)) {
79 errln("Failed to create a Calendar instance.");
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.");
91 if (idx
!= currentIdx
) {
92 errln(UnicodeString("Current era index:") + currentIdx
+ " is different from era index of now:" + idx
93 + " in calendar:" + calId
);
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
);
100 if (currentYear
< eraStartYear
) {
101 errln(UnicodeString("Current era's start year is after the current year in calendar:") + calId
);
106 void EraRulesTest::testJapanese() {
107 const int32_t HEISEI
= 235; // ICU4C does not define constants for eras
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.");
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.");
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.");
125 if (postHeiseiStartYear
!= 2019) {
126 errln(UnicodeString("Era after Heisei should start in 2019, but got ") + postHeiseiStartYear
);
130 #endif /* #if !UCONFIG_NO_FORMATTING */