2
/********************************************************************
4
* Copyright (c) 1997-2001, International Business Machines Corporation and
5
* others. All Rights Reserved.
6
********************************************************************/
9
#include "unicode/utypes.h"
14
#include "unicode/msgfmt.h"
15
#include "unicode/choicfmt.h"
17
#include "unicode/parsepos.h"
18
#include "unicode/fieldpos.h"
19
#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_out << "PP constructor() tested." << endl;
35
it_errln("*** PP getIndex or constructor() result");
42
ParsePosition pp2( to );
43
if (pp2.getIndex() == 5) {
44
it_out << "PP getIndex and constructor(int32_t) tested." << endl;
46
it_errln("*** PP getIndex or constructor(int32_t) result");
49
if (pp2.getIndex() == 3) {
50
it_out << "PP setIndex tested." << endl;
52
it_errln("*** PP getIndex or setIndex result");
56
ParsePosition pp2, pp3;
59
ParsePosition pp4( pp3 );
60
if ((pp2 != pp3) && (pp3 == pp4)) {
61
it_out << "PP copy contructor, operator== and operator != tested." << endl;
63
it_errln("*** PP operator== or operator != result");
68
if ((pp4 == pp5) && (!(pp4 != pp5))) {
69
it_out << "PP operator= tested." << endl;
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 = (int32_t)(sizeof(doubleNum)/sizeof(double));
87
UErrorCode status = U_ZERO_ERROR;
88
DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
89
fmt->setDecimalSeparatorAlwaysShown(TRUE);
91
const int32_t tempLen = 20;
94
for (int32_t i=0; i<dNumSize; i++) {
95
FieldPosition pos(NumberFormat::INTEGER_FIELD);
97
//char fmtText[tempLen];
98
//ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
99
UnicodeString res = fmt->format(doubleNum[i], buf, pos);
100
for (int32_t j=0; j<tempLen; j++) temp[j] = '='; // clear with spaces
101
int32_t tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ?
102
tempLen : (tempLen - pos.getEndIndex());
103
temp[tempOffset] = '\0';
104
it_out << "FP " << temp << res << endl;
112
void test_FieldPosition( void )
115
FieldPosition fp( 7 );
117
if (fp.getField() == 7) {
118
it_out << "FP constructor(int32_t) and getField tested." << endl;
120
it_errln("*** FP constructor(int32_t) or getField");
123
FieldPosition* fph = new FieldPosition( 3 );
124
if ( fph->getField() != 3) it_errln("*** FP getField or heap constr.");
130
for (int32_t i = -50; i < 50; i++ ) {
132
fp.setBeginIndex( i+6 );
133
fp.setEndIndex( i+7 );
134
if (fp.getField() != i+8) err1 = TRUE;
135
if (fp.getBeginIndex() != i+6) err2 = TRUE;
136
if (fp.getEndIndex() != i+7) err3 = TRUE;
139
it_out << "FP setField and getField tested." << endl;
141
it_errln("*** FP setField or getField");
144
it_out << "FP setBeginIndex and getBeginIndex tested." << endl;
146
it_errln("*** FP setBeginIndex or getBeginIndex");
149
it_out << "FP setEndIndex and getEndIndex tested." << endl;
151
it_errln("*** FP setEndIndex or getEndIndex");
158
void test_Formattable( void )
160
Formattable* ftp = new Formattable();
161
if (!ftp || !(ftp->getType() == Formattable::kLong) || !(ftp->getLong() == 0)) {
162
it_errln("*** Formattable constructor or getType or getLong");
166
Formattable fta, ftb;
167
fta.setLong(1); ftb.setLong(2);
168
if ((fta != ftb) || !(fta == ftb)) {
169
it_out << "FT setLong, operator== and operator!= tested." << endl;
171
it_errln("*** Formattable setLong or operator== or !=");
174
if ((fta == ftb) || !(fta != ftb)) {
175
it_out << "FT operator= tested." << endl;
177
it_errln("*** FT operator= or operator== or operator!=");
180
fta.setDouble( 3.0 );
181
if ((fta.getType() == Formattable::kDouble) && (fta.getDouble() == 3.0)) {
182
it_out << "FT set- and getDouble tested." << endl;
184
it_errln("*** FT set- or getDouble");
188
if ((fta.getType() == Formattable::kDate) && (fta.getDate() == 4.0)) {
189
it_out << "FT set- and getDate tested." << endl;
191
it_errln("*** FT set- or getDate");
194
fta.setString("abc");
196
if ((fta.getType() == Formattable::kString) && (fta.getString(res) == "abc")) {
197
it_out << "FT set- and getString tested." << endl;
199
it_errln("*** FT set- or getString");
203
UnicodeString ucs = "unicode-string";
204
UnicodeString* ucs_ptr = new UnicodeString("pointed-to-unicode-string");
206
const Formattable ftarray[] =
208
Formattable( 1.0, Formattable::kIsDate ),
214
const int32_t ft_cnt = (int32_t)(sizeof(ftarray) / sizeof(Formattable));
215
Formattable ft_arr( ftarray, ft_cnt );
217
if ((ft_arr[0].getType() == Formattable::kDate) && (ft_arr[0].getDate() == 1.0)
218
&& (ft_arr[1].getType() == Formattable::kDouble) && (ft_arr[1].getDouble() == 2.0)
219
&& (ft_arr[2].getType() == Formattable::kLong) && (ft_arr[2].getLong() == (int32_t)3)
220
&& (ft_arr[3].getType() == Formattable::kString) && (ft_arr[3].getString(temp) == ucs)
221
&& (ft_arr[4].getType() == Formattable::kString) && (ft_arr[4].getString(temp) == *ucs_ptr) ) {
222
it_out << "FT constr. for date, double, long, ustring, ustring* and array tested" << endl;
224
it_errln("*** FT constr. for date, double, long, ustring, ustring* or array");
228
const Formattable* res_array = ft_arr.getArray( res_cnt );
229
if (res_cnt == ft_cnt) {
231
for (int32_t i = 0; i < res_cnt; i++ ) {
232
if (res_array[i] != ftarray[i]) {
237
it_out << "FT getArray tested" << endl;
239
it_errln("*** FT getArray comparison");
242
it_out << res_cnt << " " << ft_cnt << endl;
243
it_errln("*** FT getArray count");
246
const Formattable ftarr1[] = { Formattable( (int32_t)1 ), Formattable( (int32_t)2 ) };
247
const Formattable ftarr2[] = { Formattable( (int32_t)3 ), Formattable( (int32_t)4 ) };
249
const int32_t ftarr1_cnt = (int32_t)(sizeof(ftarr1) / sizeof(Formattable));
250
const int32_t ftarr2_cnt = (int32_t)(sizeof(ftarr2) / sizeof(Formattable));
252
ft_arr.setArray( ftarr1, ftarr1_cnt );
253
if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)1)) {
254
it_out << "FT setArray tested" << endl;
256
it_errln("*** FT setArray");
259
Formattable* ft_dynarr = new Formattable[ftarr2_cnt];
260
for (int32_t i = 0; i < ftarr2_cnt; i++ ) {
261
ft_dynarr[i] = ftarr2[i];
263
if ((ft_dynarr[0].getType() == Formattable::kLong) && (ft_dynarr[0].getLong() == (int32_t)3)
264
&& (ft_dynarr[1].getType() == Formattable::kLong) && (ft_dynarr[1].getLong() == (int32_t)4)) {
265
it_out << "FT operator= and array operations tested" << endl;
267
it_errln("*** FT operator= or array operations");
270
ft_arr.adoptArray( ft_dynarr, ftarr2_cnt );
271
if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)3)
272
&& (ft_arr[1].getType() == Formattable::kLong) && (ft_arr[1].getLong() == (int32_t)4)) {
273
it_out << "FT adoptArray tested" << endl;
275
it_errln("*** FT adoptArray or operator[]");
278
ft_arr.setLong(0); // calls 'dispose' and deletes adopted array !
280
UnicodeString* ucs_dyn = new UnicodeString("ttt");
283
fta.adoptString( ucs_dyn );
284
if ((fta.getType() == Formattable::kString) && (fta.getString(tmp2) == "ttt")) {
285
it_out << "FT adoptString tested" << endl;
287
it_errln("*** FT adoptString or getString");
289
fta.setLong(0); // calls 'dispose' and deletes adopted string !
295
void TestFormatSmallClasses::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
299
if (exec) logln("TestSuite Format/SmallClasses/ParsePosition (f/chc/sma/pp): ");
300
if (exec) test_ParsePosition();
303
if (exec) logln("TestSuite Format/SmallClasses/FieldPosition (f/chc/sma/fp): ");
304
if (exec) test_FieldPosition();
306
case 2: name = "fpe";
307
if (exec) logln("TestSuite Format/SmallClasses/FieldPositionExample (f/chc/sma/fpe): ");
308
if (exec) test_FieldPosition_example();
311
if (exec) logln("TestSuite Format/SmallClasses/Formattable (f/chc/sma/ft): ");
312
if (exec) test_Formattable();
314
default: name = ""; break; //needed to end loop