• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 #pragma once
8 
9 #include "number_stringbuilder.h"
10 #include "intltest.h"
11 #include "number_affixutils.h"
12 #include "numparse_stringsegment.h"
13 #include "numrange_impl.h"
14 #include "unicode/locid.h"
15 #include "unicode/numberformatter.h"
16 #include "unicode/numberrangeformatter.h"
17 
18 using namespace icu::number;
19 using namespace icu::number::impl;
20 using namespace icu::numparse;
21 using namespace icu::numparse::impl;
22 
23 ////////////////////////////////////////////////////////////////////////////////////////
24 // INSTRUCTIONS:                                                                      //
25 // To add new NumberFormat unit test classes, create a new class like the ones below, //
26 // and then add it as a switch statement in NumberTest at the bottom of this file.    /////////
27 // To add new methods to existing unit test classes, add the method to the class declaration //
28 // below, and also add it to the class's implementation of runIndexedTest().                 //
29 ///////////////////////////////////////////////////////////////////////////////////////////////
30 
31 class AffixUtilsTest : public IntlTest {
32   public:
33     void testEscape();
34     void testUnescape();
35     void testContainsReplaceType();
36     void testInvalid();
37     void testUnescapeWithSymbolProvider();
38 
39     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
40 
41   private:
42     UnicodeString unescapeWithDefaults(const SymbolProvider &defaultProvider, UnicodeString input,
43                                        UErrorCode &status);
44 };
45 
46 class NumberFormatterApiTest : public IntlTest {
47   public:
48     NumberFormatterApiTest();
49     NumberFormatterApiTest(UErrorCode &status);
50 
51     void notationSimple();
52     void notationScientific();
53     void notationCompact();
54     void unitMeasure();
55     void unitCompoundMeasure();
56     void unitCurrency();
57     void unitPercent();
58     void roundingFraction();
59     void roundingFigures();
60     void roundingFractionFigures();
61     void roundingOther();
62     void grouping();
63     void padding();
64     void integerWidth();
65     void symbols();
66     // TODO: Add this method if currency symbols override support is added.
67     //void symbolsOverride();
68     void sign();
69     void decimal();
70     void scale();
71     void locale();
72     void formatTypes();
73     void fieldPosition();
74     void toFormat();
75     void errors();
76     void validRanges();
77     void copyMove();
78     void localPointerCAPI();
79 
80     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
81 
82   private:
83     CurrencyUnit USD;
84     CurrencyUnit GBP;
85     CurrencyUnit CZK;
86     CurrencyUnit CAD;
87     CurrencyUnit ESP;
88     CurrencyUnit PTE;
89 
90     MeasureUnit METER;
91     MeasureUnit DAY;
92     MeasureUnit SQUARE_METER;
93     MeasureUnit FAHRENHEIT;
94     MeasureUnit SECOND;
95     MeasureUnit POUND;
96     MeasureUnit SQUARE_MILE;
97     MeasureUnit JOULE;
98     MeasureUnit FURLONG;
99     MeasureUnit KELVIN;
100 
101     NumberingSystem MATHSANB;
102     NumberingSystem LATN;
103 
104     DecimalFormatSymbols FRENCH_SYMBOLS;
105     DecimalFormatSymbols SWISS_SYMBOLS;
106     DecimalFormatSymbols MYANMAR_SYMBOLS;
107 
108     void assertFormatDescending(const char16_t* message, const char16_t* skeleton,
109                                 const UnlocalizedNumberFormatter& f, Locale locale, ...);
110 
111     void assertFormatDescendingBig(const char16_t* message, const char16_t* skeleton,
112                                    const UnlocalizedNumberFormatter& f, Locale locale, ...);
113 
114     void assertFormatSingle(const char16_t* message, const char16_t* skeleton,
115                             const UnlocalizedNumberFormatter& f, Locale locale, double input,
116                             const UnicodeString& expected);
117 
118     void assertUndefinedSkeleton(const UnlocalizedNumberFormatter& f);
119 };
120 
121 class DecimalQuantityTest : public IntlTest {
122   public:
123     void testDecimalQuantityBehaviorStandalone();
124     void testSwitchStorage();
125     void testCopyMove();
126     void testAppend();
127     void testConvertToAccurateDouble();
128     void testUseApproximateDoubleWhenAble();
129     void testHardDoubleConversion();
130     void testToDouble();
131     void testMaxDigits();
132 
133     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
134 
135   private:
136     void assertDoubleEquals(UnicodeString message, double a, double b);
137     void assertHealth(const DecimalQuantity &fq);
138     void assertToStringAndHealth(const DecimalQuantity &fq, const UnicodeString &expected);
139     void checkDoubleBehavior(double d, bool explicitRequired);
140 };
141 
142 class DoubleConversionTest : public IntlTest {
143   public:
144     void testDoubleConversionApi();
145 
146     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
147 };
148 
149 class ModifiersTest : public IntlTest {
150   public:
151     void testConstantAffixModifier();
152     void testConstantMultiFieldModifier();
153     void testSimpleModifier();
154     void testCurrencySpacingEnabledModifier();
155 
156     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
157 
158   private:
159     void assertModifierEquals(const Modifier &mod, int32_t expectedPrefixLength, bool expectedStrong,
160                               UnicodeString expectedChars, UnicodeString expectedFields,
161                               UErrorCode &status);
162 
163     void assertModifierEquals(const Modifier &mod, NumberStringBuilder &sb, int32_t expectedPrefixLength,
164                               bool expectedStrong, UnicodeString expectedChars,
165                               UnicodeString expectedFields, UErrorCode &status);
166 };
167 
168 class PatternModifierTest : public IntlTest {
169   public:
170     void testBasic();
171     void testPatternWithNoPlaceholder();
172     void testMutableEqualsImmutable();
173 
174     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
175 
176   private:
177     UnicodeString getPrefix(const MutablePatternModifier &mod, UErrorCode &status);
178     UnicodeString getSuffix(const MutablePatternModifier &mod, UErrorCode &status);
179 };
180 
181 class PatternStringTest : public IntlTest {
182   public:
183     void testLocalized();
184     void testToPatternSimple();
185     void testExceptionOnInvalid();
186     void testBug13117();
187 
188     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
189 
190   private:
191 };
192 
193 class NumberStringBuilderTest : public IntlTest {
194   public:
195     void testInsertAppendUnicodeString();
196     void testSplice();
197     void testInsertAppendCodePoint();
198     void testCopy();
199     void testFields();
200     void testUnlimitedCapacity();
201     void testCodePoints();
202 
203     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
204 
205   private:
206     void assertEqualsImpl(const UnicodeString &a, const NumberStringBuilder &b);
207 };
208 
209 class StringSegmentTest : public IntlTest {
210   public:
211     void testOffset();
212     void testLength();
213     void testCharAt();
214     void testGetCodePoint();
215     void testCommonPrefixLength();
216 
217     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
218 };
219 
220 class NumberParserTest : public IntlTest {
221   public:
222     void testBasic();
223     void testLocaleFi();
224     void testSeriesMatcher();
225     void testCombinedCurrencyMatcher();
226     void testAffixPatternMatcher();
227     void testGroupingDisabled();
228     void testCaseFolding();
229 
230     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
231 };
232 
233 class NumberSkeletonTest : public IntlTest {
234   public:
235     void validTokens();
236     void invalidTokens();
237     void unknownTokens();
238     void unexpectedTokens();
239     void duplicateValues();
240     void stemsRequiringOption();
241     void defaultTokens();
242     void flexibleSeparators();
243 
244     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
245 
246   private:
247     void expectedErrorSkeleton(const char16_t** cases, int32_t casesLen);
248 };
249 
250 class NumberRangeFormatterTest : public IntlTest {
251   public:
252     NumberRangeFormatterTest();
253     NumberRangeFormatterTest(UErrorCode &status);
254 
255     void testSanity();
256     void testBasic();
257     void testCollapse();
258     void testIdentity();
259     void testDifferentFormatters();
260     void testPlurals();
261     void testCopyMove();
262 
263     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
264 
265   private:
266     CurrencyUnit USD;
267     CurrencyUnit GBP;
268     CurrencyUnit PTE;
269 
270     MeasureUnit METER;
271     MeasureUnit KILOMETER;
272     MeasureUnit FAHRENHEIT;
273     MeasureUnit KELVIN;
274 
275     void assertFormatRange(
276       const char16_t* message,
277       const UnlocalizedNumberRangeFormatter& f,
278       Locale locale,
279       const char16_t* expected_10_50,
280       const char16_t* expected_49_51,
281       const char16_t* expected_50_50,
282       const char16_t* expected_00_30,
283       const char16_t* expected_00_00,
284       const char16_t* expected_30_3K,
285       const char16_t* expected_30K_50K,
286       const char16_t* expected_49K_51K,
287       const char16_t* expected_50K_50K,
288       const char16_t* expected_50K_50M);
289 
290     void assertFormattedRangeEquals(
291       const char16_t* message,
292       const LocalizedNumberRangeFormatter& l,
293       double first,
294       double second,
295       const char16_t* expected);
296 };
297 
298 
299 // NOTE: This macro is identical to the one in itformat.cpp
300 #define TESTCLASS(id, TestClass)          \
301     case id:                              \
302         name = #TestClass;                \
303         if (exec) {                       \
304             logln(#TestClass " test---"); \
305             logln((UnicodeString)"");     \
306             TestClass test;               \
307             callTest(test, par);          \
308         }                                 \
309         break
310 
311 class NumberTest : public IntlTest {
312   public:
313     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0) {
314         if (exec) {
315             logln("TestSuite NumberTest: ");
316         }
317 
318         switch (index) {
319         TESTCLASS(0, AffixUtilsTest);
320         TESTCLASS(1, NumberFormatterApiTest);
321         TESTCLASS(2, DecimalQuantityTest);
322         TESTCLASS(3, ModifiersTest);
323         TESTCLASS(4, PatternModifierTest);
324         TESTCLASS(5, PatternStringTest);
325         TESTCLASS(6, NumberStringBuilderTest);
326         TESTCLASS(7, DoubleConversionTest);
327         TESTCLASS(8, StringSegmentTest);
328         TESTCLASS(9, NumberParserTest);
329         TESTCLASS(10, NumberSkeletonTest);
330         TESTCLASS(11, NumberRangeFormatterTest);
331         default: name = ""; break; // needed to end loop
332         }
333     }
334 };
335 
336 #endif /* #if !UCONFIG_NO_FORMATTING */
337