]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/ulistfmttest.c
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / ulistfmttest.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 2015, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ********************************************************************/
7 /* C API TEST for UListFormatter */
8
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_FORMATTING
12
13 #include "unicode/ustring.h"
14 #include "unicode/ulistformatter.h"
15 #include "cintltst.h"
16 #include "cmemory.h"
17 #include "cstring.h"
18 #include "cformtst.h"
19
20 static void TestUListFmt(void);
21 static void TestUListFmtToValue(void);
22
23 void addUListFmtTest(TestNode** root);
24
25 #define TESTCASE(x) addTest(root, &x, "tsformat/ulistfmttest/" #x)
26
27 void addUListFmtTest(TestNode** root)
28 {
29 TESTCASE(TestUListFmt);
30 TESTCASE(TestUListFmtToValue);
31 }
32
33 static const UChar str0[] = { 0x41,0 }; /* "A" */
34 static const UChar str1[] = { 0x42,0x62,0 }; /* "Bb" */
35 static const UChar str2[] = { 0x43,0x63,0x63,0 }; /* "Ccc" */
36 static const UChar str3[] = { 0x44,0x64,0x64,0x64,0 }; /* "Dddd" */
37 static const UChar str4[] = { 0x45,0x65,0x65,0x65,0x65,0 }; /* "Eeeee" */
38 static const UChar* strings[] = { str0, str1, str2, str3, str4 };
39 static const int32_t stringLengths[] = { 1, 2, 3, 4, 5 };
40 static const int32_t stringLengthsNeg[] = { -1, -1, -1, -1, -1 };
41
42 typedef struct {
43 const char * locale;
44 int32_t stringCount;
45 const char *expectedResult; /* invariant chars + escaped Unicode */
46 } ListFmtTestEntry;
47
48 static ListFmtTestEntry listFmtTestEntries[] = {
49 /* locale stringCount expectedResult */
50 { "en" , 5, "A, Bb, Ccc, Dddd, and Eeeee" },
51 { "en" , 2, "A and Bb" },
52 { "de" , 5, "A, Bb, Ccc, Dddd und Eeeee" },
53 { "de" , 2, "A und Bb" },
54 { "ja" , 5, "A\\u3001Bb\\u3001Ccc\\u3001Dddd\\u3001Eeeee" },
55 { "ja" , 2, "A\\u3001Bb" },
56 { "zh" , 5, "A\\u3001Bb\\u3001Ccc\\u3001Dddd\\u548CEeeee" },
57 { "zh" , 2, "A\\u548CBb" },
58 { NULL , 0, NULL } /* terminator */
59 };
60
61 enum {
62 kUBufMax = 128,
63 kBBufMax = 256
64 };
65
66 static void TestUListFmt() {
67 const ListFmtTestEntry * lftep;
68 for (lftep = listFmtTestEntries; lftep->locale != NULL ; lftep++ ) {
69 UErrorCode status = U_ZERO_ERROR;
70 UListFormatter *listfmt = ulistfmt_open(lftep->locale, &status);
71 if ( U_FAILURE(status) ) {
72 log_data_err("ERROR: ulistfmt_open fails for locale %s, status %s\n", lftep->locale, u_errorName(status));
73 } else {
74 UChar ubufActual[kUBufMax];
75 int32_t ulenActual = ulistfmt_format(listfmt, strings, stringLengths, lftep->stringCount, ubufActual, kUBufMax, &status);
76 if ( U_FAILURE(status) ) {
77 log_err("ERROR: ulistfmt_format fails for locale %s count %d (real lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status));
78 } else {
79 UChar ubufExpected[kUBufMax];
80 int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax);
81 if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) {
82 log_err("ERROR: ulistfmt_format for locale %s count %d (real lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale,
83 lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected));
84 }
85 }
86 /* try again with all lengths -1 */
87 status = U_ZERO_ERROR;
88 ulenActual = ulistfmt_format(listfmt, strings, stringLengthsNeg, lftep->stringCount, ubufActual, kUBufMax, &status);
89 if ( U_FAILURE(status) ) {
90 log_err("ERROR: ulistfmt_format fails for locale %s count %d (-1 lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status));
91 } else {
92 UChar ubufExpected[kUBufMax];
93 int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax);
94 if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) {
95 log_err("ERROR: ulistfmt_format for locale %s count %d (-1 lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale,
96 lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected));
97 }
98 }
99 /* try again with NULL lengths */
100 status = U_ZERO_ERROR;
101 ulenActual = ulistfmt_format(listfmt, strings, NULL, lftep->stringCount, ubufActual, kUBufMax, &status);
102 if ( U_FAILURE(status) ) {
103 log_err("ERROR: ulistfmt_format fails for locale %s count %d (NULL lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status));
104 } else {
105 UChar ubufExpected[kUBufMax];
106 int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax);
107 if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) {
108 log_err("ERROR: ulistfmt_format for locale %s count %d (NULL lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale,
109 lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected));
110 }
111 }
112
113 /* try calls that should return error */
114 status = U_ZERO_ERROR;
115 ulenActual = ulistfmt_format(listfmt, NULL, NULL, lftep->stringCount, ubufActual, kUBufMax, &status);
116 if (status != U_ILLEGAL_ARGUMENT_ERROR || ulenActual > 0) {
117 log_err("ERROR: ulistfmt_format for locale %s count %d with NULL strings, expected U_ILLEGAL_ARGUMENT_ERROR, got %s, result %d\n", lftep->locale,
118 lftep->stringCount, u_errorName(status), ulenActual);
119 }
120 status = U_ZERO_ERROR;
121 ulenActual = ulistfmt_format(listfmt, strings, NULL, lftep->stringCount, NULL, kUBufMax, &status);
122 if (status != U_ILLEGAL_ARGUMENT_ERROR || ulenActual > 0) {
123 log_err("ERROR: ulistfmt_format for locale %s count %d with NULL result, expected U_ILLEGAL_ARGUMENT_ERROR, got %s, result %d\n", lftep->locale,
124 lftep->stringCount, u_errorName(status), ulenActual);
125 }
126
127 ulistfmt_close(listfmt);
128 }
129 }
130 }
131
132 static void TestUListFmtToValue() {
133 UErrorCode ec = U_ZERO_ERROR;
134 UListFormatter* fmt = ulistfmt_open("en", &ec);
135 UFormattedList* fl = ulistfmt_openResult(&ec);
136 assertSuccess("Opening", &ec);
137
138 {
139 const char* message = "Field position test 1";
140 const UChar* expectedString = u"hello, wonderful, and world";
141 const UChar* inputs[] = {
142 u"hello",
143 u"wonderful",
144 u"world"
145 };
146 ulistfmt_formatStringsToResult(fmt, inputs, NULL, UPRV_LENGTHOF(inputs), fl, &ec);
147 assertSuccess("Formatting", &ec);
148 static const UFieldPositionWithCategory expectedFieldPositions[] = {
149 // field, begin index, end index
150 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 5},
151 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 5},
152 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 5, 7},
153 {UFIELD_CATEGORY_LIST_SPAN, 1, 7, 16},
154 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 7, 16},
155 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 16, 22},
156 {UFIELD_CATEGORY_LIST_SPAN, 2, 22, 27},
157 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 22, 27}};
158 checkMixedFormattedValue(
159 message,
160 ulistfmt_resultAsValue(fl, &ec),
161 expectedString,
162 expectedFieldPositions,
163 UPRV_LENGTHOF(expectedFieldPositions));
164 }
165 {
166 const char* message = "Field position test 1";
167 const UChar* expectedString = u"A, B, C, D, E, F, and G";
168 const UChar* inputs[] = {
169 u"A",
170 u"B",
171 u"C",
172 u"D",
173 u"E",
174 u"F",
175 u"G"
176 };
177 ulistfmt_formatStringsToResult(fmt, inputs, NULL, UPRV_LENGTHOF(inputs), fl, &ec);
178 assertSuccess("Formatting", &ec);
179 static const UFieldPositionWithCategory expectedFieldPositions[] = {
180 // field, begin index, end index
181 {UFIELD_CATEGORY_LIST_SPAN, 0, 0, 1},
182 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 0, 1},
183 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 1, 3},
184 {UFIELD_CATEGORY_LIST_SPAN, 1, 3, 4},
185 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 3, 4},
186 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 4, 6},
187 {UFIELD_CATEGORY_LIST_SPAN, 2, 6, 7},
188 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 6, 7},
189 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 7, 9},
190 {UFIELD_CATEGORY_LIST_SPAN, 3, 9, 10},
191 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 9, 10},
192 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 10, 12},
193 {UFIELD_CATEGORY_LIST_SPAN, 4, 12, 13},
194 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 12, 13},
195 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 13, 15},
196 {UFIELD_CATEGORY_LIST_SPAN, 5, 15, 16},
197 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 15, 16},
198 {UFIELD_CATEGORY_LIST, ULISTFMT_LITERAL_FIELD, 16, 22},
199 {UFIELD_CATEGORY_LIST_SPAN, 6, 22, 23},
200 {UFIELD_CATEGORY_LIST, ULISTFMT_ELEMENT_FIELD, 22, 23}};
201 checkMixedFormattedValue(
202 message,
203 ulistfmt_resultAsValue(fl, &ec),
204 expectedString,
205 expectedFieldPositions,
206 UPRV_LENGTHOF(expectedFieldPositions));
207 }
208
209 ulistfmt_close(fmt);
210 ulistfmt_closeResult(fl);
211 }
212
213
214 #endif /* #if !UCONFIG_NO_FORMATTING */