]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/itrbnfp.cpp
2 *******************************************************************************
3 * Copyright (C) 2004-2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/umachine.h"
16 #include "unicode/tblcoll.h"
17 #include "unicode/coleitr.h"
18 #include "unicode/ures.h"
19 #include "unicode/ustring.h"
20 #include "unicode/decimfmt.h"
24 // current macro not in icu1.8.1
25 #define TESTCASE(id,test) \
35 void IntlTestRBNFParse::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
37 if (exec
) logln("TestSuite RuleBasedNumberFormatParse");
40 TESTCASE(0, TestParse
);
42 TESTCASE(0, TestRBNFParseDisabled
);
53 IntlTestRBNFParse::TestParse() {
54 // Try various rule parsing errors. Shouldn't crash.
56 logln("RBNF Parse test starting");
58 // these rules make no sense but behave rationally
59 const char* okrules
[] = {
102 // these rules would throw exceptions when formatting, if we could throw exceptions
103 const char* exceptrules
[] = {
104 "10:", // formatting any value with a one's digit will fail
105 "11: << x", // formating a multiple of 10 causes rollback rule to fail
106 "%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;",
111 // none of these rules should crash the formatter
112 const char** allrules
[] = {
118 for (int j
= 0; allrules
[j
]; ++j
) {
119 const char** rules
= allrules
[j
];
120 for (int i
= 0; rules
[i
]; ++i
) {
121 const char* rule
= rules
[i
];
122 logln("rule[%d] \"%s\"", i
, rule
);
123 UErrorCode status
= U_ZERO_ERROR
;
125 RuleBasedNumberFormat
* formatter
= new RuleBasedNumberFormat(rule
, Locale::getUS(), perr
, status
);
127 if (U_SUCCESS(status
)) {
128 // format some values
130 testfmt(formatter
, 20, status
);
131 testfmt(formatter
, 1.23, status
);
132 testfmt(formatter
, -123, status
);
133 testfmt(formatter
, .123, status
);
134 testfmt(formatter
, 123, status
);
136 } else if (status
== U_PARSE_ERROR
) {
137 logln("perror line: %x offset: %x context: %s|%s", perr
.line
, perr
.offset
, perr
.preContext
, perr
.postContext
);
146 IntlTestRBNFParse::testfmt(RuleBasedNumberFormat
* formatter
, double val
, UErrorCode
& status
) {
148 formatter
->format((const Formattable
)val
, us
, status
);
149 if (U_SUCCESS(status
)) {
150 us
.insert(0, (UChar
)'"');
151 us
.append((UChar
)'"');
154 logln("error: could not format %g, returned status: %d", val
, status
);
159 IntlTestRBNFParse::testfmt(RuleBasedNumberFormat
* formatter
, int val
, UErrorCode
& status
) {
161 formatter
->format((const Formattable
)(int32_t)val
, us
, status
);
162 if (U_SUCCESS(status
)) {
163 us
.insert(0, (UChar
)'"');
164 us
.append((UChar
)'"');
167 logln("error: could not format %d, returned status: %d", val
, status
);
176 IntlTestRBNF::TestRBNFParseDisabled() {
177 errln("*** RBNF currently disabled on this platform ***\n");
183 #endif /* #if !UCONFIG_NO_FORMATTING */