]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/tfsmalls.cpp
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / tfsmalls.cpp
CommitLineData
374ca955 1/***********************************************************************
2ca993e8
A
2 * COPYRIGHT:
3 * Copyright (c) 1997-2016, International Business Machines Corporation
374ca955
A
4 * and others. All Rights Reserved.
5 ***********************************************************************/
b75a7d8f
A
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_FORMATTING
10
11#include "intltest.h"
12#include "tfsmalls.h"
b331163b 13#include "cmemory.h"
b75a7d8f
A
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
b75a7d8f
A
22/*static UBool chkstatus( UErrorCode &status, char* msg = NULL )
23{
24 UBool ok = (status == U_ZERO_ERROR);
25 if (!ok) it_errln( msg );
26 return ok;
27}*/
28
29void test_ParsePosition( void )
30{
31 ParsePosition* pp1 = new ParsePosition();
32 if (pp1 && (pp1->getIndex() == 0)) {
374ca955 33 it_logln("PP constructor() tested.");
b75a7d8f
A
34 }else{
35 it_errln("*** PP getIndex or constructor() result");
36 }
37 delete pp1;
38
39
40 {
41 int32_t to = 5;
42 ParsePosition pp2( to );
43 if (pp2.getIndex() == 5) {
374ca955 44 it_logln("PP getIndex and constructor(int32_t) tested.");
b75a7d8f
A
45 }else{
46 it_errln("*** PP getIndex or constructor(int32_t) result");
47 }
48 pp2.setIndex( 3 );
49 if (pp2.getIndex() == 3) {
374ca955 50 it_logln("PP setIndex tested.");
b75a7d8f
A
51 }else{
52 it_errln("*** PP getIndex or setIndex result");
53 }
54 }
55
56 ParsePosition pp2, pp3;
57 pp2 = 3;
58 pp3 = 5;
59 ParsePosition pp4( pp3 );
60 if ((pp2 != pp3) && (pp3 == pp4)) {
374ca955 61 it_logln("PP copy contructor, operator== and operator != tested.");
b75a7d8f
A
62 }else{
63 it_errln("*** PP operator== or operator != result");
64 }
65
66 ParsePosition pp5;
67 pp5 = pp4;
68 if ((pp4 == pp5) && (!(pp4 != pp5))) {
374ca955 69 it_logln("PP operator= tested.");
b75a7d8f
A
70 }else{
71 it_errln("*** PP operator= operator== or operator != result");
72 }
73
74
75}
76
77#include "unicode/decimfmt.h"
78
79void test_FieldPosition_example( void )
80{
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};
2ca993e8 85 int32_t dNumSize = UPRV_LENGTHOF(doubleNum);
b75a7d8f
A
86
87 UErrorCode status = U_ZERO_ERROR;
88 DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
73c04bcf
A
89 if (U_FAILURE(status)) {
90 it_dataerrln("NumberFormat::createInstance() error");
91 return;
92 }
b75a7d8f
A
93 fmt->setDecimalSeparatorAlwaysShown(TRUE);
94
95 const int32_t tempLen = 20;
96 char temp[tempLen];
97
98 for (int32_t i=0; i<dNumSize; i++) {
99 FieldPosition pos(NumberFormat::INTEGER_FIELD);
100 UnicodeString buf;
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';
374ca955 108 it_logln(UnicodeString("FP ") + UnicodeString(temp) + res);
b75a7d8f
A
109 }
110 delete fmt;
111
374ca955 112 it_logln("");
b75a7d8f
A
113
114}
115
116void test_FieldPosition( void )
117{
118
119 FieldPosition fp( 7 );
120
121 if (fp.getField() == 7) {
374ca955 122 it_logln("FP constructor(int32_t) and getField tested.");
b75a7d8f
A
123 }else{
124 it_errln("*** FP constructor(int32_t) or getField");
125 }
126
127 FieldPosition* fph = new FieldPosition( 3 );
128 if ( fph->getField() != 3) it_errln("*** FP getField or heap constr.");
129 delete fph;
130
131 UBool err1 = FALSE;
132 UBool err2 = FALSE;
133 UBool err3 = FALSE;
134 for (int32_t i = -50; i < 50; i++ ) {
135 fp.setField( i+8 );
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;
141 }
142 if (!err1) {
374ca955 143 it_logln("FP setField and getField tested.");
b75a7d8f
A
144 }else{
145 it_errln("*** FP setField or getField");
146 }
147 if (!err2) {
374ca955 148 it_logln("FP setBeginIndex and getBeginIndex tested.");
b75a7d8f
A
149 }else{
150 it_errln("*** FP setBeginIndex or getBeginIndex");
151 }
152 if (!err3) {
374ca955 153 it_logln("FP setEndIndex and getEndIndex tested.");
b75a7d8f
A
154 }else{
155 it_errln("*** FP setEndIndex or getEndIndex");
156 }
157
374ca955 158 it_logln("");
b75a7d8f
A
159
160}
161
162void test_Formattable( void )
163{
73c04bcf 164 UErrorCode status = U_ZERO_ERROR;
b75a7d8f
A
165 Formattable* ftp = new Formattable();
166 if (!ftp || !(ftp->getType() == Formattable::kLong) || !(ftp->getLong() == 0)) {
167 it_errln("*** Formattable constructor or getType or getLong");
168 }
169 delete ftp;
170
171 Formattable fta, ftb;
172 fta.setLong(1); ftb.setLong(2);
173 if ((fta != ftb) || !(fta == ftb)) {
374ca955 174 it_logln("FT setLong, operator== and operator!= tested.");
73c04bcf
A
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");
179 } else {
180 it_logln("FT getLong(UErrorCode* status) tested.");
181 }
b75a7d8f
A
182 }else{
183 it_errln("*** Formattable setLong or operator== or !=");
184 }
185 fta = ftb;
186 if ((fta == ftb) || !(fta != ftb)) {
374ca955 187 it_logln("FT operator= tested.");
b75a7d8f
A
188 }else{
189 it_errln("*** FT operator= or operator== or operator!=");
190 }
191
192 fta.setDouble( 3.0 );
193 if ((fta.getType() == Formattable::kDouble) && (fta.getDouble() == 3.0)) {
374ca955 194 it_logln("FT set- and getDouble tested.");
b75a7d8f
A
195 }else{
196 it_errln("*** FT set- or getDouble");
197 }
73c04bcf
A
198
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");
202 }
b75a7d8f
A
203 fta.setDate( 4.0 );
204 if ((fta.getType() == Formattable::kDate) && (fta.getDate() == 4.0)) {
73c04bcf
A
205 it_logln("FT set- and getDate tested.");
206 status = U_ZERO_ERROR;
207 fta.getDate(status);
208 if ( status == U_INVALID_FORMAT_ERROR){
209 it_errln("*** FT getDate with status failed on real Date");
210 } else {
211 it_logln("FT getDate with status tested.");
212 }
b75a7d8f
A
213 }else{
214 it_errln("*** FT set- or getDate");
215 }
216
73c04bcf
A
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");
221 }
222
b75a7d8f 223 fta.setString("abc");
73c04bcf 224 const Formattable ftc(fta);
b75a7d8f 225 UnicodeString res;
b75a7d8f 226
73c04bcf
A
227 {
228 UBool t;
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");
238 if (t) {
239 it_logln("FT set- and getString tested.");
240 }else{
241 it_errln("*** FT set- or getString");
242 }
243 }
b75a7d8f
A
244
245 UnicodeString ucs = "unicode-string";
246 UnicodeString* ucs_ptr = new UnicodeString("pointed-to-unicode-string");
247
248 const Formattable ftarray[] =
249 {
250 Formattable( 1.0, Formattable::kIsDate ),
251 2.0,
252 (int32_t)3,
253 ucs,
254 ucs_ptr
255 };
b331163b 256 const int32_t ft_cnt = UPRV_LENGTHOF(ftarray);
b75a7d8f
A
257 Formattable ft_arr( ftarray, ft_cnt );
258 UnicodeString temp;
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) ) {
374ca955 264 it_logln("FT constr. for date, double, long, ustring, ustring* and array tested");
b75a7d8f
A
265 }else{
266 it_errln("*** FT constr. for date, double, long, ustring, ustring* or array");
267 }
268
374ca955 269 int32_t i, res_cnt;
b75a7d8f
A
270 const Formattable* res_array = ft_arr.getArray( res_cnt );
271 if (res_cnt == ft_cnt) {
272 UBool same = TRUE;
374ca955 273 for (i = 0; i < res_cnt; i++ ) {
b75a7d8f
A
274 if (res_array[i] != ftarray[i]) {
275 same = FALSE;
276 }
277 }
278 if (same) {
374ca955 279 it_logln("FT getArray tested");
73c04bcf
A
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");
283 } else {
284 it_logln("FT getArray with status tested on real array");
285 }
b75a7d8f
A
286 }else{
287 it_errln("*** FT getArray comparison");
288 }
289 }else{
374ca955
A
290 it_errln(UnicodeString("*** FT getArray count res_cnt=") + res_cnt + UnicodeString("ft_cnt=") + ft_cnt);
291 }
73c04bcf
A
292
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");
297 } else {
298 it_errln("*** FT getArray with status return values are not consistent");
299 }
300 } else {
301 it_errln("*** FT getArray with status should fail on non-array");
302 }
374ca955
A
303
304
305 Formattable *pf;
306 for(i = 0; i < ft_cnt; ++i) {
307 pf = ftarray[i].clone();
308 if(pf == (ftarray + i) || *pf != ftarray[i]) {
51004dcb 309 it_errln(UnicodeString("Formattable.clone() failed for item ") + i);
374ca955
A
310 }
311 delete pf;
b75a7d8f
A
312 }
313
314 const Formattable ftarr1[] = { Formattable( (int32_t)1 ), Formattable( (int32_t)2 ) };
315 const Formattable ftarr2[] = { Formattable( (int32_t)3 ), Formattable( (int32_t)4 ) };
316
2ca993e8
A
317 const int32_t ftarr1_cnt = UPRV_LENGTHOF(ftarr1);
318 const int32_t ftarr2_cnt = UPRV_LENGTHOF(ftarr2);
b75a7d8f
A
319
320 ft_arr.setArray( ftarr1, ftarr1_cnt );
321 if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)1)) {
374ca955 322 it_logln("FT setArray tested");
b75a7d8f
A
323 }else{
324 it_errln("*** FT setArray");
325 }
326
327 Formattable* ft_dynarr = new Formattable[ftarr2_cnt];
374ca955 328 for (i = 0; i < ftarr2_cnt; i++ ) {
b75a7d8f
A
329 ft_dynarr[i] = ftarr2[i];
330 }
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)) {
374ca955 333 it_logln("FT operator= and array operations tested");
b75a7d8f
A
334 }else{
335 it_errln("*** FT operator= or array operations");
336 }
337
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)) {
374ca955 341 it_logln("FT adoptArray tested");
b75a7d8f
A
342 }else{
343 it_errln("*** FT adoptArray or operator[]");
344 }
345
346 ft_arr.setLong(0); // calls 'dispose' and deletes adopted array !
347
348 UnicodeString* ucs_dyn = new UnicodeString("ttt");
349 UnicodeString tmp2;
350
351 fta.adoptString( ucs_dyn );
352 if ((fta.getType() == Formattable::kString) && (fta.getString(tmp2) == "ttt")) {
374ca955 353 it_logln("FT adoptString tested");
b75a7d8f
A
354 }else{
355 it_errln("*** FT adoptString or getString");
356 }
357 fta.setLong(0); // calls 'dispose' and deletes adopted string !
358
374ca955 359 it_logln();
b75a7d8f
A
360}
361
362void TestFormatSmallClasses::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
363{
364 switch (index) {
365 case 0: name = "pp";
366 if (exec) logln("TestSuite Format/SmallClasses/ParsePosition (f/chc/sma/pp): ");
367 if (exec) test_ParsePosition();
368 break;
369 case 1: name = "fp";
370 if (exec) logln("TestSuite Format/SmallClasses/FieldPosition (f/chc/sma/fp): ");
371 if (exec) test_FieldPosition();
372 break;
373 case 2: name = "fpe";
374 if (exec) logln("TestSuite Format/SmallClasses/FieldPositionExample (f/chc/sma/fpe): ");
375 if (exec) test_FieldPosition_example();
376 break;
377 case 3: name = "ft";
378 if (exec) logln("TestSuite Format/SmallClasses/Formattable (f/chc/sma/ft): ");
379 if (exec) test_Formattable();
380 break;
381 default: name = ""; break; //needed to end loop
382 }
383}
384
385#endif /* #if !UCONFIG_NO_FORMATTING */