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