]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsdcfmsy.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / test / intltest / tsdcfmsy.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2001, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "unicode/dcfmtsym.h"
12 #include "unicode/decimfmt.h"
13 #include "unicode/unum.h"
14 #include "tsdcfmsy.h"
15
16 void IntlTestDecimalFormatSymbols::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
17 {
18 if (exec) logln("TestSuite DecimalFormatSymbols");
19 switch (index) {
20 case 0: name = "DecimalFormatSymbols test";
21 if (exec) {
22 logln("DecimalFormatSymbols test---"); logln("");
23 testSymbols(/*par*/);
24 }
25 break;
26
27 default: name = ""; break;
28 }
29 }
30
31 /**
32 * Test the API of DecimalFormatSymbols; primarily a simple get/set set.
33 */
34 void IntlTestDecimalFormatSymbols::testSymbols(/* char *par */)
35 {
36 UErrorCode status = U_ZERO_ERROR;
37
38 DecimalFormatSymbols fr(Locale::getFrench(), status);
39 if(U_FAILURE(status)) {
40 errln("ERROR: Couldn't create French DecimalFormatSymbols");
41 }
42
43 status = U_ZERO_ERROR;
44 DecimalFormatSymbols en(Locale::getEnglish(), status);
45 if(U_FAILURE(status)) {
46 errln("ERROR: Couldn't create English DecimalFormatSymbols");
47 }
48
49 if(en == fr || ! (en != fr) ) {
50 errln("ERROR: English DecimalFormatSymbols equal to French");
51 }
52
53 // just do some VERY basic tests to make sure that get/set work
54
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");
59 }
60
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");
65 }
66
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");
71 }
72
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");
77 }
78
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");
83 }
84
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");
89 }
90
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");
95 }
96
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");
102 }
103
104 UnicodeString nan(en.getSymbol(DecimalFormatSymbols::kNaNSymbol));
105 fr.setSymbol(DecimalFormatSymbols::kNaNSymbol, nan);
106 UnicodeString nan2(fr.getSymbol(DecimalFormatSymbols::kNaNSymbol));
107 if(nan != nan2) {
108 errln("ERROR: get/set NaN failed");
109 }
110
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");
115 }
116
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");
121 }
122
123 status = U_ZERO_ERROR;
124 DecimalFormatSymbols foo(status);
125
126 DecimalFormatSymbols bar(foo);
127
128 en = fr;
129
130 if(en != fr || foo != bar) {
131 errln("ERROR: Copy Constructor or Assignment failed");
132 }
133
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!");
137 return;
138 }
139
140 int i;
141 for(i = 0; i < (int)DecimalFormatSymbols::kFormatSymbolCount; ++i) {
142 foo.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol)i, UnicodeString((UChar32)(0x10330 + i)));
143 }
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) +
148 ", expected " +
149 UnicodeString((UChar32)(0x10330 + i)));
150 }
151 }
152
153
154 DecimalFormatSymbols sym(Locale::getUS(), status);
155
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");
166
167 }
168
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");
174 }
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);
181 }
182 delete df;
183 }
184
185 #endif /* #if !UCONFIG_NO_FORMATTING */