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