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 "formatted_string_builder.h"
12 #include "number_affixutils.h"
13 #include "string_segment.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 skeletonUserGuideExamples();
77 void fieldPositionLogic();
78 void fieldPositionCoverage();
83 void localPointerCAPI();
85 void toDecimalNumber();
87 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
100 MeasureUnit SQUARE_METER
;
101 MeasureUnit FAHRENHEIT
;
104 MeasureUnit SQUARE_MILE
;
109 NumberingSystem MATHSANB
;
110 NumberingSystem LATN
;
112 DecimalFormatSymbols FRENCH_SYMBOLS
;
113 DecimalFormatSymbols SWISS_SYMBOLS
;
114 DecimalFormatSymbols MYANMAR_SYMBOLS
;
116 void assertFormatDescending(const char16_t* message
, const char16_t* skeleton
,
117 const UnlocalizedNumberFormatter
& f
, Locale locale
, ...);
119 void assertFormatDescendingBig(const char16_t* message
, const char16_t* skeleton
,
120 const UnlocalizedNumberFormatter
& f
, Locale locale
, ...);
123 assertFormatSingle(const char16_t* message
, const char16_t* skeleton
,
124 const UnlocalizedNumberFormatter
& f
, Locale locale
, double input
,
125 const UnicodeString
& expected
);
127 void assertUndefinedSkeleton(const UnlocalizedNumberFormatter
& f
);
129 void assertNumberFieldPositions(
130 const char16_t* message
,
131 const FormattedNumber
& formattedNumber
,
132 const UFieldPosition
* expectedFieldPositions
,
136 class DecimalQuantityTest
: public IntlTest
{
138 void testDecimalQuantityBehaviorStandalone();
139 void testSwitchStorage();
142 void testConvertToAccurateDouble();
143 void testUseApproximateDoubleWhenAble();
144 void testHardDoubleConversion();
146 void testMaxDigits();
147 void testNickelRounding();
149 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
152 void assertDoubleEquals(UnicodeString message
, double a
, double b
);
153 void assertHealth(const DecimalQuantity
&fq
);
154 void assertToStringAndHealth(const DecimalQuantity
&fq
, const UnicodeString
&expected
);
155 void checkDoubleBehavior(double d
, bool explicitRequired
);
158 class DoubleConversionTest
: public IntlTest
{
160 void testDoubleConversionApi();
162 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
165 class ModifiersTest
: public IntlTest
{
167 void testConstantAffixModifier();
168 void testConstantMultiFieldModifier();
169 void testSimpleModifier();
170 void testCurrencySpacingEnabledModifier();
172 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
175 void assertModifierEquals(const Modifier
&mod
, int32_t expectedPrefixLength
, bool expectedStrong
,
176 UnicodeString expectedChars
, UnicodeString expectedFields
,
179 void assertModifierEquals(const Modifier
&mod
, FormattedStringBuilder
&sb
, int32_t expectedPrefixLength
,
180 bool expectedStrong
, UnicodeString expectedChars
,
181 UnicodeString expectedFields
, UErrorCode
&status
);
184 class PatternModifierTest
: public IntlTest
{
187 void testPatternWithNoPlaceholder();
188 void testMutableEqualsImmutable();
190 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
193 UnicodeString
getPrefix(const MutablePatternModifier
&mod
, UErrorCode
&status
);
194 UnicodeString
getSuffix(const MutablePatternModifier
&mod
, UErrorCode
&status
);
197 class PatternStringTest
: public IntlTest
{
199 void testLocalized();
200 void testToPatternSimple();
201 void testExceptionOnInvalid();
204 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
209 class NumberParserTest
: public IntlTest
{
213 void testSeriesMatcher();
214 void testCombinedCurrencyMatcher();
215 void testAffixPatternMatcher();
216 void testGroupingDisabled();
217 void testCaseFolding();
218 void test20360_BidiOverflow();
219 void testInfiniteRecursion();
221 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
224 class NumberSkeletonTest
: public IntlTest
{
227 void invalidTokens();
228 void unknownTokens();
229 void unexpectedTokens();
230 void duplicateValues();
231 void stemsRequiringOption();
232 void defaultTokens();
233 void flexibleSeparators();
235 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
238 void expectedErrorSkeleton(const char16_t** cases
, int32_t casesLen
);
241 class NumberRangeFormatterTest
: public IntlTestWithFieldPosition
{
243 NumberRangeFormatterTest();
244 NumberRangeFormatterTest(UErrorCode
&status
);
250 void testDifferentFormatters();
252 void testFieldPositions();
256 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
264 MeasureUnit KILOMETER
;
265 MeasureUnit FAHRENHEIT
;
268 void assertFormatRange(
269 const char16_t* message
,
270 const UnlocalizedNumberRangeFormatter
& f
,
272 const char16_t* expected_10_50
,
273 const char16_t* expected_49_51
,
274 const char16_t* expected_50_50
,
275 const char16_t* expected_00_30
,
276 const char16_t* expected_00_00
,
277 const char16_t* expected_30_3K
,
278 const char16_t* expected_30K_50K
,
279 const char16_t* expected_49K_51K
,
280 const char16_t* expected_50K_50K
,
281 const char16_t* expected_50K_50M
);
283 FormattedNumberRange
assertFormattedRangeEquals(
284 const char16_t* message
,
285 const LocalizedNumberRangeFormatter
& l
,
288 const char16_t* expected
);
291 class NumberPermutationTest
: public IntlTest
{
293 void testPermutations();
295 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
299 // NOTE: This macro is identical to the one in itformat.cpp
300 #define TESTCLASS(id, TestClass) \
304 logln(#TestClass " test---"); \
305 logln((UnicodeString)""); \
307 callTest(test, par); \
311 class NumberTest
: public IntlTest
{
313 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0) {
315 logln("TestSuite NumberTest: ");
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, DoubleConversionTest
);
326 TESTCLASS(7, NumberParserTest
);
327 TESTCLASS(8, NumberSkeletonTest
);
328 TESTCLASS(9, NumberRangeFormatterTest
);
329 TESTCLASS(10, NumberPermutationTest
);
330 default: name
= ""; break; // needed to end loop
335 #endif /* #if !UCONFIG_NO_FORMATTING */