]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/udatpg_test.c
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / test / cintltst / udatpg_test.c
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2007, 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
42 void addDateTimePatternGeneratorTest(TestNode** root) {
43 TESTCASE(TestOpenClose);
44 TESTCASE(TestUsage);
45 TESTCASE(TestBuilder);
46 }
47
48 /*
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.
51 */
52 static const UChar pipeString[]={ 0x7c, 0x0a };
53
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" */
69
70 static void TestOpenClose() {
71 UErrorCode errorCode=U_ZERO_ERROR;
72 UDateTimePatternGenerator *dtpg, *dtpg2;
73 const UChar *s;
74 int32_t length;
75
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));
80 return;
81 }
82 udatpg_close(dtpg);
83
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));
88 return;
89 }
90
91 /* Make some modification which we verify gets passed on to the clone. */
92 udatpg_setDecimal(dtpg, pipeString, 1);
93
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));
98 return;
99 }
100
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");
105 return;
106 }
107
108 udatpg_close(dtpg);
109 udatpg_close(dtpg2);
110 }
111
112 static void TestUsage() {
113 UErrorCode errorCode=U_ZERO_ERROR;
114 UDateTimePatternGenerator *dtpg;
115 UChar bestPattern[20];
116 UChar result[20];
117 int32_t length;
118 UChar *s;
119 const UChar *r;
120
121 dtpg=udatpg_open("fi", &errorCode);
122 if(U_FAILURE(errorCode)) {
123 log_err("udatpg_open(fi) failed - %s\n", u_errorName(errorCode));
124 return;
125 }
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));
130 return;
131 }
132 if((u_memcmp(bestPattern, expectingBestPattern, length)!=0) || bestPattern[length]!=0) {
133 log_err("udatpg_getBestPattern did not return the expected string\n");
134 return;
135 }
136
137
138 /* Test skeleton == NULL */
139 s=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");
143 return;
144 }
145
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));
150 return;
151 }
152 if((u_memcmp(result, expectingSkeleton, length)!=0) || result[length]!=0) {
153 log_err("udatpg_getSkeleton did not return the expected string\n");
154 return;
155 }
156
157 /* Test pattern == NULL */
158 s=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");
162 return;
163 }
164
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));
169 return;
170 }
171 if((u_memcmp(result, expectingBaseSkeleton, length)!=0) || result[length]!=0) {
172 log_err("udatpg_getBaseSkeleton did not return the expected string\n");
173 return;
174 }
175
176 /* Test pattern == NULL */
177 s=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");
181 return;
182 }
183
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);
187
188
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");
191 return;
192 }
193
194 /* set append name to hr */
195 udatpg_setAppendItemName( dtpg, UDATPG_HOUR_FIELD, appendItemName, 7 );
196 r = udatpg_getAppendItemName(dtpg, UDATPG_HOUR_FIELD, &length);
197
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");
200 return;
201 }
202
203 /* set date time format to {1}{0} */
204 udatpg_setDateTimeFormat( dtpg, testFormat, 7 );
205 r = udatpg_getDateTimeFormat(dtpg, &length);
206
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");
209 return;
210 }
211 udatpg_close(dtpg);
212 }
213
214 static void TestBuilder() {
215 UErrorCode errorCode=U_ZERO_ERROR;
216 UDateTimePatternGenerator *dtpg;
217 UDateTimePatternConflict conflict;
218 UEnumeration *en;
219 UChar result[20];
220 int32_t length, pLength;
221 const UChar *s, *p;
222 const UChar* ptrResult[2];
223 int32_t count=0;
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;
231
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));
236 return;
237 }
238
239 /* Add a pattern */
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));
244 return;
245 }
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");
251 return;
252 }
253 /* Test pattern == NULL */
254 s=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");
259 return;
260 }
261
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));
268 return;
269 }
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");
274 return;
275 }
276
277 /* Get all skeletons and the crroespong pattern for each skeleton. */
278 ptrResult[0] = testPattern2;
279 ptrResult[1] = redundantPattern;
280 count=0;
281 en = udatpg_openSkeletons(dtpg, &errorCode);
282 if (U_FAILURE(errorCode) || (length==0) ) {
283 log_err("udatpg_openSkeletons failed!\n");
284 return;
285 }
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");
290 return;
291 }
292 count++;
293 }
294 uenum_close(en);
295
296 /* Get all baseSkeletons */
297 en = udatpg_openBaseSkeletons(dtpg, &errorCode);
298 count=0;
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");
303 return;
304 }
305 count++;
306 }
307 if (U_FAILURE(errorCode) || (length==0) ) {
308 log_err("udatpg_openSkeletons failed!\n");
309 return;
310 }
311 uenum_close(en);
312
313 udatpg_close(dtpg);
314
315 /* sample code in Userguide */
316 patternCapacity = (int32_t)(sizeof(pattern)/sizeof((pattern)[0]));
317 status=U_ZERO_ERROR;
318 generator=udatpg_open(locale, &status);
319 if(U_FAILURE(status)) {
320 return;
321 }
322
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);
331 return;
332 }
333
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,
337 NULL, &status);
338 /* for French, the result is "13 sept." */
339
340 /* cannot use the result from ucal_getNow() because the value change evreyday. */
341 resultLen=udat_format(formatter, sampleDate, formatted, formattedCapacity,
342 NULL, &status);
343 if ( u_memcmp(sampleFormatted, formatted, resultLen) != 0 ) {
344 log_err("Failed udat_format() of sample code in Userguide.\n");
345 }
346 udatpg_close(generator);
347 udat_close(formatter);
348 }
349
350 #endif