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