1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2004-2009, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
16 #include "unicode/umachine.h"
18 #include "unicode/tblcoll.h"
19 #include "unicode/coleitr.h"
20 #include "unicode/ures.h"
21 #include "unicode/ustring.h"
22 #include "unicode/decimfmt.h"
26 // current macro not in icu1.8.1
27 #define TESTCASE(id,test) \
37 void IntlTestRBNFParse::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/)
39 if (exec) logln("TestSuite RuleBasedNumberFormatParse");
42 TESTCASE(0, TestParse);
44 TESTCASE(0, TestRBNFParseDisabled);
55 IntlTestRBNFParse::TestParse() {
56 // Try various rule parsing errors. Shouldn't crash.
58 logln("RBNF Parse test starting");
60 // these rules make no sense but behave rationally
61 const char* okrules[] = {
104 // these rules would throw exceptions when formatting, if we could throw exceptions
105 const char* exceptrules[] = {
106 "10:", // formatting any value with a one's digit will fail
107 "11: << x", // formating a multiple of 10 causes rollback rule to fail
108 "%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;",
113 // none of these rules should crash the formatter
114 const char** allrules[] = {
120 for (int j = 0; allrules[j]; ++j) {
121 const char** rules = allrules[j];
122 for (int i = 0; rules[i]; ++i) {
123 const char* rule = rules[i];
124 logln("rule[%d] \"%s\"", i, rule);
125 UErrorCode status = U_ZERO_ERROR;
127 RuleBasedNumberFormat* formatter = new RuleBasedNumberFormat(rule, Locale::getUS(), perr, status);
129 if (U_SUCCESS(status)) {
130 // format some values
132 testfmt(formatter, 20, status);
133 testfmt(formatter, 1.23, status);
134 testfmt(formatter, -123, status);
135 testfmt(formatter, .123, status);
136 testfmt(formatter, 123, status);
138 } else if (status == U_PARSE_ERROR) {
139 logln("perror line: %x offset: %x context: %s|%s", perr.line, perr.offset, perr.preContext, perr.postContext);
148 IntlTestRBNFParse::testfmt(RuleBasedNumberFormat* formatter, double val, UErrorCode& status) {
150 formatter->format((const Formattable)val, us, status);
151 if (U_SUCCESS(status)) {
152 us.insert(0, (UChar)'"');
153 us.append((UChar)'"');
156 logln("error: could not format %g, returned status: %d", val, status);
161 IntlTestRBNFParse::testfmt(RuleBasedNumberFormat* formatter, int val, UErrorCode& status) {
163 formatter->format((const Formattable)(int32_t)val, us, status);
164 if (U_SUCCESS(status)) {
165 us.insert(0, (UChar)'"');
166 us.append((UChar)'"');
169 logln("error: could not format %d, returned status: %d", val, status);
178 IntlTestRBNF::TestRBNFParseDisabled() {
179 errln("*** RBNF currently disabled on this platform ***\n");
185 #endif /* #if !UCONFIG_NO_FORMATTING */