2 /********************************************************************
4 * Copyright (c) 1997-2009, 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");
87 gotLimits
=(double*)form
->getLimits(count
);
89 errln("getLimits didn't update the count correctly\n");
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
]);
98 UnicodeString
*gotFormats
=0;
99 gotFormats
=(UnicodeString
*)form
->getFormats(count
);
101 errln("getFormats didn't update the count correctly\n");
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
]);
115 TestChoiceFormat::TestComplexExample( void )
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"};
121 ChoiceFormat
* fileform
= new ChoiceFormat( filelimits
, filepart
, 4);
124 it_errln("*** test_complex_example fileform");
128 Format
* filenumform
= NumberFormat::createInstance( status
);
130 dataerrln((UnicodeString
)"*** test_complex_example filenumform - " + u_errorName(status
));
134 if (!chkstatus( status
, "*** test_simple_example filenumform" )) {
140 //const Format* testFormats[] = { fileform, NULL, filenumform };
141 //pattform->setFormats( testFormats, 3 );
143 MessageFormat
* pattform
= new MessageFormat("There {0} on {1}", status
);
145 it_errln("*** test_complex_example pattform");
150 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
157 pattform
->setFormat( 0, *fileform
);
158 pattform
->setFormat( 2, *filenumform
);
161 Formattable testArgs
[] = {(int32_t)0, "Disk_A", (int32_t)0};
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!");
171 it_errln("*** ChoiceFormat to Pattern result!");
174 FieldPosition
fpos(0);
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"
184 // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a bad bail out here!
186 if (U_FAILURE(status
)) {
196 for (i
= start
; i
< 4; ++i
) {
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" )) {
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("' ! "));
216 it_logln("------ additional testing in complex test ------");
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!");
228 it_errln("*** getLimits unexpected result!");
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!");
239 it_errln("*** getFormats unexpected result!");
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"
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" )) {
258 pattform
->setFormat( 0, *fileform
);
260 for (i
= 0; i
< 4; ++i
) {
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" )) {
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("' ! "));
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!");
289 if (*form_A
== *form_A2
) {
290 it_logln("operator== tested.");
292 it_errln("*** operator==");
295 if (*form_A
!= *form_B
) {
296 it_logln("operator!= tested.");
298 it_errln("*** operator!=");
301 ChoiceFormat
* form_A3
= (ChoiceFormat
*) form_A
->clone();
303 it_errln("*** ChoiceFormat->clone is nil.");
305 if ((*form_A3
== *form_A
) && (*form_A3
!= *form_B
)) {
306 it_logln("method clone tested.");
308 it_errln("*** ChoiceFormat clone or operator==, or operator!= .");
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
);
317 it_logln("copy constructor and operator= tested.");
319 it_errln("*** copy constructor or operator= or operator == or operator != .");
325 delete form_A
; delete form_A2
; delete form_B
;
327 const char* testPattern
= "0#none|1#one|2#many";
328 ChoiceFormat
form_pat( testPattern
, status
);
329 if (!chkstatus( status
, "*** ChoiceFormat contructor( newPattern, status)" )) {
336 form_pat
.toPattern( res1
);
337 if (res1
== "0#none|1#one|2#many") {
338 it_logln("ChoiceFormat contructor( newPattern, status) tested");
340 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!");
343 double d_a2
[] = { 3.0, 4.0 };
344 UnicodeString s_a2
[] = { "third", "forth" };
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");
352 it_errln("*** ChoiceFormat adoptChoices result!");
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!");
365 status
= U_ZERO_ERROR
;
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!");
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!");
379 Formattable
ft( (int32_t)3 );
382 status
= U_ZERO_ERROR
;
383 res1
= form_pat
.format( ft
, str
, fpos
, status
);
384 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
390 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
391 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!");
393 Formattable fta
[] = { (int32_t)3 };
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, ...)" )) {
404 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
405 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!");
407 ParsePosition parse_pos
= 0;
409 UnicodeString
parsetext("third");
410 form_pat
.parse( parsetext
, result
, parse_pos
);
411 double rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
413 it_logln("parse( ..., ParsePos ) tested.");
415 it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
418 form_pat
.parse( parsetext
, result
, status
);
419 rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
421 it_logln("parse( ..., UErrorCode ) tested.");
423 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
427 UClassID classID = ChoiceFormat::getStaticClassID();
428 if (classID == form_pat.getDynamicClassID()) {
429 it_out << "getStaticClassID and getDynamicClassID tested." << endl;
431 it_errln("*** getStaticClassID and getDynamicClassID!");
444 * Test new closure API
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.
450 // 'fmt1' is created using arrays
451 UBool T
= TRUE
, F
= FALSE
;
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,"
463 ChoiceFormat
fmt1(limits
, closures
, fmts
, 6);
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");
474 // Check the patterns
480 errln("FAIL: " + str
+ ", expected " + PAT
);
486 errln("FAIL: fmt1 != fmt2");
491 const double *limits2
= fmt2
.getLimits(count2
);
492 const UBool
*closures2
= fmt2
.getClosures(count2
);
494 if((count2
!= 6) || !limits2
|| !closures2
) {
495 errln("FAIL: couldn't get limits or closures");
497 for(i
=0;i
<count2
;i
++) {
498 logln("#%d/%d: limit %g closed %s\n",
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
]);
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");
511 // Now test both format objects
512 UnicodeString exp
[] = {
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)",
528 // Each format object should behave exactly the same
529 ChoiceFormat
* FMT
[] = { &fmt1
, &fmt2
};
530 for (int32_t pass
=0; pass
<2; ++pass
) {
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
);
536 logln((UnicodeString
)"Ok: " + x
+ " => " + str
);
538 errln((UnicodeString
)"FAIL: " + x
+ " => " + str
+
539 ", expected " + exp
[j
]);
548 * Helper for TestPatterns()
550 void TestChoiceFormat::_testPattern(const char* pattern
,
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
);
559 logln((UnicodeString
)"Ok: " + pattern
+ " failed");
561 logln((UnicodeString
)"FAIL: " + pattern
+ " accepted");
566 errln((UnicodeString
)"FAIL: ChoiceFormat(" + pattern
+ ") failed");
569 logln((UnicodeString
)"Ok: Pattern: " + pattern
);
572 logln((UnicodeString
)" toPattern: " + fmt
.toPattern(out
));
574 double v
[] = {v1
, v2
, v3
};
575 const char* str
[] = {str1
, str2
, str3
};
576 for (int32_t i
=0; i
<3; ++i
) {
578 fmt
.format(v
[i
], out
);
580 logln((UnicodeString
)"Ok: " + v
[i
] + " => " + out
);
582 errln((UnicodeString
)"FAIL: " + v
[i
] + " => " + out
+
583 ", expected " + str
[i
]);
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
,
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
,
605 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf]
606 _testPattern("0.0#a|1.0<b|1.0#c", FALSE
,
609 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf]
610 _testPattern("0.0#a|1.0<b|1.0<c", FALSE
,
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
,
619 void TestChoiceFormat::TestChoiceFormatToPatternOverflow()
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
+ "\"");
632 #endif /* #if !UCONFIG_NO_FORMATTING */