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 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
12 #include "unicode/unumberformatter.h"
13 #include "unicode/umisc.h"
14 #include "unicode/unum.h"
18 static void TestSkeletonFormatToString(void);
20 static void TestSkeletonFormatToFields(void);
22 static void TestExampleCode(void);
24 void addUNumberFormatterTest(TestNode
** root
);
26 void addUNumberFormatterTest(TestNode
** root
) {
27 addTest(root
, &TestSkeletonFormatToString
, "unumberformatter/TestSkeletonFormatToString");
28 addTest(root
, &TestSkeletonFormatToFields
, "unumberformatter/TestSkeletonFormatToFields");
29 addTest(root
, &TestExampleCode
, "unumberformatter/TestExampleCode");
35 static void TestSkeletonFormatToString() {
36 UErrorCode ec
= U_ZERO_ERROR
;
37 UChar buffer
[CAPACITY
];
38 UFormattedNumber
* result
= NULL
;
41 UNumberFormatter
* f
= unumf_openForSkeletonAndLocale(
42 u
"precision-integer currency/USD sign-accounting", -1, "en", &ec
);
43 assertSuccessCheck("Should create without error", &ec
, TRUE
);
44 result
= unumf_openResult(&ec
);
45 assertSuccess("Should create result without error", &ec
);
48 unumf_formatInt(f
, -444444, result
, &ec
);
49 // Missing data will give a U_MISSING_RESOURCE_ERROR here.
50 if (assertSuccessCheck("Should format integer without error", &ec
, TRUE
)) {
51 unumf_resultToString(result
, buffer
, CAPACITY
, &ec
);
52 assertSuccess("Should print string to buffer without error", &ec
);
53 assertUEquals("Should produce expected string result", u
"($444,444)", buffer
);
56 unumf_formatDouble(f
, -5142.3, result
, &ec
);
57 assertSuccess("Should format double without error", &ec
);
58 unumf_resultToString(result
, buffer
, CAPACITY
, &ec
);
59 assertSuccess("Should print string to buffer without error", &ec
);
60 assertUEquals("Should produce expected string result", u
"($5,142)", buffer
);
63 unumf_formatDecimal(f
, "9.876E2", -1, result
, &ec
);
64 assertSuccess("Should format decimal without error", &ec
);
65 unumf_resultToString(result
, buffer
, CAPACITY
, &ec
);
66 assertSuccess("Should print string to buffer without error", &ec
);
67 assertUEquals("Should produce expected string result", u
"$988", buffer
);
71 unumf_closeResult(result
);
76 static void TestSkeletonFormatToFields() {
77 UErrorCode ec
= U_ZERO_ERROR
;
78 UFieldPositionIterator
* ufpositer
= NULL
;
81 UNumberFormatter
* uformatter
= unumf_openForSkeletonAndLocale(
82 u
".00 measure-unit/length-meter sign-always", -1, "en", &ec
);
83 assertSuccessCheck("Should create without error", &ec
, TRUE
);
84 UFormattedNumber
* uresult
= unumf_openResult(&ec
);
85 assertSuccess("Should create result without error", &ec
);
86 unumf_formatInt(uformatter
, 9876543210L, uresult
, &ec
); // "+9,876,543,210.00 m"
87 if (assertSuccessCheck("unumf_formatInt() failed", &ec
, TRUE
)) {
89 // field position test:
90 UFieldPosition ufpos
= {UNUM_DECIMAL_SEPARATOR_FIELD
};
91 unumf_resultNextFieldPosition(uresult
, &ufpos
, &ec
);
92 assertIntEquals("Field position should be correct", 14, ufpos
.beginIndex
);
93 assertIntEquals("Field position should be correct", 15, ufpos
.endIndex
);
95 // field position iterator test:
96 ufpositer
= ufieldpositer_open(&ec
);
97 if (assertSuccessCheck("Should create iterator without error", &ec
, TRUE
)) {
99 unumf_resultGetAllFieldPositions(uresult
, ufpositer
, &ec
);
100 static const UFieldPosition expectedFields
[] = {
101 // Field, begin index, end index
102 {UNUM_SIGN_FIELD
, 0, 1},
103 {UNUM_GROUPING_SEPARATOR_FIELD
, 2, 3},
104 {UNUM_GROUPING_SEPARATOR_FIELD
, 6, 7},
105 {UNUM_GROUPING_SEPARATOR_FIELD
, 10, 11},
106 {UNUM_INTEGER_FIELD
, 1, 14},
107 {UNUM_DECIMAL_SEPARATOR_FIELD
, 14, 15},
108 {UNUM_FRACTION_FIELD
, 15, 17}
110 UFieldPosition actual
;
111 for (int32_t i
= 0; i
< sizeof(expectedFields
) / sizeof(*expectedFields
); i
++) {
112 // Iterate using the UFieldPosition to hold state...
113 UFieldPosition expected
= expectedFields
[i
];
114 actual
.field
= ufieldpositer_next(ufpositer
, &actual
.beginIndex
, &actual
.endIndex
);
115 assertTrue("Should not return a negative index yet", actual
.field
>= 0);
116 if (expected
.field
!= actual
.field
) {
118 "FAIL: iteration %d; expected field %d; got %d\n", i
, expected
.field
, actual
.field
);
120 if (expected
.beginIndex
!= actual
.beginIndex
) {
122 "FAIL: iteration %d; expected beginIndex %d; got %d\n",
127 if (expected
.endIndex
!= actual
.endIndex
) {
129 "FAIL: iteration %d; expected endIndex %d; got %d\n",
135 actual
.field
= ufieldpositer_next(ufpositer
, &actual
.beginIndex
, &actual
.endIndex
);
136 assertTrue("No more fields; should return a negative index", actual
.field
< 0);
138 // next field iteration:
139 actual
.field
= UNUM_GROUPING_SEPARATOR_FIELD
;
140 actual
.beginIndex
= 0;
143 while (unumf_resultNextFieldPosition(uresult
, &actual
, &ec
)) {
144 UFieldPosition expected
= expectedFields
[i
++];
145 assertIntEquals("Grouping separator begin index", expected
.beginIndex
, actual
.beginIndex
);
146 assertIntEquals("Grouping separator end index", expected
.endIndex
, actual
.endIndex
);
148 assertIntEquals("Should have seen all grouping separators", 4, i
);
153 unumf_closeResult(uresult
);
154 unumf_close(uformatter
);
155 ufieldpositer_close(ufpositer
);
159 static void TestExampleCode() {
160 // This is the example code given in unumberformatter.h.
163 UErrorCode ec
= U_ZERO_ERROR
;
164 UNumberFormatter
* uformatter
= unumf_openForSkeletonAndLocale(u
"precision-integer", -1, "en", &ec
);
165 UFormattedNumber
* uresult
= unumf_openResult(&ec
);
166 UChar
* buffer
= NULL
;
167 assertSuccessCheck("There should not be a failure in the example code", &ec
, TRUE
);
170 unumf_formatDouble(uformatter
, 5142.3, uresult
, &ec
);
171 if (assertSuccessCheck("There should not be a failure in the example code", &ec
, TRUE
)) {
173 // Export the string to a malloc'd buffer:
174 int32_t len
= unumf_resultToString(uresult
, NULL
, 0, &ec
);
175 assertTrue("No buffer yet", ec
== U_BUFFER_OVERFLOW_ERROR
);
177 buffer
= (UChar
*) uprv_malloc((len
+1)*sizeof(UChar
));
178 unumf_resultToString(uresult
, buffer
, len
+1, &ec
);
179 assertSuccess("There should not be a failure in the example code", &ec
);
180 assertUEquals("Should produce expected string result", u
"5,142", buffer
);
184 unumf_close(uformatter
);
185 unumf_closeResult(uresult
);
190 #endif /* #if !UCONFIG_NO_FORMATTING */