2 *******************************************************************************
4 * Copyright (C) 2007-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: udatpg_test.c
10 * tab size: 8 (not used)
13 * created on: 2007aug01
14 * created by: Markus W. Scherer
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.
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.)
26 #include "unicode/utypes.h"
28 #if !UCONFIG_NO_FORMATTING
29 #include "unicode/udat.h"
30 #include "unicode/udatpg.h"
31 #include "unicode/ustring.h"
35 void addDateTimePatternGeneratorTest(TestNode
** root
);
37 #define TESTCASE(x) addTest(root, &x, "tsformat/udatpg_test/" #x)
39 static void TestOpenClose(void);
40 static void TestUsage(void);
41 static void TestBuilder(void);
42 static void TestOptions(void);
44 void addDateTimePatternGeneratorTest(TestNode
** root
) {
45 TESTCASE(TestOpenClose
);
47 TESTCASE(TestBuilder
);
48 TESTCASE(TestOptions
);
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.
55 static const UChar pipeString
[]={ 0x7c, 0x0a };
57 static const UChar testSkeleton1
[]={ 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
58 static const UChar expectingBestPattern
[]={ 0x48, 0x2e, 0x6d, 0x6d, 0 }; /* H.mm */
59 static const UChar testPattern
[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0 }; /* HH:mm */
60 static const UChar expectingSkeleton
[]= { 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
61 static const UChar expectingBaseSkeleton
[]= { 0x48, 0x6d, 0 }; /* HHmm */
62 static const UChar redundantPattern
[]={ 0x79, 0x79, 0x4d, 0x4d, 0x4d, 0 }; /* yyMMM */
63 static const UChar testFormat
[]= {0x7B, 0x31, 0x7D, 0x20, 0x7B, 0x30, 0x7D, 0}; /* {1} {0} */
64 static const UChar appendItemName
[]= {0x68, 0x72, 0}; /* hr */
65 static const UChar testPattern2
[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0x20, 0x76, 0 }; /* HH:mm v */
66 static const UChar replacedStr
[]={ 0x76, 0x76, 0x76, 0x76, 0 }; /* vvvv */
67 /* results for getBaseSkeletons() - {Hmv}, {yMMM} */
68 static const UChar resultBaseSkeletons
[2][10] = {{0x48,0x6d, 0x76, 0}, {0x79, 0x4d, 0x4d, 0x4d, 0 } };
69 static const UChar sampleFormatted
[] = {0x31, 0x30, 0x20, 0x6A, 0x75, 0x69, 0x6C, 0x2E, 0}; /* 10 juil. */
70 static const UChar skeleton
[]= {0x4d, 0x4d, 0x4d, 0x64, 0}; /* MMMd */
71 static const UChar timeZoneGMT
[] = { 0x0047, 0x004d, 0x0054, 0x0000 }; /* "GMT" */
73 static void TestOpenClose() {
74 UErrorCode errorCode
=U_ZERO_ERROR
;
75 UDateTimePatternGenerator
*dtpg
, *dtpg2
;
79 /* Open a DateTimePatternGenerator for the default locale. */
80 dtpg
=udatpg_open(NULL
, &errorCode
);
81 if(U_FAILURE(errorCode
)) {
82 log_err_status(errorCode
, "udatpg_open(NULL) failed - %s\n", u_errorName(errorCode
));
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
));
94 /* Make some modification which we verify gets passed on to the clone. */
95 udatpg_setDecimal(dtpg
, pipeString
, 1);
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
));
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");
116 UDateTimePatternField field
;
118 } AppendItemNameData
;
120 static 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 */
129 static void TestUsage() {
130 UErrorCode errorCode
=U_ZERO_ERROR
;
131 UDateTimePatternGenerator
*dtpg
;
132 const AppendItemNameData
* appItemNameDataPtr
;
133 UChar bestPattern
[20];
139 dtpg
=udatpg_open("fi", &errorCode
);
140 if(U_FAILURE(errorCode
)) {
141 log_err_status(errorCode
, "udatpg_open(fi) failed - %s\n", u_errorName(errorCode
));
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
));
150 if((u_memcmp(bestPattern
, expectingBestPattern
, length
)!=0) || bestPattern
[length
]!=0) {
151 log_err("udatpg_getBestPattern did not return the expected string\n");
156 /* Test skeleton == 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");
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
));
170 if((u_memcmp(result
, expectingSkeleton
, length
)!=0) || result
[length
]!=0) {
171 log_err("udatpg_getSkeleton did not return the expected string\n");
175 /* Test pattern == 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");
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
));
189 if((u_memcmp(result
, expectingBaseSkeleton
, length
)!=0) || result
[length
]!=0) {
190 log_err("udatpg_getBaseSkeleton did not return the expected string\n");
194 /* Test pattern == 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");
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
);
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");
212 for (appItemNameDataPtr
= appendItemNameData
; appItemNameDataPtr
->field
< UDATPG_FIELD_COUNT
; appItemNameDataPtr
++) {
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
);
220 /* set append name to hr */
221 udatpg_setAppendItemName(dtpg
, UDATPG_HOUR_FIELD
, appendItemName
, 2);
222 r
= udatpg_getAppendItemName(dtpg
, UDATPG_HOUR_FIELD
, &length
);
224 if(length
!=2 || 0!=u_memcmp(r
, appendItemName
, length
) || r
[length
]!=0) {
225 log_err("udatpg_setAppendItemName did not return the expected string\n");
229 /* set date time format to {1}{0} */
230 udatpg_setDateTimeFormat( dtpg
, testFormat
, 7 );
231 r
= udatpg_getDateTimeFormat(dtpg
, &length
);
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");
240 static void TestBuilder() {
241 UErrorCode errorCode
=U_ZERO_ERROR
;
242 UDateTimePatternGenerator
*dtpg
;
243 UDateTimePatternConflict conflict
;
246 int32_t length
, pLength
;
248 const UChar
* ptrResult
[2];
250 UDateTimePatternGenerator
*generator
;
251 int32_t formattedCapacity
, resultLen
,patternCapacity
;
252 UChar pattern
[40], formatted
[40];
253 UDateFormat
*formatter
;
254 UDate sampleDate
= 837039928046.0;
255 static const char locale
[]= "fr";
256 UErrorCode status
=U_ZERO_ERROR
;
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
));
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
));
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");
279 /* Test pattern == 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");
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
));
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");
303 /* Get all skeletons and the crroespong pattern for each skeleton. */
304 ptrResult
[0] = testPattern2
;
305 ptrResult
[1] = redundantPattern
;
307 en
= udatpg_openSkeletons(dtpg
, &errorCode
);
308 if (U_FAILURE(errorCode
) || (length
==0) ) {
309 log_err("udatpg_openSkeletons failed!\n");
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");
322 /* Get all baseSkeletons */
323 en
= udatpg_openBaseSkeletons(dtpg
, &errorCode
);
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");
333 if (U_FAILURE(errorCode
) || (length
==0) ) {
334 log_err("udatpg_openSkeletons failed!\n");
341 /* sample code in Userguide */
342 patternCapacity
= UPRV_LENGTHOF(pattern
);
344 generator
=udatpg_open(locale
, &status
);
345 if(U_FAILURE(status
)) {
349 /* get a pattern for an abbreviated month and day */
350 length
= udatpg_getBestPattern(generator
, skeleton
, 4,
351 pattern
, patternCapacity
, &status
);
352 formatter
= udat_open(UDAT_PATTERN
, UDAT_PATTERN
, locale
, timeZoneGMT
, -1,
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
);
360 /* use it to format (or parse) */
361 formattedCapacity
= UPRV_LENGTHOF(formatted
);
362 resultLen
=udat_format(formatter
, ucal_getNow(), formatted
, formattedCapacity
,
364 /* for French, the result is "13 sept." */
366 /* cannot use the result from ucal_getNow() because the value change evreyday. */
367 resultLen
=udat_format(formatter
, sampleDate
, formatted
, formattedCapacity
,
369 if ( u_memcmp(sampleFormatted
, formatted
, resultLen
) != 0 ) {
370 log_err("Failed udat_format() of sample code in Userguide.\n");
372 udatpg_close(generator
);
373 udat_close(formatter
);
376 typedef struct DTPtnGenOptionsData
{
379 UDateTimePatternMatchOptions options
;
380 const UChar
* expectedPattern
;
381 } DTPtnGenOptionsData
;
382 enum { kTestOptionsPatLenMax
= 32 };
384 static const UChar skel_Hmm
[] = { 0x0048, 0x006D, 0x006D, 0 };
385 static const UChar skel_HHmm
[] = { 0x0048, 0x0048, 0x006D, 0x006D, 0 };
386 static const UChar skel_hhmm
[] = { 0x0068, 0x0068, 0x006D, 0x006D, 0 };
387 static const UChar patn_hcmm_a
[] = { 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h:mm a */
388 static const UChar patn_HHcmm
[] = { 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0 }; /* HH:mm */
389 static const UChar patn_hhcmm_a
[] = { 0x0068, 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh:mm a */
390 static const UChar patn_HHpmm
[] = { 0x0048, 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* HH.mm */
391 static const UChar patn_hpmm_a
[] = { 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h.mm a */
392 static const UChar patn_Hpmm
[] = { 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* H.mm */
393 static const UChar patn_hhpmm_a
[] = { 0x0068, 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh.mm a */
395 static 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
},
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
},
412 int count
= UPRV_LENGTHOF(testData
);
413 const DTPtnGenOptionsData
* testDataPtr
= testData
;
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
);
432 udatpg_close(dtpgen
);
434 log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr
->locale
, myErrorName(status
));