1 /********************************************************************
3 * Copyright (c) 1997-2001, 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 errln("ERROR: Couldn't create French DecimalFormatSymbols");
43 status
= U_ZERO_ERROR
;
44 DecimalFormatSymbols
en(Locale::getEnglish(), status
);
45 if(U_FAILURE(status
)) {
46 errln("ERROR: Couldn't create English DecimalFormatSymbols");
49 if(en
== fr
|| ! (en
!= fr
) ) {
50 errln("ERROR: English DecimalFormatSymbols equal to French");
53 // just do some VERY basic tests to make sure that get/set work
55 UnicodeString zero
= en
.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol
);
56 fr
.setSymbol(DecimalFormatSymbols::kZeroDigitSymbol
, zero
);
57 if(fr
.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol
) != en
.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol
)) {
58 errln("ERROR: get/set ZeroDigit failed");
61 UnicodeString group
= en
.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
);
62 fr
.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
, group
);
63 if(fr
.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
) != en
.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
)) {
64 errln("ERROR: get/set GroupingSeparator failed");
67 UnicodeString decimal
= en
.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
);
68 fr
.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
, decimal
);
69 if(fr
.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
) != en
.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
)) {
70 errln("ERROR: get/set DecimalSeparator failed");
73 UnicodeString perMill
= en
.getSymbol(DecimalFormatSymbols::kPerMillSymbol
);
74 fr
.setSymbol(DecimalFormatSymbols::kPerMillSymbol
, perMill
);
75 if(fr
.getSymbol(DecimalFormatSymbols::kPerMillSymbol
) != en
.getSymbol(DecimalFormatSymbols::kPerMillSymbol
)) {
76 errln("ERROR: get/set PerMill failed");
79 UnicodeString percent
= en
.getSymbol(DecimalFormatSymbols::kPercentSymbol
);
80 fr
.setSymbol(DecimalFormatSymbols::kPercentSymbol
, percent
);
81 if(fr
.getSymbol(DecimalFormatSymbols::kPercentSymbol
) != en
.getSymbol(DecimalFormatSymbols::kPercentSymbol
)) {
82 errln("ERROR: get/set Percent failed");
85 UnicodeString
digit(en
.getSymbol(DecimalFormatSymbols::kDigitSymbol
));
86 fr
.setSymbol(DecimalFormatSymbols::kDigitSymbol
, digit
);
87 if(fr
.getSymbol(DecimalFormatSymbols::kDigitSymbol
) != en
.getSymbol(DecimalFormatSymbols::kDigitSymbol
)) {
88 errln("ERROR: get/set Percent failed");
91 UnicodeString patternSeparator
= en
.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
);
92 fr
.setSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
, patternSeparator
);
93 if(fr
.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
) != en
.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol
)) {
94 errln("ERROR: get/set PatternSeparator failed");
97 UnicodeString
infinity(en
.getSymbol(DecimalFormatSymbols::kInfinitySymbol
));
98 fr
.setSymbol(DecimalFormatSymbols::kInfinitySymbol
, infinity
);
99 UnicodeString
infinity2(fr
.getSymbol(DecimalFormatSymbols::kInfinitySymbol
));
100 if(infinity
!= infinity2
) {
101 errln("ERROR: get/set Infinity failed");
104 UnicodeString
nan(en
.getSymbol(DecimalFormatSymbols::kNaNSymbol
));
105 fr
.setSymbol(DecimalFormatSymbols::kNaNSymbol
, nan
);
106 UnicodeString
nan2(fr
.getSymbol(DecimalFormatSymbols::kNaNSymbol
));
108 errln("ERROR: get/set NaN failed");
111 UnicodeString minusSign
= en
.getSymbol(DecimalFormatSymbols::kMinusSignSymbol
);
112 fr
.setSymbol(DecimalFormatSymbols::kMinusSignSymbol
, minusSign
);
113 if(fr
.getSymbol(DecimalFormatSymbols::kMinusSignSymbol
) != en
.getSymbol(DecimalFormatSymbols::kMinusSignSymbol
)) {
114 errln("ERROR: get/set MinusSign failed");
117 UnicodeString
exponential(en
.getSymbol(DecimalFormatSymbols::kExponentialSymbol
));
118 fr
.setSymbol(DecimalFormatSymbols::kExponentialSymbol
, exponential
);
119 if(fr
.getSymbol(DecimalFormatSymbols::kExponentialSymbol
) != en
.getSymbol(DecimalFormatSymbols::kExponentialSymbol
)) {
120 errln("ERROR: get/set Exponential failed");
123 status
= U_ZERO_ERROR
;
124 DecimalFormatSymbols
foo(status
);
126 DecimalFormatSymbols
bar(foo
);
130 if(en
!= fr
|| foo
!= bar
) {
131 errln("ERROR: Copy Constructor or Assignment failed");
134 // test get/setSymbol()
135 if((int) UNUM_FORMAT_SYMBOL_COUNT
!= (int) DecimalFormatSymbols::kFormatSymbolCount
) {
136 errln("unum.h and decimfmt.h have inconsistent numbers of format symbols!");
141 for(i
= 0; i
< (int)DecimalFormatSymbols::kFormatSymbolCount
; ++i
) {
142 foo
.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)i
, UnicodeString((UChar32
)(0x10330 + i
)));
144 for(i
= 0; i
< (int)DecimalFormatSymbols::kFormatSymbolCount
; ++i
) {
145 if(foo
.getSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)i
) != UnicodeString((UChar32
)(0x10330 + i
))) {
146 errln("get/setSymbol did not roundtrip, got " +
147 foo
.getSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)i
) +
149 UnicodeString((UChar32
)(0x10330 + i
)));
154 DecimalFormatSymbols
sym(Locale::getUS(), status
);
156 UnicodeString
customDecSeperator("S");
157 Verify(34.5, (UnicodeString
)"00.00", sym
, (UnicodeString
)"34.50");
158 sym
.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)0, customDecSeperator
);
159 Verify(34.5, (UnicodeString
)"00.00", sym
, (UnicodeString
)"34S50");
160 sym
.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)3, (UnicodeString
)"P");
161 Verify(34.5, (UnicodeString
)"00 %", sym
, (UnicodeString
)"3450 P");
162 sym
.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)8, (UnicodeString
)"D");
163 Verify(34.5, CharsToUnicodeString("\\u00a4##.##"), sym
, (UnicodeString
)"D34.5");
164 sym
.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol
)1, (UnicodeString
)"|");
165 Verify(3456.5, (UnicodeString
)"0,000.##", sym
, (UnicodeString
)"3|456S5");
169 void IntlTestDecimalFormatSymbols::Verify(double value
, const UnicodeString
& pattern
, DecimalFormatSymbols sym
, const UnicodeString
& expected
){
170 UErrorCode status
= U_ZERO_ERROR
;
171 DecimalFormat
*df
= new DecimalFormat(pattern
, sym
, status
);
172 if(U_FAILURE(status
)){
173 errln("ERROR: construction of decimal format failed");
175 UnicodeString buffer
;
176 FieldPosition
pos(FieldPosition::DONT_CARE
);
177 buffer
= df
->format(value
, buffer
, pos
);
178 if(buffer
!= expected
){
179 errln((UnicodeString
)"ERROR: format failed after setSymbols()\n Expected " +
180 expected
+ ", Got " + buffer
);
185 #endif /* #if !UCONFIG_NO_FORMATTING */