1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2014-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
9 * File QUANTITYFORMATTERTEST.CPP
11 ********************************************************************************
15 #include "quantityformatter.h"
16 #include "unicode/simpleformatter.h"
17 #include "unicode/numfmt.h"
18 #include "unicode/plurrule.h"
20 #define ASSERT_OK(status) UPRV_BLOCK_MACRO_BEGIN { \
21 if(U_FAILURE(status)) { \
22 errcheckln(status, #status " = %s @ %s:%d", u_errorName(status), __FILE__, __LINE__); \
25 } UPRV_BLOCK_MACRO_END
27 class QuantityFormatterTest
: public IntlTest
{
29 QuantityFormatterTest() {
32 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
36 void QuantityFormatterTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
38 TESTCASE_AUTO(TestBasic
);
42 void QuantityFormatterTest::TestBasic() {
43 UErrorCode status
= U_ZERO_ERROR
;
44 #if !UCONFIG_NO_FORMATTING
45 QuantityFormatter fmt
;
48 fmt
.addIfAbsent("a bad variant", "{0} pounds", status
));
49 assertEquals("adding bad variant status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR
, status
);
50 status
= U_ZERO_ERROR
;
53 fmt
.addIfAbsent("other", "{0} {1} too many placeholders", status
));
54 assertEquals("adding bad pattern status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR
, status
);
55 status
= U_ZERO_ERROR
;
56 assertFalse("isValid with no patterns", fmt
.isValid());
58 "Adding good pattern with no placeholders",
59 fmt
.addIfAbsent("zero", "no placeholder", status
));
61 "Adding good pattern",
62 fmt
.addIfAbsent("other", "{0} pounds", status
));
63 assertTrue("isValid with other", fmt
.isValid());
65 "Adding good pattern",
66 fmt
.addIfAbsent("one", "{0} pound", status
));
70 fmt
.getByVariant("bad variant")->getTextWithNoArguments(),
74 fmt
.getByVariant("other")->getTextWithNoArguments(),
78 fmt
.getByVariant("one")->getTextWithNoArguments(),
82 fmt
.getByVariant("few")->getTextWithNoArguments(),
85 // Test copy constructor
87 QuantityFormatter
copied(fmt
);
89 "copied getByVariant",
90 copied
.getByVariant("other")->getTextWithNoArguments(),
93 "copied getByVariant",
94 copied
.getByVariant("one")->getTextWithNoArguments(),
97 "copied getByVariant",
98 copied
.getByVariant("few")->getTextWithNoArguments(),
104 QuantityFormatter assigned
;
107 "assigned getByVariant",
108 assigned
.getByVariant("other")->getTextWithNoArguments(),
111 "assigned getByVariant",
112 assigned
.getByVariant("one")->getTextWithNoArguments(),
115 "assigned getByVariant",
116 assigned
.getByVariant("few")->getTextWithNoArguments(),
122 LocalPointer
<NumberFormat
> numfmt(
123 NumberFormat::createInstance(Locale::getEnglish(), status
));
124 LocalPointer
<PluralRules
> plurrule(
125 PluralRules::forLocale("en", status
));
127 FieldPosition
pos(FieldPosition::DONT_CARE
);
128 UnicodeString appendTo
;
131 UnicodeString("1 pound"),
142 UnicodeString("2 pounds"),
152 assertFalse("isValid after reset", fmt
.isValid());
154 assertSuccess("", status
);
157 extern IntlTest
*createQuantityFormatterTest() {
158 return new QuantityFormatterTest();