• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TestEnumListReset(void)121 static void TestEnumListReset(void) {
122     UErrorCode status = U_ZERO_ERROR;
123     const char *currency1;
124     const char *currency2;
125     UEnumeration *en = ucurr_openISOCurrencies(UCURR_ALL, &status);
126     if (U_FAILURE(status)) {
127        log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status));
128        return;
129     }
130 
131     currency1 = uenum_next(en, NULL, &status);
132     uenum_reset(en, &status);
133     currency2 = uenum_next(en, NULL, &status);
134     if (U_FAILURE(status)) {
135        log_err("Error: uenum_next or uenum_reset returned %s\n", myErrorName(status));
136        return;
137     }
138     /* The first item's pointer in the list should be the same between resets. */
139     if (currency1 != currency2) {
140        log_err("Error: reset doesn't work %s != %s\n", currency1, currency2);
141     }
142     uenum_close(en);
143 }
144 
checkItemCount(uint32_t currencyType)145 static int32_t checkItemCount(uint32_t currencyType) {
146     UErrorCode status = U_ZERO_ERROR;
147     int32_t originalCount, count;
148     UEnumeration *en = ucurr_openISOCurrencies(currencyType, &status);
149     int32_t expectedLen = 3, len;
150     if (U_FAILURE(status)) {
151        log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status));
152        return -1;
153     }
154 
155     originalCount = uenum_count(en, &status);
156     for (count=0;;count++) {
157         const char *str = uenum_next(en, &len, &status);
158         if (str == NULL || len != expectedLen || (int32_t)strlen(str) != expectedLen) {
159             break;
160         }
161     }
162 
163     if (originalCount != count) {
164         log_err("Error: uenum_count returned the wrong value (type = 0x%X). Got: %d Expected %d\n",
165            currencyType, count, originalCount);
166     }
167     if (U_FAILURE(status)) {
168         log_err("Error: uenum_next got an error: %s\n", u_errorName(status));
169     }
170     uenum_close(en);
171     return count;
172 }
173 
TestEnumListCount(void)174 static void TestEnumListCount(void) {
175     checkItemCount(UCURR_ALL);
176     checkItemCount(UCURR_COMMON);
177     checkItemCount(UCURR_UNCOMMON);
178     checkItemCount(UCURR_DEPRECATED);
179     checkItemCount(UCURR_NON_DEPRECATED);
180     checkItemCount(UCURR_COMMON|UCURR_DEPRECATED);
181     checkItemCount(UCURR_COMMON|UCURR_NON_DEPRECATED);
182     checkItemCount(UCURR_UNCOMMON|UCURR_DEPRECATED);
183     checkItemCount(UCURR_UNCOMMON|UCURR_NON_DEPRECATED);
184 
185     if (checkItemCount(UCURR_DEPRECATED|UCURR_NON_DEPRECATED) != 0) {
186         log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n");
187     }
188     if (checkItemCount(UCURR_COMMON|UCURR_UNCOMMON) != 0) {
189         log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n");
190     }
191 }
192 
TestFractionDigitOverride(void)193 static void TestFractionDigitOverride(void) {
194     UErrorCode status = U_ZERO_ERROR;
195     UNumberFormat *fmt = unum_open(UNUM_CURRENCY, NULL, 0, "hu_HU", NULL, &status);
196     UChar buffer[256];
197     UChar expectedBuf[256];
198     const char expectedFirst[] = "123,46\\u00A0Ft"; /* changed to use 2 fraction digits */
199     const char expectedSecond[] = "123,46\\u00A0Ft";
200     const char expectedThird[] = "123,456\\u00A0Ft";
201     if (U_FAILURE(status)) {
202        log_data_err("Error: unum_open returned %s (Are you missing data?)\n", myErrorName(status));
203        return;
204     }
205     /* Make sure that you can format normal fraction digits. */
206     unum_formatDouble(fmt, 123.456, buffer, UPRV_LENGTHOF(buffer), NULL, &status);
207     u_unescape(expectedFirst, expectedBuf, (int32_t)strlen(expectedFirst)+1);
208     if (u_strcmp(buffer, expectedBuf) != 0) {
209        log_err("Error: unum_formatDouble didn't return %s\n", expectedFirst);
210     }
211     /* Make sure that you can format 2 fraction digits. */
212     unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 2);
213     unum_formatDouble(fmt, 123.456, buffer, UPRV_LENGTHOF(buffer), NULL, &status);
214     u_unescape(expectedSecond, expectedBuf, (int32_t)strlen(expectedSecond)+1);
215     if (u_strcmp(buffer, expectedBuf) != 0) {
216        log_err("Error: unum_formatDouble didn't return %s\n", expectedSecond);
217     }
218     /* Make sure that you can format more fraction digits. */
219     unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 3);
220     unum_formatDouble(fmt, 123.456, buffer, UPRV_LENGTHOF(buffer), NULL, &status);
221     u_unescape(expectedThird, expectedBuf, (int32_t)strlen(expectedThird)+1);
222     if (u_strcmp(buffer, expectedBuf) != 0) {
223        log_err("Error: unum_formatDouble didn't return %s\n", expectedThird);
224     }
225     unum_close(fmt);
226 }
227 
TestPrefixSuffix(void)228 static void TestPrefixSuffix(void) {
229     int32_t	pos;
230     UErrorCode status;
231     double result1 = 0.0, result2 = 0.0;
232     UNumberFormat* parser;
233     UChar buffer[4];
234     static const UChar TEST_NUMBER[] = {0x0024,0x0031,0x0032,0x002E,0x0030,0x0030,0}; /* $12.00 */
235     static const UChar NEG_PREFIX[] = {0x005B,0}; /* "[" */
236     static const UChar NEG_SUFFIX[] = {0x005D,0}; /* "]" */
237 
238 
239 	status = U_ZERO_ERROR;
240 	parser = unum_open(UNUM_CURRENCY, NULL, -1, "en_US", NULL, &status);
241     if (U_FAILURE(status)) {
242        log_data_err("Error: unum_open returned %s (Are you missing data?)\n", u_errorName(status));
243        return;
244     }
245 
246 	pos = 0;
247 	status = U_ZERO_ERROR;
248 	result1 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status);
249 
250 	unum_setTextAttribute(parser, UNUM_NEGATIVE_SUFFIX, NEG_SUFFIX, -1, &status);
251 	unum_setTextAttribute(parser, UNUM_NEGATIVE_PREFIX, NEG_PREFIX, -1, &status);
252     if (U_FAILURE(status)) {
253        log_err("Error: unum_setTextAttribute returned %s\n", u_errorName(status));
254        return;
255     }
256 
257 	pos = 0;
258 	result2 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status);
259     if (result1 != result2 || U_FAILURE(status)) {
260        log_err("Error: unum_parseDoubleCurrency didn't return the same value for same string %f %f %s\n",
261            result1, result2, u_errorName(status));
262     }
263     unum_close(parser);
264 }
265 
266 typedef struct {
267     const char* alphaCode;
268     int32_t     numericCode;
269 } NumCodeTestEntry;
270 
271 static const NumCodeTestEntry NUMCODE_TESTDATA[] = {
272     {"USD", 840},
273     {"Usd", 840},   /* mixed casing */
274     {"EUR", 978},
275     {"JPY", 392},
276     {"XFU", 0},     /* XFU: no numeric code  */
277     {"ZZZ", 0},     /* ZZZ: undefined ISO currency code */
278     {"bogus", 0},   /* bogus code */
279     {0, 0},
280 };
281 
TestNumericCode(void)282 static void TestNumericCode(void) {
283     UChar code[8];  // at least one longer than the longest alphaCode
284     int32_t i;
285     int32_t numCode;
286 
287     for (i = 0; NUMCODE_TESTDATA[i].alphaCode; i++) {
288         int32_t length = (int32_t)uprv_strlen(NUMCODE_TESTDATA[i].alphaCode);
289         u_charsToUChars(NUMCODE_TESTDATA[i].alphaCode, code, length + 1);  // +1 includes the NUL
290         numCode = ucurr_getNumericCode(code);
291         if (numCode != NUMCODE_TESTDATA[i].numericCode) {
292             log_data_err("Error: ucurr_getNumericCode returned %d for currency %s, expected - %d\n",
293                 numCode, NUMCODE_TESTDATA[i].alphaCode, NUMCODE_TESTDATA[i].numericCode);
294         }
295     }
296 }
297 
298 void addCurrencyTest(TestNode** root);
299 
300 #define TESTCASE(x) addTest(root, &x, "tsformat/currtest/" #x)
301 
addCurrencyTest(TestNode ** root)302 void addCurrencyTest(TestNode** root)
303 {
304     TESTCASE(TestEnumList);
305     TESTCASE(TestEnumListReset);
306     TESTCASE(TestEnumListCount);
307     TESTCASE(TestFractionDigitOverride);
308     TESTCASE(TestPrefixSuffix);
309     TESTCASE(TestNumericCode);
310 }
311 
312 #endif /* #if !UCONFIG_NO_FORMATTING */
313