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