1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 2012-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: listformattertest.cpp
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 2012aug27
16 * created by: Umesh P. Nair
17 */
18
19 #include "listformattertest.h"
20 #include "unicode/ulistformatter.h"
21 #include "cmemory.h"
22 #include <string.h>
23
24 #if !UCONFIG_NO_FORMATTING
25
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)26 void ListFormatterTest::runIndexedTest(int32_t index, UBool exec,
27 const char* &name, char* /*par */) {
28 TESTCASE_AUTO_BEGIN;
29 TESTCASE_AUTO(TestRoot);
30 TESTCASE_AUTO(TestBogus);
31 TESTCASE_AUTO(TestEnglish);
32 TESTCASE_AUTO(TestEnglishUS);
33 TESTCASE_AUTO(TestRussian);
34 TESTCASE_AUTO(TestMalayalam);
35 TESTCASE_AUTO(TestZulu);
36 TESTCASE_AUTO(TestOutOfOrderPatterns);
37 TESTCASE_AUTO(Test9946);
38 TESTCASE_AUTO(TestEnglishGB);
39 TESTCASE_AUTO(TestNynorsk);
40 TESTCASE_AUTO(TestChineseTradHK);
41 TESTCASE_AUTO(TestFieldPositionIteratorWith1Item);
42 TESTCASE_AUTO(TestFieldPositionIteratorWith2Items);
43 TESTCASE_AUTO(TestFieldPositionIteratorWith2ItemsPatternShift);
44 TESTCASE_AUTO(TestFieldPositionIteratorWith3Items);
45 TESTCASE_AUTO(TestFieldPositionIteratorWith3ItemsPatternShift);
46 TESTCASE_AUTO(TestFormattedValue);
47 TESTCASE_AUTO(TestDifferentStyles);
48 TESTCASE_AUTO(TestCreateStyled);
49 TESTCASE_AUTO(TestContextual);
50 TESTCASE_AUTO(TestNextPosition);
51 TESTCASE_AUTO(TestInt32Overflow);
52 TESTCASE_AUTO_END;
53 }
54
55 namespace {
attrString(int32_t attrId)56 const char* attrString(int32_t attrId) {
57 switch (attrId) {
58 case ULISTFMT_LITERAL_FIELD: return "literal";
59 case ULISTFMT_ELEMENT_FIELD: return "element";
60 default: return "xxx";
61 }
62 }
63 } // namespace
64
ExpectPositions(const FormattedList & iter,int32_t * values,int32_t tupleCount,UErrorCode & status)65 void ListFormatterTest::ExpectPositions(
66 const FormattedList& iter,
67 int32_t *values,
68 int32_t tupleCount,
69 UErrorCode& status) {
70 UBool found[10];
71 ConstrainedFieldPosition cfp;
72 cfp.constrainCategory(UFIELD_CATEGORY_LIST);
73 if (tupleCount > 10) {
74 assertTrue("internal error, tupleCount too large", FALSE);
75 } else {
76 for (int i = 0; i < tupleCount; ++i) {
77 found[i] = FALSE;
78 }
79 }
80 while (iter.nextPosition(cfp, status)) {
81 UBool ok = FALSE;
82 int32_t id = cfp.getField();
83 int32_t start = cfp.getStart();
84 int32_t limit = cfp.getLimit();
85 char buf[128];
86 sprintf(buf, "%24s %3d %3d %3d", attrString(id), id, start, limit);
87 logln(buf);
88 for (int i = 0; i < tupleCount; ++i) {
89 if (found[i]) {
90 continue;
91 }
92 if (values[i*3] == id && values[i*3+1] == start && values[i*3+2] == limit) {
93 found[i] = ok = TRUE;
94 break;
95 }
96 }
97 assertTrue((UnicodeString)"found [" + attrString(id) + "," + start + "," + limit + "]", ok);
98 }
99 // check that all were found
100 UBool ok = TRUE;
101 for (int i = 0; i < tupleCount; ++i) {
102 if (!found[i]) {
103 ok = FALSE;
104 assertTrue((UnicodeString) "missing [" + attrString(values[i*3]) + "," + values[i*3+1] +
105 "," + values[i*3+2] + "]", found[i]);
106 }
107 }
108 assertTrue("no expected values were missing", ok);
109 }
110
ListFormatterTest()111 ListFormatterTest::ListFormatterTest() :
112 prefix("Prefix: ", -1, US_INV),
113 one("Alice", -1, US_INV), two("Bob", -1, US_INV),
114 three("Charlie", -1, US_INV), four("Delta", -1, US_INV) {
115 }
116
CheckFormatting(const ListFormatter * formatter,UnicodeString data[],int32_t dataSize,const UnicodeString & expected_result,const char * testName)117 void ListFormatterTest::CheckFormatting(const ListFormatter* formatter, UnicodeString data[], int32_t dataSize,
118 const UnicodeString& expected_result, const char* testName) {
119 UnicodeString actualResult(prefix);
120 IcuTestErrorCode errorCode(*this, testName);
121 formatter->format(data, dataSize, actualResult, errorCode);
122 UnicodeString expectedStringWithPrefix = prefix + expected_result;
123 if (expectedStringWithPrefix != actualResult) {
124 errln(UnicodeString("Expected: |") + expectedStringWithPrefix + "|, Actual: |" + actualResult + "|");
125 }
126 }
127
CheckFourCases(const char * locale_string,UnicodeString one,UnicodeString two,UnicodeString three,UnicodeString four,UnicodeString results[4],const char * testName)128 void ListFormatterTest::CheckFourCases(const char* locale_string, UnicodeString one, UnicodeString two,
129 UnicodeString three, UnicodeString four, UnicodeString results[4], const char* testName) {
130 IcuTestErrorCode errorCode(*this, testName);
131 LocalPointer<ListFormatter> formatter(ListFormatter::createInstance(Locale(locale_string), errorCode));
132 if (U_FAILURE(errorCode)) {
133 dataerrln("ListFormatter::createInstance(Locale(\"%s\"), errorCode) failed in CheckFourCases: %s", locale_string, u_errorName(errorCode));
134 return;
135 }
136 UnicodeString input1[] = {one};
137 CheckFormatting(formatter.getAlias(), input1, 1, results[0], testName);
138
139 UnicodeString input2[] = {one, two};
140 CheckFormatting(formatter.getAlias(), input2, 2, results[1], testName);
141
142 UnicodeString input3[] = {one, two, three};
143 CheckFormatting(formatter.getAlias(), input3, 3, results[2], testName);
144
145 UnicodeString input4[] = {one, two, three, four};
146 CheckFormatting(formatter.getAlias(), input4, 4, results[3], testName);
147 }
148
RecordFourCases(const Locale & locale,UnicodeString one,UnicodeString two,UnicodeString three,UnicodeString four,UnicodeString results[4],const char * testName)149 UBool ListFormatterTest::RecordFourCases(const Locale& locale, UnicodeString one, UnicodeString two,
150 UnicodeString three, UnicodeString four, UnicodeString results[4], const char* testName) {
151 IcuTestErrorCode errorCode(*this, testName);
152 LocalPointer<ListFormatter> formatter(ListFormatter::createInstance(locale, errorCode));
153 if (U_FAILURE(errorCode)) {
154 dataerrln("ListFormatter::createInstance(\"%s\", errorCode) failed in RecordFourCases: %s", locale.getName(), u_errorName(errorCode));
155 return FALSE;
156 }
157 UnicodeString input1[] = {one};
158 formatter->format(input1, 1, results[0], errorCode);
159 UnicodeString input2[] = {one, two};
160 formatter->format(input2, 2, results[1], errorCode);
161 UnicodeString input3[] = {one, two, three};
162 formatter->format(input3, 3, results[2], errorCode);
163 UnicodeString input4[] = {one, two, three, four};
164 formatter->format(input4, 4, results[3], errorCode);
165 if (U_FAILURE(errorCode)) {
166 errln("RecordFourCases failed: %s", u_errorName(errorCode));
167 return FALSE;
168 }
169 return TRUE;
170 }
171
TestRoot()172 void ListFormatterTest::TestRoot() {
173 UnicodeString results[4] = {
174 one,
175 one + ", " + two,
176 one + ", " + two + ", " + three,
177 one + ", " + two + ", " + three + ", " + four
178 };
179
180 CheckFourCases("", one, two, three, four, results, "TestRoot()");
181 }
182
183 // Bogus locale should fallback to root.
TestBogus()184 void ListFormatterTest::TestBogus() {
185 UnicodeString results[4];
186 if (RecordFourCases(Locale::getDefault(), one, two, three, four, results, "TestBogus()")) {
187 CheckFourCases("ex_PY", one, two, three, four, results, "TestBogus()");
188 }
189 }
190
191 // Formatting in English.
192 // "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
TestEnglish()193 void ListFormatterTest::TestEnglish() {
194 UnicodeString results[4] = {
195 one,
196 one + " and " + two,
197 one + ", " + two + ", and " + three,
198 one + ", " + two + ", " + three + ", and " + four
199 };
200
201 CheckFourCases("en", one, two, three, four, results, "TestEnglish()");
202 }
203
Test9946()204 void ListFormatterTest::Test9946() {
205 IcuTestErrorCode errorCode(*this, "Test9946()");
206 LocalPointer<ListFormatter> formatter(ListFormatter::createInstance(Locale("en"), errorCode));
207 if (U_FAILURE(errorCode)) {
208 dataerrln(
209 "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in Test9946: %s",
210 u_errorName(errorCode));
211 return;
212 }
213 UnicodeString data[3] = {"{0}", "{1}", "{2}"};
214 UnicodeString actualResult;
215 formatter->format(data, 3, actualResult, errorCode);
216 if (U_FAILURE(errorCode)) {
217 dataerrln(
218 "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in Test9946: %s",
219 u_errorName(errorCode));
220 return;
221 }
222 UnicodeString expected("{0}, {1}, and {2}");
223 if (expected != actualResult) {
224 errln("Expected " + expected + ", got " + actualResult);
225 }
226 }
227
TestEnglishUS()228 void ListFormatterTest::TestEnglishUS() {
229 UnicodeString results[4] = {
230 one,
231 one + " and " + two,
232 one + ", " + two + ", and " + three,
233 one + ", " + two + ", " + three + ", and " + four
234 };
235
236 CheckFourCases("en_US", one, two, three, four, results, "TestEnglishUS()");
237 }
238
239 // Tests resource loading and inheritance when region sublocale
240 // has only partial data for the listPattern element (overriding
241 // some of the parent data). #12994
TestEnglishGB()242 void ListFormatterTest::TestEnglishGB() {
243 UnicodeString results[4] = {
244 one,
245 one + " and " + two,
246 one + ", " + two + " and " + three,
247 one + ", " + two + ", " + three + " and " + four
248 };
249
250 CheckFourCases("en_GB", one, two, three, four, results, "TestEnglishGB()");
251 }
252
RunTestFieldPositionIteratorWithFormatter(ListFormatter * formatter,UnicodeString data[],int32_t n,int32_t expected[],int32_t tupleCount,const char16_t * expectedFormatted,const char * testName)253 void ListFormatterTest::RunTestFieldPositionIteratorWithFormatter(
254 ListFormatter* formatter,
255 UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount,
256 const char16_t *expectedFormatted,
257 const char* testName) {
258 IcuTestErrorCode errorCode(*this, testName);
259 FormattedList fl = formatter->formatStringsToValue(data, n, errorCode);
260 UnicodeString actual = fl.toString(errorCode);
261 if (U_FAILURE(errorCode)) {
262 dataerrln(
263 "ListFormatter::format(data, %d, &iter, errorCode) "
264 "failed in %s: %s", n, testName, u_errorName(errorCode));
265 return;
266 }
267 if (actual != expectedFormatted) {
268 errln(UnicodeString("Expected: |") + expectedFormatted + "|, Actual: |" + actual + "|");
269 }
270 ExpectPositions(fl, expected, tupleCount, errorCode);
271 }
272
RunTestFieldPositionIteratorWithNItemsPatternShift(UnicodeString data[],int32_t n,int32_t expected[],int32_t tupleCount,const char16_t * expectedFormatted,const char * testName)273 void ListFormatterTest::RunTestFieldPositionIteratorWithNItemsPatternShift(
274 UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount,
275 const char16_t *expectedFormatted,
276 const char* testName) {
277 IcuTestErrorCode errorCode(*this, testName);
278 LocalPointer<ListFormatter> formatter(ListFormatter::createInstance(
279 Locale("ur", "IN"),
280 ULISTFMT_TYPE_UNITS,
281 ULISTFMT_WIDTH_NARROW,
282 errorCode));
283 if (U_FAILURE(errorCode)) {
284 dataerrln(
285 "ListFormatter::createInstance(Locale(\"ur\", \"IN\"), \"unit-narrow\", errorCode) failed in "
286 "%s: %s", testName, u_errorName(errorCode));
287 return;
288 }
289 RunTestFieldPositionIteratorWithFormatter(
290 formatter.getAlias(),
291 data, n, expected, tupleCount, expectedFormatted, testName);
292 }
293
RunTestFieldPositionIteratorWithNItems(UnicodeString data[],int32_t n,int32_t expected[],int32_t tupleCount,const char16_t * expectedFormatted,const char * testName)294 void ListFormatterTest::RunTestFieldPositionIteratorWithNItems(
295 UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount,
296 const char16_t *expectedFormatted,
297 const char* testName) {
298 IcuTestErrorCode errorCode(*this, testName);
299 LocalPointer<ListFormatter> formatter(
300 ListFormatter::createInstance(Locale("en"), errorCode));
301 if (U_FAILURE(errorCode)) {
302 dataerrln(
303 "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in "
304 "%s: %s", testName, u_errorName(errorCode));
305 return;
306 }
307 RunTestFieldPositionIteratorWithFormatter(
308 formatter.getAlias(),
309 data, n, expected, tupleCount, expectedFormatted, testName);
310 }
311
TestFieldPositionIteratorWith3Items()312 void ListFormatterTest::TestFieldPositionIteratorWith3Items() {
313 // 0 1
314 // 012345678901234
315 // "a, bbb, and cc"
316 UnicodeString data[3] = {"a", "bbb", "cc"};
317 int32_t expected[] = {
318 ULISTFMT_ELEMENT_FIELD, 0, 1,
319 ULISTFMT_LITERAL_FIELD, 1, 3,
320 ULISTFMT_ELEMENT_FIELD, 3, 6,
321 ULISTFMT_LITERAL_FIELD, 6, 12,
322 ULISTFMT_ELEMENT_FIELD, 12, 14
323 };
324 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
325 RunTestFieldPositionIteratorWithNItems(
326 data, 3, expected, tupleCount,
327 u"a, bbb, and cc",
328 "TestFieldPositionIteratorWith3Items");
329 }
330
TestFieldPositionIteratorWith3ItemsPatternShift()331 void ListFormatterTest::TestFieldPositionIteratorWith3ItemsPatternShift() {
332 // 0 1
333 // 012345678901234
334 // "cc bbb a"
335 UnicodeString data[3] = {"a", "bbb", "cc"};
336 int32_t expected[] = {
337 ULISTFMT_ELEMENT_FIELD, 7, 8,
338 ULISTFMT_LITERAL_FIELD, 6, 7,
339 ULISTFMT_ELEMENT_FIELD, 3, 6,
340 ULISTFMT_LITERAL_FIELD, 2, 3,
341 ULISTFMT_ELEMENT_FIELD, 0, 2
342 };
343 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
344 RunTestFieldPositionIteratorWithNItemsPatternShift(
345 data, 3, expected, tupleCount,
346 u"cc bbb a",
347 "TestFieldPositionIteratorWith3ItemsPatternShift");
348 }
349
TestFieldPositionIteratorWith2Items()350 void ListFormatterTest::TestFieldPositionIteratorWith2Items() {
351 // 0 1
352 // 01234567890
353 // "bbb and cc"
354 UnicodeString data[2] = {"bbb", "cc"};
355 int32_t expected[] = {
356 ULISTFMT_ELEMENT_FIELD, 0, 3,
357 ULISTFMT_LITERAL_FIELD, 3, 8,
358 ULISTFMT_ELEMENT_FIELD, 8, 10
359 };
360 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
361 RunTestFieldPositionIteratorWithNItems(
362 data, 2, expected, tupleCount,
363 u"bbb and cc",
364 "TestFieldPositionIteratorWith2Items");
365 }
366
TestFieldPositionIteratorWith2ItemsPatternShift()367 void ListFormatterTest::TestFieldPositionIteratorWith2ItemsPatternShift() {
368 // 0 1
369 // 01234567890
370 // "cc bbb"
371 UnicodeString data[2] = {"bbb", "cc"};
372 int32_t expected[] = {
373 ULISTFMT_ELEMENT_FIELD, 3, 6,
374 ULISTFMT_LITERAL_FIELD, 2, 3,
375 ULISTFMT_ELEMENT_FIELD, 0, 2
376 };
377 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
378 RunTestFieldPositionIteratorWithNItemsPatternShift(
379 data, 2, expected, tupleCount,
380 u"cc bbb",
381 "TestFieldPositionIteratorWith2ItemsPatternShift");
382 }
383
TestFieldPositionIteratorWith1Item()384 void ListFormatterTest::TestFieldPositionIteratorWith1Item() {
385 // 012
386 // "cc"
387 UnicodeString data[1] = {"cc"};
388 int32_t expected[] = {
389 ULISTFMT_ELEMENT_FIELD, 0, 2
390 };
391 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
392 RunTestFieldPositionIteratorWithNItems(
393 data, 1, expected, tupleCount,
394 u"cc",
395 "TestFieldPositionIteratorWith1Item");
396 }
397
398 // Tests resource loading and inheritance when region sublocale
399 // has only partial data for the listPattern element (overriding
400 // some of the parent data). #12994
TestNynorsk()401 void ListFormatterTest::TestNynorsk() {
402 UnicodeString results[4] = {
403 one,
404 one + " og " + two,
405 one + ", " + two + " og " + three,
406 one + ", " + two + ", " + three + " og " + four
407 };
408
409 CheckFourCases("nn", one, two, three, four, results, "TestNynorsk()");
410 }
411
412 // Tests resource loading and inheritance when region sublocale
413 // has only partial data for the listPattern element (overriding
414 // some of the parent data). #12994
TestChineseTradHK()415 void ListFormatterTest::TestChineseTradHK() {
416 UnicodeString and_string = UnicodeString("\\u53CA", -1, US_INV).unescape();
417 UnicodeString comma_string = UnicodeString("\\u3001", -1, US_INV).unescape();
418 UnicodeString results[4] = {
419 one,
420 one + and_string + two,
421 one + comma_string + two + and_string + three,
422 one + comma_string + two + comma_string + three + and_string + four
423 };
424
425 CheckFourCases("zh_Hant_HK", one, two, three, four, results, "TestChineseTradHK()");
426 }
427
428 // Formatting in Russian.
429 // "\\u0438" is used before the last element, and all elements up to (but not including) the penultimate are followed by a comma.
TestRussian()430 void ListFormatterTest::TestRussian() {
431 UnicodeString and_string = UnicodeString(" \\u0438 ", -1, US_INV).unescape();
432 UnicodeString results[4] = {
433 one,
434 one + and_string + two,
435 one + ", " + two + and_string + three,
436 one + ", " + two + ", " + three + and_string + four
437 };
438
439 CheckFourCases("ru", one, two, three, four, results, "TestRussian()");
440 }
441
442 // Formatting in Malayalam.
443 // For two elements, "\\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46" is inserted in between.
444 // For more than two elements, comma is inserted between all elements up to (and including) the penultimate,
445 // and the word \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35 is inserted in the end.
TestMalayalam()446 void ListFormatterTest::TestMalayalam() {
447 UnicodeString pair_string = UnicodeString(" \\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46 ", -1, US_INV).unescape();
448 UnicodeString total_string = UnicodeString(" \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35", -1, US_INV).unescape();
449 UnicodeString results[4] = {
450 one,
451 one + pair_string + two,
452 one + ", " + two + ", " + three + total_string,
453 one + ", " + two + ", " + three + ", " + four + total_string
454 };
455
456 CheckFourCases("ml", one, two, three, four, results, "TestMalayalam()");
457 }
458
459 // Formatting in Zulu.
460 // "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
TestZulu()461 void ListFormatterTest::TestZulu() {
462 UnicodeString results[4] = {
463 one,
464 one + " ne-" + two,
465 one + ", " + two + ", ne-" + three,
466 one + ", " + two + ", " + three + ", ne-" + four
467 };
468
469 CheckFourCases("zu", one, two, three, four, results, "TestZulu()");
470 }
471
TestOutOfOrderPatterns()472 void ListFormatterTest::TestOutOfOrderPatterns() {
473 UnicodeString results[4] = {
474 one,
475 two + " after " + one,
476 three + " in the last after " + two + " after the first " + one,
477 four + " in the last after " + three + " after " + two + " after the first " + one
478 };
479
480 IcuTestErrorCode errorCode(*this, "TestOutOfOrderPatterns()");
481 Locale locale("en");
482 ListFormatData data("{1} after {0}", "{1} after the first {0}",
483 "{1} after {0}", "{1} in the last after {0}", locale);
484 ListFormatter formatter(data, errorCode);
485
486 UnicodeString input1[] = {one};
487 CheckFormatting(&formatter, input1, 1, results[0], "TestOutOfOrderPatterns()");
488
489 UnicodeString input2[] = {one, two};
490 CheckFormatting(&formatter, input2, 2, results[1], "TestOutOfOrderPatterns()");
491
492 UnicodeString input3[] = {one, two, three};
493 CheckFormatting(&formatter, input3, 3, results[2], "TestOutOfOrderPatterns()");
494
495 UnicodeString input4[] = {one, two, three, four};
496 CheckFormatting(&formatter, input4, 4, results[3], "TestOutOfOrderPatterns()");
497 }
498
TestFormattedValue()499 void ListFormatterTest::TestFormattedValue() {
500 IcuTestErrorCode status(*this, "TestFormattedValue");
501
502 {
503 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("en", status));
504 if (status.errIfFailureAndReset()) { return; }
505 const char16_t* message = u"Field position test 1";
506 const char16_t* expectedString = u"hello, wonderful, and world";
507 const UnicodeString inputs[] = {
508 u"hello",
509 u"wonderful",
510 u"world"
511 };
512 FormattedList result = fmt->formatStringsToValue(inputs, UPRV_LENGTHOF(inputs), status);
513 static const UFieldPositionWithCategory expectedFieldPositions[] = {
514 // field, begin index, end index
515 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 5},
516 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 5},
517 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 5, 7},
518 {UFIELD_CATEGORY_LIST_SPAN, 1, 7, 16},
519 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 7, 16},
520 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 16, 22},
521 {UFIELD_CATEGORY_LIST_SPAN, 2, 22, 27},
522 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 22, 27}};
523 checkMixedFormattedValue(
524 message,
525 result,
526 expectedString,
527 expectedFieldPositions,
528 UPRV_LENGTHOF(expectedFieldPositions));
529 }
530
531 {
532 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("zh", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, status));
533 if (status.errIfFailureAndReset()) { return; }
534 const char16_t* message = u"Field position test 2 (ICU-21340)";
535 const char16_t* expectedString = u"aabbbbbbbccc";
536 const UnicodeString inputs[] = {
537 u"aa",
538 u"bbbbbbb",
539 u"ccc"
540 };
541 FormattedList result = fmt->formatStringsToValue(inputs, UPRV_LENGTHOF(inputs), status);
542 static const UFieldPositionWithCategory expectedFieldPositions[] = {
543 // field, begin index, end index
544 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 2},
545 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 2},
546 {UFIELD_CATEGORY_LIST_SPAN, 1, 2, 9},
547 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 2, 9},
548 {UFIELD_CATEGORY_LIST_SPAN, 2, 9, 12},
549 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 9, 12}};
550 checkMixedFormattedValue(
551 message,
552 result,
553 expectedString,
554 expectedFieldPositions,
555 UPRV_LENGTHOF(expectedFieldPositions));
556 }
557
558 {
559 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, status));
560 if (status.errIfFailureAndReset()) { return; }
561 const char16_t* message = u"ICU-21383 Long list";
562 const char16_t* expectedString = u"a, b, c, d, e, f, g, h, i";
563 const UnicodeString inputs[] = {
564 u"a",
565 u"b",
566 u"c",
567 u"d",
568 u"e",
569 u"f",
570 u"g",
571 u"h",
572 u"i",
573 };
574 FormattedList result = fmt->formatStringsToValue(inputs, UPRV_LENGTHOF(inputs), status);
575 static const UFieldPositionWithCategory expectedFieldPositions[] = {
576 // field, begin index, end index
577 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 1},
578 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 1},
579 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 1, 3},
580 {UFIELD_CATEGORY_LIST_SPAN, 1, 3, 4},
581 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 3, 4},
582 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 4, 6},
583 {UFIELD_CATEGORY_LIST_SPAN, 2, 6, 7},
584 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 6, 7},
585 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 7, 9},
586 {UFIELD_CATEGORY_LIST_SPAN, 3, 9, 10},
587 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 9, 10},
588 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 10, 12},
589 {UFIELD_CATEGORY_LIST_SPAN, 4, 12, 13},
590 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 12, 13},
591 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 13, 15},
592 {UFIELD_CATEGORY_LIST_SPAN, 5, 15, 16},
593 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 15, 16},
594 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 16, 18},
595 {UFIELD_CATEGORY_LIST_SPAN, 6, 18, 19},
596 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 18, 19},
597 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 19, 21},
598 {UFIELD_CATEGORY_LIST_SPAN, 7, 21, 22},
599 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 21, 22},
600 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 22, 24},
601 {UFIELD_CATEGORY_LIST_SPAN, 8, 24, 25},
602 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 24, 25},
603 };
604 checkMixedFormattedValue(
605 message,
606 result,
607 expectedString,
608 expectedFieldPositions,
609 UPRV_LENGTHOF(expectedFieldPositions));
610 }
611 }
612
DoTheRealListStyleTesting(Locale locale,UnicodeString items[],int itemCount,UListFormatterType type,UListFormatterWidth width,const char * expected,IcuTestErrorCode status)613 void ListFormatterTest::DoTheRealListStyleTesting(
614 Locale locale,
615 UnicodeString items[],
616 int itemCount,
617 UListFormatterType type,
618 UListFormatterWidth width,
619 const char* expected,
620 IcuTestErrorCode status) {
621
622 LocalPointer<ListFormatter> formatter(
623 ListFormatter::createInstance(locale, type, width, status));
624
625 UnicodeString actualResult;
626 formatter->format(items, itemCount, actualResult, status);
627 assertEquals(Int64ToUnicodeString(type) + "-" + Int64ToUnicodeString(width),
628 UnicodeString(expected), actualResult);
629 }
630
TestDifferentStyles()631 void ListFormatterTest::TestDifferentStyles() {
632 Locale locale("fr");
633 UnicodeString input[4] = { u"rouge", u"jaune", u"bleu", u"vert" };
634 IcuTestErrorCode status(*this, "TestDifferentStyles()");
635
636 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu et vert", status);
637 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu ou vert", status);
638 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu et vert", status);
639 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_NARROW, "rouge jaune bleu vert", status);
640 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, "rouge, jaune, bleu et vert", status);
641 }
642
TestCreateStyled()643 void ListFormatterTest::TestCreateStyled() {
644 IcuTestErrorCode status(*this, "TestCreateStyled");
645 // Locale en has interesting data
646 struct TestCase {
647 const char* locale;
648 UListFormatterType type;
649 UListFormatterWidth width;
650 const char16_t* expected3;
651 const char16_t* expected2;
652 const char16_t* expected1;
653 } cases[] = {
654 { "pt", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_WIDE, u"A, B e C", u"A e B", u"A" },
655 { "pt", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_SHORT, u"A, B e C", u"A e B", u"A" },
656 { "pt", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_NARROW, u"A, B, C", u"A, B", u"A" },
657 { "pt", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_WIDE, u"A, B ou C", u"A ou B", u"A" },
658 { "pt", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_SHORT, u"A, B ou C", u"A ou B", u"A" },
659 { "pt", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_NARROW, u"A, B ou C", u"A ou B", u"A" },
660 { "pt", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_WIDE, u"A, B e C", u"A e B", u"A" },
661 { "pt", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, u"A, B e C", u"A e B", u"A" },
662 { "pt", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_NARROW, u"A B C", u"A B", u"A" },
663 { "en", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_WIDE, u"A, B, and C", u"A and B", u"A" },
664 { "en", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_SHORT, u"A, B, & C", u"A & B", u"A" },
665 { "en", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_NARROW, u"A, B, C", u"A, B", u"A" },
666 { "en", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_WIDE, u"A, B, or C", u"A or B", u"A" },
667 { "en", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_SHORT, u"A, B, or C", u"A or B", u"A" },
668 { "en", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_NARROW, u"A, B, or C", u"A or B", u"A" },
669 { "en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_WIDE, u"A, B, C", u"A, B", u"A" },
670 { "en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, u"A, B, C", u"A, B", u"A" },
671 { "en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_NARROW, u"A B C", u"A B", u"A" },
672 };
673 for (auto cas : cases) {
674 LocalPointer<ListFormatter> fmt(
675 ListFormatter::createInstance(cas.locale, cas.type, cas.width, status),
676 status);
677 if (status.errIfFailureAndReset()) {
678 continue;
679 }
680 UnicodeString message = UnicodeString(u"TestCreateStyled loc=")
681 + cas.locale + u" type="
682 + Int64ToUnicodeString(cas.type) + u" width="
683 + Int64ToUnicodeString(cas.width);
684 const UnicodeString inputs3[] = {
685 u"A",
686 u"B",
687 u"C"
688 };
689 FormattedList result = fmt->formatStringsToValue(inputs3, UPRV_LENGTHOF(inputs3), status);
690 assertEquals(message, cas.expected3, result.toTempString(status));
691 const UnicodeString inputs2[] = {
692 u"A",
693 u"B"
694 };
695 result = fmt->formatStringsToValue(inputs2, UPRV_LENGTHOF(inputs2), status);
696 assertEquals(message, cas.expected2, result.toTempString(status));
697 const UnicodeString inputs1[] = {
698 u"A"
699 };
700 result = fmt->formatStringsToValue(inputs1, UPRV_LENGTHOF(inputs1), status);
701 assertEquals(message, cas.expected1, result.toTempString(status));
702 }
703 }
704
TestContextual()705 void ListFormatterTest::TestContextual() {
706 IcuTestErrorCode status(*this, "TestContextual");
707 std::vector<std::string> es = { "es", "es_419" , "es_PY", "es_DO" };
708 std::vector<std::string> he = { "he", "he_IL", "iw", "iw_IL" };
709 UListFormatterWidth widths [] = {
710 ULISTFMT_WIDTH_WIDE, ULISTFMT_WIDTH_SHORT, ULISTFMT_WIDTH_NARROW
711 };
712 struct TestCase {
713 std::vector<std::string> locales;
714 UListFormatterType type;
715 const char16_t* expected;
716 const char16_t* data1;
717 const char16_t* data2;
718 const char16_t* data3;
719 } cases[] = {
720 { es, ULISTFMT_TYPE_AND, u"fascinante e increíblemente",
721 u"fascinante", u"increíblemente", nullptr },
722 { es, ULISTFMT_TYPE_AND, u"Comunicaciones Industriales e IIoT",
723 u"Comunicaciones Industriales", u"IIoT", nullptr },
724 { es, ULISTFMT_TYPE_AND, u"España e Italia", u"España", u"Italia", nullptr },
725 { es, ULISTFMT_TYPE_AND, u"hijas intrépidas e hijos solidarios",
726 u"hijas intrépidas", u"hijos solidarios", nullptr },
727 { es, ULISTFMT_TYPE_AND, u"a un hombre e hirieron a otro",
728 u"a un hombre", u"hirieron a otro", nullptr },
729 { es, ULISTFMT_TYPE_AND, u"hija e hijo", u"hija", u"hijo", nullptr },
730 { es, ULISTFMT_TYPE_AND, u"esposa, hija e hijo", u"esposa", u"hija", u"hijo" },
731 // For 'y' exception
732 { es, ULISTFMT_TYPE_AND, u"oro y hierro", u"oro", u"hierro", nullptr },
733 { es, ULISTFMT_TYPE_AND, u"agua y hielo", u"agua", u"hielo", nullptr },
734 { es, ULISTFMT_TYPE_AND, u"colágeno y hialurónico", u"colágeno", u"hialurónico", nullptr },
735
736 { es, ULISTFMT_TYPE_OR, u"desierto u oasis", u"desierto", u"oasis", nullptr },
737 { es, ULISTFMT_TYPE_OR, u"oasis, desierto u océano", u"oasis", u"desierto", u"océano" },
738 { es, ULISTFMT_TYPE_OR, u"7 u 8", u"7", u"8", nullptr },
739 { es, ULISTFMT_TYPE_OR, u"7 u 80", u"7", u"80", nullptr },
740 { es, ULISTFMT_TYPE_OR, u"7 u 800", u"7", u"800", nullptr },
741 { es, ULISTFMT_TYPE_OR, u"6, 7 u 8", u"6", u"7", u"8" },
742 { es, ULISTFMT_TYPE_OR, u"10 u 11", u"10", u"11", nullptr },
743 { es, ULISTFMT_TYPE_OR, u"10 o 111", u"10", u"111", nullptr },
744 { es, ULISTFMT_TYPE_OR, u"10 o 11.2", u"10", u"11.2", nullptr },
745 { es, ULISTFMT_TYPE_OR, u"9, 10 u 11", u"9", u"10", u"11" },
746
747 { he, ULISTFMT_TYPE_AND, u"a, b ו-c", u"a", u"b", u"c" },
748 { he, ULISTFMT_TYPE_AND, u"a ו-b", u"a", u"b", nullptr },
749 { he, ULISTFMT_TYPE_AND, u"1, 2 ו-3", u"1", u"2", u"3" },
750 { he, ULISTFMT_TYPE_AND, u"1 ו-2", u"1", u"2", nullptr },
751 { he, ULISTFMT_TYPE_AND, u"אהבה ומקווה", u"אהבה", u"מקווה", nullptr },
752 { he, ULISTFMT_TYPE_AND, u"אהבה, מקווה ואמונה", u"אהבה", u"מקווה", u"אמונה" },
753 };
754 for (auto width : widths) {
755 for (auto cas : cases) {
756 for (auto locale : cas.locales) {
757 LocalPointer<ListFormatter> fmt(
758 ListFormatter::createInstance(locale.c_str(), cas.type, width, status),
759 status);
760 if (status.errIfFailureAndReset()) {
761 continue;
762 }
763 UnicodeString message = UnicodeString(u"TestContextual loc=")
764 + locale.c_str() + u" type="
765 + Int64ToUnicodeString(cas.type) + u" width="
766 + Int64ToUnicodeString(width);
767 if (cas.data3 == nullptr) {
768 const UnicodeString inputs2[] = { cas.data1, cas.data2 };
769 FormattedList result = fmt->formatStringsToValue(inputs2, UPRV_LENGTHOF(inputs2), status);
770 assertEquals(message, cas.expected, result.toTempString(status));
771 } else {
772 const UnicodeString inputs3[] = { cas.data1, cas.data2, cas.data3 };
773 FormattedList result = fmt->formatStringsToValue(inputs3, UPRV_LENGTHOF(inputs3), status);
774 assertEquals(message, cas.expected, result.toTempString(status));
775 }
776 }
777 }
778 }
779 }
780
TestNextPosition()781 void ListFormatterTest::TestNextPosition() {
782 IcuTestErrorCode status(*this, "TestNextPosition");
783 std::vector<std::string> locales = { "en", "es", "zh", "ja" };
784 UListFormatterWidth widths [] = {
785 ULISTFMT_WIDTH_WIDE, ULISTFMT_WIDTH_SHORT, ULISTFMT_WIDTH_NARROW
786 };
787 const char* widthStr [] = {"wide", "short", "narrow"};
788 UListFormatterType types [] = {
789 ULISTFMT_TYPE_AND, ULISTFMT_TYPE_OR, ULISTFMT_TYPE_UNITS
790 };
791 const char* typeStr [] = {"and", "or", "units"};
792 const UnicodeString inputs[] = { u"A1", u"B2", u"C3", u"D4" };
793 for (auto width : widths) {
794 for (auto type : types) {
795 for (auto locale : locales) {
796 LocalPointer<ListFormatter> fmt(
797 ListFormatter::createInstance(locale.c_str(), type, width, status),
798 status);
799 if (status.errIfFailureAndReset()) {
800 continue;
801 }
802 for (int32_t n = 1; n <= UPRV_LENGTHOF(inputs); n++) {
803 FormattedList result = fmt->formatStringsToValue(
804 inputs, n, status);
805 int32_t elements = 0;
806 icu::ConstrainedFieldPosition cfpos;
807 cfpos.constrainCategory(UFIELD_CATEGORY_LIST);
808 while (result.nextPosition(cfpos, status) && U_SUCCESS(status)) {
809 if (cfpos.getField() == ULISTFMT_ELEMENT_FIELD) {
810 elements++;
811 }
812 }
813 std::string msg = locale;
814 // Test that if there are n elements (n=1..4) in the input, then the
815 // nextPosition() should iterate through exactly n times
816 // with field == ULISTFMT_ELEMENT_FIELD.
817 assertEquals((msg
818 .append(" w=").append(widthStr[width])
819 .append(" t=").append(typeStr[type])).c_str(),
820 n, elements);
821 }
822 }
823 }
824 }
825 }
826
TestInt32Overflow()827 void ListFormatterTest::TestInt32Overflow() {
828 if (quick) {
829 return;
830 }
831 IcuTestErrorCode status(*this, "TestInt32Overflow");
832 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("en", status), status);
833 std::vector<UnicodeString> inputs;
834 UnicodeString input(0xAAAFF00, 0x00000042, 0xAAAFF00);
835 for (int32_t i = 0; i < 16; i++) {
836 inputs.push_back(input);
837 }
838 FormattedList result = fmt->formatStringsToValue(
839 inputs.data(), static_cast<int32_t>(inputs.size()), status);
840 status.expectErrorAndReset(U_INPUT_TOO_LONG_ERROR);
841 }
842
843 #endif /* #if !UCONFIG_NO_FORMATTING */
844