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(Test21871);
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(ListFormatter::createInstance(
280 Locale("ur", "IN"), // in CLDR 41 alpha1 the "backwards order" patterns in this and other locales were removed.
281 ULISTFMT_TYPE_UNITS,
282 ULISTFMT_WIDTH_NARROW,
283 errorCode));
284 if (U_FAILURE(errorCode)) {
285 dataerrln(
286 "ListFormatter::createInstance(Locale(\"ur\", \"IN\"), \"unit-narrow\", errorCode) failed in "
287 "%s: %s", testName, u_errorName(errorCode));
288 return;
289 }
290 RunTestFieldPositionIteratorWithFormatter(
291 formatter.getAlias(),
292 data, n, expected, tupleCount, expectedFormatted, testName);
293 }
294
RunTestFieldPositionIteratorWithNItems(UnicodeString data[],int32_t n,int32_t expected[],int32_t tupleCount,const char16_t * expectedFormatted,const char * testName)295 void ListFormatterTest::RunTestFieldPositionIteratorWithNItems(
296 UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount,
297 const char16_t *expectedFormatted,
298 const char* testName) {
299 IcuTestErrorCode errorCode(*this, testName);
300 LocalPointer<ListFormatter> formatter(
301 ListFormatter::createInstance(Locale("en"), errorCode));
302 if (U_FAILURE(errorCode)) {
303 dataerrln(
304 "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in "
305 "%s: %s", testName, u_errorName(errorCode));
306 return;
307 }
308 RunTestFieldPositionIteratorWithFormatter(
309 formatter.getAlias(),
310 data, n, expected, tupleCount, expectedFormatted, testName);
311 }
312
TestFieldPositionIteratorWith3Items()313 void ListFormatterTest::TestFieldPositionIteratorWith3Items() {
314 // 0 1
315 // 012345678901234
316 // "a, bbb, and cc"
317 UnicodeString data[3] = {"a", "bbb", "cc"};
318 int32_t expected[] = {
319 ULISTFMT_ELEMENT_FIELD, 0, 1,
320 ULISTFMT_LITERAL_FIELD, 1, 3,
321 ULISTFMT_ELEMENT_FIELD, 3, 6,
322 ULISTFMT_LITERAL_FIELD, 6, 12,
323 ULISTFMT_ELEMENT_FIELD, 12, 14
324 };
325 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
326 RunTestFieldPositionIteratorWithNItems(
327 data, 3, expected, tupleCount,
328 u"a, bbb, and cc",
329 "TestFieldPositionIteratorWith3Items");
330 }
331
TestFieldPositionIteratorWith3ItemsPatternShift()332 void ListFormatterTest::TestFieldPositionIteratorWith3ItemsPatternShift() {
333 // Note: In CLDR 41 alpha1 the "backwards order" patterns in ur_IN (and one or two
334 // other locales) were removed, ur_IN now just inherits list patterns from ur.
335 // So this test may no longer be interesting.
336 UnicodeString data[3] = {"a", "bbb", "cc"};
337 int32_t expected[] = {
338 ULISTFMT_ELEMENT_FIELD, 0, 1,
339 ULISTFMT_LITERAL_FIELD, 1, 3,
340 ULISTFMT_ELEMENT_FIELD, 3, 6,
341 ULISTFMT_LITERAL_FIELD, 6, 12,
342 ULISTFMT_ELEMENT_FIELD, 12, 14
343 };
344 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
345 RunTestFieldPositionIteratorWithNItemsPatternShift(
346 data, 3, expected, tupleCount,
347 u"a، bbb، اور cc",
348 "TestFieldPositionIteratorWith3ItemsPatternShift");
349 }
350
TestFieldPositionIteratorWith2Items()351 void ListFormatterTest::TestFieldPositionIteratorWith2Items() {
352 // 0 1
353 // 01234567890
354 // "bbb and cc"
355 UnicodeString data[2] = {"bbb", "cc"};
356 int32_t expected[] = {
357 ULISTFMT_ELEMENT_FIELD, 0, 3,
358 ULISTFMT_LITERAL_FIELD, 3, 8,
359 ULISTFMT_ELEMENT_FIELD, 8, 10
360 };
361 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
362 RunTestFieldPositionIteratorWithNItems(
363 data, 2, expected, tupleCount,
364 u"bbb and cc",
365 "TestFieldPositionIteratorWith2Items");
366 }
367
TestFieldPositionIteratorWith2ItemsPatternShift()368 void ListFormatterTest::TestFieldPositionIteratorWith2ItemsPatternShift() {
369 // Note: In CLDR 41 alpha1 the "backwards order" patterns in ur_IN (and one or two
370 // other locales) were removed, ur_IN now just inherits list patterns from ur.
371 // So this test may no longer be interesting.
372 UnicodeString data[2] = {"bbb", "cc"};
373 int32_t expected[] = {
374 ULISTFMT_ELEMENT_FIELD, 0, 3,
375 ULISTFMT_LITERAL_FIELD, 3, 8,
376 ULISTFMT_ELEMENT_FIELD, 8, 10
377 };
378 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
379 RunTestFieldPositionIteratorWithNItemsPatternShift(
380 data, 2, expected, tupleCount,
381 u"bbb اور cc",
382 "TestFieldPositionIteratorWith2ItemsPatternShift");
383 }
384
TestFieldPositionIteratorWith1Item()385 void ListFormatterTest::TestFieldPositionIteratorWith1Item() {
386 // 012
387 // "cc"
388 UnicodeString data[1] = {"cc"};
389 int32_t expected[] = {
390 ULISTFMT_ELEMENT_FIELD, 0, 2
391 };
392 int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
393 RunTestFieldPositionIteratorWithNItems(
394 data, 1, expected, tupleCount,
395 u"cc",
396 "TestFieldPositionIteratorWith1Item");
397 }
398
399 // Tests resource loading and inheritance when region sublocale
400 // has only partial data for the listPattern element (overriding
401 // some of the parent data). #12994
TestNynorsk()402 void ListFormatterTest::TestNynorsk() {
403 UnicodeString results[4] = {
404 one,
405 one + " og " + two,
406 one + ", " + two + " og " + three,
407 one + ", " + two + ", " + three + " og " + four
408 };
409
410 CheckFourCases("nn", one, two, three, four, results, "TestNynorsk()");
411 }
412
413 // Tests resource loading and inheritance when region sublocale
414 // has only partial data for the listPattern element (overriding
415 // some of the parent data). #12994
TestChineseTradHK()416 void ListFormatterTest::TestChineseTradHK() {
417 UnicodeString and_string = UnicodeString("\\u53CA", -1, US_INV).unescape();
418 UnicodeString comma_string = UnicodeString("\\u3001", -1, US_INV).unescape();
419 UnicodeString results[4] = {
420 one,
421 one + and_string + two,
422 one + comma_string + two + and_string + three,
423 one + comma_string + two + comma_string + three + and_string + four
424 };
425
426 CheckFourCases("zh_Hant_HK", one, two, three, four, results, "TestChineseTradHK()");
427 }
428
429 // Formatting in Russian.
430 // "\\u0438" is used before the last element, and all elements up to (but not including) the penultimate are followed by a comma.
TestRussian()431 void ListFormatterTest::TestRussian() {
432 UnicodeString and_string = UnicodeString(" \\u0438 ", -1, US_INV).unescape();
433 UnicodeString results[4] = {
434 one,
435 one + and_string + two,
436 one + ", " + two + and_string + three,
437 one + ", " + two + ", " + three + and_string + four
438 };
439
440 CheckFourCases("ru", one, two, three, four, results, "TestRussian()");
441 }
442
443 // Formatting in Malayalam.
444 // For two elements, "\\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46" is inserted in between.
445 // For more than two elements, comma is inserted between all elements up to (and including) the penultimate,
446 // and the word \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35 is inserted in the end.
TestMalayalam()447 void ListFormatterTest::TestMalayalam() {
448 UnicodeString pair_string = UnicodeString(" \\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46 ", -1, US_INV).unescape();
449 UnicodeString total_string = UnicodeString(" \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35", -1, US_INV).unescape();
450 UnicodeString results[4] = {
451 one,
452 one + pair_string + two,
453 one + ", " + two + ", " + three + total_string,
454 one + ", " + two + ", " + three + ", " + four + total_string
455 };
456
457 CheckFourCases("ml", one, two, three, four, results, "TestMalayalam()");
458 }
459
460 // Formatting in Zulu.
461 // "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
TestZulu()462 void ListFormatterTest::TestZulu() {
463 UnicodeString results[4] = {
464 one,
465 one + " ne-" + two,
466 one + ", " + two + ", ne-" + three,
467 one + ", " + two + ", " + three + ", ne-" + four
468 };
469
470 CheckFourCases("zu", one, two, three, four, results, "TestZulu()");
471 }
472
TestOutOfOrderPatterns()473 void ListFormatterTest::TestOutOfOrderPatterns() {
474 UnicodeString results[4] = {
475 one,
476 two + " after " + one,
477 three + " in the last after " + two + " after the first " + one,
478 four + " in the last after " + three + " after " + two + " after the first " + one
479 };
480
481 IcuTestErrorCode errorCode(*this, "TestOutOfOrderPatterns()");
482 Locale locale("en");
483 ListFormatData data("{1} after {0}", "{1} after the first {0}",
484 "{1} after {0}", "{1} in the last after {0}", locale);
485 ListFormatter formatter(data, errorCode);
486
487 UnicodeString input1[] = {one};
488 CheckFormatting(&formatter, input1, 1, results[0], "TestOutOfOrderPatterns()");
489
490 UnicodeString input2[] = {one, two};
491 CheckFormatting(&formatter, input2, 2, results[1], "TestOutOfOrderPatterns()");
492
493 UnicodeString input3[] = {one, two, three};
494 CheckFormatting(&formatter, input3, 3, results[2], "TestOutOfOrderPatterns()");
495
496 UnicodeString input4[] = {one, two, three, four};
497 CheckFormatting(&formatter, input4, 4, results[3], "TestOutOfOrderPatterns()");
498 }
499
TestFormattedValue()500 void ListFormatterTest::TestFormattedValue() {
501 IcuTestErrorCode status(*this, "TestFormattedValue");
502
503 {
504 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("en", status));
505 if (status.errIfFailureAndReset()) { return; }
506 const char16_t* message = u"Field position test 1";
507 const char16_t* expectedString = u"hello, wonderful, and world";
508 const UnicodeString inputs[] = {
509 u"hello",
510 u"wonderful",
511 u"world"
512 };
513 FormattedList result = fmt->formatStringsToValue(inputs, UPRV_LENGTHOF(inputs), status);
514 static const UFieldPositionWithCategory expectedFieldPositions[] = {
515 // field, begin index, end index
516 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 5},
517 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 5},
518 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 5, 7},
519 {UFIELD_CATEGORY_LIST_SPAN, 1, 7, 16},
520 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 7, 16},
521 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 16, 22},
522 {UFIELD_CATEGORY_LIST_SPAN, 2, 22, 27},
523 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 22, 27}};
524 checkMixedFormattedValue(
525 message,
526 result,
527 expectedString,
528 expectedFieldPositions,
529 UPRV_LENGTHOF(expectedFieldPositions));
530 }
531
532 {
533 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("zh", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, status));
534 if (status.errIfFailureAndReset()) { return; }
535 const char16_t* message = u"Field position test 2 (ICU-21340)";
536 const char16_t* expectedString = u"aabbbbbbbccc";
537 const UnicodeString inputs[] = {
538 u"aa",
539 u"bbbbbbb",
540 u"ccc"
541 };
542 FormattedList result = fmt->formatStringsToValue(inputs, UPRV_LENGTHOF(inputs), status);
543 static const UFieldPositionWithCategory expectedFieldPositions[] = {
544 // field, begin index, end index
545 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 2},
546 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 2},
547 {UFIELD_CATEGORY_LIST_SPAN, 1, 2, 9},
548 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 2, 9},
549 {UFIELD_CATEGORY_LIST_SPAN, 2, 9, 12},
550 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 9, 12}};
551 checkMixedFormattedValue(
552 message,
553 result,
554 expectedString,
555 expectedFieldPositions,
556 UPRV_LENGTHOF(expectedFieldPositions));
557 }
558
559 {
560 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, status));
561 if (status.errIfFailureAndReset()) { return; }
562 const char16_t* message = u"ICU-21383 Long list";
563 const char16_t* expectedString = u"a, b, c, d, e, f, g, h, i";
564 const UnicodeString inputs[] = {
565 u"a",
566 u"b",
567 u"c",
568 u"d",
569 u"e",
570 u"f",
571 u"g",
572 u"h",
573 u"i",
574 };
575 FormattedList result = fmt->formatStringsToValue(inputs, UPRV_LENGTHOF(inputs), status);
576 static const UFieldPositionWithCategory expectedFieldPositions[] = {
577 // field, begin index, end index
578 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 1},
579 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 1},
580 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 1, 3},
581 {UFIELD_CATEGORY_LIST_SPAN, 1, 3, 4},
582 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 3, 4},
583 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 4, 6},
584 {UFIELD_CATEGORY_LIST_SPAN, 2, 6, 7},
585 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 6, 7},
586 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 7, 9},
587 {UFIELD_CATEGORY_LIST_SPAN, 3, 9, 10},
588 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 9, 10},
589 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 10, 12},
590 {UFIELD_CATEGORY_LIST_SPAN, 4, 12, 13},
591 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 12, 13},
592 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 13, 15},
593 {UFIELD_CATEGORY_LIST_SPAN, 5, 15, 16},
594 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 15, 16},
595 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 16, 18},
596 {UFIELD_CATEGORY_LIST_SPAN, 6, 18, 19},
597 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 18, 19},
598 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 19, 21},
599 {UFIELD_CATEGORY_LIST_SPAN, 7, 21, 22},
600 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 21, 22},
601 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 22, 24},
602 {UFIELD_CATEGORY_LIST_SPAN, 8, 24, 25},
603 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 24, 25},
604 };
605 checkMixedFormattedValue(
606 message,
607 result,
608 expectedString,
609 expectedFieldPositions,
610 UPRV_LENGTHOF(expectedFieldPositions));
611 }
612 }
613
DoTheRealListStyleTesting(Locale locale,UnicodeString items[],int itemCount,UListFormatterType type,UListFormatterWidth width,const char * expected,IcuTestErrorCode status)614 void ListFormatterTest::DoTheRealListStyleTesting(
615 Locale locale,
616 UnicodeString items[],
617 int itemCount,
618 UListFormatterType type,
619 UListFormatterWidth width,
620 const char* expected,
621 IcuTestErrorCode status) {
622
623 LocalPointer<ListFormatter> formatter(
624 ListFormatter::createInstance(locale, type, width, status));
625
626 UnicodeString actualResult;
627 formatter->format(items, itemCount, actualResult, status);
628 assertEquals(Int64ToUnicodeString(type) + "-" + Int64ToUnicodeString(width),
629 UnicodeString(expected), actualResult);
630 }
631
TestDifferentStyles()632 void ListFormatterTest::TestDifferentStyles() {
633 Locale locale("fr");
634 UnicodeString input[4] = { u"rouge", u"jaune", u"bleu", u"vert" };
635 IcuTestErrorCode status(*this, "TestDifferentStyles()");
636
637 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu et vert", status);
638 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu ou vert", status);
639 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_WIDE, "rouge, jaune, bleu et vert", status);
640 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_NARROW, "rouge jaune bleu vert", status);
641 DoTheRealListStyleTesting(locale, input, 4, ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, "rouge, jaune, bleu et vert", status);
642 }
643
TestCreateStyled()644 void ListFormatterTest::TestCreateStyled() {
645 IcuTestErrorCode status(*this, "TestCreateStyled");
646 // Locale en has interesting data
647 struct TestCase {
648 const char* locale;
649 UListFormatterType type;
650 UListFormatterWidth width;
651 const char16_t* expected3;
652 const char16_t* expected2;
653 const char16_t* expected1;
654 } cases[] = {
655 { "pt", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_WIDE, u"A, B e C", u"A e B", u"A" },
656 { "pt", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_SHORT, u"A, B e C", u"A e B", u"A" },
657 { "pt", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_NARROW, u"A, B, C", u"A, B", u"A" },
658 { "pt", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_WIDE, u"A, B ou C", u"A ou B", u"A" },
659 { "pt", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_SHORT, u"A, B ou C", u"A ou B", u"A" },
660 { "pt", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_NARROW, u"A, B ou C", u"A ou B", u"A" },
661 { "pt", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_WIDE, u"A, B e C", u"A e B", u"A" },
662 { "pt", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, u"A, B e C", u"A e B", u"A" },
663 { "pt", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_NARROW, u"A B C", u"A B", u"A" },
664 { "en", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_WIDE, u"A, B, and C", u"A and B", u"A" },
665 { "en", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_SHORT, u"A, B, & C", u"A & B", u"A" },
666 { "en", ULISTFMT_TYPE_AND, ULISTFMT_WIDTH_NARROW, u"A, B, C", u"A, B", u"A" },
667 { "en", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_WIDE, u"A, B, or C", u"A or B", u"A" },
668 { "en", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_SHORT, u"A, B, or C", u"A or B", u"A" },
669 { "en", ULISTFMT_TYPE_OR, ULISTFMT_WIDTH_NARROW, u"A, B, or C", u"A or B", u"A" },
670 { "en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_WIDE, u"A, B, C", u"A, B", u"A" },
671 { "en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_SHORT, u"A, B, C", u"A, B", u"A" },
672 { "en", ULISTFMT_TYPE_UNITS, ULISTFMT_WIDTH_NARROW, u"A B C", u"A B", u"A" },
673 };
674 for (auto cas : cases) {
675 LocalPointer<ListFormatter> fmt(
676 ListFormatter::createInstance(cas.locale, cas.type, cas.width, status),
677 status);
678 if (status.errIfFailureAndReset()) {
679 continue;
680 }
681 UnicodeString message = UnicodeString(u"TestCreateStyled loc=")
682 + cas.locale + u" type="
683 + Int64ToUnicodeString(cas.type) + u" width="
684 + Int64ToUnicodeString(cas.width);
685 const UnicodeString inputs3[] = {
686 u"A",
687 u"B",
688 u"C"
689 };
690 FormattedList result = fmt->formatStringsToValue(inputs3, UPRV_LENGTHOF(inputs3), status);
691 assertEquals(message, cas.expected3, result.toTempString(status));
692 const UnicodeString inputs2[] = {
693 u"A",
694 u"B"
695 };
696 result = fmt->formatStringsToValue(inputs2, UPRV_LENGTHOF(inputs2), status);
697 assertEquals(message, cas.expected2, result.toTempString(status));
698 const UnicodeString inputs1[] = {
699 u"A"
700 };
701 result = fmt->formatStringsToValue(inputs1, UPRV_LENGTHOF(inputs1), status);
702 assertEquals(message, cas.expected1, result.toTempString(status));
703 }
704 }
705
TestContextual()706 void ListFormatterTest::TestContextual() {
707 IcuTestErrorCode status(*this, "TestContextual");
708 std::vector<std::string> es = { "es", "es_419" , "es_PY", "es_DO" };
709 std::vector<std::string> he = { "he", "he_IL", "iw", "iw_IL" };
710 UListFormatterWidth widths [] = {
711 ULISTFMT_WIDTH_WIDE, ULISTFMT_WIDTH_SHORT, ULISTFMT_WIDTH_NARROW
712 };
713 struct TestCase {
714 std::vector<std::string> locales;
715 UListFormatterType type;
716 const char16_t* expected;
717 const char16_t* data1;
718 const char16_t* data2;
719 const char16_t* data3;
720 } cases[] = {
721 { es, ULISTFMT_TYPE_AND, u"fascinante e increíblemente",
722 u"fascinante", u"increíblemente", nullptr },
723 { es, ULISTFMT_TYPE_AND, u"Comunicaciones Industriales e IIoT",
724 u"Comunicaciones Industriales", u"IIoT", nullptr },
725 { es, ULISTFMT_TYPE_AND, u"España e Italia", u"España", u"Italia", nullptr },
726 { es, ULISTFMT_TYPE_AND, u"hijas intrépidas e hijos solidarios",
727 u"hijas intrépidas", u"hijos solidarios", nullptr },
728 { es, ULISTFMT_TYPE_AND, u"a un hombre e hirieron a otro",
729 u"a un hombre", u"hirieron a otro", nullptr },
730 { es, ULISTFMT_TYPE_AND, u"hija e hijo", u"hija", u"hijo", nullptr },
731 { es, ULISTFMT_TYPE_AND, u"esposa, hija e hijo", u"esposa", u"hija", u"hijo" },
732 // For 'y' exception
733 { es, ULISTFMT_TYPE_AND, u"oro y hierro", u"oro", u"hierro", nullptr },
734 { es, ULISTFMT_TYPE_AND, u"agua y hielo", u"agua", u"hielo", nullptr },
735 { es, ULISTFMT_TYPE_AND, u"colágeno y hialurónico", u"colágeno", u"hialurónico", nullptr },
736
737 { es, ULISTFMT_TYPE_OR, u"desierto u oasis", u"desierto", u"oasis", nullptr },
738 { es, ULISTFMT_TYPE_OR, u"oasis, desierto u océano", u"oasis", u"desierto", u"océano" },
739 { es, ULISTFMT_TYPE_OR, u"7 u 8", u"7", u"8", nullptr },
740 { es, ULISTFMT_TYPE_OR, u"7 u 80", u"7", u"80", nullptr },
741 { es, ULISTFMT_TYPE_OR, u"7 u 800", u"7", u"800", nullptr },
742 { es, ULISTFMT_TYPE_OR, u"6, 7 u 8", u"6", u"7", u"8" },
743 { es, ULISTFMT_TYPE_OR, u"10 u 11", u"10", u"11", nullptr },
744 { es, ULISTFMT_TYPE_OR, u"10 o 111", u"10", u"111", nullptr },
745 { es, ULISTFMT_TYPE_OR, u"10 o 11.2", u"10", u"11.2", nullptr },
746 { es, ULISTFMT_TYPE_OR, u"9, 10 u 11", u"9", u"10", u"11" },
747
748 { he, ULISTFMT_TYPE_AND, u"a, b ו-c", u"a", u"b", u"c" },
749 { he, ULISTFMT_TYPE_AND, u"a ו-b", u"a", u"b", nullptr },
750 { he, ULISTFMT_TYPE_AND, u"1, 2 ו-3", u"1", u"2", u"3" },
751 { he, ULISTFMT_TYPE_AND, u"1 ו-2", u"1", u"2", nullptr },
752 { he, ULISTFMT_TYPE_AND, u"אהבה ומקווה", u"אהבה", u"מקווה", nullptr },
753 { he, ULISTFMT_TYPE_AND, u"אהבה, מקווה ואמונה", u"אהבה", u"מקווה", u"אמונה" },
754 };
755 for (auto width : widths) {
756 for (auto cas : cases) {
757 for (auto locale : cas.locales) {
758 LocalPointer<ListFormatter> fmt(
759 ListFormatter::createInstance(locale.c_str(), cas.type, width, status),
760 status);
761 if (status.errIfFailureAndReset()) {
762 continue;
763 }
764 UnicodeString message = UnicodeString(u"TestContextual loc=")
765 + locale.c_str() + u" type="
766 + Int64ToUnicodeString(cas.type) + u" width="
767 + Int64ToUnicodeString(width);
768 if (cas.data3 == nullptr) {
769 const UnicodeString inputs2[] = { cas.data1, cas.data2 };
770 FormattedList result = fmt->formatStringsToValue(inputs2, UPRV_LENGTHOF(inputs2), status);
771 assertEquals(message, cas.expected, result.toTempString(status));
772 } else {
773 const UnicodeString inputs3[] = { cas.data1, cas.data2, cas.data3 };
774 FormattedList result = fmt->formatStringsToValue(inputs3, UPRV_LENGTHOF(inputs3), status);
775 assertEquals(message, cas.expected, result.toTempString(status));
776 }
777 }
778 }
779 }
780 }
781
TestNextPosition()782 void ListFormatterTest::TestNextPosition() {
783 IcuTestErrorCode status(*this, "TestNextPosition");
784 std::vector<std::string> locales = { "en", "es", "zh", "ja" };
785 UListFormatterWidth widths [] = {
786 ULISTFMT_WIDTH_WIDE, ULISTFMT_WIDTH_SHORT, ULISTFMT_WIDTH_NARROW
787 };
788 const char* widthStr [] = {"wide", "short", "narrow"};
789 UListFormatterType types [] = {
790 ULISTFMT_TYPE_AND, ULISTFMT_TYPE_OR, ULISTFMT_TYPE_UNITS
791 };
792 const char* typeStr [] = {"and", "or", "units"};
793 const UnicodeString inputs[] = { u"A1", u"B2", u"C3", u"D4" };
794 for (auto width : widths) {
795 for (auto type : types) {
796 for (auto locale : locales) {
797 LocalPointer<ListFormatter> fmt(
798 ListFormatter::createInstance(locale.c_str(), type, width, status),
799 status);
800 if (status.errIfFailureAndReset()) {
801 continue;
802 }
803 for (int32_t n = 1; n <= UPRV_LENGTHOF(inputs); n++) {
804 FormattedList result = fmt->formatStringsToValue(
805 inputs, n, status);
806 int32_t elements = 0;
807 icu::ConstrainedFieldPosition cfpos;
808 cfpos.constrainCategory(UFIELD_CATEGORY_LIST);
809 while (result.nextPosition(cfpos, status) && U_SUCCESS(status)) {
810 if (cfpos.getField() == ULISTFMT_ELEMENT_FIELD) {
811 elements++;
812 }
813 }
814 std::string msg = locale;
815 // Test that if there are n elements (n=1..4) in the input, then the
816 // nextPosition() should iterate through exactly n times
817 // with field == ULISTFMT_ELEMENT_FIELD.
818 assertEquals((msg
819 .append(" w=").append(widthStr[width])
820 .append(" t=").append(typeStr[type])).c_str(),
821 n, elements);
822 }
823 }
824 }
825 }
826 }
827
TestInt32Overflow()828 void ListFormatterTest::TestInt32Overflow() {
829 if (quick) {
830 return;
831 }
832 IcuTestErrorCode status(*this, "TestInt32Overflow");
833 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("en", status), status);
834 std::vector<UnicodeString> inputs;
835 UnicodeString input(0xAAAFF00, 0x00000042, 0xAAAFF00);
836 for (int32_t i = 0; i < 16; i++) {
837 inputs.push_back(input);
838 }
839 FormattedList result = fmt->formatStringsToValue(
840 inputs.data(), static_cast<int32_t>(inputs.size()), status);
841 status.expectErrorAndReset(U_INPUT_TOO_LONG_ERROR);
842 }
843
844
Test21871()845 void ListFormatterTest::Test21871() {
846 IcuTestErrorCode status(*this, "Test21871");
847 LocalPointer<ListFormatter> fmt(ListFormatter::createInstance("en", status), status);
848 {
849 UnicodeString strings[] = {{u"A"}, {u""}};
850 FormattedList result = fmt->formatStringsToValue(strings, 2, status);
851 ConstrainedFieldPosition cfp;
852 cfp.constrainField(UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD);
853 assertTrue("nextPosition 1", result.nextPosition(cfp, status));
854 assertEquals("start", cfp.getStart(), 0);
855 assertEquals("limit", cfp.getLimit(), 1);
856 assertFalse("nextPosition 2", result.nextPosition(cfp, status));
857 }
858 {
859 UnicodeString strings[] = {{u""}, {u"B"}};
860 FormattedList result = fmt->formatStringsToValue(strings, 2, status);
861 ConstrainedFieldPosition cfp;
862 cfp.constrainField(UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD);
863 assertTrue("nextPosition 1", result.nextPosition(cfp, status));
864 assertEquals("start", cfp.getStart(), 5);
865 assertEquals("limit", cfp.getLimit(), 6);
866 assertFalse("nextPosition 2", result.nextPosition(cfp, status));
867 }
868 }
869
870 #endif /* #if !UCONFIG_NO_FORMATTING */
871