1 /********************************************************************
3 * Copyright (c) 2005-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 #include "unicode/utypes.h"
8 #if !UCONFIG_NO_FORMATTING
9 #include "unicode/unum.h"
10 #include "unicode/ucurr.h"
11 #include "unicode/ustring.h"
16 static void expectInList(const char *isoCurrency
, uint32_t currencyType
, UBool isExpected
) {
17 UErrorCode status
= U_ZERO_ERROR
;
18 const char *foundCurrency
= NULL
;
19 const char *currentCurrency
;
20 UEnumeration
*en
= ucurr_openISOCurrencies(currencyType
, &status
);
21 if (U_FAILURE(status
)) {
22 log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status
));
26 while ((currentCurrency
= uenum_next(en
, NULL
, &status
)) != NULL
) {
27 if (strcmp(isoCurrency
, currentCurrency
) == 0) {
28 foundCurrency
= currentCurrency
;
33 if ((foundCurrency
!= NULL
) != isExpected
) {
34 log_err("Error: could not find %s as expected. isExpected = %s type=0x%X\n",
35 isoCurrency
, isExpected
? "TRUE" : "FALSE", currencyType
);
40 static void TestEnumList(void) {
41 expectInList("ADP", UCURR_ALL
, TRUE
); /* First in list */
42 expectInList("ZWD", UCURR_ALL
, TRUE
); /* Last in list */
44 expectInList("USD", UCURR_ALL
, TRUE
);
45 expectInList("USD", UCURR_COMMON
, TRUE
);
46 expectInList("USD", UCURR_UNCOMMON
, FALSE
);
47 expectInList("USD", UCURR_DEPRECATED
, FALSE
);
48 expectInList("USD", UCURR_NON_DEPRECATED
, TRUE
);
49 expectInList("USD", UCURR_COMMON
|UCURR_DEPRECATED
, FALSE
);
50 expectInList("USD", UCURR_COMMON
|UCURR_NON_DEPRECATED
, TRUE
);
51 expectInList("USD", UCURR_UNCOMMON
|UCURR_DEPRECATED
, FALSE
);
52 expectInList("USD", UCURR_UNCOMMON
|UCURR_NON_DEPRECATED
, FALSE
);
54 expectInList("USN", UCURR_ALL
, TRUE
);
55 expectInList("USN", UCURR_COMMON
, FALSE
);
56 expectInList("USN", UCURR_UNCOMMON
, TRUE
);
57 expectInList("USN", UCURR_DEPRECATED
, FALSE
);
58 expectInList("USN", UCURR_NON_DEPRECATED
, TRUE
);
59 expectInList("USN", UCURR_COMMON
|UCURR_DEPRECATED
, FALSE
);
60 expectInList("USN", UCURR_COMMON
|UCURR_NON_DEPRECATED
, FALSE
);
61 expectInList("USN", UCURR_UNCOMMON
|UCURR_DEPRECATED
, FALSE
);
62 expectInList("USN", UCURR_UNCOMMON
|UCURR_NON_DEPRECATED
, TRUE
);
64 expectInList("DEM", UCURR_ALL
, TRUE
);
65 expectInList("DEM", UCURR_COMMON
, TRUE
);
66 expectInList("DEM", UCURR_UNCOMMON
, FALSE
);
67 expectInList("DEM", UCURR_DEPRECATED
, TRUE
);
68 expectInList("DEM", UCURR_NON_DEPRECATED
, FALSE
);
69 expectInList("DEM", UCURR_COMMON
|UCURR_DEPRECATED
, TRUE
);
70 expectInList("DEM", UCURR_COMMON
|UCURR_NON_DEPRECATED
, FALSE
);
71 expectInList("DEM", UCURR_UNCOMMON
|UCURR_DEPRECATED
, FALSE
);
72 expectInList("DEM", UCURR_UNCOMMON
|UCURR_NON_DEPRECATED
, FALSE
);
74 expectInList("XEU", UCURR_ALL
, TRUE
);
75 expectInList("XEU", UCURR_COMMON
, FALSE
);
76 expectInList("XEU", UCURR_UNCOMMON
, TRUE
);
77 expectInList("XEU", UCURR_DEPRECATED
, TRUE
);
78 expectInList("XEU", UCURR_NON_DEPRECATED
, FALSE
);
79 expectInList("XEU", UCURR_COMMON
|UCURR_DEPRECATED
, FALSE
);
80 expectInList("XEU", UCURR_COMMON
|UCURR_NON_DEPRECATED
, FALSE
);
81 expectInList("XEU", UCURR_UNCOMMON
|UCURR_DEPRECATED
, TRUE
);
82 expectInList("XEU", UCURR_UNCOMMON
|UCURR_NON_DEPRECATED
, FALSE
);
86 static void TestEnumListReset(void) {
87 UErrorCode status
= U_ZERO_ERROR
;
88 const char *currency1
;
89 const char *currency2
;
90 UEnumeration
*en
= ucurr_openISOCurrencies(UCURR_ALL
, &status
);
91 if (U_FAILURE(status
)) {
92 log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status
));
96 currency1
= uenum_next(en
, NULL
, &status
);
97 uenum_reset(en
, &status
);
98 currency2
= uenum_next(en
, NULL
, &status
);
99 if (U_FAILURE(status
)) {
100 log_err("Error: uenum_next or uenum_reset returned %s\n", myErrorName(status
));
103 /* The first item's pointer in the list should be the same between resets. */
104 if (currency1
!= currency2
) {
105 log_err("Error: reset doesn't work %s != %s\n", currency1
, currency2
);
110 static int32_t checkItemCount(uint32_t currencyType
) {
111 UErrorCode status
= U_ZERO_ERROR
;
112 int32_t originalCount
, count
;
113 UEnumeration
*en
= ucurr_openISOCurrencies(currencyType
, &status
);
114 int32_t expectedLen
= 3, len
;
115 if (U_FAILURE(status
)) {
116 log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status
));
120 originalCount
= uenum_count(en
, &status
);
121 for (count
=0;;count
++) {
122 const char *str
= uenum_next(en
, &len
, &status
);
123 if (str
== NULL
|| len
!= expectedLen
|| strlen(str
) != expectedLen
) {
128 if (originalCount
!= count
) {
129 log_err("Error: uenum_count returned the wrong value (type = 0x%X). Got: %d Expected %d\n",
130 currencyType
, count
, originalCount
);
132 if (U_FAILURE(status
)) {
133 log_err("Error: uenum_next got an error: %s\n", u_errorName(status
));
139 static void TestEnumListCount(void) {
140 checkItemCount(UCURR_ALL
);
141 checkItemCount(UCURR_COMMON
);
142 checkItemCount(UCURR_UNCOMMON
);
143 checkItemCount(UCURR_DEPRECATED
);
144 checkItemCount(UCURR_NON_DEPRECATED
);
145 checkItemCount(UCURR_COMMON
|UCURR_DEPRECATED
);
146 checkItemCount(UCURR_COMMON
|UCURR_NON_DEPRECATED
);
147 checkItemCount(UCURR_UNCOMMON
|UCURR_DEPRECATED
);
148 checkItemCount(UCURR_UNCOMMON
|UCURR_NON_DEPRECATED
);
150 if (checkItemCount(UCURR_DEPRECATED
|UCURR_NON_DEPRECATED
) != 0) {
151 log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n");
153 if (checkItemCount(UCURR_COMMON
|UCURR_UNCOMMON
) != 0) {
154 log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n");
158 static void TestFractionDigitOverride(void) {
159 UErrorCode status
= U_ZERO_ERROR
;
160 UNumberFormat
*fmt
= unum_open(UNUM_CURRENCY
, NULL
, 0, "hu_HU", NULL
, &status
);
162 UChar expectedBuf
[256];
163 const char expectedFirst
[] = "123,46\\u00A0Ft"; /* changed to use 2 fraction digits */
164 const char expectedSecond
[] = "123,46\\u00A0Ft";
165 const char expectedThird
[] = "123,456\\u00A0Ft";
166 if (U_FAILURE(status
)) {
167 log_data_err("Error: unum_open returned %s (Are you missing data?)\n", myErrorName(status
));
170 /* Make sure that you can format normal fraction digits. */
171 unum_formatDouble(fmt
, 123.456, buffer
, UPRV_LENGTHOF(buffer
), NULL
, &status
);
172 u_unescape(expectedFirst
, expectedBuf
, strlen(expectedFirst
)+1);
173 if (u_strcmp(buffer
, expectedBuf
) != 0) {
174 log_err("Error: unum_formatDouble didn't return %s\n", expectedFirst
);
176 /* Make sure that you can format 2 fraction digits. */
177 unum_setAttribute(fmt
, UNUM_FRACTION_DIGITS
, 2);
178 unum_formatDouble(fmt
, 123.456, buffer
, UPRV_LENGTHOF(buffer
), NULL
, &status
);
179 u_unescape(expectedSecond
, expectedBuf
, strlen(expectedSecond
)+1);
180 if (u_strcmp(buffer
, expectedBuf
) != 0) {
181 log_err("Error: unum_formatDouble didn't return %s\n", expectedSecond
);
183 /* Make sure that you can format more fraction digits. */
184 unum_setAttribute(fmt
, UNUM_FRACTION_DIGITS
, 3);
185 unum_formatDouble(fmt
, 123.456, buffer
, UPRV_LENGTHOF(buffer
), NULL
, &status
);
186 u_unescape(expectedThird
, expectedBuf
, strlen(expectedThird
)+1);
187 if (u_strcmp(buffer
, expectedBuf
) != 0) {
188 log_err("Error: unum_formatDouble didn't return %s\n", expectedThird
);
193 static void TestPrefixSuffix(void) {
196 double result1
= 0.0, result2
= 0.0;
197 UNumberFormat
* parser
;
199 static const UChar TEST_NUMBER
[] = {0x0024,0x0031,0x0032,0x002E,0x0030,0x0030,0}; /* $12.00 */
200 static const UChar NEG_PREFIX
[] = {0x005B,0}; /* "[" */
201 static const UChar NEG_SUFFIX
[] = {0x005D,0}; /* "]" */
204 status
= U_ZERO_ERROR
;
205 parser
= unum_open(UNUM_CURRENCY
, NULL
, -1, "en_US", NULL
, &status
);
206 if (U_FAILURE(status
)) {
207 log_data_err("Error: unum_open returned %s (Are you missing data?)\n", u_errorName(status
));
212 status
= U_ZERO_ERROR
;
213 result1
= unum_parseDoubleCurrency(parser
, TEST_NUMBER
, -1, &pos
, buffer
, &status
);
215 unum_setTextAttribute(parser
, UNUM_NEGATIVE_SUFFIX
, NEG_SUFFIX
, -1, &status
);
216 unum_setTextAttribute(parser
, UNUM_NEGATIVE_PREFIX
, NEG_PREFIX
, -1, &status
);
217 if (U_FAILURE(status
)) {
218 log_err("Error: unum_setTextAttribute returned %s\n", u_errorName(status
));
223 result2
= unum_parseDoubleCurrency(parser
, TEST_NUMBER
, -1, &pos
, buffer
, &status
);
224 if (result1
!= result2
|| U_FAILURE(status
)) {
225 log_err("Error: unum_parseDoubleCurrency didn't return the same value for same string %f %f %s\n",
226 result1
, result2
, u_errorName(status
));
232 const char* alphaCode
;
236 static const NumCodeTestEntry NUMCODE_TESTDATA
[] = {
238 {"Usd", 840}, /* mixed casing */
241 {"XFU", 0}, /* XFU: no numeric code */
242 {"ZZZ", 0}, /* ZZZ: undefined ISO currency code */
243 {"bogus", 0}, /* bogus code */
247 static void TestNumericCode(void) {
248 UChar code
[8]; // at least one longer than the longest alphaCode
252 for (i
= 0; NUMCODE_TESTDATA
[i
].alphaCode
; i
++) {
253 int32_t length
= uprv_strlen(NUMCODE_TESTDATA
[i
].alphaCode
);
254 u_charsToUChars(NUMCODE_TESTDATA
[i
].alphaCode
, code
, length
+ 1); // +1 includes the NUL
255 numCode
= ucurr_getNumericCode(code
);
256 if (numCode
!= NUMCODE_TESTDATA
[i
].numericCode
) {
257 log_data_err("Error: ucurr_getNumericCode returned %d for currency %s, expected - %d\n",
258 numCode
, NUMCODE_TESTDATA
[i
].alphaCode
, NUMCODE_TESTDATA
[i
].numericCode
);
263 void addCurrencyTest(TestNode
** root
);
265 #define TESTCASE(x) addTest(root, &x, "tsformat/currtest/" #x)
267 void addCurrencyTest(TestNode
** root
)
269 TESTCASE(TestEnumList
);
270 TESTCASE(TestEnumListReset
);
271 TESTCASE(TestEnumListCount
);
272 TESTCASE(TestFractionDigitOverride
);
273 TESTCASE(TestPrefixSuffix
);
274 TESTCASE(TestNumericCode
);
277 #endif /* #if !UCONFIG_NO_FORMATTING */