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 (void)expectedFieldPositionsLength
; // suppress compiler warnings about unused variable
217 UErrorCode ec
= U_ZERO_ERROR
;
218 checkFormattedValueString(message
, fv
, expectedString
, &ec
);
219 if (U_FAILURE(ec
)) { return; }
221 // Basic loop over the fields (more rigorous testing in C++)
222 UConstrainedFieldPosition
* ucfpos
= ucfpos_open(&ec
);
224 while (ufmtval_nextPosition(fv
, ucfpos
, &ec
)) {
225 assertIntEquals("category",
226 expectedCategory
, ucfpos_getCategory(ucfpos
, &ec
));
227 assertIntEquals("field",
228 expectedFieldPositions
[i
].field
, ucfpos_getField(ucfpos
, &ec
));
229 int32_t start
, limit
;
230 ucfpos_getIndexes(ucfpos
, &start
, &limit
, &ec
);
231 assertIntEquals("start",
232 expectedFieldPositions
[i
].beginIndex
, start
);
233 assertIntEquals("limit",
234 expectedFieldPositions
[i
].endIndex
, limit
);
237 assertTrue("After loop", !ufmtval_nextPosition(fv
, ucfpos
, &ec
));
238 assertSuccess("After loop", &ec
);
239 ucfpos_close(ucfpos
);
242 void checkMixedFormattedValue(
244 const UFormattedValue
* fv
,
245 const UChar
* expectedString
,
246 const UFieldPositionWithCategory
* expectedFieldPositions
,
248 (void)length
; // suppress compiler warnings about unused variable
249 UErrorCode ec
= U_ZERO_ERROR
;
250 checkFormattedValueString(message
, fv
, expectedString
, &ec
);
251 if (U_FAILURE(ec
)) { return; }
253 // Basic loop over the fields (more rigorous testing in C++)
254 UConstrainedFieldPosition
* ucfpos
= ucfpos_open(&ec
);
256 while (ufmtval_nextPosition(fv
, ucfpos
, &ec
)) {
257 assertIntEquals("category",
258 expectedFieldPositions
[i
].category
, ucfpos_getCategory(ucfpos
, &ec
));
259 assertIntEquals("field",
260 expectedFieldPositions
[i
].field
, ucfpos_getField(ucfpos
, &ec
));
261 int32_t start
, limit
;
262 ucfpos_getIndexes(ucfpos
, &start
, &limit
, &ec
);
263 assertIntEquals("start",
264 expectedFieldPositions
[i
].beginIndex
, start
);
265 assertIntEquals("limit",
266 expectedFieldPositions
[i
].endIndex
, limit
);
269 assertTrue("After loop", !ufmtval_nextPosition(fv
, ucfpos
, &ec
));
270 assertSuccess("After loop", &ec
);
271 ucfpos_close(ucfpos
);
275 #endif /* #if !UCONFIG_NO_FORMATTING */