• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1997-2005, 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)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 
TestParsePosition()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 
94     ParsePosition *ppp = pp5.clone();
95     if(ppp == &pp5 || *ppp != pp5) {
96         errln("ParsePosition.clone() failed");
97     }
98     delete ppp;
99 }
100 
TestFieldPosition()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("");
150 
151     FieldPosition *pfp = fp.clone();
152     if(pfp == &fp || *pfp != fp) {
153         errln("FieldPosition.clone() failed");
154     }
155     delete pfp;
156 }
157 
TestFieldPosition_example()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     if (failure(status, "NumberFormat::createInstance")){
177         delete nf;
178         return;
179     };
180 
181     if(nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
182         errln("NumberFormat::createInstance returned unexpected class type");
183         return;
184     }
185     DecimalFormat *fmt = (DecimalFormat*) nf;
186     fmt->setDecimalSeparatorAlwaysShown(TRUE);
187 
188     const int tempLen = 20;
189     UnicodeString temp;
190 
191     for (int i=0; i < dNumSize; i++) {
192         temp.remove();
193         //temp = new StringBuffer(); // Get new buffer
194 
195         FieldPosition pos(NumberFormat::INTEGER_FIELD);
196         UnicodeString buf;// = new StringBuffer();
197         //char fmtText[tempLen];
198         //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
199         UnicodeString res;
200         res = fmt->format(doubleNum[i], buf, pos);
201         int tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ?
202             tempLen : (tempLen - pos.getEndIndex());
203         for (int j=0; j<tempOffset; j++)
204             temp += UnicodeString("="/*'='*/); // initialize
205         logln("FP " + temp + res);
206     }
207 
208     logln("");
209     delete nf;
210 }
211 
212 /* @bug 4109023
213  * Need to override ParsePosition.equals and FieldPosition.equals.
214  */
Test4109023()215 void ParsePositionTest::Test4109023()
216 {
217     ParsePosition p(3);
218     ParsePosition p2(3);
219     if (p != p2)
220         errln("Error : ParsePosition.equals() failed");
221     FieldPosition fp(2);
222     FieldPosition fp2(2);
223     if (fp != fp2)
224         errln("Error : FieldPosition.equals() failed");
225 }
226 
227 #endif /* #if !UCONFIG_NO_FORMATTING */
228