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"
21 class QuantityFormatterTest
: public IntlTest
{
23 QuantityFormatterTest() {
26 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
30 void QuantityFormatterTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
32 TESTCASE_AUTO(TestBasic
);
36 void QuantityFormatterTest::TestBasic() {
37 UErrorCode status
= U_ZERO_ERROR
;
38 #if !UCONFIG_NO_FORMATTING
39 QuantityFormatter fmt
;
42 fmt
.addIfAbsent("a bad variant", "{0} pounds", status
));
43 assertEquals("adding bad variant status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR
, status
);
44 status
= U_ZERO_ERROR
;
47 fmt
.addIfAbsent("other", "{0} {1} too many placeholders", status
));
48 assertEquals("adding bad pattern status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR
, status
);
49 status
= U_ZERO_ERROR
;
50 assertFalse("isValid with no patterns", fmt
.isValid());
52 "Adding good pattern with no placeholders",
53 fmt
.addIfAbsent("zero", "no placeholder", status
));
55 "Adding good pattern",
56 fmt
.addIfAbsent("other", "{0} pounds", status
));
57 assertTrue("isValid with other", fmt
.isValid());
59 "Adding good pattern",
60 fmt
.addIfAbsent("one", "{0} pound", status
));
64 fmt
.getByVariant("bad variant")->getTextWithNoArguments(),
68 fmt
.getByVariant("other")->getTextWithNoArguments(),
72 fmt
.getByVariant("one")->getTextWithNoArguments(),
76 fmt
.getByVariant("few")->getTextWithNoArguments(),
79 // Test copy constructor
81 QuantityFormatter
copied(fmt
);
83 "copied getByVariant",
84 copied
.getByVariant("other")->getTextWithNoArguments(),
87 "copied getByVariant",
88 copied
.getByVariant("one")->getTextWithNoArguments(),
91 "copied getByVariant",
92 copied
.getByVariant("few")->getTextWithNoArguments(),
98 QuantityFormatter assigned
;
101 "assigned getByVariant",
102 assigned
.getByVariant("other")->getTextWithNoArguments(),
105 "assigned getByVariant",
106 assigned
.getByVariant("one")->getTextWithNoArguments(),
109 "assigned getByVariant",
110 assigned
.getByVariant("few")->getTextWithNoArguments(),
116 LocalPointer
<NumberFormat
> numfmt(
117 NumberFormat::createInstance(Locale::getEnglish(), status
));
118 LocalPointer
<PluralRules
> plurrule(
119 PluralRules::forLocale("en", status
));
120 FieldPosition
pos(FieldPosition::DONT_CARE
);
121 UnicodeString appendTo
;
124 UnicodeString("1 pound"),
135 UnicodeString("2 pounds"),
145 assertFalse("isValid after reset", fmt
.isValid());
147 assertSuccess("", status
);
150 extern IntlTest
*createQuantityFormatterTest() {
151 return new QuantityFormatterTest();