1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2014-2015, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
9 * File PLURALMAPTEST.CPP
11 ********************************************************************************
13 #include "unicode/unistr.h"
16 #include "pluralmap.h"
18 class PluralMapForPluralMapTest
: public PluralMap
<UnicodeString
> {
20 UBool
operator==(const PluralMapForPluralMapTest
&other
) {
21 return equals(other
, strEqual
);
24 static UBool
strEqual(const UnicodeString
&lhs
, const UnicodeString
&rhs
) {
29 class PluralMapTest
: public IntlTest
{
33 void TestToCategory();
34 void TestGetCategoryName();
38 void TestCopyAndAssign();
39 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
41 PluralMapBase::Category v
,
42 const UnicodeString
&value
,
43 PluralMapForPluralMapTest
&map
);
47 void PluralMapTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
49 TESTCASE_AUTO(TestToCategory
);
50 TESTCASE_AUTO(TestGetCategoryName
);
51 TESTCASE_AUTO(TestGet
);
52 TESTCASE_AUTO(TestIterate
);
53 TESTCASE_AUTO(TestEqual
);
54 TESTCASE_AUTO(TestCopyAndAssign
);
58 void PluralMapTest::TestToCategory() {
59 assertEquals("", (int32_t)PluralMapBase::OTHER
, PluralMapBase::toCategory("other"));
60 assertEquals("", (int32_t)PluralMapBase::ZERO
, PluralMapBase::toCategory("zero"));
61 assertEquals("", (int32_t)PluralMapBase::ONE
, PluralMapBase::toCategory("one"));
62 assertEquals("", (int32_t)PluralMapBase::TWO
, PluralMapBase::toCategory("two"));
63 assertEquals("", (int32_t)PluralMapBase::FEW
, PluralMapBase::toCategory("few"));
64 assertEquals("", (int32_t)PluralMapBase::MANY
, PluralMapBase::toCategory("many"));
65 assertEquals("", (int32_t)PluralMapBase::NONE
, PluralMapBase::toCategory("Many"));
68 (int32_t)PluralMapBase::FEW
,
69 PluralMapBase::toCategory(UnicodeString("few")));
72 (int32_t)PluralMapBase::MANY
,
73 PluralMapBase::toCategory(UnicodeString("many")));
76 (int32_t)PluralMapBase::NONE
,
77 PluralMapBase::toCategory(UnicodeString("Many")));
80 void PluralMapTest::TestGetCategoryName() {
81 assertTrue("", PluralMapBase::getCategoryName(PluralMapBase::NONE
) == NULL
);
82 assertTrue("", PluralMapBase::getCategoryName(PluralMapBase::CATEGORY_COUNT
) == NULL
);
83 assertEquals("", "other", PluralMapBase::getCategoryName(PluralMapBase::OTHER
));
84 assertEquals("", "zero", PluralMapBase::getCategoryName(PluralMapBase::ZERO
));
85 assertEquals("", "one", PluralMapBase::getCategoryName(PluralMapBase::ONE
));
86 assertEquals("", "two", PluralMapBase::getCategoryName(PluralMapBase::TWO
));
87 assertEquals("", "few", PluralMapBase::getCategoryName(PluralMapBase::FEW
));
88 assertEquals("", "many", PluralMapBase::getCategoryName(PluralMapBase::MANY
));
91 void PluralMapTest::TestGet() {
92 PluralMapForPluralMapTest map
;
93 addVariant(PluralMapBase::OTHER
, "pickles", map
);
94 addVariant(PluralMapBase::ONE
, "pickle", map
);
95 addVariant(PluralMapBase::FEW
, "picklefew", map
);
96 assertEquals("", "pickles", map
.get(PluralMapBase::OTHER
));
97 assertEquals("", "pickle", map
.get(PluralMapBase::ONE
));
98 assertEquals("", "picklefew", map
.get(PluralMapBase::FEW
));
99 assertEquals("", "pickles", map
.get(PluralMapBase::MANY
));
100 assertEquals("", "pickles", map
.get(PluralMapBase::NONE
));
101 assertEquals("", "pickles", map
.get(PluralMapBase::CATEGORY_COUNT
));
102 assertEquals("", "picklefew", map
.get("few"));
103 assertEquals("", "pickles", map
.get("many"));
104 assertEquals("", "pickles", map
.get("somebadform"));
105 assertEquals("", "pickle", map
.get(UnicodeString("one")));
106 assertEquals("", "pickles", map
.get(UnicodeString("many")));
107 assertEquals("", "pickles", map
.get(UnicodeString("somebadform")));
108 assertEquals("", "pickles", map
.getOther());
111 void PluralMapTest::TestIterate() {
112 PluralMapForPluralMapTest map
;
113 addVariant(PluralMapBase::OTHER
, "pickles", map
);
114 addVariant(PluralMapBase::ONE
, "pickle", map
);
115 addVariant(PluralMapBase::FEW
, "pickleops", map
);
116 addVariant(PluralMapBase::FEW
, "picklefew", map
);
117 PluralMapBase::Category index
= PluralMapBase::NONE
;
118 const UnicodeString
*current
= map
.next(index
);
119 assertEquals("", "pickles", *current
);
120 assertEquals("", (int32_t)PluralMapBase::OTHER
, index
);
121 current
= map
.next(index
);
122 assertEquals("", "pickle", *current
);
123 assertEquals("", (int32_t)PluralMapBase::ONE
, index
);
124 current
= map
.next(index
);
125 assertEquals("", "picklefew", *current
);
126 assertEquals("", (int32_t)PluralMapBase::FEW
, index
);
127 current
= map
.next(index
);
128 assertEquals("", (int32_t)PluralMapBase::CATEGORY_COUNT
, index
);
129 assertTrue("", current
== NULL
);
131 PluralMapForPluralMapTest map2
;
132 index
= PluralMapBase::NONE
;
133 current
= map2
.next(index
);
134 assertEquals("", "", *current
);
135 assertEquals("", (int32_t)PluralMapBase::OTHER
, index
);
136 current
= map2
.next(index
);
137 assertEquals("", (int32_t)PluralMapBase::CATEGORY_COUNT
, index
);
138 assertTrue("", current
== NULL
);
141 void PluralMapTest::TestEqual() {
142 PluralMapForPluralMapTest control
;
143 addVariant(PluralMapBase::OTHER
, "pickles", control
);
144 addVariant(PluralMapBase::ONE
, "pickle", control
);
145 addVariant(PluralMapBase::FEW
, "picklefew", control
);
148 PluralMapForPluralMapTest map
;
149 addVariant(PluralMapBase::FEW
, "picklefew", map
);
150 addVariant(PluralMapBase::OTHER
, "pickles", map
);
151 addVariant(PluralMapBase::ONE
, "pickle", map
);
152 assertTrue("", control
== map
);
153 addVariant(PluralMapBase::ONE
, "pickl", map
);
154 assertFalse("", control
== map
);
157 PluralMapForPluralMapTest map
;
158 addVariant(PluralMapBase::MANY
, "picklemany", map
);
159 addVariant(PluralMapBase::OTHER
, "pickles", map
);
160 addVariant(PluralMapBase::ONE
, "pickle", map
);
161 assertFalse("", control
== map
);
165 void PluralMapTest::TestCopyAndAssign() {
166 PluralMapForPluralMapTest control
;
167 addVariant(PluralMapBase::OTHER
, "pickles", control
);
168 addVariant(PluralMapBase::ONE
, "pickle", control
);
169 addVariant(PluralMapBase::FEW
, "picklefew", control
);
171 PluralMapForPluralMapTest
*rhs
= new PluralMapForPluralMapTest();
173 errln("Memory allocation error.");
176 addVariant(PluralMapBase::OTHER
, "pickles", *rhs
);
177 addVariant(PluralMapBase::ONE
, "pickle", *rhs
);
178 addVariant(PluralMapBase::FEW
, "picklefew", *rhs
);
179 PluralMapForPluralMapTest
lhs(*rhs
);
181 assertTrue("", lhs
== control
);
184 PluralMapForPluralMapTest
*rhs
= new PluralMapForPluralMapTest();
186 errln("Memory allocation error.");
189 addVariant(PluralMapBase::OTHER
, "pickles", *rhs
);
190 addVariant(PluralMapBase::ONE
, "pickle", *rhs
);
191 addVariant(PluralMapBase::FEW
, "picklefew", *rhs
);
192 PluralMapForPluralMapTest lhs
;
193 addVariant(PluralMapBase::OTHER
, "pickles", lhs
);
194 addVariant(PluralMapBase::TWO
, "pickletwo", lhs
);
195 addVariant(PluralMapBase::MANY
, "picklemany", lhs
);
196 addVariant(PluralMapBase::FEW
, "picklefew", lhs
);
199 assertTrue("", lhs
== control
);
206 void PluralMapTest::addVariant(
207 PluralMapBase::Category v
,
208 const UnicodeString
&value
,
209 PluralMapForPluralMapTest
&map
) {
210 UErrorCode status
= U_ZERO_ERROR
;
211 UnicodeString
*current
= map
.getMutable(v
, status
);
212 if (!assertSuccess("", status
)) {
218 extern IntlTest
*createPluralMapTest() {
219 return new PluralMapTest();