• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2012-2016, 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 
ListFormatterTest()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) {
24 }
25 
CheckFormatting(const ListFormatter * formatter,UnicodeString data[],int32_t dataSize,const UnicodeString & expected_result)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 + "|");
34     }
35 }
36 
CheckFourCases(const char * locale_string,UnicodeString one,UnicodeString two,UnicodeString three,UnicodeString four,UnicodeString results[4])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));
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 
RecordFourCases(const Locale & locale,UnicodeString one,UnicodeString two,UnicodeString three,UnicodeString four,UnicodeString results[4])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));
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 
TestRoot()81 void 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.
TestBogus()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);
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.
TestEnglish()102 void 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 
Test9946()113 void ListFormatterTest::Test9946() {
114     UErrorCode errorCode = U_ZERO_ERROR;
115     LocalPointer<ListFormatter> formatter(ListFormatter::createInstance(Locale("en"), errorCode));
116     if (U_FAILURE(errorCode)) {
117         dataerrln(
118             "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in Test9946: %s",
119             u_errorName(errorCode));
120         return;
121     }
122     UnicodeString data[3] = {"{0}", "{1}", "{2}"};
123     UnicodeString actualResult;
124     formatter->format(data, 3, actualResult, errorCode);
125     if (U_FAILURE(errorCode)) {
126         dataerrln(
127             "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in Test9946: %s",
128             u_errorName(errorCode));
129         return;
130     }
131     UnicodeString expected("{0}, {1}, and {2}");
132     if (expected != actualResult) {
133         errln("Expected " + expected + ", got " + actualResult);
134     }
135 }
136 
TestEnglishUS()137 void ListFormatterTest::TestEnglishUS() {
138     UnicodeString results[4] = {
139         one,
140         one + " and " + two,
141         one + ", " + two + ", and " + three,
142         one + ", " + two + ", " + three + ", and " + four
143     };
144 
145     CheckFourCases("en_US", one, two, three, four, results);
146 }
147 
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.
TestRussian()150 void ListFormatterTest::TestRussian() {
151     UnicodeString and_string = UnicodeString(" \\u0438 ", -1, US_INV).unescape();
152     UnicodeString results[4] = {
153         one,
154         one + and_string + two,
155         one + ", " + two + and_string + three,
156         one + ", " + two + ", " + three + and_string + four
157     };
158 
159     CheckFourCases("ru", one, two, three, four, results);
160 }
161 
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.
TestMalayalam()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] = {
170         one,
171         one + pair_string + two,
172         one + ", " + two + ", " + three + total_string,
173         one + ", " + two + ", " + three + ", " + four + total_string
174     };
175 
176     CheckFourCases("ml", one, two, three, four, results);
177 }
178 
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.
TestZulu()181 void ListFormatterTest::TestZulu() {
182     UnicodeString results[4] = {
183         one,
184         "I-" + one + " ne-" + two,
185         one + ", " + two + ", ne-" + three,
186         one + ", " + two + ", " + three + ", ne-" + four
187     };
188 
189     CheckFourCases("zu", one, two, three, four, results);
190 }
191 
TestOutOfOrderPatterns()192 void ListFormatterTest::TestOutOfOrderPatterns() {
193     UnicodeString results[4] = {
194         one,
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
198     };
199 
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);
204 
205     UnicodeString input1[] = {one};
206     CheckFormatting(&formatter, input1, 1, results[0]);
207 
208     UnicodeString input2[] = {one, two};
209     CheckFormatting(&formatter, input2, 2, results[1]);
210 
211     UnicodeString input3[] = {one, two, three};
212     CheckFormatting(&formatter, input3, 3, results[2]);
213 
214     UnicodeString input4[] = {one, two, three, four};
215     CheckFormatting(&formatter, input4, 4, results[3]);
216 }
217 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)218 void ListFormatterTest::runIndexedTest(int32_t index, UBool exec,
219                                        const char* &name, char* /*par */) {
220     switch(index) {
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;
230 
231         default: name = ""; break;
232     }
233 }
234