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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2004-2009, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "itrbnfp.h"
#include "unicode/umachine.h"
#include "unicode/tblcoll.h"
#include "unicode/coleitr.h"
#include "unicode/ures.h"
#include "unicode/ustring.h"
#include "unicode/decimfmt.h"
#include <string.h>
// current macro not in icu1.8.1
#define TESTCASE(id,test) \
case id: \
name = #test; \
if (exec) { \
logln(#test "---"); \
logln(); \
test(); \
} \
break
void IntlTestRBNFParse::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/)
{
if (exec) logln("TestSuite RuleBasedNumberFormatParse");
switch (index) {
#if U_HAVE_RBNF
TESTCASE(0, TestParse);
TESTCASE(1, TestNullRuleSet);
#else
TESTCASE(0, TestRBNFParseDisabled);
#endif
default:
name = "";
break;
}
}
#if U_HAVE_RBNF
void
IntlTestRBNFParse::TestParse() {
// Try various rule parsing errors. Shouldn't crash.
logln("RBNF Parse test starting");
// these rules make no sense but behave rationally
const char* okrules[] = {
"",
"random text",
"%foo:bar",
"%foo: bar",
"0:",
"0::",
";",
";;",
"%%foo:;",
":",
"::",
":1",
":;",
":;:;",
"-",
"-1",
"-:",
".",
".1",
"[",
"]",
"[]",
"[foo]",
"[[]",
"[]]",
"[[]]",
"[][]",
"<",
"<<",
"<<<",
"10:;9:;",
">",
">>",
">>>",
"=",
"==",
"===",
"=foo=",
nullptr,
};
// these rules would throw exceptions when formatting, if we could throw exceptions
const char* exceptrules[] = {
"10:", // formatting any value with a one's digit will fail
"11: << x", // formatting a multiple of 10 causes rollback rule to fail
"%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;",
nullptr,
};
// none of these rules should crash the formatter
const char** allrules[] = {
okrules,
exceptrules,
nullptr,
};
for (int j = 0; allrules[j]; ++j) {
const char** rules = allrules[j];
for (int i = 0; rules[i]; ++i) {
const char* rule = rules[i];
logln("rule[%d] \"%s\"", i, rule);
UErrorCode status = U_ZERO_ERROR;
UParseError perr;
RuleBasedNumberFormat* formatter = new RuleBasedNumberFormat(rule, Locale::getUS(), perr, status);
if (U_SUCCESS(status)) {
// format some values
testfmt(formatter, 20, status);
testfmt(formatter, 1.23, status);
testfmt(formatter, -123, status);
testfmt(formatter, .123, status);
testfmt(formatter, 123, status);
} else if (status == U_PARSE_ERROR) {
logln("perror line: %x offset: %x context: %s|%s", perr.line, perr.offset, perr.preContext, perr.postContext);
}
delete formatter;
}
}
}
void
IntlTestRBNFParse::TestNullRuleSet() {
icu::UnicodeString fuzzstr = u"x00:a>>>b>#>";
UParseError perror;
UErrorCode status = U_ZERO_ERROR;
icu::RuleBasedNumberFormat rbfmt(fuzzstr, Locale::getUS(), perror, status);
if (status != U_PARSE_ERROR) {
logln("should get U_PARSE_ERROR");
}
}
void
IntlTestRBNFParse::testfmt(RuleBasedNumberFormat* formatter, double val, UErrorCode& status) {
UnicodeString us;
formatter->format(static_cast<const Formattable>(val), us, status);
if (U_SUCCESS(status)) {
us.insert(0, static_cast<char16_t>('"'));
us.append(static_cast<char16_t>('"'));
logln(us);
} else {
logln("error: could not format %g, returned status: %d", val, status);
}
}
void
IntlTestRBNFParse::testfmt(RuleBasedNumberFormat* formatter, int val, UErrorCode& status) {
UnicodeString us;
formatter->format(static_cast<const Formattable>(static_cast<int32_t>(val)), us, status);
if (U_SUCCESS(status)) {
us.insert(0, static_cast<char16_t>('"'));
us.append(static_cast<char16_t>('"'));
logln(us);
} else {
logln("error: could not format %d, returned status: %d", val, status);
}
}
/* U_HAVE_RBNF */
#else
void
IntlTestRBNF::TestRBNFParseDisabled() {
errln("*** RBNF currently disabled on this platform ***\n");
}
/* U_HAVE_RBNF */
#endif
#endif /* #if !UCONFIG_NO_FORMATTING */
|