]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/unumberformattertst.c
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / unumberformattertst.c
CommitLineData
0f5d89e8
A
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 "unicode/unumberformatter.h"
13#include "unicode/umisc.h"
14#include "unicode/unum.h"
15#include "cintltst.h"
16#include "cmemory.h"
17
18static void TestSkeletonFormatToString(void);
19
20static void TestSkeletonFormatToFields(void);
21
22static void TestExampleCode(void);
23
24void addUNumberFormatterTest(TestNode** root);
25
26void addUNumberFormatterTest(TestNode** root) {
27 addTest(root, &TestSkeletonFormatToString, "unumberformatter/TestSkeletonFormatToString");
28 addTest(root, &TestSkeletonFormatToFields, "unumberformatter/TestSkeletonFormatToFields");
29 addTest(root, &TestExampleCode, "unumberformatter/TestExampleCode");
30}
31
32
33#define CAPACITY 30
34
35static void TestSkeletonFormatToString() {
36 UErrorCode ec = U_ZERO_ERROR;
37 UChar buffer[CAPACITY];
38 UFormattedNumber* result = NULL;
39
40 // setup:
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);
46
47 // int64 test:
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);
54
55 // double test:
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);
61
62 // decnumber test:
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);
68 }
69
70 // cleanup:
71 unumf_closeResult(result);
72 unumf_close(f);
73}
74
75
76static void TestSkeletonFormatToFields() {
77 UErrorCode ec = U_ZERO_ERROR;
78 UFieldPositionIterator* ufpositer = NULL;
79
80 // setup:
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)) {
88
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);
94
95 // field position iterator test:
96 ufpositer = ufieldpositer_open(&ec);
97 if (assertSuccessCheck("Should create iterator without error", &ec, TRUE)) {
98
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}
109 };
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) {
117 log_err(
118 "FAIL: iteration %d; expected field %d; got %d\n", i, expected.field, actual.field);
119 }
120 if (expected.beginIndex != actual.beginIndex) {
121 log_err(
122 "FAIL: iteration %d; expected beginIndex %d; got %d\n",
123 i,
124 expected.beginIndex,
125 actual.beginIndex);
126 }
127 if (expected.endIndex != actual.endIndex) {
128 log_err(
129 "FAIL: iteration %d; expected endIndex %d; got %d\n",
130 i,
131 expected.endIndex,
132 actual.endIndex);
133 }
134 }
135 actual.field = ufieldpositer_next(ufpositer, &actual.beginIndex, &actual.endIndex);
136 assertTrue("No more fields; should return a negative index", actual.field < 0);
137
138 // next field iteration:
139 actual.field = UNUM_GROUPING_SEPARATOR_FIELD;
140 actual.beginIndex = 0;
141 actual.endIndex = 0;
142 int32_t i = 1;
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);
147 }
148 assertIntEquals("Should have seen all grouping separators", 4, i);
149 }
150 }
151
152 // cleanup:
153 unumf_closeResult(uresult);
154 unumf_close(uformatter);
155 ufieldpositer_close(ufpositer);
156}
157
158
159static void TestExampleCode() {
160 // This is the example code given in unumberformatter.h.
161
162 // Setup:
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);
168
169 // Format a double:
170 unumf_formatDouble(uformatter, 5142.3, uresult, &ec);
171 if (assertSuccessCheck("There should not be a failure in the example code", &ec, TRUE)) {
172
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);
176 ec = U_ZERO_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);
181 }
182
183 // Cleanup:
184 unumf_close(uformatter);
185 unumf_closeResult(uresult);
186 uprv_free(buffer);
187}
188
189
190#endif /* #if !UCONFIG_NO_FORMATTING */