]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/tsdcfmsy.cpp
ICU-511.35.tar.gz
[apple/icu.git] / icuSources / test / intltest / tsdcfmsy.cpp
CommitLineData
b75a7d8f 1/********************************************************************
729e4ab9
A
2 * COPYRIGHT:
3 * Copyright (c) 1997-2010, International Business Machines Corporation and
b75a7d8f
A
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
16void IntlTestDecimalFormatSymbols::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
17{
18 if (exec) logln("TestSuite DecimalFormatSymbols");
19 switch (index) {
729e4ab9 20 case 0: name = "DecimalFormatSymbols test";
b75a7d8f
A
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 */
34void IntlTestDecimalFormatSymbols::testSymbols(/* char *par */)
35{
36 UErrorCode status = U_ZERO_ERROR;
37
38 DecimalFormatSymbols fr(Locale::getFrench(), status);
39 if(U_FAILURE(status)) {
729e4ab9
A
40 errcheckln(status, "ERROR: Couldn't create French DecimalFormatSymbols - %s", u_errorName(status));
41 return;
b75a7d8f
A
42 }
43
44 status = U_ZERO_ERROR;
45 DecimalFormatSymbols en(Locale::getEnglish(), status);
46 if(U_FAILURE(status)) {
729e4ab9
A
47 errcheckln(status, "ERROR: Couldn't create English DecimalFormatSymbols - %s", u_errorName(status));
48 return;
b75a7d8f
A
49 }
50
51 if(en == fr || ! (en != fr) ) {
52 errln("ERROR: English DecimalFormatSymbols equal to French");
53 }
54
55 // just do some VERY basic tests to make sure that get/set work
56
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");
61 }
62
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");
67 }
68
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");
73 }
74
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");
79 }
80
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");
85 }
86
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");
91 }
92
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");
97 }
98
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");
104 }
105
106 UnicodeString nan(en.getSymbol(DecimalFormatSymbols::kNaNSymbol));
107 fr.setSymbol(DecimalFormatSymbols::kNaNSymbol, nan);
108 UnicodeString nan2(fr.getSymbol(DecimalFormatSymbols::kNaNSymbol));
109 if(nan != nan2) {
110 errln("ERROR: get/set NaN failed");
111 }
112
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");
117 }
729e4ab9 118
b75a7d8f
A
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");
123 }
124
729e4ab9
A
125 // Test get currency spacing before the currency.
126 status = U_ZERO_ERROR;
4388f060 127 for (int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; i++) {
729e4ab9 128 UnicodeString enCurrencyPattern = en.getPatternForCurrencySpacing(
4388f060 129 (UCurrencySpacing)i, TRUE, status);
729e4ab9
A
130 if(U_FAILURE(status)) {
131 errln("Error: cannot get CurrencyMatch for locale:en");
132 status = U_ZERO_ERROR;
133 }
134 UnicodeString frCurrencyPattern = fr.getPatternForCurrencySpacing(
4388f060 135 (UCurrencySpacing)i, TRUE, status);
729e4ab9
A
136 if(U_FAILURE(status)) {
137 errln("Error: cannot get CurrencyMatch for locale:fr");
138 }
139 if (enCurrencyPattern != frCurrencyPattern) {
140 errln("ERROR: get CurrencySpacing failed");
141 }
142 }
143 // Test get currencySpacing after the currency.
144 status = U_ZERO_ERROR;
4388f060 145 for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) {
729e4ab9 146 UnicodeString enCurrencyPattern = en.getPatternForCurrencySpacing(
4388f060 147 (UCurrencySpacing)i, FALSE, status);
729e4ab9
A
148 if(U_FAILURE(status)) {
149 errln("Error: cannot get CurrencyMatch for locale:en");
150 status = U_ZERO_ERROR;
151 }
152 UnicodeString frCurrencyPattern = fr.getPatternForCurrencySpacing(
4388f060 153 (UCurrencySpacing)i, FALSE, status);
729e4ab9
A
154 if(U_FAILURE(status)) {
155 errln("Error: cannot get CurrencyMatch for locale:fr");
156 }
157 if (enCurrencyPattern != frCurrencyPattern) {
158 errln("ERROR: get CurrencySpacing failed");
159 }
160 }
161 // Test set curerncySpacing APIs
162 status = U_ZERO_ERROR;
163 UnicodeString dash = UnicodeString("-");
4388f060 164 en.setPatternForCurrencySpacing(UNUM_CURRENCY_INSERT, TRUE, dash);
729e4ab9 165 UnicodeString enCurrencyInsert = en.getPatternForCurrencySpacing(
4388f060 166 UNUM_CURRENCY_INSERT, TRUE, status);
729e4ab9
A
167 if (dash != enCurrencyInsert) {
168 errln("Error: Failed to setCurrencyInsert for locale:en");
169 }
170
b75a7d8f
A
171 status = U_ZERO_ERROR;
172 DecimalFormatSymbols foo(status);
729e4ab9 173
b75a7d8f
A
174 DecimalFormatSymbols bar(foo);
175
176 en = fr;
177
178 if(en != fr || foo != bar) {
179 errln("ERROR: Copy Constructor or Assignment failed");
180 }
181
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!");
185 return;
186 }
187
188 int i;
189 for(i = 0; i < (int)DecimalFormatSymbols::kFormatSymbolCount; ++i) {
190 foo.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol)i, UnicodeString((UChar32)(0x10330 + i)));
191 }
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) +
196 ", expected " +
197 UnicodeString((UChar32)(0x10330 + i)));
198 }
199 }
729e4ab9 200
b75a7d8f
A
201 DecimalFormatSymbols sym(Locale::getUS(), status);
202
203 UnicodeString customDecSeperator("S");
204 Verify(34.5, (UnicodeString)"00.00", sym, (UnicodeString)"34.50");
729e4ab9 205 sym.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, customDecSeperator);
b75a7d8f 206 Verify(34.5, (UnicodeString)"00.00", sym, (UnicodeString)"34S50");
729e4ab9 207 sym.setSymbol(DecimalFormatSymbols::kPercentSymbol, (UnicodeString)"P");
b75a7d8f 208 Verify(34.5, (UnicodeString)"00 %", sym, (UnicodeString)"3450 P");
729e4ab9 209 sym.setSymbol(DecimalFormatSymbols::kCurrencySymbol, (UnicodeString)"D");
b75a7d8f 210 Verify(34.5, CharsToUnicodeString("\\u00a4##.##"), sym, (UnicodeString)"D34.5");
729e4ab9 211 sym.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, (UnicodeString)"|");
b75a7d8f 212 Verify(3456.5, (UnicodeString)"0,000.##", sym, (UnicodeString)"3|456S5");
729e4ab9 213
b75a7d8f
A
214}
215
216void 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");
221 }
222 UnicodeString buffer;
223 FieldPosition pos(FieldPosition::DONT_CARE);
224 buffer = df->format(value, buffer, pos);
225 if(buffer != expected){
729e4ab9 226 errln((UnicodeString)"ERROR: format failed after setSymbols()\n Expected " +
b75a7d8f
A
227 expected + ", Got " + buffer);
228 }
229 delete df;
230}
231
232#endif /* #if !UCONFIG_NO_FORMATTING */