2 /********************************************************************
4 * Copyright (c) 1997-2011, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 ********************************************************************/
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
15 #include "unicode/msgfmt.h"
16 #include "unicode/choicfmt.h"
20 // tests have obvious memory leaks!
22 void TestChoiceFormat::runIndexedTest(int32_t index
, UBool exec
,
23 const char* &name
, char* /*par*/) {
25 TESTCASE(0,TestSimpleExample
);
26 TESTCASE(1,TestComplexExample
);
27 TESTCASE(2,TestClosures
);
28 TESTCASE(3,TestPatterns
);
29 TESTCASE(4,TestChoiceFormatToPatternOverflow
);
30 default: name
= ""; break;
34 static UBool
chkstatus( UErrorCode
&status
, const char* msg
= NULL
)
36 UBool ok
= U_SUCCESS(status
);
37 if (!ok
) it_errln( msg
);
42 TestChoiceFormat::TestSimpleExample( void )
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...
50 UnicodeString res1
, res2
;
52 FieldPosition
fpos(0);
55 //for (double i = 0.0; i <= 8.0; ++i) {
56 for (ix
= 0; ix
<= 8; ++ix
) {
58 status
= U_ZERO_ERROR
;
61 res1
= form
->format(i
, str
, fpos
, status
);
62 if (!chkstatus( status
, "*** test_simple_example format" )) {
66 //form->parse(res1, f, parse_pos);
68 it_logln(UnicodeString("") + ix
+ UnicodeString(" -> ") + res1
+ UnicodeString(" -> ") + res2
);
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");
78 if(!(*form
== *formequal
)){
79 errln("ERROR: ==operator failed\n");
86 const double *gotLimits
=form
->getLimits(count
);
87 #if 1 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
88 if(count
!= 0 || gotLimits
!= NULL
) {
89 errln("getLimits() returns something, should be disabled");
91 const UnicodeString
*gotFormats
=form
->getFormats(count
);
92 if(count
!= 0 || gotFormats
!= NULL
) {
93 errln("getFormats() returns something, should be disabled");
95 const UBool
*gotClosures
=form
->getClosures(count
);
96 if(count
!= 0 || gotClosures
!= NULL
) {
97 errln("getClosures() returns something, should be disabled");
101 errln("getLimits didn't update the count correctly\n");
103 for(ix
=0; ix
<count
; ix
++){
104 if(gotLimits
[ix
] != limits
[ix
]){
105 errln((UnicodeString
)"getLimits didn't get the limits correctly. Expected " + limits
[ix
] + " Got " + gotLimits
[ix
]);
108 //Testing getFormats()
110 const UnicodeString
*gotFormats
=form
->getFormats(count
);
112 errln("getFormats didn't update the count correctly\n");
114 for(ix
=0; ix
<count
; ix
++){
115 if(gotFormats
[ix
] != monthNames
[ix
]){
116 errln((UnicodeString
)"getFormats didn't get the Formats correctly. Expected " + monthNames
[ix
] + " Got " + gotFormats
[ix
]);
125 TestChoiceFormat::TestComplexExample( void )
127 UErrorCode status
= U_ZERO_ERROR
;
128 const double filelimits
[] = {-1, 0,1,2};
129 const UnicodeString filepart
[] = {"are corrupted files", "are no files","is one file","are {2} files"};
131 ChoiceFormat
* fileform
= new ChoiceFormat( filelimits
, filepart
, 4);
134 it_errln("*** test_complex_example fileform");
138 Format
* filenumform
= NumberFormat::createInstance( status
);
140 dataerrln((UnicodeString
)"*** test_complex_example filenumform - " + u_errorName(status
));
144 if (!chkstatus( status
, "*** test_simple_example filenumform" )) {
150 //const Format* testFormats[] = { fileform, NULL, filenumform };
151 //pattform->setFormats( testFormats, 3 );
153 MessageFormat
* pattform
= new MessageFormat("There {0} on {1}", status
);
155 it_errln("*** test_complex_example pattform");
160 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
167 pattform
->setFormat( 0, *fileform
);
168 pattform
->setFormat( 2, *filenumform
);
171 Formattable testArgs
[] = {(int32_t)0, "Disk_A", (int32_t)0};
173 UnicodeString res1
, res2
;
174 pattform
->toPattern( res1
);
175 it_logln("MessageFormat toPattern: " + res1
);
176 fileform
->toPattern( res1
);
177 it_logln("ChoiceFormat toPattern: " + res1
);
178 if (res1
== "-1#are corrupted files|0#are no files|1#is one file|2#are {2} files") {
179 it_logln("toPattern tested!");
181 it_errln("*** ChoiceFormat to Pattern result!");
184 FieldPosition
fpos(0);
186 UnicodeString checkstr
[] = {
187 "There are corrupted files on Disk_A",
188 "There are no files on Disk_A",
189 "There is one file on Disk_A",
190 "There are 2 files on Disk_A",
191 "There are 3 files on Disk_A"
194 // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a bad bail out here!
196 if (U_FAILURE(status
)) {
206 for (i
= start
; i
< 4; ++i
) {
208 status
= U_ZERO_ERROR
;
209 testArgs
[0] = Formattable((int32_t)i
);
210 testArgs
[2] = testArgs
[0];
211 res2
= pattform
->format(testArgs
, 3, str
, fpos
, status
);
212 if (!chkstatus( status
, "*** test_complex_example format" )) {
218 it_logln(i
+ UnicodeString(" -> ") + res2
);
219 if (res2
!= checkstr
[i
- start
]) {
220 it_errln("*** test_complex_example res string");
221 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr
[i
] + UnicodeString("' ! "));
226 it_logln("------ additional testing in complex test ------");
229 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
231 const double* retLimits
= fileform
->getLimits( retCount
);
232 if ((retCount
== 4) && (retLimits
)
233 && (retLimits
[0] == -1.0)
234 && (retLimits
[1] == 0.0)
235 && (retLimits
[2] == 1.0)
236 && (retLimits
[3] == 2.0)) {
237 it_logln("getLimits tested!");
239 it_errln("*** getLimits unexpected result!");
242 const UnicodeString
* retFormats
= fileform
->getFormats( retCount
);
243 if ((retCount
== 4) && (retFormats
)
244 && (retFormats
[0] == "are corrupted files")
245 && (retFormats
[1] == "are no files")
246 && (retFormats
[2] == "is one file")
247 && (retFormats
[3] == "are {2} files")) {
248 it_logln("getFormats tested!");
250 it_errln("*** getFormats unexpected result!");
254 UnicodeString checkstr2
[] = {
255 "There is no folder on Disk_A",
256 "There is one folder on Disk_A",
257 "There are many folders on Disk_A",
258 "There are many folders on Disk_A"
261 fileform
->applyPattern("0#is no folder|1#is one folder|2#are many folders", status
);
262 if (status
== U_ZERO_ERROR
)
263 it_logln("status applyPattern OK!");
264 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
270 pattform
->setFormat( 0, *fileform
);
272 for (i
= 0; i
< 4; ++i
) {
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" )) {
284 it_logln(UnicodeString() + i
+ UnicodeString(" -> ") + res2
);
285 if (res2
!= checkstr2
[i
]) {
286 it_errln("*** test_complex_example res string");
287 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr2
[i
] + UnicodeString("' ! "));
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!");
301 if (*form_A
== *form_A2
) {
302 it_logln("operator== tested.");
304 it_errln("*** operator==");
307 if (*form_A
!= *form_B
) {
308 it_logln("operator!= tested.");
310 it_errln("*** operator!=");
313 ChoiceFormat
* form_A3
= (ChoiceFormat
*) form_A
->clone();
315 it_errln("*** ChoiceFormat->clone is nil.");
317 if ((*form_A3
== *form_A
) && (*form_A3
!= *form_B
)) {
318 it_logln("method clone tested.");
320 it_errln("*** ChoiceFormat clone or operator==, or operator!= .");
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
);
329 it_logln("copy constructor and operator= tested.");
331 it_errln("*** copy constructor or operator= or operator == or operator != .");
337 delete form_A
; delete form_A2
; delete form_B
;
339 const char* testPattern
= "0#none|1#one|2#many";
340 ChoiceFormat
form_pat( testPattern
, status
);
341 if (!chkstatus( status
, "*** ChoiceFormat contructor( newPattern, status)" )) {
348 form_pat
.toPattern( res1
);
349 if (res1
== "0#none|1#one|2#many") {
350 it_logln("ChoiceFormat contructor( newPattern, status) tested");
352 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!");
355 double d_a2
[] = { 3.0, 4.0 };
356 UnicodeString s_a2
[] = { "third", "forth" };
358 form_pat
.setChoices( d_a2
, s_a2
, 2 );
359 form_pat
.toPattern( res1
);
360 it_logln(UnicodeString("ChoiceFormat adoptChoices toPattern: ") + res1
);
361 if (res1
== "3#third|4#forth") {
362 it_logln("ChoiceFormat adoptChoices tested");
364 it_errln("*** ChoiceFormat adoptChoices result!");
369 status
= U_ZERO_ERROR
;
370 double arg_double
= 3.0;
371 res1
= form_pat
.format( arg_double
, str
, fpos
);
372 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
373 if (res1
!= "third") it_errln("*** ChoiceFormat format (double, ...) result!");
377 status
= U_ZERO_ERROR
;
379 res1
= form_pat
.format( arg_64
, str
, fpos
);
380 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
381 if (res1
!= "third") it_errln("*** ChoiceFormat format (int64_t, ...) result!");
385 status
= U_ZERO_ERROR
;
386 int32_t arg_long
= 3;
387 res1
= form_pat
.format( arg_long
, str
, fpos
);
388 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
389 if (res1
!= "third") it_errln("*** ChoiceFormat format (int32_t, ...) result!");
391 Formattable
ft( (int32_t)3 );
394 status
= U_ZERO_ERROR
;
395 res1
= form_pat
.format( ft
, str
, fpos
, status
);
396 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
402 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
403 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!");
405 Formattable fta
[] = { (int32_t)3 };
408 status
= U_ZERO_ERROR
;
409 res1
= form_pat
.format( fta
, 1, str
, fpos
, status
);
410 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
416 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
417 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!");
419 ParsePosition parse_pos
= 0;
421 UnicodeString
parsetext("third");
422 form_pat
.parse( parsetext
, result
, parse_pos
);
423 double rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
425 it_logln("parse( ..., ParsePos ) tested.");
427 it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
430 form_pat
.parse( parsetext
, result
, status
);
431 rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
433 it_logln("parse( ..., UErrorCode ) tested.");
435 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
439 UClassID classID = ChoiceFormat::getStaticClassID();
440 if (classID == form_pat.getDynamicClassID()) {
441 it_out << "getStaticClassID and getDynamicClassID tested." << endl;
443 it_errln("*** getStaticClassID and getDynamicClassID!");
456 * Test new closure API
458 void TestChoiceFormat::TestClosures(void) {
459 // Construct open, half-open, half-open (the other way), and closed
460 // intervals. Do this both using arrays and using a pattern.
462 // 'fmt1' is created using arrays
463 UBool T
= TRUE
, F
= FALSE
;
470 double limits
[] = { 0, 1, 2, 3, 4, 5 };
471 UBool closures
[] = { F
, F
, T
, T
, F
, F
};
472 UnicodeString fmts
[] = {
473 ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5,"
475 ChoiceFormat
fmt1(limits
, closures
, fmts
, 6);
477 // 'fmt2' is created using a pattern; it should be equivalent
478 UErrorCode status
= U_ZERO_ERROR
;
479 const char* PAT
= "0#,1)|1#[1,2]|2<(2,3]|3<(3,4)|4#[4,5)|5#[5,";
480 ChoiceFormat
fmt2(PAT
, status
);
481 if (U_FAILURE(status
)) {
482 errln("FAIL: ChoiceFormat constructor failed");
486 // Check the patterns
492 errln("FAIL: " + str
+ ", expected " + PAT
);
498 errln("FAIL: fmt1 != fmt2");
501 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
504 const double *limits2
= fmt2
.getLimits(count2
);
505 const UBool
*closures2
= fmt2
.getClosures(count2
);
507 if((count2
!= 6) || !limits2
|| !closures2
) {
508 errln("FAIL: couldn't get limits or closures");
510 for(i
=0;i
<count2
;i
++) {
511 logln("#%d/%d: limit %g closed %s\n",
514 closures2
[i
] ?"T":"F");
515 if(limits2
[i
] != limits
[i
]) {
516 errln("FAIL: limit #%d = %g, should be %g\n", i
, limits2
[i
], limits
[i
]);
518 if((closures2
[i
]!=0) != (closures
[i
]!=0)) {
519 errln("FAIL: closure #%d = %s, should be %s\n", i
, closures2
[i
]?"T":"F", closures
[i
]?"T":"F");
525 // Now test both format objects
526 UnicodeString exp
[] = {
530 /* 1.0 => */ "[1,2]",
531 /* 1.5 => */ "[1,2]",
532 /* 2.0 => */ "[1,2]",
533 /* 2.5 => */ "(2,3]",
534 /* 3.0 => */ "(2,3]",
535 /* 3.5 => */ "(3,4)",
536 /* 4.0 => */ "[4,5)",
537 /* 4.5 => */ "[4,5)",
542 // Each format object should behave exactly the same
543 ChoiceFormat
* FMT
[] = { &fmt1
, &fmt2
};
544 for (int32_t pass
=0; pass
<2; ++pass
) {
546 for (int32_t ix
=-5; ix
<=55; ix
+=5) {
547 double x
= ix
/ 10.0; // -0.5 to 5.5 step +0.5
548 FMT
[pass
]->format(x
, str
);
550 logln((UnicodeString
)"Ok: " + x
+ " => " + str
);
552 errln((UnicodeString
)"FAIL: " + x
+ " => " + str
+
553 ", expected " + exp
[j
]);
562 * Helper for TestPatterns()
564 void TestChoiceFormat::_testPattern(const char* pattern
,
566 double v1
, const char* str1
,
567 double v2
, const char* str2
,
568 double v3
, const char* str3
) {
569 UErrorCode ec
= U_ZERO_ERROR
;
570 ChoiceFormat
fmt(pattern
, ec
);
573 logln((UnicodeString
)"Ok: " + pattern
+ " failed");
575 logln((UnicodeString
)"FAIL: " + pattern
+ " accepted");
580 errln((UnicodeString
)"FAIL: ChoiceFormat(" + pattern
+ ") failed");
583 logln((UnicodeString
)"Ok: Pattern: " + pattern
);
586 logln((UnicodeString
)" toPattern: " + fmt
.toPattern(out
));
588 double v
[] = {v1
, v2
, v3
};
589 const char* str
[] = {str1
, str2
, str3
};
590 for (int32_t i
=0; i
<3; ++i
) {
592 fmt
.format(v
[i
], out
);
594 logln((UnicodeString
)"Ok: " + v
[i
] + " => " + out
);
596 errln((UnicodeString
)"FAIL: " + v
[i
] + " => " + out
+
597 ", expected " + str
[i
]);
605 void TestChoiceFormat::TestPatterns(void) {
606 // Try a pattern that isolates a single value. Create
607 // three ranges: [-Inf,1.0) [1.0,1.0] (1.0,+Inf]
608 _testPattern("0.0#a|1.0#b|1.0<c", TRUE
,
613 #if 0 // ICU 4.8 only checks the pattern syntax, not whether the ranges make sense.
614 // Try an invalid pattern that isolates a single value.
615 // [-Inf,1.0) [1.0,1.0) [1.0,+Inf]
616 _testPattern("0.0#a|1.0#b|1.0#c", FALSE
,
620 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf]
621 _testPattern("0.0#a|1.0<b|1.0#c", FALSE
,
624 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf]
625 _testPattern("0.0#a|1.0<b|1.0<c", FALSE
,
628 // Try a grossly invalid pattern.
629 // [-Inf,2.0) [2.0,1.0) [1.0,+Inf]
630 _testPattern("0.0#a|2.0#b|1.0#c", FALSE
,
635 void TestChoiceFormat::TestChoiceFormatToPatternOverflow()
637 static const double limits
[] = {0.1e-78, 1e13
, 0.1e78
};
638 UnicodeString monthNames
[] = { "one", "two", "three" };
639 ChoiceFormat
fmt(limits
, monthNames
, sizeof(limits
)/sizeof(limits
[0]));
640 UnicodeString patStr
, expectedPattern1("1e-79#one|10000000000000#two|1e+77#three"),
641 expectedPattern2("1e-079#one|10000000000000#two|1e+077#three");
642 fmt
.toPattern(patStr
);
643 if (patStr
!= expectedPattern1
&& patStr
!= expectedPattern2
) {
644 errln("ChoiceFormat returned \"" + patStr
+ "\" instead of \"" + expectedPattern1
+ " or " + expectedPattern2
+ "\"");
648 #endif /* #if !UCONFIG_NO_FORMATTING */