1 /************************************************************************ 2 * COPYRIGHT: 3 * Copyright (c) 1997-2015, International Business Machines Corporation 4 * and others. All Rights Reserved. 5 ************************************************************************/ 6 7 #ifndef _NUMBERFORMATTEST_ 8 #define _NUMBERFORMATTEST_ 9 10 #include "unicode/utypes.h" 11 12 #if !UCONFIG_NO_FORMATTING 13 14 #include "unicode/numfmt.h" 15 #include "unicode/decimfmt.h" 16 #include "caltztst.h" 17 18 /** 19 * Expected field positions from field position iterator. Tests should 20 * stack allocate an array of these making sure that the last element is 21 * {0, -1, 0} (The sentinel element indicating end of iterator). Then test 22 * should call verifyFieldPositionIterator() passing both this array of 23 * expected results and the field position iterator from the format method. 24 */ 25 struct NumberFormatTest_Attributes { 26 int32_t id; 27 int32_t spos; 28 int32_t epos; 29 }; 30 31 /** 32 * Performs various in-depth test on NumberFormat 33 **/ 34 class NumberFormatTest: public CalendarTimeZoneTest { 35 36 // IntlTest override 37 void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par ); 38 public: 39 40 /** 41 * Test APIs (to increase code coverage) 42 */ 43 void TestAPI(void); 44 45 void TestCoverage(void); 46 47 /** 48 * Test the handling of quotes 49 **/ 50 void TestQuotes(void); 51 /** 52 * Test patterns with exponential representation 53 **/ 54 void TestExponential(void); 55 /** 56 * Test handling of patterns with currency symbols 57 **/ 58 void TestCurrencySign(void); 59 /** 60 * Test different format patterns 61 **/ 62 void TestPatterns(void); 63 /** 64 * API coverage for DigitList 65 **/ 66 //void TestDigitList(void); 67 68 /** 69 * Test localized currency patterns. 70 */ 71 void TestCurrency(void); 72 73 /** 74 * Test the Currency object handling, new as of ICU 2.2. 75 */ 76 void TestCurrencyObject(void); 77 78 void TestCurrencyPatterns(void); 79 80 /** 81 * Do rudimentary testing of parsing. 82 */ 83 void TestParse(void); 84 /** 85 * Test proper rounding by the format method. 86 */ 87 void TestRounding487(void); 88 89 // New tests for alphaWorks upgrade 90 void TestExponent(void); 91 92 void TestScientific(void); 93 94 void TestScientific2(void); 95 96 void TestScientificGrouping(void); 97 98 void TestInt64(void); 99 100 void TestSurrogateSupport(void); 101 102 /** 103 * Test the functioning of the secondary grouping value. 104 */ 105 void TestSecondaryGrouping(void); 106 107 void TestWhiteSpaceParsing(void); 108 109 void TestComplexCurrency(void); 110 111 void TestPad(void); 112 void TestPatterns2(void); 113 114 /** 115 * Test currency registration. 116 */ 117 void TestRegCurrency(void); 118 119 void TestCurrencyNames(void); 120 121 void TestCurrencyAmount(void); 122 123 void TestCurrencyUnit(void); 124 125 void TestSymbolsWithBadLocale(void); 126 127 void TestAdoptDecimalFormatSymbols(void); 128 129 void TestPerMill(void); 130 131 void TestIllegalPatterns(void); 132 133 void TestCases(void); 134 135 void TestJB3832(void); 136 137 void TestHost(void); 138 139 void TestHostClone(void); 140 141 void TestCurrencyFormat(void); 142 143 /* Port of ICU4J rounding test. */ 144 void TestRounding(void); 145 146 void TestRoundingPattern(void); 147 148 void TestNonpositiveMultiplier(void); 149 150 void TestNumberingSystems(); 151 152 153 void TestSpaceParsing(); 154 void TestMultiCurrencySign(); 155 void TestCurrencyFormatForMixParsing(); 156 void TestDecimalFormatCurrencyParse(); 157 void TestCurrencyIsoPluralFormat(); 158 void TestCurrencyParsing(); 159 void TestParseCurrencyInUCurr(); 160 void TestFormatAttributes(); 161 void TestFieldPositionIterator(); 162 163 void TestLenientParse(); 164 165 void TestDecimal(); 166 void TestCurrencyFractionDigits(); 167 168 void TestExponentParse(); 169 void TestExplicitParents(); 170 void TestAvailableNumberingSystems(); 171 void Test9087(); 172 void TestFormatFastpaths(); 173 174 void TestFormattableSize(); 175 176 void TestUFormattable(); 177 178 void TestEnumSet(); 179 180 void TestSignificantDigits(); 181 void TestShowZero(); 182 183 void TestCompatibleCurrencies(); 184 void TestBug9936(); 185 void TestParseNegativeWithFaLocale(); 186 void TestParseNegativeWithAlternateMinusSign(); 187 188 void TestCustomCurrencySignAndSeparator(); 189 190 void TestParseSignsAndMarks(); 191 void Test10419RoundingWith0FractionDigits(); 192 void Test10468ApplyPattern(); 193 void TestRoundingScientific10542(); 194 void TestZeroScientific10547(); 195 void TestAccountingCurrency(); 196 void TestEquality(); 197 198 void TestCurrencyUsage(); 199 void TestNumberFormatTestTuple(); 200 void TestDataDriven(); 201 202 void TestDoubleLimit11439(); 203 void TestFastPathConsistent11524(); 204 void TestGetAffixes(); 205 void TestToPatternScientific11648(); 206 void TestBenchmark(); 207 void TestCtorApplyPatternDifference(); 208 void TestFractionalDigitsForCurrency(); 209 void TestFormatCurrencyPlural(); 210 void Test11868(); 211 void Test10727_RoundingZero(); 212 void Test11376_getAndSetPositivePrefix(); 213 void Test11475_signRecognition(); 214 void Test11640_getAffixes(); 215 void Test11649_toPatternWithMultiCurrency(); 216 217 private: 218 UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f); 219 220 void expectParseCurrency(const NumberFormat &fmt, const UChar* currency, double amount, const char *text); 221 222 static UBool equalValue(const Formattable& a, const Formattable& b); 223 224 void expectPositions(FieldPositionIterator& iter, int32_t *values, int32_t tupleCount, 225 const UnicodeString& str); 226 227 void expectPosition(FieldPosition& pos, int32_t id, int32_t start, int32_t limit, 228 const UnicodeString& str); 229 230 void expect2(NumberFormat& fmt, const Formattable& n, const UnicodeString& str); 231 232 void expect3(NumberFormat& fmt, const Formattable& n, const UnicodeString& str); 233 expect2(NumberFormat & fmt,const Formattable & n,const char * str)234 void expect2(NumberFormat& fmt, const Formattable& n, const char* str) { 235 expect2(fmt, n, UnicodeString(str, "")); 236 } 237 238 void expect2(NumberFormat* fmt, const Formattable& n, const UnicodeString& str, UErrorCode ec); 239 expect2(NumberFormat * fmt,const Formattable & n,const char * str,UErrorCode ec)240 void expect2(NumberFormat* fmt, const Formattable& n, const char* str, UErrorCode ec) { 241 expect2(fmt, n, UnicodeString(str, ""), ec); 242 } 243 244 void expect(NumberFormat& fmt, const UnicodeString& str, const Formattable& n); 245 expect(NumberFormat & fmt,const char * str,const Formattable & n)246 void expect(NumberFormat& fmt, const char *str, const Formattable& n) { 247 expect(fmt, UnicodeString(str, ""), n); 248 } 249 250 void expect(NumberFormat& fmt, const Formattable& n, 251 const UnicodeString& exp, UBool rt=TRUE); 252 253 void expect(NumberFormat& fmt, const Formattable& n, 254 const char *exp, UBool rt=TRUE) { 255 expect(fmt, n, UnicodeString(exp, ""), rt); 256 } 257 258 void expect(NumberFormat* fmt, const Formattable& n, 259 const UnicodeString& exp, UBool rt, UErrorCode errorCode); 260 expect(NumberFormat * fmt,const Formattable & n,const char * exp,UBool rt,UErrorCode errorCode)261 void expect(NumberFormat* fmt, const Formattable& n, 262 const char *exp, UBool rt, UErrorCode errorCode) { 263 expect(fmt, n, UnicodeString(exp, ""), rt, errorCode); 264 } 265 expect(NumberFormat * fmt,const Formattable & n,const UnicodeString & exp,UErrorCode errorCode)266 void expect(NumberFormat* fmt, const Formattable& n, 267 const UnicodeString& exp, UErrorCode errorCode) { 268 expect(fmt, n, exp, TRUE, errorCode); 269 } 270 expect(NumberFormat * fmt,const Formattable & n,const char * exp,UErrorCode errorCode)271 void expect(NumberFormat* fmt, const Formattable& n, 272 const char *exp, UErrorCode errorCode) { 273 expect(fmt, n, UnicodeString(exp, ""), TRUE, errorCode); 274 } 275 276 void expectCurrency(NumberFormat& nf, const Locale& locale, 277 double value, const UnicodeString& string); 278 279 void expectPad(DecimalFormat& fmt, const UnicodeString& pat, 280 int32_t pos, int32_t width, UChar pad); 281 expectPad(DecimalFormat & fmt,const char * pat,int32_t pos,int32_t width,UChar pad)282 void expectPad(DecimalFormat& fmt, const char *pat, 283 int32_t pos, int32_t width, UChar pad) { 284 expectPad(fmt, UnicodeString(pat, ""), pos, width, pad); 285 } 286 287 void expectPad(DecimalFormat& fmt, const UnicodeString& pat, 288 int32_t pos, int32_t width, const UnicodeString& pad); 289 expectPad(DecimalFormat & fmt,const char * pat,int32_t pos,int32_t width,const UnicodeString & pad)290 void expectPad(DecimalFormat& fmt, const char *pat, 291 int32_t pos, int32_t width, const UnicodeString& pad) { 292 expectPad(fmt, UnicodeString(pat, ""), pos, width, pad); 293 } 294 295 void expectPat(DecimalFormat& fmt, const UnicodeString& exp); 296 expectPat(DecimalFormat & fmt,const char * exp)297 void expectPat(DecimalFormat& fmt, const char *exp) { 298 expectPat(fmt, UnicodeString(exp, "")); 299 } 300 301 void expectPad(DecimalFormat& fmt, const UnicodeString& pat, 302 int32_t pos); 303 expectPad(DecimalFormat & fmt,const char * pat,int32_t pos)304 void expectPad(DecimalFormat& fmt, const char *pat, 305 int32_t pos) { 306 expectPad(fmt, pat, pos, 0, (UChar)0); 307 } 308 309 void expect_rbnf(NumberFormat& fmt, const UnicodeString& str, const Formattable& n); 310 311 void expect_rbnf(NumberFormat& fmt, const Formattable& n, 312 const UnicodeString& exp, UBool rt=TRUE); 313 314 // internal utility routine 315 static UnicodeString& escape(UnicodeString& s); 316 317 enum { ILLEGAL = -1 }; 318 319 // internal subtest used by TestRounding487 320 void roundingTest(NumberFormat& nf, double x, int32_t maxFractionDigits, const char* expected); 321 322 // internal rounding checking for TestRounding 323 void checkRounding(DecimalFormat* df, double base, int iterations, double increment); 324 325 double checkRound(DecimalFormat* df, double iValue, double lastParsed); 326 327 void verifyRounding( 328 DecimalFormat& format, 329 const double *values, 330 const char * const *expected, 331 const DecimalFormat::ERoundingMode *roundingModes, 332 const char * const *descriptions, 333 int32_t valueSize, 334 int32_t roundingModeSize); 335 336 void verifyFieldPositionIterator( 337 NumberFormatTest_Attributes *expected, 338 FieldPositionIterator &iter); 339 340 }; 341 342 #endif /* #if !UCONFIG_NO_FORMATTING */ 343 344 #endif // _NUMBERFORMATTEST_ 345