]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/dadrcal.cpp
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / test / intltest / dadrcal.cpp
CommitLineData
46f4442e
A
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2008, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7/***********************************************************************
8 * Modification history
9 * Date Name Description
10 * 07/09/2007 srl Copied from dadrcoll.cpp
11 ***********************************************************************/
12
13#include "unicode/utypes.h"
14
15#if !UCONFIG_NO_FORMATTING
16
17#include "unicode/tstdtmod.h"
18#include "tsdate.h"
19#include "dadrcal.h"
20#include "unicode/calendar.h"
21#include "intltest.h"
22#include <string.h>
23#include "unicode/schriter.h"
24#include "unicode/regex.h"
25#include "unicode/smpdtfmt.h"
26#include "unicode/dbgutil.h"
27
28#include <stdio.h>
29
30DataDrivenCalendarTest::DataDrivenCalendarTest() {
31 UErrorCode status = U_ZERO_ERROR;
32 driver = TestDataModule::getTestDataModule("calendar", *this, status);
33}
34
35DataDrivenCalendarTest::~DataDrivenCalendarTest() {
36 delete driver;
37}
38
39void DataDrivenCalendarTest::runIndexedTest(int32_t index, UBool exec,
40 const char* &name, char* /*par */) {
41 if (driver != NULL) {
42 if (exec) {
43 // logln("Begin ");
44 }
45 const DataMap *info= NULL;
46 UErrorCode status= U_ZERO_ERROR;
47 TestData *testData = driver->createTestData(index, status);
48 if (U_SUCCESS(status)) {
49 name = testData->getName();
50 if (testData->getInfo(info, status)) {
51 log(info->getString("Description", status));
52 }
53 if (exec) {
54 log(name);
55 logln("---");
56 logln("");
57
58 processTest(testData);
59 }
60 delete testData;
61 } else {
62 name = "";
63 }
64 } else {
65 dataerrln("[DATA] format/DataDriven*Test data (calendar.res) not initialized!");
66 name = "";
67 }
68
69}
70
71void DataDrivenCalendarTest::testOps(TestData *testData,
72 const DataMap * /*settings*/) {
73 UErrorCode status = U_ZERO_ERROR;
74 const DataMap *currentCase= NULL;
75 char toCalLoc[256] = "";
76
77 // TODO: static strings?
78 const UnicodeString kADD("add", "");
79 const UnicodeString kROLL("roll", "");
80
81 // Get 'from' time
82 CalendarFieldsSet fromSet, toSet, paramsSet, diffSet;
83 SimpleDateFormat fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"),
84 status);
85 if (U_FAILURE(status)) {
86 errln("FAIL: Couldn't create SimpleDateFormat: %s\n",
87 u_errorName(status));
88 return;
89 }
90 // Start the processing
91 int n = 0;
92 while (testData->nextCase(currentCase, status)) {
93 ++n;
94 Calendar *toCalendar= NULL;
95 Calendar *fromCalendar= NULL;
96
97 // load parameters
98 char theCase[200];
99 sprintf(theCase, "[case %d]", n);
100 UnicodeString caseString(theCase, "");
101 // build to calendar
102 // Headers { "locale","from","operation","params","to" }
103 // #1 locale
104 const char *param = "locale";
105 UnicodeString locale;
106 UnicodeString testSetting = currentCase->getString(param, status);
107 if (U_FAILURE(status)) {
108 errln(caseString+": Unable to get param '"+param+"' "
109 + UnicodeString(" - "));
110 continue;
111 }
112 testSetting.extract(0, testSetting.length(), toCalLoc, (const char*)0);
113 fromCalendar = Calendar::createInstance(toCalLoc, status);
114 if (U_FAILURE(status)) {
115 errln(caseString+": Unable to instantiate calendar for "
116 +testSetting);
117 continue;
118 }
119
120 fromSet.clear();
121 // #2 'from' info
122 param = "from";
123 UnicodeString from = testSetting=currentCase->getString(param, status);
124 if (U_FAILURE(status)) {
125 errln(caseString+": Unable to get parameter '"+param+"' "
126 + UnicodeString(" - "));
127 continue;
128 }
129 fromSet.parseFrom(testSetting, status);
130 if (U_FAILURE(status)) {
131 errln(caseString+": Failed to parse '"+param+"' parameter: "
132 +testSetting);
133 continue;
134 }
135
136 // #4 'operation' info
137 param = "operation";
138 UnicodeString operation = testSetting=currentCase->getString(param,
139 status);
140 if (U_FAILURE(status)) {
141 errln(caseString+": Unable to get parameter '"+param+"' "
142 + UnicodeString(" - "));
143 continue;
144 }
145 if (U_FAILURE(status)) {
146 errln(caseString+": Failed to parse '"+param+"' parameter: "
147 +testSetting);
148 continue;
149 }
150
151 paramsSet.clear();
152 // #3 'params' info
153 param = "params";
154 UnicodeString params = testSetting
155 =currentCase->getString(param, status);
156 if (U_FAILURE(status)) {
157 errln(caseString+": Unable to get parameter '"+param+"' "
158 + UnicodeString(" - "));
159 continue;
160 }
161 paramsSet.parseFrom(testSetting, status); // parse with inheritance.
162 if (U_FAILURE(status)) {
163 errln(caseString+": Failed to parse '"+param+"' parameter: "
164 +testSetting);
165 continue;
166 }
167
168 toSet.clear();
169 // #4 'to' info
170 param = "to";
171 UnicodeString to = testSetting=currentCase->getString(param, status);
172 if (U_FAILURE(status)) {
173 errln(caseString+": Unable to get parameter '"+param+"' "
174 + UnicodeString(" - "));
175 continue;
176 }
177 toSet.parseFrom(testSetting, &fromSet, status); // parse with inheritance.
178 if (U_FAILURE(status)) {
179 errln(caseString+": Failed to parse '"+param+"' parameter: "
180 +testSetting);
181 continue;
182 }
183
184 UnicodeString caseContentsString = locale+": from "+from+": "
185 +operation +" [[[ "+params+" ]]] >>> "+to;
186 logln(caseString+": "+caseContentsString);
187
188 // ------
189 // now, do it.
190
191 /// prepare calendar
192 fromSet.setOnCalendar(fromCalendar, status);
193 if (U_FAILURE(status)) {
194 errln(caseString+" FAIL: Failed to set on Source calendar: "
195 + u_errorName(status));
196 return;
197 }
198
199
200 diffSet.clear();
201 // Is the calendar sane after being set?
202 if (!fromSet.matches(fromCalendar, diffSet, status)) {
203 UnicodeString diffs = diffSet.diffFrom(fromSet, status);
204 errln((UnicodeString)"FAIL: "+caseString
205 +", SET SOURCE calendar was not set: Differences: "+ diffs
206 +"', status: "+ u_errorName(status));
207 } else if (U_FAILURE(status)) {
208 errln("FAIL: "+caseString+" SET SOURCE calendar Failed to match: "
209 +u_errorName(status));
210 } else {
211 logln("PASS: "+caseString+" SET SOURCE calendar match.");
212 }
213
214 // to calendar - copy of from calendar
215 toCalendar = fromCalendar->clone();
216
217 /// perform op
218 for (int q=0; q<UCAL_FIELD_COUNT; q++) {
219 if (paramsSet.isSet((UCalendarDateFields)q)) {
220 if (operation == kROLL) {
221 toCalendar->roll((UCalendarDateFields)q,
222 paramsSet.get((UCalendarDateFields)q), status);
223 } else if (operation == kADD) {
224 toCalendar->add((UCalendarDateFields)q,
225 paramsSet.get((UCalendarDateFields)q), status);
226 } else {
227 errln(caseString+ " FAIL: unknown operation "+ operation);
228 }
229 logln(operation + " of "+ paramsSet.get((UCalendarDateFields)q)
230 +" -> "+u_errorName(status));
231 }
232 }
233 if (U_FAILURE(status)) {
234 errln(caseString+" FAIL: after "+operation+" of "+params+" -> "
235 +u_errorName(status));
236 continue;
237 }
238
239 // now - what's the result?
240 diffSet.clear();
241
242 if (!toSet.matches(toCalendar, diffSet, status)) {
243 UnicodeString diffs = diffSet.diffFrom(toSet, status);
244 errln((UnicodeString)"FAIL: "+caseString+" - , "+caseContentsString
245 +" Differences: "+ diffs +"', status: "
246 + u_errorName(status));
247 } else if (U_FAILURE(status)) {
248 errln("FAIL: "+caseString+" Match operation had an error: "
249 +u_errorName(status));
250 } else {
251 logln("PASS: "+caseString+" matched!");
252 }
253
254 delete fromCalendar;
255 delete toCalendar;
256 }
257}
258
259void DataDrivenCalendarTest::testConvert(int32_t n,
260 const CalendarFieldsSet &fromSet, Calendar *fromCalendar,
261 const CalendarFieldsSet &toSet, Calendar *toCalendar, UBool forward) {
262 UErrorCode status = U_ZERO_ERROR;
263 UnicodeString thisString = (UnicodeString)"#"+n+" "+(forward ? "forward"
264 : "reverse")+" "+fromCalendar->getType()+"->"+toCalendar->getType();
265
266 fromCalendar->clear();
267
268 fromSet.setOnCalendar(fromCalendar, status);
269 if (U_FAILURE(status)) {
270 errln("FAIL: Failed to set on Source calendar: %s", u_errorName(status));
271 return;
272 }
273
274 CalendarFieldsSet diffSet;
275
276 diffSet.clear();
277 // Is the calendar sane at the first?
278 if (!fromSet.matches(fromCalendar, diffSet, status)) {
279 UnicodeString diffs = diffSet.diffFrom(fromSet, status);
280 errln((UnicodeString)"FAIL: "+thisString
281 +", SOURCE calendar was not set: Differences: "+ diffs
282 +"', status: "+ u_errorName(status));
283 } else if (U_FAILURE(status)) {
284 errln("FAIL: "+thisString+" SOURCE calendar Failed to match: "
285 +u_errorName(status));
286 } else {
287 logln("PASS: "+thisString+" SOURCE calendar match.");
288 }
289
290 //logln("Set Source calendar: " + from);
291
292 UDate fromTime = fromCalendar->getTime(status);
293 if (U_FAILURE(status)) {
294 errln("FAIL: Failed to get Source time: %s", u_errorName(status));
295 return;
296 }
297
298 diffSet.clear();
299 // Is the calendar sane after being set?
300 if (!fromSet.matches(fromCalendar, diffSet, status)) {
301 UnicodeString diffs = diffSet.diffFrom(fromSet, status);
302 errln((UnicodeString)"FAIL: "+thisString
303 +", SET SOURCE calendar was not set: Differences: "+ diffs
304 +"', status: "+ u_errorName(status));
305 } else if (U_FAILURE(status)) {
306 errln("FAIL: "+thisString+" SET SOURCE calendar Failed to match: "
307 +u_errorName(status));
308 } else {
309 logln("PASS: "+thisString+" SET SOURCE calendar match.");
310 }
311
312 toCalendar->clear();
313 toCalendar->setTime(fromTime, status);
314 if (U_FAILURE(status)) {
315 errln("FAIL: Failed to set Target time: %s", u_errorName(status));
316 return;
317 }
318
319 diffSet.clear();
320 if (!toSet.matches(toCalendar, diffSet, status)) {
321 UnicodeString diffs = diffSet.diffFrom(toSet, status);
322 errln((UnicodeString)"FAIL: "+thisString+", Differences: "+ diffs
323 +"', status: "+ u_errorName(status));
324 SimpleDateFormat fmt(UnicodeString("EEE MMM dd yyyy G"), status);
325 UnicodeString fromString;
326 fmt.format(fromTime, fromString);
327 logln("Source Time: "+fromString+", Source Calendar: "
328 +fromCalendar->getType());
329 } else if (U_FAILURE(status)) {
330 errln("FAIL: "+thisString+" Failed to match: "+u_errorName(status));
331 } else {
332 logln("PASS: "+thisString+" match.");
333 }
334}
335
336void DataDrivenCalendarTest::testConvert(TestData *testData,
337 const DataMap *settings, UBool forward) {
338 UErrorCode status = U_ZERO_ERROR;
339 Calendar *toCalendar= NULL;
340 const DataMap *currentCase= NULL;
341 char toCalLoc[256] = "";
342 char fromCalLoc[256] = "";
343 // build to calendar
344 UnicodeString testSetting = settings->getString("ToCalendar", status);
345 if (U_SUCCESS(status)) {
346 testSetting.extract(0, testSetting.length(), toCalLoc, (const char*)0);
347 toCalendar = Calendar::createInstance(toCalLoc, status);
348 if (U_FAILURE(status)) {
349 errln("Unable to instantiate ToCalendar for "+testSetting);
350 return;
351 }
352 }
353
354 CalendarFieldsSet fromSet, toSet, diffSet;
355 SimpleDateFormat fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"),
356 status);
357 if (U_FAILURE(status)) {
358 errln("FAIL: Couldn't create SimpleDateFormat: %s\n",
359 u_errorName(status));
360 return;
361 }
362 // Start the processing
363 int n = 0;
364 while (testData->nextCase(currentCase, status)) {
365 ++n;
366 Calendar *fromCalendar= NULL;
367 UnicodeString locale = currentCase->getString("locale", status);
368 if (U_SUCCESS(status)) {
369 locale.extract(0, locale.length(), fromCalLoc, (const char*)0); // default codepage. Invariant codepage doesn't have '@'!
370 fromCalendar = Calendar::createInstance(fromCalLoc, status);
371 if (U_FAILURE(status)) {
372 errln("Unable to instantiate fromCalendar for "+locale);
373 return;
374 }
375 } else {
376 errln("No 'locale' line.");
377 continue;
378 }
379
380 fromSet.clear();
381 toSet.clear();
382
383 UnicodeString from = currentCase->getString("from", status);
384 if (U_FAILURE(status)) {
385 errln("No 'from' line.");
386 continue;
387 }
388 fromSet.parseFrom(from, status);
389 if (U_FAILURE(status)) {
390 errln("Failed to parse 'from' parameter: "+from);
391 continue;
392 }
393 UnicodeString to = currentCase->getString("to", status);
394 if (U_FAILURE(status)) {
395 errln("No 'to' line.");
396 continue;
397 }
398 toSet.parseFrom(to, &fromSet, status);
399 if (U_FAILURE(status)) {
400 errln("Failed to parse 'to' parameter: "+to);
401 continue;
402 }
403
404 // now, do it.
405 if (forward) {
406 logln((UnicodeString)"#"+n+" "+locale+"/"+from+" >>> "+toCalLoc+"/"
407 +to);
408 testConvert(n, fromSet, fromCalendar, toSet, toCalendar, forward);
409 } else {
410 logln((UnicodeString)"#"+n+" "+locale+"/"+from+" <<< "+toCalLoc+"/"
411 +to);
412 testConvert(n, toSet, toCalendar, fromSet, fromCalendar, forward);
413 }
414
415 delete fromCalendar;
416 }
417 delete toCalendar;
418}
419
420void DataDrivenCalendarTest::processTest(TestData *testData) {
421 //Calendar *cal= NULL;
422 //const UChar *arguments= NULL;
423 //int32_t argLen = 0;
424 char testType[256];
425 const DataMap *settings= NULL;
426 //const UChar *type= NULL;
427 UErrorCode status = U_ZERO_ERROR;
428 UnicodeString testSetting;
429 int n = 0;
430 while (testData->nextSettings(settings, status)) {
431 status = U_ZERO_ERROR;
432 // try to get a locale
433 testSetting = settings->getString("Type", status);
434 if (U_SUCCESS(status)) {
435 if ((++n)>0) {
436 logln("---");
437 }
438 logln(testSetting + "---");
439 testSetting.extract(0, testSetting.length(), testType, "");
440 } else {
441 errln("Unable to extract 'Type'. Skipping..");
442 continue;
443 }
444
445 if (!strcmp(testType, "convert_fwd")) {
446 testConvert(testData, settings, true);
447 } else if (!strcmp(testType, "convert_rev")) {
448 testConvert(testData, settings, false);
449 } else if (!strcmp(testType, "ops")) {
450 testOps(testData, settings);
451 } else {
452 errln("Unknown type: %s", testType);
453 }
454 }
455}
456
457#endif