]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cdattst.c
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cdattst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
46f4442e 3 * Copyright (c) 1997-2009, 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);
46f4442e 36static void TestRelativeCrash(void);
374ca955
A
37
38#define LEN(a) (sizeof(a)/sizeof(a[0]))
39
b75a7d8f
A
40void addDateForTest(TestNode** root);
41
374ca955 42#define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x)
b75a7d8f
A
43
44void addDateForTest(TestNode** root)
45{
374ca955 46 TESTCASE(TestDateFormat);
46f4442e 47 TESTCASE(TestRelativeDateFormat);
374ca955
A
48 TESTCASE(TestSymbols);
49 TESTCASE(TestDateFormatCalendar);
50 TESTCASE(TestExtremeDates);
73c04bcf 51 TESTCASE(TestAllLocales);
46f4442e 52 TESTCASE(TestRelativeCrash);
b75a7d8f
A
53}
54/* Testing the DateFormat API */
55static void TestDateFormat()
56{
57 UDateFormat *def, *fr, *it, *de, *def1, *fr_pat;
58 UDateFormat *any;
59 UDateFormat *copy;
60 UErrorCode status = U_ZERO_ERROR;
61 UChar* result = NULL;
62 const UCalendar *cal;
63 const UNumberFormat *numformat1, *numformat2;
46f4442e 64 UChar temp[50];
b75a7d8f
A
65 int32_t numlocales;
66 UDate d1;
67 int i;
68 int32_t resultlength;
69 int32_t resultlengthneeded;
70 int32_t parsepos;
71 UDate d = 837039928046.0;
72 double num = -10456.37;
73 /*const char* str="yyyy.MM.dd G 'at' hh:mm:ss z";
74 const char t[]="2/3/76 2:50 AM";*/
75 /*Testing udat_open() to open a dateformat */
374ca955
A
76
77 ctest_setTimeZone(NULL, &status);
78
b75a7d8f
A
79 log_verbose("\nTesting udat_open() with various parameters\n");
80 fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL,0, NULL, 0,&status);
81 if(U_FAILURE(status))
82 {
83 log_err("FAIL: error in creating the dateformat using full time style with french locale\n %s\n",
84 myErrorName(status) );
85 return;
86 }
87 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
88 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
89 /* def = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0, &status); */
90 def = udat_open(UDAT_SHORT, UDAT_SHORT, "en_US", NULL, 0,NULL, 0, &status);
91 if(U_FAILURE(status))
92 {
93 log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
94 myErrorName(status) );
95 return;
96 }
97 it = udat_open(UDAT_DEFAULT, UDAT_MEDIUM, "it_IT", NULL, 0, NULL, 0,&status);
98 if(U_FAILURE(status))
99 {
100 log_err("FAIL: error in creating the dateformat using medium date style with italian locale\n %s\n",
101 myErrorName(status) );
102 return;
103 }
104 de = udat_open(UDAT_LONG, UDAT_LONG, "de_DE", NULL, 0, NULL, 0,&status);
105 if(U_FAILURE(status))
106 {
107 log_err("FAIL: error in creating the dateformat using long time and date styles with german locale\n %s\n",
108 myErrorName(status));
109 return;
110 }
111 /*creating a default dateformat */
112 def1 = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0,NULL, 0, &status);
113 if(U_FAILURE(status))
114 {
115 log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
116 myErrorName(status) );
117 return;
118 }
119
120
121 /*Testing udat_getAvailable() and udat_countAvailable()*/
122 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
123 numlocales=udat_countAvailable();
124 /* use something sensible w/o hardcoding the count */
125 if(numlocales < 0)
126 log_data_err("FAIL: error in countAvailable\n");
127 log_verbose("The number of locales for which date/time formatting patterns are available is %d\n", numlocales);
128
129 for(i=0;i<numlocales;i++) {
130 UErrorCode subStatus = U_ZERO_ERROR;
131 log_verbose("Testing open of %s\n", udat_getAvailable(i));
73c04bcf 132 any = udat_open(UDAT_SHORT, UDAT_SHORT, udat_getAvailable(i), NULL ,0, NULL, 0, &subStatus);
b75a7d8f 133 if(U_FAILURE(subStatus)) {
73c04bcf 134 log_data_err("FAIL: date format %s (getAvailable(%d)) is not instantiable: %s\n", udat_getAvailable(i), i, u_errorName(subStatus));
b75a7d8f
A
135 }
136 udat_close(any);
137 }
138
139 /*Testing udat_clone()*/
140 log_verbose("\nTesting the udat_clone() function of date format\n");
141 copy=udat_clone(def, &status);
142 if(U_FAILURE(status)){
143 log_err("Error in creating the clone using udat_clone: %s\n", myErrorName(status) );
144 }
145 /*if(def != copy)
146 log_err("Error in udat_clone");*/ /*how should i check for equality???? */
147
148 /*Testing udat_format()*/
149 log_verbose("\nTesting the udat_format() function of date format\n");
150 u_uastrcpy(temp, "7/10/96 4:05 PM");
151 /*format using def */
152 resultlength=0;
153 resultlengthneeded=udat_format(def, d, NULL, resultlength, NULL, &status);
154 if(status==U_BUFFER_OVERFLOW_ERROR)
155 {
156 status=U_ZERO_ERROR;
157 resultlength=resultlengthneeded+1;
158 if(result != NULL) {
159 free(result);
160 result = NULL;
161 }
162 result=(UChar*)malloc(sizeof(UChar) * resultlength);
163 udat_format(def, d, result, resultlength, NULL, &status);
164 }
165 if(U_FAILURE(status) || !result)
166 {
167 log_err("FAIL: Error in formatting using udat_format(.....) %s\n", myErrorName(status) );
168 return;
169 }
170 else
171 log_verbose("PASS: formatting successful\n");
172 if(u_strcmp(result, temp)==0)
46f4442e 173 log_verbose("PASS: Date Format for US locale successful using udat_format()\n");
b75a7d8f
A
174 else
175 log_err("FAIL: Date Format for US locale failed using udat_format()\n");
176 /*format using fr */
177
46f4442e 178 u_unescape("10 juil. 1996 16:05:28 \\u00C9tats-Unis (Los Angeles)", temp, 50);
b75a7d8f
A
179 if(result != NULL) {
180 free(result);
181 result = NULL;
182 }
183 result=myDateFormat(fr, d);
184 if(u_strcmp(result, temp)==0)
374ca955 185 log_verbose("PASS: Date Format for french locale successful using udat_format()\n");
b75a7d8f 186 else
46f4442e
A
187 log_data_err("FAIL: Date Format for french locale failed using udat_format().\n" );
188
374ca955 189 /*format using it */
46f4442e 190 u_uastrcpy(temp, "10/lug/1996 16.05.28");
b75a7d8f 191
46f4442e
A
192 {
193 UChar *fmtted;
194 char g[100];
195 char x[100];
196
197 fmtted = myDateFormat(it,d);
198 u_austrcpy(g, fmtted);
199 u_austrcpy(x, temp);
200 if(u_strcmp(fmtted, temp)==0) {
201 log_verbose("PASS: Date Format for italian locale successful uisng udat_format() - wanted %s, got %s\n", x, g);
202 } else {
203 log_data_err("FAIL: Date Format for italian locale failed using udat_format() - wanted %s, got %s\n", x, g);
204 }
205 }
b75a7d8f
A
206
207 /*Testing parsing using udat_parse()*/
208 log_verbose("\nTesting parsing using udat_parse()\n");
209 u_uastrcpy(temp,"2/3/76 2:50 AM");
210 parsepos=0;
374ca955 211 status=U_ZERO_ERROR;
b75a7d8f
A
212
213 d1=udat_parse(def, temp, u_strlen(temp), &parsepos, &status);
214 if(U_FAILURE(status))
215 {
216 log_err("FAIL: Error in parsing using udat_parse(.....) %s\n", myErrorName(status) );
217 }
218 else
219 log_verbose("PASS: parsing succesful\n");
220 /*format it back and check for equality */
221
222
223 if(u_strcmp(myDateFormat(def, d1),temp)!=0)
224 log_err("FAIL: error in parsing\n");
225
374ca955
A
226 /*Testing parsing using udat_parse()*/
227 log_verbose("\nTesting parsing using udat_parse()\n");
228 u_uastrcpy(temp,"2/Don't parse this part");
229 status=U_ZERO_ERROR;
230
231 d1=udat_parse(def, temp, u_strlen(temp), NULL, &status);
232 if(status != U_PARSE_ERROR)
233 {
234 log_err("FAIL: udat_parse(\"bad string\") passed when it should have failed\n");
235 }
236 else
237 log_verbose("PASS: parsing succesful\n");
b75a7d8f
A
238
239
240
241 /*Testing udat_openPattern() */
242 status=U_ZERO_ERROR;
243 log_verbose("\nTesting the udat_openPattern with a specified pattern\n");
244 /*for french locale */
245 fr_pat=udat_open(UDAT_IGNORE, UDAT_IGNORE,"fr_FR",NULL,0,temp, u_strlen(temp), &status);
246 if(U_FAILURE(status))
247 {
248 log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n",
249 myErrorName(status) );
250 }
251 else
252 log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");
253
254
255 /*Testing applyPattern and toPattern */
256 log_verbose("\nTesting applyPattern and toPattern()\n");
257 udat_applyPattern(def1, FALSE, temp, u_strlen(temp));
258 log_verbose("Extracting the pattern\n");
259
260 resultlength=0;
261 resultlengthneeded=udat_toPattern(def1, FALSE, NULL, resultlength, &status);
262 if(status==U_BUFFER_OVERFLOW_ERROR)
263 {
264 status=U_ZERO_ERROR;
265 resultlength=resultlengthneeded + 1;
266 result=(UChar*)malloc(sizeof(UChar) * resultlength);
267 udat_toPattern(def1, FALSE, result, resultlength, &status);
268 }
269 if(U_FAILURE(status))
270 {
271 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
272 myErrorName(status) );
273 }
274 if(u_strcmp(result, temp)!=0)
275 log_err("FAIL: Error in extracting the pattern\n");
276 else
277 log_verbose("PASS: applyPattern and toPattern work fine\n");
278
279 if(result != NULL) {
280 free(result);
281 result = NULL;
282 }
283
284
285 /*Testing getter and setter functions*/
286 /*isLenient and setLenient()*/
287 log_verbose("\nTesting the isLenient and setLenient properties\n");
288 udat_setLenient(fr, udat_isLenient(it));
289 if(udat_isLenient(fr) != udat_isLenient(it))
290 log_err("ERROR: setLenient() failed\n");
291 else
292 log_verbose("PASS: setLenient() successful\n");
293
294
295 /*Test get2DigitYearStart set2DigitYearStart */
296 log_verbose("\nTesting the get and set 2DigitYearStart properties\n");
297 d1= udat_get2DigitYearStart(fr_pat,&status);
298 if(U_FAILURE(status)) {
299 log_err("ERROR: udat_get2DigitYearStart failed %s\n", myErrorName(status) );
300 }
301 status = U_ZERO_ERROR;
302 udat_set2DigitYearStart(def1 ,d1, &status);
303 if(U_FAILURE(status)) {
304 log_err("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) );
305 }
306 if(udat_get2DigitYearStart(fr_pat, &status) != udat_get2DigitYearStart(def1, &status))
307 log_err("FAIL: error in set2DigitYearStart\n");
308 else
309 log_verbose("PASS: set2DigitYearStart successful\n");
310 /*try setting it to another value */
311 udat_set2DigitYearStart(de, 2000.0, &status);
312 if(U_FAILURE(status)){
313 log_verbose("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) );
314 }
315 if(udat_get2DigitYearStart(de, &status) != 2000)
316 log_err("FAIL: error in set2DigitYearStart\n");
317 else
318 log_verbose("PASS: set2DigitYearStart successful\n");
319
320
321
322 /*Test getNumberFormat() and setNumberFormat() */
323 log_verbose("\nTesting the get and set NumberFormat properties of date format\n");
324 numformat1=udat_getNumberFormat(fr_pat);
325 udat_setNumberFormat(def1, numformat1);
326 numformat2=udat_getNumberFormat(def1);
327 if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0)
328 log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
329 else
330 log_verbose("PASS:setNumberFormat and getNumberFormat succesful\n");
331
332 /*try setting the number format to another format */
333 numformat1=udat_getNumberFormat(def);
334 udat_setNumberFormat(def1, numformat1);
335 numformat2=udat_getNumberFormat(def1);
336 if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0)
337 log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
338 else
339 log_verbose("PASS: setNumberFormat and getNumberFormat succesful\n");
340
341
342
343 /*Test getCalendar and setCalendar*/
344 log_verbose("\nTesting the udat_getCalendar() and udat_setCalendar() properties\n");
345 cal=udat_getCalendar(fr_pat);
346
347
348 udat_setCalendar(def1, cal);
349 if(!ucal_equivalentTo(udat_getCalendar(fr_pat), udat_getCalendar(def1)))
350 log_err("FAIL: Error in setting and getting the calendar\n");
351 else
352 log_verbose("PASS: getting and setting calendar successful\n");
353
354 if(result!=NULL) {
355 free(result);
356 }
357
358 /*Closing the UDateForamt */
359 udat_close(def);
360 udat_close(fr);
361 udat_close(it);
362 udat_close(de);
363 udat_close(def1);
364 udat_close(fr_pat);
365 udat_close(copy);
366
374ca955 367 ctest_resetTimeZone();
b75a7d8f
A
368}
369
46f4442e
A
370/*
371Test combined relative date formatting (relative date + non-relative time).
372This is a bit tricky since we can't have static test data for comparison, the
373relative date formatting is relative to the time the tests are run. We generate
374the data for comparison dynamically. However, the tests could fail if they are
375run right at midnight Pacific time and the call to ucal_getNow() is before midnight
376while the calls to udat_format are after midnight or span midnight.
377*/
378static const UDate dayInterval = 24.0*60.0*60.0*1000.0;
379static const UChar trdfZone[] = { 0x0055, 0x0053, 0x002F, 0x0050, 0x0061, 0x0063, 0x0069, 0x0066, 0x0069, 0x0063, 0 }; /* US/Pacific */
380static const char trdfLocale[] = "en_US";
381static const UChar minutesPatn[] = { 0x006D, 0x006D, 0 }; /* "mm" */
382static const UChar monthLongPatn[] = { 0x004D, 0x004D, 0x004D, 0x004D, 0 }; /* "MMMM" */
383static const UChar monthMediumPatn[] = { 0x004D, 0x004D, 0x004D, 0 }; /* "MMM" */
384static const UChar monthShortPatn[] = { 0x004D, 0 }; /* "M" */
385static const UDateFormatStyle dateStylesList[] = { UDAT_FULL, UDAT_LONG, UDAT_MEDIUM, UDAT_SHORT, UDAT_NONE };
386static const UChar *monthPatnsList[] = { monthLongPatn, monthLongPatn, monthMediumPatn, monthShortPatn, NULL };
387static const UChar newTimePatn[] = { 0x0048, 0x0048, 0x002C, 0x006D, 0x006D, 0 }; /* "HH,mm" */
388static const UChar minutesStr[] = { 0x0034, 0x0039, 0 }; /* "49", minutes string to search for in output */
389enum { kDateOrTimeOutMax = 96, kDateAndTimeOutMax = 192 };
390
391static void TestRelativeDateFormat()
392{
393 UDate today = 0.0;
394 const UDateFormatStyle * stylePtr;
395 const UChar ** monthPtnPtr;
396 UErrorCode status = U_ZERO_ERROR;
397 UCalendar * ucal = ucal_open(trdfZone, -1, trdfLocale, UCAL_GREGORIAN, &status);
398 if ( U_SUCCESS(status) ) {
399 int32_t year, month, day;
400 ucal_setMillis(ucal, ucal_getNow(), &status);
401 year = ucal_get(ucal, UCAL_YEAR, &status);
402 month = ucal_get(ucal, UCAL_MONTH, &status);
403 day = ucal_get(ucal, UCAL_DATE, &status);
404 ucal_setDateTime(ucal, year, month, day, 18, 49, 0, &status); /* set to today at 18:49:00 */
405 today = ucal_getMillis(ucal, &status);
406 ucal_close(ucal);
407 }
408 if ( U_FAILURE(status) || today == 0.0 ) {
409 log_err("Generate UDate for a specified time today fails, error %s\n", myErrorName(status) );
410 return;
411 }
412 for (stylePtr = dateStylesList, monthPtnPtr = monthPatnsList; *stylePtr != UDAT_NONE; ++stylePtr, ++monthPtnPtr) {
413 UDateFormat* fmtRelDateTime;
414 UDateFormat* fmtRelDate;
415 UDateFormat* fmtTime;
416 int32_t dayOffset, limit;
417 UFieldPosition fp;
418 UChar strDateTime[kDateAndTimeOutMax];
419 UChar strDate[kDateOrTimeOutMax];
420 UChar strTime[kDateOrTimeOutMax];
421 UChar * strPtr;
422 int32_t dtpatLen;
423
424 fmtRelDateTime = udat_open(UDAT_SHORT, *stylePtr | UDAT_RELATIVE, trdfLocale, trdfZone, -1, NULL, 0, &status);
425 if ( U_FAILURE(status) ) {
426 log_err("udat_open timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
427 continue;
428 }
429 fmtRelDate = udat_open(UDAT_NONE, *stylePtr | UDAT_RELATIVE, trdfLocale, trdfZone, -1, NULL, 0, &status);
430 if ( U_FAILURE(status) ) {
431 log_err("udat_open timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
432 udat_close(fmtRelDateTime);
433 continue;
434 }
435 fmtTime = udat_open(UDAT_SHORT, UDAT_NONE, trdfLocale, trdfZone, -1, NULL, 0, &status);
436 if ( U_FAILURE(status) ) {
437 log_err("udat_open timeStyle SHORT dateStyle NONE fails, error %s\n", myErrorName(status) );
438 udat_close(fmtRelDateTime);
439 udat_close(fmtRelDate);
440 continue;
441 }
442
443 dtpatLen = udat_toPatternRelativeDate(fmtRelDateTime, strDate, kDateAndTimeOutMax, &status);
444 if ( U_FAILURE(status) ) {
445 log_err("udat_toPatternRelativeDate timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
446 status = U_ZERO_ERROR;
447 } else if ( u_strstr(strDate, *monthPtnPtr) == NULL || dtpatLen != u_strlen(strDate) ) {
448 log_err("udat_toPatternRelativeDate timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) date pattern incorrect\n", *stylePtr );
449 }
450 dtpatLen = udat_toPatternRelativeTime(fmtRelDateTime, strTime, kDateAndTimeOutMax, &status);
451 if ( U_FAILURE(status) ) {
452 log_err("udat_toPatternRelativeTime timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
453 status = U_ZERO_ERROR;
454 } else if ( u_strstr(strTime, minutesPatn) == NULL || dtpatLen != u_strlen(strTime) ) {
455 log_err("udat_toPatternRelativeTime timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) time pattern incorrect\n", *stylePtr );
456 }
457 dtpatLen = udat_toPattern(fmtRelDateTime, FALSE, strDateTime, kDateAndTimeOutMax, &status);
458 if ( U_FAILURE(status) ) {
459 log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
460 status = U_ZERO_ERROR;
461 } else if ( u_strstr(strDateTime, strDate) == NULL || u_strstr(strDateTime, strTime) == NULL || dtpatLen != u_strlen(strDateTime) ) {
462 log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) dateTime pattern incorrect\n", *stylePtr );
463 }
464 udat_applyPatternRelative(fmtRelDateTime, strDate, u_strlen(strDate), newTimePatn, u_strlen(newTimePatn), &status);
465 if ( U_FAILURE(status) ) {
466 log_err("udat_applyPatternRelative timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
467 status = U_ZERO_ERROR;
468 } else {
469 udat_toPattern(fmtRelDateTime, FALSE, strDateTime, kDateAndTimeOutMax, &status);
470 if ( U_FAILURE(status) ) {
471 log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
472 status = U_ZERO_ERROR;
473 } else if ( u_strstr(strDateTime, newTimePatn) == NULL ) {
474 log_err("udat_applyPatternRelative timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) didn't update time pattern\n", *stylePtr );
475 }
476 }
477 udat_applyPatternRelative(fmtRelDateTime, strDate, u_strlen(strDate), strTime, u_strlen(strTime), &status); /* restore original */
478
479 fp.field = UDAT_MINUTE_FIELD;
480 for (dayOffset = -2, limit = 2; dayOffset <= limit; ++dayOffset) {
481 UDate dateToUse = today + (float)dayOffset*dayInterval;
482
483 udat_format(fmtRelDateTime, dateToUse, strDateTime, kDateAndTimeOutMax, &fp, &status);
484 if ( U_FAILURE(status) ) {
485 log_err("udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
486 status = U_ZERO_ERROR;
487 } else {
488 udat_format(fmtRelDate, dateToUse, strDate, kDateOrTimeOutMax, NULL, &status);
489 if ( U_FAILURE(status) ) {
490 log_err("udat_format timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
491 status = U_ZERO_ERROR;
492 } else if ( u_strstr(strDateTime, strDate) == NULL ) {
493 log_err("relative date string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr );
494 }
495
496 udat_format(fmtTime, dateToUse, strTime, kDateOrTimeOutMax, NULL, &status);
497 if ( U_FAILURE(status) ) {
498 log_err("udat_format timeStyle SHORT dateStyle NONE fails, error %s\n", myErrorName(status) );
499 status = U_ZERO_ERROR;
500 } else if ( u_strstr(strDateTime, strTime) == NULL ) {
501 log_err("time string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr );
502 }
503
504 strPtr = u_strstr(strDateTime, minutesStr);
505 if ( strPtr != NULL ) {
506 int32_t beginIndex = strPtr - strDateTime;
507 if ( fp.beginIndex != beginIndex ) {
508 log_err("UFieldPosition beginIndex %d, expected %d, in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", fp.beginIndex, beginIndex, *stylePtr );
509 }
510 } else {
511 log_err("minutes string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr );
512 }
513 }
514 }
515
516 udat_close(fmtRelDateTime);
517 udat_close(fmtRelDate);
518 udat_close(fmtTime);
519 }
520}
521
b75a7d8f
A
522/*Testing udat_getSymbols() and udat_setSymbols() and udat_countSymbols()*/
523static void TestSymbols()
524{
525 UDateFormat *def, *fr;
526 UErrorCode status = U_ZERO_ERROR;
527 UChar *value=NULL;
528 UChar *result = NULL;
529 int32_t resultlength;
530 int32_t resultlengthout;
531 UChar *pattern;
532
533
534 /*creating a dateformat with french locale */
535 log_verbose("\ncreating a date format with french locale\n");
536 fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL, 0, NULL, 0, &status);
537 if(U_FAILURE(status))
538 {
539 log_err("error in creating the dateformat using full time style with french locale\n %s\n",
540 myErrorName(status) );
541 return;
542 }
543 /*creating a default dateformat */
544 log_verbose("\ncreating a date format with default locale\n");
545 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
546 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
547 /* def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status); */
548 def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status);
549 if(U_FAILURE(status))
550 {
551 log_err("error in creating the dateformat using short date and time style\n %s\n",
552 myErrorName(status) );
553 return;
554 }
555
556
557 /*Testing countSymbols, getSymbols and setSymbols*/
558 log_verbose("\nTesting countSymbols\n");
559 /*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 */
560 if(udat_countSymbols(def, UDAT_ERAS)!=2 || udat_countSymbols(def, UDAT_MONTHS)!=12 ||
561 udat_countSymbols(def, UDAT_SHORT_MONTHS)!=12 || udat_countSymbols(def, UDAT_WEEKDAYS)!=8 ||
562 udat_countSymbols(def, UDAT_SHORT_WEEKDAYS)!=8 || udat_countSymbols(def, UDAT_AM_PMS)!=2 ||
73c04bcf 563 udat_countSymbols(def, UDAT_QUARTERS) != 4 || udat_countSymbols(def, UDAT_SHORT_QUARTERS) != 4 ||
b75a7d8f
A
564 udat_countSymbols(def, UDAT_LOCALIZED_CHARS)!=1)
565 {
566 log_err("FAIL: error in udat_countSymbols\n");
567 }
568 else
569 log_verbose("PASS: udat_countSymbols() successful\n");
570
571 /*testing getSymbols*/
572 log_verbose("\nTesting getSymbols\n");
573 pattern=(UChar*)malloc(sizeof(UChar) * 10);
574 u_uastrcpy(pattern, "jeudi");
575 resultlength=0;
576 resultlengthout=udat_getSymbols(fr, UDAT_WEEKDAYS, 5 , NULL, resultlength, &status);
577 if(status==U_BUFFER_OVERFLOW_ERROR)
578 {
579 status=U_ZERO_ERROR;
580 resultlength=resultlengthout+1;
581 if(result != NULL) {
582 free(result);
583 result = NULL;
584 }
585 result=(UChar*)malloc(sizeof(UChar) * resultlength);
586 udat_getSymbols(fr, UDAT_WEEKDAYS, 5, result, resultlength, &status);
587
588 }
589 if(U_FAILURE(status))
590 {
591 log_err("FAIL: Error in udat_getSymbols().... %s\n", myErrorName(status) );
592 }
593 else
594 log_verbose("PASS: getSymbols succesful\n");
595
596 if(u_strcmp(result, pattern)==0)
597 log_verbose("PASS: getSymbols retrieved the right value\n");
598 else
599 log_data_err("FAIL: getSymbols retrieved the wrong value\n");
600
601 /*run series of tests to test getsymbols regressively*/
602 log_verbose("\nTesting getSymbols() regressively\n");
603 VerifygetSymbols(fr, UDAT_WEEKDAYS, 1, "dimanche");
604 VerifygetSymbols(def, UDAT_WEEKDAYS, 1, "Sunday");
605 VerifygetSymbols(fr, UDAT_SHORT_WEEKDAYS, 7, "sam.");
606 VerifygetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Sat");
607 VerifygetSymbols(def, UDAT_MONTHS, 11, "December");
608 VerifygetSymbols(def, UDAT_MONTHS, 0, "January");
609 VerifygetSymbols(fr, UDAT_ERAS, 0, "av. J.-C.");
610 VerifygetSymbols(def, UDAT_AM_PMS, 0, "AM");
611 VerifygetSymbols(def, UDAT_AM_PMS, 1, "PM");
612 VerifygetSymbols(fr, UDAT_SHORT_MONTHS, 0, "janv.");
613 VerifygetSymbols(def, UDAT_SHORT_MONTHS, 11, "Dec");
73c04bcf
A
614 VerifygetSymbols(fr, UDAT_QUARTERS, 0, "1er trimestre");
615 VerifygetSymbols(def, UDAT_QUARTERS, 3, "4th quarter");
616 VerifygetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "T2");
617 VerifygetSymbols(def, UDAT_SHORT_QUARTERS, 2, "Q3");
46f4442e 618 VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcLQqV");
b75a7d8f
A
619
620
621 if(result != NULL) {
622 free(result);
623 result = NULL;
624 }
625free(pattern);
626
627 log_verbose("\nTesting setSymbols\n");
628 /*applying the pattern so that setSymbolss works */
629 resultlength=0;
630 resultlengthout=udat_toPattern(fr, FALSE, NULL, resultlength, &status);
631 if(status==U_BUFFER_OVERFLOW_ERROR)
632 {
633 status=U_ZERO_ERROR;
634 resultlength=resultlengthout + 1;
635 pattern=(UChar*)malloc(sizeof(UChar) * resultlength);
636 udat_toPattern(fr, FALSE, pattern, resultlength, &status);
637 }
638 if(U_FAILURE(status))
639 {
640 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
641 myErrorName(status) );
642 }
643
644 udat_applyPattern(def, FALSE, pattern, u_strlen(pattern));
645 resultlength=0;
646 resultlengthout=udat_toPattern(def, FALSE, NULL, resultlength,&status);
647 if(status==U_BUFFER_OVERFLOW_ERROR)
648 {
649 status=U_ZERO_ERROR;
650 resultlength=resultlengthout + 1;
651 if(result != NULL) {
652 free(result);
653 result = NULL;
654 }
655 result=(UChar*)malloc(sizeof(UChar) * resultlength);
656 udat_toPattern(fr, FALSE,result, resultlength, &status);
657 }
658 if(U_FAILURE(status))
659 {
660 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
661 myErrorName(status) );
662 }
663 if(u_strcmp(result, pattern)==0)
664 log_verbose("Pattern applied properly\n");
665 else
666 log_err("pattern could not be applied properly\n");
667
668free(pattern);
669 /*testing set symbols */
670 resultlength=0;
671 resultlengthout=udat_getSymbols(fr, UDAT_MONTHS, 11 , NULL, resultlength, &status);
672 if(status==U_BUFFER_OVERFLOW_ERROR){
673 status=U_ZERO_ERROR;
674 resultlength=resultlengthout+1;
675 if(result != NULL) {
676 free(result);
677 result = NULL;
678 }
679 result=(UChar*)malloc(sizeof(UChar) * resultlength);
680 udat_getSymbols(fr, UDAT_MONTHS, 11, result, resultlength, &status);
681
682 }
683 if(U_FAILURE(status))
684 log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) );
685 resultlength=resultlengthout+1;
686
687 udat_setSymbols(def, UDAT_MONTHS, 11, result, resultlength, &status);
688 if(U_FAILURE(status))
689 {
690 log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) );
691 }
692 else
693 log_verbose("PASS: SetSymbols successful\n");
694
695 resultlength=0;
696 resultlengthout=udat_getSymbols(def, UDAT_MONTHS, 11, NULL, resultlength, &status);
697 if(status==U_BUFFER_OVERFLOW_ERROR){
698 status=U_ZERO_ERROR;
699 resultlength=resultlengthout+1;
700 value=(UChar*)malloc(sizeof(UChar) * resultlength);
701 udat_getSymbols(def, UDAT_MONTHS, 11, value, resultlength, &status);
702 }
703 if(U_FAILURE(status))
704 log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n");
705
706 if(u_strcmp(result, value)!=0)
707 log_data_err("FAIL: Error in settting and getting symbols\n");
708 else
709 log_verbose("PASS: setSymbols successful\n");
710
711
712 /*run series of tests to test setSymbols regressively*/
713 log_verbose("\nTesting setSymbols regressively\n");
b75a7d8f 714 VerifysetSymbols(def, UDAT_ERAS, 0, "BeforeChrist");
73c04bcf
A
715 VerifysetSymbols(def, UDAT_ERA_NAMES, 1, "AnnoDomini");
716 VerifysetSymbols(def, UDAT_WEEKDAYS, 1, "Sundayweek");
b75a7d8f 717 VerifysetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Satweek");
73c04bcf
A
718 VerifysetSymbols(def, UDAT_NARROW_WEEKDAYS, 4, "M");
719 VerifysetSymbols(def, UDAT_STANDALONE_WEEKDAYS, 1, "Sonntagweek");
720 VerifysetSymbols(def, UDAT_STANDALONE_SHORT_WEEKDAYS, 7, "Sams");
721 VerifysetSymbols(def, UDAT_STANDALONE_NARROW_WEEKDAYS, 4, "V");
b75a7d8f
A
722 VerifysetSymbols(fr, UDAT_MONTHS, 11, "december");
723 VerifysetSymbols(fr, UDAT_SHORT_MONTHS, 0, "Jan");
73c04bcf
A
724 VerifysetSymbols(fr, UDAT_NARROW_MONTHS, 1, "R");
725 VerifysetSymbols(fr, UDAT_STANDALONE_MONTHS, 11, "dezember");
726 VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_MONTHS, 7, "Aug");
727 VerifysetSymbols(fr, UDAT_STANDALONE_NARROW_MONTHS, 2, "M");
728 VerifysetSymbols(fr, UDAT_QUARTERS, 0, "1. Quart");
729 VerifysetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "QQ2");
730 VerifysetSymbols(fr, UDAT_STANDALONE_QUARTERS, 2, "3rd Quar.");
731 VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_QUARTERS, 3, "4QQ");
b75a7d8f
A
732
733
734 /*run series of tests to test get and setSymbols regressively*/
735 log_verbose("\nTesting get and set symbols regressively\n");
736 VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 1);
737 VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 7);
738 VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 1);
739 VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 7);
740 VerifygetsetSymbols(fr, def, UDAT_MONTHS, 0);
741 VerifygetsetSymbols(fr, def, UDAT_SHORT_MONTHS, 0);
742 VerifygetsetSymbols(fr, def, UDAT_ERAS,1);
743 VerifygetsetSymbols(fr, def, UDAT_LOCALIZED_CHARS, 0);
744 VerifygetsetSymbols(fr, def, UDAT_AM_PMS, 1);
745
746
747 /*closing*/
748
749 udat_close(fr);
750 udat_close(def);
751 if(result != NULL) {
752 free(result);
753 result = NULL;
754 }
755 free(value);
756
757}
758
759/**
760 * Test DateFormat(Calendar) API
761 */
762static void TestDateFormatCalendar() {
763 UDateFormat *date=0, *time=0, *full=0;
764 UCalendar *cal=0;
765 UChar buf[256];
766 char cbuf[256];
767 int32_t pos;
768 UDate when;
769 UErrorCode ec = U_ZERO_ERROR;
770
374ca955
A
771 ctest_setTimeZone(NULL, &ec);
772
b75a7d8f
A
773 /* Create a formatter for date fields. */
774 date = udat_open(UDAT_NONE, UDAT_SHORT, "en_US", NULL, 0, NULL, 0, &ec);
775 if (U_FAILURE(ec)) {
776 log_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s\n",
777 u_errorName(ec));
778 goto FAIL;
779 }
780
781 /* Create a formatter for time fields. */
782 time = udat_open(UDAT_SHORT, UDAT_NONE, "en_US", NULL, 0, NULL, 0, &ec);
783 if (U_FAILURE(ec)) {
784 log_err("FAIL: udat_open(SHORT, NONE, en_US) failed with %s\n",
785 u_errorName(ec));
786 goto FAIL;
787 }
788
789 /* Create a full format for output */
790 full = udat_open(UDAT_FULL, UDAT_FULL, "en_US", NULL, 0, NULL, 0, &ec);
791 if (U_FAILURE(ec)) {
792 log_err("FAIL: udat_open(FULL, FULL, en_US) failed with %s\n",
793 u_errorName(ec));
794 goto FAIL;
795 }
796
797 /* Create a calendar */
798 cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &ec);
799 if (U_FAILURE(ec)) {
800 log_err("FAIL: ucal_open(en_US) failed with %s\n",
801 u_errorName(ec));
802 goto FAIL;
803 }
804
805 /* Parse the date */
806 ucal_clear(cal);
807 u_uastrcpy(buf, "4/5/2001");
808 pos = 0;
809 udat_parseCalendar(date, cal, buf, -1, &pos, &ec);
810 if (U_FAILURE(ec)) {
811 log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n",
812 pos, u_errorName(ec));
813 goto FAIL;
814 }
815
816 /* Parse the time */
817 u_uastrcpy(buf, "5:45 PM");
818 pos = 0;
819 udat_parseCalendar(time, cal, buf, -1, &pos, &ec);
820 if (U_FAILURE(ec)) {
821 log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n",
822 pos, u_errorName(ec));
823 goto FAIL;
824 }
825
826 /* Check result */
827 when = ucal_getMillis(cal, &ec);
828 if (U_FAILURE(ec)) {
829 log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec));
830 goto FAIL;
831 }
832 udat_format(full, when, buf, sizeof(buf), NULL, &ec);
833 if (U_FAILURE(ec)) {
834 log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec));
835 goto FAIL;
836 }
837 u_austrcpy(cbuf, buf);
838 /* Thursday, April 5, 2001 5:45:00 PM PDT 986517900000 */
839 if (when == 986517900000.0) {
840 log_verbose("Ok: Parsed result: %s\n", cbuf);
841 } else {
374ca955 842 log_err("FAIL: Parsed result: %s, exp 4/5/2001 5:45 PM\n", cbuf);
b75a7d8f
A
843 }
844
845 FAIL:
846 udat_close(date);
847 udat_close(time);
848 udat_close(full);
849 ucal_close(cal);
374ca955
A
850
851 ctest_resetTimeZone();
b75a7d8f
A
852}
853
854/*INTERNAL FUNCTIONS USED*/
855static void VerifygetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t index, const char* expected)
856{
857 UChar *pattern=NULL;
858 UErrorCode status = U_ZERO_ERROR;
859 UChar *result=NULL;
860 int32_t resultlength, resultlengthout;
861
862
863 pattern=(UChar*)malloc(sizeof(UChar) * (strlen(expected)+1));
864 u_uastrcpy(pattern, expected);
865 resultlength=0;
866 resultlengthout=udat_getSymbols(datfor, type, index , NULL, resultlength, &status);
867 if(status==U_BUFFER_OVERFLOW_ERROR)
868 {
869 status=U_ZERO_ERROR;
870 resultlength=resultlengthout+1;
871 result=(UChar*)malloc(sizeof(UChar) * resultlength);
872 udat_getSymbols(datfor, type, index, result, resultlength, &status);
873
874 }
875 if(U_FAILURE(status))
876 {
877 log_err("FAIL: Error in udat_getSymbols()... %s\n", myErrorName(status) );
878 return;
879 }
880 if(u_strcmp(result, pattern)==0)
881 log_verbose("PASS: getSymbols retrieved the right value\n");
882 else{
883 log_data_err("FAIL: getSymbols retrieved the wrong value\n Expected %s Got %s\n", austrdup(pattern),
884 austrdup(result) );
885 }
886 free(result);
887 free(pattern);
888}
889
890static void VerifysetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t index, const char* expected)
891{
892 UChar *result=NULL;
893 UChar *value=NULL;
894 int32_t resultlength, resultlengthout;
895 UErrorCode status = U_ZERO_ERROR;
896
897 value=(UChar*)malloc(sizeof(UChar) * (strlen(expected) + 1));
898 u_uastrcpy(value, expected);
899 udat_setSymbols(datfor, type, index, value, u_strlen(value), &status);
900 if(U_FAILURE(status))
901 {
902 log_err("FAIL: Error in udat_setSymbols() %s\n", myErrorName(status) );
903 return;
904 }
905
906 resultlength=0;
907 resultlengthout=udat_getSymbols(datfor, type, index, NULL, resultlength, &status);
908 if(status==U_BUFFER_OVERFLOW_ERROR){
909 status=U_ZERO_ERROR;
910 resultlength=resultlengthout+1;
911 result=(UChar*)malloc(sizeof(UChar) * resultlength);
912 udat_getSymbols(datfor, type, index, result, resultlength, &status);
913 }
914 if(U_FAILURE(status)){
915 log_err("FAIL: error in retrieving the value using getSymbols after setting it previously\n %s\n",
916 myErrorName(status) );
917 return;
918 }
919
920 if(u_strcmp(result, value)!=0){
921 log_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(value),
922 austrdup(result) );
923 }
924 else
925 log_verbose("PASS: setSymbols successful\n");
926
927 free(value);
928 free(result);
929}
930
931
932static void VerifygetsetSymbols(UDateFormat* from, UDateFormat* to, UDateFormatSymbolType type, int32_t index)
933{
934 UChar *result=NULL;
935 UChar *value=NULL;
936 int32_t resultlength, resultlengthout;
937 UErrorCode status = U_ZERO_ERROR;
938
939 resultlength=0;
940 resultlengthout=udat_getSymbols(from, type, index , NULL, resultlength, &status);
941 if(status==U_BUFFER_OVERFLOW_ERROR){
942 status=U_ZERO_ERROR;
943 resultlength=resultlengthout+1;
944 result=(UChar*)malloc(sizeof(UChar) * resultlength);
945 udat_getSymbols(from, type, index, result, resultlength, &status);
946 }
947 if(U_FAILURE(status)){
948 log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) );
949 return;
950 }
951
952 resultlength=resultlengthout+1;
953 udat_setSymbols(to, type, index, result, resultlength, &status);
954 if(U_FAILURE(status))
955 {
956 log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) );
957 return;
958 }
959
960 resultlength=0;
961 resultlengthout=udat_getSymbols(to, type, index, NULL, resultlength, &status);
962 if(status==U_BUFFER_OVERFLOW_ERROR){
963 status=U_ZERO_ERROR;
964 resultlength=resultlengthout+1;
965 value=(UChar*)malloc(sizeof(UChar) * resultlength);
966 udat_getSymbols(to, type, index, value, resultlength, &status);
967 }
968 if(U_FAILURE(status)){
969 log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n %s\n",
970 myErrorName(status) );
971 return;
972 }
973
974 if(u_strcmp(result, value)!=0){
975 log_data_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(result),
976 austrdup(value) );
977 }
978 else
979 log_verbose("PASS: setSymbols successful\n");
980
981 free(value);
982 free(result);
983}
984
985
986static UChar* myNumformat(const UNumberFormat* numfor, double d)
987{
988 UChar *result2=NULL;
989 int32_t resultlength, resultlengthneeded;
990 UErrorCode status = U_ZERO_ERROR;
991
992 resultlength=0;
993 resultlengthneeded=unum_formatDouble(numfor, d, NULL, resultlength, NULL, &status);
994 if(status==U_BUFFER_OVERFLOW_ERROR)
995 {
996 status=U_ZERO_ERROR;
997 resultlength=resultlengthneeded+1;
998 /*result2=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /* this leaks */
999 result2=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/
1000 unum_formatDouble(numfor, d, result2, resultlength, NULL, &status);
1001 }
1002 if(U_FAILURE(status))
1003 {
1004 log_err("FAIL: Error in formatting using unum_format(.....) %s\n", myErrorName(status) );
1005 return 0;
1006 }
1007
1008 return result2;
1009}
1010
374ca955
A
1011/**
1012 * The search depth for TestExtremeDates. The total number of
1013 * dates that will be tested is (2^EXTREME_DATES_DEPTH) - 1.
1014 */
1015#define EXTREME_DATES_DEPTH 8
1016
1017/**
1018 * Support for TestExtremeDates (below).
1019 *
1020 * Test a single date to see whether udat_format handles it properly.
1021 */
1022static UBool _aux1ExtremeDates(UDateFormat* fmt, UDate date,
1023 UChar* buf, int32_t buflen, char* cbuf,
1024 UErrorCode* ec) {
1025 int32_t len = udat_format(fmt, date, buf, buflen, 0, ec);
1026 if (!assertSuccess("udat_format", ec)) return FALSE;
1027 u_austrncpy(cbuf, buf, buflen);
1028 if (len < 4) {
1029 log_err("FAIL: udat_format(%g) => \"%s\"\n", date, cbuf);
1030 } else {
1031 log_verbose("udat_format(%g) => \"%s\"\n", date, cbuf);
1032 }
1033 return TRUE;
1034}
1035
1036/**
1037 * Support for TestExtremeDates (below).
1038 *
1039 * Recursively test between 'small' and 'large', up to the depth
1040 * limit specified by EXTREME_DATES_DEPTH.
1041 */
1042static UBool _aux2ExtremeDates(UDateFormat* fmt, UDate small, UDate large,
1043 UChar* buf, int32_t buflen, char* cbuf,
1044 int32_t count,
1045 UErrorCode* ec) {
1046 /* Logarithmic midpoint; see below */
1047 UDate mid = (UDate) exp((log(small) + log(large)) / 2);
1048 if (count == EXTREME_DATES_DEPTH) {
1049 return TRUE;
1050 }
1051 return
1052 _aux1ExtremeDates(fmt, mid, buf, buflen, cbuf, ec) &&
1053 _aux2ExtremeDates(fmt, small, mid, buf, buflen, cbuf, count+1, ec) &&
1054 _aux2ExtremeDates(fmt, mid, large, buf, buflen, cbuf, count+1, ec);
1055}
1056
1057/**
1058 * http://www.jtcsv.com/cgibin/icu-bugs?findid=3659
1059 *
1060 * For certain large dates, udat_format crashes on MacOS. This test
1061 * attempts to reproduce this problem by doing a recursive logarithmic*
1062 * binary search of a predefined interval (from 'small' to 'large').
1063 *
1064 * The limit of the search is given by EXTREME_DATES_DEPTH, above.
1065 *
1066 * *The search has to be logarithmic, not linear. A linear search of the
1067 * range 0..10^30, for example, will find 0.5*10^30, then 0.25*10^30 and
1068 * 0.75*10^30, etc. A logarithmic search will find 10^15, then 10^7.5
1069 * and 10^22.5, etc.
1070 */
1071static void TestExtremeDates() {
1072 UDateFormat *fmt;
1073 UErrorCode ec;
1074 UChar buf[256];
1075 char cbuf[256];
1076 const double small = 1000; /* 1 sec */
1077 const double large = 1e+30; /* well beyond usable UDate range */
1078
1079 /* There is no need to test larger values from 1e+30 to 1e+300;
1080 the failures occur around 1e+27, and never above 1e+30. */
1081
1082 ec = U_ZERO_ERROR;
1083 fmt = udat_open(UDAT_LONG, UDAT_LONG, "en_US",
1084 0, 0, 0, 0, &ec);
1085 if (!assertSuccess("udat_open", &ec)) return;
1086
1087 _aux2ExtremeDates(fmt, small, large, buf, LEN(buf), cbuf, 0, &ec);
1088
1089 udat_close(fmt);
1090}
1091
73c04bcf
A
1092static void TestAllLocales(void) {
1093 int32_t idx, dateIdx, timeIdx, localeCount;
1094 static const UDateFormatStyle style[] = {
1095 UDAT_FULL, UDAT_LONG, UDAT_MEDIUM, UDAT_SHORT
1096 };
1097 localeCount = uloc_countAvailable();
1098 for (idx = 0; idx < localeCount; idx++) {
1099 for (dateIdx = 0; dateIdx < (int32_t)(sizeof(style)/sizeof(style[0])); dateIdx++) {
1100 for (timeIdx = 0; timeIdx < (int32_t)(sizeof(style)/sizeof(style[0])); timeIdx++) {
1101 UErrorCode status = U_ZERO_ERROR;
1102 udat_close(udat_open(style[dateIdx], style[timeIdx],
1103 uloc_getAvailable(idx), NULL, 0, NULL, 0, &status));
1104 if (U_FAILURE(status)) {
1105 log_err("FAIL: udat_open(%s) failed with (%s) dateIdx=%d, timeIdx=%d\n",
1106 uloc_getAvailable(idx), u_errorName(status), dateIdx, timeIdx);
1107 }
1108 }
1109 }
1110 }
1111}
1112
46f4442e
A
1113static void TestRelativeCrash(void) {
1114 static const UChar tzName[] = { 0x0055, 0x0053, 0x002F, 0x0050, 0x0061, 0x0063, 0x0069, 0x0066, 0x0069, 0x0063, 0 };
1115 static const UDate aDate = -631152000000.0;
1116
1117 UErrorCode status = U_ZERO_ERROR;
1118 UErrorCode expectStatus = U_ILLEGAL_ARGUMENT_ERROR;
1119 UDateFormat icudf;
1120
1121 icudf = udat_open(UDAT_NONE, UDAT_SHORT_RELATIVE, "en", tzName, -1, NULL, 0, &status);
1122 if ( U_SUCCESS(status) ) {
1123 const char *what = "???";
1124 {
1125 UErrorCode subStatus = U_ZERO_ERROR;
1126 what = "udat_set2DigitYearStart";
1127 log_verbose("Trying %s on a relative date..\n", what);
1128 udat_set2DigitYearStart(icudf, aDate, &subStatus);
1129 if(subStatus == expectStatus) {
1130 log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
1131 } else {
1132 log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
1133 }
1134 }
1135 {
1136 /* clone works polymorphically. try it anyways */
1137 UErrorCode subStatus = U_ZERO_ERROR;
1138 UDateFormat *oth;
1139 what = "clone";
1140 log_verbose("Trying %s on a relative date..\n", what);
1141 oth = udat_clone(icudf, &subStatus);
1142 if(subStatus == U_ZERO_ERROR) {
1143 log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
1144 udat_close(oth); /* ? */
1145 } else {
1146 log_err("FAIL: didn't crash on %s, but got %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
1147 }
1148 }
1149 {
1150 UErrorCode subStatus = U_ZERO_ERROR;
1151 what = "udat_get2DigitYearStart";
1152 log_verbose("Trying %s on a relative date..\n", what);
1153 udat_get2DigitYearStart(icudf, &subStatus);
1154 if(subStatus == expectStatus) {
1155 log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
1156 } else {
1157 log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
1158 }
1159 }
1160 {
1161 /* Now udat_toPattern works for relative date formatters, unless localized is TRUE */
1162 UErrorCode subStatus = U_ZERO_ERROR;
1163 what = "udat_toPattern";
1164 log_verbose("Trying %s on a relative date..\n", what);
1165 udat_toPattern(icudf, TRUE,NULL,0, &subStatus);
1166 if(subStatus == expectStatus) {
1167 log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
1168 } else {
1169 log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
1170 }
1171 }
1172 {
1173 UErrorCode subStatus = U_ZERO_ERROR;
1174 what = "udat_applyPattern";
1175 log_verbose("Trying %s on a relative date..\n", what);
1176 udat_applyPattern(icudf, FALSE,tzName,-1);
1177 subStatus = U_ILLEGAL_ARGUMENT_ERROR; /* what it should be, if this took an errorcode. */
1178 if(subStatus == expectStatus) {
1179 log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
1180 } else {
1181 log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
1182 }
1183 }
1184 {
1185 UErrorCode subStatus = U_ZERO_ERROR;
1186 what = "udat_getSymbols";
1187 log_verbose("Trying %s on a relative date..\n", what);
1188 udat_getSymbols(icudf, UDAT_ERAS,0,NULL,0, &subStatus); /* bogus values */
1189 if(subStatus == expectStatus) {
1190 log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
1191 } else {
1192 log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
1193 }
1194 }
1195 {
1196 UErrorCode subStatus = U_ZERO_ERROR;
1197 what = "udat_countSymbols";
1198 log_verbose("Trying %s on a relative date..\n", what);
1199 udat_countSymbols(icudf, UDAT_ERAS);
1200 subStatus = U_ILLEGAL_ARGUMENT_ERROR; /* should have an errorcode. */
1201 if(subStatus == expectStatus) {
1202 log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
1203 } else {
1204 log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
1205 }
1206 }
1207
1208 udat_close(icudf);
1209 } else {
1210 log_err("FAIL: err %s\n", u_errorName(status));
1211 }
1212}
1213
b75a7d8f 1214#endif /* #if !UCONFIG_NO_FORMATTING */