1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2002-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
12 // Decimal Formatter tests, data driven.
17 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_REGULAR_EXPRESSIONS
19 #include "unicode/regex.h"
20 #include "unicode/uchar.h"
21 #include "unicode/ustring.h"
22 #include "unicode/unistr.h"
23 #include "unicode/dcfmtsym.h"
24 #include "unicode/decimfmt.h"
25 #include "unicode/locid.h"
34 #if defined(__GLIBCXX__)
35 namespace std
{ class type_info
; } // WORKAROUND: http://llvm.org/bugs/show_bug.cgi?id=13364
41 //---------------------------------------------------------------------------
43 // Test class boilerplate
45 //---------------------------------------------------------------------------
46 DecimalFormatTest::DecimalFormatTest()
51 DecimalFormatTest::~DecimalFormatTest()
57 void DecimalFormatTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
59 if (exec
) logln("TestSuite DecimalFormatTest: ");
62 #if !UCONFIG_NO_FILE_IO
63 case 0: name
= "DataDrivenTests";
64 if (exec
) DataDrivenTests();
67 case 0: name
= "skip";
72 break; //needed to end loop
77 //---------------------------------------------------------------------------
79 // Error Checking / Reporting macros used in all of the tests.
81 //---------------------------------------------------------------------------
82 #define DF_CHECK_STATUS {if (U_FAILURE(status)) \
83 {dataerrln("DecimalFormatTest failure at line %d. status=%s", \
84 __LINE__, u_errorName(status)); return 0;}}
86 #define DF_ASSERT(expr) {if ((expr)==FALSE) {errln("DecimalFormatTest failure at line %d.\n", __LINE__);};}
88 #define DF_ASSERT_FAIL(expr, errcode) {UErrorCode status=U_ZERO_ERROR; (expr);\
89 if (status!=errcode) {dataerrln("DecimalFormatTest failure at line %d. Expected status=%s, got %s", \
90 __LINE__, u_errorName(errcode), u_errorName(status));};}
92 #define DF_CHECK_STATUS_L(line) {if (U_FAILURE(status)) {errln( \
93 "DecimalFormatTest failure at line %d, from %d. status=%d\n",__LINE__, (line), status); }}
95 #define DF_ASSERT_L(expr, line) {if ((expr)==FALSE) { \
96 errln("DecimalFormatTest failure at line %d, from %d.", __LINE__, (line)); return;}}
101 // InvariantStringPiece
102 // Wrap a StringPiece around the extracted invariant data of a UnicodeString.
103 // The data is guaranteed to be nul terminated. (This is not true of StringPiece
104 // in general, but is true of InvariantStringPiece)
106 class InvariantStringPiece
: public StringPiece
{
108 InvariantStringPiece(const UnicodeString
&s
);
109 ~InvariantStringPiece() {};
111 MaybeStackArray
<char, 20> buf
;
114 InvariantStringPiece::InvariantStringPiece(const UnicodeString
&s
) {
115 int32_t len
= s
.length();
116 if (len
+1 > buf
.getCapacity()) {
119 // Buffer size is len+1 so that s.extract() will nul-terminate the string.
120 s
.extract(0, len
, buf
.getAlias(), len
+1, US_INV
);
121 this->set(buf
.getAlias(), len
);
125 // UnicodeStringPiece
126 // Wrap a StringPiece around the extracted (to the default charset) data of
127 // a UnicodeString. The extracted data is guaranteed to be nul terminated.
128 // (This is not true of StringPiece in general, but is true of UnicodeStringPiece)
130 class UnicodeStringPiece
: public StringPiece
{
132 UnicodeStringPiece(const UnicodeString
&s
);
133 ~UnicodeStringPiece() {};
135 MaybeStackArray
<char, 20> buf
;
138 UnicodeStringPiece::UnicodeStringPiece(const UnicodeString
&s
) {
139 int32_t len
= s
.length();
140 int32_t capacity
= buf
.getCapacity();
141 int32_t requiredCapacity
= s
.extract(0, len
, buf
.getAlias(), capacity
) + 1;
142 if (capacity
< requiredCapacity
) {
143 buf
.resize(requiredCapacity
);
144 capacity
= requiredCapacity
;
145 s
.extract(0, len
, buf
.getAlias(), capacity
);
147 this->set(buf
.getAlias(), requiredCapacity
- 1);
152 //---------------------------------------------------------------------------
155 // The test cases are in a separate data file,
157 //---------------------------------------------------------------------------
159 // Translate a Formattable::type enum value to a string, for error message formatting.
160 static const char *formattableType(Formattable::Type typ
) {
161 static const char *types
[] = {"kDate",
169 if (typ
<0 || typ
>Formattable::kObject
) {
176 DecimalFormatTest::getPath(char *buffer
, const char *filename
) {
177 UErrorCode status
=U_ZERO_ERROR
;
178 const char *testDataDirectory
= IntlTest::getSourceTestData(status
);
181 strcpy(buffer
, testDataDirectory
);
182 strcat(buffer
, filename
);
186 void DecimalFormatTest::DataDrivenTests() {
189 UErrorCode status
= U_ZERO_ERROR
;
193 // Open and read the test data file.
195 srcPath
=getPath(tdd
, "dcfmtest.txt");
197 return; /* something went wrong, error already output */
201 UChar
*testData
= ReadAndConvertFile(srcPath
, len
, status
);
202 if (U_FAILURE(status
)) {
203 return; /* something went wrong, error already output */
207 // Put the test data into a UnicodeString
209 UnicodeString
testString(FALSE
, testData
, len
);
211 RegexMatcher
parseLineMat(UnicodeString(
213 "\"([^\"]*)\"\\s+" // Capture group 1: input text
214 "([ild])\\s+" // Capture group 2: expected parsed type
215 "\"([^\"]*)\"\\s+" // Capture group 3: expected parsed decimal
216 "\\s*(?:#.*)?"), // Trailing comment
219 RegexMatcher
formatLineMat(UnicodeString(
221 "(\\S+)\\s+" // Capture group 1: pattern
222 "(ceiling|floor|down|up|halfeven|halfdown|halfup|default|unnecessary)\\s+" // Capture group 2: Rounding Mode
223 "\"([^\"]*)\"\\s+" // Capture group 3: input
224 "\"([^\"]*)\"" // Capture group 4: expected output
225 "\\s*(?:#.*)?"), // Trailing comment
228 RegexMatcher
commentMat (UNICODE_STRING_SIMPLE("\\s*(#.*)?$"), 0, status
);
229 RegexMatcher
lineMat(UNICODE_STRING_SIMPLE("(?m)^(.*?)$"), testString
, 0, status
);
231 if (U_FAILURE(status
)){
232 dataerrln("Construct RegexMatcher() error.");
238 // Loop over the test data file, once per line.
240 while (lineMat
.find()) {
242 if (U_FAILURE(status
)) {
243 dataerrln("File dcfmtest.txt, line %d: ICU Error \"%s\"", lineNum
, u_errorName(status
));
246 status
= U_ZERO_ERROR
;
247 UnicodeString testLine
= lineMat
.group(1, status
);
248 // printf("%s\n", UnicodeStringPiece(testLine).data());
249 if (testLine
.length() == 0) {
254 // Parse the test line. Skip blank and comment only lines.
255 // Separate out the three main fields - pattern, flags, target.
258 commentMat
.reset(testLine
);
259 if (commentMat
.lookingAt(status
)) {
260 // This line is a comment, or blank.
266 // Handle "parse" test case line from file
268 parseLineMat
.reset(testLine
);
269 if (parseLineMat
.lookingAt(status
)) {
270 execParseTest(lineNum
,
271 parseLineMat
.group(1, status
), // input
272 parseLineMat
.group(2, status
), // Expected Type
273 parseLineMat
.group(3, status
), // Expected Decimal String
280 // Handle "format" test case line
282 formatLineMat
.reset(testLine
);
283 if (formatLineMat
.lookingAt(status
)) {
284 execFormatTest(lineNum
,
285 formatLineMat
.group(1, status
), // Pattern
286 formatLineMat
.group(2, status
), // rounding mode
287 formatLineMat
.group(3, status
), // input decimal number
288 formatLineMat
.group(4, status
), // expected formatted result
292 execFormatTest(lineNum
,
293 formatLineMat
.group(1, status
), // Pattern
294 formatLineMat
.group(2, status
), // rounding mode
295 formatLineMat
.group(3, status
), // input decimal number
296 formatLineMat
.group(4, status
), // expected formatted result
303 // Line is not a recognizable test case.
305 errln("Badly formed test case at line %d.\n%s\n",
306 lineNum
, UnicodeStringPiece(testLine
).data());
315 void DecimalFormatTest::execParseTest(int32_t lineNum
,
316 const UnicodeString
&inputText
,
317 const UnicodeString
&expectedType
,
318 const UnicodeString
&expectedDecimal
,
319 UErrorCode
&status
) {
321 if (U_FAILURE(status
)) {
325 DecimalFormatSymbols
symbols(Locale::getUS(), status
);
326 UnicodeString pattern
= UNICODE_STRING_SIMPLE("####");
327 DecimalFormat
format(pattern
, symbols
, status
);
329 if (U_FAILURE(status
)) {
330 dataerrln("file dcfmtest.txt, line %d: %s error creating the formatter.",
331 lineNum
, u_errorName(status
));
336 int32_t expectedParseEndPosition
= inputText
.length();
338 format
.parse(inputText
, result
, pos
);
340 if (expectedParseEndPosition
!= pos
.getIndex()) {
341 errln("file dcfmtest.txt, line %d: Expected parse position afeter parsing: %d. "
342 "Actual parse position: %d", expectedParseEndPosition
, pos
.getIndex());
346 char expectedTypeC
[2];
347 expectedType
.extract(0, 1, expectedTypeC
, 2, US_INV
);
348 Formattable::Type expectType
= Formattable::kDate
;
349 switch (expectedTypeC
[0]) {
350 case 'd': expectType
= Formattable::kDouble
; break;
351 case 'i': expectType
= Formattable::kLong
; break;
352 case 'l': expectType
= Formattable::kInt64
; break;
354 errln("file dcfmtest.tx, line %d: unrecongized expected type \"%s\"",
355 lineNum
, InvariantStringPiece(expectedType
).data());
358 if (result
.getType() != expectType
) {
359 errln("file dcfmtest.txt, line %d: expectedParseType(%s) != actual parseType(%s)",
360 lineNum
, formattableType(expectType
), formattableType(result
.getType()));
364 StringPiece decimalResult
= result
.getDecimalNumber(status
);
365 if (U_FAILURE(status
)) {
366 errln("File %s, line %d: error %s. Line in file dcfmtest.txt: %d:",
367 __FILE__
, __LINE__
, u_errorName(status
), lineNum
);
371 InvariantStringPiece
expectedResults(expectedDecimal
);
372 if (decimalResult
!= expectedResults
) {
373 errln("file dcfmtest.txt, line %d: expected \"%s\", got \"%s\"",
374 lineNum
, expectedResults
.data(), decimalResult
.data());
381 void DecimalFormatTest::execFormatTest(int32_t lineNum
,
382 const UnicodeString
&pattern
, // Pattern
383 const UnicodeString
&round
, // rounding mode
384 const UnicodeString
&input
, // input decimal number
385 const UnicodeString
&expected
, // expected formatted result
386 EFormatInputType inType
, // input number type
387 UErrorCode
&status
) {
388 if (U_FAILURE(status
)) {
392 DecimalFormatSymbols
symbols(Locale::getUS(), status
);
393 // printf("Pattern = %s\n", UnicodeStringPiece(pattern).data());
394 DecimalFormat
fmtr(pattern
, symbols
, status
);
395 if (U_FAILURE(status
)) {
396 dataerrln("file dcfmtest.txt, line %d: %s error creating the formatter.",
397 lineNum
, u_errorName(status
));
400 if (round
=="ceiling") {
401 fmtr
.setRoundingMode(DecimalFormat::kRoundCeiling
);
402 } else if (round
=="floor") {
403 fmtr
.setRoundingMode(DecimalFormat::kRoundFloor
);
404 } else if (round
=="down") {
405 fmtr
.setRoundingMode(DecimalFormat::kRoundDown
);
406 } else if (round
=="up") {
407 fmtr
.setRoundingMode(DecimalFormat::kRoundUp
);
408 } else if (round
=="halfeven") {
409 fmtr
.setRoundingMode(DecimalFormat::kRoundHalfEven
);
410 } else if (round
=="halfdown") {
411 fmtr
.setRoundingMode(DecimalFormat::kRoundHalfDown
);
412 } else if (round
=="halfup") {
413 fmtr
.setRoundingMode(DecimalFormat::kRoundHalfUp
);
414 } else if (round
=="default") {
415 // don't set any value.
416 } else if (round
=="unnecessary") {
417 fmtr
.setRoundingMode(DecimalFormat::kRoundUnnecessary
);
419 fmtr
.setRoundingMode(DecimalFormat::kRoundFloor
);
420 errln("file dcfmtest.txt, line %d: Bad rounding mode \"%s\"",
421 lineNum
, UnicodeStringPiece(round
).data());
424 const char *typeStr
= "Unknown";
425 UnicodeString result
;
426 UnicodeStringPiece
spInput(input
);
431 typeStr
= "Formattable";
433 fmtbl
.setDecimalNumber(spInput
, status
);
434 fmtr
.format(fmtbl
, result
, NULL
, status
);
438 typeStr
= "StringPiece";
439 fmtr
.format(spInput
, result
, NULL
, status
);
443 if ((status
== U_FORMAT_INEXACT_ERROR
) && (result
== "") && (expected
== "Inexact")) {
445 status
= U_ZERO_ERROR
;
449 if (U_FAILURE(status
)) {
450 errln("[%s] file dcfmtest.txt, line %d: format() returned %s.",
451 typeStr
, lineNum
, u_errorName(status
));
452 status
= U_ZERO_ERROR
;
456 if (result
!= expected
) {
457 errln("[%s] file dcfmtest.txt, line %d: expected \"%s\", got \"%s\"",
458 typeStr
, lineNum
, UnicodeStringPiece(expected
).data(), UnicodeStringPiece(result
).data());
463 //-------------------------------------------------------------------------------
465 // Read a text data file, convert it from UTF-8 to UChars, and return the data
466 // in one big UChar * buffer, which the caller must delete.
468 // (Lightly modified version of a similar function in regextst.cpp)
470 //--------------------------------------------------------------------------------
471 UChar
*DecimalFormatTest::ReadAndConvertFile(const char *fileName
, int32_t &ulen
,
472 UErrorCode
&status
) {
473 UChar
*retPtr
= NULL
;
474 char *fileBuf
= NULL
;
475 const char *fileBufNoBOM
= NULL
;
479 if (U_FAILURE(status
)) {
486 f
= fopen(fileName
, "rb");
488 dataerrln("Error opening test data file %s\n", fileName
);
489 status
= U_FILE_ACCESS_ERROR
;
497 int32_t amtReadNoBOM
;
499 fseek( f
, 0, SEEK_END
);
501 fileBuf
= new char[fileSize
];
502 fseek(f
, 0, SEEK_SET
);
503 amtRead
= fread(fileBuf
, 1, fileSize
, f
);
504 if (amtRead
!= fileSize
|| fileSize
<= 0) {
505 errln("Error reading test data file.");
506 goto cleanUpAndReturn
;
510 // Look for a UTF-8 BOM on the data just read.
511 // The test data file is UTF-8.
512 // The BOM needs to be there in the source file to keep the Windows &
513 // EBCDIC machines happy, so force an error if it goes missing.
514 // Many Linux editors will silently strip it.
516 fileBufNoBOM
= fileBuf
+ 3;
517 amtReadNoBOM
= amtRead
- 3;
518 if (fileSize
<3 || uprv_strncmp(fileBuf
, "\xEF\xBB\xBF", 3) != 0) {
519 // TODO: restore this check.
520 errln("Test data file %s is missing its BOM", fileName
);
521 fileBufNoBOM
= fileBuf
;
522 amtReadNoBOM
= amtRead
;
526 // Find the length of the input in UTF-16 UChars
527 // (by preflighting the conversion)
529 u_strFromUTF8(NULL
, 0, &ulen
, fileBufNoBOM
, amtReadNoBOM
, &status
);
532 // Convert file contents from UTF-8 to UTF-16
534 if (status
== U_BUFFER_OVERFLOW_ERROR
) {
535 // Buffer Overflow is expected from the preflight operation.
536 status
= U_ZERO_ERROR
;
537 retPtr
= new UChar
[ulen
+1];
538 u_strFromUTF8(retPtr
, ulen
+1, NULL
, fileBufNoBOM
, amtReadNoBOM
, &status
);
544 if (U_FAILURE(status
)) {
545 errln("ICU Error \"%s\"\n", u_errorName(status
));
552 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */