1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2010, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_FORMATTING
15 #include "unicode/numfmt.h"
16 #include "unicode/decimfmt.h"
18 // *****************************************************************************
19 // class ParsePositionTest
20 // *****************************************************************************
22 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
24 void ParsePositionTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
26 // if (exec) logln((UnicodeString)"TestSuite ParsePositionTest");
28 CASE(0, TestParsePosition
)
29 CASE(1, TestFieldPosition
)
30 CASE(2, TestFieldPosition_example
)
33 default: name
= ""; break;
38 ParsePositionTest::failure(UErrorCode status
, const char* msg
, UBool possibleDataError
)
40 if(U_FAILURE(status
)) {
41 if (possibleDataError
) {
42 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
44 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
52 void ParsePositionTest::TestParsePosition()
55 if (pp1
.getIndex() == 0) {
56 logln("PP constructor() tested.");
58 errln("*** PP getIndex or constructor() result");
63 ParsePosition
pp2( to
);
64 if (pp2
.getIndex() == 5) {
65 logln("PP getIndex and constructor(int32_t) tested.");
67 errln("*** PP getIndex or constructor(int32_t) result");
70 if (pp2
.getIndex() == 3) {
71 logln("PP setIndex tested.");
73 errln("*** PP getIndex or setIndex result");
77 ParsePosition
pp2(3), pp3(5);
78 //pp2 = new ParsePosition( 3 );
79 //pp3 = new ParsePosition( 5 );
82 logln("PP not equals tested.");
84 errln("*** PP not equals fails");
87 logln("PP equals tested.");
89 errln(UnicodeString("*** PP equals fails (") + pp3
.getIndex() + " != " + pp4
.getIndex() + ")");
95 logln("PP operator= tested.");
97 errln("*** PP operator= operator== or operator != result");
100 ParsePosition
*ppp
= pp5
.clone();
101 if(ppp
== &pp5
|| *ppp
!= pp5
) {
102 errln("ParsePosition.clone() failed");
107 void ParsePositionTest::TestFieldPosition()
109 FieldPosition
fp( 7 );
111 if (fp
.getField() == 7) {
112 logln("FP constructor(int) and getField tested.");
114 errln("*** FP constructor(int) or getField");
117 FieldPosition
fpc(fp
);
118 if (fpc
.getField() == 7) {
119 logln("FP Constructor(FP&) passed");
121 errln("*** FP Constructor(FP&)");
124 FieldPosition
fph( 3 );
125 if ( fph
.getField() != 3)
126 errln("*** FP getField or heap constr.");
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;
140 logln("FP setField and getField tested.");
142 errln("*** FP setField or getField");
145 logln("FP setBeginIndex and getBeginIndex tested.");
147 errln("*** FP setBeginIndex or getBeginIndex");
150 logln("FP setEndIndex and getEndIndex tested.");
152 errln("*** FP setEndIndex or getEndIndex");
157 FieldPosition
*pfp
= fp
.clone();
158 if(pfp
== &fp
|| *pfp
!= fp
) {
159 errln("FieldPosition.clone() failed");
164 void ParsePositionTest::TestFieldPosition_example()
166 //***** no error detection yet !!!!!!!
167 //***** this test is for compiler checks and visual verification only.
168 double doubleNum
[] = {
180 UErrorCode status
= U_ZERO_ERROR
;
181 NumberFormat
*nf
= NumberFormat::createInstance(status
);
182 if (failure(status
, "NumberFormat::createInstance", TRUE
)){
187 DecimalFormat
*fmt
= dynamic_cast<DecimalFormat
*>(nf
);
189 errln("NumberFormat::createInstance returned unexpected class type");
192 fmt
->setDecimalSeparatorAlwaysShown(TRUE
);
194 const int tempLen
= 20;
197 for (int i
=0; i
< dNumSize
; i
++) {
199 //temp = new StringBuffer(); // Get new buffer
201 FieldPosition
pos(NumberFormat::INTEGER_FIELD
);
202 UnicodeString buf
;// = new StringBuffer();
203 //char fmtText[tempLen];
204 //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
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
);
219 * Need to override ParsePosition.equals and FieldPosition.equals.
221 void ParsePositionTest::Test4109023()
226 errln("Error : ParsePosition.equals() failed");
228 FieldPosition
fp2(2);
230 errln("Error : FieldPosition.equals() failed");
233 #endif /* #if !UCONFIG_NO_FORMATTING */