]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
374ca955 | 3 | * Copyright (c) 1997-2003, 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 "pptest.h" | |
12 | ||
13 | #include "unicode/numfmt.h" | |
14 | #include "unicode/decimfmt.h" | |
15 | ||
16 | // ***************************************************************************** | |
17 | // class ParsePositionTest | |
18 | // ***************************************************************************** | |
19 | ||
20 | #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; | |
21 | ||
22 | void ParsePositionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
23 | { | |
24 | // if (exec) logln((UnicodeString)"TestSuite ParsePositionTest"); | |
25 | switch (index) { | |
26 | CASE(0, TestParsePosition) | |
27 | CASE(1, TestFieldPosition) | |
28 | CASE(2, TestFieldPosition_example) | |
29 | CASE(3, Test4109023) | |
30 | ||
31 | default: name = ""; break; | |
32 | } | |
33 | } | |
34 | ||
35 | UBool | |
36 | ParsePositionTest::failure(UErrorCode status, const char* msg) | |
37 | { | |
38 | if(U_FAILURE(status)) { | |
39 | errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status)); | |
40 | return TRUE; | |
41 | } | |
42 | ||
43 | return FALSE; | |
44 | } | |
45 | ||
46 | void ParsePositionTest::TestParsePosition() | |
47 | { | |
48 | ParsePosition pp1(0); | |
49 | if (pp1.getIndex() == 0) { | |
50 | logln("PP constructor() tested."); | |
51 | }else{ | |
52 | errln("*** PP getIndex or constructor() result"); | |
53 | } | |
54 | ||
55 | { | |
56 | int to = 5; | |
57 | ParsePosition pp2( to ); | |
58 | if (pp2.getIndex() == 5) { | |
59 | logln("PP getIndex and constructor(int32_t) tested."); | |
60 | }else{ | |
61 | errln("*** PP getIndex or constructor(int32_t) result"); | |
62 | } | |
63 | pp2.setIndex( 3 ); | |
64 | if (pp2.getIndex() == 3) { | |
65 | logln("PP setIndex tested."); | |
66 | }else{ | |
67 | errln("*** PP getIndex or setIndex result"); | |
68 | } | |
69 | } | |
70 | ||
71 | ParsePosition pp2(3), pp3(5); | |
72 | //pp2 = new ParsePosition( 3 ); | |
73 | //pp3 = new ParsePosition( 5 ); | |
74 | ParsePosition pp4(5); | |
75 | if ( pp2 != pp3) { | |
76 | logln("PP not equals tested."); | |
77 | }else{ | |
78 | errln("*** PP not equals fails"); | |
79 | } | |
80 | if (pp3 == pp4) { | |
81 | logln("PP equals tested."); | |
82 | }else{ | |
83 | errln(UnicodeString("*** PP equals fails (") + pp3.getIndex() + " != " + pp4.getIndex() + ")"); | |
84 | } | |
85 | ||
86 | ParsePosition pp5; | |
87 | pp5 = pp4; | |
88 | if (pp4 == pp5) { | |
89 | logln("PP operator= tested."); | |
90 | }else{ | |
91 | errln("*** PP operator= operator== or operator != result"); | |
92 | } | |
93 | ||
374ca955 A |
94 | ParsePosition *ppp = pp5.clone(); |
95 | if(ppp == &pp5 || *ppp != pp5) { | |
96 | errln("ParsePosition.clone() failed"); | |
97 | } | |
98 | delete ppp; | |
b75a7d8f A |
99 | } |
100 | ||
101 | void ParsePositionTest::TestFieldPosition() | |
102 | { | |
103 | FieldPosition fp( 7 ); | |
104 | ||
105 | if (fp.getField() == 7) { | |
106 | logln("FP constructor(int) and getField tested."); | |
107 | }else{ | |
108 | errln("*** FP constructor(int) or getField"); | |
109 | } | |
110 | ||
111 | FieldPosition fpc(fp); | |
112 | if (fpc.getField() == 7) { | |
113 | logln("FP Constructor(FP&) passed"); | |
114 | } else { | |
115 | errln("*** FP Constructor(FP&)"); | |
116 | } | |
117 | ||
118 | FieldPosition fph( 3 ); | |
119 | if ( fph.getField() != 3) | |
120 | errln("*** FP getField or heap constr."); | |
121 | ||
122 | UBool err1 = FALSE; | |
123 | UBool err2 = FALSE; | |
124 | UBool err3 = FALSE; | |
125 | // for (long i = -50; i < 50; i++ ) { | |
126 | // fp.setField( i+8 ); | |
127 | // fp.setBeginIndex( i+6 ); | |
128 | // fp.setEndIndex( i+7 ); | |
129 | // if (fp.getField() != i+8) err1 = TRUE; | |
130 | // if (fp.getBeginIndex() != i+6) err2 = TRUE; | |
131 | // if (fp.getEndIndex() != i+7) err3 = TRUE; | |
132 | // } | |
133 | if (!err1) { | |
134 | logln("FP setField and getField tested."); | |
135 | }else{ | |
136 | errln("*** FP setField or getField"); | |
137 | } | |
138 | if (!err2) { | |
139 | logln("FP setBeginIndex and getBeginIndex tested."); | |
140 | }else{ | |
141 | errln("*** FP setBeginIndex or getBeginIndex"); | |
142 | } | |
143 | if (!err3) { | |
144 | logln("FP setEndIndex and getEndIndex tested."); | |
145 | }else{ | |
146 | errln("*** FP setEndIndex or getEndIndex"); | |
147 | } | |
148 | ||
149 | logln(""); | |
374ca955 A |
150 | |
151 | FieldPosition *pfp = fp.clone(); | |
152 | if(pfp == &fp || *pfp != fp) { | |
153 | errln("FieldPosition.clone() failed"); | |
154 | } | |
155 | delete pfp; | |
b75a7d8f A |
156 | } |
157 | ||
158 | void ParsePositionTest::TestFieldPosition_example() | |
159 | { | |
160 | //***** no error detection yet !!!!!!! | |
161 | //***** this test is for compiler checks and visual verification only. | |
162 | double doubleNum[] = { | |
163 | 123456789.0, | |
164 | -12345678.9, | |
165 | 1234567.89, | |
166 | -123456.789, | |
167 | 12345.6789, | |
168 | -1234.56789, | |
169 | 123.456789, | |
170 | -12.3456789, | |
171 | 1.23456789}; | |
172 | int dNumSize = 9; | |
173 | ||
174 | UErrorCode status = U_ZERO_ERROR; | |
175 | NumberFormat *nf = NumberFormat::createInstance(status); | |
176 | failure(status, "NumberFormat::createInstance"); | |
177 | ||
178 | if(nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) { | |
179 | errln("NumberFormat::createInstance returned unexpected class type"); | |
180 | return; | |
181 | } | |
182 | DecimalFormat *fmt = (DecimalFormat*) nf; | |
183 | fmt->setDecimalSeparatorAlwaysShown(TRUE); | |
184 | ||
185 | const int tempLen = 20; | |
186 | UnicodeString temp; | |
187 | ||
188 | for (int i=0; i < dNumSize; i++) { | |
189 | temp.remove(); | |
190 | //temp = new StringBuffer(); // Get new buffer | |
191 | ||
192 | FieldPosition pos(NumberFormat::INTEGER_FIELD); | |
193 | UnicodeString buf;// = new StringBuffer(); | |
194 | //char fmtText[tempLen]; | |
195 | //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText); | |
196 | UnicodeString res; | |
197 | res = fmt->format(doubleNum[i], buf, pos); | |
198 | int tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ? | |
199 | tempLen : (tempLen - pos.getEndIndex()); | |
200 | for (int j=0; j<tempOffset; j++) | |
201 | temp += UnicodeString("="/*'='*/); // initialize | |
202 | logln("FP " + temp + res); | |
203 | } | |
204 | ||
205 | logln(""); | |
206 | delete nf; | |
207 | } | |
208 | ||
209 | /* @bug 4109023 | |
210 | * Need to override ParsePosition.equals and FieldPosition.equals. | |
211 | */ | |
212 | void ParsePositionTest::Test4109023() | |
213 | { | |
214 | ParsePosition p(3); | |
215 | ParsePosition p2(3); | |
216 | if (p != p2) | |
217 | errln("Error : ParsePosition.equals() failed"); | |
218 | FieldPosition fp(2); | |
219 | FieldPosition fp2(2); | |
220 | if (fp != fp2) | |
221 | errln("Error : FieldPosition.equals() failed"); | |
222 | } | |
223 | ||
224 | #endif /* #if !UCONFIG_NO_FORMATTING */ |