2 *******************************************************************************
4 * Copyright (C) 2007, 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"
34 void addDateTimePatternGeneratorTest(TestNode
** root
);
36 #define TESTCASE(x) addTest(root, &x, "tsformat/udatpg_test/" #x)
38 static void TestOpenClose(void);
39 static void TestUsage(void);
40 static void TestBuilder(void);
42 void addDateTimePatternGeneratorTest(TestNode
** root
) {
43 TESTCASE(TestOpenClose
);
45 TESTCASE(TestBuilder
);
49 * Pipe symbol '|'. We pass only the first UChar without NUL-termination.
50 * The second UChar is just to verify that the API does not pick that up.
52 static const UChar pipeString
[]={ 0x7c, 0x0a };
54 static const UChar testSkeleton1
[]={ 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
55 static const UChar expectingBestPattern
[]={ 0x48, 0x48, 0x2e, 0x6d, 0x6d, 0 }; /* HH.mm */
56 static const UChar testPattern
[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0 }; /* HH:mm */
57 static const UChar expectingSkeleton
[]= { 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
58 static const UChar expectingBaseSkeleton
[]= { 0x48, 0x6d, 0 }; /* HHmm */
59 static const UChar redundantPattern
[]={ 0x79, 0x79, 0x4d, 0x4d, 0x4d, 0 }; /* yyMMM */
60 static const UChar testFormat
[]= {0x7B, 0x31, 0x7D, 0x20, 0x7B, 0x30, 0x7D, 0}; /* {1} {0} */
61 static const UChar appendItemName
[]= {0x68, 0x72, 0}; /* hr */
62 static const UChar testPattern2
[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0x20, 0x76, 0 }; /* HH:mm v */
63 static const UChar replacedStr
[]={ 0x76, 0x76, 0x76, 0x76, 0 }; /* vvvv */
64 /* results for getBaseSkeletons() - {Hmv}, {yMMM} */
65 static const UChar resultBaseSkeletons
[2][10] = {{0x48,0x6d, 0x76, 0}, {0x79, 0x4d, 0x4d, 0x4d, 0 } };
66 static const UChar sampleFormatted
[] = {0x31, 0x30, 0x20, 0x6A, 0x75, 0x69, 0x6C, 0x2E, 0}; /* 10 juil. */
67 static const UChar skeleton
[]= {0x4d, 0x4d, 0x4d, 0x64, 0}; /* MMMd */
68 static const UChar timeZoneGMT
[] = { 0x0047, 0x004d, 0x0054, 0x0000 }; /* "GMT" */
70 static void TestOpenClose() {
71 UErrorCode errorCode
=U_ZERO_ERROR
;
72 UDateTimePatternGenerator
*dtpg
, *dtpg2
;
76 /* Open a DateTimePatternGenerator for the default locale. */
77 dtpg
=udatpg_open(NULL
, &errorCode
);
78 if(U_FAILURE(errorCode
)) {
79 log_err("udatpg_open(NULL) failed - %s\n", u_errorName(errorCode
));
84 /* Now one for German. */
85 dtpg
=udatpg_open("de", &errorCode
);
86 if(U_FAILURE(errorCode
)) {
87 log_err("udatpg_open(de) failed - %s\n", u_errorName(errorCode
));
91 /* Make some modification which we verify gets passed on to the clone. */
92 udatpg_setDecimal(dtpg
, pipeString
, 1);
94 /* Clone the generator. */
95 dtpg2
=udatpg_clone(dtpg
, &errorCode
);
96 if(U_FAILURE(errorCode
) || dtpg2
==NULL
) {
97 log_err("udatpg_clone() failed - %s\n", u_errorName(errorCode
));
101 /* Verify that the clone has the custom decimal symbol. */
102 s
=udatpg_getDecimal(dtpg2
, &length
);
103 if(s
==pipeString
|| length
!=1 || 0!=u_memcmp(s
, pipeString
, length
) || s
[length
]!=0) {
104 log_err("udatpg_getDecimal(cloned object) did not return the expected string\n");
112 static void TestUsage() {
113 UErrorCode errorCode
=U_ZERO_ERROR
;
114 UDateTimePatternGenerator
*dtpg
;
115 UChar bestPattern
[20];
121 dtpg
=udatpg_open("fi", &errorCode
);
122 if(U_FAILURE(errorCode
)) {
123 log_err("udatpg_open(fi) failed - %s\n", u_errorName(errorCode
));
126 length
= udatpg_getBestPattern(dtpg
, testSkeleton1
, 4,
127 bestPattern
, 20, &errorCode
);
128 if(U_FAILURE(errorCode
)) {
129 log_err("udatpg_getBestPattern failed - %s\n", u_errorName(errorCode
));
132 if((u_memcmp(bestPattern
, expectingBestPattern
, length
)!=0) || bestPattern
[length
]!=0) {
133 log_err("udatpg_getBestPattern did not return the expected string\n");
138 /* Test skeleton == NULL */
140 length
= udatpg_getBestPattern(dtpg
, s
, 0, bestPattern
, 20, &errorCode
);
141 if(!U_FAILURE(errorCode
)&&(length
!=0) ) {
142 log_err("udatpg_getBestPattern failed in illegal argument - skeleton is NULL.\n");
146 /* Test udatpg_getSkeleton */
147 length
= udatpg_getSkeleton(dtpg
, testPattern
, 5, result
, 20, &errorCode
);
148 if(U_FAILURE(errorCode
)) {
149 log_err("udatpg_getSkeleton failed - %s\n", u_errorName(errorCode
));
152 if((u_memcmp(result
, expectingSkeleton
, length
)!=0) || result
[length
]!=0) {
153 log_err("udatpg_getSkeleton did not return the expected string\n");
157 /* Test pattern == NULL */
159 length
= udatpg_getSkeleton(dtpg
, s
, 0, result
, 20, &errorCode
);
160 if(!U_FAILURE(errorCode
)&&(length
!=0) ) {
161 log_err("udatpg_getSkeleton failed in illegal argument - pattern is NULL.\n");
165 /* Test udatpg_getBaseSkeleton */
166 length
= udatpg_getBaseSkeleton(dtpg
, testPattern
, 5, result
, 20, &errorCode
);
167 if(U_FAILURE(errorCode
)) {
168 log_err("udatpg_getBaseSkeleton failed - %s\n", u_errorName(errorCode
));
171 if((u_memcmp(result
, expectingBaseSkeleton
, length
)!=0) || result
[length
]!=0) {
172 log_err("udatpg_getBaseSkeleton did not return the expected string\n");
176 /* Test pattern == NULL */
178 length
= udatpg_getBaseSkeleton(dtpg
, s
, 0, result
, 20, &errorCode
);
179 if(!U_FAILURE(errorCode
)&&(length
!=0) ) {
180 log_err("udatpg_getBaseSkeleton failed in illegal argument - pattern is NULL.\n");
184 /* set append format to {1}{0} */
185 udatpg_setAppendItemFormat( dtpg
, UDATPG_MONTH_FIELD
, testFormat
, 7 );
186 r
= udatpg_getAppendItemFormat(dtpg
, UDATPG_MONTH_FIELD
, &length
);
189 if(length
!=7 || 0!=u_memcmp(r
, testFormat
, length
) || r
[length
]!=0) {
190 log_err("udatpg_setAppendItemFormat did not return the expected string\n");
194 /* set append name to hr */
195 udatpg_setAppendItemName( dtpg
, UDATPG_HOUR_FIELD
, appendItemName
, 7 );
196 r
= udatpg_getAppendItemName(dtpg
, UDATPG_HOUR_FIELD
, &length
);
198 if(length
!=7 || 0!=u_memcmp(r
, appendItemName
, length
) || r
[length
]!=0) {
199 log_err("udatpg_setAppendItemName did not return the expected string\n");
203 /* set date time format to {1}{0} */
204 udatpg_setDateTimeFormat( dtpg
, testFormat
, 7 );
205 r
= udatpg_getDateTimeFormat(dtpg
, &length
);
207 if(length
!=7 || 0!=u_memcmp(r
, testFormat
, length
) || r
[length
]!=0) {
208 log_err("udatpg_setDateTimeFormat did not return the expected string\n");
214 static void TestBuilder() {
215 UErrorCode errorCode
=U_ZERO_ERROR
;
216 UDateTimePatternGenerator
*dtpg
;
217 UDateTimePatternConflict conflict
;
220 int32_t length
, pLength
;
222 const UChar
* ptrResult
[2];
224 UDateTimePatternGenerator
*generator
;
225 int32_t formattedCapacity
, resultLen
,patternCapacity
;
226 UChar pattern
[40], formatted
[40];
227 UDateFormat
*formatter
;
228 UDate sampleDate
= 837039928046.0;
229 static const char locale
[]= "fr";
230 UErrorCode status
=U_ZERO_ERROR
;
232 /* test create an empty DateTimePatternGenerator */
233 dtpg
=udatpg_openEmpty(&errorCode
);
234 if(U_FAILURE(errorCode
)) {
235 log_err("udatpg_openEmpty() failed - %s\n", u_errorName(errorCode
));
240 conflict
= udatpg_addPattern(dtpg
, redundantPattern
, 5, FALSE
, result
, 20,
241 &length
, &errorCode
);
242 if(U_FAILURE(errorCode
)) {
243 log_err("udatpg_addPattern() failed - %s\n", u_errorName(errorCode
));
246 /* Add a redundant pattern */
247 conflict
= udatpg_addPattern(dtpg
, redundantPattern
, 5, FALSE
, result
, 20,
248 &length
, &errorCode
);
249 if(conflict
== UDATPG_NO_CONFLICT
) {
250 log_err("udatpg_addPattern() failed to find the duplicate pattern.\n");
253 /* Test pattern == NULL */
255 length
= udatpg_addPattern(dtpg
, s
, 0, FALSE
, result
, 20,
256 &length
, &errorCode
);
257 if(!U_FAILURE(errorCode
)&&(length
!=0) ) {
258 log_err("udatpg_addPattern failed in illegal argument - pattern is NULL.\n");
262 /* replace field type */
263 errorCode
=U_ZERO_ERROR
;
264 conflict
= udatpg_addPattern(dtpg
, testPattern2
, 7, FALSE
, result
, 20,
265 &length
, &errorCode
);
266 if((conflict
!= UDATPG_NO_CONFLICT
)||U_FAILURE(errorCode
)) {
267 log_err("udatpg_addPattern() failed to add HH:mm v. - %s\n", u_errorName(errorCode
));
270 length
= udatpg_replaceFieldTypes(dtpg
, testPattern2
, 7, replacedStr
, 4,
271 result
, 20, &errorCode
);
272 if (U_FAILURE(errorCode
) || (length
==0) ) {
273 log_err("udatpg_replaceFieldTypes failed!\n");
277 /* Get all skeletons and the crroespong pattern for each skeleton. */
278 ptrResult
[0] = testPattern2
;
279 ptrResult
[1] = redundantPattern
;
281 en
= udatpg_openSkeletons(dtpg
, &errorCode
);
282 if (U_FAILURE(errorCode
) || (length
==0) ) {
283 log_err("udatpg_openSkeletons failed!\n");
286 while ( (s
=uenum_unext(en
, &length
, &errorCode
))!= NULL
) {
287 p
= udatpg_getPatternForSkeleton(dtpg
, s
, length
, &pLength
);
288 if (U_FAILURE(errorCode
) || p
==NULL
|| u_memcmp(p
, ptrResult
[count
], pLength
)!=0 ) {
289 log_err("udatpg_getPatternForSkeleton failed!\n");
296 /* Get all baseSkeletons */
297 en
= udatpg_openBaseSkeletons(dtpg
, &errorCode
);
299 while ( (s
=uenum_unext(en
, &length
, &errorCode
))!= NULL
) {
300 p
= udatpg_getPatternForSkeleton(dtpg
, s
, length
, &pLength
);
301 if (U_FAILURE(errorCode
) || p
==NULL
|| u_memcmp(p
, resultBaseSkeletons
[count
], pLength
)!=0 ) {
302 log_err("udatpg_getPatternForSkeleton failed!\n");
307 if (U_FAILURE(errorCode
) || (length
==0) ) {
308 log_err("udatpg_openSkeletons failed!\n");
315 /* sample code in Userguide */
316 patternCapacity
= (int32_t)(sizeof(pattern
)/sizeof((pattern
)[0]));
318 generator
=udatpg_open(locale
, &status
);
319 if(U_FAILURE(status
)) {
323 /* get a pattern for an abbreviated month and day */
324 length
= udatpg_getBestPattern(generator
, skeleton
, 4,
325 pattern
, patternCapacity
, &status
);
326 formatter
= udat_open(UDAT_IGNORE
, UDAT_DEFAULT
, locale
, timeZoneGMT
, -1,
327 pattern
, length
, &status
);
328 if (formatter
==NULL
) {
329 log_err("Failed to initialize the UDateFormat of the sample code in Userguide.\n");
330 udatpg_close(generator
);
334 /* use it to format (or parse) */
335 formattedCapacity
= (int32_t)(sizeof(formatted
)/sizeof((formatted
)[0]));
336 resultLen
=udat_format(formatter
, ucal_getNow(), formatted
, formattedCapacity
,
338 /* for French, the result is "13 sept." */
340 /* cannot use the result from ucal_getNow() because the value change evreyday. */
341 resultLen
=udat_format(formatter
, sampleDate
, formatted
, formattedCapacity
,
343 if ( u_memcmp(sampleFormatted
, formatted
, resultLen
) != 0 ) {
344 log_err("Failed udat_format() of sample code in Userguide.\n");
346 udatpg_close(generator
);
347 udat_close(formatter
);