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