1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
8 #include "unicode/uformattedvalue.h"
9 #include "unicode/unum.h"
10 #include "unicode/ustring.h"
17 static void TestBasic(void);
18 static void TestSetters(void);
20 static void AssertAllPartsEqual(
21 const char* messagePrefix
,
22 const UConstrainedFieldPosition
* ucfpos
,
24 UFieldCategory category
,
30 void addUFormattedValueTest(TestNode
** root
);
32 #define TESTCASE(x) addTest(root, &x, "tsformat/uformattedvalue/" #x)
34 void addUFormattedValueTest(TestNode
** root
) {
36 TESTCASE(TestSetters
);
40 static 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
);
50 UFIELD_CATEGORY_UNDEFINED
,
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
);
65 ucfpos_constrainCategory(ucfpos
, UFIELD_CATEGORY_DATE
, &status
);
66 assertSuccess("setters 0", &status
);
77 ucfpos_constrainField(ucfpos
, UFIELD_CATEGORY_NUMBER
, UNUM_COMPACT_FIELD
, &status
);
78 assertSuccess("setters 1", &status
);
83 UFIELD_CATEGORY_NUMBER
,
89 ucfpos_setInt64IterationContext(ucfpos
, 42424242424242LL, &status
);
90 assertSuccess("setters 2", &status
);
95 UFIELD_CATEGORY_NUMBER
,
101 ucfpos_setState(ucfpos
, UFIELD_CATEGORY_NUMBER
, UNUM_COMPACT_FIELD
, 5, 10, &status
);
102 assertSuccess("setters 3", &status
);
107 UFIELD_CATEGORY_NUMBER
,
113 ucfpos_reset(ucfpos
, &status
);
114 assertSuccess("setters 4", &status
);
119 UFIELD_CATEGORY_UNDEFINED
,
125 ucfpos_close(ucfpos
);
128 /** For matching, turn on these bits:
130 * 1 = UNUM_INTEGER_FIELD
131 * 2 = UNUM_COMPACT_FIELD
132 * 4 = UDAT_AM_PM_FIELD
134 static void AssertAllPartsEqual(
135 const char* messagePrefix
,
136 const UConstrainedFieldPosition
* ucfpos
,
138 UFieldCategory category
,
144 UErrorCode status
= U_ZERO_ERROR
;
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);
153 #define AAPE_MSG(suffix) (uprv_strncpy(message+prefixEnd, suffix, 256-prefixEnd)-prefixEnd)
155 UFieldCategory _category
= ucfpos_getCategory(ucfpos
, &status
);
156 assertSuccess(AAPE_MSG("_"), &status
);
157 assertIntEquals(AAPE_MSG("category"), category
, _category
);
159 int32_t _field
= ucfpos_getField(ucfpos
, &status
);
160 assertSuccess(AAPE_MSG("field"), &status
);
161 assertIntEquals(AAPE_MSG("field"), field
, _field
);
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
);
169 int64_t _context
= ucfpos_getInt64IterationContext(ucfpos
, &status
);
170 assertSuccess(AAPE_MSG("context"), &status
);
171 assertIntEquals(AAPE_MSG("context"), context
, _context
);
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
);
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
);
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
);
190 static void checkFormattedValueString(
192 const UFormattedValue
* fv
,
193 const UChar
* expectedString
,
196 const UChar
* actualString
= ufmtval_getString(fv
, &length
, ec
);
197 if (U_FAILURE(*ec
)) {
198 assertIntEquals(message
, 0, length
);
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
);
208 // Declared in cformtst.h
209 void checkFormattedValue(
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; }
220 // Basic loop over the fields (more rigorous testing in C++)
221 UConstrainedFieldPosition
* ucfpos
= ucfpos_open(&ec
);
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
);
236 assertTrue("After loop", !ufmtval_nextPosition(fv
, ucfpos
, &ec
));
237 assertSuccess("After loop", &ec
);
238 ucfpos_close(ucfpos
);
241 void checkMixedFormattedValue(
243 const UFormattedValue
* fv
,
244 const UChar
* expectedString
,
245 const UFieldPositionWithCategory
* expectedFieldPositions
,
247 UErrorCode ec
= U_ZERO_ERROR
;
248 checkFormattedValueString(message
, fv
, expectedString
, &ec
);
249 if (U_FAILURE(ec
)) { return; }
251 // Basic loop over the fields (more rigorous testing in C++)
252 UConstrainedFieldPosition
* ucfpos
= ucfpos_open(&ec
);
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
);
267 assertTrue("After loop", !ufmtval_nextPosition(fv
, ucfpos
, &ec
));
268 assertSuccess("After loop", &ec
);
269 ucfpos_close(ucfpos
);
273 #endif /* #if !UCONFIG_NO_FORMATTING */