1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2014-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 *
9 * File QUANTITYFORMATTERTEST.CPP
10 *
11 ********************************************************************************
12 */
13 #include "cstring.h"
14 #include "intltest.h"
15 #include "quantityformatter.h"
16 #include "unicode/simpleformatter.h"
17 #include "unicode/numfmt.h"
18 #include "unicode/plurrule.h"
19
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__); \
23 return; \
24 } \
25 } UPRV_BLOCK_MACRO_END
26
27 class QuantityFormatterTest : public IntlTest {
28 public:
QuantityFormatterTest()29 QuantityFormatterTest() {
30 }
31 void TestBasic();
32 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
33 private:
34 };
35
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)36 void QuantityFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
37 TESTCASE_AUTO_BEGIN;
38 TESTCASE_AUTO(TestBasic);
39 TESTCASE_AUTO_END;
40 }
41
TestBasic()42 void QuantityFormatterTest::TestBasic() {
43 UErrorCode status = U_ZERO_ERROR;
44 #if !UCONFIG_NO_FORMATTING
45 QuantityFormatter fmt;
46 assertFalse(
47 "adding bad variant",
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;
51 assertFalse(
52 "Adding bad pattern",
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());
57 assertTrue(
58 "Adding good pattern with no placeholders",
59 fmt.addIfAbsent("zero", "no placeholder", status));
60 assertTrue(
61 "Adding good pattern",
62 fmt.addIfAbsent("other", "{0} pounds", status));
63 assertTrue("isValid with other", fmt.isValid());
64 assertTrue(
65 "Adding good pattern",
66 fmt.addIfAbsent("one", "{0} pound", status));
67
68 assertEquals(
69 "getByVariant",
70 fmt.getByVariant("bad variant")->getTextWithNoArguments(),
71 " pounds");
72 assertEquals(
73 "getByVariant",
74 fmt.getByVariant("other")->getTextWithNoArguments(),
75 " pounds");
76 assertEquals(
77 "getByVariant",
78 fmt.getByVariant("one")->getTextWithNoArguments(),
79 " pound");
80 assertEquals(
81 "getByVariant",
82 fmt.getByVariant("few")->getTextWithNoArguments(),
83 " pounds");
84
85 // Test copy constructor
86 {
87 QuantityFormatter copied(fmt);
88 assertEquals(
89 "copied getByVariant",
90 copied.getByVariant("other")->getTextWithNoArguments(),
91 " pounds");
92 assertEquals(
93 "copied getByVariant",
94 copied.getByVariant("one")->getTextWithNoArguments(),
95 " pound");
96 assertEquals(
97 "copied getByVariant",
98 copied.getByVariant("few")->getTextWithNoArguments(),
99 " pounds");
100 }
101
102 // Test assignment
103 {
104 QuantityFormatter assigned;
105 assigned = fmt;
106 assertEquals(
107 "assigned getByVariant",
108 assigned.getByVariant("other")->getTextWithNoArguments(),
109 " pounds");
110 assertEquals(
111 "assigned getByVariant",
112 assigned.getByVariant("one")->getTextWithNoArguments(),
113 " pound");
114 assertEquals(
115 "assigned getByVariant",
116 assigned.getByVariant("few")->getTextWithNoArguments(),
117 " pounds");
118 }
119
120 // Test format.
121 {
122 LocalPointer<NumberFormat> numfmt(
123 NumberFormat::createInstance(Locale::getEnglish(), status));
124 LocalPointer<PluralRules> plurrule(
125 PluralRules::forLocale("en", status));
126 ASSERT_OK(status);
127 FieldPosition pos(FieldPosition::DONT_CARE);
128 UnicodeString appendTo;
129 assertEquals(
130 "format singular",
131 UnicodeString("1 pound"),
132 fmt.format(
133 1.0,
134 *numfmt,
135 *plurrule,
136 appendTo,
137 pos,
138 status), TRUE);
139 appendTo.remove();
140 assertEquals(
141 "format plural",
142 UnicodeString("2 pounds"),
143 fmt.format(
144 2.0,
145 *numfmt,
146 *plurrule,
147 appendTo,
148 pos,
149 status), TRUE);
150 }
151 fmt.reset();
152 assertFalse("isValid after reset", fmt.isValid());
153 #endif
154 assertSuccess("", status);
155 }
156
createQuantityFormatterTest()157 extern IntlTest *createQuantityFormatterTest() {
158 return new QuantityFormatterTest();
159 }
160