]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/udatpg_test.c
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / udatpg_test.c
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
46f4442e
A
3/*
4*******************************************************************************
5*
2ca993e8 6* Copyright (C) 2007-2016, International Business Machines
46f4442e
A
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: udatpg_test.c
f3c0d7a5 11* encoding: UTF-8
46f4442e
A
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#include "unicode/udat.h"
32#include "unicode/udatpg.h"
33#include "unicode/ustring.h"
34#include "cintltst.h"
2ca993e8 35#include "cmemory.h"
46f4442e
A
36
37void addDateTimePatternGeneratorTest(TestNode** root);
38
39#define TESTCASE(x) addTest(root, &x, "tsformat/udatpg_test/" #x)
40
41static void TestOpenClose(void);
42static void TestUsage(void);
43static void TestBuilder(void);
729e4ab9 44static void TestOptions(void);
0f5d89e8 45static void TestGetFieldDisplayNames(void);
3d1f044b 46static void TestJapaneseCalendarItems(void); // rdar://52042600
46f4442e
A
47
48void addDateTimePatternGeneratorTest(TestNode** root) {
49 TESTCASE(TestOpenClose);
50 TESTCASE(TestUsage);
51 TESTCASE(TestBuilder);
729e4ab9 52 TESTCASE(TestOptions);
0f5d89e8 53 TESTCASE(TestGetFieldDisplayNames);
3d1f044b 54 TESTCASE(TestJapaneseCalendarItems);
46f4442e
A
55}
56
57/*
58 * Pipe symbol '|'. We pass only the first UChar without NUL-termination.
59 * The second UChar is just to verify that the API does not pick that up.
60 */
61static const UChar pipeString[]={ 0x7c, 0x0a };
62
63static const UChar testSkeleton1[]={ 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
729e4ab9 64static const UChar expectingBestPattern[]={ 0x48, 0x2e, 0x6d, 0x6d, 0 }; /* H.mm */
46f4442e
A
65static const UChar testPattern[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0 }; /* HH:mm */
66static const UChar expectingSkeleton[]= { 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
67static const UChar expectingBaseSkeleton[]= { 0x48, 0x6d, 0 }; /* HHmm */
68static const UChar redundantPattern[]={ 0x79, 0x79, 0x4d, 0x4d, 0x4d, 0 }; /* yyMMM */
69static const UChar testFormat[]= {0x7B, 0x31, 0x7D, 0x20, 0x7B, 0x30, 0x7D, 0}; /* {1} {0} */
70static const UChar appendItemName[]= {0x68, 0x72, 0}; /* hr */
71static const UChar testPattern2[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0x20, 0x76, 0 }; /* HH:mm v */
72static const UChar replacedStr[]={ 0x76, 0x76, 0x76, 0x76, 0 }; /* vvvv */
73/* results for getBaseSkeletons() - {Hmv}, {yMMM} */
74static const UChar resultBaseSkeletons[2][10] = {{0x48,0x6d, 0x76, 0}, {0x79, 0x4d, 0x4d, 0x4d, 0 } };
75static const UChar sampleFormatted[] = {0x31, 0x30, 0x20, 0x6A, 0x75, 0x69, 0x6C, 0x2E, 0}; /* 10 juil. */
76static const UChar skeleton[]= {0x4d, 0x4d, 0x4d, 0x64, 0}; /* MMMd */
77static const UChar timeZoneGMT[] = { 0x0047, 0x004d, 0x0054, 0x0000 }; /* "GMT" */
78
79static void TestOpenClose() {
80 UErrorCode errorCode=U_ZERO_ERROR;
81 UDateTimePatternGenerator *dtpg, *dtpg2;
82 const UChar *s;
83 int32_t length;
84
85 /* Open a DateTimePatternGenerator for the default locale. */
86 dtpg=udatpg_open(NULL, &errorCode);
87 if(U_FAILURE(errorCode)) {
729e4ab9 88 log_err_status(errorCode, "udatpg_open(NULL) failed - %s\n", u_errorName(errorCode));
46f4442e
A
89 return;
90 }
91 udatpg_close(dtpg);
92
93 /* Now one for German. */
94 dtpg=udatpg_open("de", &errorCode);
95 if(U_FAILURE(errorCode)) {
96 log_err("udatpg_open(de) failed - %s\n", u_errorName(errorCode));
97 return;
98 }
99
100 /* Make some modification which we verify gets passed on to the clone. */
101 udatpg_setDecimal(dtpg, pipeString, 1);
102
103 /* Clone the generator. */
104 dtpg2=udatpg_clone(dtpg, &errorCode);
105 if(U_FAILURE(errorCode) || dtpg2==NULL) {
106 log_err("udatpg_clone() failed - %s\n", u_errorName(errorCode));
107 return;
108 }
109
110 /* Verify that the clone has the custom decimal symbol. */
111 s=udatpg_getDecimal(dtpg2, &length);
112 if(s==pipeString || length!=1 || 0!=u_memcmp(s, pipeString, length) || s[length]!=0) {
113 log_err("udatpg_getDecimal(cloned object) did not return the expected string\n");
114 return;
115 }
116
117 udatpg_close(dtpg);
118 udatpg_close(dtpg2);
119}
120
57a6839d
A
121typedef struct {
122 UDateTimePatternField field;
123 UChar name[12];
124} AppendItemNameData;
125
126static const AppendItemNameData appendItemNameData[] = { /* for Finnish */
127 { UDATPG_YEAR_FIELD, {0x0076,0x0075,0x006F,0x0073,0x0069,0} }, /* "vuosi" */
128 { UDATPG_MONTH_FIELD, {0x006B,0x0075,0x0075,0x006B,0x0061,0x0075,0x0073,0x0069,0} }, /* "kuukausi" */
129 { UDATPG_WEEKDAY_FIELD, {0x0076,0x0069,0x0069,0x006B,0x006F,0x006E,0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
130 { UDATPG_DAY_FIELD, {0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
131 { UDATPG_HOUR_FIELD, {0x0074,0x0075,0x006E,0x0074,0x0069,0} }, /* "tunti" */
132 { UDATPG_FIELD_COUNT, {0} } /* terminator */
133};
134
46f4442e
A
135static void TestUsage() {
136 UErrorCode errorCode=U_ZERO_ERROR;
137 UDateTimePatternGenerator *dtpg;
57a6839d 138 const AppendItemNameData * appItemNameDataPtr;
46f4442e
A
139 UChar bestPattern[20];
140 UChar result[20];
141 int32_t length;
142 UChar *s;
143 const UChar *r;
144
145 dtpg=udatpg_open("fi", &errorCode);
146 if(U_FAILURE(errorCode)) {
729e4ab9 147 log_err_status(errorCode, "udatpg_open(fi) failed - %s\n", u_errorName(errorCode));
46f4442e
A
148 return;
149 }
150 length = udatpg_getBestPattern(dtpg, testSkeleton1, 4,
151 bestPattern, 20, &errorCode);
152 if(U_FAILURE(errorCode)) {
153 log_err("udatpg_getBestPattern failed - %s\n", u_errorName(errorCode));
154 return;
155 }
156 if((u_memcmp(bestPattern, expectingBestPattern, length)!=0) || bestPattern[length]!=0) {
157 log_err("udatpg_getBestPattern did not return the expected string\n");
158 return;
159 }
160
161
162 /* Test skeleton == NULL */
163 s=NULL;
164 length = udatpg_getBestPattern(dtpg, s, 0, bestPattern, 20, &errorCode);
165 if(!U_FAILURE(errorCode)&&(length!=0) ) {
166 log_err("udatpg_getBestPattern failed in illegal argument - skeleton is NULL.\n");
167 return;
168 }
169
170 /* Test udatpg_getSkeleton */
171 length = udatpg_getSkeleton(dtpg, testPattern, 5, result, 20, &errorCode);
172 if(U_FAILURE(errorCode)) {
173 log_err("udatpg_getSkeleton failed - %s\n", u_errorName(errorCode));
174 return;
175 }
176 if((u_memcmp(result, expectingSkeleton, length)!=0) || result[length]!=0) {
177 log_err("udatpg_getSkeleton did not return the expected string\n");
178 return;
179 }
180
181 /* Test pattern == NULL */
182 s=NULL;
183 length = udatpg_getSkeleton(dtpg, s, 0, result, 20, &errorCode);
184 if(!U_FAILURE(errorCode)&&(length!=0) ) {
185 log_err("udatpg_getSkeleton failed in illegal argument - pattern is NULL.\n");
186 return;
187 }
188
189 /* Test udatpg_getBaseSkeleton */
190 length = udatpg_getBaseSkeleton(dtpg, testPattern, 5, result, 20, &errorCode);
191 if(U_FAILURE(errorCode)) {
192 log_err("udatpg_getBaseSkeleton failed - %s\n", u_errorName(errorCode));
193 return;
194 }
195 if((u_memcmp(result, expectingBaseSkeleton, length)!=0) || result[length]!=0) {
196 log_err("udatpg_getBaseSkeleton did not return the expected string\n");
197 return;
198 }
199
200 /* Test pattern == NULL */
201 s=NULL;
202 length = udatpg_getBaseSkeleton(dtpg, s, 0, result, 20, &errorCode);
203 if(!U_FAILURE(errorCode)&&(length!=0) ) {
204 log_err("udatpg_getBaseSkeleton failed in illegal argument - pattern is NULL.\n");
205 return;
206 }
207
208 /* set append format to {1}{0} */
209 udatpg_setAppendItemFormat( dtpg, UDATPG_MONTH_FIELD, testFormat, 7 );
210 r = udatpg_getAppendItemFormat(dtpg, UDATPG_MONTH_FIELD, &length);
211
212
213 if(length!=7 || 0!=u_memcmp(r, testFormat, length) || r[length]!=0) {
214 log_err("udatpg_setAppendItemFormat did not return the expected string\n");
215 return;
216 }
217
57a6839d
A
218 for (appItemNameDataPtr = appendItemNameData; appItemNameDataPtr->field < UDATPG_FIELD_COUNT; appItemNameDataPtr++) {
219 int32_t nameLength;
220 const UChar * namePtr = udatpg_getAppendItemName(dtpg, appItemNameDataPtr->field, &nameLength);
221 if ( namePtr == NULL || u_strncmp(appItemNameDataPtr->name, namePtr, nameLength) != 0 ) {
222 log_err("udatpg_getAppendItemName returns invalid name for field %d\n", (int)appItemNameDataPtr->field);
223 }
224 }
225
46f4442e 226 /* set append name to hr */
2ca993e8 227 udatpg_setAppendItemName(dtpg, UDATPG_HOUR_FIELD, appendItemName, 2);
46f4442e
A
228 r = udatpg_getAppendItemName(dtpg, UDATPG_HOUR_FIELD, &length);
229
2ca993e8 230 if(length!=2 || 0!=u_memcmp(r, appendItemName, length) || r[length]!=0) {
46f4442e
A
231 log_err("udatpg_setAppendItemName did not return the expected string\n");
232 return;
233 }
234
235 /* set date time format to {1}{0} */
236 udatpg_setDateTimeFormat( dtpg, testFormat, 7 );
237 r = udatpg_getDateTimeFormat(dtpg, &length);
238
239 if(length!=7 || 0!=u_memcmp(r, testFormat, length) || r[length]!=0) {
240 log_err("udatpg_setDateTimeFormat did not return the expected string\n");
241 return;
242 }
243 udatpg_close(dtpg);
244}
245
246static void TestBuilder() {
247 UErrorCode errorCode=U_ZERO_ERROR;
248 UDateTimePatternGenerator *dtpg;
249 UDateTimePatternConflict conflict;
250 UEnumeration *en;
251 UChar result[20];
252 int32_t length, pLength;
253 const UChar *s, *p;
254 const UChar* ptrResult[2];
255 int32_t count=0;
256 UDateTimePatternGenerator *generator;
257 int32_t formattedCapacity, resultLen,patternCapacity ;
258 UChar pattern[40], formatted[40];
259 UDateFormat *formatter;
260 UDate sampleDate = 837039928046.0;
729e4ab9 261 static const char locale[]= "fr";
46f4442e
A
262 UErrorCode status=U_ZERO_ERROR;
263
264 /* test create an empty DateTimePatternGenerator */
265 dtpg=udatpg_openEmpty(&errorCode);
266 if(U_FAILURE(errorCode)) {
267 log_err("udatpg_openEmpty() failed - %s\n", u_errorName(errorCode));
268 return;
269 }
270
271 /* Add a pattern */
272 conflict = udatpg_addPattern(dtpg, redundantPattern, 5, FALSE, result, 20,
273 &length, &errorCode);
274 if(U_FAILURE(errorCode)) {
275 log_err("udatpg_addPattern() failed - %s\n", u_errorName(errorCode));
276 return;
277 }
278 /* Add a redundant pattern */
279 conflict = udatpg_addPattern(dtpg, redundantPattern, 5, FALSE, result, 20,
280 &length, &errorCode);
281 if(conflict == UDATPG_NO_CONFLICT) {
282 log_err("udatpg_addPattern() failed to find the duplicate pattern.\n");
283 return;
284 }
285 /* Test pattern == NULL */
286 s=NULL;
287 length = udatpg_addPattern(dtpg, s, 0, FALSE, result, 20,
288 &length, &errorCode);
289 if(!U_FAILURE(errorCode)&&(length!=0) ) {
290 log_err("udatpg_addPattern failed in illegal argument - pattern is NULL.\n");
291 return;
292 }
293
294 /* replace field type */
295 errorCode=U_ZERO_ERROR;
296 conflict = udatpg_addPattern(dtpg, testPattern2, 7, FALSE, result, 20,
297 &length, &errorCode);
298 if((conflict != UDATPG_NO_CONFLICT)||U_FAILURE(errorCode)) {
299 log_err("udatpg_addPattern() failed to add HH:mm v. - %s\n", u_errorName(errorCode));
300 return;
301 }
302 length = udatpg_replaceFieldTypes(dtpg, testPattern2, 7, replacedStr, 4,
303 result, 20, &errorCode);
304 if (U_FAILURE(errorCode) || (length==0) ) {
305 log_err("udatpg_replaceFieldTypes failed!\n");
306 return;
307 }
308
309 /* Get all skeletons and the crroespong pattern for each skeleton. */
310 ptrResult[0] = testPattern2;
311 ptrResult[1] = redundantPattern;
312 count=0;
313 en = udatpg_openSkeletons(dtpg, &errorCode);
314 if (U_FAILURE(errorCode) || (length==0) ) {
315 log_err("udatpg_openSkeletons failed!\n");
316 return;
317 }
318 while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
319 p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
320 if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, ptrResult[count], pLength)!=0 ) {
321 log_err("udatpg_getPatternForSkeleton failed!\n");
322 return;
323 }
324 count++;
325 }
326 uenum_close(en);
327
328 /* Get all baseSkeletons */
329 en = udatpg_openBaseSkeletons(dtpg, &errorCode);
330 count=0;
331 while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
332 p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
333 if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, resultBaseSkeletons[count], pLength)!=0 ) {
334 log_err("udatpg_getPatternForSkeleton failed!\n");
335 return;
336 }
337 count++;
338 }
339 if (U_FAILURE(errorCode) || (length==0) ) {
340 log_err("udatpg_openSkeletons failed!\n");
341 return;
342 }
343 uenum_close(en);
344
345 udatpg_close(dtpg);
346
347 /* sample code in Userguide */
2ca993e8 348 patternCapacity = UPRV_LENGTHOF(pattern);
46f4442e
A
349 status=U_ZERO_ERROR;
350 generator=udatpg_open(locale, &status);
351 if(U_FAILURE(status)) {
352 return;
353 }
354
355 /* get a pattern for an abbreviated month and day */
356 length = udatpg_getBestPattern(generator, skeleton, 4,
357 pattern, patternCapacity, &status);
51004dcb 358 formatter = udat_open(UDAT_PATTERN, UDAT_PATTERN, locale, timeZoneGMT, -1,
46f4442e
A
359 pattern, length, &status);
360 if (formatter==NULL) {
361 log_err("Failed to initialize the UDateFormat of the sample code in Userguide.\n");
362 udatpg_close(generator);
363 return;
364 }
365
366 /* use it to format (or parse) */
2ca993e8 367 formattedCapacity = UPRV_LENGTHOF(formatted);
46f4442e
A
368 resultLen=udat_format(formatter, ucal_getNow(), formatted, formattedCapacity,
369 NULL, &status);
370 /* for French, the result is "13 sept." */
371
372 /* cannot use the result from ucal_getNow() because the value change evreyday. */
373 resultLen=udat_format(formatter, sampleDate, formatted, formattedCapacity,
374 NULL, &status);
375 if ( u_memcmp(sampleFormatted, formatted, resultLen) != 0 ) {
376 log_err("Failed udat_format() of sample code in Userguide.\n");
377 }
378 udatpg_close(generator);
379 udat_close(formatter);
380}
381
729e4ab9
A
382typedef struct DTPtnGenOptionsData {
383 const char * locale;
384 const UChar * skel;
385 UDateTimePatternMatchOptions options;
386 const UChar * expectedPattern;
387} DTPtnGenOptionsData;
388enum { kTestOptionsPatLenMax = 32 };
389
390static const UChar skel_Hmm[] = { 0x0048, 0x006D, 0x006D, 0 };
391static const UChar skel_HHmm[] = { 0x0048, 0x0048, 0x006D, 0x006D, 0 };
392static const UChar skel_hhmm[] = { 0x0068, 0x0068, 0x006D, 0x006D, 0 };
3bb97ae2
A
393static const UChar skel_mmss[] = { 0x006D, 0x006D, 0x0073, 0x0073, 0 };
394static const UChar skel_mmssSS[] = { 0x006D, 0x006D, 0x0073, 0x0073, 0x0053, 0x0053, 0 };
729e4ab9
A
395static const UChar patn_hcmm_a[] = { 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h:mm a */
396static const UChar patn_HHcmm[] = { 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0 }; /* HH:mm */
397static const UChar patn_hhcmm_a[] = { 0x0068, 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh:mm a */
398static const UChar patn_HHpmm[] = { 0x0048, 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* HH.mm */
399static const UChar patn_hpmm_a[] = { 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h.mm a */
400static const UChar patn_Hpmm[] = { 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* H.mm */
401static const UChar patn_hhpmm_a[] = { 0x0068, 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh.mm a */
3bb97ae2
A
402static const UChar patn_mmcss[] = { 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0 }; /* mm:ss */
403static const UChar patn_mmcsspSS[]= { 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0x002E, 0x0053, 0x0053, 0 }; /* mm:ss.SS */
729e4ab9
A
404
405static void TestOptions() {
406 const DTPtnGenOptionsData testData[] = {
407 /*loc skel options expectedPattern */
408 { "en", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm },
409 { "en", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm },
410 { "en", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hcmm_a },
411 { "en", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm },
412 { "en", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm },
413 { "en", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhcmm_a },
2ca993e8
A
414 { "da", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm },
415 { "da", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm },
416 { "da", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hpmm_a },
417 { "da", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_Hpmm },
418 { "da", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHpmm },
419 { "da", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhpmm_a },
3bb97ae2
A
420 { "en_JP@calendar=japanese", skel_mmss, UDATPG_MATCH_NO_OPTIONS, patn_mmcss },
421 { "en_JP@calendar=japanese", skel_mmssSS, UDATPG_MATCH_NO_OPTIONS, patn_mmcsspSS },
729e4ab9
A
422 };
423
2ca993e8 424 int count = UPRV_LENGTHOF(testData);
729e4ab9
A
425 const DTPtnGenOptionsData * testDataPtr = testData;
426
427 for (; count-- > 0; ++testDataPtr) {
428 UErrorCode status = U_ZERO_ERROR;
429 UDateTimePatternGenerator * dtpgen = udatpg_open(testDataPtr->locale, &status);
430 if ( U_SUCCESS(status) ) {
431 UChar pattern[kTestOptionsPatLenMax];
432 int32_t patLen = udatpg_getBestPatternWithOptions(dtpgen, testDataPtr->skel, -1,
433 testDataPtr->options, pattern,
434 kTestOptionsPatLenMax, &status);
435 if ( U_FAILURE(status) || u_strncmp(pattern, testDataPtr->expectedPattern, patLen+1) != 0 ) {
436 char skelBytes[kTestOptionsPatLenMax];
437 char expectedPatternBytes[kTestOptionsPatLenMax];
438 char patternBytes[kTestOptionsPatLenMax];
439 log_err("ERROR udatpg_getBestPatternWithOptions, locale %s, skeleton %s, options 0x%04X, expected pattern %s, got %s, status %d\n",
440 testDataPtr->locale, u_austrncpy(skelBytes,testDataPtr->skel,kTestOptionsPatLenMax), testDataPtr->options,
441 u_austrncpy(expectedPatternBytes,testDataPtr->expectedPattern,kTestOptionsPatLenMax),
442 u_austrncpy(patternBytes,pattern,kTestOptionsPatLenMax), status );
443 }
444 udatpg_close(dtpgen);
445 } else {
446 log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr->locale, myErrorName(status));
447 }
448 }
449}
450
0f5d89e8
A
451typedef struct FieldDisplayNameData {
452 const char * locale;
453 UDateTimePatternField field;
454 UDateTimePGDisplayWidth width;
455 const char * expected;
456} FieldDisplayNameData;
457enum { kFieldDisplayNameMax = 32, kFieldDisplayNameBytesMax = 64};
458
459static void TestGetFieldDisplayNames() {
460 const FieldDisplayNameData testData[] = {
461 /*loc field width expectedName */
462 { "de", UDATPG_QUARTER_FIELD, UDATPG_WIDE, "Quartal" },
463 { "de", UDATPG_QUARTER_FIELD, UDATPG_ABBREVIATED, "Quart." },
464 { "de", UDATPG_QUARTER_FIELD, UDATPG_NARROW, "Q" },
465 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_WIDE, "weekday of the month" },
466 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_ABBREVIATED, "wkday. of mo." },
467 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_NARROW, "wkday. of mo." }, // fallback
468 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_WIDE, "weekday of the month" },
469 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_ABBREVIATED, "wkday of mo" }, // override
470 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_NARROW, "wkday of mo" },
471 { "it", UDATPG_SECOND_FIELD, UDATPG_WIDE, "secondo" },
472 { "it", UDATPG_SECOND_FIELD, UDATPG_ABBREVIATED, "s" },
473 { "it", UDATPG_SECOND_FIELD, UDATPG_NARROW, "s" },
474 };
475
476 int count = UPRV_LENGTHOF(testData);
477 const FieldDisplayNameData * testDataPtr = testData;
478 for (; count-- > 0; ++testDataPtr) {
479 UErrorCode status = U_ZERO_ERROR;
480 UDateTimePatternGenerator * dtpgen = udatpg_open(testDataPtr->locale, &status);
481 if ( U_FAILURE(status) ) {
482 log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr->locale, myErrorName(status));
483 } else {
484 UChar expName[kFieldDisplayNameMax];
485 UChar getName[kFieldDisplayNameMax];
486 u_unescape(testDataPtr->expected, expName, kFieldDisplayNameMax);
487
488 int32_t getLen = udatpg_getFieldDisplayName(dtpgen, testDataPtr->field, testDataPtr->width,
489 getName, kFieldDisplayNameMax, &status);
490 if ( U_FAILURE(status) ) {
491 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, got status %s, len %d\n",
492 testDataPtr->locale, testDataPtr->field, testDataPtr->width, u_errorName(status), getLen);
493 } else if ( u_strncmp(expName, getName, kFieldDisplayNameMax) != 0 ) {
494 char expNameB[kFieldDisplayNameBytesMax];
495 char getNameB[kFieldDisplayNameBytesMax];
496 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, expected %s, got %s, status %s\n",
497 testDataPtr->locale, testDataPtr->field, testDataPtr->width,
498 u_austrncpy(expNameB,expName,kFieldDisplayNameBytesMax),
499 u_austrncpy(getNameB,getName,kFieldDisplayNameBytesMax), u_errorName(status) );
500 } else if (testDataPtr->width == UDATPG_WIDE && getLen > 1) {
501 // test preflight & inadequate buffer
502 int32_t getNewLen;
503 status = U_ZERO_ERROR;
504 getNewLen = udatpg_getFieldDisplayName(dtpgen, testDataPtr->field, UDATPG_WIDE, NULL, 0, &status);
505 if (U_FAILURE(status) || getNewLen != getLen) {
506 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, preflight expected len %d, got %d, status %s\n",
507 testDataPtr->locale, testDataPtr->field, testDataPtr->width, getLen, getNewLen, u_errorName(status) );
508 }
509 status = U_ZERO_ERROR;
510 getNewLen = udatpg_getFieldDisplayName(dtpgen, testDataPtr->field, UDATPG_WIDE, getName, getLen-1, &status);
511 if (status!=U_BUFFER_OVERFLOW_ERROR || getNewLen != getLen) {
512 log_err("ERROR udatpg_getFieldDisplayName locale %s field %d width %d, overflow expected len %d & BUFFER_OVERFLOW_ERROR, got %d & status %s\n",
513 testDataPtr->locale, testDataPtr->field, testDataPtr->width, getLen, getNewLen, u_errorName(status) );
514 }
515 }
516 udatpg_close(dtpgen);
517 }
518 }
519}
520
3d1f044b
A
521enum { kUFmtMax = 64, kBFmtMax = 128 };
522static void TestJapaneseCalendarItems(void) { // rdar://52042600
523 static const UChar* jaJpnCalSkelAndFmt[][2] = {
524 { u"yMd", u"GGGGGy/MM/dd" },
525 { u"GGGGGyMd", u"GGGGGy/MM/dd" },
526 { u"GyMd", u"GGGGGy/MM/dd" },
527 { u"yyMMdd", u"GGGGGy/MM/dd" },
528 //{ u"GGGGGyyMMdd", u"GGGGGy/MM/dd" },
529 { u"GyyMMdd", u"GGGGGy/MM/dd" },
530 { u"yyMMEdd", u"GGGGGy/MM/dd(EEE)" },
531 { u"GGGGGyyMMEdd", u"GGGGGy/MM/dd(EEE)" },
532 { u"yyMEdjmma", u"GGGGGy/MM/dd(EEE) H:mm" },
533 { NULL, NULL }
534 };
535 UErrorCode status = U_ZERO_ERROR;
536 UDateTimePatternGenerator* udatpg = udatpg_open("ja@calendar=japanese", &status);
537 if ( U_FAILURE(status) ) {
538 log_data_err("FAIL udatpg_open failed for locale ja@calendar=japanese : %s\n", myErrorName(status));
539 } else {
540 int32_t idx;
541 for (idx = 0; jaJpnCalSkelAndFmt[idx][0] != NULL; idx++) {
542 UChar uget[kUFmtMax];
543 char bskel[kBFmtMax];
544 status = U_ZERO_ERROR;
545 u_strToUTF8(bskel, kBFmtMax, NULL, jaJpnCalSkelAndFmt[idx][0], -1, &status);
546 int32_t ulen = udatpg_getBestPattern(udatpg, jaJpnCalSkelAndFmt[idx][0], -1, uget, kUFmtMax, &status);
547 if ( U_FAILURE(status) ) {
548 log_data_err("FAIL udatpg_getBestPattern status for skeleton %s : %s\n", bskel);
549 } else if (u_strcmp(uget,jaJpnCalSkelAndFmt[idx][1]) != 0) {
550 char bexp[kBFmtMax];
551 char bget[kBFmtMax];
552 u_strToUTF8(bexp, kBFmtMax, NULL, jaJpnCalSkelAndFmt[idx][1], -1, &status);
553 u_strToUTF8(bget, kBFmtMax, NULL, uget, ulen, &status);
554 log_data_err("ERROR udatpg_getBestPattern for skeleton %s, expect %s, get %s\n", bskel, bexp, bget);
555 }
556 }
557 udatpg_close(udatpg);
558 }
559}
560
46f4442e 561#endif