1 /********************************************************************
3 * Copyright (c) 1997-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/numfmt.h"
14 #include "unicode/decimfmt.h"
16 // *****************************************************************************
17 // class ParsePositionTest
18 // *****************************************************************************
20 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
22 void ParsePositionTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
24 // if (exec) logln((UnicodeString)"TestSuite ParsePositionTest");
26 CASE(0, TestParsePosition
)
27 CASE(1, TestFieldPosition
)
28 CASE(2, TestFieldPosition_example
)
31 default: name
= ""; break;
36 ParsePositionTest::failure(UErrorCode status
, const char* msg
)
38 if(U_FAILURE(status
)) {
39 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
46 void ParsePositionTest::TestParsePosition()
49 if (pp1
.getIndex() == 0) {
50 logln("PP constructor() tested.");
52 errln("*** PP getIndex or constructor() result");
57 ParsePosition
pp2( to
);
58 if (pp2
.getIndex() == 5) {
59 logln("PP getIndex and constructor(int32_t) tested.");
61 errln("*** PP getIndex or constructor(int32_t) result");
64 if (pp2
.getIndex() == 3) {
65 logln("PP setIndex tested.");
67 errln("*** PP getIndex or setIndex result");
71 ParsePosition
pp2(3), pp3(5);
72 //pp2 = new ParsePosition( 3 );
73 //pp3 = new ParsePosition( 5 );
76 logln("PP not equals tested.");
78 errln("*** PP not equals fails");
81 logln("PP equals tested.");
83 errln(UnicodeString("*** PP equals fails (") + pp3
.getIndex() + " != " + pp4
.getIndex() + ")");
89 logln("PP operator= tested.");
91 errln("*** PP operator= operator== or operator != result");
94 ParsePosition
*ppp
= pp5
.clone();
95 if(ppp
== &pp5
|| *ppp
!= pp5
) {
96 errln("ParsePosition.clone() failed");
101 void ParsePositionTest::TestFieldPosition()
103 FieldPosition
fp( 7 );
105 if (fp
.getField() == 7) {
106 logln("FP constructor(int) and getField tested.");
108 errln("*** FP constructor(int) or getField");
111 FieldPosition
fpc(fp
);
112 if (fpc
.getField() == 7) {
113 logln("FP Constructor(FP&) passed");
115 errln("*** FP Constructor(FP&)");
118 FieldPosition
fph( 3 );
119 if ( fph
.getField() != 3)
120 errln("*** FP getField or heap constr.");
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;
134 logln("FP setField and getField tested.");
136 errln("*** FP setField or getField");
139 logln("FP setBeginIndex and getBeginIndex tested.");
141 errln("*** FP setBeginIndex or getBeginIndex");
144 logln("FP setEndIndex and getEndIndex tested.");
146 errln("*** FP setEndIndex or getEndIndex");
151 FieldPosition
*pfp
= fp
.clone();
152 if(pfp
== &fp
|| *pfp
!= fp
) {
153 errln("FieldPosition.clone() failed");
158 void ParsePositionTest::TestFieldPosition_example()
160 //***** no error detection yet !!!!!!!
161 //***** this test is for compiler checks and visual verification only.
162 double doubleNum
[] = {
174 UErrorCode status
= U_ZERO_ERROR
;
175 NumberFormat
*nf
= NumberFormat::createInstance(status
);
176 failure(status
, "NumberFormat::createInstance");
178 if(nf
->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
179 errln("NumberFormat::createInstance returned unexpected class type");
182 DecimalFormat
*fmt
= (DecimalFormat
*) nf
;
183 fmt
->setDecimalSeparatorAlwaysShown(TRUE
);
185 const int tempLen
= 20;
188 for (int i
=0; i
< dNumSize
; i
++) {
190 //temp = new StringBuffer(); // Get new buffer
192 FieldPosition
pos(NumberFormat::INTEGER_FIELD
);
193 UnicodeString buf
;// = new StringBuffer();
194 //char fmtText[tempLen];
195 //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
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
);
210 * Need to override ParsePosition.equals and FieldPosition.equals.
212 void ParsePositionTest::Test4109023()
217 errln("Error : ParsePosition.equals() failed");
219 FieldPosition
fp2(2);
221 errln("Error : FieldPosition.equals() failed");
224 #endif /* #if !UCONFIG_NO_FORMATTING */