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 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
11
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include "unicode/unumberformatter.h"
15 #include "unicode/umisc.h"
16 #include "unicode/unum.h"
17 #include "unicode/ustring.h"
18 #include "cformtst.h"
19 #include "cintltst.h"
20 #include "cmemory.h"
21
22 static void TestSkeletonFormatToString(void);
23
24 static void TestSkeletonFormatToFields(void);
25
26 static void TestExampleCode(void);
27
28 static void TestFormattedValue(void);
29
30 static void TestSkeletonParseError(void);
31
32 static void TestToDecimalNumber(void);
33
34 static void TestPerUnitInArabic(void);
35
36 static void Test21674_State(void);
37
38 static void TestNegativeDegrees(void);
39
40 void addUNumberFormatterTest(TestNode** root);
41
42 #define TESTCASE(x) addTest(root, &x, "tsformat/unumberformatter/" #x)
43
addUNumberFormatterTest(TestNode ** root)44 void addUNumberFormatterTest(TestNode** root) {
45 TESTCASE(TestSkeletonFormatToString);
46 TESTCASE(TestSkeletonFormatToFields);
47 TESTCASE(TestExampleCode);
48 TESTCASE(TestFormattedValue);
49 TESTCASE(TestSkeletonParseError);
50 TESTCASE(TestToDecimalNumber);
51 TESTCASE(TestPerUnitInArabic);
52 TESTCASE(Test21674_State);
53 TESTCASE(TestNegativeDegrees);
54 }
55
56
57 #define CAPACITY 30
58
TestSkeletonFormatToString()59 static void TestSkeletonFormatToString() {
60 UErrorCode ec = U_ZERO_ERROR;
61 UChar buffer[CAPACITY];
62 UFormattedNumber* result = NULL;
63
64 // setup:
65 UNumberFormatter* f = unumf_openForSkeletonAndLocale(
66 u"precision-integer currency/USD sign-accounting", -1, "en", &ec);
67 assertSuccessCheck("Should create without error", &ec, true);
68 result = unumf_openResult(&ec);
69 assertSuccess("Should create result without error", &ec);
70
71 // int64 test:
72 unumf_formatInt(f, -444444, result, &ec);
73 // Missing data will give a U_MISSING_RESOURCE_ERROR here.
74 if (assertSuccessCheck("Should format integer without error", &ec, true)) {
75 unumf_resultToString(result, buffer, CAPACITY, &ec);
76 assertSuccess("Should print string to buffer without error", &ec);
77 assertUEquals("Should produce expected string result", u"($444,444)", buffer);
78
79 // double test:
80 unumf_formatDouble(f, -5142.3, result, &ec);
81 assertSuccess("Should format double without error", &ec);
82 unumf_resultToString(result, buffer, CAPACITY, &ec);
83 assertSuccess("Should print string to buffer without error", &ec);
84 assertUEquals("Should produce expected string result", u"($5,142)", buffer);
85
86 // decnumber test:
87 unumf_formatDecimal(f, "9.876E2", -1, result, &ec);
88 assertSuccess("Should format decimal without error", &ec);
89 unumf_resultToString(result, buffer, CAPACITY, &ec);
90 assertSuccess("Should print string to buffer without error", &ec);
91 assertUEquals("Should produce expected string result", u"$988", buffer);
92 }
93
94 // cleanup:
95 unumf_closeResult(result);
96 unumf_close(f);
97 }
98
99
TestSkeletonFormatToFields()100 static void TestSkeletonFormatToFields() {
101 UErrorCode ec = U_ZERO_ERROR;
102 UFieldPositionIterator* ufpositer = NULL;
103
104 // setup:
105 UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(
106 u".00 measure-unit/length-meter sign-always", -1, "en", &ec);
107 assertSuccessCheck("Should create without error", &ec, true);
108 UFormattedNumber* uresult = unumf_openResult(&ec);
109 assertSuccess("Should create result without error", &ec);
110 unumf_formatInt(uformatter, 9876543210L, uresult, &ec); // "+9,876,543,210.00 m"
111 if (assertSuccessCheck("unumf_formatInt() failed", &ec, true)) {
112
113 // field position test:
114 UFieldPosition ufpos = {UNUM_DECIMAL_SEPARATOR_FIELD, 0, 0};
115 unumf_resultNextFieldPosition(uresult, &ufpos, &ec);
116 assertIntEquals("Field position should be correct", 14, ufpos.beginIndex);
117 assertIntEquals("Field position should be correct", 15, ufpos.endIndex);
118
119 // field position iterator test:
120 ufpositer = ufieldpositer_open(&ec);
121 if (assertSuccessCheck("Should create iterator without error", &ec, true)) {
122
123 unumf_resultGetAllFieldPositions(uresult, ufpositer, &ec);
124 static const UFieldPosition expectedFields[] = {
125 // Field, begin index, end index
126 {UNUM_SIGN_FIELD, 0, 1},
127 {UNUM_GROUPING_SEPARATOR_FIELD, 2, 3},
128 {UNUM_GROUPING_SEPARATOR_FIELD, 6, 7},
129 {UNUM_GROUPING_SEPARATOR_FIELD, 10, 11},
130 {UNUM_INTEGER_FIELD, 1, 14},
131 {UNUM_DECIMAL_SEPARATOR_FIELD, 14, 15},
132 {UNUM_FRACTION_FIELD, 15, 17},
133 {UNUM_MEASURE_UNIT_FIELD, 18, 19}
134 };
135 UFieldPosition actual;
136 for (int32_t i = 0; i < (int32_t)(sizeof(expectedFields) / sizeof(*expectedFields)); i++) {
137 // Iterate using the UFieldPosition to hold state...
138 UFieldPosition expected = expectedFields[i];
139 actual.field = ufieldpositer_next(ufpositer, &actual.beginIndex, &actual.endIndex);
140 assertTrue("Should not return a negative index yet", actual.field >= 0);
141 if (expected.field != actual.field) {
142 log_err(
143 "FAIL: iteration %d; expected field %d; got %d\n", i, expected.field, actual.field);
144 }
145 if (expected.beginIndex != actual.beginIndex) {
146 log_err(
147 "FAIL: iteration %d; expected beginIndex %d; got %d\n",
148 i,
149 expected.beginIndex,
150 actual.beginIndex);
151 }
152 if (expected.endIndex != actual.endIndex) {
153 log_err(
154 "FAIL: iteration %d; expected endIndex %d; got %d\n",
155 i,
156 expected.endIndex,
157 actual.endIndex);
158 }
159 }
160 actual.field = ufieldpositer_next(ufpositer, &actual.beginIndex, &actual.endIndex);
161 assertTrue("No more fields; should return a negative index", actual.field < 0);
162
163 // next field iteration:
164 actual.field = UNUM_GROUPING_SEPARATOR_FIELD;
165 actual.beginIndex = 0;
166 actual.endIndex = 0;
167 int32_t i = 1;
168 while (unumf_resultNextFieldPosition(uresult, &actual, &ec)) {
169 UFieldPosition expected = expectedFields[i++];
170 assertIntEquals("Grouping separator begin index", expected.beginIndex, actual.beginIndex);
171 assertIntEquals("Grouping separator end index", expected.endIndex, actual.endIndex);
172 }
173 assertIntEquals("Should have seen all grouping separators", 4, i);
174 }
175 }
176
177 // cleanup:
178 unumf_closeResult(uresult);
179 unumf_close(uformatter);
180 ufieldpositer_close(ufpositer);
181 }
182
183
TestExampleCode()184 static void TestExampleCode() {
185 // This is the example code given in unumberformatter.h.
186
187 // Setup:
188 UErrorCode ec = U_ZERO_ERROR;
189 UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(u"precision-integer", -1, "en", &ec);
190 UFormattedNumber* uresult = unumf_openResult(&ec);
191 UChar* buffer = NULL;
192 assertSuccessCheck("There should not be a failure in the example code", &ec, true);
193
194 // Format a double:
195 unumf_formatDouble(uformatter, 5142.3, uresult, &ec);
196 if (assertSuccessCheck("There should not be a failure in the example code", &ec, true)) {
197
198 // Export the string to a malloc'd buffer:
199 int32_t len = unumf_resultToString(uresult, NULL, 0, &ec);
200 assertTrue("No buffer yet", ec == U_BUFFER_OVERFLOW_ERROR);
201 ec = U_ZERO_ERROR;
202 buffer = (UChar*) uprv_malloc((len+1)*sizeof(UChar));
203 unumf_resultToString(uresult, buffer, len+1, &ec);
204 assertSuccess("There should not be a failure in the example code", &ec);
205 assertUEquals("Should produce expected string result", u"5,142", buffer);
206 }
207
208 // Cleanup:
209 unumf_close(uformatter);
210 unumf_closeResult(uresult);
211 uprv_free(buffer);
212 }
213
214
TestFormattedValue()215 static void TestFormattedValue() {
216 UErrorCode ec = U_ZERO_ERROR;
217 UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(
218 u".00 compact-short", -1, "en", &ec);
219 assertSuccessCheck("Should create without error", &ec, true);
220 UFormattedNumber* uresult = unumf_openResult(&ec);
221 assertSuccess("Should create result without error", &ec);
222
223 unumf_formatInt(uformatter, 55000, uresult, &ec); // "55.00 K"
224 if (assertSuccessCheck("Should format without error", &ec, true)) {
225 const UFormattedValue* fv = unumf_resultAsValue(uresult, &ec);
226 assertSuccess("Should convert without error", &ec);
227 static const UFieldPosition expectedFieldPositions[] = {
228 // field, begin index, end index
229 {UNUM_INTEGER_FIELD, 0, 2},
230 {UNUM_DECIMAL_SEPARATOR_FIELD, 2, 3},
231 {UNUM_FRACTION_FIELD, 3, 5},
232 {UNUM_COMPACT_FIELD, 5, 6}};
233 checkFormattedValue(
234 "FormattedNumber as FormattedValue",
235 fv,
236 u"55.00K",
237 UFIELD_CATEGORY_NUMBER,
238 expectedFieldPositions,
239 UPRV_LENGTHOF(expectedFieldPositions));
240 }
241
242 // cleanup:
243 unumf_closeResult(uresult);
244 unumf_close(uformatter);
245 }
246
247
TestSkeletonParseError()248 static void TestSkeletonParseError() {
249 UErrorCode ec = U_ZERO_ERROR;
250 UNumberFormatter* uformatter;
251 UParseError perror;
252
253 // The UParseError can be null. The following should not segfault.
254 uformatter = unumf_openForSkeletonAndLocaleWithError(
255 u".00 measure-unit/typo", -1, "en", NULL, &ec);
256 unumf_close(uformatter);
257
258 // Now test the behavior.
259 ec = U_ZERO_ERROR;
260 uformatter = unumf_openForSkeletonAndLocaleWithError(
261 u".00 measure-unit/typo", -1, "en", &perror, &ec);
262
263 assertIntEquals("Should have set error code", U_NUMBER_SKELETON_SYNTAX_ERROR, ec);
264 assertIntEquals("Should have correct skeleton error offset", 17, perror.offset);
265 assertUEquals("Should have correct pre context", u"0 measure-unit/", perror.preContext);
266 assertUEquals("Should have correct post context", u"typo", perror.postContext);
267
268 // cleanup:
269 unumf_close(uformatter);
270 }
271
272
TestToDecimalNumber()273 static void TestToDecimalNumber() {
274 UErrorCode ec = U_ZERO_ERROR;
275 UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(
276 u"currency/USD",
277 -1,
278 "en-US",
279 &ec);
280 assertSuccessCheck("Should create without error", &ec, true);
281 UFormattedNumber* uresult = unumf_openResult(&ec);
282 assertSuccess("Should create result without error", &ec);
283
284 unumf_formatDouble(uformatter, 3.0, uresult, &ec);
285 const UChar* str = ufmtval_getString(unumf_resultAsValue(uresult, &ec), NULL, &ec);
286 assertSuccessCheck("Formatting should succeed", &ec, true);
287 assertUEquals("Should produce expected string result", u"$3.00", str);
288
289 char buffer[CAPACITY];
290
291 int32_t len = unumf_resultToDecimalNumber(uresult, buffer, CAPACITY, &ec);
292 assertIntEquals("Length should be as expected", strlen(buffer), len);
293 assertEquals("Decimal should be as expected", "3", buffer);
294
295 // cleanup:
296 unumf_closeResult(uresult);
297 unumf_close(uformatter);
298 }
299
300
TestPerUnitInArabic()301 static void TestPerUnitInArabic() {
302 const char* simpleMeasureUnits[] = {
303 "area-acre",
304 "digital-bit",
305 "digital-byte",
306 "temperature-celsius",
307 "length-centimeter",
308 "duration-day",
309 "angle-degree",
310 "temperature-fahrenheit",
311 "volume-fluid-ounce",
312 "length-foot",
313 "volume-gallon",
314 "digital-gigabit",
315 "digital-gigabyte",
316 "mass-gram",
317 "area-hectare",
318 "duration-hour",
319 "length-inch",
320 "digital-kilobit",
321 "digital-kilobyte",
322 "mass-kilogram",
323 "length-kilometer",
324 "volume-liter",
325 "digital-megabit",
326 "digital-megabyte",
327 "length-meter",
328 "length-mile",
329 "length-mile-scandinavian",
330 "volume-milliliter",
331 "length-millimeter",
332 "duration-millisecond",
333 "duration-minute",
334 "duration-month",
335 "mass-ounce",
336 "concentr-percent",
337 "digital-petabyte",
338 "mass-pound",
339 "duration-second",
340 "mass-stone",
341 "digital-terabit",
342 "digital-terabyte",
343 "duration-week",
344 "length-yard",
345 "duration-year"
346 };
347 #define BUFFER_LEN 256
348 char buffer[BUFFER_LEN];
349 UChar ubuffer[BUFFER_LEN];
350 const char* locale = "ar";
351 UErrorCode status = U_ZERO_ERROR;
352 UFormattedNumber* formatted = unumf_openResult(&status);
353 if (U_FAILURE(status)) {
354 log_err("FAIL: unumf_openResult failed");
355 return;
356 }
357 for(int32_t i=0; i < UPRV_LENGTHOF(simpleMeasureUnits); ++i) {
358 for(int32_t j=0; j < UPRV_LENGTHOF(simpleMeasureUnits); ++j) {
359 status = U_ZERO_ERROR;
360 sprintf(buffer, "measure-unit/%s per-measure-unit/%s",
361 simpleMeasureUnits[i], simpleMeasureUnits[j]);
362 int32_t outputlen = 0;
363 u_strFromUTF8(ubuffer, BUFFER_LEN, &outputlen, buffer, (int32_t)strlen(buffer), &status);
364 if (U_FAILURE(status)) {
365 log_err("FAIL u_strFromUTF8: %s = %s ( %s )\n", locale, buffer,
366 u_errorName(status));
367 }
368 UNumberFormatter* nf = unumf_openForSkeletonAndLocale(
369 ubuffer, outputlen, locale, &status);
370 if (U_FAILURE(status)) {
371 log_err("FAIL unumf_openForSkeletonAndLocale: %s = %s ( %s )\n",
372 locale, buffer, u_errorName(status));
373 } else {
374 unumf_formatDouble(nf, 1, formatted, &status);
375 if (U_FAILURE(status)) {
376 log_err("FAIL unumf_formatDouble: %s = %s ( %s )\n",
377 locale, buffer, u_errorName(status));
378 }
379 }
380 unumf_close(nf);
381 }
382 }
383 unumf_closeResult(formatted);
384 }
385
386
Test21674_State()387 static void Test21674_State() {
388 UErrorCode status = U_ZERO_ERROR;
389 UNumberFormatter* nf = NULL;
390 UFormattedNumber* result = NULL;
391
392 nf = unumf_openForSkeletonAndLocale(u"precision-increment/0.05/w", -1, "en", &status);
393 if (!assertSuccess("unumf_openForSkeletonAndLocale", &status)) { goto cleanup; }
394
395 result = unumf_openResult(&status);
396 if (!assertSuccess("unumf_openResult", &status)) { goto cleanup; }
397
398 typedef struct TestCase {
399 double num;
400 const UChar* expected;
401 } TestCase;
402 TestCase cases[] = {
403 { 1.975, u"2" },
404 { 1.97, u"1.95" },
405 { 1.975, u"2" },
406 };
407 for (int i=0; i<3; i++) {
408 unumf_formatDouble(nf, cases[i].num, result, &status);
409 if (!assertSuccess("unumf_formatDouble", &status)) { goto cleanup; }
410
411 const UFormattedValue* formattedValue = unumf_resultAsValue(result, &status);
412 if (!assertSuccess("unumf_resultAsValue", &status)) { goto cleanup; }
413
414 int32_t length;
415 const UChar* str = ufmtval_getString(formattedValue, &length, &status);
416 if (!assertSuccess("ufmtval_getString", &status)) { goto cleanup; }
417
418 char message[] = {i + '0', '\0'};
419 assertUEquals(message, cases[i].expected, str);
420 }
421
422 cleanup:
423 unumf_close(nf);
424 unumf_closeResult(result);
425 }
426
427 // Test for ICU-22105
TestNegativeDegrees(void)428 static void TestNegativeDegrees(void) {
429 typedef struct {
430 const UChar* skeleton;
431 double value;
432 const UChar* expectedResult;
433 } TestCase;
434
435 TestCase testCases[] = {
436 { u"measure-unit/temperature-celsius unit-width-short", 0, u"0°C" },
437 { u"measure-unit/temperature-celsius unit-width-short usage/default", 0, u"32°F" },
438 { u"measure-unit/temperature-celsius unit-width-short usage/weather", 0, u"32°F" },
439
440 { u"measure-unit/temperature-celsius unit-width-short", -1, u"-1°C" },
441 { u"measure-unit/temperature-celsius unit-width-short usage/default", -1, u"30°F" },
442 { u"measure-unit/temperature-celsius unit-width-short usage/weather", -1, u"30°F" }
443 };
444
445 for (int32_t i = 0; i < UPRV_LENGTHOF(testCases); i++) {
446 UErrorCode err = U_ZERO_ERROR;
447 UNumberFormatter* nf = unumf_openForSkeletonAndLocale(testCases[i].skeleton, -1, "en_US", &err);
448 UFormattedNumber* fn = unumf_openResult(&err);
449
450 if (assertSuccess("Failed to create formatter or result", &err)) {
451 UChar result[200];
452 unumf_formatDouble(nf, testCases[i].value, fn, &err);
453 unumf_resultToString(fn, result, 200, &err);
454
455 if (assertSuccess("Formatting number failed", &err)) {
456 assertUEquals("Got wrong result", testCases[i].expectedResult, result);
457 }
458 }
459
460 unumf_closeResult(fn);
461 unumf_close(nf);
462 }
463 }
464
465
466 #endif /* #if !UCONFIG_NO_FORMATTING */
467