]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/listformattertest.cpp
ICU-511.32.tar.gz
[apple/icu.git] / icuSources / test / intltest / listformattertest.cpp
CommitLineData
51004dcb
A
1/*
2*******************************************************************************
3*
4* Copyright (C) 2012-2013, International Business Machines
5* Corporation and others. All Rights Reserved.
6*
7*******************************************************************************
8* file name: listformattertest.cpp
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2012aug27
14* created by: Umesh P. Nair
15*/
16
17#include "listformattertest.h"
18#include <string.h>
19
20ListFormatterTest::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) {
24}
25
26void 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 + "|");
34 }
35}
36
37void 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));
43 return;
44 }
45 UnicodeString input1[] = {one};
46 CheckFormatting(formatter.getAlias(), input1, 1, results[0]);
47
48 UnicodeString input2[] = {one, two};
49 CheckFormatting(formatter.getAlias(), input2, 2, results[1]);
50
51 UnicodeString input3[] = {one, two, three};
52 CheckFormatting(formatter.getAlias(), input3, 3, results[2]);
53
54 UnicodeString input4[] = {one, two, three, four};
55 CheckFormatting(formatter.getAlias(), input4, 4, results[3]);
56}
57
58UBool 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));
64 return FALSE;
65 }
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));
76 return FALSE;
77 }
78 return TRUE;
79}
80
81void ListFormatterTest::TestRoot() {
82 UnicodeString results[4] = {
83 one,
84 one + ", " + two,
85 one + ", " + two + ", " + three,
86 one + ", " + two + ", " + three + ", " + four
87 };
88
89 CheckFourCases("", one, two, three, four, results);
90}
91
92// Bogus locale should fallback to root.
93void 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);
97 }
98}
99
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.
102void ListFormatterTest::TestEnglish() {
103 UnicodeString results[4] = {
104 one,
105 one + " and " + two,
106 one + ", " + two + ", and " + three,
107 one + ", " + two + ", " + three + ", and " + four
108 };
109
110 CheckFourCases("en", one, two, three, four, results);
111}
112
113void ListFormatterTest::TestEnglishUS() {
114 UnicodeString results[4] = {
115 one,
116 one + " and " + two,
117 one + ", " + two + ", and " + three,
118 one + ", " + two + ", " + three + ", and " + four
119 };
120
121 CheckFourCases("en_US", one, two, three, four, results);
122}
123
124// Formatting in Russian.
125// "\\u0438" is used before the last element, and all elements up to (but not including) the penultimate are followed by a comma.
126void ListFormatterTest::TestRussian() {
127 UnicodeString and_string = UnicodeString(" \\u0438 ", -1, US_INV).unescape();
128 UnicodeString results[4] = {
129 one,
130 one + and_string + two,
131 one + ", " + two + and_string + three,
132 one + ", " + two + ", " + three + and_string + four
133 };
134
135 CheckFourCases("ru", one, two, three, four, results);
136}
137
138// Formatting in Malayalam.
139// For two elements, "\\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46" is inserted in between.
140// For more than two elements, comma is inserted between all elements up to (and including) the penultimate,
141// and the word \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35 is inserted in the end.
142void ListFormatterTest::TestMalayalam() {
143 UnicodeString pair_string = UnicodeString(" \\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46 ", -1, US_INV).unescape();
144 UnicodeString total_string = UnicodeString(" \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35", -1, US_INV).unescape();
145 UnicodeString results[4] = {
146 one,
147 one + pair_string + two,
148 one + ", " + two + ", " + three + total_string,
149 one + ", " + two + ", " + three + ", " + four + total_string
150 };
151
152 CheckFourCases("ml", one, two, three, four, results);
153}
154
155// Formatting in Zulu.
156// "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
157void ListFormatterTest::TestZulu() {
158 UnicodeString results[4] = {
159 one,
160 "I-" + one + " ne-" + two,
161 one + ", " + two + ", no-" + three,
162 one + ", " + two + ", " + three + ", no-" + four
163 };
164
165 CheckFourCases("zu", one, two, three, four, results);
166}
167
168void ListFormatterTest::TestOutOfOrderPatterns() {
169 UnicodeString results[4] = {
170 one,
171 two + " after " + one,
172 three + " in the last after " + two + " after the first " + one,
173 four + " in the last after " + three + " after " + two + " after the first " + one
174 };
175
176 ListFormatData data("{1} after {0}", "{1} after the first {0}",
177 "{1} after {0}", "{1} in the last after {0}");
178 ListFormatter formatter(data);
179
180 UnicodeString input1[] = {one};
181 CheckFormatting(&formatter, input1, 1, results[0]);
182
183 UnicodeString input2[] = {one, two};
184 CheckFormatting(&formatter, input2, 2, results[1]);
185
186 UnicodeString input3[] = {one, two, three};
187 CheckFormatting(&formatter, input3, 3, results[2]);
188
189 UnicodeString input4[] = {one, two, three, four};
190 CheckFormatting(&formatter, input4, 4, results[3]);
191}
192
193void ListFormatterTest::runIndexedTest(int32_t index, UBool exec,
194 const char* &name, char* /*par */) {
195 switch(index) {
196 case 0: name = "TestRoot"; if (exec) TestRoot(); break;
197 case 1: name = "TestBogus"; if (exec) TestBogus(); break;
198 case 2: name = "TestEnglish"; if (exec) TestEnglish(); break;
199 case 3: name = "TestEnglishUS"; if (exec) TestEnglishUS(); break;
200 case 4: name = "TestRussian"; if (exec) TestRussian(); break;
201 case 5: name = "TestMalayalam"; if (exec) TestMalayalam(); break;
202 case 6: name = "TestZulu"; if (exec) TestZulu(); break;
203 case 7: name = "TestOutOfOrderPatterns"; if (exec) TestOutOfOrderPatterns(); break;
204
205 default: name = ""; break;
206 }
207}