1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
9 #include "number_stringbuilder.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"
19 using namespace icu::number
;
20 using namespace icu::number::impl
;
21 using namespace icu::numparse
;
22 using namespace icu::numparse::impl
;
24 ////////////////////////////////////////////////////////////////////////////////////////
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 ///////////////////////////////////////////////////////////////////////////////////////////////
32 class AffixUtilsTest
: public IntlTest
{
36 void testContainsReplaceType();
38 void testUnescapeWithSymbolProvider();
40 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
43 UnicodeString
unescapeWithDefaults(const SymbolProvider
&defaultProvider
, UnicodeString input
,
47 class NumberFormatterApiTest
: public IntlTestWithFieldPosition
{
49 NumberFormatterApiTest();
50 NumberFormatterApiTest(UErrorCode
&status
);
52 void notationSimple();
53 void notationScientific();
54 void notationCompact();
56 void unitCompoundMeasure();
60 void roundingFraction();
61 void roundingFigures();
62 void roundingFractionFigures();
68 // TODO: Add this method if currency symbols override support is added.
69 //void symbolsOverride();
75 void fieldPositionLogic();
76 void fieldPositionCoverage();
81 void localPointerCAPI();
84 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
97 MeasureUnit SQUARE_METER
;
98 MeasureUnit FAHRENHEIT
;
101 MeasureUnit SQUARE_MILE
;
106 NumberingSystem MATHSANB
;
107 NumberingSystem LATN
;
109 DecimalFormatSymbols FRENCH_SYMBOLS
;
110 DecimalFormatSymbols SWISS_SYMBOLS
;
111 DecimalFormatSymbols MYANMAR_SYMBOLS
;
113 void assertFormatDescending(const char16_t* message
, const char16_t* skeleton
,
114 const UnlocalizedNumberFormatter
& f
, Locale locale
, ...);
116 void assertFormatDescendingBig(const char16_t* message
, const char16_t* skeleton
,
117 const UnlocalizedNumberFormatter
& f
, Locale locale
, ...);
120 assertFormatSingle(const char16_t* message
, const char16_t* skeleton
,
121 const UnlocalizedNumberFormatter
& f
, Locale locale
, double input
,
122 const UnicodeString
& expected
);
124 void assertUndefinedSkeleton(const UnlocalizedNumberFormatter
& f
);
126 void assertNumberFieldPositions(
127 const char16_t* message
,
128 const FormattedNumber
& formattedNumber
,
129 const UFieldPosition
* expectedFieldPositions
,
133 class DecimalQuantityTest
: public IntlTest
{
135 void testDecimalQuantityBehaviorStandalone();
136 void testSwitchStorage();
139 void testConvertToAccurateDouble();
140 void testUseApproximateDoubleWhenAble();
141 void testHardDoubleConversion();
143 void testMaxDigits();
144 void testNickelRounding();
146 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
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
);
155 class DoubleConversionTest
: public IntlTest
{
157 void testDoubleConversionApi();
159 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
162 class ModifiersTest
: public IntlTest
{
164 void testConstantAffixModifier();
165 void testConstantMultiFieldModifier();
166 void testSimpleModifier();
167 void testCurrencySpacingEnabledModifier();
169 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
172 void assertModifierEquals(const Modifier
&mod
, int32_t expectedPrefixLength
, bool expectedStrong
,
173 UnicodeString expectedChars
, UnicodeString expectedFields
,
176 void assertModifierEquals(const Modifier
&mod
, NumberStringBuilder
&sb
, int32_t expectedPrefixLength
,
177 bool expectedStrong
, UnicodeString expectedChars
,
178 UnicodeString expectedFields
, UErrorCode
&status
);
181 class PatternModifierTest
: public IntlTest
{
184 void testPatternWithNoPlaceholder();
185 void testMutableEqualsImmutable();
187 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
190 UnicodeString
getPrefix(const MutablePatternModifier
&mod
, UErrorCode
&status
);
191 UnicodeString
getSuffix(const MutablePatternModifier
&mod
, UErrorCode
&status
);
194 class PatternStringTest
: public IntlTest
{
196 void testLocalized();
197 void testToPatternSimple();
198 void testExceptionOnInvalid();
201 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
206 class NumberStringBuilderTest
: public IntlTest
{
208 void testInsertAppendUnicodeString();
210 void testInsertAppendCodePoint();
213 void testUnlimitedCapacity();
214 void testCodePoints();
216 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
219 void assertEqualsImpl(const UnicodeString
&a
, const NumberStringBuilder
&b
);
222 class StringSegmentTest
: public IntlTest
{
227 void testGetCodePoint();
228 void testCommonPrefixLength();
230 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
233 class NumberParserTest
: public IntlTest
{
237 void testSeriesMatcher();
238 void testCombinedCurrencyMatcher();
239 void testAffixPatternMatcher();
240 void testGroupingDisabled();
241 void testCaseFolding();
242 void test20360_BidiOverflow();
243 void testInfiniteRecursion();
245 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
248 class NumberSkeletonTest
: public IntlTest
{
251 void invalidTokens();
252 void unknownTokens();
253 void unexpectedTokens();
254 void duplicateValues();
255 void stemsRequiringOption();
256 void defaultTokens();
257 void flexibleSeparators();
259 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
262 void expectedErrorSkeleton(const char16_t** cases
, int32_t casesLen
);
265 class NumberRangeFormatterTest
: public IntlTestWithFieldPosition
{
267 NumberRangeFormatterTest();
268 NumberRangeFormatterTest(UErrorCode
&status
);
274 void testDifferentFormatters();
276 void testFieldPositions();
280 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
288 MeasureUnit KILOMETER
;
289 MeasureUnit FAHRENHEIT
;
292 void assertFormatRange(
293 const char16_t* message
,
294 const UnlocalizedNumberRangeFormatter
& f
,
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
);
307 FormattedNumberRange
assertFormattedRangeEquals(
308 const char16_t* message
,
309 const LocalizedNumberRangeFormatter
& l
,
312 const char16_t* expected
);
316 // NOTE: This macro is identical to the one in itformat.cpp
317 #define TESTCLASS(id, TestClass) \
321 logln(#TestClass " test---"); \
322 logln((UnicodeString)""); \
324 callTest(test, par); \
328 class NumberTest
: public IntlTest
{
330 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0) {
332 logln("TestSuite NumberTest: ");
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
353 #endif /* #if !UCONFIG_NO_FORMATTING */