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