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