1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 2007-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: udatpg_test.c
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 2007aug01
16 * created by: Markus W. Scherer
17 *
18 * Test of the C wrapper for the DateTimePatternGenerator.
19 * Calls each C API function and exercises code paths in the wrapper,
20 * but the full functionality is tested in the C++ intltest.
21 *
22 * One item to note: C API functions which return a const UChar *
23 * should return a NUL-terminated string.
24 * (The C++ implementation needs to use getTerminatedBuffer()
25 * on UnicodeString objects which end up being returned this way.)
26 */
27
28 #include "unicode/utypes.h"
29
30 #if !UCONFIG_NO_FORMATTING
31
32 #include <stdbool.h>
33
34 #include "unicode/udat.h"
35 #include "unicode/udatpg.h"
36 #include "unicode/ustring.h"
37 #include "cintltst.h"
38 #include "cmemory.h"
39
40 void addDateTimePatternGeneratorTest(TestNode** root);
41
42 #define TESTCASE(x) addTest(root, &x, "tsformat/udatpg_test/" #x)
43
44 static void TestOpenClose(void);
45 static void TestUsage(void);
46 static void TestBuilder(void);
47 static void TestOptions(void);
48 static void TestGetFieldDisplayNames(void);
49 static void TestGetDefaultHourCycle(void);
50 static void TestGetDefaultHourCycleOnEmptyInstance(void);
51 static void TestEras(void);
52 static void TestDateTimePatterns(void);
53 static void TestRegionOverride(void);
54
addDateTimePatternGeneratorTest(TestNode ** root)55 void addDateTimePatternGeneratorTest(TestNode** root) {
56 TESTCASE(TestOpenClose);
57 TESTCASE(TestUsage);
58 TESTCASE(TestBuilder);
59 TESTCASE(TestOptions);
60 TESTCASE(TestGetFieldDisplayNames);
61 TESTCASE(TestGetDefaultHourCycle);
62 TESTCASE(TestGetDefaultHourCycleOnEmptyInstance);
63 TESTCASE(TestEras);
64 TESTCASE(TestDateTimePatterns);
65 TESTCASE(TestRegionOverride);
66 }
67
68 /*
69 * Pipe symbol '|'. We pass only the first UChar without NUL-termination.
70 * The second UChar is just to verify that the API does not pick that up.
71 */
72 static const UChar pipeString[]={ 0x7c, 0x0a };
73
74 static const UChar testSkeleton1[]={ 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
75 static const UChar expectingBestPattern[]={ 0x48, 0x2e, 0x6d, 0x6d, 0 }; /* H.mm */
76 static const UChar testPattern[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0 }; /* HH:mm */
77 static const UChar expectingSkeleton[]= { 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
78 static const UChar expectingBaseSkeleton[]= { 0x48, 0x6d, 0 }; /* HHmm */
79 static const UChar redundantPattern[]={ 0x79, 0x79, 0x4d, 0x4d, 0x4d, 0 }; /* yyMMM */
80 static const UChar testFormat[]= {0x7B, 0x31, 0x7D, 0x20, 0x7B, 0x30, 0x7D, 0}; /* {1} {0} */
81 static const UChar appendItemName[]= {0x68, 0x72, 0}; /* hr */
82 static const UChar testPattern2[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0x20, 0x76, 0 }; /* HH:mm v */
83 static const UChar replacedStr[]={ 0x76, 0x76, 0x76, 0x76, 0 }; /* vvvv */
84 /* results for getBaseSkeletons() - {Hmv}, {yMMM} */
85 static const UChar resultBaseSkeletons[2][10] = {{0x48,0x6d, 0x76, 0}, {0x79, 0x4d, 0x4d, 0x4d, 0 } };
86 static const UChar sampleFormatted[] = {0x31, 0x30, 0x20, 0x6A, 0x75, 0x69, 0x6C, 0x2E, 0}; /* 10 juil. */
87 static const UChar skeleton[]= {0x4d, 0x4d, 0x4d, 0x64, 0}; /* MMMd */
88 static const UChar timeZoneGMT[] = { 0x0047, 0x004d, 0x0054, 0x0000 }; /* "GMT" */
89
TestOpenClose()90 static void TestOpenClose() {
91 UErrorCode errorCode=U_ZERO_ERROR;
92 UDateTimePatternGenerator *dtpg, *dtpg2;
93 const UChar *s;
94 int32_t length;
95
96 /* Open a DateTimePatternGenerator for the default locale. */
97 dtpg=udatpg_open(NULL, &errorCode);
98 if(U_FAILURE(errorCode)) {
99 log_err_status(errorCode, "udatpg_open(NULL) failed - %s\n", u_errorName(errorCode));
100 return;
101 }
102 udatpg_close(dtpg);
103
104 /* Now one for German. */
105 dtpg=udatpg_open("de", &errorCode);
106 if(U_FAILURE(errorCode)) {
107 log_err("udatpg_open(de) failed - %s\n", u_errorName(errorCode));
108 return;
109 }
110
111 /* Make some modification which we verify gets passed on to the clone. */
112 udatpg_setDecimal(dtpg, pipeString, 1);
113
114 /* Clone the generator. */
115 dtpg2=udatpg_clone(dtpg, &errorCode);
116 if(U_FAILURE(errorCode) || dtpg2==NULL) {
117 log_err("udatpg_clone() failed - %s\n", u_errorName(errorCode));
118 return;
119 }
120
121 /* Verify that the clone has the custom decimal symbol. */
122 s=udatpg_getDecimal(dtpg2, &length);
123 if(s==pipeString || length!=1 || 0!=u_memcmp(s, pipeString, length) || s[length]!=0) {
124 log_err("udatpg_getDecimal(cloned object) did not return the expected string\n");
125 return;
126 }
127
128 udatpg_close(dtpg);
129 udatpg_close(dtpg2);
130 }
131
132 typedef struct {
133 UDateTimePatternField field;
134 UChar name[12];
135 } AppendItemNameData;
136
137 static const AppendItemNameData appendItemNameData[] = { /* for Finnish */
138 { UDATPG_YEAR_FIELD, {0x0076,0x0075,0x006F,0x0073,0x0069,0} }, /* "vuosi" */
139 { UDATPG_MONTH_FIELD, {0x006B,0x0075,0x0075,0x006B,0x0061,0x0075,0x0073,0x0069,0} }, /* "kuukausi" */
140 { UDATPG_WEEKDAY_FIELD, {0x0076,0x0069,0x0069,0x006B,0x006F,0x006E,0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
141 { UDATPG_DAY_FIELD, {0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
142 { UDATPG_HOUR_FIELD, {0x0074,0x0075,0x006E,0x0074,0x0069,0} }, /* "tunti" */
143 { UDATPG_FIELD_COUNT, {0} } /* terminator */
144 };
145
TestUsage()146 static void TestUsage() {
147 UErrorCode errorCode=U_ZERO_ERROR;
148 UDateTimePatternGenerator *dtpg;
149 const AppendItemNameData * appItemNameDataPtr;
150 UChar bestPattern[20];
151 UChar result[20];
152 int32_t length;
153 UChar *s;
154 const UChar *r;
155
156 dtpg=udatpg_open("fi", &errorCode);
157 if(U_FAILURE(errorCode)) {
158 log_err_status(errorCode, "udatpg_open(fi) failed - %s\n", u_errorName(errorCode));
159 return;
160 }
161 length = udatpg_getBestPattern(dtpg, testSkeleton1, 4,
162 bestPattern, 20, &errorCode);
163 if(U_FAILURE(errorCode)) {
164 log_err("udatpg_getBestPattern failed - %s\n", u_errorName(errorCode));
165 return;
166 }
167 if((u_memcmp(bestPattern, expectingBestPattern, length)!=0) || bestPattern[length]!=0) {
168 log_err("udatpg_getBestPattern did not return the expected string\n");
169 return;
170 }
171
172
173 /* Test skeleton == NULL */
174 s=NULL;
175 length = udatpg_getBestPattern(dtpg, s, 0, bestPattern, 20, &errorCode);
176 if(!U_FAILURE(errorCode)&&(length!=0) ) {
177 log_err("udatpg_getBestPattern failed in illegal argument - skeleton is NULL.\n");
178 return;
179 }
180
181 /* Test udatpg_getSkeleton */
182 length = udatpg_getSkeleton(dtpg, testPattern, 5, result, 20, &errorCode);
183 if(U_FAILURE(errorCode)) {
184 log_err("udatpg_getSkeleton failed - %s\n", u_errorName(errorCode));
185 return;
186 }
187 if((u_memcmp(result, expectingSkeleton, length)!=0) || result[length]!=0) {
188 log_err("udatpg_getSkeleton did not return the expected string\n");
189 return;
190 }
191
192 /* Test pattern == NULL */
193 s=NULL;
194 length = udatpg_getSkeleton(dtpg, s, 0, result, 20, &errorCode);
195 if(!U_FAILURE(errorCode)&&(length!=0) ) {
196 log_err("udatpg_getSkeleton failed in illegal argument - pattern is NULL.\n");
197 return;
198 }
199
200 /* Test udatpg_getBaseSkeleton */
201 length = udatpg_getBaseSkeleton(dtpg, testPattern, 5, result, 20, &errorCode);
202 if(U_FAILURE(errorCode)) {
203 log_err("udatpg_getBaseSkeleton failed - %s\n", u_errorName(errorCode));
204 return;
205 }
206 if((u_memcmp(result, expectingBaseSkeleton, length)!=0) || result[length]!=0) {
207 log_err("udatpg_getBaseSkeleton did not return the expected string\n");
208 return;
209 }
210
211 /* Test pattern == NULL */
212 s=NULL;
213 length = udatpg_getBaseSkeleton(dtpg, s, 0, result, 20, &errorCode);
214 if(!U_FAILURE(errorCode)&&(length!=0) ) {
215 log_err("udatpg_getBaseSkeleton failed in illegal argument - pattern is NULL.\n");
216 return;
217 }
218
219 /* set append format to {1}{0} */
220 udatpg_setAppendItemFormat( dtpg, UDATPG_MONTH_FIELD, testFormat, 7 );
221 r = udatpg_getAppendItemFormat(dtpg, UDATPG_MONTH_FIELD, &length);
222
223
224 if(length!=7 || 0!=u_memcmp(r, testFormat, length) || r[length]!=0) {
225 log_err("udatpg_setAppendItemFormat did not return the expected string\n");
226 return;
227 }
228
229 for (appItemNameDataPtr = appendItemNameData; appItemNameDataPtr->field < UDATPG_FIELD_COUNT; appItemNameDataPtr++) {
230 int32_t nameLength;
231 const UChar * namePtr = udatpg_getAppendItemName(dtpg, appItemNameDataPtr->field, &nameLength);
232 if ( namePtr == NULL || u_strncmp(appItemNameDataPtr->name, namePtr, nameLength) != 0 ) {
233 log_err("udatpg_getAppendItemName returns invalid name for field %d\n", (int)appItemNameDataPtr->field);
234 }
235 }
236
237 /* set append name to hr */
238 udatpg_setAppendItemName(dtpg, UDATPG_HOUR_FIELD, appendItemName, 2);
239 r = udatpg_getAppendItemName(dtpg, UDATPG_HOUR_FIELD, &length);
240
241 if(length!=2 || 0!=u_memcmp(r, appendItemName, length) || r[length]!=0) {
242 log_err("udatpg_setAppendItemName did not return the expected string\n");
243 return;
244 }
245
246 /* set date time format to {1}{0} */
247 udatpg_setDateTimeFormat( dtpg, testFormat, 7 );
248 r = udatpg_getDateTimeFormat(dtpg, &length);
249
250 if(length!=7 || 0!=u_memcmp(r, testFormat, length) || r[length]!=0) {
251 log_err("udatpg_setDateTimeFormat did not return the expected string\n");
252 return;
253 }
254 udatpg_close(dtpg);
255 }
256
TestBuilder()257 static void TestBuilder() {
258 UErrorCode errorCode=U_ZERO_ERROR;
259 UDateTimePatternGenerator *dtpg;
260 UDateTimePatternConflict conflict;
261 UEnumeration *en;
262 UChar result[20];
263 int32_t length, pLength;
264 const UChar *s, *p;
265 const UChar* ptrResult[2];
266 int32_t count=0;
267 UDateTimePatternGenerator *generator;
268 int32_t formattedCapacity, resultLen,patternCapacity ;
269 UChar pattern[40], formatted[40];
270 UDateFormat *formatter;
271 UDate sampleDate = 837039928046.0;
272 static const char locale[]= "fr";
273 UErrorCode status=U_ZERO_ERROR;
274
275 /* test create an empty DateTimePatternGenerator */
276 dtpg=udatpg_openEmpty(&errorCode);
277 if(U_FAILURE(errorCode)) {
278 log_err("udatpg_openEmpty() failed - %s\n", u_errorName(errorCode));
279 return;
280 }
281
282 /* Add a pattern */
283 conflict = udatpg_addPattern(dtpg, redundantPattern, 5, false, result, 20,
284 &length, &errorCode);
285 if(U_FAILURE(errorCode)) {
286 log_err("udatpg_addPattern() failed - %s\n", u_errorName(errorCode));
287 return;
288 }
289 /* Add a redundant pattern */
290 conflict = udatpg_addPattern(dtpg, redundantPattern, 5, false, result, 20,
291 &length, &errorCode);
292 if(conflict == UDATPG_NO_CONFLICT) {
293 log_err("udatpg_addPattern() failed to find the duplicate pattern.\n");
294 return;
295 }
296 /* Test pattern == NULL */
297 s=NULL;
298 length = udatpg_addPattern(dtpg, s, 0, false, result, 20,
299 &length, &errorCode);
300 if(!U_FAILURE(errorCode)&&(length!=0) ) {
301 log_err("udatpg_addPattern failed in illegal argument - pattern is NULL.\n");
302 return;
303 }
304
305 /* replace field type */
306 errorCode=U_ZERO_ERROR;
307 conflict = udatpg_addPattern(dtpg, testPattern2, 7, false, result, 20,
308 &length, &errorCode);
309 if((conflict != UDATPG_NO_CONFLICT)||U_FAILURE(errorCode)) {
310 log_err("udatpg_addPattern() failed to add HH:mm v. - %s\n", u_errorName(errorCode));
311 return;
312 }
313 length = udatpg_replaceFieldTypes(dtpg, testPattern2, 7, replacedStr, 4,
314 result, 20, &errorCode);
315 if (U_FAILURE(errorCode) || (length==0) ) {
316 log_err("udatpg_replaceFieldTypes failed!\n");
317 return;
318 }
319
320 /* Get all skeletons and the crroespong pattern for each skeleton. */
321 ptrResult[0] = testPattern2;
322 ptrResult[1] = redundantPattern;
323 count=0;
324 en = udatpg_openSkeletons(dtpg, &errorCode);
325 if (U_FAILURE(errorCode) || (length==0) ) {
326 log_err("udatpg_openSkeletons failed!\n");
327 return;
328 }
329 while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
330 p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
331 if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, ptrResult[count], pLength)!=0 ) {
332 log_err("udatpg_getPatternForSkeleton failed!\n");
333 return;
334 }
335 count++;
336 }
337 uenum_close(en);
338
339 /* Get all baseSkeletons */
340 en = udatpg_openBaseSkeletons(dtpg, &errorCode);
341 count=0;
342 while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
343 p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
344 if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, resultBaseSkeletons[count], pLength)!=0 ) {
345 log_err("udatpg_getPatternForSkeleton failed!\n");
346 return;
347 }
348 count++;
349 }
350 if (U_FAILURE(errorCode) || (length==0) ) {
351 log_err("udatpg_openSkeletons failed!\n");
352 return;
353 }
354 uenum_close(en);
355
356 udatpg_close(dtpg);
357
358 /* sample code in Userguide */
359 patternCapacity = UPRV_LENGTHOF(pattern);
360 status=U_ZERO_ERROR;
361 generator=udatpg_open(locale, &status);
362 if(U_FAILURE(status)) {
363 return;
364 }
365
366 /* get a pattern for an abbreviated month and day */
367 length = udatpg_getBestPattern(generator, skeleton, 4,
368 pattern, patternCapacity, &status);
369 formatter = udat_open(UDAT_PATTERN, UDAT_PATTERN, locale, timeZoneGMT, -1,
370 pattern, length, &status);
371 if (formatter==NULL) {
372 log_err("Failed to initialize the UDateFormat of the sample code in Userguide.\n");
373 udatpg_close(generator);
374 return;
375 }
376
377 /* use it to format (or parse) */
378 formattedCapacity = UPRV_LENGTHOF(formatted);
379 resultLen=udat_format(formatter, ucal_getNow(), formatted, formattedCapacity,
380 NULL, &status);
381 /* for French, the result is "13 sept." */
382
383 /* cannot use the result from ucal_getNow() because the value change evreyday. */
384 resultLen=udat_format(formatter, sampleDate, formatted, formattedCapacity,
385 NULL, &status);
386 if ( u_memcmp(sampleFormatted, formatted, resultLen) != 0 ) {
387 log_err("Failed udat_format() of sample code in Userguide.\n");
388 }
389 udatpg_close(generator);
390 udat_close(formatter);
391 }
392
393 typedef struct DTPtnGenOptionsData {
394 const char * locale;
395 const UChar * skel;
396 UDateTimePatternMatchOptions options;
397 const UChar * expectedPattern;
398 } DTPtnGenOptionsData;
399 enum { kTestOptionsPatLenMax = 32 };
400
401 static const UChar skel_Hmm[] = u"Hmm";
402 static const UChar skel_HHmm[] = u"HHmm";
403 static const UChar skel_hhmm[] = u"hhmm";
404 static const UChar patn_hcmm_a[] = u"h:mm\u202Fa";
405 static const UChar patn_HHcmm[] = u"HH:mm";
406 static const UChar patn_hhcmm_a[] = u"hh:mm\u202Fa";
407 static const UChar patn_HHpmm[] = u"HH.mm";
408 static const UChar patn_hpmm_a[] = u"h.mm\u202Fa";
409 static const UChar patn_Hpmm[] = u"H.mm";
410 static const UChar patn_hhpmm_a[] = u"hh.mm\u202Fa";
411
TestOptions()412 static void TestOptions() {
413 const DTPtnGenOptionsData testData[] = {
414 /*loc skel options expectedPattern */
415 { "en", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm },
416 { "en", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm },
417 { "en", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hcmm_a },
418 { "en", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm },
419 { "en", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm },
420 { "en", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhcmm_a },
421 { "da", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm },
422 { "da", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm },
423 { "da", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hpmm_a },
424 { "da", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_Hpmm },
425 { "da", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHpmm },
426 { "da", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhpmm_a },
427 };
428
429 int count = UPRV_LENGTHOF(testData);
430 const DTPtnGenOptionsData * testDataPtr = testData;
431
432 for (; count-- > 0; ++testDataPtr) {
433 UErrorCode status = U_ZERO_ERROR;
434 UDateTimePatternGenerator * dtpgen = udatpg_open(testDataPtr->locale, &status);
435 if ( U_SUCCESS(status) ) {
436 UChar pattern[kTestOptionsPatLenMax];
437 int32_t patLen = udatpg_getBestPatternWithOptions(dtpgen, testDataPtr->skel, -1,
438 testDataPtr->options, pattern,
439 kTestOptionsPatLenMax, &status);
440 if ( U_FAILURE(status) || u_strncmp(pattern, testDataPtr->expectedPattern, patLen+1) != 0 ) {
441 char skelBytes[kTestOptionsPatLenMax];
442 char expectedPatternBytes[kTestOptionsPatLenMax];
443 char patternBytes[kTestOptionsPatLenMax];
444 log_err("ERROR udatpg_getBestPatternWithOptions, locale %s, skeleton %s, options 0x%04X, expected pattern %s, got %s, status %d\n",
445 testDataPtr->locale, u_austrncpy(skelBytes,testDataPtr->skel,kTestOptionsPatLenMax), testDataPtr->options,
446 u_austrncpy(expectedPatternBytes,testDataPtr->expectedPattern,kTestOptionsPatLenMax),
447 u_austrncpy(patternBytes,pattern,kTestOptionsPatLenMax), status );
448 }
449 udatpg_close(dtpgen);
450 } else {
451 log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr->locale, myErrorName(status));
452 }
453 }
454 }
455
456 typedef struct FieldDisplayNameData {
457 const char * locale;
458 UDateTimePatternField field;
459 UDateTimePGDisplayWidth width;
460 const char * expected;
461 } FieldDisplayNameData;
462 enum { kFieldDisplayNameMax = 32, kFieldDisplayNameBytesMax = 64};
463
TestGetFieldDisplayNames()464 static void TestGetFieldDisplayNames() {
465 const FieldDisplayNameData testData[] = {
466 /*loc field width expectedName */
467 { "de", UDATPG_QUARTER_FIELD, UDATPG_WIDE, "Quartal" },
468 { "de", UDATPG_QUARTER_FIELD, UDATPG_ABBREVIATED, "Quart." },
469 { "de", UDATPG_QUARTER_FIELD, UDATPG_NARROW, "Q" },
470 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_WIDE, "weekday of the month" },
471 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_ABBREVIATED, "wkday. of mo." },
472 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_NARROW, "wkday. of mo." }, // fallback
473 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_WIDE, "weekday of the month" },
474 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_ABBREVIATED, "wkday of mo" }, // override
475 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_NARROW, "wkday of mo" },
476 { "it", UDATPG_SECOND_FIELD, UDATPG_WIDE, "secondo" },
477 { "it", UDATPG_SECOND_FIELD, UDATPG_ABBREVIATED, "s" },
478 { "it", UDATPG_SECOND_FIELD, UDATPG_NARROW, "s" },
479 };
480
481 int count = UPRV_LENGTHOF(testData);
482 const FieldDisplayNameData * testDataPtr = testData;
483 for (; count-- > 0; ++testDataPtr) {
484 UErrorCode status = U_ZERO_ERROR;
485 UDateTimePatternGenerator * dtpgen = udatpg_open(testDataPtr->locale, &status);
486 if ( U_FAILURE(status) ) {
487 log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr->locale, myErrorName(status));
488 } else {
489 UChar expName[kFieldDisplayNameMax];
490 UChar getName[kFieldDisplayNameMax];
491 u_unescape(testDataPtr->expected, expName, kFieldDisplayNameMax);
492
493 int32_t getLen = udatpg_getFieldDisplayName(dtpgen, testDataPtr->field, testDataPtr->width,
494 getName, kFieldDisplayNameMax, &status);
495 if ( U_FAILURE(status) ) {
496 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, got status %s, len %d\n",
497 testDataPtr->locale, testDataPtr->field, testDataPtr->width, u_errorName(status), getLen);
498 } else if ( u_strncmp(expName, getName, kFieldDisplayNameMax) != 0 ) {
499 char expNameB[kFieldDisplayNameBytesMax];
500 char getNameB[kFieldDisplayNameBytesMax];
501 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, expected %s, got %s, status %s\n",
502 testDataPtr->locale, testDataPtr->field, testDataPtr->width,
503 u_austrncpy(expNameB,expName,kFieldDisplayNameBytesMax),
504 u_austrncpy(getNameB,getName,kFieldDisplayNameBytesMax), u_errorName(status) );
505 } else if (testDataPtr->width == UDATPG_WIDE && getLen > 1) {
506 // test preflight & inadequate buffer
507 int32_t getNewLen;
508 status = U_ZERO_ERROR;
509 getNewLen = udatpg_getFieldDisplayName(dtpgen, testDataPtr->field, UDATPG_WIDE, NULL, 0, &status);
510 if (U_FAILURE(status) || getNewLen != getLen) {
511 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, preflight expected len %d, got %d, status %s\n",
512 testDataPtr->locale, testDataPtr->field, testDataPtr->width, getLen, getNewLen, u_errorName(status) );
513 }
514 status = U_ZERO_ERROR;
515 getNewLen = udatpg_getFieldDisplayName(dtpgen, testDataPtr->field, UDATPG_WIDE, getName, getLen-1, &status);
516 if (status!=U_BUFFER_OVERFLOW_ERROR || getNewLen != getLen) {
517 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, overflow expected len %d & BUFFER_OVERFLOW_ERROR, got %d & status %s\n",
518 testDataPtr->locale, testDataPtr->field, testDataPtr->width, getLen, getNewLen, u_errorName(status) );
519 }
520 }
521 udatpg_close(dtpgen);
522 }
523 }
524 }
525
526 typedef struct HourCycleData {
527 const char * locale;
528 UDateFormatHourCycle expected;
529 } HourCycleData;
530
TestGetDefaultHourCycle()531 static void TestGetDefaultHourCycle() {
532 const HourCycleData testData[] = {
533 /*loc expected */
534 { "ar_EG", UDAT_HOUR_CYCLE_12 },
535 { "de_DE", UDAT_HOUR_CYCLE_23 },
536 { "en_AU", UDAT_HOUR_CYCLE_12 },
537 { "en_CA", UDAT_HOUR_CYCLE_12 },
538 { "en_US", UDAT_HOUR_CYCLE_12 },
539 { "es_ES", UDAT_HOUR_CYCLE_23 },
540 { "fi", UDAT_HOUR_CYCLE_23 },
541 { "fr", UDAT_HOUR_CYCLE_23 },
542 { "ja_JP", UDAT_HOUR_CYCLE_23 },
543 { "zh_CN", UDAT_HOUR_CYCLE_23 },
544 { "zh_HK", UDAT_HOUR_CYCLE_12 },
545 { "zh_TW", UDAT_HOUR_CYCLE_12 },
546 { "ko_KR", UDAT_HOUR_CYCLE_12 },
547 };
548 int count = UPRV_LENGTHOF(testData);
549 const HourCycleData * testDataPtr = testData;
550 for (; count-- > 0; ++testDataPtr) {
551 UErrorCode status = U_ZERO_ERROR;
552 UDateTimePatternGenerator * dtpgen =
553 udatpg_open(testDataPtr->locale, &status);
554 if ( U_FAILURE(status) ) {
555 log_data_err( "ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n",
556 testDataPtr->locale, myErrorName(status));
557 } else {
558 UDateFormatHourCycle actual = udatpg_getDefaultHourCycle(dtpgen, &status);
559 if (U_FAILURE(status) || testDataPtr->expected != actual) {
560 log_err("ERROR dtpgen locale %s udatpg_getDefaultHourCycle expected to get %d but get %d\n",
561 testDataPtr->locale, testDataPtr->expected, actual);
562 }
563 udatpg_close(dtpgen);
564 }
565 }
566 }
567
568 // Ensure that calling udatpg_getDefaultHourCycle on an empty instance doesn't call UPRV_UNREACHABLE_EXIT/abort.
TestGetDefaultHourCycleOnEmptyInstance()569 static void TestGetDefaultHourCycleOnEmptyInstance() {
570 UErrorCode status = U_ZERO_ERROR;
571 UDateTimePatternGenerator * dtpgen = udatpg_openEmpty(&status);
572
573 if (U_FAILURE(status)) {
574 log_data_err("ERROR udatpg_openEmpty failed, status: %s \n", myErrorName(status));
575 return;
576 }
577
578 (void)udatpg_getDefaultHourCycle(dtpgen, &status);
579 if (!U_FAILURE(status)) {
580 log_data_err("ERROR expected udatpg_getDefaultHourCycle on an empty instance to fail, status: %s", myErrorName(status));
581 }
582
583 status = U_USELESS_COLLATOR_ERROR;
584 (void)udatpg_getDefaultHourCycle(dtpgen, &status);
585 if (status != U_USELESS_COLLATOR_ERROR) {
586 log_data_err("ERROR udatpg_getDefaultHourCycle shouldn't modify status if it is already failed, status: %s", myErrorName(status));
587 }
588
589 udatpg_close(dtpgen);
590 }
591
592 // Test for ICU-21202: Make sure DateTimePatternGenerator supplies an era field for year formats using the
593 // Buddhist and Japanese calendars for all English-speaking locales.
TestEras(void)594 static void TestEras(void) {
595 const char* localeIDs[] = {
596 "en_US@calendar=japanese",
597 "en_GB@calendar=japanese",
598 "en_150@calendar=japanese",
599 "en_001@calendar=japanese",
600 "en@calendar=japanese",
601 "en_US@calendar=buddhist",
602 "en_GB@calendar=buddhist",
603 "en_150@calendar=buddhist",
604 "en_001@calendar=buddhist",
605 "en@calendar=buddhist",
606 };
607
608 UErrorCode err = U_ZERO_ERROR;
609 for (int32_t i = 0; i < UPRV_LENGTHOF(localeIDs); i++) {
610 const char* locale = localeIDs[i];
611 UDateTimePatternGenerator* dtpg = udatpg_open(locale, &err);
612 if (U_SUCCESS(err)) {
613 UChar pattern[200];
614 udatpg_getBestPattern(dtpg, u"y", 1, pattern, 200, &err);
615
616 if (u_strchr(pattern, u'G') == NULL) {
617 log_err("missing era field for locale %s\n", locale);
618 }
619 }
620 udatpg_close(dtpg);
621 }
622 }
623
624 enum { kNumDateTimePatterns = 4 };
625
626 typedef struct {
627 const char* localeID;
628 const UChar* expectPat[kNumDateTimePatterns];
629 } DTPLocaleAndResults;
630
631 static void doDTPatternTest(UDateTimePatternGenerator* udtpg,
632 const UChar** skeletons,
633 DTPLocaleAndResults* localeAndResultsPtr);
634
TestDateTimePatterns(void)635 static void TestDateTimePatterns(void) {
636 const UChar* skeletons[kNumDateTimePatterns] = {
637 u"yMMMMEEEEdjmm", // full date, short time
638 u"yMMMMdjmm", // long date, short time
639 u"yMMMdjmm", // medium date, short time
640 u"yMdjmm" // short date, short time
641 };
642 // The following tests some locales in which there are differences between the
643 // DateTimePatterns of various length styles.
644 DTPLocaleAndResults localeAndResults[] = {
645 { "en", { u"EEEE, MMMM d, y 'at' h:mm\u202Fa", // long != medium
646 u"MMMM d, y 'at' h:mm\u202Fa",
647 u"MMM d, y, h:mm\u202Fa",
648 u"M/d/y, h:mm\u202Fa" } },
649 { "fr", { u"EEEE d MMMM y 'à' HH:mm", // medium != short
650 u"d MMMM y 'à' HH:mm",
651 u"d MMM y, HH:mm",
652 u"dd/MM/y HH:mm" } },
653 { "ha", { u"EEEE d MMMM, y 'da' HH:mm",
654 u"d MMMM, y 'da' HH:mm",
655 u"d MMM, y, HH:mm",
656 u"y-MM-dd, HH:mm" } },
657 { NULL, { NULL, NULL, NULL, NULL } } // terminator
658 };
659
660 const UChar* enDTPatterns[kNumDateTimePatterns] = {
661 u"{1} 'at' {0}",
662 u"{1} 'at' {0}",
663 u"{1}, {0}",
664 u"{1}, {0}"
665 };
666 const UChar* modDTPatterns[kNumDateTimePatterns] = {
667 u"{1} _0_ {0}",
668 u"{1} _1_ {0}",
669 u"{1} _2_ {0}",
670 u"{1} _3_ {0}"
671 };
672 DTPLocaleAndResults enModResults = { "en", { u"EEEE, MMMM d, y _0_ h:mm\u202Fa",
673 u"MMMM d, y _1_ h:mm\u202Fa",
674 u"MMM d, y _2_ h:mm\u202Fa",
675 u"M/d/y _3_ h:mm\u202Fa" }
676 };
677
678 // Test various locales with standard data
679 UErrorCode status;
680 UDateTimePatternGenerator* udtpg;
681 DTPLocaleAndResults* localeAndResultsPtr = localeAndResults;
682 for (; localeAndResultsPtr->localeID != NULL; localeAndResultsPtr++) {
683 status = U_ZERO_ERROR;
684 udtpg = udatpg_open(localeAndResultsPtr->localeID, &status);
685 if (U_FAILURE(status)) {
686 log_data_err("FAIL: udatpg_open for locale %s: %s", localeAndResultsPtr->localeID, myErrorName(status));
687 } else {
688 doDTPatternTest(udtpg, skeletons, localeAndResultsPtr);
689 udatpg_close(udtpg);
690 }
691 }
692 // Test getting and modifying date-time combining patterns
693 status = U_ZERO_ERROR;
694 udtpg = udatpg_open("en", &status);
695 if (U_FAILURE(status)) {
696 log_data_err("FAIL: udatpg_open #2 for locale en: %s", myErrorName(status));
697 } else {
698 char bExpect[64];
699 char bGet[64];
700 const UChar* uGet;
701 int32_t uGetLen, uExpectLen;
702
703 // Test error: style out of range
704 status = U_ZERO_ERROR;
705 uGet = udatpg_getDateTimeFormatForStyle(udtpg, UDAT_NONE, &uGetLen, &status);
706 if (status != U_ILLEGAL_ARGUMENT_ERROR || uGetLen != 0 || uGet==NULL || *uGet!= 0) {
707 if (uGet==NULL) {
708 log_err("FAIL: udatpg_getDateTimeFormatForStyle with invalid style, expected U_ILLEGAL_ARGUMENT_ERROR "
709 "and ptr to empty string but got %s, len %d, ptr = NULL\n", myErrorName(status), uGetLen);
710 } else {
711 log_err("FAIL: udatpg_getDateTimeFormatForStyle with invalid style, expected U_ILLEGAL_ARGUMENT_ERROR "
712 "and ptr to empty string but got %s, len %d, *ptr = %04X\n", myErrorName(status), uGetLen, *uGet);
713 }
714 }
715
716 // Test normal getting and setting
717 for (int32_t patStyle = 0; patStyle < kNumDateTimePatterns; patStyle++) {
718 status = U_ZERO_ERROR;
719 uExpectLen = u_strlen(enDTPatterns[patStyle]);
720 uGet = udatpg_getDateTimeFormatForStyle(udtpg, patStyle, &uGetLen, &status);
721 if (U_FAILURE(status)) {
722 log_err("FAIL udatpg_getDateTimeFormatForStyle %d (en before mod), get %s\n", patStyle, myErrorName(status));
723 } else if (uGetLen != uExpectLen || u_strncmp(uGet, enDTPatterns[patStyle], uExpectLen) != 0) {
724 u_austrcpy(bExpect, enDTPatterns[patStyle]);
725 u_austrcpy(bGet, uGet);
726 log_err("ERROR udatpg_getDateTimeFormatForStyle %d (en before mod), expect %d:\"%s\", get %d:\"%s\"\n",
727 patStyle, uExpectLen, bExpect, uGetLen, bGet);
728 }
729 status = U_ZERO_ERROR;
730 udatpg_setDateTimeFormatForStyle(udtpg, patStyle, modDTPatterns[patStyle], -1, &status);
731 if (U_FAILURE(status)) {
732 log_err("FAIL udatpg_setDateTimeFormatForStyle %d (en), get %s\n", patStyle, myErrorName(status));
733 } else {
734 uExpectLen = u_strlen(modDTPatterns[patStyle]);
735 uGet = udatpg_getDateTimeFormatForStyle(udtpg, patStyle, &uGetLen, &status);
736 if (U_FAILURE(status)) {
737 log_err("FAIL udatpg_getDateTimeFormatForStyle %d (en after mod), get %s\n", patStyle, myErrorName(status));
738 } else if (uGetLen != uExpectLen || u_strncmp(uGet, modDTPatterns[patStyle], uExpectLen) != 0) {
739 u_austrcpy(bExpect, modDTPatterns[patStyle]);
740 u_austrcpy(bGet, uGet);
741 log_err("ERROR udatpg_getDateTimeFormatForStyle %d (en after mod), expect %d:\"%s\", get %d:\"%s\"\n",
742 patStyle, uExpectLen, bExpect, uGetLen, bGet);
743 }
744 }
745 }
746 // Test result of setting
747 doDTPatternTest(udtpg, skeletons, &enModResults);
748 // Test old get/set functions
749 uExpectLen = u_strlen(modDTPatterns[UDAT_MEDIUM]);
750 uGet = udatpg_getDateTimeFormat(udtpg, &uGetLen);
751 if (uGetLen != uExpectLen || u_strncmp(uGet, modDTPatterns[UDAT_MEDIUM], uExpectLen) != 0) {
752 u_austrcpy(bExpect, modDTPatterns[UDAT_MEDIUM]);
753 u_austrcpy(bGet, uGet);
754 log_err("ERROR udatpg_getDateTimeFormat (en after mod), expect %d:\"%s\", get %d:\"%s\"\n",
755 uExpectLen, bExpect, uGetLen, bGet);
756 }
757 udatpg_setDateTimeFormat(udtpg, modDTPatterns[UDAT_SHORT], -1); // set all dateTimePatterns to the short format
758 uExpectLen = u_strlen(modDTPatterns[UDAT_SHORT]);
759 u_austrcpy(bExpect, modDTPatterns[UDAT_SHORT]);
760 for (int32_t patStyle = 0; patStyle < kNumDateTimePatterns; patStyle++) {
761 status = U_ZERO_ERROR;
762 uGet = udatpg_getDateTimeFormatForStyle(udtpg, patStyle, &uGetLen, &status);
763 if (U_FAILURE(status)) {
764 log_err("FAIL udatpg_getDateTimeFormatForStyle %d (en after second mod), get %s\n", patStyle, myErrorName(status));
765 } else if (uGetLen != uExpectLen || u_strncmp(uGet, modDTPatterns[UDAT_SHORT], uExpectLen) != 0) {
766 u_austrcpy(bGet, uGet);
767 log_err("ERROR udatpg_getDateTimeFormatForStyle %d (en after second mod), expect %d:\"%s\", get %d:\"%s\"\n",
768 patStyle, uExpectLen, bExpect, uGetLen, bGet);
769 }
770 }
771
772 udatpg_close(udtpg);
773 }
774 }
775
doDTPatternTest(UDateTimePatternGenerator * udtpg,const UChar ** skeletons,DTPLocaleAndResults * localeAndResultsPtr)776 static void doDTPatternTest(UDateTimePatternGenerator* udtpg,
777 const UChar** skeletons,
778 DTPLocaleAndResults* localeAndResultsPtr) {
779 for (int32_t patStyle = 0; patStyle < kNumDateTimePatterns; patStyle++) {
780 UChar uGet[64];
781 int32_t uGetLen, uExpectLen;
782 UErrorCode status = U_ZERO_ERROR;
783 uExpectLen = u_strlen(localeAndResultsPtr->expectPat[patStyle]);
784 uGetLen = udatpg_getBestPattern(udtpg, skeletons[patStyle], -1, uGet, 64, &status);
785 if (U_FAILURE(status)) {
786 log_err("FAIL udatpg_getBestPattern locale %s style %d: %s\n", localeAndResultsPtr->localeID, patStyle, myErrorName(status));
787 } else if (uGetLen != uExpectLen || u_strncmp(uGet, localeAndResultsPtr->expectPat[patStyle], uExpectLen) != 0) {
788 char bExpect[64];
789 char bGet[64];
790 u_austrcpy(bExpect, localeAndResultsPtr->expectPat[patStyle]);
791 u_austrcpy(bGet, uGet);
792 log_err("ERROR udatpg_getBestPattern locale %s style %d, expect %d:\"%s\", get %d:\"%s\"\n",
793 localeAndResultsPtr->localeID, patStyle, uExpectLen, bExpect, uGetLen, bGet);
794 }
795 }
796 }
797
TestRegionOverride(void)798 static void TestRegionOverride(void) {
799 typedef struct RegionOverrideTest {
800 const char* locale;
801 const UChar* expectedPattern;
802 UDateFormatHourCycle expectedHourCycle;
803 } RegionOverrideTest;
804
805 const RegionOverrideTest testCases[] = {
806 { "en_US", u"h:mm\u202fa", UDAT_HOUR_CYCLE_12 },
807 { "en_GB", u"HH:mm", UDAT_HOUR_CYCLE_23 },
808 { "en_US@rg=GBZZZZ", u"HH:mm", UDAT_HOUR_CYCLE_23 },
809 { "en_US@hours=h23", u"HH:mm", UDAT_HOUR_CYCLE_23 },
810 };
811
812 for (int32_t i = 0; i < UPRV_LENGTHOF(testCases); i++) {
813 UErrorCode err = U_ZERO_ERROR;
814 UChar actualPattern[200];
815 UDateTimePatternGenerator* dtpg = udatpg_open(testCases[i].locale, &err);
816
817 if (assertSuccess("Error creating dtpg", &err)) {
818 UDateFormatHourCycle actualHourCycle = udatpg_getDefaultHourCycle(dtpg, &err);
819 udatpg_getBestPattern(dtpg, u"jmm", -1, actualPattern, 200, &err);
820
821 if (assertSuccess("Error using dtpg", &err)) {
822 assertIntEquals("Wrong hour cycle", testCases[i].expectedHourCycle, actualHourCycle);
823 assertUEquals("Wrong pattern", testCases[i].expectedPattern, actualPattern);
824 }
825 }
826 udatpg_close(dtpg);
827 }
828 }
829 #endif
830