]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tfsmalls.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2016, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
15 #include "unicode/msgfmt.h"
16 #include "unicode/choicfmt.h"
18 #include "unicode/parsepos.h"
19 #include "unicode/fieldpos.h"
20 #include "unicode/fmtable.h"
22 /*static UBool chkstatus( UErrorCode &status, char* msg = NULL )
24 UBool ok = (status == U_ZERO_ERROR);
25 if (!ok) it_errln( msg );
29 void test_ParsePosition( void )
31 ParsePosition
* pp1
= new ParsePosition();
32 if (pp1
&& (pp1
->getIndex() == 0)) {
33 it_logln("PP constructor() tested.");
35 it_errln("*** PP getIndex or constructor() result");
42 ParsePosition
pp2( to
);
43 if (pp2
.getIndex() == 5) {
44 it_logln("PP getIndex and constructor(int32_t) tested.");
46 it_errln("*** PP getIndex or constructor(int32_t) result");
49 if (pp2
.getIndex() == 3) {
50 it_logln("PP setIndex tested.");
52 it_errln("*** PP getIndex or setIndex result");
56 ParsePosition pp2
, pp3
;
59 ParsePosition
pp4( pp3
);
60 if ((pp2
!= pp3
) && (pp3
== pp4
)) {
61 it_logln("PP copy contructor, operator== and operator != tested.");
63 it_errln("*** PP operator== or operator != result");
68 if ((pp4
== pp5
) && (!(pp4
!= pp5
))) {
69 it_logln("PP operator= tested.");
71 it_errln("*** PP operator= operator== or operator != result");
77 #include "unicode/decimfmt.h"
79 void test_FieldPosition_example( void )
81 //***** no error detection yet !!!!!!!
82 //***** this test is for compiler checks and visual verification only.
83 double doubleNum
[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789,
84 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
85 int32_t dNumSize
= UPRV_LENGTHOF(doubleNum
);
87 UErrorCode status
= U_ZERO_ERROR
;
88 DecimalFormat
* fmt
= (DecimalFormat
*) NumberFormat::createInstance(status
);
89 if (U_FAILURE(status
)) {
90 it_dataerrln("NumberFormat::createInstance() error");
93 fmt
->setDecimalSeparatorAlwaysShown(TRUE
);
95 const int32_t tempLen
= 20;
98 for (int32_t i
=0; i
<dNumSize
; i
++) {
99 FieldPosition
pos(NumberFormat::INTEGER_FIELD
);
101 //char fmtText[tempLen];
102 //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
103 UnicodeString res
= fmt
->format(doubleNum
[i
], buf
, pos
);
104 for (int32_t j
=0; j
<tempLen
; j
++) temp
[j
] = '='; // clear with spaces
105 int32_t tempOffset
= (tempLen
<= (tempLen
- pos
.getEndIndex())) ?
106 tempLen
: (tempLen
- pos
.getEndIndex());
107 temp
[tempOffset
] = '\0';
108 it_logln(UnicodeString("FP ") + UnicodeString(temp
) + res
);
116 void test_FieldPosition( void )
119 FieldPosition
fp( 7 );
121 if (fp
.getField() == 7) {
122 it_logln("FP constructor(int32_t) and getField tested.");
124 it_errln("*** FP constructor(int32_t) or getField");
127 FieldPosition
* fph
= new FieldPosition( 3 );
128 if ( fph
->getField() != 3) it_errln("*** FP getField or heap constr.");
134 for (int32_t i
= -50; i
< 50; i
++ ) {
136 fp
.setBeginIndex( i
+6 );
137 fp
.setEndIndex( i
+7 );
138 if (fp
.getField() != i
+8) err1
= TRUE
;
139 if (fp
.getBeginIndex() != i
+6) err2
= TRUE
;
140 if (fp
.getEndIndex() != i
+7) err3
= TRUE
;
143 it_logln("FP setField and getField tested.");
145 it_errln("*** FP setField or getField");
148 it_logln("FP setBeginIndex and getBeginIndex tested.");
150 it_errln("*** FP setBeginIndex or getBeginIndex");
153 it_logln("FP setEndIndex and getEndIndex tested.");
155 it_errln("*** FP setEndIndex or getEndIndex");
162 void test_Formattable( void )
164 UErrorCode status
= U_ZERO_ERROR
;
165 Formattable
* ftp
= new Formattable();
166 if (!ftp
|| !(ftp
->getType() == Formattable::kLong
) || !(ftp
->getLong() == 0)) {
167 it_errln("*** Formattable constructor or getType or getLong");
171 Formattable fta
, ftb
;
172 fta
.setLong(1); ftb
.setLong(2);
173 if ((fta
!= ftb
) || !(fta
== ftb
)) {
174 it_logln("FT setLong, operator== and operator!= tested.");
175 status
= U_ZERO_ERROR
;
176 fta
.getLong(&status
);
177 if ( status
== U_INVALID_FORMAT_ERROR
){
178 it_errln("*** FT getLong(UErrorCode* status) failed on real Long");
180 it_logln("FT getLong(UErrorCode* status) tested.");
183 it_errln("*** Formattable setLong or operator== or !=");
186 if ((fta
== ftb
) || !(fta
!= ftb
)) {
187 it_logln("FT operator= tested.");
189 it_errln("*** FT operator= or operator== or operator!=");
192 fta
.setDouble( 3.0 );
193 if ((fta
.getType() == Formattable::kDouble
) && (fta
.getDouble() == 3.0)) {
194 it_logln("FT set- and getDouble tested.");
196 it_errln("*** FT set- or getDouble");
199 fta
.getDate(status
= U_ZERO_ERROR
);
200 if (status
!= U_INVALID_FORMAT_ERROR
){
201 it_errln("*** FT getDate with status should fail on non-Date");
204 if ((fta
.getType() == Formattable::kDate
) && (fta
.getDate() == 4.0)) {
205 it_logln("FT set- and getDate tested.");
206 status
= U_ZERO_ERROR
;
208 if ( status
== U_INVALID_FORMAT_ERROR
){
209 it_errln("*** FT getDate with status failed on real Date");
211 it_logln("FT getDate with status tested.");
214 it_errln("*** FT set- or getDate");
217 status
= U_ZERO_ERROR
;
218 fta
.getLong(&status
);
219 if (status
!= U_INVALID_FORMAT_ERROR
){
220 it_errln("*** FT getLong(UErrorCode* status) should fail on non-Long");
223 fta
.setString("abc");
224 const Formattable
ftc(fta
);
229 t
= (fta
.getType() == Formattable::kString
)
230 && (fta
.getString(res
) == "abc")
231 && (fta
.getString() == "abc");
232 res
= fta
.getString(status
= U_ZERO_ERROR
);
233 t
= t
&& (status
!= U_INVALID_FORMAT_ERROR
&& res
== "abc");
234 res
= ftc
.getString(status
= U_ZERO_ERROR
);
235 t
= t
&& (status
!= U_INVALID_FORMAT_ERROR
&& res
== "abc");
236 ftc
.getString(res
,status
= U_ZERO_ERROR
);
237 t
= t
&& (status
!= U_INVALID_FORMAT_ERROR
&& res
== "abc");
239 it_logln("FT set- and getString tested.");
241 it_errln("*** FT set- or getString");
245 UnicodeString ucs
= "unicode-string";
246 UnicodeString
* ucs_ptr
= new UnicodeString("pointed-to-unicode-string");
248 const Formattable ftarray
[] =
250 Formattable( 1.0, Formattable::kIsDate
),
256 const int32_t ft_cnt
= UPRV_LENGTHOF(ftarray
);
257 Formattable
ft_arr( ftarray
, ft_cnt
);
259 if ((ft_arr
[0].getType() == Formattable::kDate
) && (ft_arr
[0].getDate() == 1.0)
260 && (ft_arr
[1].getType() == Formattable::kDouble
) && (ft_arr
[1].getDouble() == 2.0)
261 && (ft_arr
[2].getType() == Formattable::kLong
) && (ft_arr
[2].getLong() == (int32_t)3)
262 && (ft_arr
[3].getType() == Formattable::kString
) && (ft_arr
[3].getString(temp
) == ucs
)
263 && (ft_arr
[4].getType() == Formattable::kString
) && (ft_arr
[4].getString(temp
) == *ucs_ptr
) ) {
264 it_logln("FT constr. for date, double, long, ustring, ustring* and array tested");
266 it_errln("*** FT constr. for date, double, long, ustring, ustring* or array");
270 const Formattable
* res_array
= ft_arr
.getArray( res_cnt
);
271 if (res_cnt
== ft_cnt
) {
273 for (i
= 0; i
< res_cnt
; i
++ ) {
274 if (res_array
[i
] != ftarray
[i
]) {
279 it_logln("FT getArray tested");
280 res_array
= ft_arr
.getArray( res_cnt
, status
= U_ZERO_ERROR
);
281 if (status
== U_INVALID_FORMAT_ERROR
){
282 it_errln("*** FT getArray with status failed on real array");
284 it_logln("FT getArray with status tested on real array");
287 it_errln("*** FT getArray comparison");
290 it_errln(UnicodeString("*** FT getArray count res_cnt=") + res_cnt
+ UnicodeString("ft_cnt=") + ft_cnt
);
293 res_array
= fta
.getArray(res_cnt
, status
= U_ZERO_ERROR
);
294 if (status
== U_INVALID_FORMAT_ERROR
){
295 if (res_cnt
== 0 && res_array
== NULL
){
296 it_logln("FT getArray with status tested on non array");
298 it_errln("*** FT getArray with status return values are not consistent");
301 it_errln("*** FT getArray with status should fail on non-array");
306 for(i
= 0; i
< ft_cnt
; ++i
) {
307 pf
= ftarray
[i
].clone();
308 if(pf
== (ftarray
+ i
) || *pf
!= ftarray
[i
]) {
309 it_errln(UnicodeString("Formattable.clone() failed for item ") + i
);
314 const Formattable ftarr1
[] = { Formattable( (int32_t)1 ), Formattable( (int32_t)2 ) };
315 const Formattable ftarr2
[] = { Formattable( (int32_t)3 ), Formattable( (int32_t)4 ) };
317 const int32_t ftarr1_cnt
= UPRV_LENGTHOF(ftarr1
);
318 const int32_t ftarr2_cnt
= UPRV_LENGTHOF(ftarr2
);
320 ft_arr
.setArray( ftarr1
, ftarr1_cnt
);
321 if ((ft_arr
[0].getType() == Formattable::kLong
) && (ft_arr
[0].getLong() == (int32_t)1)) {
322 it_logln("FT setArray tested");
324 it_errln("*** FT setArray");
327 Formattable
* ft_dynarr
= new Formattable
[ftarr2_cnt
];
328 for (i
= 0; i
< ftarr2_cnt
; i
++ ) {
329 ft_dynarr
[i
] = ftarr2
[i
];
331 if ((ft_dynarr
[0].getType() == Formattable::kLong
) && (ft_dynarr
[0].getLong() == (int32_t)3)
332 && (ft_dynarr
[1].getType() == Formattable::kLong
) && (ft_dynarr
[1].getLong() == (int32_t)4)) {
333 it_logln("FT operator= and array operations tested");
335 it_errln("*** FT operator= or array operations");
338 ft_arr
.adoptArray( ft_dynarr
, ftarr2_cnt
);
339 if ((ft_arr
[0].getType() == Formattable::kLong
) && (ft_arr
[0].getLong() == (int32_t)3)
340 && (ft_arr
[1].getType() == Formattable::kLong
) && (ft_arr
[1].getLong() == (int32_t)4)) {
341 it_logln("FT adoptArray tested");
343 it_errln("*** FT adoptArray or operator[]");
346 ft_arr
.setLong(0); // calls 'dispose' and deletes adopted array !
348 UnicodeString
* ucs_dyn
= new UnicodeString("ttt");
351 fta
.adoptString( ucs_dyn
);
352 if ((fta
.getType() == Formattable::kString
) && (fta
.getString(tmp2
) == "ttt")) {
353 it_logln("FT adoptString tested");
355 it_errln("*** FT adoptString or getString");
357 fta
.setLong(0); // calls 'dispose' and deletes adopted string !
362 void TestFormatSmallClasses::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
366 if (exec
) logln("TestSuite Format/SmallClasses/ParsePosition (f/chc/sma/pp): ");
367 if (exec
) test_ParsePosition();
370 if (exec
) logln("TestSuite Format/SmallClasses/FieldPosition (f/chc/sma/fp): ");
371 if (exec
) test_FieldPosition();
373 case 2: name
= "fpe";
374 if (exec
) logln("TestSuite Format/SmallClasses/FieldPositionExample (f/chc/sma/fpe): ");
375 if (exec
) test_FieldPosition_example();
378 if (exec
) logln("TestSuite Format/SmallClasses/Formattable (f/chc/sma/ft): ");
379 if (exec
) test_Formattable();
381 default: name
= ""; break; //needed to end loop
385 #endif /* #if !UCONFIG_NO_FORMATTING */