]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/uformattedvaluetst.c
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / uformattedvaluetst.c
CommitLineData
3d1f044b
A
1// © 2018 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3
4#include "unicode/utypes.h"
5
6#if !UCONFIG_NO_FORMATTING
7
8#include "unicode/uformattedvalue.h"
9#include "unicode/unum.h"
10#include "unicode/ustring.h"
11#include "cformtst.h"
12#include "cintltst.h"
13#include "cmemory.h"
14#include "cstring.h"
15#include "uassert.h"
16
17static void TestBasic(void);
18static void TestSetters(void);
19
20static void AssertAllPartsEqual(
21 const char* messagePrefix,
22 const UConstrainedFieldPosition* ucfpos,
23 int32_t matching,
24 UFieldCategory category,
25 int32_t field,
26 int32_t start,
27 int32_t limit,
28 int64_t context);
29
30void addUFormattedValueTest(TestNode** root);
31
32#define TESTCASE(x) addTest(root, &x, "tsformat/uformattedvalue/" #x)
33
34void addUFormattedValueTest(TestNode** root) {
35 TESTCASE(TestBasic);
36 TESTCASE(TestSetters);
37}
38
39
40static void TestBasic() {
41 UErrorCode status = U_ZERO_ERROR;
42 UConstrainedFieldPosition* ucfpos = ucfpos_open(&status);
43 assertSuccess("opening ucfpos", &status);
44 assertTrue("ucfpos should not be null", ucfpos != NULL);
45
46 AssertAllPartsEqual(
47 "basic",
48 ucfpos,
49 7,
50 UFIELD_CATEGORY_UNDEFINED,
51 0,
52 0,
53 0,
54 0LL);
55
56 ucfpos_close(ucfpos);
57}
58
59void TestSetters() {
60 UErrorCode status = U_ZERO_ERROR;
61 UConstrainedFieldPosition* ucfpos = ucfpos_open(&status);
62 assertSuccess("opening ucfpos", &status);
63 assertTrue("ucfpos should not be null", ucfpos != NULL);
64
65 ucfpos_constrainCategory(ucfpos, UFIELD_CATEGORY_DATE, &status);
66 assertSuccess("setters 0", &status);
67 AssertAllPartsEqual(
68 "setters 0",
69 ucfpos,
70 4,
71 UFIELD_CATEGORY_DATE,
72 0,
73 0,
74 0,
75 0LL);
76
77 ucfpos_constrainField(ucfpos, UFIELD_CATEGORY_NUMBER, UNUM_COMPACT_FIELD, &status);
78 assertSuccess("setters 1", &status);
79 AssertAllPartsEqual(
80 "setters 1",
81 ucfpos,
82 2,
83 UFIELD_CATEGORY_NUMBER,
84 UNUM_COMPACT_FIELD,
85 0,
86 0,
87 0LL);
88
89 ucfpos_setInt64IterationContext(ucfpos, 42424242424242LL, &status);
90 assertSuccess("setters 2", &status);
91 AssertAllPartsEqual(
92 "setters 2",
93 ucfpos,
94 2,
95 UFIELD_CATEGORY_NUMBER,
96 UNUM_COMPACT_FIELD,
97 0,
98 0,
99 42424242424242LL);
100
101 ucfpos_setState(ucfpos, UFIELD_CATEGORY_NUMBER, UNUM_COMPACT_FIELD, 5, 10, &status);
102 assertSuccess("setters 3", &status);
103 AssertAllPartsEqual(
104 "setters 3",
105 ucfpos,
106 2,
107 UFIELD_CATEGORY_NUMBER,
108 UNUM_COMPACT_FIELD,
109 5,
110 10,
111 42424242424242LL);
112
113 ucfpos_reset(ucfpos, &status);
114 assertSuccess("setters 4", &status);
115 AssertAllPartsEqual(
116 "setters 4",
117 ucfpos,
118 7,
119 UFIELD_CATEGORY_UNDEFINED,
120 0,
121 0,
122 0,
123 0LL);
124
125 ucfpos_close(ucfpos);
126}
127
128/** For matching, turn on these bits:
129 *
130 * 1 = UNUM_INTEGER_FIELD
131 * 2 = UNUM_COMPACT_FIELD
132 * 4 = UDAT_AM_PM_FIELD
133 */
134static void AssertAllPartsEqual(
135 const char* messagePrefix,
136 const UConstrainedFieldPosition* ucfpos,
137 int32_t matching,
138 UFieldCategory category,
139 int32_t field,
140 int32_t start,
141 int32_t limit,
142 int64_t context) {
143
144 UErrorCode status = U_ZERO_ERROR;
145
146 char message[256];
147 uprv_strncpy(message, messagePrefix, 256);
148 int32_t prefixEnd = (int32_t)uprv_strlen(messagePrefix);
149 message[prefixEnd++] = ':';
150 message[prefixEnd++] = ' ';
151 U_ASSERT(prefixEnd < 256);
152
153#define AAPE_MSG(suffix) (uprv_strncpy(message+prefixEnd, suffix, 256-prefixEnd)-prefixEnd)
154
155 UFieldCategory _category = ucfpos_getCategory(ucfpos, &status);
156 assertSuccess(AAPE_MSG("_"), &status);
157 assertIntEquals(AAPE_MSG("category"), category, _category);
158
159 int32_t _field = ucfpos_getField(ucfpos, &status);
160 assertSuccess(AAPE_MSG("field"), &status);
161 assertIntEquals(AAPE_MSG("field"), field, _field);
162
163 int32_t _start, _limit;
164 ucfpos_getIndexes(ucfpos, &_start, &_limit, &status);
165 assertSuccess(AAPE_MSG("indexes"), &status);
166 assertIntEquals(AAPE_MSG("start"), start, _start);
167 assertIntEquals(AAPE_MSG("limit"), limit, _limit);
168
169 int64_t _context = ucfpos_getInt64IterationContext(ucfpos, &status);
170 assertSuccess(AAPE_MSG("context"), &status);
171 assertIntEquals(AAPE_MSG("context"), context, _context);
172
173 UBool _matchesInteger = ucfpos_matchesField(ucfpos, UFIELD_CATEGORY_NUMBER, UNUM_INTEGER_FIELD, &status);
174 assertSuccess(AAPE_MSG("integer field"), &status);
175 assertTrue(AAPE_MSG("integer field"),
176 ((matching & 1) != 0) ? _matchesInteger : !_matchesInteger);
177
178 UBool _matchesCompact = ucfpos_matchesField(ucfpos, UFIELD_CATEGORY_NUMBER, UNUM_COMPACT_FIELD, &status);
179 assertSuccess(AAPE_MSG("compact field"), &status);
180 assertTrue(AAPE_MSG("compact field"),
181 ((matching & 2) != 0) ? _matchesCompact : !_matchesCompact);
182
183 UBool _matchesDate = ucfpos_matchesField(ucfpos, UFIELD_CATEGORY_DATE, UDAT_AM_PM_FIELD, &status);
184 assertSuccess(AAPE_MSG("date field"), &status);
185 assertTrue(AAPE_MSG("date field"),
186 ((matching & 4) != 0) ? _matchesDate : !_matchesDate);
187}
188
189
190static void checkFormattedValueString(
191 const char* message,
192 const UFormattedValue* fv,
193 const UChar* expectedString,
194 UErrorCode* ec) {
195 int32_t length;
196 const UChar* actualString = ufmtval_getString(fv, &length, ec);
197 if (U_FAILURE(*ec)) {
198 assertIntEquals(message, 0, length);
199 return;
200 }
201 assertSuccess(message, ec);
202 // The string is guaranteed to be NUL-terminated.
203 int32_t actualLength = u_strlen(actualString);
204 assertIntEquals(message, actualLength, length);
205 assertUEquals(message, expectedString, actualString);
206}
207
208// Declared in cformtst.h
209void checkFormattedValue(
210 const char* message,
211 const UFormattedValue* fv,
212 const UChar* expectedString,
213 UFieldCategory expectedCategory,
214 const UFieldPosition* expectedFieldPositions,
215 int32_t expectedFieldPositionsLength) {
216 UErrorCode ec = U_ZERO_ERROR;
217 checkFormattedValueString(message, fv, expectedString, &ec);
218 if (U_FAILURE(ec)) { return; }
219
220 // Basic loop over the fields (more rigorous testing in C++)
221 UConstrainedFieldPosition* ucfpos = ucfpos_open(&ec);
222 int32_t i = 0;
223 while (ufmtval_nextPosition(fv, ucfpos, &ec)) {
224 assertIntEquals("category",
225 expectedCategory, ucfpos_getCategory(ucfpos, &ec));
226 assertIntEquals("field",
227 expectedFieldPositions[i].field, ucfpos_getField(ucfpos, &ec));
228 int32_t start, limit;
229 ucfpos_getIndexes(ucfpos, &start, &limit, &ec);
230 assertIntEquals("start",
231 expectedFieldPositions[i].beginIndex, start);
232 assertIntEquals("limit",
233 expectedFieldPositions[i].endIndex, limit);
234 i++;
235 }
236 assertTrue("After loop", !ufmtval_nextPosition(fv, ucfpos, &ec));
237 assertSuccess("After loop", &ec);
238 ucfpos_close(ucfpos);
239}
240
241void checkMixedFormattedValue(
242 const char* message,
243 const UFormattedValue* fv,
244 const UChar* expectedString,
245 const UFieldPositionWithCategory* expectedFieldPositions,
246 int32_t length) {
247 UErrorCode ec = U_ZERO_ERROR;
248 checkFormattedValueString(message, fv, expectedString, &ec);
249 if (U_FAILURE(ec)) { return; }
250
251 // Basic loop over the fields (more rigorous testing in C++)
252 UConstrainedFieldPosition* ucfpos = ucfpos_open(&ec);
253 int32_t i = 0;
254 while (ufmtval_nextPosition(fv, ucfpos, &ec)) {
255 assertIntEquals("category",
256 expectedFieldPositions[i].category, ucfpos_getCategory(ucfpos, &ec));
257 assertIntEquals("field",
258 expectedFieldPositions[i].field, ucfpos_getField(ucfpos, &ec));
259 int32_t start, limit;
260 ucfpos_getIndexes(ucfpos, &start, &limit, &ec);
261 assertIntEquals("start",
262 expectedFieldPositions[i].beginIndex, start);
263 assertIntEquals("limit",
264 expectedFieldPositions[i].endIndex, limit);
265 i++;
266 }
267 assertTrue("After loop", !ufmtval_nextPosition(fv, ucfpos, &ec));
268 assertSuccess("After loop", &ec);
269 ucfpos_close(ucfpos);
270}
271
272
273#endif /* #if !UCONFIG_NO_FORMATTING */