]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cdtdptst.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cdtdptst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2001, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File CDTDPTST.C
9 *
10 * Modification History:
11 * Name Description
12 * Madhu Katragadda Creation
13 *********************************************************************************
14 */
15 /* INDEPTH TEST FOR DATE FORMAT */
16
17 #include "unicode/utypes.h"
18
19 #if !UCONFIG_NO_FORMATTING
20
21 #include "unicode/uloc.h"
22 #include "unicode/udat.h"
23 #include "unicode/ucal.h"
24 #include "unicode/unum.h"
25 #include "unicode/ustring.h"
26 #include "cintltst.h"
27 #include "cdtdptst.h"
28 #include "cformtst.h"
29
30 #include "cmemory.h"
31
32 void addDtFrDepTest(TestNode** root);
33
34 void addDtFrDepTest(TestNode** root)
35 {
36 addTest(root, &TestTwoDigitYearDSTParse, "tsformat/cdtdptst/TestTwoDigitYearDSTParse");
37 addTest(root, &TestPartialParse994, "tsformat/cdtdptst/TestPartialParse994");
38 addTest(root, &TestRunTogetherPattern985, "tsformat/cdtdptst/TestRunTogetherPattern985");
39 addTest(root, &TestCzechMonths459, "tsformat/cdtdptst/TestCzechMonths459");
40 addTest(root, &TestQuotePattern161, "tsformat/cdtdptst/TestQuotePattern161");
41
42 }
43
44 /**
45 * Test the parsing of 2-digit years.
46 */
47 void TestTwoDigitYearDSTParse()
48 {
49 UDateFormat *fullFmt, *fmt;
50 UErrorCode status = U_ZERO_ERROR;
51 UChar *pattern;
52 UDate d;
53 UChar *s;
54 int32_t pos;
55
56 pattern=(UChar*)malloc(sizeof(UChar) * (strlen("EEE MMM dd HH:mm:ss.SSS zzz yyyy G")+1 ));
57 u_uastrcpy(pattern, "EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
58 fullFmt= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US",NULL,0,pattern, u_strlen(pattern),&status);
59 if(U_FAILURE(status)) {
60 log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n",
61 myErrorName(status) );
62 }
63 else {log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");}
64
65 u_uastrcpy(pattern, "dd-MMM-yy h:mm:ss 'o''clock' a z");
66 fmt= udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status);
67
68
69 s=(UChar*)malloc(sizeof(UChar) * (strlen("03-Apr-04 2:20:47 o'clock AM PST")+1) );
70 u_uastrcpy(s, "03-Apr-04 2:20:47 o'clock AM PST");
71 pos=0;
72 d = udat_parse(fmt, s, u_strlen(s), &pos, &status);
73 if (U_FAILURE(status)) {
74 log_err("FAIL: Could not parse \"%s\"\n", austrdup(s));
75 } else {
76 UCalendar *cal = ucal_open(NULL, 0, uloc_getDefault(), UCAL_TRADITIONAL, &status);
77 if (U_FAILURE(status)) {
78 log_err("FAIL: Could not open calendar");
79 } else {
80 int32_t h;
81 ucal_setMillis(cal, d, &status);
82 h = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
83 if (U_FAILURE(status)) {
84 log_err("FAIL: Some calendar operations failed");
85 } else if (h != 2) {
86 log_err("FAIL: Parse of \"%s\" returned HOUR_OF_DAY %d\n",
87 austrdup(s), h);
88 }
89 ucal_close(cal);
90 }
91 }
92
93 udat_close(fullFmt);
94 udat_close(fmt);
95 free(pattern);
96 free(s);
97 }
98
99
100 /**
101 * Verify that strings which contain incomplete specifications are parsed
102 * correctly. In some instances, this means not being parsed at all, and
103 * returning an appropriate error.
104 */
105 void TestPartialParse994()
106 {
107 int32_t pos;
108 UDateFormat *f;
109 UErrorCode status = U_ZERO_ERROR;
110 UChar *s;
111 UChar *fmtChars;
112 UDate d, null;
113 null=0;
114
115 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
116 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
117 /* f = udat_open(UDAT_DEFAULT, UDAT_SHORT, NULL, NULL, 0, &status); */
118 f = udat_open(UDAT_DEFAULT, UDAT_SHORT, "en_US", NULL, 0, NULL, 0,&status);
119 if(U_FAILURE(status)){
120 log_err("FAIL: ErrorCode received during test: %s\n", myErrorName(status));
121 return;
122 }
123 s=(UChar*)malloc(sizeof(UChar) * (strlen("01/01/1997 10:11:42 AM")+1) );
124 u_uastrcpy(s, "01/01/1997 10:11:42 AM");
125 pos=0;
126 d = udat_parse(f, s, u_strlen(s), &pos, &status);
127 if(U_FAILURE(status)) {
128 log_data_err("FAIL: could not parse - exitting");
129 return;
130 }
131 fmtChars = myDateFormat(f, d);
132 if(fmtChars) {
133 log_verbose("%s\n", fmtChars);
134 } else {
135 log_data_err("FAIL: could not format \n");
136 return;
137 }
138 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:11:42", d);
139 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:", null);
140 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10", null);
141 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 ", null);
142 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01", null);
143 udat_close(f);
144 free(s);
145 }
146
147
148
149 void tryPat994(UDateFormat* format, const char* pattern, const char* s, UDate expected)
150 {
151 UChar *f;
152 UChar *str, *pat;
153 UDate date;
154 UDate null=0;
155 int32_t pos;
156 UErrorCode status = U_ZERO_ERROR;
157 str=(UChar*)malloc(sizeof(UChar) * (strlen(s) + 1) );
158 u_uastrcpy(str, s);
159 pat=(UChar*)malloc(sizeof(UChar) * (strlen(pattern) + 1) );
160 u_uastrcpy(pat, pattern);
161 log_verbose("Pattern : %s ; String : %s\n", austrdup(pat), austrdup(str));
162 udat_applyPattern(format, FALSE, pat, u_strlen(pat));
163 pos=0;
164 date = udat_parse(format, str, u_strlen(str), &pos, &status);
165 if(U_FAILURE(status) || date == null) {
166 log_verbose("ParseException: : %s\n", myErrorName(status) );
167 if (expected != null)
168 log_err("FAIL: Expected: %s\n", austrdup(myDateFormat(format, expected)) );
169 }
170 else {
171 f=myDateFormat(format, date);
172 log_verbose(" parse( %s ) -> %s\n", austrdup(str), austrdup(f));
173 if (expected == null || date != expected)
174 log_err("FAIL: Expected null for \"%s\"\n", s);
175 if (u_strcmp(f, str) !=0)
176 log_err("FAIL: Expected : %s\n", austrdup(str) );
177 }
178
179 free(str);
180 free(pat);
181 }
182
183
184 /**
185 * Verify the behavior of patterns in which digits for different fields run together
186 * without intervening separators.
187 */
188 void TestRunTogetherPattern985()
189 {
190 int32_t pos;
191 UChar *pattern=NULL, *now=NULL, *then=NULL;
192 UDateFormat *format;
193 UDate date1, date2;
194 UErrorCode status = U_ZERO_ERROR;
195 pattern=(UChar*)malloc(sizeof(UChar) * (strlen("yyyyMMddHHmmssSSS")+1) );
196 u_uastrcpy(pattern, "yyyyMMddHHmmssSSS");
197 format = udat_open(UDAT_IGNORE, UDAT_IGNORE, NULL, NULL, 0,pattern, u_strlen(pattern), &status);
198 if(U_FAILURE(status)){
199 log_err("FAIL: Error in date format construction with pattern: %s\n", myErrorName(status));
200 return;
201 }
202 date1 = ucal_getNow();
203 now=myDateFormat(format, date1);
204 log_verbose("%s\n", austrdup(now) );
205 pos = 0;
206 date2 = udat_parse(format, now, u_strlen(now), &pos, &status);
207 if (date2 == 0) log_verbose("Parse stopped at : %d\n", pos);
208 else then=myDateFormat(format, date2);
209 log_verbose("%s\n", austrdup(then) );
210 if (!(date2 == date1)) log_err("FAIL\n");
211
212 udat_close(format);
213 free(pattern);
214
215 }
216
217 /**
218 * Verify the handling of Czech June and July, which have the unique attribute that
219 * one is a proper prefix substring of the other.
220 */
221 void TestCzechMonths459()
222 {
223 int32_t lneed, pos;
224 UChar *pattern=NULL, *tzID=NULL;
225 UChar *juneStr, *julyStr;
226 UDateFormat *fmt;
227 UCalendar *cal;
228 UDate june, july, d;
229 UErrorCode status = U_ZERO_ERROR;
230 char buffer[512];
231 UChar *date;
232
233 fmt = udat_open(UDAT_FULL, UDAT_FULL, "cs", NULL, 0, NULL, 0, &status);
234 if(U_FAILURE(status)){
235 log_err("Error in constructing the date format\n");
236 return;
237 }
238 lneed=0;
239 lneed=udat_toPattern(fmt, TRUE, NULL, lneed, &status);
240 if(status==U_BUFFER_OVERFLOW_ERROR){
241 status=U_ZERO_ERROR;
242 pattern=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
243 udat_toPattern(fmt, TRUE, pattern, lneed+1, &status);
244 }
245 if(U_FAILURE(status)){ log_err("Error in extracting the pattern\n"); }
246 tzID=(UChar*)malloc(sizeof(UChar) * 4);
247 u_uastrcpy(tzID, "GMT");
248 cal=ucal_open(tzID, u_strlen(tzID), "cs", UCAL_GREGORIAN, &status);
249 if(U_FAILURE(status)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status)); }
250
251 ucal_setDate(cal, 1997, UCAL_JUNE, 15, &status);
252 june=ucal_getMillis(cal, &status);
253 ucal_setDate(cal, 1997, UCAL_JULY, 15, &status);
254 july=ucal_getMillis(cal, &status);
255
256
257 juneStr = myDateFormat(fmt, june);
258 julyStr = myDateFormat(fmt, july);
259 pos=0;
260 if(juneStr == NULL) {
261 log_data_err("Can't load juneStr. Quitting.\n");
262 return;
263 }
264 d = udat_parse(fmt, juneStr, u_strlen(juneStr), &pos, &status);
265 date = myDateFormat(fmt, d);
266 u_UCharsToChars(date, buffer, (int32_t)(u_strlen(date)+1));
267 if(u_strcmp(myDateFormat(fmt, june), myDateFormat(fmt, d) ) !=0)
268 log_err("Error in handling the czech month june\n");
269 else
270 log_verbose("Pass: Date = %s\n", buffer );
271 pos=0;
272 d = udat_parse(fmt, julyStr, u_strlen(julyStr), &pos, &status);
273 date = myDateFormat(fmt, d);
274 u_UCharsToChars(date, buffer, (int32_t)(u_strlen(date)+1));
275 if(u_strcmp(myDateFormat(fmt, july), myDateFormat(fmt, d) ) !=0)
276 log_err("Error in handling the czech month july\n");
277 else
278 log_verbose("Pass: Date = %s\n", buffer );
279
280 udat_close(fmt);
281 ucal_close(cal);
282 free(pattern);
283 free(tzID);
284 }
285
286 /**
287 * Test the handling of single quotes in patterns.
288 */
289 void TestQuotePattern161()
290 {
291 UDateFormat *format;
292 UCalendar *cal;
293 UDate currentTime_1;
294 UChar *pattern, *tzID, *exp;
295 UChar *dateString;
296 UErrorCode status = U_ZERO_ERROR;
297 const char* expStr = "04/13/1999 at 10:42:28 AM ";
298 pattern=(UChar*)malloc(sizeof(UChar) * (strlen("MM/dd/yyyy 'at' hh:mm:ss a zzz")+1) );
299 u_uastrcpy(pattern, "MM/dd/yyyy 'at' hh:mm:ss a zzz");
300
301 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
302 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
303 /* format= udat_openPattern(pattern, u_strlen(pattern), NULL, &status); */
304 format= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status);
305 if(U_FAILURE(status)){
306 log_err("error in udat_open: %s\n", myErrorName(status));
307 return;
308 }
309 tzID=(UChar*)malloc(sizeof(UChar) * 4);
310 u_uastrcpy(tzID, "PST");
311 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
312 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
313 /* cal=ucal_open(tzID, u_strlen(tzID), NULL, UCAL_TRADITIONAL, &status); */
314 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
315 if(U_FAILURE(status)){ log_err("error in ucal_open cal : %s\n", myErrorName(status)); }
316
317 ucal_setDateTime(cal, 1999, UCAL_APRIL, 13, 10, 42, 28, &status);
318 currentTime_1 = ucal_getMillis(cal, &status);
319
320 dateString = myDateFormat(format, currentTime_1);
321 exp=(UChar*)malloc(sizeof(UChar) * (strlen(expStr) + 1) );
322 u_uastrcpy(exp, expStr);
323
324 log_verbose("%s\n", austrdup(dateString) );
325 if(u_strncmp(dateString, exp, strlen(expStr)) !=0)
326 log_err("Error in formatting a pattern with single quotes\n");
327
328 udat_close(format);
329 ucal_close(cal);
330 free(exp);
331 free(tzID);
332 free(pattern);
333
334 }
335
336 #endif /* #if !UCONFIG_NO_FORMATTING */