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