1 /********************************************************************
3 * Copyright (c) 1997-2010, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/dcfmtsym.h"
12 #include "unicode/decimfmt.h"
13 #include "unicode/unum.h"
16 void IntlTestDecimalFormatSymbols::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
18 if (exec
) logln("TestSuite DecimalFormatSymbols");
20 case 0: name
= "DecimalFormatSymbols test";
22 logln("DecimalFormatSymbols test---"); logln("");
27 default: name
= ""; break;
32 * Test the API of DecimalFormatSymbols; primarily a simple get/set set.
34 void IntlTestDecimalFormatSymbols::testSymbols(/* char *par */)
36 UErrorCode status
= U_ZERO_ERROR
;
38 DecimalFormatSymbols
fr(Locale::getFrench(), status
);
39 if(U_FAILURE(status
)) {
40 errcheckln(status
, "ERROR: Couldn't create French DecimalFormatSymbols - %s", u_errorName(status
));
44 status
= U_ZERO_ERROR
;
45 DecimalFormatSymbols
en(Locale::getEnglish(), status
);
46 if(U_FAILURE(status
)) {
47 errcheckln(status
, "ERROR: Couldn't create English DecimalFormatSymbols - %s", u_errorName(status
));
51 if(en
== fr
|| ! (en
!= fr
) ) {
52 errln("ERROR: English DecimalFormatSymbols equal to French");
55 // just do some VERY basic tests to make sure that get/set work
57 UnicodeString zero
= en
.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol
);
58 fr
.setSymbol(DecimalFormatSymbols::kZeroDigitSymbol
, zero
);
59 if(fr
.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol
) != en
.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol
)) {
60 errln("ERROR: get/set ZeroDigit failed");
63 UnicodeString group
= en
.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
);
64 fr
.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
, group
);
65 if(fr
.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
) != en
.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
)) {
66 errln("ERROR: get/set GroupingSeparator failed");
69 UnicodeString decimal
= en
.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
);
70 fr
.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
, decimal
);
71 if(fr
.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
) != en
.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
)) {
72 errln("ERROR: get/set DecimalSeparator failed");
75 UnicodeString perMill
= en
.getSymbol(DecimalFormatSymbols::kPerMillSymbol
);
76 fr
.setSymbol(DecimalFormatSymbols::kPerMillSymbol
, perMill
);
77 if(fr
.getSymbol(DecimalFormatSymbols::kPerMillSymbol
) != en
.getSymbol(DecimalFormatSymbols::kPerMillSymbol
)) {
78 errln("ERROR: get/set PerMill failed");
81 UnicodeString percent
= en
.getSymbol(DecimalFormatSymbols::kPercentSymbol
);
82 fr
.setSymbol(DecimalFormatSymbols::kPercentSymbol
, percent
);
83 if(fr
.getSymbol(DecimalFormatSymbols::kPercentSymbol
) != en
.getSymbol(DecimalFormatSymbols::kPercentSymbol
)) {
84 errln("ERROR: get/set Percent failed");
87 UnicodeString
digit(en
.getSymbol(DecimalFormatSymbols::kDigitSymbol
));
88 fr
.setSymbol(DecimalFormatSymbols::kDigitSymbol
, digit
);
89 if(fr
.getSymbol(DecimalFormatSymbols::kDigitSymbol
) != en
.getSymbol(DecimalFormatSymbols::kDigitSymbol
)) {
90 errln("ERROR: get/set Percent failed");
93 UnicodeString patternSeparator
= en
.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
);
94 fr
.setSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
, patternSeparator
);
95 if(fr
.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
) != en
.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
)) {
96 errln("ERROR: get/set PatternSeparator failed");
99 UnicodeString
infinity(en
.getSymbol(DecimalFormatSymbols::kInfinitySymbol
));
100 fr
.setSymbol(DecimalFormatSymbols::kInfinitySymbol
, infinity
);
101 UnicodeString
infinity2(fr
.getSymbol(DecimalFormatSymbols::kInfinitySymbol
));
102 if(infinity
!= infinity2
) {
103 errln("ERROR: get/set Infinity failed");
106 UnicodeString
nan(en
.getSymbol(DecimalFormatSymbols::kNaNSymbol
));
107 fr
.setSymbol(DecimalFormatSymbols::kNaNSymbol
, nan
);
108 UnicodeString
nan2(fr
.getSymbol(DecimalFormatSymbols::kNaNSymbol
));
110 errln("ERROR: get/set NaN failed");
113 UnicodeString minusSign
= en
.getSymbol(DecimalFormatSymbols::kMinusSignSymbol
);
114 fr
.setSymbol(DecimalFormatSymbols::kMinusSignSymbol
, minusSign
);
115 if(fr
.getSymbol(DecimalFormatSymbols::kMinusSignSymbol
) != en
.getSymbol(DecimalFormatSymbols::kMinusSignSymbol
)) {
116 errln("ERROR: get/set MinusSign failed");
119 UnicodeString
exponential(en
.getSymbol(DecimalFormatSymbols::kExponentialSymbol
));
120 fr
.setSymbol(DecimalFormatSymbols::kExponentialSymbol
, exponential
);
121 if(fr
.getSymbol(DecimalFormatSymbols::kExponentialSymbol
) != en
.getSymbol(DecimalFormatSymbols::kExponentialSymbol
)) {
122 errln("ERROR: get/set Exponential failed");
125 // Test get currency spacing before the currency.
126 status
= U_ZERO_ERROR
;
127 for (int32_t i
= 0; i
< (int32_t)UNUM_CURRENCY_SPACING_COUNT
; i
++) {
128 UnicodeString enCurrencyPattern
= en
.getPatternForCurrencySpacing(
129 (UCurrencySpacing
)i
, TRUE
, status
);
130 if(U_FAILURE(status
)) {
131 errln("Error: cannot get CurrencyMatch for locale:en");
132 status
= U_ZERO_ERROR
;
134 UnicodeString frCurrencyPattern
= fr
.getPatternForCurrencySpacing(
135 (UCurrencySpacing
)i
, TRUE
, status
);
136 if(U_FAILURE(status
)) {
137 errln("Error: cannot get CurrencyMatch for locale:fr");
139 if (enCurrencyPattern
!= frCurrencyPattern
) {
140 errln("ERROR: get CurrencySpacing failed");
143 // Test get currencySpacing after the currency.
144 status
= U_ZERO_ERROR
;
145 for (int32_t i
= 0; i
< UNUM_CURRENCY_SPACING_COUNT
; i
++) {
146 UnicodeString enCurrencyPattern
= en
.getPatternForCurrencySpacing(
147 (UCurrencySpacing
)i
, FALSE
, status
);
148 if(U_FAILURE(status
)) {
149 errln("Error: cannot get CurrencyMatch for locale:en");
150 status
= U_ZERO_ERROR
;
152 UnicodeString frCurrencyPattern
= fr
.getPatternForCurrencySpacing(
153 (UCurrencySpacing
)i
, FALSE
, status
);
154 if(U_FAILURE(status
)) {
155 errln("Error: cannot get CurrencyMatch for locale:fr");
157 if (enCurrencyPattern
!= frCurrencyPattern
) {
158 errln("ERROR: get CurrencySpacing failed");
161 // Test set curerncySpacing APIs
162 status
= U_ZERO_ERROR
;
163 UnicodeString dash
= UnicodeString("-");
164 en
.setPatternForCurrencySpacing(UNUM_CURRENCY_INSERT
, TRUE
, dash
);
165 UnicodeString enCurrencyInsert
= en
.getPatternForCurrencySpacing(
166 UNUM_CURRENCY_INSERT
, TRUE
, status
);
167 if (dash
!= enCurrencyInsert
) {
168 errln("Error: Failed to setCurrencyInsert for locale:en");
171 status
= U_ZERO_ERROR
;
172 DecimalFormatSymbols
foo(status
);
174 DecimalFormatSymbols
bar(foo
);
178 if(en
!= fr
|| foo
!= bar
) {
179 errln("ERROR: Copy Constructor or Assignment failed");
182 // test get/setSymbol()
183 if((int) UNUM_FORMAT_SYMBOL_COUNT
!= (int) DecimalFormatSymbols::kFormatSymbolCount
) {
184 errln("unum.h and decimfmt.h have inconsistent numbers of format symbols!");
189 for(i
= 0; i
< (int)DecimalFormatSymbols::kFormatSymbolCount
; ++i
) {
190 foo
.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)i
, UnicodeString((UChar32
)(0x10330 + i
)));
192 for(i
= 0; i
< (int)DecimalFormatSymbols::kFormatSymbolCount
; ++i
) {
193 if(foo
.getSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)i
) != UnicodeString((UChar32
)(0x10330 + i
))) {
194 errln("get/setSymbol did not roundtrip, got " +
195 foo
.getSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)i
) +
197 UnicodeString((UChar32
)(0x10330 + i
)));
201 DecimalFormatSymbols
sym(Locale::getUS(), status
);
203 UnicodeString
customDecSeperator("S");
204 Verify(34.5, (UnicodeString
)"00.00", sym
, (UnicodeString
)"34.50");
205 sym
.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
, customDecSeperator
);
206 Verify(34.5, (UnicodeString
)"00.00", sym
, (UnicodeString
)"34S50");
207 sym
.setSymbol(DecimalFormatSymbols::kPercentSymbol
, (UnicodeString
)"P");
208 Verify(34.5, (UnicodeString
)"00 %", sym
, (UnicodeString
)"3450 P");
209 sym
.setSymbol(DecimalFormatSymbols::kCurrencySymbol
, (UnicodeString
)"D");
210 Verify(34.5, CharsToUnicodeString("\\u00a4##.##"), sym
, (UnicodeString
)"D34.5");
211 sym
.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
, (UnicodeString
)"|");
212 Verify(3456.5, (UnicodeString
)"0,000.##", sym
, (UnicodeString
)"3|456S5");
216 void IntlTestDecimalFormatSymbols::Verify(double value
, const UnicodeString
& pattern
, DecimalFormatSymbols sym
, const UnicodeString
& expected
){
217 UErrorCode status
= U_ZERO_ERROR
;
218 DecimalFormat
*df
= new DecimalFormat(pattern
, sym
, status
);
219 if(U_FAILURE(status
)){
220 errln("ERROR: construction of decimal format failed");
222 UnicodeString buffer
;
223 FieldPosition
pos(FieldPosition::DONT_CARE
);
224 buffer
= df
->format(value
, buffer
, pos
);
225 if(buffer
!= expected
){
226 errln((UnicodeString
)"ERROR: format failed after setSymbols()\n Expected " +
227 expected
+ ", Got " + buffer
);
232 #endif /* #if !UCONFIG_NO_FORMATTING */