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