2 *******************************************************************************
3 * Copyright (C) 2014-2015, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 * File PLURALMAPTEST.CPP
9 ********************************************************************************
11 #include "unicode/unistr.h"
14 #include "pluralmap.h"
16 class PluralMapForPluralMapTest
: public PluralMap
<UnicodeString
> {
18 UBool
operator==(const PluralMapForPluralMapTest
&other
) {
19 return equals(other
, strEqual
);
22 static UBool
strEqual(const UnicodeString
&lhs
, const UnicodeString
&rhs
) {
27 class PluralMapTest
: public IntlTest
{
31 void TestToCategory();
32 void TestGetCategoryName();
36 void TestCopyAndAssign();
37 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
39 PluralMapBase::Category v
,
40 const UnicodeString
&value
,
41 PluralMapForPluralMapTest
&map
);
45 void PluralMapTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
47 TESTCASE_AUTO(TestToCategory
);
48 TESTCASE_AUTO(TestGetCategoryName
);
49 TESTCASE_AUTO(TestGet
);
50 TESTCASE_AUTO(TestIterate
);
51 TESTCASE_AUTO(TestEqual
);
52 TESTCASE_AUTO(TestCopyAndAssign
);
56 void PluralMapTest::TestToCategory() {
57 assertEquals("", PluralMapBase::OTHER
, PluralMapBase::toCategory("other"));
58 assertEquals("", PluralMapBase::ZERO
, PluralMapBase::toCategory("zero"));
59 assertEquals("", PluralMapBase::ONE
, PluralMapBase::toCategory("one"));
60 assertEquals("", PluralMapBase::TWO
, PluralMapBase::toCategory("two"));
61 assertEquals("", PluralMapBase::FEW
, PluralMapBase::toCategory("few"));
62 assertEquals("", PluralMapBase::MANY
, PluralMapBase::toCategory("many"));
63 assertEquals("", PluralMapBase::NONE
, PluralMapBase::toCategory("Many"));
67 PluralMapBase::toCategory(UnicodeString("few")));
71 PluralMapBase::toCategory(UnicodeString("many")));
75 PluralMapBase::toCategory(UnicodeString("Many")));
78 void PluralMapTest::TestGetCategoryName() {
79 assertTrue("", PluralMapBase::getCategoryName(PluralMapBase::NONE
) == NULL
);
80 assertTrue("", PluralMapBase::getCategoryName(PluralMapBase::CATEGORY_COUNT
) == NULL
);
81 assertEquals("", "other", PluralMapBase::getCategoryName(PluralMapBase::OTHER
));
82 assertEquals("", "zero", PluralMapBase::getCategoryName(PluralMapBase::ZERO
));
83 assertEquals("", "one", PluralMapBase::getCategoryName(PluralMapBase::ONE
));
84 assertEquals("", "two", PluralMapBase::getCategoryName(PluralMapBase::TWO
));
85 assertEquals("", "few", PluralMapBase::getCategoryName(PluralMapBase::FEW
));
86 assertEquals("", "many", PluralMapBase::getCategoryName(PluralMapBase::MANY
));
89 void PluralMapTest::TestGet() {
90 PluralMapForPluralMapTest map
;
91 addVariant(PluralMapBase::OTHER
, "pickles", map
);
92 addVariant(PluralMapBase::ONE
, "pickle", map
);
93 addVariant(PluralMapBase::FEW
, "picklefew", map
);
94 assertEquals("", "pickles", map
.get(PluralMapBase::OTHER
));
95 assertEquals("", "pickle", map
.get(PluralMapBase::ONE
));
96 assertEquals("", "picklefew", map
.get(PluralMapBase::FEW
));
97 assertEquals("", "pickles", map
.get(PluralMapBase::MANY
));
98 assertEquals("", "pickles", map
.get(PluralMapBase::NONE
));
99 assertEquals("", "pickles", map
.get(PluralMapBase::CATEGORY_COUNT
));
100 assertEquals("", "picklefew", map
.get("few"));
101 assertEquals("", "pickles", map
.get("many"));
102 assertEquals("", "pickles", map
.get("somebadform"));
103 assertEquals("", "pickle", map
.get(UnicodeString("one")));
104 assertEquals("", "pickles", map
.get(UnicodeString("many")));
105 assertEquals("", "pickles", map
.get(UnicodeString("somebadform")));
106 assertEquals("", "pickles", map
.getOther());
109 void PluralMapTest::TestIterate() {
110 PluralMapForPluralMapTest map
;
111 addVariant(PluralMapBase::OTHER
, "pickles", map
);
112 addVariant(PluralMapBase::ONE
, "pickle", map
);
113 addVariant(PluralMapBase::FEW
, "pickleops", map
);
114 addVariant(PluralMapBase::FEW
, "picklefew", map
);
115 PluralMapBase::Category index
= PluralMapBase::NONE
;
116 const UnicodeString
*current
= map
.next(index
);
117 assertEquals("", "pickles", *current
);
118 assertEquals("", PluralMapBase::OTHER
, index
);
119 current
= map
.next(index
);
120 assertEquals("", "pickle", *current
);
121 assertEquals("", PluralMapBase::ONE
, index
);
122 current
= map
.next(index
);
123 assertEquals("", "picklefew", *current
);
124 assertEquals("", PluralMapBase::FEW
, index
);
125 current
= map
.next(index
);
126 assertEquals("", PluralMapBase::CATEGORY_COUNT
, index
);
127 assertTrue("", current
== NULL
);
129 PluralMapForPluralMapTest map2
;
130 index
= PluralMapBase::NONE
;
131 current
= map2
.next(index
);
132 assertEquals("", "", *current
);
133 assertEquals("", PluralMapBase::OTHER
, index
);
134 current
= map2
.next(index
);
135 assertEquals("", PluralMapBase::CATEGORY_COUNT
, index
);
136 assertTrue("", current
== NULL
);
139 void PluralMapTest::TestEqual() {
140 PluralMapForPluralMapTest control
;
141 addVariant(PluralMapBase::OTHER
, "pickles", control
);
142 addVariant(PluralMapBase::ONE
, "pickle", control
);
143 addVariant(PluralMapBase::FEW
, "picklefew", control
);
146 PluralMapForPluralMapTest map
;
147 addVariant(PluralMapBase::FEW
, "picklefew", map
);
148 addVariant(PluralMapBase::OTHER
, "pickles", map
);
149 addVariant(PluralMapBase::ONE
, "pickle", map
);
150 assertTrue("", control
== map
);
151 addVariant(PluralMapBase::ONE
, "pickl", map
);
152 assertFalse("", control
== map
);
155 PluralMapForPluralMapTest map
;
156 addVariant(PluralMapBase::MANY
, "picklemany", map
);
157 addVariant(PluralMapBase::OTHER
, "pickles", map
);
158 addVariant(PluralMapBase::ONE
, "pickle", map
);
159 assertFalse("", control
== map
);
163 void PluralMapTest::TestCopyAndAssign() {
164 PluralMapForPluralMapTest control
;
165 addVariant(PluralMapBase::OTHER
, "pickles", control
);
166 addVariant(PluralMapBase::ONE
, "pickle", control
);
167 addVariant(PluralMapBase::FEW
, "picklefew", control
);
169 PluralMapForPluralMapTest
*rhs
= new PluralMapForPluralMapTest();
171 errln("Memory allocation error.");
174 addVariant(PluralMapBase::OTHER
, "pickles", *rhs
);
175 addVariant(PluralMapBase::ONE
, "pickle", *rhs
);
176 addVariant(PluralMapBase::FEW
, "picklefew", *rhs
);
177 PluralMapForPluralMapTest
lhs(*rhs
);
179 assertTrue("", lhs
== control
);
182 PluralMapForPluralMapTest
*rhs
= new PluralMapForPluralMapTest();
184 errln("Memory allocation error.");
187 addVariant(PluralMapBase::OTHER
, "pickles", *rhs
);
188 addVariant(PluralMapBase::ONE
, "pickle", *rhs
);
189 addVariant(PluralMapBase::FEW
, "picklefew", *rhs
);
190 PluralMapForPluralMapTest lhs
;
191 addVariant(PluralMapBase::OTHER
, "pickles", lhs
);
192 addVariant(PluralMapBase::TWO
, "pickletwo", lhs
);
193 addVariant(PluralMapBase::MANY
, "picklemany", lhs
);
194 addVariant(PluralMapBase::FEW
, "picklefew", lhs
);
197 assertTrue("", lhs
== control
);
204 void PluralMapTest::addVariant(
205 PluralMapBase::Category v
,
206 const UnicodeString
&value
,
207 PluralMapForPluralMapTest
&map
) {
208 UErrorCode status
= U_ZERO_ERROR
;
209 UnicodeString
*current
= map
.getMutable(v
, status
);
210 if (!assertSuccess("", status
)) {
216 extern IntlTest
*createPluralMapTest() {
217 return new PluralMapTest();