]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/pluralmaptest.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / intltest / pluralmaptest.cpp
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-2015, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
8 *
9 * File PLURALMAPTEST.CPP
10 *
11 ********************************************************************************
12 */
13 #include "unicode/unistr.h"
14
15 #include "intltest.h"
16 #include "pluralmap.h"
17
18 class PluralMapForPluralMapTest : public PluralMap<UnicodeString> {
19 public:
20 UBool operator==(const PluralMapForPluralMapTest &other) {
21 return equals(other, strEqual);
22 }
23 private:
24 static UBool strEqual(const UnicodeString &lhs, const UnicodeString &rhs) {
25 return lhs == rhs;
26 }
27 };
28
29 class PluralMapTest : public IntlTest {
30 public:
31 PluralMapTest() {
32 }
33 void TestToCategory();
34 void TestGetCategoryName();
35 void TestGet();
36 void TestIterate();
37 void TestEqual();
38 void TestCopyAndAssign();
39 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
40 void addVariant(
41 PluralMapBase::Category v,
42 const UnicodeString &value,
43 PluralMapForPluralMapTest &map);
44 private:
45 };
46
47 void PluralMapTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
48 TESTCASE_AUTO_BEGIN;
49 TESTCASE_AUTO(TestToCategory);
50 TESTCASE_AUTO(TestGetCategoryName);
51 TESTCASE_AUTO(TestGet);
52 TESTCASE_AUTO(TestIterate);
53 TESTCASE_AUTO(TestEqual);
54 TESTCASE_AUTO(TestCopyAndAssign);
55 TESTCASE_AUTO_END;
56 }
57
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"));
66 assertEquals(
67 "",
68 (int32_t)PluralMapBase::FEW,
69 PluralMapBase::toCategory(UnicodeString("few")));
70 assertEquals(
71 "",
72 (int32_t)PluralMapBase::MANY,
73 PluralMapBase::toCategory(UnicodeString("many")));
74 assertEquals(
75 "",
76 (int32_t)PluralMapBase::NONE,
77 PluralMapBase::toCategory(UnicodeString("Many")));
78 }
79
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));
89 }
90
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());
109 }
110
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);
130
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);
139 }
140
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);
146
147 {
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);
155 }
156 {
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);
162 }
163 }
164
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);
170 {
171 PluralMapForPluralMapTest *rhs = new PluralMapForPluralMapTest();
172 if (rhs == NULL) {
173 errln("Memory allocation error.");
174 return;
175 }
176 addVariant(PluralMapBase::OTHER, "pickles", *rhs);
177 addVariant(PluralMapBase::ONE, "pickle", *rhs);
178 addVariant(PluralMapBase::FEW, "picklefew", *rhs);
179 PluralMapForPluralMapTest lhs(*rhs);
180 delete rhs;
181 assertTrue("", lhs == control);
182 }
183 {
184 PluralMapForPluralMapTest *rhs = new PluralMapForPluralMapTest();
185 if (rhs == NULL) {
186 errln("Memory allocation error.");
187 return;
188 }
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);
197 lhs = *rhs;
198 delete rhs;
199 assertTrue("", lhs == control);
200 }
201
202 }
203
204
205
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)) {
213 return;
214 }
215 (*current) = value;
216 }
217
218 extern IntlTest *createPluralMapTest() {
219 return new PluralMapTest();
220 }