1 /********************************************************************
3 * Copyright (c) 2002-2010, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
10 // Decimal Formatter tests, data driven.
15 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_REGULAR_EXPRESSIONS
17 #include "unicode/regex.h"
18 #include "unicode/uchar.h"
19 #include "unicode/ustring.h"
20 #include "unicode/unistr.h"
21 #include "unicode/dcfmtsym.h"
22 #include "unicode/decimfmt.h"
23 #include "unicode/locid.h"
35 //---------------------------------------------------------------------------
37 // Test class boilerplate
39 //---------------------------------------------------------------------------
40 DecimalFormatTest::DecimalFormatTest()
45 DecimalFormatTest::~DecimalFormatTest()
51 void DecimalFormatTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
53 if (exec
) logln("TestSuite DecimalFormatTest: ");
56 #if !UCONFIG_NO_FILE_IO
57 case 0: name
= "DataDrivenTests";
58 if (exec
) DataDrivenTests();
61 case 0: name
= "skip";
66 break; //needed to end loop
71 //---------------------------------------------------------------------------
73 // Error Checking / Reporting macros used in all of the tests.
75 //---------------------------------------------------------------------------
76 #define DF_CHECK_STATUS {if (U_FAILURE(status)) \
77 {dataerrln("DecimalFormatTest failure at line %d. status=%s", \
78 __LINE__, u_errorName(status)); return 0;}}
80 #define DF_ASSERT(expr) {if ((expr)==FALSE) {errln("DecimalFormatTest failure at line %d.\n", __LINE__);};}
82 #define DF_ASSERT_FAIL(expr, errcode) {UErrorCode status=U_ZERO_ERROR; (expr);\
83 if (status!=errcode) {dataerrln("DecimalFormatTest failure at line %d. Expected status=%s, got %s", \
84 __LINE__, u_errorName(errcode), u_errorName(status));};}
86 #define DF_CHECK_STATUS_L(line) {if (U_FAILURE(status)) {errln( \
87 "DecimalFormatTest failure at line %d, from %d. status=%d\n",__LINE__, (line), status); }}
89 #define DF_ASSERT_L(expr, line) {if ((expr)==FALSE) { \
90 errln("DecimalFormatTest failure at line %d, from %d.", __LINE__, (line)); return;}}
95 // InvariantStringPiece
96 // Wrap a StringPiece around the extracted invariant data of a UnicodeString.
97 // The data is guaranteed to be nul terminated. (This is not true of StringPiece
98 // in general, but is true of InvariantStringPiece)
100 class InvariantStringPiece
: public StringPiece
{
102 InvariantStringPiece(const UnicodeString
&s
);
103 ~InvariantStringPiece() {};
105 MaybeStackArray
<char, 20> buf
;
108 InvariantStringPiece::InvariantStringPiece(const UnicodeString
&s
) {
109 int32_t len
= s
.length();
110 if (len
+1 > buf
.getCapacity()) {
113 // Buffer size is len+1 so that s.extract() will nul-terminate the string.
114 s
.extract(0, len
, buf
.getAlias(), len
+1, US_INV
);
119 // UnicodeStringPiece
120 // Wrap a StringPiece around the extracted (to the default charset) data of
121 // a UnicodeString. The extracted data is guaranteed to be nul terminated.
122 // (This is not true of StringPiece in general, but is true of UnicodeStringPiece)
124 class UnicodeStringPiece
: public StringPiece
{
126 UnicodeStringPiece(const UnicodeString
&s
);
127 ~UnicodeStringPiece() {};
129 MaybeStackArray
<char, 20> buf
;
132 UnicodeStringPiece::UnicodeStringPiece(const UnicodeString
&s
) {
133 int32_t len
= s
.length();
134 int32_t capacity
= buf
.getCapacity();
135 int32_t requiredCapacity
= s
.extract(0, len
, buf
.getAlias(), capacity
) + 1;
136 if (capacity
< requiredCapacity
) {
137 buf
.resize(requiredCapacity
);
138 capacity
= requiredCapacity
;
139 s
.extract(0, len
, buf
.getAlias(), capacity
);
141 this->set(buf
, requiredCapacity
- 1);
146 //---------------------------------------------------------------------------
149 // The test cases are in a separate data file,
151 //---------------------------------------------------------------------------
153 // Translate a Formattable::type enum value to a string, for error message formatting.
154 static const char *formattableType(Formattable::Type typ
) {
155 static const char *types
[] = {"kDate",
163 if (typ
<0 || typ
>Formattable::kObject
) {
170 DecimalFormatTest::getPath(char *buffer
, const char *filename
) {
171 UErrorCode status
=U_ZERO_ERROR
;
172 const char *testDataDirectory
= IntlTest::getSourceTestData(status
);
175 strcpy(buffer
, testDataDirectory
);
176 strcat(buffer
, filename
);
180 void DecimalFormatTest::DataDrivenTests() {
183 UErrorCode status
= U_ZERO_ERROR
;
187 // Open and read the test data file.
189 srcPath
=getPath(tdd
, "dcfmtest.txt");
191 return; /* something went wrong, error already output */
195 UChar
*testData
= ReadAndConvertFile(srcPath
, len
, status
);
196 if (U_FAILURE(status
)) {
197 return; /* something went wrong, error already output */
201 // Put the test data into a UnicodeString
203 UnicodeString
testString(FALSE
, testData
, len
);
205 RegexMatcher
parseLineMat(UnicodeString(
207 "\"([^\"]*)\"\\s+" // Capture group 1: input text
208 "([ild])\\s+" // Capture group 2: expected parsed type
209 "\"([^\"]*)\"\\s+" // Capture group 3: expected parsed decimal
210 "\\s*(?:#.*)?"), // Trailing comment
213 RegexMatcher
formatLineMat(UnicodeString(
215 "(\\S+)\\s+" // Capture group 1: pattern
216 "(ceiling|floor|down|up|halfeven|halfdown|halfup|default)\\s+" // Capture group 2: Rounding Mode
217 "\"([^\"]*)\"\\s+" // Capture group 3: input
218 "\"([^\"]*)\"" // Capture group 4: expected output
219 "\\s*(?:#.*)?"), // Trailing comment
222 RegexMatcher
commentMat (UNICODE_STRING_SIMPLE("\\s*(#.*)?$"), 0, status
);
223 RegexMatcher
lineMat(UNICODE_STRING_SIMPLE("(?m)^(.*?)$"), testString
, 0, status
);
225 if (U_FAILURE(status
)){
226 dataerrln("Construct RegexMatcher() error.");
232 // Loop over the test data file, once per line.
234 while (lineMat
.find()) {
236 if (U_FAILURE(status
)) {
237 errln("File dcfmtest.txt, line %d: ICU Error \"%s\"", lineNum
, u_errorName(status
));
240 status
= U_ZERO_ERROR
;
241 UnicodeString testLine
= lineMat
.group(1, status
);
242 // printf("%s\n", UnicodeStringPiece(testLine).data());
243 if (testLine
.length() == 0) {
248 // Parse the test line. Skip blank and comment only lines.
249 // Separate out the three main fields - pattern, flags, target.
252 commentMat
.reset(testLine
);
253 if (commentMat
.lookingAt(status
)) {
254 // This line is a comment, or blank.
260 // Handle "parse" test case line from file
262 parseLineMat
.reset(testLine
);
263 if (parseLineMat
.lookingAt(status
)) {
264 execParseTest(lineNum
,
265 parseLineMat
.group(1, status
), // input
266 parseLineMat
.group(2, status
), // Expected Type
267 parseLineMat
.group(3, status
), // Expected Decimal String
274 // Handle "format" test case line
276 formatLineMat
.reset(testLine
);
277 if (formatLineMat
.lookingAt(status
)) {
278 execFormatTest(lineNum
,
279 formatLineMat
.group(1, status
), // Pattern
280 formatLineMat
.group(2, status
), // rounding mode
281 formatLineMat
.group(3, status
), // input decimal number
282 formatLineMat
.group(4, status
), // expected formatted result
288 // Line is not a recognizable test case.
290 errln("Badly formed test case at line %d.\n%s\n",
291 lineNum
, UnicodeStringPiece(testLine
).data());
300 void DecimalFormatTest::execParseTest(int32_t lineNum
,
301 const UnicodeString
&inputText
,
302 const UnicodeString
&expectedType
,
303 const UnicodeString
&expectedDecimal
,
304 UErrorCode
&status
) {
306 if (U_FAILURE(status
)) {
310 DecimalFormatSymbols
symbols(Locale::getUS(), status
);
311 UnicodeString pattern
= UNICODE_STRING_SIMPLE("####");
312 DecimalFormat
format(pattern
, symbols
, status
);
314 if (U_FAILURE(status
)) {
315 errln("file dcfmtest.txt, line %d: %s error creating the formatter.",
316 lineNum
, u_errorName(status
));
321 int32_t expectedParseEndPosition
= inputText
.length();
323 format
.parse(inputText
, result
, pos
);
325 if (expectedParseEndPosition
!= pos
.getIndex()) {
326 errln("file dcfmtest.txt, line %d: Expected parse position afeter parsing: %d. "
327 "Actual parse position: %d", expectedParseEndPosition
, pos
.getIndex());
331 char expectedTypeC
[2];
332 expectedType
.extract(0, 1, expectedTypeC
, 2, US_INV
);
333 Formattable::Type expectType
= Formattable::kDate
;
334 switch (expectedTypeC
[0]) {
335 case 'd': expectType
= Formattable::kDouble
; break;
336 case 'i': expectType
= Formattable::kLong
; break;
337 case 'l': expectType
= Formattable::kInt64
; break;
339 errln("file dcfmtest.tx, line %d: unrecongized expected type \"%s\"",
340 lineNum
, InvariantStringPiece(expectedType
).data());
343 if (result
.getType() != expectType
) {
344 errln("file dcfmtest.txt, line %d: expectedParseType(%s) != actual parseType(%s)",
345 lineNum
, formattableType(expectType
), formattableType(result
.getType()));
349 StringPiece decimalResult
= result
.getDecimalNumber(status
);
350 if (U_FAILURE(status
)) {
351 errln("File %s, line %d: error %s. Line in file dcfmtest.txt: %d:",
352 __FILE__
, __LINE__
, u_errorName(status
), lineNum
);
356 InvariantStringPiece
expectedResults(expectedDecimal
);
357 if (decimalResult
!= expectedResults
) {
358 errln("file dcfmtest.txt, line %d: expected \"%s\", got \"%s\"",
359 lineNum
, expectedResults
.data(), decimalResult
.data());
366 void DecimalFormatTest::execFormatTest(int32_t lineNum
,
367 const UnicodeString
&pattern
, // Pattern
368 const UnicodeString
&round
, // rounding mode
369 const UnicodeString
&input
, // input decimal number
370 const UnicodeString
&expected
, // expected formatted result
371 UErrorCode
&status
) {
372 if (U_FAILURE(status
)) {
376 DecimalFormatSymbols
symbols(Locale::getUS(), status
);
377 // printf("Pattern = %s\n", UnicodeStringPiece(pattern).data());
378 DecimalFormat
fmtr(pattern
, symbols
, status
);
379 if (U_FAILURE(status
)) {
380 errln("file dcfmtest.txt, line %d: %s error creating the formatter.",
381 lineNum
, u_errorName(status
));
384 if (round
=="ceiling") {
385 fmtr
.setRoundingMode(DecimalFormat::kRoundCeiling
);
386 } else if (round
=="floor") {
387 fmtr
.setRoundingMode(DecimalFormat::kRoundFloor
);
388 } else if (round
=="down") {
389 fmtr
.setRoundingMode(DecimalFormat::kRoundDown
);
390 } else if (round
=="up") {
391 fmtr
.setRoundingMode(DecimalFormat::kRoundUp
);
392 } else if (round
=="halfeven") {
393 fmtr
.setRoundingMode(DecimalFormat::kRoundHalfEven
);
394 } else if (round
=="halfdown") {
395 fmtr
.setRoundingMode(DecimalFormat::kRoundHalfDown
);
396 } else if (round
=="halfup") {
397 fmtr
.setRoundingMode(DecimalFormat::kRoundHalfUp
);
398 } else if (round
=="default") {
399 // don't set any value.
401 fmtr
.setRoundingMode(DecimalFormat::kRoundFloor
);
402 errln("file dcfmtest.txt, line %d: Bad rounding mode \"%s\"",
403 lineNum
, UnicodeStringPiece(round
).data());
406 UnicodeString result
;
407 UnicodeStringPiece
spInput(input
);
408 //fmtr.format(spInput, result, NULL, status);
411 fmtbl
.setDecimalNumber(spInput
, status
);
412 //NumberFormat &nfmtr = fmtr;
413 fmtr
.format(fmtbl
, result
, NULL
, status
);
415 if (U_FAILURE(status
)) {
416 errln("file dcfmtest.txt, line %d: format() returned %s.",
417 lineNum
, u_errorName(status
));
421 if (result
!= expected
) {
422 errln("file dcfmtest.txt, line %d: expected \"%s\", got \"%s\"",
423 lineNum
, UnicodeStringPiece(expected
).data(), UnicodeStringPiece(result
).data());
428 //-------------------------------------------------------------------------------
430 // Read a text data file, convert it from UTF-8 to UChars, and return the data
431 // in one big UChar * buffer, which the caller must delete.
433 // (Lightly modified version of a similar function in regextst.cpp)
435 //--------------------------------------------------------------------------------
436 UChar
*DecimalFormatTest::ReadAndConvertFile(const char *fileName
, int32_t &ulen
,
437 UErrorCode
&status
) {
438 UChar
*retPtr
= NULL
;
439 char *fileBuf
= NULL
;
440 const char *fileBufNoBOM
= NULL
;
444 if (U_FAILURE(status
)) {
451 f
= fopen(fileName
, "rb");
453 dataerrln("Error opening test data file %s\n", fileName
);
454 status
= U_FILE_ACCESS_ERROR
;
462 int32_t amtReadNoBOM
;
464 fseek( f
, 0, SEEK_END
);
466 fileBuf
= new char[fileSize
];
467 fseek(f
, 0, SEEK_SET
);
468 amtRead
= fread(fileBuf
, 1, fileSize
, f
);
469 if (amtRead
!= fileSize
|| fileSize
<= 0) {
470 errln("Error reading test data file.");
471 goto cleanUpAndReturn
;
475 // Look for a UTF-8 BOM on the data just read.
476 // The test data file is UTF-8.
477 // The BOM needs to be there in the source file to keep the Windows &
478 // EBCDIC machines happy, so force an error if it goes missing.
479 // Many Linux editors will silently strip it.
481 fileBufNoBOM
= fileBuf
+ 3;
482 amtReadNoBOM
= amtRead
- 3;
483 if (fileSize
<3 || uprv_strncmp(fileBuf
, "\xEF\xBB\xBF", 3) != 0) {
484 // TODO: restore this check.
485 errln("Test data file %s is missing its BOM", fileName
);
486 fileBufNoBOM
= fileBuf
;
487 amtReadNoBOM
= amtRead
;
491 // Find the length of the input in UTF-16 UChars
492 // (by preflighting the conversion)
494 u_strFromUTF8(NULL
, 0, &ulen
, fileBufNoBOM
, amtReadNoBOM
, &status
);
497 // Convert file contents from UTF-8 to UTF-16
499 if (status
== U_BUFFER_OVERFLOW_ERROR
) {
500 // Buffer Overflow is expected from the preflight operation.
501 status
= U_ZERO_ERROR
;
502 retPtr
= new UChar
[ulen
+1];
503 u_strFromUTF8(retPtr
, ulen
+1, NULL
, fileBufNoBOM
, amtReadNoBOM
, &status
);
509 if (U_FAILURE(status
)) {
510 errln("ICU Error \"%s\"\n", u_errorName(status
));
517 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */