OLD | NEW |
1 /******************************************************************** | 1 /******************************************************************** |
2 * COPYRIGHT: | 2 * COPYRIGHT: |
3 * Copyright (c) 1997-2013, International Business Machines Corporation and | 3 * Copyright (c) 1997-2014, International Business Machines Corporation and |
4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
5 ********************************************************************/ | 5 ********************************************************************/ |
6 /*******************************************************************************
* | 6 /*******************************************************************************
* |
7 * | 7 * |
8 * File CNUMTST.C | 8 * File CNUMTST.C |
9 * | 9 * |
10 * Madhu Katragadda Creation | 10 * Madhu Katragadda Creation |
11 * | 11 * |
12 * Modification History: | 12 * Modification History: |
13 * | 13 * |
14 * Date Name Description | 14 * Date Name Description |
15 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixe
s | 15 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixe
s |
16 * 07/15/99 helena Ported to HPUX 10/11 CC. | 16 * 07/15/99 helena Ported to HPUX 10/11 CC. |
17 ********************************************************************************
* | 17 ********************************************************************************
* |
18 */ | 18 */ |
19 | 19 |
20 /* C API TEST FOR NUMBER FORMAT */ | 20 /* C API TEST FOR NUMBER FORMAT */ |
21 | 21 |
22 #include "unicode/utypes.h" | 22 #include "unicode/utypes.h" |
23 | 23 |
24 #if !UCONFIG_NO_FORMATTING | 24 #if !UCONFIG_NO_FORMATTING |
25 | 25 |
26 #include "unicode/uloc.h" | 26 #include "unicode/uloc.h" |
27 #include "unicode/umisc.h" | 27 #include "unicode/umisc.h" |
28 #include "unicode/unum.h" | 28 #include "unicode/unum.h" |
29 #include "unicode/unumsys.h" | 29 #include "unicode/unumsys.h" |
30 #include "unicode/ustring.h" | 30 #include "unicode/ustring.h" |
| 31 #include "unicode/udisplaycontext.h" |
31 | 32 |
32 #include "cintltst.h" | 33 #include "cintltst.h" |
33 #include "cnumtst.h" | 34 #include "cnumtst.h" |
34 #include "cmemory.h" | 35 #include "cmemory.h" |
35 #include "cstring.h" | 36 #include "cstring.h" |
36 #include "putilimp.h" | 37 #include "putilimp.h" |
37 #include <stdio.h> | 38 #include <stdio.h> |
| 39 #include <stdlib.h> |
38 | 40 |
39 #define LENGTH(arr) (sizeof(arr)/sizeof(arr[0])) | 41 #define LENGTH(arr) (sizeof(arr)/sizeof(arr[0])) |
40 | 42 |
41 static const char *tagAssert(const char *f, int32_t l, const char *msg) { | 43 static const char *tagAssert(const char *f, int32_t l, const char *msg) { |
42 static char _fileline[1000]; | 44 static char _fileline[1000]; |
43 sprintf(_fileline, "%s:%d: ASSERT_TRUE(%s)", f, l, msg); | 45 sprintf(_fileline, "%s:%d: ASSERT_TRUE(%s)", f, l, msg); |
44 return _fileline; | 46 return _fileline; |
45 } | 47 } |
46 | 48 |
47 #define ASSERT_TRUE(x) assertTrue(tagAssert(__FILE__, __LINE__, #x), (x)) | 49 #define ASSERT_TRUE(x) assertTrue(tagAssert(__FILE__, __LINE__, #x), (x)) |
48 | 50 |
49 void addNumForTest(TestNode** root); | 51 void addNumForTest(TestNode** root); |
50 static void TestTextAttributeCrash(void); | 52 static void TestTextAttributeCrash(void); |
51 static void TestNBSPInPattern(void); | 53 static void TestNBSPInPattern(void); |
52 static void TestInt64Parse(void); | 54 static void TestInt64Parse(void); |
53 static void TestParseCurrency(void); | 55 static void TestParseCurrency(void); |
54 static void TestMaxInt(void); | 56 static void TestMaxInt(void); |
55 static void TestNoExponent(void); | 57 static void TestNoExponent(void); |
56 static void TestUFormattable(void); | 58 static void TestUFormattable(void); |
57 static void TestUNumberingSystem(void); | 59 static void TestUNumberingSystem(void); |
| 60 static void TestCurrencyIsoPluralFormat(void); |
| 61 static void TestContext(void); |
| 62 static void TestCurrencyUsage(void); |
| 63 static void TestCurrFmtNegSameAsPositive(void); |
58 | 64 |
59 #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x) | 65 #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x) |
60 | 66 |
61 void addNumForTest(TestNode** root) | 67 void addNumForTest(TestNode** root) |
62 { | 68 { |
63 TESTCASE(TestNumberFormat); | 69 TESTCASE(TestNumberFormat); |
64 TESTCASE(TestSpelloutNumberParse); | 70 TESTCASE(TestSpelloutNumberParse); |
65 TESTCASE(TestSignificantDigits); | 71 TESTCASE(TestSignificantDigits); |
66 TESTCASE(TestSigDigRounding); | 72 TESTCASE(TestSigDigRounding); |
67 TESTCASE(TestNumberFormatPadding); | 73 TESTCASE(TestNumberFormatPadding); |
68 TESTCASE(TestInt64Format); | 74 TESTCASE(TestInt64Format); |
69 TESTCASE(TestNonExistentCurrency); | 75 TESTCASE(TestNonExistentCurrency); |
70 TESTCASE(TestCurrencyRegression); | 76 TESTCASE(TestCurrencyRegression); |
71 TESTCASE(TestTextAttributeCrash); | 77 TESTCASE(TestTextAttributeCrash); |
72 TESTCASE(TestRBNFFormat); | 78 TESTCASE(TestRBNFFormat); |
73 TESTCASE(TestNBSPInPattern); | 79 TESTCASE(TestNBSPInPattern); |
74 TESTCASE(TestInt64Parse); | 80 TESTCASE(TestInt64Parse); |
75 TESTCASE(TestParseZero); | 81 TESTCASE(TestParseZero); |
76 TESTCASE(TestParseCurrency); | 82 TESTCASE(TestParseCurrency); |
77 TESTCASE(TestCloneWithRBNF); | 83 TESTCASE(TestCloneWithRBNF); |
78 TESTCASE(TestMaxInt); | 84 TESTCASE(TestMaxInt); |
79 TESTCASE(TestNoExponent); | 85 TESTCASE(TestNoExponent); |
80 TESTCASE(TestUFormattable); | 86 TESTCASE(TestUFormattable); |
81 TESTCASE(TestUNumberingSystem); | 87 TESTCASE(TestUNumberingSystem); |
| 88 TESTCASE(TestCurrencyIsoPluralFormat); |
| 89 TESTCASE(TestContext); |
| 90 TESTCASE(TestCurrencyUsage); |
| 91 TESTCASE(TestCurrFmtNegSameAsPositive); |
82 } | 92 } |
83 | 93 |
84 /* test Parse int 64 */ | 94 /* test Parse int 64 */ |
85 | 95 |
86 static void TestInt64Parse() | 96 static void TestInt64Parse() |
87 { | 97 { |
88 | 98 |
89 UErrorCode st = U_ZERO_ERROR; | 99 UErrorCode st = U_ZERO_ERROR; |
90 UErrorCode* status = &st; | 100 UErrorCode* status = &st; |
91 | 101 |
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1065 log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s pos
%d val %.1f cur %s, get %s pos %d val %.1f cur %s\n", | 1075 log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s pos
%d val %.1f cur %s, get %s pos %d val %.1f cur %s\n", |
1066 itemPtr->locale, itemPtr->descrip, | 1076 itemPtr->locale, itemPtr->descrip, |
1067 u_errorName(itemPtr->parsCurrExpectErr), itemPtr->parsCu
rrExpectPos, itemPtr->parsCurrExpectVal, itemPtr->parsCurrExpectCurr, | 1077 u_errorName(itemPtr->parsCurrExpectErr), itemPtr->parsCu
rrExpectPos, itemPtr->parsCurrExpectVal, itemPtr->parsCurrExpectCurr, |
1068 u_errorName(status), parsePos, parseVal, parseCurrB ); | 1078 u_errorName(status), parsePos, parseVal, parseCurrB ); |
1069 } | 1079 } |
1070 unum_close(unum); | 1080 unum_close(unum); |
1071 } else { | 1081 } else { |
1072 log_data_err("unexpected error in unum_open UNUM_CURRENCY for locale
%s: '%s'\n", itemPtr->locale, u_errorName(status)); | 1082 log_data_err("unexpected error in unum_open UNUM_CURRENCY for locale
%s: '%s'\n", itemPtr->locale, u_errorName(status)); |
1073 } | 1083 } |
1074 | 1084 |
1075 #if 0 | |
1076 /* Hmm, for UNUM_CURRENCY_PLURAL, currently unum_open always sets U_UNSU
PPORTED_ERROR, save this test until it is supported */ | |
1077 if (itemPtr->plurStr != NULL) { | 1085 if (itemPtr->plurStr != NULL) { |
1078 status = U_ZERO_ERROR; | 1086 status = U_ZERO_ERROR; |
1079 unum = unum_open(UNUM_CURRENCY_PLURAL, NULL, 0, itemPtr->locale, NUL
L, &status); | 1087 unum = unum_open(UNUM_CURRENCY_PLURAL, NULL, 0, itemPtr->locale, NUL
L, &status); |
1080 if (U_SUCCESS(status)) { | 1088 if (U_SUCCESS(status)) { |
1081 status = U_ZERO_ERROR; | 1089 status = U_ZERO_ERROR; |
1082 parsePos = 0; | 1090 parsePos = 0; |
1083 parseVal = unum_parseDouble(unum, itemPtr->plurStr, -1, &parsePo
s, &status); | 1091 parseVal = unum_parseDouble(unum, itemPtr->plurStr, -1, &parsePo
s, &status); |
1084 if (status != itemPtr->parsDoubExpectErr || parseVal != itemPtr-
>parsDoubExpectVal) { | 1092 if (status != itemPtr->parsDoubExpectErr || parseVal != itemPtr-
>parsDoubExpectVal) { |
1085 log_err("UNUM_CURRENCY parseDouble %s/%s, expect %s val %.1f
, get %s val %.1f\n", | 1093 log_err("UNUM_CURRENCY parseDouble %s/%s, expect %s val %.1f
, get %s val %.1f\n", |
1086 itemPtr->locale, itemPtr->descrip, | 1094 itemPtr->locale, itemPtr->descrip, |
(...skipping 10 matching lines...) Expand all Loading... |
1097 log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s
val %.1f cur %s, get %s val %.1f cur %s\n", | 1105 log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s
val %.1f cur %s, get %s val %.1f cur %s\n", |
1098 itemPtr->locale, itemPtr->descrip, | 1106 itemPtr->locale, itemPtr->descrip, |
1099 u_errorName(itemPtr->parsCurrExpectErr), itemPtr->pa
rsCurrExpectVal, itemPtr->parsCurrExpectCurr, | 1107 u_errorName(itemPtr->parsCurrExpectErr), itemPtr->pa
rsCurrExpectVal, itemPtr->parsCurrExpectCurr, |
1100 u_errorName(status), parseVal, parseCurrB ); | 1108 u_errorName(status), parseVal, parseCurrB ); |
1101 } | 1109 } |
1102 unum_close(unum); | 1110 unum_close(unum); |
1103 } else { | 1111 } else { |
1104 log_data_err("unexpected error in unum_open UNUM_CURRENCY_PLURAL
for locale %s: '%s'\n", itemPtr->locale, u_errorName(status)); | 1112 log_data_err("unexpected error in unum_open UNUM_CURRENCY_PLURAL
for locale %s: '%s'\n", itemPtr->locale, u_errorName(status)); |
1105 } | 1113 } |
1106 } | 1114 } |
1107 #endif | |
1108 } | 1115 } |
1109 } | 1116 } |
1110 | 1117 |
1111 typedef struct { | 1118 typedef struct { |
1112 const char * testname; | 1119 const char * testname; |
1113 const char * locale; | 1120 const char * locale; |
1114 const UChar * source; | 1121 const UChar * source; |
1115 int32_t startPos; | 1122 int32_t startPos; |
1116 int32_t value; | 1123 int32_t value; |
1117 int32_t endPos; | 1124 int32_t endPos; |
(...skipping 25 matching lines...) Expand all Loading... |
1143 { "ja123", "ja", ustr_ja123, 1, 23, 4, U_ZERO_ERROR }, | 1150 { "ja123", "ja", ustr_ja123, 1, 23, 4, U_ZERO_ERROR }, |
1144 { "ja123", "fr", ustr_ja123, 0, 0, 0, U_PARSE_ERROR }, | 1151 { "ja123", "fr", ustr_ja123, 0, 0, 0, U_PARSE_ERROR }, |
1145 { NULL, NULL, NULL, 0, 0, 0, 0 } /* terminator */ | 1152 { NULL, NULL, NULL, 0, 0, 0, 0 } /* terminator */ |
1146 }; | 1153 }; |
1147 | 1154 |
1148 static void TestSpelloutNumberParse() | 1155 static void TestSpelloutNumberParse() |
1149 { | 1156 { |
1150 const SpelloutParseTest * testPtr; | 1157 const SpelloutParseTest * testPtr; |
1151 for (testPtr = spelloutParseTests; testPtr->testname != NULL; ++testPtr) { | 1158 for (testPtr = spelloutParseTests; testPtr->testname != NULL; ++testPtr) { |
1152 UErrorCode status = U_ZERO_ERROR; | 1159 UErrorCode status = U_ZERO_ERROR; |
1153 int32_t»value, position = testPtr->startPos; | 1160 int32_t value, position = testPtr->startPos; |
1154 UNumberFormat *nf = unum_open(UNUM_SPELLOUT, NULL, 0, testPtr->locale, N
ULL, &status); | 1161 UNumberFormat *nf = unum_open(UNUM_SPELLOUT, NULL, 0, testPtr->locale, N
ULL, &status); |
1155 if (U_FAILURE(status)) { | 1162 if (U_FAILURE(status)) { |
1156 log_err_status(status, "unum_open fails for UNUM_SPELLOUT with local
e %s, status %s\n", testPtr->locale, myErrorName(status)); | 1163 log_err_status(status, "unum_open fails for UNUM_SPELLOUT with local
e %s, status %s\n", testPtr->locale, myErrorName(status)); |
1157 continue; | 1164 continue; |
1158 } | 1165 } |
1159 value = unum_parse(nf, testPtr->source, -1, &position, &status); | 1166 value = unum_parse(nf, testPtr->source, -1, &position, &status); |
1160 if ( value != testPtr->value || position != testPtr->endPos || status !=
testPtr->status ) { | 1167 if ( value != testPtr->value || position != testPtr->endPos || status !=
testPtr->status ) { |
1161 log_err("unum_parse SPELLOUT, locale %s, testname %s, startPos %d: f
or value / endPos / status, expected %d / %d / %s, got %d / %d / %s\n", | 1168 log_err("unum_parse SPELLOUT, locale %s, testname %s, startPos %d: f
or value / endPos / status, expected %d / %d / %s, got %d / %d / %s\n", |
1162 testPtr->locale, testPtr->testname, testPtr->startPos, | 1169 testPtr->locale, testPtr->testname, testPtr->startPos, |
1163 testPtr->value, testPtr->endPos, myErrorName(testPtr->status
), | 1170 testPtr->value, testPtr->endPos, myErrorName(testPtr->status
), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1207 log_err("FAIL: Error in number formatting using unum_formatDouble()\n"); | 1214 log_err("FAIL: Error in number formatting using unum_formatDouble()\n"); |
1208 free(result); | 1215 free(result); |
1209 unum_close(fmt); | 1216 unum_close(fmt); |
1210 } | 1217 } |
1211 | 1218 |
1212 static void TestSigDigRounding() | 1219 static void TestSigDigRounding() |
1213 { | 1220 { |
1214 UErrorCode status = U_ZERO_ERROR; | 1221 UErrorCode status = U_ZERO_ERROR; |
1215 UChar expected[128]; | 1222 UChar expected[128]; |
1216 UChar result[128]; | 1223 UChar result[128]; |
1217 char» » temp1[128]; | 1224 char temp1[128]; |
1218 char» » temp2[128]; | 1225 char temp2[128]; |
1219 UNumberFormat* fmt; | 1226 UNumberFormat* fmt; |
1220 double d = 123.4; | 1227 double d = 123.4; |
1221 | 1228 |
1222 fmt=unum_open(UNUM_DECIMAL, NULL, 0, NULL /* "en_US"*/, NULL, &status); | 1229 fmt=unum_open(UNUM_DECIMAL, NULL, 0, NULL /* "en_US"*/, NULL, &status); |
1223 if (U_FAILURE(status)) { | 1230 if (U_FAILURE(status)) { |
1224 log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(s
tatus)); | 1231 log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(s
tatus)); |
1225 return; | 1232 return; |
1226 } | 1233 } |
1227 unum_setAttribute(fmt, UNUM_LENIENT_PARSE, FALSE); | 1234 unum_setAttribute(fmt, UNUM_LENIENT_PARSE, FALSE); |
1228 unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE); | 1235 unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE); |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1471 | 1478 |
1472 status = U_ZERO_ERROR; | 1479 status = U_ZERO_ERROR; |
1473 parsepos = 0; | 1480 parsepos = 0; |
1474 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepo
s, &status); | 1481 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepo
s, &status); |
1475 if (U_FAILURE(status)) { | 1482 if (U_FAILURE(status)) { |
1476 log_err("parseDouble returned error: %s\n", myErrorName(status))
; | 1483 log_err("parseDouble returned error: %s\n", myErrorName(status))
; |
1477 } else if (!withinErr(valDouble, doubleBig, 1e-15)) { | 1484 } else if (!withinErr(valDouble, doubleBig, 1e-15)) { |
1478 log_err("parseDouble returned incorrect value, got: %g\n", valDo
uble); | 1485 log_err("parseDouble returned incorrect value, got: %g\n", valDo
uble); |
1479 } | 1486 } |
1480 } | 1487 } |
1481 » » | 1488 |
1482 » » u_uastrcpy(result, "5.06e-27"); | 1489 u_uastrcpy(result, "5.06e-27"); |
1483 parsepos = 0; | 1490 parsepos = 0; |
1484 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &
status); | 1491 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &
status); |
1485 if (U_FAILURE(status)) { | 1492 if (U_FAILURE(status)) { |
1486 log_err("parseDouble() returned error: %s\n", myErrorName(status)); | 1493 log_err("parseDouble() returned error: %s\n", myErrorName(status)); |
1487 } else if (!withinErr(valDouble, 5.06e-27, 1e-15)) { | 1494 } else if (!withinErr(valDouble, 5.06e-27, 1e-15)) { |
1488 log_err("parseDouble() returned incorrect value, got: %g\n", valDoub
le); | 1495 log_err("parseDouble() returned incorrect value, got: %g\n", valDoub
le); |
1489 } | 1496 } |
1490 } | 1497 } |
1491 unum_close(fmt); | 1498 unum_close(fmt); |
1492 } | 1499 } |
(...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2332 if ( U_SUCCESS(status) ) { | 2339 if ( U_SUCCESS(status) ) { |
2333 UChar ubuf[kNumSysDescripBufMax]; | 2340 UChar ubuf[kNumSysDescripBufMax]; |
2334 int32_t ulen, radix = unumsys_getRadix(unumsys); | 2341 int32_t ulen, radix = unumsys_getRadix(unumsys); |
2335 UBool isAlgorithmic = unumsys_isAlgorithmic(unumsys); | 2342 UBool isAlgorithmic = unumsys_isAlgorithmic(unumsys); |
2336 numsys = unumsys_getName(unumsys); | 2343 numsys = unumsys_getName(unumsys); |
2337 if ( uprv_strcmp(numsys, itemPtr->numsys) != 0 || radix != itemPtr->
radix || !isAlgorithmic != !itemPtr->isAlgorithmic ) { | 2344 if ( uprv_strcmp(numsys, itemPtr->numsys) != 0 || radix != itemPtr->
radix || !isAlgorithmic != !itemPtr->isAlgorithmic ) { |
2338 log_data_err("unumsys name/radix/isAlgorithmic for locale %s, ex
pected %s/%d/%d, got %s/%d/%d\n", | 2345 log_data_err("unumsys name/radix/isAlgorithmic for locale %s, ex
pected %s/%d/%d, got %s/%d/%d\n", |
2339 itemPtr->locale, itemPtr->numsys, itemPtr->radix, itemPt
r->isAlgorithmic, numsys, radix, isAlgorithmic); | 2346 itemPtr->locale, itemPtr->numsys, itemPtr->radix, itemPt
r->isAlgorithmic, numsys, radix, isAlgorithmic); |
2340 } | 2347 } |
2341 ulen = unumsys_getDescription(unumsys, ubuf, kNumSysDescripBufMax, &
status); | 2348 ulen = unumsys_getDescription(unumsys, ubuf, kNumSysDescripBufMax, &
status); |
| 2349 (void)ulen; // Suppress variable not used warning. |
2342 if ( U_FAILURE(status) || u_strcmp(ubuf, itemPtr->description) != 0
) { | 2350 if ( U_FAILURE(status) || u_strcmp(ubuf, itemPtr->description) != 0
) { |
2343 log_data_err("unumsys description for locale %s, description une
xpected and/or status %\n", myErrorName(status)); | 2351 log_data_err("unumsys description for locale %s, description une
xpected and/or status %\n", myErrorName(status)); |
2344 } | 2352 } |
2345 unumsys_close(unumsys); | 2353 unumsys_close(unumsys); |
2346 } else { | 2354 } else { |
2347 log_data_err("unumsys_open for locale %s fails with status %s\n", it
emPtr->locale, myErrorName(status)); | 2355 log_data_err("unumsys_open for locale %s fails with status %s\n", it
emPtr->locale, myErrorName(status)); |
2348 } | 2356 } |
2349 } | 2357 } |
2350 | 2358 |
2351 status = U_ZERO_ERROR; | 2359 status = U_ZERO_ERROR; |
(...skipping 19 matching lines...) Expand all Loading... |
2371 uenum_close(uenum); | 2379 uenum_close(uenum); |
2372 if ( numsysCount < 40 || !foundLatn || !foundArab ) { | 2380 if ( numsysCount < 40 || !foundLatn || !foundArab ) { |
2373 log_err("unumsys_openAvailableNames results incomplete: numsysCount
%d, foundLatn %d, foundArab %d\n", | 2381 log_err("unumsys_openAvailableNames results incomplete: numsysCount
%d, foundLatn %d, foundArab %d\n", |
2374 numsysCount, foundLatn, foundArab); | 2382 numsysCount, foundLatn, foundArab); |
2375 } | 2383 } |
2376 } else { | 2384 } else { |
2377 log_data_err("unumsys_openAvailableNames fails with status %s\n", myErro
rName(status)); | 2385 log_data_err("unumsys_openAvailableNames fails with status %s\n", myErro
rName(status)); |
2378 } | 2386 } |
2379 } | 2387 } |
2380 | 2388 |
| 2389 /* plain-C version of test in numfmtst.cpp */ |
| 2390 enum { kUBufMax = 64 }; |
| 2391 static void TestCurrencyIsoPluralFormat(void) { |
| 2392 static const char* DATA[][6] = { |
| 2393 // the data are: |
| 2394 // locale, |
| 2395 // currency amount to be formatted, |
| 2396 // currency ISO code to be formatted, |
| 2397 // format result using CURRENCYSTYLE, |
| 2398 // format result using ISOCURRENCYSTYLE, |
| 2399 // format result using PLURALCURRENCYSTYLE, |
| 2400 |
| 2401 {"en_US", "1", "USD", "$1.00", "USD1.00", "1.00 US dollars"}, |
| 2402 {"en_US", "1234.56", "USD", "$1,234.56", "USD1,234.56", "1,234.56 US dol
lars"}, |
| 2403 {"en_US", "-1234.56", "USD", "-$1,234.56", "-USD1,234.56", "-1,234.56 US
dollars"}, |
| 2404 {"zh_CN", "1", "USD", "US$\\u00A01.00", "USD\\u00A01.00", "1.00\\u7F8E\\
u5143"}, |
| 2405 {"zh_CN", "1234.56", "USD", "US$\\u00A01,234.56", "USD\\u00A01,234.56",
"1,234.56\\u7F8E\\u5143"}, |
| 2406 // wrong ISO code {"zh_CN", "1", "CHY", "CHY1.00", "CHY1.00", "1.00 CHY"
}, |
| 2407 // wrong ISO code {"zh_CN", "1234.56", "CHY", "CHY1,234.56", "CHY1,234.5
6", "1,234.56 CHY"}, |
| 2408 {"zh_CN", "1", "CNY", "\\uFFE5\\u00A01.00", "CNY\\u00A01.00", "1.00\\u4E
BA\\u6C11\\u5E01"}, |
| 2409 {"zh_CN", "1234.56", "CNY", "\\uFFE5\\u00A01,234.56", "CNY\\u00A01,234.5
6", "1,234.56\\u4EBA\\u6C11\\u5E01"}, |
| 2410 {"ru_RU", "1", "RUB", "1,00\\u00A0\\u0440\\u0443\\u0431.", "1,00\\u00A0R
UB", "1,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u043
3\\u043E \\u0440\\u0443\\u0431\\u043B\\u044F"}, |
| 2411 {"ru_RU", "2", "RUB", "2,00\\u00A0\\u0440\\u0443\\u0431.", "2,00\\u00A0R
UB", "2,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u043
3\\u043E \\u0440\\u0443\\u0431\\u043B\\u044F"}, |
| 2412 {"ru_RU", "5", "RUB", "5,00\\u00A0\\u0440\\u0443\\u0431.", "5,00\\u00A0R
UB", "5,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u043
3\\u043E \\u0440\\u0443\\u0431\\u043B\\u044F"}, |
| 2413 // test locale without currency information |
| 2414 {"root", "-1.23", "USD", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 US
D"}, |
| 2415 // test choice format |
| 2416 {"es_AR", "1", "INR", "INR1,00", "INR1,00", "1,00 rupia india"}, |
| 2417 }; |
| 2418 static const UNumberFormatStyle currencyStyles[] = { |
| 2419 UNUM_CURRENCY, |
| 2420 UNUM_CURRENCY_ISO, |
| 2421 UNUM_CURRENCY_PLURAL |
| 2422 }; |
| 2423 |
| 2424 int32_t i, sIndex; |
| 2425 |
| 2426 for (i=0; i<LENGTH(DATA); ++i) { |
| 2427 const char* localeString = DATA[i][0]; |
| 2428 double numberToBeFormat = atof(DATA[i][1]); |
| 2429 const char* currencyISOCode = DATA[i][2]; |
| 2430 for (sIndex = 0; sIndex < LENGTH(currencyStyles); ++sIndex) { |
| 2431 UNumberFormatStyle style = currencyStyles[sIndex]; |
| 2432 UErrorCode status = U_ZERO_ERROR; |
| 2433 UChar currencyCode[4]; |
| 2434 UChar ubufResult[kUBufMax]; |
| 2435 UChar ubufExpected[kUBufMax]; |
| 2436 int32_t ulenRes; |
| 2437 |
| 2438 UNumberFormat* unumFmt = unum_open(style, NULL, 0, localeString, NULL, &
status); |
| 2439 if (U_FAILURE(status)) { |
| 2440 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", localeSt
ring, (int)style, myErrorName(status)); |
| 2441 continue; |
| 2442 } |
| 2443 u_charsToUChars(currencyISOCode, currencyCode, 4); |
| 2444 unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, currencyCode, 3, &sta
tus); |
| 2445 if (U_FAILURE(status)) { |
| 2446 log_err("FAIL: unum_setTextAttribute, locale %s, UNUM_CURRENCY_CODE
%s\n", localeString, currencyISOCode); |
| 2447 } |
| 2448 ulenRes = unum_formatDouble(unumFmt, numberToBeFormat, ubufResult, kUBuf
Max, NULL, &status); |
| 2449 if (U_FAILURE(status)) { |
| 2450 log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s -
%s\n", localeString, currencyISOCode, myErrorName(status)); |
| 2451 } else { |
| 2452 int32_t ulenExp = u_unescape(DATA[i][3 + sIndex], ubufExpected, kUBu
fMax); |
| 2453 if (ulenRes != ulenExp || u_strncmp(ubufResult, ubufExpected, ulenEx
p) != 0) { |
| 2454 log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE
%s, expected %s, got something else\n", |
| 2455 localeString, currencyISOCode, DATA[i][3 + sIndex]); |
| 2456 } |
| 2457 } |
| 2458 unum_close(unumFmt); |
| 2459 } |
| 2460 } |
| 2461 } |
| 2462 |
| 2463 typedef struct { |
| 2464 const char * locale; |
| 2465 UNumberFormatStyle style; |
| 2466 UDisplayContext context; |
| 2467 const char * expectedResult; |
| 2468 } TestContextItem; |
| 2469 |
| 2470 /* currently no locales have contextTransforms data for "symbol" type */ |
| 2471 static const TestContextItem tcItems[] = { /* results for 123.45 */ |
| 2472 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "e
tt\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, |
| 2473 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "E
tt\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, |
| 2474 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, "e
tt\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, |
| 2475 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, "e
tt\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, |
| 2476 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "o
ne hundred twenty-three point four five" }, |
| 2477 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "O
ne hundred twenty-three point four five" }, |
| 2478 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, "O
ne hundred twenty-three point four five" }, |
| 2479 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, "O
ne hundred twenty-three point four five" }, |
| 2480 { NULL, (UNumberFormatStyle)0, (UDisplayContext)0, NULL } |
| 2481 }; |
| 2482 |
| 2483 static void TestContext(void) { |
| 2484 UErrorCode status = U_ZERO_ERROR; |
| 2485 const TestContextItem* itemPtr; |
| 2486 |
| 2487 UNumberFormat *unum = unum_open(UNUM_SPELLOUT, NULL, 0, "en", NULL, &status)
; |
| 2488 if ( U_SUCCESS(status) ) { |
| 2489 UDisplayContext context = unum_getContext(unum, UDISPCTX_TYPE_CAPITALIZA
TION, &status); |
| 2490 if ( U_FAILURE(status) || context != UDISPCTX_CAPITALIZATION_NONE) { |
| 2491 log_err("FAIL: Initial unum_getContext is not UDISPCTX_CAPITALIZATIO
N_NONE\n"); |
| 2492 status = U_ZERO_ERROR; |
| 2493 } |
| 2494 unum_setContext(unum, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, &status); |
| 2495 context = unum_getContext(unum, UDISPCTX_TYPE_CAPITALIZATION, &status); |
| 2496 if ( U_FAILURE(status) || context != UDISPCTX_CAPITALIZATION_FOR_STANDAL
ONE) { |
| 2497 log_err("FAIL: unum_getContext does not return the value set, UDISPC
TX_CAPITALIZATION_FOR_STANDALONE\n"); |
| 2498 } |
| 2499 unum_close(unum); |
| 2500 } else { |
| 2501 log_data_err("unum_open UNUM_SPELLOUT for en fails with status %s\n", my
ErrorName(status)); |
| 2502 } |
| 2503 #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION |
| 2504 for (itemPtr = tcItems; itemPtr->locale != NULL; itemPtr++) { |
| 2505 UChar ubufResult[kUBufMax]; |
| 2506 int32_t ulenRes; |
| 2507 |
| 2508 status = U_ZERO_ERROR; |
| 2509 unum = unum_open(itemPtr->style, NULL, 0, itemPtr->locale, NULL, &status
); |
| 2510 if (U_FAILURE(status)) { |
| 2511 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", |
| 2512 itemPtr->locale, (int)itemPtr->style, myErrorName(status
)); |
| 2513 continue; |
| 2514 } |
| 2515 unum_setContext(unum, itemPtr->context, &status); |
| 2516 ulenRes = unum_formatDouble(unum, 123.45, ubufResult, kUBufMax, NULL, &s
tatus); |
| 2517 if (U_FAILURE(status)) { |
| 2518 log_err("FAIL: unum_formatDouble, locale %s, style %d, context %d -
%s\n", |
| 2519 itemPtr->locale, (int)itemPtr->style, (int)itemPtr->context,
myErrorName(status)); |
| 2520 } else { |
| 2521 UChar ubufExpected[kUBufMax]; |
| 2522 int32_t ulenExp = u_unescape(itemPtr->expectedResult, ubufExpected,
kUBufMax); |
| 2523 if (ulenRes != ulenExp || u_strncmp(ubufResult, ubufExpected, ulenEx
p) != 0) { |
| 2524 char bbuf[kUBufMax*2]; |
| 2525 u_austrncpy(bbuf, ubufResult, sizeof(bbuf)); |
| 2526 log_err("FAIL: unum_formatDouble, locale %s, style %d, context %
d, expected %d:\"%s\", got %d:\"%s\"\n", |
| 2527 itemPtr->locale, (int)itemPtr->style, (int)itemPtr->cont
ext, ulenExp, |
| 2528 itemPtr->expectedResult, ulenRes, bbuf); |
| 2529 } |
| 2530 } |
| 2531 unum_close(unum); |
| 2532 } |
| 2533 #endif /* #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION */ |
| 2534 } |
| 2535 |
| 2536 static void TestCurrencyUsage(void) { |
| 2537 static const char* DATA[][2] = { |
| 2538 /* the data are: |
| 2539 * currency ISO code to be formatted, |
| 2540 * format result using CURRENCYSTYLE with CASH purpose,- |
| 2541 * Note that as of CLDR 26:- |
| 2542 * - TWD switches from 0 decimals to 2; PKR still has 0, so change test
to that |
| 2543 * - CAD and all other currencies that rounded to .05 no longer do |
| 2544 */ |
| 2545 |
| 2546 {"PKR", "PKR124"}, |
| 2547 {"CAD", "CA$123.57"}, |
| 2548 {"USD", "$123.57"} |
| 2549 }; |
| 2550 |
| 2551 // 1st time for getter/setter, 2nd for factory method |
| 2552 int32_t i; |
| 2553 for(i=0; i<2; i++){ |
| 2554 const char* localeString = "en_US"; |
| 2555 double numberToBeFormat = 123.567; |
| 2556 UNumberFormat* unumFmt; |
| 2557 UNumberFormatStyle style = UNUM_CURRENCY; |
| 2558 UErrorCode status = U_ZERO_ERROR; |
| 2559 int32_t j; |
| 2560 |
| 2561 if(i == 1){ // change for factory method |
| 2562 style = UNUM_CASH_CURRENCY; |
| 2563 } |
| 2564 |
| 2565 unumFmt = unum_open(style, NULL, 0, localeString, NULL, &status); |
| 2566 if (U_FAILURE(status)) { |
| 2567 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", |
| 2568 localeString, (int)style, myErrorName(status)); |
| 2569 continue; |
| 2570 } |
| 2571 |
| 2572 if(i == 0){ // this is for the getter/setter |
| 2573 if(unum_getAttribute(unumFmt, UNUM_CURRENCY_USAGE) != UCURR_USAGE_ST
ANDARD) { |
| 2574 log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_STA
NDARD\n"); |
| 2575 } |
| 2576 |
| 2577 unum_setAttribute(unumFmt, UNUM_CURRENCY_USAGE, UCURR_USAGE_CASH); |
| 2578 } |
| 2579 |
| 2580 if(unum_getAttribute(unumFmt, UNUM_CURRENCY_USAGE) != UCURR_USAGE_CASH)
{ |
| 2581 log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_CASH\n"
); |
| 2582 } |
| 2583 |
| 2584 for (j=0; j<LENGTH(DATA); ++j) { |
| 2585 UChar expect[64]; |
| 2586 int32_t expectLen; |
| 2587 UChar currencyCode[4]; |
| 2588 UChar result[64]; |
| 2589 int32_t resultLen; |
| 2590 UFieldPosition pos = {0}; |
| 2591 |
| 2592 u_charsToUChars(DATA[j][0], currencyCode, 3); |
| 2593 expectLen = u_unescape(DATA[j][1], expect, LENGTH(expect)); |
| 2594 |
| 2595 unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, currencyCode, 3,
&status); |
| 2596 assertSuccess("num_setTextAttribute()", &status); |
| 2597 |
| 2598 resultLen = unum_formatDouble(unumFmt, numberToBeFormat, result, LEN
GTH(result), |
| 2599 &pos, &status); |
| 2600 assertSuccess("num_formatDouble()", &status); |
| 2601 |
| 2602 if(resultLen != expectLen || u_strcmp(result, expect) != 0) { |
| 2603 log_err("Fail: Error in Number Format Currency Purpose using unu
m_setAttribute() expected: %s, got %s\n", |
| 2604 aescstrdup(expect, expectLen), aescstrdup(result, resultLen)); |
| 2605 } |
| 2606 |
| 2607 } |
| 2608 |
| 2609 unum_close(unumFmt); |
| 2610 } |
| 2611 } |
| 2612 |
| 2613 static UChar currFmtNegSameAsPos[] = /* "\u00A4#,##0.00;\u00A4#,##0.00" */ |
| 2614 {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0x3B,0xA4,0x23,0x2C,0x23,0x23,
0x30,0x2E,0x30,0x30,0}; |
| 2615 |
| 2616 static UChar currFmtToPatExpected[] = /* "\u00A4#,##0.00" */ |
| 2617 {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0}; |
| 2618 |
| 2619 static UChar currFmtResultExpected[] = /* "$100.00" */ |
| 2620 {0x24,0x31,0x30,0x30,0x2E,0x30,0x30,0}; |
| 2621 |
| 2622 static UChar emptyString[] = {0}; |
| 2623 |
| 2624 enum { kUBufSize = 64 }; |
| 2625 |
| 2626 static void TestCurrFmtNegSameAsPositive(void) { |
| 2627 UErrorCode status = U_ZERO_ERROR; |
| 2628 UNumberFormat* unumfmt = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &s
tatus); |
| 2629 if ( U_SUCCESS(status) ) { |
| 2630 unum_applyPattern(unumfmt, FALSE, currFmtNegSameAsPos, -1, NULL, &status
); |
| 2631 if (U_SUCCESS(status)) { |
| 2632 UChar ubuf[kUBufSize]; |
| 2633 int32_t ulen = unum_toPattern(unumfmt, FALSE, ubuf, kUBufSize, &stat
us); |
| 2634 if (U_FAILURE(status)) { |
| 2635 log_err("unum_toPattern fails with status %s\n", myErrorName(sta
tus)); |
| 2636 } else if (u_strcmp(ubuf, currFmtToPatExpected) != 0) { |
| 2637 log_err("unum_toPattern result wrong, expected %s, got %s\n", ae
scstrdup(currFmtToPatExpected,-1), aescstrdup(ubuf,ulen)); |
| 2638 } |
| 2639 unum_setSymbol(unumfmt, UNUM_MINUS_SIGN_SYMBOL, emptyString, 0, &sta
tus); |
| 2640 if (U_SUCCESS(status)) { |
| 2641 ulen = unum_formatDouble(unumfmt, -100.0, ubuf, kUBufSize, NULL,
&status); |
| 2642 if (U_FAILURE(status)) { |
| 2643 log_err("unum_formatDouble fails with status %s\n", myErrorN
ame(status)); |
| 2644 } else if (u_strcmp(ubuf, currFmtResultExpected) != 0) { |
| 2645 log_err("unum_formatDouble result wrong, expected %s, got %s
\n", aescstrdup(currFmtResultExpected,-1), aescstrdup(ubuf,ulen)); |
| 2646 } |
| 2647 } else { |
| 2648 log_err("unum_setSymbol fails with status %s\n", myErrorName(sta
tus)); |
| 2649 } |
| 2650 } else { |
| 2651 log_err("unum_applyPattern fails with status %s\n", myErrorName(stat
us)); |
| 2652 } |
| 2653 unum_close(unumfmt); |
| 2654 } else { |
| 2655 log_data_err("unum_open UNUM_CURRENCY for en_US fails with status %s\n",
myErrorName(status)); |
| 2656 } |
| 2657 } |
| 2658 |
2381 #endif /* #if !UCONFIG_NO_FORMATTING */ | 2659 #endif /* #if !UCONFIG_NO_FORMATTING */ |
OLD | NEW |