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