1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2005-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 #include "unicode/utypes.h"
9
10 #if !UCONFIG_NO_FORMATTING
11
12 #include <stdbool.h>
13
14 #include "unicode/unum.h"
15 #include "unicode/ucurr.h"
16 #include "unicode/ustring.h"
17 #include "cintltst.h"
18 #include "cmemory.h"
19 #include "cstring.h"
20
expectInList(const char * isoCurrency,uint32_t currencyType,UBool isExpected)21 static void expectInList(const char *isoCurrency, uint32_t currencyType, UBool isExpected) {
22 UErrorCode status = U_ZERO_ERROR;
23 const char *foundCurrency = NULL;
24 const char *currentCurrency;
25 UEnumeration *en = ucurr_openISOCurrencies(currencyType, &status);
26 if (U_FAILURE(status)) {
27 log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status));
28 return;
29 }
30
31 while ((currentCurrency = uenum_next(en, NULL, &status)) != NULL) {
32 if (strcmp(isoCurrency, currentCurrency) == 0) {
33 foundCurrency = currentCurrency;
34 break;
35 }
36 }
37
38 if ((foundCurrency != NULL) != isExpected) {
39 log_err("Error: could not find %s as expected. isExpected = %s type=0x%X\n",
40 isoCurrency, isExpected ? "true" : "false", currencyType);
41 }
42 uenum_close(en);
43 }
44
TestEnumList(void)45 static void TestEnumList(void) {
46 expectInList("ADP", UCURR_ALL, true); /* First in list */
47 expectInList("ZWD", UCURR_ALL, true); /* Last in list */
48
49 expectInList("USD", UCURR_ALL, true);
50 expectInList("USD", UCURR_COMMON, true);
51 expectInList("USD", UCURR_UNCOMMON, false);
52 expectInList("USD", UCURR_DEPRECATED, false);
53 expectInList("USD", UCURR_NON_DEPRECATED, true);
54 expectInList("USD", UCURR_COMMON|UCURR_DEPRECATED, false);
55 expectInList("USD", UCURR_COMMON|UCURR_NON_DEPRECATED, true);
56 expectInList("USD", UCURR_UNCOMMON|UCURR_DEPRECATED, false);
57 expectInList("USD", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, false);
58
59 expectInList("USN", UCURR_ALL, true);
60 expectInList("USN", UCURR_COMMON, false);
61 expectInList("USN", UCURR_UNCOMMON, true);
62 expectInList("USN", UCURR_DEPRECATED, false);
63 expectInList("USN", UCURR_NON_DEPRECATED, true);
64 expectInList("USN", UCURR_COMMON|UCURR_DEPRECATED, false);
65 expectInList("USN", UCURR_COMMON|UCURR_NON_DEPRECATED, false);
66 expectInList("USN", UCURR_UNCOMMON|UCURR_DEPRECATED, false);
67 expectInList("USN", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, true);
68
69 expectInList("DEM", UCURR_ALL, true);
70 expectInList("DEM", UCURR_COMMON, true);
71 expectInList("DEM", UCURR_UNCOMMON, false);
72 expectInList("DEM", UCURR_DEPRECATED, true);
73 expectInList("DEM", UCURR_NON_DEPRECATED, false);
74 expectInList("DEM", UCURR_COMMON|UCURR_DEPRECATED, true);
75 expectInList("DEM", UCURR_COMMON|UCURR_NON_DEPRECATED, false);
76 expectInList("DEM", UCURR_UNCOMMON|UCURR_DEPRECATED, false);
77 expectInList("DEM", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, false);
78
79 expectInList("XEU", UCURR_ALL, true);
80 expectInList("XEU", UCURR_COMMON, false);
81 expectInList("XEU", UCURR_UNCOMMON, true);
82 expectInList("XEU", UCURR_DEPRECATED, true);
83 expectInList("XEU", UCURR_NON_DEPRECATED, false);
84 expectInList("XEU", UCURR_COMMON|UCURR_DEPRECATED, false);
85 expectInList("XEU", UCURR_COMMON|UCURR_NON_DEPRECATED, false);
86 expectInList("XEU", UCURR_UNCOMMON|UCURR_DEPRECATED, true);
87 expectInList("XEU", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, false);
88
89 // ICU-21622
90 expectInList("UYW", UCURR_ALL, true);
91 expectInList("UYW", UCURR_COMMON, false);
92 expectInList("UYW", UCURR_UNCOMMON, true);
93 expectInList("UYW", UCURR_DEPRECATED, false);
94 expectInList("UYW", UCURR_NON_DEPRECATED, true);
95
96 // ICU-21685
97 expectInList("VES", UCURR_ALL, true);
98 expectInList("VES", UCURR_COMMON, true);
99 expectInList("VES", UCURR_UNCOMMON, false);
100 expectInList("VES", UCURR_DEPRECATED, false);
101 expectInList("VES", UCURR_NON_DEPRECATED, true);
102
103 // CLDR 41/42 and ICU-21989
104 expectInList("SLE", UCURR_ALL, true);
105 expectInList("SLE", UCURR_COMMON, true);
106 expectInList("SLE", UCURR_UNCOMMON, false);
107 expectInList("SLE", UCURR_DEPRECATED, false);
108 expectInList("SLE", UCURR_NON_DEPRECATED, true);
109 expectInList("VED", UCURR_ALL, true);
110 expectInList("VED", UCURR_COMMON, false);
111 expectInList("VED", UCURR_UNCOMMON, true);
112 expectInList("VED", UCURR_DEPRECATED, false);
113 expectInList("VED", UCURR_NON_DEPRECATED, true);
114
115 expectInList("EQE", UCURR_ALL, false);
116
117 // CLDR 45 and ICU-22726
118 expectInList("XCG", UCURR_ALL, true);
119
120 // CLDR 46 and ICU-22935
121 expectInList("ZWG", UCURR_ALL, true);
122 }
123
TestEnumListReset(void)124 static void TestEnumListReset(void) {
125 UErrorCode status = U_ZERO_ERROR;
126 const char *currency1;
127 const char *currency2;
128 UEnumeration *en = ucurr_openISOCurrencies(UCURR_ALL, &status);
129 if (U_FAILURE(status)) {
130 log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status));
131 return;
132 }
133
134 currency1 = uenum_next(en, NULL, &status);
135 uenum_reset(en, &status);
136 currency2 = uenum_next(en, NULL, &status);
137 if (U_FAILURE(status)) {
138 log_err("Error: uenum_next or uenum_reset returned %s\n", myErrorName(status));
139 return;
140 }
141 /* The first item's pointer in the list should be the same between resets. */
142 if (currency1 != currency2) {
143 log_err("Error: reset doesn't work %s != %s\n", currency1, currency2);
144 }
145 uenum_close(en);
146 }
147
checkItemCount(uint32_t currencyType)148 static int32_t checkItemCount(uint32_t currencyType) {
149 UErrorCode status = U_ZERO_ERROR;
150 int32_t originalCount, count;
151 UEnumeration *en = ucurr_openISOCurrencies(currencyType, &status);
152 int32_t expectedLen = 3, len;
153 if (U_FAILURE(status)) {
154 log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status));
155 return -1;
156 }
157
158 originalCount = uenum_count(en, &status);
159 for (count=0;;count++) {
160 const char *str = uenum_next(en, &len, &status);
161 if (str == NULL || len != expectedLen || (int32_t)strlen(str) != expectedLen) {
162 break;
163 }
164 }
165
166 if (originalCount != count) {
167 log_err("Error: uenum_count returned the wrong value (type = 0x%X). Got: %d Expected %d\n",
168 currencyType, count, originalCount);
169 }
170 if (U_FAILURE(status)) {
171 log_err("Error: uenum_next got an error: %s\n", u_errorName(status));
172 }
173 uenum_close(en);
174 return count;
175 }
176
TestEnumListCount(void)177 static void TestEnumListCount(void) {
178 checkItemCount(UCURR_ALL);
179 checkItemCount(UCURR_COMMON);
180 checkItemCount(UCURR_UNCOMMON);
181 checkItemCount(UCURR_DEPRECATED);
182 checkItemCount(UCURR_NON_DEPRECATED);
183 checkItemCount(UCURR_COMMON|UCURR_DEPRECATED);
184 checkItemCount(UCURR_COMMON|UCURR_NON_DEPRECATED);
185 checkItemCount(UCURR_UNCOMMON|UCURR_DEPRECATED);
186 checkItemCount(UCURR_UNCOMMON|UCURR_NON_DEPRECATED);
187
188 if (checkItemCount(UCURR_DEPRECATED|UCURR_NON_DEPRECATED) != 0) {
189 log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n");
190 }
191 if (checkItemCount(UCURR_COMMON|UCURR_UNCOMMON) != 0) {
192 log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n");
193 }
194 }
195
TestFractionDigitOverride(void)196 static void TestFractionDigitOverride(void) {
197 UErrorCode status = U_ZERO_ERROR;
198 UNumberFormat *fmt = unum_open(UNUM_CURRENCY, NULL, 0, "hu_HU", NULL, &status);
199 UChar buffer[256];
200 UChar expectedBuf[256];
201 const char expectedFirst[] = "123,46\\u00A0Ft"; /* changed to use 2 fraction digits */
202 const char expectedSecond[] = "123,46\\u00A0Ft";
203 const char expectedThird[] = "123,456\\u00A0Ft";
204 if (U_FAILURE(status)) {
205 log_data_err("Error: unum_open returned %s (Are you missing data?)\n", myErrorName(status));
206 return;
207 }
208 /* Make sure that you can format normal fraction digits. */
209 unum_formatDouble(fmt, 123.456, buffer, UPRV_LENGTHOF(buffer), NULL, &status);
210 u_unescape(expectedFirst, expectedBuf, (int32_t)strlen(expectedFirst)+1);
211 if (u_strcmp(buffer, expectedBuf) != 0) {
212 log_err("Error: unum_formatDouble didn't return %s\n", expectedFirst);
213 }
214 /* Make sure that you can format 2 fraction digits. */
215 unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 2);
216 unum_formatDouble(fmt, 123.456, buffer, UPRV_LENGTHOF(buffer), NULL, &status);
217 u_unescape(expectedSecond, expectedBuf, (int32_t)strlen(expectedSecond)+1);
218 if (u_strcmp(buffer, expectedBuf) != 0) {
219 log_err("Error: unum_formatDouble didn't return %s\n", expectedSecond);
220 }
221 /* Make sure that you can format more fraction digits. */
222 unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 3);
223 unum_formatDouble(fmt, 123.456, buffer, UPRV_LENGTHOF(buffer), NULL, &status);
224 u_unescape(expectedThird, expectedBuf, (int32_t)strlen(expectedThird)+1);
225 if (u_strcmp(buffer, expectedBuf) != 0) {
226 log_err("Error: unum_formatDouble didn't return %s\n", expectedThird);
227 }
228 unum_close(fmt);
229 }
230
TestPrefixSuffix(void)231 static void TestPrefixSuffix(void) {
232 int32_t pos;
233 UErrorCode status;
234 double result1 = 0.0, result2 = 0.0;
235 UNumberFormat* parser;
236 UChar buffer[4];
237 static const UChar TEST_NUMBER[] = {0x0024,0x0031,0x0032,0x002E,0x0030,0x0030,0}; /* $12.00 */
238 static const UChar NEG_PREFIX[] = {0x005B,0}; /* "[" */
239 static const UChar NEG_SUFFIX[] = {0x005D,0}; /* "]" */
240
241
242 status = U_ZERO_ERROR;
243 parser = unum_open(UNUM_CURRENCY, NULL, -1, "en_US", NULL, &status);
244 if (U_FAILURE(status)) {
245 log_data_err("Error: unum_open returned %s (Are you missing data?)\n", u_errorName(status));
246 return;
247 }
248
249 pos = 0;
250 status = U_ZERO_ERROR;
251 result1 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status);
252
253 unum_setTextAttribute(parser, UNUM_NEGATIVE_SUFFIX, NEG_SUFFIX, -1, &status);
254 unum_setTextAttribute(parser, UNUM_NEGATIVE_PREFIX, NEG_PREFIX, -1, &status);
255 if (U_FAILURE(status)) {
256 log_err("Error: unum_setTextAttribute returned %s\n", u_errorName(status));
257 return;
258 }
259
260 pos = 0;
261 result2 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status);
262 if (result1 != result2 || U_FAILURE(status)) {
263 log_err("Error: unum_parseDoubleCurrency didn't return the same value for same string %f %f %s\n",
264 result1, result2, u_errorName(status));
265 }
266 unum_close(parser);
267 }
268
269 typedef struct {
270 const char* alphaCode;
271 int32_t numericCode;
272 } NumCodeTestEntry;
273
274 static const NumCodeTestEntry NUMCODE_TESTDATA[] = {
275 {"USD", 840},
276 {"Usd", 840}, /* mixed casing */
277 {"EUR", 978},
278 {"JPY", 392},
279 {"XFU", 0}, /* XFU: no numeric code */
280 {"ZZZ", 0}, /* ZZZ: undefined ISO currency code */
281 {"bogus", 0}, /* bogus code */
282 {0, 0},
283 };
284
TestNumericCode(void)285 static void TestNumericCode(void) {
286 UChar code[8]; // at least one longer than the longest alphaCode
287 int32_t i;
288 int32_t numCode;
289
290 for (i = 0; NUMCODE_TESTDATA[i].alphaCode; i++) {
291 int32_t length = (int32_t)uprv_strlen(NUMCODE_TESTDATA[i].alphaCode);
292 u_charsToUChars(NUMCODE_TESTDATA[i].alphaCode, code, length + 1); // +1 includes the NUL
293 numCode = ucurr_getNumericCode(code);
294 if (numCode != NUMCODE_TESTDATA[i].numericCode) {
295 log_data_err("Error: ucurr_getNumericCode returned %d for currency %s, expected - %d\n",
296 numCode, NUMCODE_TESTDATA[i].alphaCode, NUMCODE_TESTDATA[i].numericCode);
297 }
298 }
299 }
300
301 void addCurrencyTest(TestNode** root);
302
303 #define TESTCASE(x) addTest(root, &x, "tsformat/currtest/" #x)
304
addCurrencyTest(TestNode ** root)305 void addCurrencyTest(TestNode** root)
306 {
307 TESTCASE(TestEnumList);
308 TESTCASE(TestEnumListReset);
309 TESTCASE(TestEnumListCount);
310 TESTCASE(TestFractionDigitOverride);
311 TESTCASE(TestPrefixSuffix);
312 TESTCASE(TestNumericCode);
313 }
314
315 #endif /* #if !UCONFIG_NO_FORMATTING */
316