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