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