]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tchcfmt.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / intltest / tchcfmt.cpp
1
2 /********************************************************************
3 * COPYRIGHT:
4 * Copyright (c) 1997-2003, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 ********************************************************************/
7
8 #include "unicode/utypes.h"
9
10 #if !UCONFIG_NO_FORMATTING
11
12 #include "intltest.h"
13 #include "tchcfmt.h"
14 #include "cmemory.h"
15 #include "unicode/msgfmt.h"
16 #include "unicode/choicfmt.h"
17
18 #include <float.h>
19
20 // tests have obvious memory leaks!
21
22 void TestChoiceFormat::runIndexedTest(int32_t index, UBool exec,
23 const char* &name, char* /*par*/) {
24 switch (index) {
25 TESTCASE(0,TestSimpleExample);
26 TESTCASE(1,TestComplexExample);
27 TESTCASE(2,TestClosures);
28 TESTCASE(3,TestPatterns);
29 default: name = ""; break;
30 }
31 }
32
33 static UBool chkstatus( UErrorCode &status, const char* msg = NULL )
34 {
35 UBool ok = U_SUCCESS(status);
36 if (!ok) it_errln( msg );
37 return ok;
38 }
39
40 void
41 TestChoiceFormat::TestSimpleExample( void )
42 {
43 double limits[] = {1,2,3,4,5,6,7};
44 UnicodeString monthNames[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
45 ChoiceFormat* form = new ChoiceFormat(limits, monthNames, 7);
46 ParsePosition parse_pos;
47 // TODO Fix this ParsePosition stuff...
48 UnicodeString str;
49 UnicodeString res1, res2;
50 UErrorCode status;
51 FieldPosition fpos(0);
52 Formattable f;
53 int32_t ix;
54 //for (double i = 0.0; i <= 8.0; ++i) {
55 for (ix = 0; ix <= 8; ++ix) {
56 double i = ix; //nos
57 status = U_ZERO_ERROR;
58 fpos = 0;
59 str = "";
60 res1 = form->format(i, str, fpos, status );
61 if (!chkstatus( status, "*** test_simple_example format" )) {
62 delete form;
63 return;
64 }
65 //form->parse(res1, f, parse_pos);
66 res2 = " ??? ";
67 it_out << ix << " -> " << res1 << " -> " << res2 << endl;
68 }
69 //Testing ==operator
70 const double filelimits[] = {0,1,2};
71 const UnicodeString filepart[] = {"are no files","is one file","are {2} files"};
72 ChoiceFormat* formnew=new ChoiceFormat(filelimits, filepart, 3);
73 ChoiceFormat* formequal=new ChoiceFormat(limits, monthNames, 7);
74 if(*formnew == *form){
75 errln("ERROR: ==operator failed\n");
76 }
77 if(!(*form == *formequal)){
78 errln("ERROR: ==operator failed\n");
79 }
80 delete formequal;
81
82 #ifdef U_USE_CHOICE_FORMAT_DEPRECATES
83 //Testing adoptChoices()
84 double *limitsToAdopt = (double *)uprv_malloc(7 * sizeof(double));
85 UnicodeString *monthNamesToAdopt = new UnicodeString[7];
86
87 uprv_arrayCopy(monthNames, monthNamesToAdopt, 7);
88 uprv_memcpy(limitsToAdopt, limits, (size_t)(7 * sizeof(limits[0])));
89
90 formnew->adoptChoices(limitsToAdopt, monthNamesToAdopt, 7);
91 if(!(*formnew == *form)){
92 errln("ERROR: ==Operator or adoptChoices failed\n");
93 }
94 #endif
95
96 delete formnew;
97
98 //Testing getLimits()
99 double *gotLimits=0;
100 int32_t count=0;
101 gotLimits=(double*)form->getLimits(count);
102 if(count != 7){
103 errln("getLimits didn't update the count correctly\n");
104 }
105 for(ix=0; ix<count; ix++){
106 if(gotLimits[ix] != limits[ix]){
107 errln((UnicodeString)"getLimits didn't get the limits correctly. Expected " + limits[ix] + " Got " + gotLimits[ix]);
108 }
109 }
110 //Testing getFormat()
111 count=0;
112 UnicodeString *gotFormats=0;
113 gotFormats=(UnicodeString*)form->getFormats(count);
114 if(count != 7){
115 errln("getFormats didn't update the count correctly\n");
116 }
117 for(ix=0; ix<count; ix++){
118 if(gotFormats[ix] != monthNames[ix]){
119 errln((UnicodeString)"getFormats didn't get the Formats correctly. Expected " + monthNames[ix] + " Got " + gotFormats[ix]);
120 }
121 }
122
123
124 delete form;
125
126 }
127
128 void
129 TestChoiceFormat::TestComplexExample( void )
130 {
131 UErrorCode status = U_ZERO_ERROR;
132 const double filelimits[] = {-1, 0,1,2};
133 const UnicodeString filepart[] = {"are corrupted files", "are no files","is one file","are {2} files"};
134
135 ChoiceFormat* fileform = new ChoiceFormat( filelimits, filepart, 4);
136
137 if (!fileform) {
138 it_errln("*** test_complex_example fileform");
139 return;
140 }
141
142 Format* filenumform = NumberFormat::createInstance( status );
143 if (!filenumform) {
144 it_errln("*** test_complex_example filenumform");
145 delete fileform;
146 return;
147 }
148 if (!chkstatus( status, "*** test_simple_example filenumform" )) {
149 delete fileform;
150 delete filenumform;
151 return;
152 }
153
154 //const Format* testFormats[] = { fileform, NULL, filenumform };
155 //pattform->setFormats( testFormats, 3 );
156
157 MessageFormat* pattform = new MessageFormat("There {0} on {1}", status );
158 if (!pattform) {
159 it_errln("*** test_complex_example pattform");
160 delete fileform;
161 delete filenumform;
162 return;
163 }
164 if (!chkstatus( status, "*** test_complex_example pattform" )) {
165 delete fileform;
166 delete filenumform;
167 delete pattform;
168 return;
169 }
170
171 pattform->setFormat( 0, *fileform );
172 pattform->setFormat( 2, *filenumform );
173
174
175 Formattable testArgs[] = {(int32_t)0, "Disk_A", (int32_t)0};
176 UnicodeString str;
177 UnicodeString res1, res2;
178 pattform->toPattern( res1 );
179 it_out << "MessageFormat toPattern: " << res1 << endl;
180 fileform->toPattern( res1 );
181 it_out << "ChoiceFormat toPattern: " << res1 << endl;
182 if (res1 == "-1.0#are corrupted files|0.0#are no files|1.0#is one file|2.0#are {2} files") {
183 it_out << "toPattern tested!" << endl;
184 }else{
185 it_errln("*** ChoiceFormat to Pattern result!");
186 }
187
188 FieldPosition fpos(0);
189
190 UnicodeString checkstr[] = {
191 "There are corrupted files on Disk_A",
192 "There are no files on Disk_A",
193 "There is one file on Disk_A",
194 "There are 2 files on Disk_A",
195 "There are 3 files on Disk_A"
196 };
197
198 // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a bad bail out here!
199
200 if (U_FAILURE(status)) {
201 delete fileform;
202 delete filenumform;
203 delete pattform;
204 return;
205 }
206
207
208 int32_t i;
209 int32_t start = -1;
210 for (i = start; i < 4; ++i) {
211 str = "";
212 status = U_ZERO_ERROR;
213 testArgs[0] = Formattable((int32_t)i);
214 testArgs[2] = testArgs[0];
215 res2 = pattform->format(testArgs, 3, str, fpos, status );
216 if (!chkstatus( status, "*** test_complex_example format" )) {
217 delete fileform;
218 delete filenumform;
219 delete pattform;
220 return;
221 }
222 it_out << i << " -> " << res2 << endl;
223 if (res2 != checkstr[i - start]) {
224 it_errln("*** test_complex_example res string");
225 it_out << "*** " << i << " -> '" << res2 << "' unlike '" << checkstr[i] << "' ! " << endl;
226 }
227 }
228 it_out << endl;
229
230 it_out << "------ additional testing in complex test ------" << endl << endl;
231 //
232 int32_t retCount;
233 const double* retLimits = fileform->getLimits( retCount );
234 if ((retCount == 4) && (retLimits)
235 && (retLimits[0] == -1.0)
236 && (retLimits[1] == 0.0)
237 && (retLimits[2] == 1.0)
238 && (retLimits[3] == 2.0)) {
239 it_out << "getLimits tested!" << endl;
240 }else{
241 it_errln("*** getLimits unexpected result!");
242 }
243
244 const UnicodeString* retFormats = fileform->getFormats( retCount );
245 if ((retCount == 4) && (retFormats)
246 && (retFormats[0] == "are corrupted files")
247 && (retFormats[1] == "are no files")
248 && (retFormats[2] == "is one file")
249 && (retFormats[3] == "are {2} files")) {
250 it_out << "getFormats tested!" << endl;
251 }else{
252 it_errln("*** getFormats unexpected result!");
253 }
254
255 UnicodeString checkstr2[] = {
256 "There is no folder on Disk_A",
257 "There is one folder on Disk_A",
258 "There are many folders on Disk_A",
259 "There are many folders on Disk_A"
260 };
261
262 fileform->applyPattern("0#is no folder|1#is one folder|2#are many folders", status );
263 if (status == U_ZERO_ERROR) it_out << "status applyPattern OK!" << endl;
264 if (!chkstatus( status, "*** test_complex_example pattform" )) {
265 delete fileform;
266 delete filenumform;
267 delete pattform;
268 return;
269 }
270 pattform->setFormat( 0, *fileform );
271 fpos = 0;
272 for (i = 0; i < 4; ++i) {
273 str = "";
274 status = U_ZERO_ERROR;
275 testArgs[0] = Formattable((int32_t)i);
276 testArgs[2] = testArgs[0];
277 res2 = pattform->format(testArgs, 3, str, fpos, status );
278 if (!chkstatus( status, "*** test_complex_example format 2" )) {
279 delete fileform;
280 delete filenumform;
281 delete pattform;
282 return;
283 }
284 it_out << i << " -> " << res2 << endl;
285 if (res2 != checkstr2[i]) {
286 it_errln("*** test_complex_example res string");
287 it_out << "*** " << i << " -> '" << res2 << "' unlike '" << checkstr2[i] << "' ! " << endl;
288 }
289 }
290
291 const double limits_A[] = {1,2,3,4,5,6,7};
292 const UnicodeString monthNames_A[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
293 ChoiceFormat* form_A = new ChoiceFormat(limits_A, monthNames_A, 7);
294 ChoiceFormat* form_A2 = new ChoiceFormat(limits_A, monthNames_A, 7);
295 const double limits_B[] = {1,2,3,4,5,6,7};
296 const UnicodeString monthNames_B[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat_BBB"};
297 ChoiceFormat* form_B = new ChoiceFormat(limits_B, monthNames_B, 7);
298 if (!form_A || !form_B || !form_A2) {
299 it_errln("*** test-choiceFormat not allocatable!");
300 }else{
301 if (*form_A == *form_A2) {
302 it_out << "operator== tested." << endl;
303 }else{
304 it_errln("*** operator==");
305 }
306
307 if (*form_A != *form_B) {
308 it_out << "operator!= tested." << endl;
309 }else{
310 it_errln("*** operator!=");
311 }
312
313 ChoiceFormat* form_A3 = (ChoiceFormat*) form_A->clone();
314 if (!form_A3) {
315 it_errln("*** ChoiceFormat->clone is nil.");
316 }else{
317 if ((*form_A3 == *form_A) && (*form_A3 != *form_B)) {
318 it_out << "method clone tested." << endl;
319 }else{
320 it_errln("*** ChoiceFormat clone or operator==, or operator!= .");
321 }
322 }
323
324 ChoiceFormat form_Assigned( *form_A );
325 UBool ok = (form_Assigned == *form_A) && (form_Assigned != *form_B);
326 form_Assigned = *form_B;
327 ok = ok && (form_Assigned != *form_A) && (form_Assigned == *form_B);
328 if (ok) {
329 it_out << "copy constructor and operator= tested." << endl;
330 }else{
331 it_errln("*** copy constructor or operator= or operator == or operator != .");
332 }
333 delete form_A3;
334 }
335
336
337 delete form_A; delete form_A2; delete form_B;
338
339 const char* testPattern = "0#none|1#one|2#many";
340 ChoiceFormat form_pat( testPattern, status );
341 if (!chkstatus( status, "*** ChoiceFormat contructor( newPattern, status)" )) {
342 delete fileform;
343 delete filenumform;
344 delete pattform;
345 return;
346 }
347
348 form_pat.toPattern( res1 );
349 if (res1 == "0.0#none|1.0#one|2.0#many") {
350 it_out << "ChoiceFormat contructor( newPattern, status) tested" << endl;
351 }else{
352 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!");
353 }
354
355 #ifdef U_USE_CHOICE_FORMAT_DEPRECATES
356 double* d_a = (double *)uprv_malloc(2 * sizeof(double));
357 if (!d_a) { it_errln("*** allocation error."); return; }
358 d_a[0] = 1.0; d_a[1] = 2.0;
359
360 UnicodeString* s_a = new UnicodeString[2];
361 if (!s_a) { it_errln("*** allocation error."); return; }
362 s_a[0] = "first"; s_a[1] = "second";
363
364 form_pat.adoptChoices( d_a, s_a, 2 );
365 form_pat.toPattern( res1 );
366 it_out << "ChoiceFormat adoptChoices toPattern: " << res1 << endl;
367 if (res1 == "1.0#first|2.0#second") {
368 it_out << "ChoiceFormat adoptChoices tested" << endl;
369 }else{
370 it_errln("*** ChoiceFormat adoptChoices result!");
371 }
372 #endif
373
374 double d_a2[] = { 3.0, 4.0 };
375 UnicodeString s_a2[] = { "third", "forth" };
376
377 form_pat.setChoices( d_a2, s_a2, 2 );
378 form_pat.toPattern( res1 );
379 it_out << "ChoiceFormat adoptChoices toPattern: " << res1 << endl;
380 if (res1 == "3.0#third|4.0#forth") {
381 it_out << "ChoiceFormat adoptChoices tested" << endl;
382 }else{
383 it_errln("*** ChoiceFormat adoptChoices result!");
384 }
385
386 str = "";
387 fpos = 0;
388 status = U_ZERO_ERROR;
389 double arg_double = 3.0;
390 res1 = form_pat.format( arg_double, str, fpos );
391 it_out << "ChoiceFormat format:" << res1 << endl;
392 if (res1 != "third") it_errln("*** ChoiceFormat format (double, ...) result!");
393
394 str = "";
395 fpos = 0;
396 status = U_ZERO_ERROR;
397 int32_t arg_long = 3;
398 res1 = form_pat.format( arg_long, str, fpos );
399 it_out << "ChoiceFormat format:" << res1 << endl;
400 if (res1 != "third") it_errln("*** ChoiceFormat format (int32_t, ...) result!");
401
402 Formattable ft( (int32_t)3 );
403 str = "";
404 fpos = 0;
405 status = U_ZERO_ERROR;
406 res1 = form_pat.format( ft, str, fpos, status );
407 if (!chkstatus( status, "*** test_complex_example format (int32_t, ...)" )) {
408 delete fileform;
409 delete filenumform;
410 delete pattform;
411 return;
412 }
413 it_out << "ChoiceFormat format:" << res1 << endl;
414 if (res1 != "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!");
415
416 Formattable fta[] = { (int32_t)3 };
417 str = "";
418 fpos = 0;
419 status = U_ZERO_ERROR;
420 res1 = form_pat.format( fta, 1, str, fpos, status );
421 if (!chkstatus( status, "*** test_complex_example format (int32_t, ...)" )) {
422 delete fileform;
423 delete filenumform;
424 delete pattform;
425 return;
426 }
427 it_out << "ChoiceFormat format:" << res1 << endl;
428 if (res1 != "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!");
429
430 ParsePosition parse_pos = 0;
431 Formattable result;
432 UnicodeString parsetext("third");
433 form_pat.parse( parsetext, result, parse_pos );
434 double rd = (result.getType() == Formattable::kLong) ? result.getLong() : result.getDouble();
435 if (rd == 3.0) {
436 it_out << "parse( ..., ParsePos ) tested." << endl;
437 }else{
438 it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
439 }
440
441 form_pat.parse( parsetext, result, status );
442 rd = (result.getType() == Formattable::kLong) ? result.getLong() : result.getDouble();
443 if (rd == 3.0) {
444 it_out << "parse( ..., UErrorCode ) tested." << endl;
445 }else{
446 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
447 }
448
449 /*
450 UClassID classID = ChoiceFormat::getStaticClassID();
451 if (classID == form_pat.getDynamicClassID()) {
452 it_out << "getStaticClassID and getDynamicClassID tested." << endl;
453 }else{
454 it_errln("*** getStaticClassID and getDynamicClassID!");
455 }
456 */
457
458 it_out << endl;
459
460 delete fileform;
461 delete filenumform;
462 delete pattform;
463 }
464
465
466 /**
467 * Test new closure API
468 */
469 void TestChoiceFormat::TestClosures(void) {
470 // Construct open, half-open, half-open (the other way), and closed
471 // intervals. Do this both using arrays and using a pattern.
472
473 // 'fmt1' is created using arrays
474 UBool T = TRUE, F = FALSE;
475 // 0: ,1)
476 // 1: [1,2]
477 // 2: (2,3]
478 // 3: (3,4)
479 // 4: [4,5)
480 // 5: [5,
481 double limits[] = { 0, 1, 2, 3, 4, 5 };
482 UBool closures[] = { F, F, T, T, F, F };
483 UnicodeString fmts[] = {
484 ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5,"
485 };
486 ChoiceFormat fmt1(limits, closures, fmts, 6);
487
488 // 'fmt2' is created using a pattern; it should be equivalent
489 UErrorCode status = U_ZERO_ERROR;
490 const char* PAT = "0.0#,1)|1.0#[1,2]|2.0<(2,3]|3.0<(3,4)|4.0#[4,5)|5.0#[5,";
491 ChoiceFormat fmt2(PAT, status);
492 if (U_FAILURE(status)) {
493 errln("FAIL: ChoiceFormat constructor failed");
494 return;
495 }
496
497 // Check the patterns
498 UnicodeString str;
499 fmt1.toPattern(str);
500 if (str == PAT) {
501 logln("Ok: " + str);
502 } else {
503 errln("FAIL: " + str + ", expected " + PAT);
504 }
505 str.truncate(0);
506
507 // Check equality
508 if (fmt1 != fmt2) {
509 errln("FAIL: fmt1 != fmt2");
510 }
511
512 int32_t i;
513 int32_t count2 = 0;
514 const double *limits2 = fmt2.getLimits(count2);
515 const UBool *closures2 = fmt2.getClosures(count2);
516
517 if((count2 != 6) || !limits2 || !closures2) {
518 errln("FAIL: couldn't get limits or closures");
519 } else {
520 for(i=0;i<count2;i++) {
521 logln("#%d/%d: limit %g closed %s\n",
522 i, count2,
523 limits2[i],
524 closures2[i] ?"T":"F");
525 if(limits2[i] != limits[i]) {
526 errln("FAIL: limit #%d = %g, should be %g\n", i, limits2[i], limits[i]);
527 }
528 if((closures2[i]!=0) != (closures[i]!=0)) {
529 errln("FAIL: closure #%d = %s, should be %s\n", i, closures2[i]?"T":"F", closures[i]?"T":"F");
530 }
531 }
532 }
533
534 // Now test both format objects
535 UnicodeString exp[] = {
536 /*-0.5 => */ ",1)",
537 /* 0.0 => */ ",1)",
538 /* 0.5 => */ ",1)",
539 /* 1.0 => */ "[1,2]",
540 /* 1.5 => */ "[1,2]",
541 /* 2.0 => */ "[1,2]",
542 /* 2.5 => */ "(2,3]",
543 /* 3.0 => */ "(2,3]",
544 /* 3.5 => */ "(3,4)",
545 /* 4.0 => */ "[4,5)",
546 /* 4.5 => */ "[4,5)",
547 /* 5.0 => */ "[5,",
548 /* 5.5 => */ "[5,"
549 };
550
551 // Each format object should behave exactly the same
552 ChoiceFormat* FMT[] = { &fmt1, &fmt2 };
553 for (int32_t pass=0; pass<2; ++pass) {
554 int32_t j=0;
555 for (int32_t ix=-5; ix<=55; ix+=5) {
556 double x = ix / 10.0; // -0.5 to 5.5 step +0.5
557 FMT[pass]->format(x, str);
558 if (str == exp[j]) {
559 logln((UnicodeString)"Ok: " + x + " => " + str);
560 } else {
561 errln((UnicodeString)"FAIL: " + x + " => " + str +
562 ", expected " + exp[j]);
563 }
564 str.truncate(0);
565 ++j;
566 }
567 }
568 }
569
570 /**
571 * Helper for TestPatterns()
572 */
573 void TestChoiceFormat::_testPattern(const char* pattern,
574 UBool isValid,
575 double v1, const char* str1,
576 double v2, const char* str2,
577 double v3, const char* str3) {
578 UErrorCode ec = U_ZERO_ERROR;
579 ChoiceFormat fmt(pattern, ec);
580 if (!isValid) {
581 if (U_FAILURE(ec)) {
582 logln((UnicodeString)"Ok: " + pattern + " failed");
583 } else {
584 logln((UnicodeString)"FAIL: " + pattern + " accepted");
585 }
586 return;
587 }
588 if (U_FAILURE(ec)) {
589 errln((UnicodeString)"FAIL: ChoiceFormat(" + pattern + ") failed");
590 return;
591 } else {
592 logln((UnicodeString)"Ok: Pattern: " + pattern);
593 }
594 UnicodeString out;
595 logln((UnicodeString)" toPattern: " + fmt.toPattern(out));
596
597 double v[] = {v1, v2, v3};
598 const char* str[] = {str1, str2, str3};
599 for (int32_t i=0; i<3; ++i) {
600 out.truncate(0);
601 fmt.format(v[i], out);
602 if (out == str[i]) {
603 logln((UnicodeString)"Ok: " + v[i] + " => " + out);
604 } else {
605 errln((UnicodeString)"FAIL: " + v[i] + " => " + out +
606 ", expected " + str[i]);
607 }
608 }
609 }
610
611 /**
612 * Test applyPattern
613 */
614 void TestChoiceFormat::TestPatterns(void) {
615 // Try a pattern that isolates a single value. Create
616 // three ranges: [-Inf,1.0) [1.0,1.0] (1.0,+Inf]
617 _testPattern("0.0#a|1.0#b|1.0<c", TRUE,
618 1.0 - 1e-9, "a",
619 1.0, "b",
620 1.0 + 1e-9, "c");
621
622 // Try an invalid pattern that isolates a single value.
623 // [-Inf,1.0) [1.0,1.0) [1.0,+Inf]
624 _testPattern("0.0#a|1.0#b|1.0#c", FALSE,
625 0, 0, 0, 0, 0, 0);
626
627 // Another
628 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf]
629 _testPattern("0.0#a|1.0<b|1.0#c", FALSE,
630 0, 0, 0, 0, 0, 0);
631 // Another
632 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf]
633 _testPattern("0.0#a|1.0<b|1.0<c", FALSE,
634 0, 0, 0, 0, 0, 0);
635
636 // Try a grossly invalid pattern.
637 // [-Inf,2.0) [2.0,1.0) [1.0,+Inf]
638 _testPattern("0.0#a|2.0#b|1.0#c", FALSE,
639 0, 0, 0, 0, 0, 0);
640 }
641
642 #endif /* #if !UCONFIG_NO_FORMATTING */