1 /********************************************************************
3 * Copyright (c) 1997-2010, 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
, UBool possibleDataError
)
38 if(U_FAILURE(status
)) {
39 if (possibleDataError
) {
40 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
42 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
50 void ParsePositionTest::TestParsePosition()
53 if (pp1
.getIndex() == 0) {
54 logln("PP constructor() tested.");
56 errln("*** PP getIndex or constructor() result");
61 ParsePosition
pp2( to
);
62 if (pp2
.getIndex() == 5) {
63 logln("PP getIndex and constructor(int32_t) tested.");
65 errln("*** PP getIndex or constructor(int32_t) result");
68 if (pp2
.getIndex() == 3) {
69 logln("PP setIndex tested.");
71 errln("*** PP getIndex or setIndex result");
75 ParsePosition
pp2(3), pp3(5);
76 //pp2 = new ParsePosition( 3 );
77 //pp3 = new ParsePosition( 5 );
80 logln("PP not equals tested.");
82 errln("*** PP not equals fails");
85 logln("PP equals tested.");
87 errln(UnicodeString("*** PP equals fails (") + pp3
.getIndex() + " != " + pp4
.getIndex() + ")");
93 logln("PP operator= tested.");
95 errln("*** PP operator= operator== or operator != result");
98 ParsePosition
*ppp
= pp5
.clone();
99 if(ppp
== &pp5
|| *ppp
!= pp5
) {
100 errln("ParsePosition.clone() failed");
105 void ParsePositionTest::TestFieldPosition()
107 FieldPosition
fp( 7 );
109 if (fp
.getField() == 7) {
110 logln("FP constructor(int) and getField tested.");
112 errln("*** FP constructor(int) or getField");
115 FieldPosition
fpc(fp
);
116 if (fpc
.getField() == 7) {
117 logln("FP Constructor(FP&) passed");
119 errln("*** FP Constructor(FP&)");
122 FieldPosition
fph( 3 );
123 if ( fph
.getField() != 3)
124 errln("*** FP getField or heap constr.");
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;
138 logln("FP setField and getField tested.");
140 errln("*** FP setField or getField");
143 logln("FP setBeginIndex and getBeginIndex tested.");
145 errln("*** FP setBeginIndex or getBeginIndex");
148 logln("FP setEndIndex and getEndIndex tested.");
150 errln("*** FP setEndIndex or getEndIndex");
155 FieldPosition
*pfp
= fp
.clone();
156 if(pfp
== &fp
|| *pfp
!= fp
) {
157 errln("FieldPosition.clone() failed");
162 void ParsePositionTest::TestFieldPosition_example()
164 //***** no error detection yet !!!!!!!
165 //***** this test is for compiler checks and visual verification only.
166 double doubleNum
[] = {
178 UErrorCode status
= U_ZERO_ERROR
;
179 NumberFormat
*nf
= NumberFormat::createInstance(status
);
180 if (failure(status
, "NumberFormat::createInstance", TRUE
)){
185 DecimalFormat
*fmt
= dynamic_cast<DecimalFormat
*>(nf
);
187 errln("NumberFormat::createInstance returned unexpected class type");
190 fmt
->setDecimalSeparatorAlwaysShown(TRUE
);
192 const int tempLen
= 20;
195 for (int i
=0; i
< dNumSize
; i
++) {
197 //temp = new StringBuffer(); // Get new buffer
199 FieldPosition
pos(NumberFormat::INTEGER_FIELD
);
200 UnicodeString buf
;// = new StringBuffer();
201 //char fmtText[tempLen];
202 //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
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
);
217 * Need to override ParsePosition.equals and FieldPosition.equals.
219 void ParsePositionTest::Test4109023()
224 errln("Error : ParsePosition.equals() failed");
226 FieldPosition
fp2(2);
228 errln("Error : FieldPosition.equals() failed");
231 #endif /* #if !UCONFIG_NO_FORMATTING */