blob: 2c1a1832e7595b035ccc580355680b838e2a3a8c [file] [log] [blame]
Fredrik Roubert0596fae2017-04-18 21:34:02 +02001// © 2016 and later: Unicode, Inc. and others.
Fredrik Roubert64339d32016-10-21 19:43:16 +02002// License & terms of use: http://www.unicode.org/copyright.html
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -07003/********************************************************************
Craig Cornelius83933352013-06-14 14:20:29 -07004 * Copyright (c) 1997-2013, International Business Machines
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -07005 * 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
22void 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);
Craig Cornelius83933352013-06-14 14:20:29 -070031 TESTCASE(5,TestGetSetSpecificItems);
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -070032 default: name = ""; break;
33 }
34}
35
36/**
37 * Test getMonths.
38 */
39void 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
60void 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
85void 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
111void 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
Craig Cornelius83933352013-06-14 14:20:29 -0700132UBool 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
144void 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 }
Victor Chang92c98b52021-04-27 16:37:23 +0100152 symbol->operator=(*symbol); // self-assignment should be a no-op
Craig Cornelius83933352013-06-14 14:20:29 -0700153 int32_t cntFmtAbbrev, cntFmtShort, cntStdAloneShort;
154 const UnicodeString * wdFmtAbbrev = symbol->getWeekdays(cntFmtAbbrev,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
155 const UnicodeString * wdFmtShort = symbol->getWeekdays(cntFmtShort,DateFormatSymbols::FORMAT,DateFormatSymbols::SHORT);
156 const UnicodeString * wdStdAloneShort = symbol->getWeekdays(cntStdAloneShort,DateFormatSymbols::STANDALONE,DateFormatSymbols::SHORT);
157 // Expect that English short names are prefixes of abbreviated names
158 if (cntFmtShort != cntFmtAbbrev || !UnicodeStringsArePrefixes(cntFmtAbbrev, 2, wdFmtShort, wdFmtAbbrev)) {
159 errln("ERROR: English format short weekday names don't match prefixes of format abbreviated names");
160 }
161 if (cntStdAloneShort != cntFmtAbbrev || !UnicodeStringsArePrefixes(cntFmtAbbrev, 2, wdStdAloneShort, wdFmtAbbrev)) {
162 errln("ERROR: English standalone short weekday names don't match prefixes of format abbreviated names");
163 }
164
165 delete symbol;
166}
167
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700168/**
169 * Test the API of DateFormatSymbols; primarily a simple get/set set.
170 */
171void IntlTestDateFormatSymbols::TestSymbols(/* char *par */)
172{
173 UErrorCode status = U_ZERO_ERROR;
174
175 DateFormatSymbols fr(Locale::getFrench(), status);
176 if(U_FAILURE(status)) {
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700177 dataerrln("ERROR: Couldn't create French DateFormatSymbols " + (UnicodeString)u_errorName(status));
Claire Ho85bf2e22009-11-24 14:23:02 -0800178 return;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700179 }
180
181 status = U_ZERO_ERROR;
182 DateFormatSymbols fr2(Locale::getFrench(), status);
183 if(U_FAILURE(status)) {
Claire Ho85bf2e22009-11-24 14:23:02 -0800184 errcheckln(status, "ERROR: Couldn't create French DateFormatSymbols " + (UnicodeString)u_errorName(status));
185 return;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700186 }
187
188 status = U_ZERO_ERROR;
189 DateFormatSymbols en(Locale::getEnglish(), status);
190 if(U_FAILURE(status)) {
Claire Ho85bf2e22009-11-24 14:23:02 -0800191 errcheckln(status, "ERROR: Couldn't create English DateFormatSymbols " + (UnicodeString)u_errorName(status));
192 return;
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700193 }
194
195 if(en == fr || ! (en != fr) ) {
196 errln("ERROR: English DateFormatSymbols equal to French");
197 }
198
199 // just do some VERY basic tests to make sure that get/set work
200
201 int32_t count = 0;
202 const UnicodeString *eras = en.getEras(count);
203 if(count == 0) {
Claire Ho85bf2e22009-11-24 14:23:02 -0800204 errln("ERROR: 0 english eras.. exiting..\n");
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700205 return;
206 }
Claire Ho85bf2e22009-11-24 14:23:02 -0800207 int32_t eraNamesCount = 0;
208 const UnicodeString *eraNames = en.getEraNames(eraNamesCount);
209 if(eraNamesCount == 0) {
210 errln("ERROR: 0 english eraNames\n");
211 } else if ( eraNames[0].length() <= eras[0].length() ) {
212 // At least for English we know a wide eraName should be longer than an abbrev era
213 errln("ERROR: english eraNames[0] not longer than eras[0]\n");
214 }
215 int32_t narrowErasCount = 0;
216 const UnicodeString *narrowEras = en.getNarrowEras(narrowErasCount);
217 if(narrowErasCount == 0) {
218 errln("ERROR: 0 english narrowEras\n");
219 } else if ( narrowEras[0].length() >= eras[0].length() ) {
220 // At least for English we know a narrowEra should be shorter than an abbrev era
221 errln("ERROR: english narrowEras[0] not shorter than eras[0]\n");
222 }
Jean-Baptiste Queruac04d0b2009-07-17 17:11:19 -0700223
224 fr.setEras(eras, count);
225 if( *en.getEras(count) != *fr.getEras(count)) {
226 errln("ERROR: setEras() failed");
227 }
228
229 const UnicodeString *months = en.getMonths(count);
230 fr.setMonths(months, count);
231 if( *en.getMonths(count) != *fr.getMonths(count)) {
232 errln("ERROR: setMonths() failed");
233 }
234
235 const UnicodeString *shortMonths = en.getShortMonths(count);
236 fr.setShortMonths(shortMonths, count);
237 if( *en.getShortMonths(count) != *fr.getShortMonths(count)) {
238 errln("ERROR: setShortMonths() failed");
239 }
240
241 const UnicodeString *wideMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
242 fr2.setMonths(wideMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
243 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE) !=
244 *fr2.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE )) {
245 errln("ERROR: setMonths(FORMAT,WIDE) failed");
246 }
247
248 const UnicodeString *abbrMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
249 fr2.setMonths(abbrMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
250 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED) !=
251 *fr2.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED )) {
252 errln("ERROR: setMonths(FORMAT,ABBREVIATED) failed");
253 }
254
255 const UnicodeString *narrowMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
256 fr.setMonths(narrowMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
257 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW) !=
258 *fr.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW )) {
259 errln("ERROR: setMonths(FORMAT,NARROW) failed");
260 }
261
262 const UnicodeString *standaloneWideMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
263 fr.setMonths(standaloneWideMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
264 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE) !=
265 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE )) {
266 errln("ERROR: setMonths(STANDALONE,WIDE) failed");
267 }
268
269 const UnicodeString *standaloneShortMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
270 fr.setMonths(standaloneShortMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
271 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED) !=
272 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED )) {
273 errln("ERROR: setMonths(STANDALONE,ABBREVIATED) failed");
274 }
275
276 const UnicodeString *standaloneNarrowMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
277 fr.setMonths(standaloneNarrowMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
278 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW) !=
279 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW )) {
280 errln("ERROR: setMonths(STANDALONE,NARROW) failed");
281 }
282
283 const UnicodeString *weekdays = en.getWeekdays(count);
284 fr.setWeekdays(weekdays, count);
285 if( *en.getWeekdays(count) != *fr.getWeekdays(count)) {
286 errln("ERROR: setWeekdays() failed");
287 }
288
289 const UnicodeString *shortWeekdays = en.getShortWeekdays(count);
290 fr.setShortWeekdays(shortWeekdays, count);
291 if( *en.getShortWeekdays(count) != *fr.getShortWeekdays(count)) {
292 errln("ERROR: setShortWeekdays() failed");
293 }
294
295 const UnicodeString *wideWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
296 fr2.setWeekdays(wideWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE);
297 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE) !=
298 *fr2.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE )) {
299 errln("ERROR: setWeekdays(FORMAT,WIDE) failed");
300 }
301
302 const UnicodeString *abbrWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
303 fr2.setWeekdays(abbrWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
304 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED) !=
305 *fr2.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED )) {
306 errln("ERROR: setWeekdays(FORMAT,ABBREVIATED) failed");
307 }
308
309 const UnicodeString *narrowWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
310 fr.setWeekdays(narrowWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW);
311 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW) !=
312 *fr.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW )) {
313 errln("ERROR: setWeekdays(FORMAT,NARROW) failed");
314 }
315
316 const UnicodeString *standaloneWideWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
317 fr.setWeekdays(standaloneWideWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE);
318 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE) !=
319 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE )) {
320 errln("ERROR: setWeekdays(STANDALONE,WIDE) failed");
321 }
322
323 const UnicodeString *standaloneShortWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
324 fr.setWeekdays(standaloneShortWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED);
325 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED) !=
326 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED )) {
327 errln("ERROR: setWeekdays(STANDALONE,ABBREVIATED) failed");
328 }
329
330 const UnicodeString *standaloneNarrowWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
331 fr.setWeekdays(standaloneNarrowWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW);
332 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW) !=
333 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW )) {
334 errln("ERROR: setWeekdays(STANDALONE,NARROW) failed");
335 }
336
337 const UnicodeString *wideQuarters = en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
338 fr2.setQuarters(wideQuarters, count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
339 if( *en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE) !=
340 *fr2.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE )) {
341 errln("ERROR: setQuarters(FORMAT, WIDE) failed");
342 }
343
344 const UnicodeString *abbrQuarters = en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
345 fr2.setQuarters(abbrQuarters, count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
346 if( *en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED) !=
347 *fr2.getQuarters(count,DateFormatSymbols::FORMAT ,DateFormatSymbols::ABBREVIATED )) {
348 errln("ERROR: setQuarters(FORMAT, ABBREVIATED) failed");
349 }
350
351 const UnicodeString *standaloneWideQuarters = en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
352 fr.setQuarters(standaloneWideQuarters, count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
353 if( *en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE) !=
354 *fr.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE )) {
355 errln("ERROR: setQuarters(STANDALONE, WIDE) failed");
356 }
357
358 const UnicodeString *standaloneShortQuarters = en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
359 fr.setQuarters(standaloneShortQuarters, count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
360 if( *en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED) !=
361 *fr.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED )) {
362 errln("ERROR: setQuarters(STANDALONE, ABBREVIATED) failed");
363 }
364
365 const UnicodeString *ampms = en.getAmPmStrings(count);
366 fr.setAmPmStrings(ampms, count);
367 if( *en.getAmPmStrings(count) != *fr.getAmPmStrings(count)) {
368 errln("ERROR: setAmPmStrings() failed");
369 }
370
371 int32_t rowCount = 0, columnCount = 0;
372 const UnicodeString **strings = en.getZoneStrings(rowCount, columnCount);
373 fr.setZoneStrings(strings, rowCount, columnCount);
374 const UnicodeString **strings1 = fr.getZoneStrings(rowCount, columnCount);
375 for(int32_t i = 0; i < rowCount; i++) {
376 for(int32_t j = 0; j < columnCount; j++) {
377 if( strings[i][j] != strings1[i][j] ) {
378 errln("ERROR: setZoneStrings() failed");
379 }
380 }
381 }
382
383 UnicodeString localPattern, pat1, pat2;
384 localPattern = en.getLocalPatternChars(localPattern);
385 fr.setLocalPatternChars(localPattern);
386 if( en.getLocalPatternChars(pat1) != fr.getLocalPatternChars(pat2)) {
387 errln("ERROR: setLocalPatternChars() failed");
388 }
389
390
391 status = U_ZERO_ERROR;
392 DateFormatSymbols foo(status);
393 DateFormatSymbols bar(foo);
394
395 en = fr;
396
397 if(en != fr) {
398 errln("ERROR: Assignment operator failed");
399 }
400 if(foo != bar) {
401 errln("ERROR: Copy Constructor failed");
402 }
403}
404
405#endif /* #if !UCONFIG_NO_FORMATTING */