1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 1997-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 ********************************************************************/
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/dtfmtsym.h"
17 //--------------------------------------------------------------------
18 // Time bomb - allows temporary behavior that expires at a given
20 //--------------------------------------------------------------------
22 void IntlTestDateFormatSymbols::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
24 if (exec) logln("TestSuite DateFormatSymbols");
26 TESTCASE(0,TestSymbols);
27 TESTCASE(1,TestGetMonths);
28 TESTCASE(2,TestGetMonths2);
29 TESTCASE(3,TestGetWeekdays2);
30 TESTCASE(4,TestGetEraNames);
31 TESTCASE(5,TestGetSetSpecificItems);
32 default: name = ""; break;
39 void IntlTestDateFormatSymbols::TestGetMonths()
41 UErrorCode status = U_ZERO_ERROR;
43 const UnicodeString* month;
44 DateFormatSymbols *symbol;
46 symbol=new DateFormatSymbols(Locale::getDefault(), status);
48 month=symbol->getMonths(cnt);
50 logln((UnicodeString)"size = " + cnt);
52 for (int32_t i=0; i<cnt; ++i)
60 void IntlTestDateFormatSymbols::TestGetMonths2()
62 UErrorCode status = U_ZERO_ERROR;
63 DateFormatSymbols *symbol;
65 symbol=new DateFormatSymbols(Locale::getDefault(), status);
67 DateFormatSymbols::DtContextType context[] = {DateFormatSymbols::STANDALONE, DateFormatSymbols::FORMAT};
68 DateFormatSymbols::DtWidthType width[] = {DateFormatSymbols::WIDE, DateFormatSymbols::ABBREVIATED, DateFormatSymbols::NARROW};
70 for (int32_t i = 0; i < 2; i++) {
71 for (int32_t j = 0; j < 3; j++) {
73 const UnicodeString * month = symbol->getMonths(cnt,context[i],width[j]);
75 logln((UnicodeString)"size = " + cnt);
77 for (int32_t k = 0; k < cnt; k++) {
85 void IntlTestDateFormatSymbols::TestGetWeekdays2()
87 UErrorCode status = U_ZERO_ERROR;
88 DateFormatSymbols *symbol;
90 symbol=new DateFormatSymbols(Locale::getDefault(), status);
92 DateFormatSymbols::DtContextType context[] = {DateFormatSymbols::STANDALONE, DateFormatSymbols::FORMAT};
93 DateFormatSymbols::DtWidthType width[] = {DateFormatSymbols::WIDE, DateFormatSymbols::ABBREVIATED, DateFormatSymbols::NARROW};
95 for (int32_t i = 0; i < 2; i++) {
96 for (int32_t j = 0; j < 3; j++) {
98 const UnicodeString * wd = symbol->getWeekdays(cnt,context[i],width[j]);
100 logln((UnicodeString)"size = " + cnt);
102 for (int32_t k = 0; k < cnt; k++) {
111 void IntlTestDateFormatSymbols::TestGetEraNames()
113 UErrorCode status = U_ZERO_ERROR;
115 const UnicodeString* name;
116 DateFormatSymbols *symbol;
118 symbol=new DateFormatSymbols(Locale::getDefault(), status);
120 name=symbol->getEraNames(cnt);
122 logln((UnicodeString)"size = " + cnt);
124 for (int32_t i=0; i<cnt; ++i)
132 UBool IntlTestDateFormatSymbols::UnicodeStringsArePrefixes(int32_t count, int32_t prefixLen, const UnicodeString *prefixArray, const UnicodeString *baseArray)
135 for (i = 0; i < count; i++) {
136 if (baseArray[i].compare(0, prefixLen, prefixArray[i]) != 0) {
137 errln("ERROR: Mismatch example: expect prefix \"" + prefixArray[i] + "\" of base \"" + baseArray[i] + "\".");
144 void IntlTestDateFormatSymbols::TestGetSetSpecificItems()
146 UErrorCode status = U_ZERO_ERROR;
147 DateFormatSymbols *symbol=new DateFormatSymbols(Locale::getEnglish(), status);
148 if(U_FAILURE(status)) {
149 dataerrln("ERROR: Couldn't create English DateFormatSymbols " + (UnicodeString)u_errorName(status));
152 int32_t cntFmtAbbrev, cntFmtShort, cntStdAloneShort;
153 const UnicodeString * wdFmtAbbrev = symbol->getWeekdays(cntFmtAbbrev,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
154 const UnicodeString * wdFmtShort = symbol->getWeekdays(cntFmtShort,DateFormatSymbols::FORMAT,DateFormatSymbols::SHORT);
155 const UnicodeString * wdStdAloneShort = symbol->getWeekdays(cntStdAloneShort,DateFormatSymbols::STANDALONE,DateFormatSymbols::SHORT);
156 // Expect that English short names are prefixes of abbreviated names
157 if (cntFmtShort != cntFmtAbbrev || !UnicodeStringsArePrefixes(cntFmtAbbrev, 2, wdFmtShort, wdFmtAbbrev)) {
158 errln("ERROR: English format short weekday names don't match prefixes of format abbreviated names");
160 if (cntStdAloneShort != cntFmtAbbrev || !UnicodeStringsArePrefixes(cntFmtAbbrev, 2, wdStdAloneShort, wdFmtAbbrev)) {
161 errln("ERROR: English standalone short weekday names don't match prefixes of format abbreviated names");
168 * Test the API of DateFormatSymbols; primarily a simple get/set set.
170 void IntlTestDateFormatSymbols::TestSymbols(/* char *par */)
172 UErrorCode status = U_ZERO_ERROR;
174 DateFormatSymbols fr(Locale::getFrench(), status);
175 if(U_FAILURE(status)) {
176 dataerrln("ERROR: Couldn't create French DateFormatSymbols " + (UnicodeString)u_errorName(status));
180 status = U_ZERO_ERROR;
181 DateFormatSymbols fr2(Locale::getFrench(), status);
182 if(U_FAILURE(status)) {
183 errcheckln(status, "ERROR: Couldn't create French DateFormatSymbols " + (UnicodeString)u_errorName(status));
187 status = U_ZERO_ERROR;
188 DateFormatSymbols en(Locale::getEnglish(), status);
189 if(U_FAILURE(status)) {
190 errcheckln(status, "ERROR: Couldn't create English DateFormatSymbols " + (UnicodeString)u_errorName(status));
194 if(en == fr || ! (en != fr) ) {
195 errln("ERROR: English DateFormatSymbols equal to French");
198 // just do some VERY basic tests to make sure that get/set work
201 const UnicodeString *eras = en.getEras(count);
203 errln("ERROR: 0 english eras.. exiting..\n");
206 int32_t eraNamesCount = 0;
207 const UnicodeString *eraNames = en.getEraNames(eraNamesCount);
208 if(eraNamesCount == 0) {
209 errln("ERROR: 0 english eraNames\n");
210 } else if ( eraNames[0].length() <= eras[0].length() ) {
211 // At least for English we know a wide eraName should be longer than an abbrev era
212 errln("ERROR: english eraNames[0] not longer than eras[0]\n");
214 int32_t narrowErasCount = 0;
215 const UnicodeString *narrowEras = en.getNarrowEras(narrowErasCount);
216 if(narrowErasCount == 0) {
217 errln("ERROR: 0 english narrowEras\n");
218 } else if ( narrowEras[0].length() >= eras[0].length() ) {
219 // At least for English we know a narrowEra should be shorter than an abbrev era
220 errln("ERROR: english narrowEras[0] not shorter than eras[0]\n");
223 fr.setEras(eras, count);
224 if( *en.getEras(count) != *fr.getEras(count)) {
225 errln("ERROR: setEras() failed");
228 const UnicodeString *months = en.getMonths(count);
229 fr.setMonths(months, count);
230 if( *en.getMonths(count) != *fr.getMonths(count)) {
231 errln("ERROR: setMonths() failed");
234 const UnicodeString *shortMonths = en.getShortMonths(count);
235 fr.setShortMonths(shortMonths, count);
236 if( *en.getShortMonths(count) != *fr.getShortMonths(count)) {
237 errln("ERROR: setShortMonths() failed");
240 const UnicodeString *wideMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
241 fr2.setMonths(wideMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
242 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE) !=
243 *fr2.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE )) {
244 errln("ERROR: setMonths(FORMAT,WIDE) failed");
247 const UnicodeString *abbrMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
248 fr2.setMonths(abbrMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
249 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED) !=
250 *fr2.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED )) {
251 errln("ERROR: setMonths(FORMAT,ABBREVIATED) failed");
254 const UnicodeString *narrowMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
255 fr.setMonths(narrowMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
256 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW) !=
257 *fr.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW )) {
258 errln("ERROR: setMonths(FORMAT,NARROW) failed");
261 const UnicodeString *standaloneWideMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
262 fr.setMonths(standaloneWideMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
263 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE) !=
264 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE )) {
265 errln("ERROR: setMonths(STANDALONE,WIDE) failed");
268 const UnicodeString *standaloneShortMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
269 fr.setMonths(standaloneShortMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
270 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED) !=
271 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED )) {
272 errln("ERROR: setMonths(STANDALONE,ABBREVIATED) failed");
275 const UnicodeString *standaloneNarrowMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
276 fr.setMonths(standaloneNarrowMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
277 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW) !=
278 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW )) {
279 errln("ERROR: setMonths(STANDALONE,NARROW) failed");
282 const UnicodeString *weekdays = en.getWeekdays(count);
283 fr.setWeekdays(weekdays, count);
284 if( *en.getWeekdays(count) != *fr.getWeekdays(count)) {
285 errln("ERROR: setWeekdays() failed");
288 const UnicodeString *shortWeekdays = en.getShortWeekdays(count);
289 fr.setShortWeekdays(shortWeekdays, count);
290 if( *en.getShortWeekdays(count) != *fr.getShortWeekdays(count)) {
291 errln("ERROR: setShortWeekdays() failed");
294 const UnicodeString *wideWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
295 fr2.setWeekdays(wideWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
296 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE) !=
297 *fr2.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE )) {
298 errln("ERROR: setWeekdays(FORMAT,WIDE) failed");
301 const UnicodeString *abbrWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
302 fr2.setWeekdays(abbrWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
303 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED) !=
304 *fr2.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED )) {
305 errln("ERROR: setWeekdays(FORMAT,ABBREVIATED) failed");
308 const UnicodeString *narrowWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
309 fr.setWeekdays(narrowWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
310 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW) !=
311 *fr.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW )) {
312 errln("ERROR: setWeekdays(FORMAT,NARROW) failed");
315 const UnicodeString *standaloneWideWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
316 fr.setWeekdays(standaloneWideWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
317 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE) !=
318 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE )) {
319 errln("ERROR: setWeekdays(STANDALONE,WIDE) failed");
322 const UnicodeString *standaloneShortWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
323 fr.setWeekdays(standaloneShortWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
324 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED) !=
325 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED )) {
326 errln("ERROR: setWeekdays(STANDALONE,ABBREVIATED) failed");
329 const UnicodeString *standaloneNarrowWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
330 fr.setWeekdays(standaloneNarrowWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
331 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW) !=
332 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW )) {
333 errln("ERROR: setWeekdays(STANDALONE,NARROW) failed");
336 const UnicodeString *wideQuarters = en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
337 fr2.setQuarters(wideQuarters, count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
338 if( *en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE) !=
339 *fr2.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE )) {
340 errln("ERROR: setQuarters(FORMAT, WIDE) failed");
343 const UnicodeString *abbrQuarters = en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
344 fr2.setQuarters(abbrQuarters, count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
345 if( *en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED) !=
346 *fr2.getQuarters(count,DateFormatSymbols::FORMAT ,DateFormatSymbols::ABBREVIATED )) {
347 errln("ERROR: setQuarters(FORMAT, ABBREVIATED) failed");
350 const UnicodeString *standaloneWideQuarters = en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
351 fr.setQuarters(standaloneWideQuarters, count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
352 if( *en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE) !=
353 *fr.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE )) {
354 errln("ERROR: setQuarters(STANDALONE, WIDE) failed");
357 const UnicodeString *standaloneShortQuarters = en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
358 fr.setQuarters(standaloneShortQuarters, count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
359 if( *en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED) !=
360 *fr.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED )) {
361 errln("ERROR: setQuarters(STANDALONE, ABBREVIATED) failed");
364 const UnicodeString *ampms = en.getAmPmStrings(count);
365 fr.setAmPmStrings(ampms, count);
366 if( *en.getAmPmStrings(count) != *fr.getAmPmStrings(count)) {
367 errln("ERROR: setAmPmStrings() failed");
370 int32_t rowCount = 0, columnCount = 0;
371 const UnicodeString **strings = en.getZoneStrings(rowCount, columnCount);
372 fr.setZoneStrings(strings, rowCount, columnCount);
373 const UnicodeString **strings1 = fr.getZoneStrings(rowCount, columnCount);
374 for(int32_t i = 0; i < rowCount; i++) {
375 for(int32_t j = 0; j < columnCount; j++) {
376 if( strings[i][j] != strings1[i][j] ) {
377 errln("ERROR: setZoneStrings() failed");
382 UnicodeString localPattern, pat1, pat2;
383 localPattern = en.getLocalPatternChars(localPattern);
384 fr.setLocalPatternChars(localPattern);
385 if( en.getLocalPatternChars(pat1) != fr.getLocalPatternChars(pat2)) {
386 errln("ERROR: setLocalPatternChars() failed");
390 status = U_ZERO_ERROR;
391 DateFormatSymbols foo(status);
392 DateFormatSymbols bar(foo);
397 errln("ERROR: Assignment operator failed");
400 errln("ERROR: Copy Constructor failed");
404 #endif /* #if !UCONFIG_NO_FORMATTING */