]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cdattst.c
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cdattst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
73c04bcf 3 * Copyright (c) 1997-2006, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6/********************************************************************************
7*
8* File CDATTST.C
9*
10* Modification History:
11* Name Description
12* Madhu Katragadda Creation
13*********************************************************************************
14*/
15
16/* C API TEST FOR DATE FORMAT */
17
18#include "unicode/utypes.h"
19
20#if !UCONFIG_NO_FORMATTING
21
22#include "unicode/uloc.h"
23#include "unicode/udat.h"
24#include "unicode/ucal.h"
25#include "unicode/unum.h"
26#include "unicode/ustring.h"
27#include "cintltst.h"
28#include "cdattst.h"
29#include "cformtst.h"
30#include "cmemory.h"
31
374ca955
A
32#include <math.h>
33
34static void TestExtremeDates(void);
73c04bcf 35static void TestAllLocales(void);
374ca955
A
36
37#define LEN(a) (sizeof(a)/sizeof(a[0]))
38
b75a7d8f
A
39void addDateForTest(TestNode** root);
40
374ca955 41#define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x)
b75a7d8f
A
42
43void addDateForTest(TestNode** root)
44{
374ca955
A
45 TESTCASE(TestDateFormat);
46 TESTCASE(TestSymbols);
47 TESTCASE(TestDateFormatCalendar);
48 TESTCASE(TestExtremeDates);
73c04bcf 49 TESTCASE(TestAllLocales);
b75a7d8f
A
50}
51/* Testing the DateFormat API */
52static void TestDateFormat()
53{
54 UDateFormat *def, *fr, *it, *de, *def1, *fr_pat;
55 UDateFormat *any;
56 UDateFormat *copy;
57 UErrorCode status = U_ZERO_ERROR;
58 UChar* result = NULL;
59 const UCalendar *cal;
60 const UNumberFormat *numformat1, *numformat2;
61 UChar temp[30];
62 int32_t numlocales;
63 UDate d1;
64 int i;
65 int32_t resultlength;
66 int32_t resultlengthneeded;
67 int32_t parsepos;
68 UDate d = 837039928046.0;
69 double num = -10456.37;
70 /*const char* str="yyyy.MM.dd G 'at' hh:mm:ss z";
71 const char t[]="2/3/76 2:50 AM";*/
72 /*Testing udat_open() to open a dateformat */
374ca955
A
73
74 ctest_setTimeZone(NULL, &status);
75
b75a7d8f
A
76 log_verbose("\nTesting udat_open() with various parameters\n");
77 fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL,0, NULL, 0,&status);
78 if(U_FAILURE(status))
79 {
80 log_err("FAIL: error in creating the dateformat using full time style with french locale\n %s\n",
81 myErrorName(status) );
82 return;
83 }
84 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
85 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
86 /* def = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0, &status); */
87 def = udat_open(UDAT_SHORT, UDAT_SHORT, "en_US", NULL, 0,NULL, 0, &status);
88 if(U_FAILURE(status))
89 {
90 log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
91 myErrorName(status) );
92 return;
93 }
94 it = udat_open(UDAT_DEFAULT, UDAT_MEDIUM, "it_IT", NULL, 0, NULL, 0,&status);
95 if(U_FAILURE(status))
96 {
97 log_err("FAIL: error in creating the dateformat using medium date style with italian locale\n %s\n",
98 myErrorName(status) );
99 return;
100 }
101 de = udat_open(UDAT_LONG, UDAT_LONG, "de_DE", NULL, 0, NULL, 0,&status);
102 if(U_FAILURE(status))
103 {
104 log_err("FAIL: error in creating the dateformat using long time and date styles with german locale\n %s\n",
105 myErrorName(status));
106 return;
107 }
108 /*creating a default dateformat */
109 def1 = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0,NULL, 0, &status);
110 if(U_FAILURE(status))
111 {
112 log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
113 myErrorName(status) );
114 return;
115 }
116
117
118 /*Testing udat_getAvailable() and udat_countAvailable()*/
119 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
120 numlocales=udat_countAvailable();
121 /* use something sensible w/o hardcoding the count */
122 if(numlocales < 0)
123 log_data_err("FAIL: error in countAvailable\n");
124 log_verbose("The number of locales for which date/time formatting patterns are available is %d\n", numlocales);
125
126 for(i=0;i<numlocales;i++) {
127 UErrorCode subStatus = U_ZERO_ERROR;
128 log_verbose("Testing open of %s\n", udat_getAvailable(i));
73c04bcf 129 any = udat_open(UDAT_SHORT, UDAT_SHORT, udat_getAvailable(i), NULL ,0, NULL, 0, &subStatus);
b75a7d8f 130 if(U_FAILURE(subStatus)) {
73c04bcf 131 log_data_err("FAIL: date format %s (getAvailable(%d)) is not instantiable: %s\n", udat_getAvailable(i), i, u_errorName(subStatus));
b75a7d8f
A
132 }
133 udat_close(any);
134 }
135
136 /*Testing udat_clone()*/
137 log_verbose("\nTesting the udat_clone() function of date format\n");
138 copy=udat_clone(def, &status);
139 if(U_FAILURE(status)){
140 log_err("Error in creating the clone using udat_clone: %s\n", myErrorName(status) );
141 }
142 /*if(def != copy)
143 log_err("Error in udat_clone");*/ /*how should i check for equality???? */
144
145 /*Testing udat_format()*/
146 log_verbose("\nTesting the udat_format() function of date format\n");
147 u_uastrcpy(temp, "7/10/96 4:05 PM");
148 /*format using def */
149 resultlength=0;
150 resultlengthneeded=udat_format(def, d, NULL, resultlength, NULL, &status);
151 if(status==U_BUFFER_OVERFLOW_ERROR)
152 {
153 status=U_ZERO_ERROR;
154 resultlength=resultlengthneeded+1;
155 if(result != NULL) {
156 free(result);
157 result = NULL;
158 }
159 result=(UChar*)malloc(sizeof(UChar) * resultlength);
160 udat_format(def, d, result, resultlength, NULL, &status);
161 }
162 if(U_FAILURE(status) || !result)
163 {
164 log_err("FAIL: Error in formatting using udat_format(.....) %s\n", myErrorName(status) );
165 return;
166 }
167 else
168 log_verbose("PASS: formatting successful\n");
169 if(u_strcmp(result, temp)==0)
170 log_verbose("PASS: Date Format for US locale successful uisng udat_format()\n");
171 else
172 log_err("FAIL: Date Format for US locale failed using udat_format()\n");
173 /*format using fr */
174
374ca955 175 u_unescape("10 juil. 96 16 h 05 HAP (\\u00c9UA)", temp, 30);
b75a7d8f
A
176 if(result != NULL) {
177 free(result);
178 result = NULL;
179 }
180 result=myDateFormat(fr, d);
181 if(u_strcmp(result, temp)==0)
374ca955 182 log_verbose("PASS: Date Format for french locale successful using udat_format()\n");
b75a7d8f 183 else
374ca955
A
184 log_data_err("FAIL: Date Format for french locale failed using udat_format(). Expected:\n");
185 /*format using it */
b75a7d8f
A
186 u_uastrcpy(temp, "10/lug/96 16:05:28");
187
188 if(u_strcmp(myDateFormat(it,d), temp)==0)
189 log_verbose("PASS: Date Format for italian locale successful uisng udat_format()\n");
190 else
191 log_data_err("FAIL: Date Format for italian locale failed using udat_format()\n");
192
193
194 /*Testing parsing using udat_parse()*/
195 log_verbose("\nTesting parsing using udat_parse()\n");
196 u_uastrcpy(temp,"2/3/76 2:50 AM");
197 parsepos=0;
374ca955 198 status=U_ZERO_ERROR;
b75a7d8f
A
199
200 d1=udat_parse(def, temp, u_strlen(temp), &parsepos, &status);
201 if(U_FAILURE(status))
202 {
203 log_err("FAIL: Error in parsing using udat_parse(.....) %s\n", myErrorName(status) );
204 }
205 else
206 log_verbose("PASS: parsing succesful\n");
207 /*format it back and check for equality */
208
209
210 if(u_strcmp(myDateFormat(def, d1),temp)!=0)
211 log_err("FAIL: error in parsing\n");
212
374ca955
A
213 /*Testing parsing using udat_parse()*/
214 log_verbose("\nTesting parsing using udat_parse()\n");
215 u_uastrcpy(temp,"2/Don't parse this part");
216 status=U_ZERO_ERROR;
217
218 d1=udat_parse(def, temp, u_strlen(temp), NULL, &status);
219 if(status != U_PARSE_ERROR)
220 {
221 log_err("FAIL: udat_parse(\"bad string\") passed when it should have failed\n");
222 }
223 else
224 log_verbose("PASS: parsing succesful\n");
b75a7d8f
A
225
226
227
228 /*Testing udat_openPattern() */
229 status=U_ZERO_ERROR;
230 log_verbose("\nTesting the udat_openPattern with a specified pattern\n");
231 /*for french locale */
232 fr_pat=udat_open(UDAT_IGNORE, UDAT_IGNORE,"fr_FR",NULL,0,temp, u_strlen(temp), &status);
233 if(U_FAILURE(status))
234 {
235 log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n",
236 myErrorName(status) );
237 }
238 else
239 log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");
240
241
242 /*Testing applyPattern and toPattern */
243 log_verbose("\nTesting applyPattern and toPattern()\n");
244 udat_applyPattern(def1, FALSE, temp, u_strlen(temp));
245 log_verbose("Extracting the pattern\n");
246
247 resultlength=0;
248 resultlengthneeded=udat_toPattern(def1, FALSE, NULL, resultlength, &status);
249 if(status==U_BUFFER_OVERFLOW_ERROR)
250 {
251 status=U_ZERO_ERROR;
252 resultlength=resultlengthneeded + 1;
253 result=(UChar*)malloc(sizeof(UChar) * resultlength);
254 udat_toPattern(def1, FALSE, result, resultlength, &status);
255 }
256 if(U_FAILURE(status))
257 {
258 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
259 myErrorName(status) );
260 }
261 if(u_strcmp(result, temp)!=0)
262 log_err("FAIL: Error in extracting the pattern\n");
263 else
264 log_verbose("PASS: applyPattern and toPattern work fine\n");
265
266 if(result != NULL) {
267 free(result);
268 result = NULL;
269 }
270
271
272 /*Testing getter and setter functions*/
273 /*isLenient and setLenient()*/
274 log_verbose("\nTesting the isLenient and setLenient properties\n");
275 udat_setLenient(fr, udat_isLenient(it));
276 if(udat_isLenient(fr) != udat_isLenient(it))
277 log_err("ERROR: setLenient() failed\n");
278 else
279 log_verbose("PASS: setLenient() successful\n");
280
281
282 /*Test get2DigitYearStart set2DigitYearStart */
283 log_verbose("\nTesting the get and set 2DigitYearStart properties\n");
284 d1= udat_get2DigitYearStart(fr_pat,&status);
285 if(U_FAILURE(status)) {
286 log_err("ERROR: udat_get2DigitYearStart failed %s\n", myErrorName(status) );
287 }
288 status = U_ZERO_ERROR;
289 udat_set2DigitYearStart(def1 ,d1, &status);
290 if(U_FAILURE(status)) {
291 log_err("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) );
292 }
293 if(udat_get2DigitYearStart(fr_pat, &status) != udat_get2DigitYearStart(def1, &status))
294 log_err("FAIL: error in set2DigitYearStart\n");
295 else
296 log_verbose("PASS: set2DigitYearStart successful\n");
297 /*try setting it to another value */
298 udat_set2DigitYearStart(de, 2000.0, &status);
299 if(U_FAILURE(status)){
300 log_verbose("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) );
301 }
302 if(udat_get2DigitYearStart(de, &status) != 2000)
303 log_err("FAIL: error in set2DigitYearStart\n");
304 else
305 log_verbose("PASS: set2DigitYearStart successful\n");
306
307
308
309 /*Test getNumberFormat() and setNumberFormat() */
310 log_verbose("\nTesting the get and set NumberFormat properties of date format\n");
311 numformat1=udat_getNumberFormat(fr_pat);
312 udat_setNumberFormat(def1, numformat1);
313 numformat2=udat_getNumberFormat(def1);
314 if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0)
315 log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
316 else
317 log_verbose("PASS:setNumberFormat and getNumberFormat succesful\n");
318
319 /*try setting the number format to another format */
320 numformat1=udat_getNumberFormat(def);
321 udat_setNumberFormat(def1, numformat1);
322 numformat2=udat_getNumberFormat(def1);
323 if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0)
324 log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
325 else
326 log_verbose("PASS: setNumberFormat and getNumberFormat succesful\n");
327
328
329
330 /*Test getCalendar and setCalendar*/
331 log_verbose("\nTesting the udat_getCalendar() and udat_setCalendar() properties\n");
332 cal=udat_getCalendar(fr_pat);
333
334
335 udat_setCalendar(def1, cal);
336 if(!ucal_equivalentTo(udat_getCalendar(fr_pat), udat_getCalendar(def1)))
337 log_err("FAIL: Error in setting and getting the calendar\n");
338 else
339 log_verbose("PASS: getting and setting calendar successful\n");
340
341 if(result!=NULL) {
342 free(result);
343 }
344
345 /*Closing the UDateForamt */
346 udat_close(def);
347 udat_close(fr);
348 udat_close(it);
349 udat_close(de);
350 udat_close(def1);
351 udat_close(fr_pat);
352 udat_close(copy);
353
374ca955 354 ctest_resetTimeZone();
b75a7d8f
A
355}
356
357/*Testing udat_getSymbols() and udat_setSymbols() and udat_countSymbols()*/
358static void TestSymbols()
359{
360 UDateFormat *def, *fr;
361 UErrorCode status = U_ZERO_ERROR;
362 UChar *value=NULL;
363 UChar *result = NULL;
364 int32_t resultlength;
365 int32_t resultlengthout;
366 UChar *pattern;
367
368
369 /*creating a dateformat with french locale */
370 log_verbose("\ncreating a date format with french locale\n");
371 fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL, 0, NULL, 0, &status);
372 if(U_FAILURE(status))
373 {
374 log_err("error in creating the dateformat using full time style with french locale\n %s\n",
375 myErrorName(status) );
376 return;
377 }
378 /*creating a default dateformat */
379 log_verbose("\ncreating a date format with default locale\n");
380 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
381 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
382 /* def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status); */
383 def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status);
384 if(U_FAILURE(status))
385 {
386 log_err("error in creating the dateformat using short date and time style\n %s\n",
387 myErrorName(status) );
388 return;
389 }
390
391
392 /*Testing countSymbols, getSymbols and setSymbols*/
393 log_verbose("\nTesting countSymbols\n");
394 /*since the month names has the last string empty and week names are 1 based 1.e first string in the weeknames array is empty */
395 if(udat_countSymbols(def, UDAT_ERAS)!=2 || udat_countSymbols(def, UDAT_MONTHS)!=12 ||
396 udat_countSymbols(def, UDAT_SHORT_MONTHS)!=12 || udat_countSymbols(def, UDAT_WEEKDAYS)!=8 ||
397 udat_countSymbols(def, UDAT_SHORT_WEEKDAYS)!=8 || udat_countSymbols(def, UDAT_AM_PMS)!=2 ||
73c04bcf 398 udat_countSymbols(def, UDAT_QUARTERS) != 4 || udat_countSymbols(def, UDAT_SHORT_QUARTERS) != 4 ||
b75a7d8f
A
399 udat_countSymbols(def, UDAT_LOCALIZED_CHARS)!=1)
400 {
401 log_err("FAIL: error in udat_countSymbols\n");
402 }
403 else
404 log_verbose("PASS: udat_countSymbols() successful\n");
405
406 /*testing getSymbols*/
407 log_verbose("\nTesting getSymbols\n");
408 pattern=(UChar*)malloc(sizeof(UChar) * 10);
409 u_uastrcpy(pattern, "jeudi");
410 resultlength=0;
411 resultlengthout=udat_getSymbols(fr, UDAT_WEEKDAYS, 5 , NULL, resultlength, &status);
412 if(status==U_BUFFER_OVERFLOW_ERROR)
413 {
414 status=U_ZERO_ERROR;
415 resultlength=resultlengthout+1;
416 if(result != NULL) {
417 free(result);
418 result = NULL;
419 }
420 result=(UChar*)malloc(sizeof(UChar) * resultlength);
421 udat_getSymbols(fr, UDAT_WEEKDAYS, 5, result, resultlength, &status);
422
423 }
424 if(U_FAILURE(status))
425 {
426 log_err("FAIL: Error in udat_getSymbols().... %s\n", myErrorName(status) );
427 }
428 else
429 log_verbose("PASS: getSymbols succesful\n");
430
431 if(u_strcmp(result, pattern)==0)
432 log_verbose("PASS: getSymbols retrieved the right value\n");
433 else
434 log_data_err("FAIL: getSymbols retrieved the wrong value\n");
435
436 /*run series of tests to test getsymbols regressively*/
437 log_verbose("\nTesting getSymbols() regressively\n");
438 VerifygetSymbols(fr, UDAT_WEEKDAYS, 1, "dimanche");
439 VerifygetSymbols(def, UDAT_WEEKDAYS, 1, "Sunday");
440 VerifygetSymbols(fr, UDAT_SHORT_WEEKDAYS, 7, "sam.");
441 VerifygetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Sat");
442 VerifygetSymbols(def, UDAT_MONTHS, 11, "December");
443 VerifygetSymbols(def, UDAT_MONTHS, 0, "January");
444 VerifygetSymbols(fr, UDAT_ERAS, 0, "av. J.-C.");
445 VerifygetSymbols(def, UDAT_AM_PMS, 0, "AM");
446 VerifygetSymbols(def, UDAT_AM_PMS, 1, "PM");
447 VerifygetSymbols(fr, UDAT_SHORT_MONTHS, 0, "janv.");
448 VerifygetSymbols(def, UDAT_SHORT_MONTHS, 11, "Dec");
73c04bcf
A
449 VerifygetSymbols(fr, UDAT_QUARTERS, 0, "1er trimestre");
450 VerifygetSymbols(def, UDAT_QUARTERS, 3, "4th quarter");
451 VerifygetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "T2");
452 VerifygetSymbols(def, UDAT_SHORT_QUARTERS, 2, "Q3");
453 VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcL");
b75a7d8f
A
454
455
456 if(result != NULL) {
457 free(result);
458 result = NULL;
459 }
460free(pattern);
461
462 log_verbose("\nTesting setSymbols\n");
463 /*applying the pattern so that setSymbolss works */
464 resultlength=0;
465 resultlengthout=udat_toPattern(fr, FALSE, NULL, resultlength, &status);
466 if(status==U_BUFFER_OVERFLOW_ERROR)
467 {
468 status=U_ZERO_ERROR;
469 resultlength=resultlengthout + 1;
470 pattern=(UChar*)malloc(sizeof(UChar) * resultlength);
471 udat_toPattern(fr, FALSE, pattern, resultlength, &status);
472 }
473 if(U_FAILURE(status))
474 {
475 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
476 myErrorName(status) );
477 }
478
479 udat_applyPattern(def, FALSE, pattern, u_strlen(pattern));
480 resultlength=0;
481 resultlengthout=udat_toPattern(def, FALSE, NULL, resultlength,&status);
482 if(status==U_BUFFER_OVERFLOW_ERROR)
483 {
484 status=U_ZERO_ERROR;
485 resultlength=resultlengthout + 1;
486 if(result != NULL) {
487 free(result);
488 result = NULL;
489 }
490 result=(UChar*)malloc(sizeof(UChar) * resultlength);
491 udat_toPattern(fr, FALSE,result, resultlength, &status);
492 }
493 if(U_FAILURE(status))
494 {
495 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
496 myErrorName(status) );
497 }
498 if(u_strcmp(result, pattern)==0)
499 log_verbose("Pattern applied properly\n");
500 else
501 log_err("pattern could not be applied properly\n");
502
503free(pattern);
504 /*testing set symbols */
505 resultlength=0;
506 resultlengthout=udat_getSymbols(fr, UDAT_MONTHS, 11 , NULL, resultlength, &status);
507 if(status==U_BUFFER_OVERFLOW_ERROR){
508 status=U_ZERO_ERROR;
509 resultlength=resultlengthout+1;
510 if(result != NULL) {
511 free(result);
512 result = NULL;
513 }
514 result=(UChar*)malloc(sizeof(UChar) * resultlength);
515 udat_getSymbols(fr, UDAT_MONTHS, 11, result, resultlength, &status);
516
517 }
518 if(U_FAILURE(status))
519 log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) );
520 resultlength=resultlengthout+1;
521
522 udat_setSymbols(def, UDAT_MONTHS, 11, result, resultlength, &status);
523 if(U_FAILURE(status))
524 {
525 log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) );
526 }
527 else
528 log_verbose("PASS: SetSymbols successful\n");
529
530 resultlength=0;
531 resultlengthout=udat_getSymbols(def, UDAT_MONTHS, 11, NULL, resultlength, &status);
532 if(status==U_BUFFER_OVERFLOW_ERROR){
533 status=U_ZERO_ERROR;
534 resultlength=resultlengthout+1;
535 value=(UChar*)malloc(sizeof(UChar) * resultlength);
536 udat_getSymbols(def, UDAT_MONTHS, 11, value, resultlength, &status);
537 }
538 if(U_FAILURE(status))
539 log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n");
540
541 if(u_strcmp(result, value)!=0)
542 log_data_err("FAIL: Error in settting and getting symbols\n");
543 else
544 log_verbose("PASS: setSymbols successful\n");
545
546
547 /*run series of tests to test setSymbols regressively*/
548 log_verbose("\nTesting setSymbols regressively\n");
b75a7d8f 549 VerifysetSymbols(def, UDAT_ERAS, 0, "BeforeChrist");
73c04bcf
A
550 VerifysetSymbols(def, UDAT_ERA_NAMES, 1, "AnnoDomini");
551 VerifysetSymbols(def, UDAT_WEEKDAYS, 1, "Sundayweek");
b75a7d8f 552 VerifysetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Satweek");
73c04bcf
A
553 VerifysetSymbols(def, UDAT_NARROW_WEEKDAYS, 4, "M");
554 VerifysetSymbols(def, UDAT_STANDALONE_WEEKDAYS, 1, "Sonntagweek");
555 VerifysetSymbols(def, UDAT_STANDALONE_SHORT_WEEKDAYS, 7, "Sams");
556 VerifysetSymbols(def, UDAT_STANDALONE_NARROW_WEEKDAYS, 4, "V");
b75a7d8f
A
557 VerifysetSymbols(fr, UDAT_MONTHS, 11, "december");
558 VerifysetSymbols(fr, UDAT_SHORT_MONTHS, 0, "Jan");
73c04bcf
A
559 VerifysetSymbols(fr, UDAT_NARROW_MONTHS, 1, "R");
560 VerifysetSymbols(fr, UDAT_STANDALONE_MONTHS, 11, "dezember");
561 VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_MONTHS, 7, "Aug");
562 VerifysetSymbols(fr, UDAT_STANDALONE_NARROW_MONTHS, 2, "M");
563 VerifysetSymbols(fr, UDAT_QUARTERS, 0, "1. Quart");
564 VerifysetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "QQ2");
565 VerifysetSymbols(fr, UDAT_STANDALONE_QUARTERS, 2, "3rd Quar.");
566 VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_QUARTERS, 3, "4QQ");
b75a7d8f
A
567
568
569 /*run series of tests to test get and setSymbols regressively*/
570 log_verbose("\nTesting get and set symbols regressively\n");
571 VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 1);
572 VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 7);
573 VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 1);
574 VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 7);
575 VerifygetsetSymbols(fr, def, UDAT_MONTHS, 0);
576 VerifygetsetSymbols(fr, def, UDAT_SHORT_MONTHS, 0);
577 VerifygetsetSymbols(fr, def, UDAT_ERAS,1);
578 VerifygetsetSymbols(fr, def, UDAT_LOCALIZED_CHARS, 0);
579 VerifygetsetSymbols(fr, def, UDAT_AM_PMS, 1);
580
581
582 /*closing*/
583
584 udat_close(fr);
585 udat_close(def);
586 if(result != NULL) {
587 free(result);
588 result = NULL;
589 }
590 free(value);
591
592}
593
594/**
595 * Test DateFormat(Calendar) API
596 */
597static void TestDateFormatCalendar() {
598 UDateFormat *date=0, *time=0, *full=0;
599 UCalendar *cal=0;
600 UChar buf[256];
601 char cbuf[256];
602 int32_t pos;
603 UDate when;
604 UErrorCode ec = U_ZERO_ERROR;
605
374ca955
A
606 ctest_setTimeZone(NULL, &ec);
607
b75a7d8f
A
608 /* Create a formatter for date fields. */
609 date = udat_open(UDAT_NONE, UDAT_SHORT, "en_US", NULL, 0, NULL, 0, &ec);
610 if (U_FAILURE(ec)) {
611 log_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s\n",
612 u_errorName(ec));
613 goto FAIL;
614 }
615
616 /* Create a formatter for time fields. */
617 time = udat_open(UDAT_SHORT, UDAT_NONE, "en_US", NULL, 0, NULL, 0, &ec);
618 if (U_FAILURE(ec)) {
619 log_err("FAIL: udat_open(SHORT, NONE, en_US) failed with %s\n",
620 u_errorName(ec));
621 goto FAIL;
622 }
623
624 /* Create a full format for output */
625 full = udat_open(UDAT_FULL, UDAT_FULL, "en_US", NULL, 0, NULL, 0, &ec);
626 if (U_FAILURE(ec)) {
627 log_err("FAIL: udat_open(FULL, FULL, en_US) failed with %s\n",
628 u_errorName(ec));
629 goto FAIL;
630 }
631
632 /* Create a calendar */
633 cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &ec);
634 if (U_FAILURE(ec)) {
635 log_err("FAIL: ucal_open(en_US) failed with %s\n",
636 u_errorName(ec));
637 goto FAIL;
638 }
639
640 /* Parse the date */
641 ucal_clear(cal);
642 u_uastrcpy(buf, "4/5/2001");
643 pos = 0;
644 udat_parseCalendar(date, cal, buf, -1, &pos, &ec);
645 if (U_FAILURE(ec)) {
646 log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n",
647 pos, u_errorName(ec));
648 goto FAIL;
649 }
650
651 /* Parse the time */
652 u_uastrcpy(buf, "5:45 PM");
653 pos = 0;
654 udat_parseCalendar(time, cal, buf, -1, &pos, &ec);
655 if (U_FAILURE(ec)) {
656 log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n",
657 pos, u_errorName(ec));
658 goto FAIL;
659 }
660
661 /* Check result */
662 when = ucal_getMillis(cal, &ec);
663 if (U_FAILURE(ec)) {
664 log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec));
665 goto FAIL;
666 }
667 udat_format(full, when, buf, sizeof(buf), NULL, &ec);
668 if (U_FAILURE(ec)) {
669 log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec));
670 goto FAIL;
671 }
672 u_austrcpy(cbuf, buf);
673 /* Thursday, April 5, 2001 5:45:00 PM PDT 986517900000 */
674 if (when == 986517900000.0) {
675 log_verbose("Ok: Parsed result: %s\n", cbuf);
676 } else {
374ca955 677 log_err("FAIL: Parsed result: %s, exp 4/5/2001 5:45 PM\n", cbuf);
b75a7d8f
A
678 }
679
680 FAIL:
681 udat_close(date);
682 udat_close(time);
683 udat_close(full);
684 ucal_close(cal);
374ca955
A
685
686 ctest_resetTimeZone();
b75a7d8f
A
687}
688
689/*INTERNAL FUNCTIONS USED*/
690static void VerifygetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t index, const char* expected)
691{
692 UChar *pattern=NULL;
693 UErrorCode status = U_ZERO_ERROR;
694 UChar *result=NULL;
695 int32_t resultlength, resultlengthout;
696
697
698 pattern=(UChar*)malloc(sizeof(UChar) * (strlen(expected)+1));
699 u_uastrcpy(pattern, expected);
700 resultlength=0;
701 resultlengthout=udat_getSymbols(datfor, type, index , NULL, resultlength, &status);
702 if(status==U_BUFFER_OVERFLOW_ERROR)
703 {
704 status=U_ZERO_ERROR;
705 resultlength=resultlengthout+1;
706 result=(UChar*)malloc(sizeof(UChar) * resultlength);
707 udat_getSymbols(datfor, type, index, result, resultlength, &status);
708
709 }
710 if(U_FAILURE(status))
711 {
712 log_err("FAIL: Error in udat_getSymbols()... %s\n", myErrorName(status) );
713 return;
714 }
715 if(u_strcmp(result, pattern)==0)
716 log_verbose("PASS: getSymbols retrieved the right value\n");
717 else{
718 log_data_err("FAIL: getSymbols retrieved the wrong value\n Expected %s Got %s\n", austrdup(pattern),
719 austrdup(result) );
720 }
721 free(result);
722 free(pattern);
723}
724
725static void VerifysetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t index, const char* expected)
726{
727 UChar *result=NULL;
728 UChar *value=NULL;
729 int32_t resultlength, resultlengthout;
730 UErrorCode status = U_ZERO_ERROR;
731
732 value=(UChar*)malloc(sizeof(UChar) * (strlen(expected) + 1));
733 u_uastrcpy(value, expected);
734 udat_setSymbols(datfor, type, index, value, u_strlen(value), &status);
735 if(U_FAILURE(status))
736 {
737 log_err("FAIL: Error in udat_setSymbols() %s\n", myErrorName(status) );
738 return;
739 }
740
741 resultlength=0;
742 resultlengthout=udat_getSymbols(datfor, type, index, NULL, resultlength, &status);
743 if(status==U_BUFFER_OVERFLOW_ERROR){
744 status=U_ZERO_ERROR;
745 resultlength=resultlengthout+1;
746 result=(UChar*)malloc(sizeof(UChar) * resultlength);
747 udat_getSymbols(datfor, type, index, result, resultlength, &status);
748 }
749 if(U_FAILURE(status)){
750 log_err("FAIL: error in retrieving the value using getSymbols after setting it previously\n %s\n",
751 myErrorName(status) );
752 return;
753 }
754
755 if(u_strcmp(result, value)!=0){
756 log_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(value),
757 austrdup(result) );
758 }
759 else
760 log_verbose("PASS: setSymbols successful\n");
761
762 free(value);
763 free(result);
764}
765
766
767static void VerifygetsetSymbols(UDateFormat* from, UDateFormat* to, UDateFormatSymbolType type, int32_t index)
768{
769 UChar *result=NULL;
770 UChar *value=NULL;
771 int32_t resultlength, resultlengthout;
772 UErrorCode status = U_ZERO_ERROR;
773
774 resultlength=0;
775 resultlengthout=udat_getSymbols(from, type, index , NULL, resultlength, &status);
776 if(status==U_BUFFER_OVERFLOW_ERROR){
777 status=U_ZERO_ERROR;
778 resultlength=resultlengthout+1;
779 result=(UChar*)malloc(sizeof(UChar) * resultlength);
780 udat_getSymbols(from, type, index, result, resultlength, &status);
781 }
782 if(U_FAILURE(status)){
783 log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) );
784 return;
785 }
786
787 resultlength=resultlengthout+1;
788 udat_setSymbols(to, type, index, result, resultlength, &status);
789 if(U_FAILURE(status))
790 {
791 log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) );
792 return;
793 }
794
795 resultlength=0;
796 resultlengthout=udat_getSymbols(to, type, index, NULL, resultlength, &status);
797 if(status==U_BUFFER_OVERFLOW_ERROR){
798 status=U_ZERO_ERROR;
799 resultlength=resultlengthout+1;
800 value=(UChar*)malloc(sizeof(UChar) * resultlength);
801 udat_getSymbols(to, type, index, value, resultlength, &status);
802 }
803 if(U_FAILURE(status)){
804 log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n %s\n",
805 myErrorName(status) );
806 return;
807 }
808
809 if(u_strcmp(result, value)!=0){
810 log_data_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(result),
811 austrdup(value) );
812 }
813 else
814 log_verbose("PASS: setSymbols successful\n");
815
816 free(value);
817 free(result);
818}
819
820
821static UChar* myNumformat(const UNumberFormat* numfor, double d)
822{
823 UChar *result2=NULL;
824 int32_t resultlength, resultlengthneeded;
825 UErrorCode status = U_ZERO_ERROR;
826
827 resultlength=0;
828 resultlengthneeded=unum_formatDouble(numfor, d, NULL, resultlength, NULL, &status);
829 if(status==U_BUFFER_OVERFLOW_ERROR)
830 {
831 status=U_ZERO_ERROR;
832 resultlength=resultlengthneeded+1;
833 /*result2=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /* this leaks */
834 result2=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/
835 unum_formatDouble(numfor, d, result2, resultlength, NULL, &status);
836 }
837 if(U_FAILURE(status))
838 {
839 log_err("FAIL: Error in formatting using unum_format(.....) %s\n", myErrorName(status) );
840 return 0;
841 }
842
843 return result2;
844}
845
374ca955
A
846/**
847 * The search depth for TestExtremeDates. The total number of
848 * dates that will be tested is (2^EXTREME_DATES_DEPTH) - 1.
849 */
850#define EXTREME_DATES_DEPTH 8
851
852/**
853 * Support for TestExtremeDates (below).
854 *
855 * Test a single date to see whether udat_format handles it properly.
856 */
857static UBool _aux1ExtremeDates(UDateFormat* fmt, UDate date,
858 UChar* buf, int32_t buflen, char* cbuf,
859 UErrorCode* ec) {
860 int32_t len = udat_format(fmt, date, buf, buflen, 0, ec);
861 if (!assertSuccess("udat_format", ec)) return FALSE;
862 u_austrncpy(cbuf, buf, buflen);
863 if (len < 4) {
864 log_err("FAIL: udat_format(%g) => \"%s\"\n", date, cbuf);
865 } else {
866 log_verbose("udat_format(%g) => \"%s\"\n", date, cbuf);
867 }
868 return TRUE;
869}
870
871/**
872 * Support for TestExtremeDates (below).
873 *
874 * Recursively test between 'small' and 'large', up to the depth
875 * limit specified by EXTREME_DATES_DEPTH.
876 */
877static UBool _aux2ExtremeDates(UDateFormat* fmt, UDate small, UDate large,
878 UChar* buf, int32_t buflen, char* cbuf,
879 int32_t count,
880 UErrorCode* ec) {
881 /* Logarithmic midpoint; see below */
882 UDate mid = (UDate) exp((log(small) + log(large)) / 2);
883 if (count == EXTREME_DATES_DEPTH) {
884 return TRUE;
885 }
886 return
887 _aux1ExtremeDates(fmt, mid, buf, buflen, cbuf, ec) &&
888 _aux2ExtremeDates(fmt, small, mid, buf, buflen, cbuf, count+1, ec) &&
889 _aux2ExtremeDates(fmt, mid, large, buf, buflen, cbuf, count+1, ec);
890}
891
892/**
893 * http://www.jtcsv.com/cgibin/icu-bugs?findid=3659
894 *
895 * For certain large dates, udat_format crashes on MacOS. This test
896 * attempts to reproduce this problem by doing a recursive logarithmic*
897 * binary search of a predefined interval (from 'small' to 'large').
898 *
899 * The limit of the search is given by EXTREME_DATES_DEPTH, above.
900 *
901 * *The search has to be logarithmic, not linear. A linear search of the
902 * range 0..10^30, for example, will find 0.5*10^30, then 0.25*10^30 and
903 * 0.75*10^30, etc. A logarithmic search will find 10^15, then 10^7.5
904 * and 10^22.5, etc.
905 */
906static void TestExtremeDates() {
907 UDateFormat *fmt;
908 UErrorCode ec;
909 UChar buf[256];
910 char cbuf[256];
911 const double small = 1000; /* 1 sec */
912 const double large = 1e+30; /* well beyond usable UDate range */
913
914 /* There is no need to test larger values from 1e+30 to 1e+300;
915 the failures occur around 1e+27, and never above 1e+30. */
916
917 ec = U_ZERO_ERROR;
918 fmt = udat_open(UDAT_LONG, UDAT_LONG, "en_US",
919 0, 0, 0, 0, &ec);
920 if (!assertSuccess("udat_open", &ec)) return;
921
922 _aux2ExtremeDates(fmt, small, large, buf, LEN(buf), cbuf, 0, &ec);
923
924 udat_close(fmt);
925}
926
73c04bcf
A
927static void TestAllLocales(void) {
928 int32_t idx, dateIdx, timeIdx, localeCount;
929 static const UDateFormatStyle style[] = {
930 UDAT_FULL, UDAT_LONG, UDAT_MEDIUM, UDAT_SHORT
931 };
932 localeCount = uloc_countAvailable();
933 for (idx = 0; idx < localeCount; idx++) {
934 for (dateIdx = 0; dateIdx < (int32_t)(sizeof(style)/sizeof(style[0])); dateIdx++) {
935 for (timeIdx = 0; timeIdx < (int32_t)(sizeof(style)/sizeof(style[0])); timeIdx++) {
936 UErrorCode status = U_ZERO_ERROR;
937 udat_close(udat_open(style[dateIdx], style[timeIdx],
938 uloc_getAvailable(idx), NULL, 0, NULL, 0, &status));
939 if (U_FAILURE(status)) {
940 log_err("FAIL: udat_open(%s) failed with (%s) dateIdx=%d, timeIdx=%d\n",
941 uloc_getAvailable(idx), u_errorName(status), dateIdx, timeIdx);
942 }
943 }
944 }
945 }
946}
947
b75a7d8f 948#endif /* #if !UCONFIG_NO_FORMATTING */