2 *******************************************************************************
4 * Copyright (C) 2012-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: listformattertest.cpp
10 * tab size: 8 (not used)
13 * created on: 2012aug27
14 * created by: Umesh P. Nair
17 #include "listformattertest.h"
20 ListFormatterTest::ListFormatterTest() :
21 prefix("Prefix: ", -1, US_INV
),
22 one("Alice", -1, US_INV
), two("Bob", -1, US_INV
),
23 three("Charlie", -1, US_INV
), four("Delta", -1, US_INV
) {
26 void ListFormatterTest::CheckFormatting(const ListFormatter
* formatter
, UnicodeString data
[], int32_t dataSize
,
27 const UnicodeString
& expected_result
) {
28 UnicodeString
actualResult(prefix
);
29 UErrorCode errorCode
= U_ZERO_ERROR
;
30 formatter
->format(data
, dataSize
, actualResult
, errorCode
);
31 UnicodeString expectedStringWithPrefix
= prefix
+ expected_result
;
32 if (expectedStringWithPrefix
!= actualResult
) {
33 errln(UnicodeString("Expected: |") + expectedStringWithPrefix
+ "|, Actual: |" + actualResult
+ "|");
37 void ListFormatterTest::CheckFourCases(const char* locale_string
, UnicodeString one
, UnicodeString two
,
38 UnicodeString three
, UnicodeString four
, UnicodeString results
[4]) {
39 UErrorCode errorCode
= U_ZERO_ERROR
;
40 LocalPointer
<ListFormatter
> formatter(ListFormatter::createInstance(Locale(locale_string
), errorCode
));
41 if (U_FAILURE(errorCode
)) {
42 dataerrln("ListFormatter::createInstance(Locale(\"%s\"), errorCode) failed in CheckFourCases: %s", locale_string
, u_errorName(errorCode
));
45 UnicodeString input1
[] = {one
};
46 CheckFormatting(formatter
.getAlias(), input1
, 1, results
[0]);
48 UnicodeString input2
[] = {one
, two
};
49 CheckFormatting(formatter
.getAlias(), input2
, 2, results
[1]);
51 UnicodeString input3
[] = {one
, two
, three
};
52 CheckFormatting(formatter
.getAlias(), input3
, 3, results
[2]);
54 UnicodeString input4
[] = {one
, two
, three
, four
};
55 CheckFormatting(formatter
.getAlias(), input4
, 4, results
[3]);
58 UBool
ListFormatterTest::RecordFourCases(const Locale
& locale
, UnicodeString one
, UnicodeString two
,
59 UnicodeString three
, UnicodeString four
, UnicodeString results
[4]) {
60 UErrorCode errorCode
= U_ZERO_ERROR
;
61 LocalPointer
<ListFormatter
> formatter(ListFormatter::createInstance(locale
, errorCode
));
62 if (U_FAILURE(errorCode
)) {
63 dataerrln("ListFormatter::createInstance(\"%s\", errorCode) failed in RecordFourCases: %s", locale
.getName(), u_errorName(errorCode
));
66 UnicodeString input1
[] = {one
};
67 formatter
->format(input1
, 1, results
[0], errorCode
);
68 UnicodeString input2
[] = {one
, two
};
69 formatter
->format(input2
, 2, results
[1], errorCode
);
70 UnicodeString input3
[] = {one
, two
, three
};
71 formatter
->format(input3
, 3, results
[2], errorCode
);
72 UnicodeString input4
[] = {one
, two
, three
, four
};
73 formatter
->format(input4
, 4, results
[3], errorCode
);
74 if (U_FAILURE(errorCode
)) {
75 errln("RecordFourCases failed: %s", u_errorName(errorCode
));
81 void ListFormatterTest::TestRoot() {
82 UnicodeString results
[4] = {
85 one
+ ", " + two
+ ", " + three
,
86 one
+ ", " + two
+ ", " + three
+ ", " + four
89 CheckFourCases("", one
, two
, three
, four
, results
);
92 // Bogus locale should fallback to root.
93 void ListFormatterTest::TestBogus() {
94 UnicodeString results
[4];
95 if (RecordFourCases(Locale::getDefault(), one
, two
, three
, four
, results
)) {
96 CheckFourCases("ex_PY", one
, two
, three
, four
, results
);
100 // Formatting in English.
101 // "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
102 void ListFormatterTest::TestEnglish() {
103 UnicodeString results
[4] = {
106 one
+ ", " + two
+ ", and " + three
,
107 one
+ ", " + two
+ ", " + three
+ ", and " + four
110 CheckFourCases("en", one
, two
, three
, four
, results
);
113 void ListFormatterTest::Test9946() {
114 UErrorCode errorCode
= U_ZERO_ERROR
;
115 LocalPointer
<ListFormatter
> formatter(ListFormatter::createInstance(Locale("en"), errorCode
));
116 if (U_FAILURE(errorCode
)) {
118 "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in Test9946: %s",
119 u_errorName(errorCode
));
122 UnicodeString data
[3] = {"{0}", "{1}", "{2}"};
123 UnicodeString actualResult
;
124 formatter
->format(data
, 3, actualResult
, errorCode
);
125 if (U_FAILURE(errorCode
)) {
127 "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in Test9946: %s",
128 u_errorName(errorCode
));
131 UnicodeString
expected("{0}, {1}, and {2}");
132 if (expected
!= actualResult
) {
133 errln("Expected " + expected
+ ", got " + actualResult
);
137 void ListFormatterTest::TestEnglishUS() {
138 UnicodeString results
[4] = {
141 one
+ ", " + two
+ ", and " + three
,
142 one
+ ", " + two
+ ", " + three
+ ", and " + four
145 CheckFourCases("en_US", one
, two
, three
, four
, results
);
148 // Formatting in Russian.
149 // "\\u0438" is used before the last element, and all elements up to (but not including) the penultimate are followed by a comma.
150 void ListFormatterTest::TestRussian() {
151 UnicodeString and_string
= UnicodeString(" \\u0438 ", -1, US_INV
).unescape();
152 UnicodeString results
[4] = {
154 one
+ and_string
+ two
,
155 one
+ ", " + two
+ and_string
+ three
,
156 one
+ ", " + two
+ ", " + three
+ and_string
+ four
159 CheckFourCases("ru", one
, two
, three
, four
, results
);
162 // Formatting in Malayalam.
163 // For two elements, "\\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46" is inserted in between.
164 // For more than two elements, comma is inserted between all elements up to (and including) the penultimate,
165 // and the word \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35 is inserted in the end.
166 void ListFormatterTest::TestMalayalam() {
167 UnicodeString pair_string
= UnicodeString(" \\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46 ", -1, US_INV
).unescape();
168 UnicodeString total_string
= UnicodeString(" \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35", -1, US_INV
).unescape();
169 UnicodeString results
[4] = {
171 one
+ pair_string
+ two
,
172 one
+ ", " + two
+ ", " + three
+ total_string
,
173 one
+ ", " + two
+ ", " + three
+ ", " + four
+ total_string
176 CheckFourCases("ml", one
, two
, three
, four
, results
);
179 // Formatting in Zulu.
180 // "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
181 void ListFormatterTest::TestZulu() {
182 UnicodeString results
[4] = {
184 "I-" + one
+ " ne-" + two
,
185 one
+ ", " + two
+ ", ne-" + three
,
186 one
+ ", " + two
+ ", " + three
+ ", ne-" + four
189 CheckFourCases("zu", one
, two
, three
, four
, results
);
192 void ListFormatterTest::TestOutOfOrderPatterns() {
193 UnicodeString results
[4] = {
195 two
+ " after " + one
,
196 three
+ " in the last after " + two
+ " after the first " + one
,
197 four
+ " in the last after " + three
+ " after " + two
+ " after the first " + one
200 UErrorCode errorCode
= U_ZERO_ERROR
;
201 ListFormatData
data("{1} after {0}", "{1} after the first {0}",
202 "{1} after {0}", "{1} in the last after {0}");
203 ListFormatter
formatter(data
, errorCode
);
205 UnicodeString input1
[] = {one
};
206 CheckFormatting(&formatter
, input1
, 1, results
[0]);
208 UnicodeString input2
[] = {one
, two
};
209 CheckFormatting(&formatter
, input2
, 2, results
[1]);
211 UnicodeString input3
[] = {one
, two
, three
};
212 CheckFormatting(&formatter
, input3
, 3, results
[2]);
214 UnicodeString input4
[] = {one
, two
, three
, four
};
215 CheckFormatting(&formatter
, input4
, 4, results
[3]);
218 void ListFormatterTest::runIndexedTest(int32_t index
, UBool exec
,
219 const char* &name
, char* /*par */) {
221 case 0: name
= "TestRoot"; if (exec
) TestRoot(); break;
222 case 1: name
= "TestBogus"; if (exec
) TestBogus(); break;
223 case 2: name
= "TestEnglish"; if (exec
) TestEnglish(); break;
224 case 3: name
= "TestEnglishUS"; if (exec
) TestEnglishUS(); break;
225 case 4: name
= "TestRussian"; if (exec
) TestRussian(); break;
226 case 5: name
= "TestMalayalam"; if (exec
) TestMalayalam(); break;
227 case 6: name
= "TestZulu"; if (exec
) TestZulu(); break;
228 case 7: name
= "TestOutOfOrderPatterns"; if (exec
) TestOutOfOrderPatterns(); break;
229 case 8: name
= "Test9946"; if (exec
) Test9946(); break;
231 default: name
= ""; break;