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 ********************************************************************/
7
8 #include "unicode/utypes.h"
9
10 #if !UCONFIG_NO_FORMATTING
11
12 #include "tsdtfmsy.h"
13
14 #include "unicode/dtfmtsym.h"
15
16
17 //--------------------------------------------------------------------
18 // Time bomb - allows temporary behavior that expires at a given
19 // release
20 //--------------------------------------------------------------------
21
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)22 void IntlTestDateFormatSymbols::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
23 {
24 if (exec) logln("TestSuite DateFormatSymbols");
25 switch (index) {
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;
33 }
34 }
35
36 /**
37 * Test getMonths.
38 */
TestGetMonths()39 void IntlTestDateFormatSymbols::TestGetMonths()
40 {
41 UErrorCode status = U_ZERO_ERROR;
42 int32_t cnt;
43 const UnicodeString* month;
44 DateFormatSymbols *symbol;
45
46 symbol=new DateFormatSymbols(Locale::getDefault(), status);
47
48 month=symbol->getMonths(cnt);
49
50 logln((UnicodeString)"size = " + cnt);
51
52 for (int32_t i=0; i<cnt; ++i)
53 {
54 logln(month[i]);
55 }
56
57 delete symbol;
58 }
59
TestGetMonths2()60 void IntlTestDateFormatSymbols::TestGetMonths2()
61 {
62 UErrorCode status = U_ZERO_ERROR;
63 DateFormatSymbols *symbol;
64
65 symbol=new DateFormatSymbols(Locale::getDefault(), status);
66
67 DateFormatSymbols::DtContextType context[] = {DateFormatSymbols::STANDALONE, DateFormatSymbols::FORMAT};
68 DateFormatSymbols::DtWidthType width[] = {DateFormatSymbols::WIDE, DateFormatSymbols::ABBREVIATED, DateFormatSymbols::NARROW};
69
70 for (int32_t i = 0; i < 2; i++) {
71 for (int32_t j = 0; j < 3; j++) {
72 int32_t cnt;
73 const UnicodeString * month = symbol->getMonths(cnt,context[i],width[j]);
74
75 logln((UnicodeString)"size = " + cnt);
76
77 for (int32_t k = 0; k < cnt; k++) {
78 logln(month[k]);
79 }
80 }
81 }
82 delete symbol;
83 }
84
TestGetWeekdays2()85 void IntlTestDateFormatSymbols::TestGetWeekdays2()
86 {
87 UErrorCode status = U_ZERO_ERROR;
88 DateFormatSymbols *symbol;
89
90 symbol=new DateFormatSymbols(Locale::getDefault(), status);
91
92 DateFormatSymbols::DtContextType context[] = {DateFormatSymbols::STANDALONE, DateFormatSymbols::FORMAT};
93 DateFormatSymbols::DtWidthType width[] = {DateFormatSymbols::WIDE, DateFormatSymbols::ABBREVIATED, DateFormatSymbols::NARROW};
94
95 for (int32_t i = 0; i < 2; i++) {
96 for (int32_t j = 0; j < 3; j++) {
97 int32_t cnt;
98 const UnicodeString * wd = symbol->getWeekdays(cnt,context[i],width[j]);
99
100 logln((UnicodeString)"size = " + cnt);
101
102 for (int32_t k = 0; k < cnt; k++) {
103 logln(wd[k]);
104 }
105 }
106 }
107 delete symbol;
108 }
109
110
TestGetEraNames()111 void IntlTestDateFormatSymbols::TestGetEraNames()
112 {
113 UErrorCode status = U_ZERO_ERROR;
114 int32_t cnt;
115 const UnicodeString* name;
116 DateFormatSymbols *symbol;
117
118 symbol=new DateFormatSymbols(Locale::getDefault(), status);
119
120 name=symbol->getEraNames(cnt);
121
122 logln((UnicodeString)"size = " + cnt);
123
124 for (int32_t i=0; i<cnt; ++i)
125 {
126 logln(name[i]);
127 }
128
129 delete symbol;
130 }
131
UnicodeStringsArePrefixes(int32_t count,int32_t prefixLen,const UnicodeString * prefixArray,const UnicodeString * baseArray)132 UBool IntlTestDateFormatSymbols::UnicodeStringsArePrefixes(int32_t count, int32_t prefixLen, const UnicodeString *prefixArray, const UnicodeString *baseArray)
133 {
134 int32_t i;
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] + "\".");
138 return FALSE;
139 }
140 }
141 return TRUE;
142 }
143
TestGetSetSpecificItems()144 void IntlTestDateFormatSymbols::TestGetSetSpecificItems()
145 {
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));
150 return;
151 }
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");
159 }
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");
162 }
163
164 delete symbol;
165 }
166
167 /**
168 * Test the API of DateFormatSymbols; primarily a simple get/set set.
169 */
TestSymbols()170 void IntlTestDateFormatSymbols::TestSymbols(/* char *par */)
171 {
172 UErrorCode status = U_ZERO_ERROR;
173
174 DateFormatSymbols fr(Locale::getFrench(), status);
175 if(U_FAILURE(status)) {
176 dataerrln("ERROR: Couldn't create French DateFormatSymbols " + (UnicodeString)u_errorName(status));
177 return;
178 }
179
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));
184 return;
185 }
186
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));
191 return;
192 }
193
194 if(en == fr || ! (en != fr) ) {
195 errln("ERROR: English DateFormatSymbols equal to French");
196 }
197
198 // just do some VERY basic tests to make sure that get/set work
199
200 int32_t count = 0;
201 const UnicodeString *eras = en.getEras(count);
202 if(count == 0) {
203 errln("ERROR: 0 english eras.. exiting..\n");
204 return;
205 }
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");
213 }
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");
221 }
222
223 fr.setEras(eras, count);
224 if( *en.getEras(count) != *fr.getEras(count)) {
225 errln("ERROR: setEras() failed");
226 }
227
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");
232 }
233
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");
238 }
239
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");
245 }
246
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");
252 }
253
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");
259 }
260
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");
266 }
267
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");
273 }
274
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");
280 }
281
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");
286 }
287
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");
292 }
293
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");
299 }
300
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");
306 }
307
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");
313 }
314
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");
320 }
321
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");
327 }
328
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");
334 }
335
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");
341 }
342
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");
348 }
349
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");
355 }
356
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");
362 }
363
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");
368 }
369
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");
378 }
379 }
380 }
381
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");
387 }
388
389
390 status = U_ZERO_ERROR;
391 DateFormatSymbols foo(status);
392 DateFormatSymbols bar(foo);
393
394 en = fr;
395
396 if(en != fr) {
397 errln("ERROR: Assignment operator failed");
398 }
399 if(foo != bar) {
400 errln("ERROR: Copy Constructor failed");
401 }
402 }
403
404 #endif /* #if !UCONFIG_NO_FORMATTING */
405