]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/udatpg_test.c
ICU-57163.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / udatpg_test.c
CommitLineData
46f4442e
A
1/*
2*******************************************************************************
3*
2ca993e8 4* Copyright (C) 2007-2016, International Business Machines
46f4442e
A
5* Corporation and others. All Rights Reserved.
6*
7*******************************************************************************
8* file name: udatpg_test.c
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2007aug01
14* created by: Markus W. Scherer
15*
16* Test of the C wrapper for the DateTimePatternGenerator.
17* Calls each C API function and exercises code paths in the wrapper,
18* but the full functionality is tested in the C++ intltest.
19*
20* One item to note: C API functions which return a const UChar *
21* should return a NUL-terminated string.
22* (The C++ implementation needs to use getTerminatedBuffer()
23* on UnicodeString objects which end up being returned this way.)
24*/
25
26#include "unicode/utypes.h"
27
28#if !UCONFIG_NO_FORMATTING
29#include "unicode/udat.h"
30#include "unicode/udatpg.h"
31#include "unicode/ustring.h"
32#include "cintltst.h"
2ca993e8 33#include "cmemory.h"
46f4442e
A
34
35void addDateTimePatternGeneratorTest(TestNode** root);
36
37#define TESTCASE(x) addTest(root, &x, "tsformat/udatpg_test/" #x)
38
39static void TestOpenClose(void);
40static void TestUsage(void);
41static void TestBuilder(void);
729e4ab9 42static void TestOptions(void);
46f4442e
A
43
44void addDateTimePatternGeneratorTest(TestNode** root) {
45 TESTCASE(TestOpenClose);
46 TESTCASE(TestUsage);
47 TESTCASE(TestBuilder);
729e4ab9 48 TESTCASE(TestOptions);
46f4442e
A
49}
50
51/*
52 * Pipe symbol '|'. We pass only the first UChar without NUL-termination.
53 * The second UChar is just to verify that the API does not pick that up.
54 */
55static const UChar pipeString[]={ 0x7c, 0x0a };
56
57static const UChar testSkeleton1[]={ 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
729e4ab9 58static const UChar expectingBestPattern[]={ 0x48, 0x2e, 0x6d, 0x6d, 0 }; /* H.mm */
46f4442e
A
59static const UChar testPattern[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0 }; /* HH:mm */
60static const UChar expectingSkeleton[]= { 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
61static const UChar expectingBaseSkeleton[]= { 0x48, 0x6d, 0 }; /* HHmm */
62static const UChar redundantPattern[]={ 0x79, 0x79, 0x4d, 0x4d, 0x4d, 0 }; /* yyMMM */
63static const UChar testFormat[]= {0x7B, 0x31, 0x7D, 0x20, 0x7B, 0x30, 0x7D, 0}; /* {1} {0} */
64static const UChar appendItemName[]= {0x68, 0x72, 0}; /* hr */
65static const UChar testPattern2[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0x20, 0x76, 0 }; /* HH:mm v */
66static const UChar replacedStr[]={ 0x76, 0x76, 0x76, 0x76, 0 }; /* vvvv */
67/* results for getBaseSkeletons() - {Hmv}, {yMMM} */
68static const UChar resultBaseSkeletons[2][10] = {{0x48,0x6d, 0x76, 0}, {0x79, 0x4d, 0x4d, 0x4d, 0 } };
69static const UChar sampleFormatted[] = {0x31, 0x30, 0x20, 0x6A, 0x75, 0x69, 0x6C, 0x2E, 0}; /* 10 juil. */
70static const UChar skeleton[]= {0x4d, 0x4d, 0x4d, 0x64, 0}; /* MMMd */
71static const UChar timeZoneGMT[] = { 0x0047, 0x004d, 0x0054, 0x0000 }; /* "GMT" */
72
73static void TestOpenClose() {
74 UErrorCode errorCode=U_ZERO_ERROR;
75 UDateTimePatternGenerator *dtpg, *dtpg2;
76 const UChar *s;
77 int32_t length;
78
79 /* Open a DateTimePatternGenerator for the default locale. */
80 dtpg=udatpg_open(NULL, &errorCode);
81 if(U_FAILURE(errorCode)) {
729e4ab9 82 log_err_status(errorCode, "udatpg_open(NULL) failed - %s\n", u_errorName(errorCode));
46f4442e
A
83 return;
84 }
85 udatpg_close(dtpg);
86
87 /* Now one for German. */
88 dtpg=udatpg_open("de", &errorCode);
89 if(U_FAILURE(errorCode)) {
90 log_err("udatpg_open(de) failed - %s\n", u_errorName(errorCode));
91 return;
92 }
93
94 /* Make some modification which we verify gets passed on to the clone. */
95 udatpg_setDecimal(dtpg, pipeString, 1);
96
97 /* Clone the generator. */
98 dtpg2=udatpg_clone(dtpg, &errorCode);
99 if(U_FAILURE(errorCode) || dtpg2==NULL) {
100 log_err("udatpg_clone() failed - %s\n", u_errorName(errorCode));
101 return;
102 }
103
104 /* Verify that the clone has the custom decimal symbol. */
105 s=udatpg_getDecimal(dtpg2, &length);
106 if(s==pipeString || length!=1 || 0!=u_memcmp(s, pipeString, length) || s[length]!=0) {
107 log_err("udatpg_getDecimal(cloned object) did not return the expected string\n");
108 return;
109 }
110
111 udatpg_close(dtpg);
112 udatpg_close(dtpg2);
113}
114
57a6839d
A
115typedef struct {
116 UDateTimePatternField field;
117 UChar name[12];
118} AppendItemNameData;
119
120static const AppendItemNameData appendItemNameData[] = { /* for Finnish */
121 { UDATPG_YEAR_FIELD, {0x0076,0x0075,0x006F,0x0073,0x0069,0} }, /* "vuosi" */
122 { UDATPG_MONTH_FIELD, {0x006B,0x0075,0x0075,0x006B,0x0061,0x0075,0x0073,0x0069,0} }, /* "kuukausi" */
123 { UDATPG_WEEKDAY_FIELD, {0x0076,0x0069,0x0069,0x006B,0x006F,0x006E,0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
124 { UDATPG_DAY_FIELD, {0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
125 { UDATPG_HOUR_FIELD, {0x0074,0x0075,0x006E,0x0074,0x0069,0} }, /* "tunti" */
126 { UDATPG_FIELD_COUNT, {0} } /* terminator */
127};
128
46f4442e
A
129static void TestUsage() {
130 UErrorCode errorCode=U_ZERO_ERROR;
131 UDateTimePatternGenerator *dtpg;
57a6839d 132 const AppendItemNameData * appItemNameDataPtr;
46f4442e
A
133 UChar bestPattern[20];
134 UChar result[20];
135 int32_t length;
136 UChar *s;
137 const UChar *r;
138
139 dtpg=udatpg_open("fi", &errorCode);
140 if(U_FAILURE(errorCode)) {
729e4ab9 141 log_err_status(errorCode, "udatpg_open(fi) failed - %s\n", u_errorName(errorCode));
46f4442e
A
142 return;
143 }
144 length = udatpg_getBestPattern(dtpg, testSkeleton1, 4,
145 bestPattern, 20, &errorCode);
146 if(U_FAILURE(errorCode)) {
147 log_err("udatpg_getBestPattern failed - %s\n", u_errorName(errorCode));
148 return;
149 }
150 if((u_memcmp(bestPattern, expectingBestPattern, length)!=0) || bestPattern[length]!=0) {
151 log_err("udatpg_getBestPattern did not return the expected string\n");
152 return;
153 }
154
155
156 /* Test skeleton == NULL */
157 s=NULL;
158 length = udatpg_getBestPattern(dtpg, s, 0, bestPattern, 20, &errorCode);
159 if(!U_FAILURE(errorCode)&&(length!=0) ) {
160 log_err("udatpg_getBestPattern failed in illegal argument - skeleton is NULL.\n");
161 return;
162 }
163
164 /* Test udatpg_getSkeleton */
165 length = udatpg_getSkeleton(dtpg, testPattern, 5, result, 20, &errorCode);
166 if(U_FAILURE(errorCode)) {
167 log_err("udatpg_getSkeleton failed - %s\n", u_errorName(errorCode));
168 return;
169 }
170 if((u_memcmp(result, expectingSkeleton, length)!=0) || result[length]!=0) {
171 log_err("udatpg_getSkeleton did not return the expected string\n");
172 return;
173 }
174
175 /* Test pattern == NULL */
176 s=NULL;
177 length = udatpg_getSkeleton(dtpg, s, 0, result, 20, &errorCode);
178 if(!U_FAILURE(errorCode)&&(length!=0) ) {
179 log_err("udatpg_getSkeleton failed in illegal argument - pattern is NULL.\n");
180 return;
181 }
182
183 /* Test udatpg_getBaseSkeleton */
184 length = udatpg_getBaseSkeleton(dtpg, testPattern, 5, result, 20, &errorCode);
185 if(U_FAILURE(errorCode)) {
186 log_err("udatpg_getBaseSkeleton failed - %s\n", u_errorName(errorCode));
187 return;
188 }
189 if((u_memcmp(result, expectingBaseSkeleton, length)!=0) || result[length]!=0) {
190 log_err("udatpg_getBaseSkeleton did not return the expected string\n");
191 return;
192 }
193
194 /* Test pattern == NULL */
195 s=NULL;
196 length = udatpg_getBaseSkeleton(dtpg, s, 0, result, 20, &errorCode);
197 if(!U_FAILURE(errorCode)&&(length!=0) ) {
198 log_err("udatpg_getBaseSkeleton failed in illegal argument - pattern is NULL.\n");
199 return;
200 }
201
202 /* set append format to {1}{0} */
203 udatpg_setAppendItemFormat( dtpg, UDATPG_MONTH_FIELD, testFormat, 7 );
204 r = udatpg_getAppendItemFormat(dtpg, UDATPG_MONTH_FIELD, &length);
205
206
207 if(length!=7 || 0!=u_memcmp(r, testFormat, length) || r[length]!=0) {
208 log_err("udatpg_setAppendItemFormat did not return the expected string\n");
209 return;
210 }
211
57a6839d
A
212 for (appItemNameDataPtr = appendItemNameData; appItemNameDataPtr->field < UDATPG_FIELD_COUNT; appItemNameDataPtr++) {
213 int32_t nameLength;
214 const UChar * namePtr = udatpg_getAppendItemName(dtpg, appItemNameDataPtr->field, &nameLength);
215 if ( namePtr == NULL || u_strncmp(appItemNameDataPtr->name, namePtr, nameLength) != 0 ) {
216 log_err("udatpg_getAppendItemName returns invalid name for field %d\n", (int)appItemNameDataPtr->field);
217 }
218 }
219
46f4442e 220 /* set append name to hr */
2ca993e8 221 udatpg_setAppendItemName(dtpg, UDATPG_HOUR_FIELD, appendItemName, 2);
46f4442e
A
222 r = udatpg_getAppendItemName(dtpg, UDATPG_HOUR_FIELD, &length);
223
2ca993e8 224 if(length!=2 || 0!=u_memcmp(r, appendItemName, length) || r[length]!=0) {
46f4442e
A
225 log_err("udatpg_setAppendItemName did not return the expected string\n");
226 return;
227 }
228
229 /* set date time format to {1}{0} */
230 udatpg_setDateTimeFormat( dtpg, testFormat, 7 );
231 r = udatpg_getDateTimeFormat(dtpg, &length);
232
233 if(length!=7 || 0!=u_memcmp(r, testFormat, length) || r[length]!=0) {
234 log_err("udatpg_setDateTimeFormat did not return the expected string\n");
235 return;
236 }
237 udatpg_close(dtpg);
238}
239
240static void TestBuilder() {
241 UErrorCode errorCode=U_ZERO_ERROR;
242 UDateTimePatternGenerator *dtpg;
243 UDateTimePatternConflict conflict;
244 UEnumeration *en;
245 UChar result[20];
246 int32_t length, pLength;
247 const UChar *s, *p;
248 const UChar* ptrResult[2];
249 int32_t count=0;
250 UDateTimePatternGenerator *generator;
251 int32_t formattedCapacity, resultLen,patternCapacity ;
252 UChar pattern[40], formatted[40];
253 UDateFormat *formatter;
254 UDate sampleDate = 837039928046.0;
729e4ab9 255 static const char locale[]= "fr";
46f4442e
A
256 UErrorCode status=U_ZERO_ERROR;
257
258 /* test create an empty DateTimePatternGenerator */
259 dtpg=udatpg_openEmpty(&errorCode);
260 if(U_FAILURE(errorCode)) {
261 log_err("udatpg_openEmpty() failed - %s\n", u_errorName(errorCode));
262 return;
263 }
264
265 /* Add a pattern */
266 conflict = udatpg_addPattern(dtpg, redundantPattern, 5, FALSE, result, 20,
267 &length, &errorCode);
268 if(U_FAILURE(errorCode)) {
269 log_err("udatpg_addPattern() failed - %s\n", u_errorName(errorCode));
270 return;
271 }
272 /* Add a redundant pattern */
273 conflict = udatpg_addPattern(dtpg, redundantPattern, 5, FALSE, result, 20,
274 &length, &errorCode);
275 if(conflict == UDATPG_NO_CONFLICT) {
276 log_err("udatpg_addPattern() failed to find the duplicate pattern.\n");
277 return;
278 }
279 /* Test pattern == NULL */
280 s=NULL;
281 length = udatpg_addPattern(dtpg, s, 0, FALSE, result, 20,
282 &length, &errorCode);
283 if(!U_FAILURE(errorCode)&&(length!=0) ) {
284 log_err("udatpg_addPattern failed in illegal argument - pattern is NULL.\n");
285 return;
286 }
287
288 /* replace field type */
289 errorCode=U_ZERO_ERROR;
290 conflict = udatpg_addPattern(dtpg, testPattern2, 7, FALSE, result, 20,
291 &length, &errorCode);
292 if((conflict != UDATPG_NO_CONFLICT)||U_FAILURE(errorCode)) {
293 log_err("udatpg_addPattern() failed to add HH:mm v. - %s\n", u_errorName(errorCode));
294 return;
295 }
296 length = udatpg_replaceFieldTypes(dtpg, testPattern2, 7, replacedStr, 4,
297 result, 20, &errorCode);
298 if (U_FAILURE(errorCode) || (length==0) ) {
299 log_err("udatpg_replaceFieldTypes failed!\n");
300 return;
301 }
302
303 /* Get all skeletons and the crroespong pattern for each skeleton. */
304 ptrResult[0] = testPattern2;
305 ptrResult[1] = redundantPattern;
306 count=0;
307 en = udatpg_openSkeletons(dtpg, &errorCode);
308 if (U_FAILURE(errorCode) || (length==0) ) {
309 log_err("udatpg_openSkeletons failed!\n");
310 return;
311 }
312 while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
313 p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
314 if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, ptrResult[count], pLength)!=0 ) {
315 log_err("udatpg_getPatternForSkeleton failed!\n");
316 return;
317 }
318 count++;
319 }
320 uenum_close(en);
321
322 /* Get all baseSkeletons */
323 en = udatpg_openBaseSkeletons(dtpg, &errorCode);
324 count=0;
325 while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
326 p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
327 if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, resultBaseSkeletons[count], pLength)!=0 ) {
328 log_err("udatpg_getPatternForSkeleton failed!\n");
329 return;
330 }
331 count++;
332 }
333 if (U_FAILURE(errorCode) || (length==0) ) {
334 log_err("udatpg_openSkeletons failed!\n");
335 return;
336 }
337 uenum_close(en);
338
339 udatpg_close(dtpg);
340
341 /* sample code in Userguide */
2ca993e8 342 patternCapacity = UPRV_LENGTHOF(pattern);
46f4442e
A
343 status=U_ZERO_ERROR;
344 generator=udatpg_open(locale, &status);
345 if(U_FAILURE(status)) {
346 return;
347 }
348
349 /* get a pattern for an abbreviated month and day */
350 length = udatpg_getBestPattern(generator, skeleton, 4,
351 pattern, patternCapacity, &status);
51004dcb 352 formatter = udat_open(UDAT_PATTERN, UDAT_PATTERN, locale, timeZoneGMT, -1,
46f4442e
A
353 pattern, length, &status);
354 if (formatter==NULL) {
355 log_err("Failed to initialize the UDateFormat of the sample code in Userguide.\n");
356 udatpg_close(generator);
357 return;
358 }
359
360 /* use it to format (or parse) */
2ca993e8 361 formattedCapacity = UPRV_LENGTHOF(formatted);
46f4442e
A
362 resultLen=udat_format(formatter, ucal_getNow(), formatted, formattedCapacity,
363 NULL, &status);
364 /* for French, the result is "13 sept." */
365
366 /* cannot use the result from ucal_getNow() because the value change evreyday. */
367 resultLen=udat_format(formatter, sampleDate, formatted, formattedCapacity,
368 NULL, &status);
369 if ( u_memcmp(sampleFormatted, formatted, resultLen) != 0 ) {
370 log_err("Failed udat_format() of sample code in Userguide.\n");
371 }
372 udatpg_close(generator);
373 udat_close(formatter);
374}
375
729e4ab9
A
376typedef struct DTPtnGenOptionsData {
377 const char * locale;
378 const UChar * skel;
379 UDateTimePatternMatchOptions options;
380 const UChar * expectedPattern;
381} DTPtnGenOptionsData;
382enum { kTestOptionsPatLenMax = 32 };
383
384static const UChar skel_Hmm[] = { 0x0048, 0x006D, 0x006D, 0 };
385static const UChar skel_HHmm[] = { 0x0048, 0x0048, 0x006D, 0x006D, 0 };
386static const UChar skel_hhmm[] = { 0x0068, 0x0068, 0x006D, 0x006D, 0 };
729e4ab9
A
387static const UChar patn_hcmm_a[] = { 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h:mm a */
388static const UChar patn_HHcmm[] = { 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0 }; /* HH:mm */
389static const UChar patn_hhcmm_a[] = { 0x0068, 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh:mm a */
390static const UChar patn_HHpmm[] = { 0x0048, 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* HH.mm */
391static const UChar patn_hpmm_a[] = { 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h.mm a */
392static const UChar patn_Hpmm[] = { 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* H.mm */
393static const UChar patn_hhpmm_a[] = { 0x0068, 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh.mm a */
394
395static void TestOptions() {
396 const DTPtnGenOptionsData testData[] = {
397 /*loc skel options expectedPattern */
398 { "en", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm },
399 { "en", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm },
400 { "en", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hcmm_a },
401 { "en", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm },
402 { "en", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm },
403 { "en", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhcmm_a },
2ca993e8
A
404 { "da", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm },
405 { "da", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm },
406 { "da", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hpmm_a },
407 { "da", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_Hpmm },
408 { "da", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHpmm },
409 { "da", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhpmm_a },
729e4ab9
A
410 };
411
2ca993e8 412 int count = UPRV_LENGTHOF(testData);
729e4ab9
A
413 const DTPtnGenOptionsData * testDataPtr = testData;
414
415 for (; count-- > 0; ++testDataPtr) {
416 UErrorCode status = U_ZERO_ERROR;
417 UDateTimePatternGenerator * dtpgen = udatpg_open(testDataPtr->locale, &status);
418 if ( U_SUCCESS(status) ) {
419 UChar pattern[kTestOptionsPatLenMax];
420 int32_t patLen = udatpg_getBestPatternWithOptions(dtpgen, testDataPtr->skel, -1,
421 testDataPtr->options, pattern,
422 kTestOptionsPatLenMax, &status);
423 if ( U_FAILURE(status) || u_strncmp(pattern, testDataPtr->expectedPattern, patLen+1) != 0 ) {
424 char skelBytes[kTestOptionsPatLenMax];
425 char expectedPatternBytes[kTestOptionsPatLenMax];
426 char patternBytes[kTestOptionsPatLenMax];
427 log_err("ERROR udatpg_getBestPatternWithOptions, locale %s, skeleton %s, options 0x%04X, expected pattern %s, got %s, status %d\n",
428 testDataPtr->locale, u_austrncpy(skelBytes,testDataPtr->skel,kTestOptionsPatLenMax), testDataPtr->options,
429 u_austrncpy(expectedPatternBytes,testDataPtr->expectedPattern,kTestOptionsPatLenMax),
430 u_austrncpy(patternBytes,pattern,kTestOptionsPatLenMax), status );
431 }
432 udatpg_close(dtpgen);
433 } else {
434 log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr->locale, myErrorName(status));
435 }
436 }
437}
438
46f4442e 439#endif