1 /*
2 *******************************************************************************
3 * Copyright (C) 2014-2015, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 *
7 * File PLURALMAPTEST.CPP
8 *
9 ********************************************************************************
10 */
11 #include "unicode/unistr.h"
12
13 #include "intltest.h"
14 #include "pluralmap.h"
15
16 class PluralMapForPluralMapTest : public PluralMap<UnicodeString> {
17 public:
operator ==(const PluralMapForPluralMapTest & other)18 UBool operator==(const PluralMapForPluralMapTest &other) {
19 return equals(other, strEqual);
20 }
21 private:
strEqual(const UnicodeString & lhs,const UnicodeString & rhs)22 static UBool strEqual(const UnicodeString &lhs, const UnicodeString &rhs) {
23 return lhs == rhs;
24 }
25 };
26
27 class PluralMapTest : public IntlTest {
28 public:
PluralMapTest()29 PluralMapTest() {
30 }
31 void TestToCategory();
32 void TestGetCategoryName();
33 void TestGet();
34 void TestIterate();
35 void TestEqual();
36 void TestCopyAndAssign();
37 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
38 void addVariant(
39 PluralMapBase::Category v,
40 const UnicodeString &value,
41 PluralMapForPluralMapTest &map);
42 private:
43 };
44
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)45 void PluralMapTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
46 TESTCASE_AUTO_BEGIN;
47 TESTCASE_AUTO(TestToCategory);
48 TESTCASE_AUTO(TestGetCategoryName);
49 TESTCASE_AUTO(TestGet);
50 TESTCASE_AUTO(TestIterate);
51 TESTCASE_AUTO(TestEqual);
52 TESTCASE_AUTO(TestCopyAndAssign);
53 TESTCASE_AUTO_END;
54 }
55
TestToCategory()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"));
64 assertEquals(
65 "",
66 PluralMapBase::FEW,
67 PluralMapBase::toCategory(UnicodeString("few")));
68 assertEquals(
69 "",
70 PluralMapBase::MANY,
71 PluralMapBase::toCategory(UnicodeString("many")));
72 assertEquals(
73 "",
74 PluralMapBase::NONE,
75 PluralMapBase::toCategory(UnicodeString("Many")));
76 }
77
TestGetCategoryName()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));
87 }
88
TestGet()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());
107 }
108
TestIterate()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);
128
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);
137 }
138
TestEqual()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);
144
145 {
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);
153 }
154 {
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);
160 }
161 }
162
TestCopyAndAssign()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);
168 {
169 PluralMapForPluralMapTest *rhs = new PluralMapForPluralMapTest();
170 if (rhs == NULL) {
171 errln("Memory allocation error.");
172 return;
173 }
174 addVariant(PluralMapBase::OTHER, "pickles", *rhs);
175 addVariant(PluralMapBase::ONE, "pickle", *rhs);
176 addVariant(PluralMapBase::FEW, "picklefew", *rhs);
177 PluralMapForPluralMapTest lhs(*rhs);
178 delete rhs;
179 assertTrue("", lhs == control);
180 }
181 {
182 PluralMapForPluralMapTest *rhs = new PluralMapForPluralMapTest();
183 if (rhs == NULL) {
184 errln("Memory allocation error.");
185 return;
186 }
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);
195 lhs = *rhs;
196 delete rhs;
197 assertTrue("", lhs == control);
198 }
199
200 }
201
202
203
addVariant(PluralMapBase::Category v,const UnicodeString & value,PluralMapForPluralMapTest & map)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)) {
211 return;
212 }
213 (*current) = value;
214 }
215
createPluralMapTest()216 extern IntlTest *createPluralMapTest() {
217 return new PluralMapTest();
218 }
219