]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/tfsmalls.cpp
ICU-6.2.6.tar.gz
[apple/icu.git] / icuSources / test / intltest / tfsmalls.cpp
CommitLineData
b75a7d8f 1
374ca955 2/***********************************************************************
b75a7d8f 3 * COPYRIGHT:
374ca955
A
4 * Copyright (c) 1997-2004, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ***********************************************************************/
b75a7d8f
A
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_FORMATTING
11
12#include "intltest.h"
13#include "tfsmalls.h"
14
15#include "unicode/msgfmt.h"
16#include "unicode/choicfmt.h"
17
18#include "unicode/parsepos.h"
19#include "unicode/fieldpos.h"
20#include "unicode/fmtable.h"
21
374ca955 22#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
b75a7d8f
A
23
24/*static UBool chkstatus( UErrorCode &status, char* msg = NULL )
25{
26 UBool ok = (status == U_ZERO_ERROR);
27 if (!ok) it_errln( msg );
28 return ok;
29}*/
30
31void test_ParsePosition( void )
32{
33 ParsePosition* pp1 = new ParsePosition();
34 if (pp1 && (pp1->getIndex() == 0)) {
374ca955 35 it_logln("PP constructor() tested.");
b75a7d8f
A
36 }else{
37 it_errln("*** PP getIndex or constructor() result");
38 }
39 delete pp1;
40
41
42 {
43 int32_t to = 5;
44 ParsePosition pp2( to );
45 if (pp2.getIndex() == 5) {
374ca955 46 it_logln("PP getIndex and constructor(int32_t) tested.");
b75a7d8f
A
47 }else{
48 it_errln("*** PP getIndex or constructor(int32_t) result");
49 }
50 pp2.setIndex( 3 );
51 if (pp2.getIndex() == 3) {
374ca955 52 it_logln("PP setIndex tested.");
b75a7d8f
A
53 }else{
54 it_errln("*** PP getIndex or setIndex result");
55 }
56 }
57
58 ParsePosition pp2, pp3;
59 pp2 = 3;
60 pp3 = 5;
61 ParsePosition pp4( pp3 );
62 if ((pp2 != pp3) && (pp3 == pp4)) {
374ca955 63 it_logln("PP copy contructor, operator== and operator != tested.");
b75a7d8f
A
64 }else{
65 it_errln("*** PP operator== or operator != result");
66 }
67
68 ParsePosition pp5;
69 pp5 = pp4;
70 if ((pp4 == pp5) && (!(pp4 != pp5))) {
374ca955 71 it_logln("PP operator= tested.");
b75a7d8f
A
72 }else{
73 it_errln("*** PP operator= operator== or operator != result");
74 }
75
76
77}
78
79#include "unicode/decimfmt.h"
80
81void test_FieldPosition_example( void )
82{
83 //***** no error detection yet !!!!!!!
84 //***** this test is for compiler checks and visual verification only.
85 double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789,
86 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
87 int32_t dNumSize = (int32_t)(sizeof(doubleNum)/sizeof(double));
88
89 UErrorCode status = U_ZERO_ERROR;
90 DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
91 fmt->setDecimalSeparatorAlwaysShown(TRUE);
92
93 const int32_t tempLen = 20;
94 char temp[tempLen];
95
96 for (int32_t i=0; i<dNumSize; i++) {
97 FieldPosition pos(NumberFormat::INTEGER_FIELD);
98 UnicodeString buf;
99 //char fmtText[tempLen];
100 //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
101 UnicodeString res = fmt->format(doubleNum[i], buf, pos);
102 for (int32_t j=0; j<tempLen; j++) temp[j] = '='; // clear with spaces
103 int32_t tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ?
104 tempLen : (tempLen - pos.getEndIndex());
105 temp[tempOffset] = '\0';
374ca955 106 it_logln(UnicodeString("FP ") + UnicodeString(temp) + res);
b75a7d8f
A
107 }
108 delete fmt;
109
374ca955 110 it_logln("");
b75a7d8f
A
111
112}
113
114void test_FieldPosition( void )
115{
116
117 FieldPosition fp( 7 );
118
119 if (fp.getField() == 7) {
374ca955 120 it_logln("FP constructor(int32_t) and getField tested.");
b75a7d8f
A
121 }else{
122 it_errln("*** FP constructor(int32_t) or getField");
123 }
124
125 FieldPosition* fph = new FieldPosition( 3 );
126 if ( fph->getField() != 3) it_errln("*** FP getField or heap constr.");
127 delete fph;
128
129 UBool err1 = FALSE;
130 UBool err2 = FALSE;
131 UBool err3 = FALSE;
132 for (int32_t i = -50; i < 50; i++ ) {
133 fp.setField( i+8 );
134 fp.setBeginIndex( i+6 );
135 fp.setEndIndex( i+7 );
136 if (fp.getField() != i+8) err1 = TRUE;
137 if (fp.getBeginIndex() != i+6) err2 = TRUE;
138 if (fp.getEndIndex() != i+7) err3 = TRUE;
139 }
140 if (!err1) {
374ca955 141 it_logln("FP setField and getField tested.");
b75a7d8f
A
142 }else{
143 it_errln("*** FP setField or getField");
144 }
145 if (!err2) {
374ca955 146 it_logln("FP setBeginIndex and getBeginIndex tested.");
b75a7d8f
A
147 }else{
148 it_errln("*** FP setBeginIndex or getBeginIndex");
149 }
150 if (!err3) {
374ca955 151 it_logln("FP setEndIndex and getEndIndex tested.");
b75a7d8f
A
152 }else{
153 it_errln("*** FP setEndIndex or getEndIndex");
154 }
155
374ca955 156 it_logln("");
b75a7d8f
A
157
158}
159
160void test_Formattable( void )
161{
162 Formattable* ftp = new Formattable();
163 if (!ftp || !(ftp->getType() == Formattable::kLong) || !(ftp->getLong() == 0)) {
164 it_errln("*** Formattable constructor or getType or getLong");
165 }
166 delete ftp;
167
168 Formattable fta, ftb;
169 fta.setLong(1); ftb.setLong(2);
170 if ((fta != ftb) || !(fta == ftb)) {
374ca955 171 it_logln("FT setLong, operator== and operator!= tested.");
b75a7d8f
A
172 }else{
173 it_errln("*** Formattable setLong or operator== or !=");
174 }
175 fta = ftb;
176 if ((fta == ftb) || !(fta != ftb)) {
374ca955 177 it_logln("FT operator= tested.");
b75a7d8f
A
178 }else{
179 it_errln("*** FT operator= or operator== or operator!=");
180 }
181
182 fta.setDouble( 3.0 );
183 if ((fta.getType() == Formattable::kDouble) && (fta.getDouble() == 3.0)) {
374ca955 184 it_logln("FT set- and getDouble tested.");
b75a7d8f
A
185 }else{
186 it_errln("*** FT set- or getDouble");
187 }
188
189 fta.setDate( 4.0 );
190 if ((fta.getType() == Formattable::kDate) && (fta.getDate() == 4.0)) {
374ca955 191 it_logln("FT set- and getDate tested.");
b75a7d8f
A
192 }else{
193 it_errln("*** FT set- or getDate");
194 }
195
196 fta.setString("abc");
197 UnicodeString res;
198 if ((fta.getType() == Formattable::kString) && (fta.getString(res) == "abc")) {
374ca955 199 it_logln("FT set- and getString tested.");
b75a7d8f
A
200 }else{
201 it_errln("*** FT set- or getString");
202 }
203
204
205 UnicodeString ucs = "unicode-string";
206 UnicodeString* ucs_ptr = new UnicodeString("pointed-to-unicode-string");
207
208 const Formattable ftarray[] =
209 {
210 Formattable( 1.0, Formattable::kIsDate ),
211 2.0,
212 (int32_t)3,
213 ucs,
214 ucs_ptr
215 };
374ca955 216 const int32_t ft_cnt = LENGTHOF(ftarray);
b75a7d8f
A
217 Formattable ft_arr( ftarray, ft_cnt );
218 UnicodeString temp;
219 if ((ft_arr[0].getType() == Formattable::kDate) && (ft_arr[0].getDate() == 1.0)
220 && (ft_arr[1].getType() == Formattable::kDouble) && (ft_arr[1].getDouble() == 2.0)
221 && (ft_arr[2].getType() == Formattable::kLong) && (ft_arr[2].getLong() == (int32_t)3)
222 && (ft_arr[3].getType() == Formattable::kString) && (ft_arr[3].getString(temp) == ucs)
223 && (ft_arr[4].getType() == Formattable::kString) && (ft_arr[4].getString(temp) == *ucs_ptr) ) {
374ca955 224 it_logln("FT constr. for date, double, long, ustring, ustring* and array tested");
b75a7d8f
A
225 }else{
226 it_errln("*** FT constr. for date, double, long, ustring, ustring* or array");
227 }
228
374ca955 229 int32_t i, res_cnt;
b75a7d8f
A
230 const Formattable* res_array = ft_arr.getArray( res_cnt );
231 if (res_cnt == ft_cnt) {
232 UBool same = TRUE;
374ca955 233 for (i = 0; i < res_cnt; i++ ) {
b75a7d8f
A
234 if (res_array[i] != ftarray[i]) {
235 same = FALSE;
236 }
237 }
238 if (same) {
374ca955 239 it_logln("FT getArray tested");
b75a7d8f
A
240 }else{
241 it_errln("*** FT getArray comparison");
242 }
243 }else{
374ca955
A
244 it_errln(UnicodeString("*** FT getArray count res_cnt=") + res_cnt + UnicodeString("ft_cnt=") + ft_cnt);
245 }
246
247
248 Formattable *pf;
249 for(i = 0; i < ft_cnt; ++i) {
250 pf = ftarray[i].clone();
251 if(pf == (ftarray + i) || *pf != ftarray[i]) {
252 it_errln("Formattable.clone() failed for item %d" + i);
253 }
254 delete pf;
b75a7d8f
A
255 }
256
257 const Formattable ftarr1[] = { Formattable( (int32_t)1 ), Formattable( (int32_t)2 ) };
258 const Formattable ftarr2[] = { Formattable( (int32_t)3 ), Formattable( (int32_t)4 ) };
259
260 const int32_t ftarr1_cnt = (int32_t)(sizeof(ftarr1) / sizeof(Formattable));
261 const int32_t ftarr2_cnt = (int32_t)(sizeof(ftarr2) / sizeof(Formattable));
262
263 ft_arr.setArray( ftarr1, ftarr1_cnt );
264 if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)1)) {
374ca955 265 it_logln("FT setArray tested");
b75a7d8f
A
266 }else{
267 it_errln("*** FT setArray");
268 }
269
270 Formattable* ft_dynarr = new Formattable[ftarr2_cnt];
374ca955 271 for (i = 0; i < ftarr2_cnt; i++ ) {
b75a7d8f
A
272 ft_dynarr[i] = ftarr2[i];
273 }
274 if ((ft_dynarr[0].getType() == Formattable::kLong) && (ft_dynarr[0].getLong() == (int32_t)3)
275 && (ft_dynarr[1].getType() == Formattable::kLong) && (ft_dynarr[1].getLong() == (int32_t)4)) {
374ca955 276 it_logln("FT operator= and array operations tested");
b75a7d8f
A
277 }else{
278 it_errln("*** FT operator= or array operations");
279 }
280
281 ft_arr.adoptArray( ft_dynarr, ftarr2_cnt );
282 if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)3)
283 && (ft_arr[1].getType() == Formattable::kLong) && (ft_arr[1].getLong() == (int32_t)4)) {
374ca955 284 it_logln("FT adoptArray tested");
b75a7d8f
A
285 }else{
286 it_errln("*** FT adoptArray or operator[]");
287 }
288
289 ft_arr.setLong(0); // calls 'dispose' and deletes adopted array !
290
291 UnicodeString* ucs_dyn = new UnicodeString("ttt");
292 UnicodeString tmp2;
293
294 fta.adoptString( ucs_dyn );
295 if ((fta.getType() == Formattable::kString) && (fta.getString(tmp2) == "ttt")) {
374ca955 296 it_logln("FT adoptString tested");
b75a7d8f
A
297 }else{
298 it_errln("*** FT adoptString or getString");
299 }
300 fta.setLong(0); // calls 'dispose' and deletes adopted string !
301
374ca955 302 it_logln();
b75a7d8f
A
303}
304
305void TestFormatSmallClasses::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
306{
307 switch (index) {
308 case 0: name = "pp";
309 if (exec) logln("TestSuite Format/SmallClasses/ParsePosition (f/chc/sma/pp): ");
310 if (exec) test_ParsePosition();
311 break;
312 case 1: name = "fp";
313 if (exec) logln("TestSuite Format/SmallClasses/FieldPosition (f/chc/sma/fp): ");
314 if (exec) test_FieldPosition();
315 break;
316 case 2: name = "fpe";
317 if (exec) logln("TestSuite Format/SmallClasses/FieldPositionExample (f/chc/sma/fpe): ");
318 if (exec) test_FieldPosition_example();
319 break;
320 case 3: name = "ft";
321 if (exec) logln("TestSuite Format/SmallClasses/Formattable (f/chc/sma/ft): ");
322 if (exec) test_Formattable();
323 break;
324 default: name = ""; break; //needed to end loop
325 }
326}
327
328#endif /* #if !UCONFIG_NO_FORMATTING */