1 /********************************************************************
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/msgfmt.h"
15 #include "unicode/choicfmt.h"
19 // tests have obvious memory leaks!
21 void TestChoiceFormat::runIndexedTest(int32_t index
, UBool exec
,
22 const char* &name
, char* /*par*/) {
24 TESTCASE(0,TestSimpleExample
);
25 TESTCASE(1,TestComplexExample
);
26 TESTCASE(2,TestClosures
);
27 TESTCASE(3,TestPatterns
);
28 TESTCASE(4,TestChoiceFormatToPatternOverflow
);
29 default: name
= ""; break;
33 static UBool
chkstatus( UErrorCode
&status
, const char* msg
= NULL
)
35 UBool ok
= U_SUCCESS(status
);
36 if (!ok
) it_errln( msg
);
41 TestChoiceFormat::TestSimpleExample( void )
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...
49 UnicodeString res1
, res2
;
51 FieldPosition
fpos(0);
54 //for (double i = 0.0; i <= 8.0; ++i) {
55 for (ix
= 0; ix
<= 8; ++ix
) {
57 status
= U_ZERO_ERROR
;
60 res1
= form
->format(i
, str
, fpos
, status
);
61 if (!chkstatus( status
, "*** test_simple_example format" )) {
65 //form->parse(res1, f, parse_pos);
67 it_logln(UnicodeString("") + ix
+ UnicodeString(" -> ") + res1
+ UnicodeString(" -> ") + res2
);
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");
77 if(!(*form
== *formequal
)){
78 errln("ERROR: ==operator failed\n");
85 const double *gotLimits
=form
->getLimits(count
);
86 #if 1 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
87 if(count
!= 0 || gotLimits
!= NULL
) {
88 errln("getLimits() returns something, should be disabled");
90 const UnicodeString
*gotFormats
=form
->getFormats(count
);
91 if(count
!= 0 || gotFormats
!= NULL
) {
92 errln("getFormats() returns something, should be disabled");
94 const UBool
*gotClosures
=form
->getClosures(count
);
95 if(count
!= 0 || gotClosures
!= NULL
) {
96 errln("getClosures() returns something, should be disabled");
100 errln("getLimits didn't update the count correctly\n");
102 for(ix
=0; ix
<count
; ix
++){
103 if(gotLimits
[ix
] != limits
[ix
]){
104 errln((UnicodeString
)"getLimits didn't get the limits correctly. Expected " + limits
[ix
] + " Got " + gotLimits
[ix
]);
107 //Testing getFormats()
109 const UnicodeString
*gotFormats
=form
->getFormats(count
);
111 errln("getFormats didn't update the count correctly\n");
113 for(ix
=0; ix
<count
; ix
++){
114 if(gotFormats
[ix
] != monthNames
[ix
]){
115 errln((UnicodeString
)"getFormats didn't get the Formats correctly. Expected " + monthNames
[ix
] + " Got " + gotFormats
[ix
]);
124 TestChoiceFormat::TestComplexExample( void )
126 UErrorCode status
= U_ZERO_ERROR
;
127 const double filelimits
[] = {-1, 0,1,2};
128 const UnicodeString filepart
[] = {"are corrupted files", "are no files","is one file","are {2} files"};
130 ChoiceFormat
* fileform
= new ChoiceFormat( filelimits
, filepart
, 4);
133 it_errln("*** test_complex_example fileform");
137 Format
* filenumform
= NumberFormat::createInstance( status
);
139 dataerrln((UnicodeString
)"*** test_complex_example filenumform - " + u_errorName(status
));
143 if (!chkstatus( status
, "*** test_simple_example filenumform" )) {
149 //const Format* testFormats[] = { fileform, NULL, filenumform };
150 //pattform->setFormats( testFormats, 3 );
152 MessageFormat
* pattform
= new MessageFormat("There {0} on {1}", status
);
154 it_errln("*** test_complex_example pattform");
159 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
166 pattform
->setFormat( 0, *fileform
);
167 pattform
->setFormat( 2, *filenumform
);
170 Formattable testArgs
[] = {(int32_t)0, "Disk_A", (int32_t)0};
172 UnicodeString res1
, res2
;
173 pattform
->toPattern( res1
);
174 it_logln("MessageFormat toPattern: " + res1
);
175 fileform
->toPattern( res1
);
176 it_logln("ChoiceFormat toPattern: " + res1
);
177 if (res1
== "-1#are corrupted files|0#are no files|1#is one file|2#are {2} files") {
178 it_logln("toPattern tested!");
180 it_errln("*** ChoiceFormat to Pattern result!");
183 FieldPosition
fpos(0);
185 UnicodeString checkstr
[] = {
186 "There are corrupted files on Disk_A",
187 "There are no files on Disk_A",
188 "There is one file on Disk_A",
189 "There are 2 files on Disk_A",
190 "There are 3 files on Disk_A"
193 // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a bad bail out here!
195 if (U_FAILURE(status
)) {
205 for (i
= start
; i
< 4; ++i
) {
207 status
= U_ZERO_ERROR
;
208 testArgs
[0] = Formattable((int32_t)i
);
209 testArgs
[2] = testArgs
[0];
210 res2
= pattform
->format(testArgs
, 3, str
, fpos
, status
);
211 if (!chkstatus( status
, "*** test_complex_example format" )) {
217 it_logln(i
+ UnicodeString(" -> ") + res2
);
218 if (res2
!= checkstr
[i
- start
]) {
219 it_errln("*** test_complex_example res string");
220 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr
[i
] + UnicodeString("' ! "));
225 it_logln("------ additional testing in complex test ------");
228 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
230 const double* retLimits
= fileform
->getLimits( retCount
);
231 if ((retCount
== 4) && (retLimits
)
232 && (retLimits
[0] == -1.0)
233 && (retLimits
[1] == 0.0)
234 && (retLimits
[2] == 1.0)
235 && (retLimits
[3] == 2.0)) {
236 it_logln("getLimits tested!");
238 it_errln("*** getLimits unexpected result!");
241 const UnicodeString
* retFormats
= fileform
->getFormats( retCount
);
242 if ((retCount
== 4) && (retFormats
)
243 && (retFormats
[0] == "are corrupted files")
244 && (retFormats
[1] == "are no files")
245 && (retFormats
[2] == "is one file")
246 && (retFormats
[3] == "are {2} files")) {
247 it_logln("getFormats tested!");
249 it_errln("*** getFormats unexpected result!");
253 UnicodeString checkstr2
[] = {
254 "There is no folder on Disk_A",
255 "There is one folder on Disk_A",
256 "There are many folders on Disk_A",
257 "There are many folders on Disk_A"
260 fileform
->applyPattern("0#is no folder|1#is one folder|2#are many folders", status
);
261 if (status
== U_ZERO_ERROR
)
262 it_logln("status applyPattern OK!");
263 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
269 pattform
->setFormat( 0, *fileform
);
271 for (i
= 0; i
< 4; ++i
) {
273 status
= U_ZERO_ERROR
;
274 testArgs
[0] = Formattable((int32_t)i
);
275 testArgs
[2] = testArgs
[0];
276 res2
= pattform
->format(testArgs
, 3, str
, fpos
, status
);
277 if (!chkstatus( status
, "*** test_complex_example format 2" )) {
283 it_logln(UnicodeString() + i
+ UnicodeString(" -> ") + res2
);
284 if (res2
!= checkstr2
[i
]) {
285 it_errln("*** test_complex_example res string");
286 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr2
[i
] + UnicodeString("' ! "));
290 const double limits_A
[] = {1,2,3,4,5,6,7};
291 const UnicodeString monthNames_A
[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
292 ChoiceFormat
* form_A
= new ChoiceFormat(limits_A
, monthNames_A
, 7);
293 ChoiceFormat
* form_A2
= new ChoiceFormat(limits_A
, monthNames_A
, 7);
294 const double limits_B
[] = {1,2,3,4,5,6,7};
295 const UnicodeString monthNames_B
[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat_BBB"};
296 ChoiceFormat
* form_B
= new ChoiceFormat(limits_B
, monthNames_B
, 7);
297 if (!form_A
|| !form_B
|| !form_A2
) {
298 it_errln("*** test-choiceFormat not allocatable!");
300 if (*form_A
== *form_A2
) {
301 it_logln("operator== tested.");
303 it_errln("*** operator==");
306 if (*form_A
!= *form_B
) {
307 it_logln("operator!= tested.");
309 it_errln("*** operator!=");
312 ChoiceFormat
* form_A3
= (ChoiceFormat
*) form_A
->clone();
314 it_errln("*** ChoiceFormat->clone is nil.");
316 if ((*form_A3
== *form_A
) && (*form_A3
!= *form_B
)) {
317 it_logln("method clone tested.");
319 it_errln("*** ChoiceFormat clone or operator==, or operator!= .");
323 ChoiceFormat
form_Assigned( *form_A
);
324 UBool ok
= (form_Assigned
== *form_A
) && (form_Assigned
!= *form_B
);
325 form_Assigned
= *form_B
;
326 ok
= ok
&& (form_Assigned
!= *form_A
) && (form_Assigned
== *form_B
);
328 it_logln("copy constructor and operator= tested.");
330 it_errln("*** copy constructor or operator= or operator == or operator != .");
336 delete form_A
; delete form_A2
; delete form_B
;
338 const char* testPattern
= "0#none|1#one|2#many";
339 ChoiceFormat
form_pat( testPattern
, status
);
340 if (!chkstatus( status
, "*** ChoiceFormat contructor( newPattern, status)" )) {
347 form_pat
.toPattern( res1
);
348 if (res1
== "0#none|1#one|2#many") {
349 it_logln("ChoiceFormat contructor( newPattern, status) tested");
351 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!");
354 double d_a2
[] = { 3.0, 4.0 };
355 UnicodeString s_a2
[] = { "third", "forth" };
357 form_pat
.setChoices( d_a2
, s_a2
, 2 );
358 form_pat
.toPattern( res1
);
359 it_logln(UnicodeString("ChoiceFormat adoptChoices toPattern: ") + res1
);
360 if (res1
== "3#third|4#forth") {
361 it_logln("ChoiceFormat adoptChoices tested");
363 it_errln("*** ChoiceFormat adoptChoices result!");
368 status
= U_ZERO_ERROR
;
369 double arg_double
= 3.0;
370 res1
= form_pat
.format( arg_double
, str
, fpos
);
371 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
372 if (res1
!= "third") it_errln("*** ChoiceFormat format (double, ...) result!");
376 status
= U_ZERO_ERROR
;
378 res1
= form_pat
.format( arg_64
, str
, fpos
);
379 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
380 if (res1
!= "third") it_errln("*** ChoiceFormat format (int64_t, ...) result!");
384 status
= U_ZERO_ERROR
;
385 int32_t arg_long
= 3;
386 res1
= form_pat
.format( arg_long
, str
, fpos
);
387 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
388 if (res1
!= "third") it_errln("*** ChoiceFormat format (int32_t, ...) result!");
390 Formattable
ft( (int32_t)3 );
393 status
= U_ZERO_ERROR
;
394 res1
= form_pat
.format( ft
, str
, fpos
, status
);
395 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
401 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
402 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!");
404 Formattable fta
[] = { (int32_t)3 };
407 status
= U_ZERO_ERROR
;
408 res1
= form_pat
.format( fta
, 1, str
, fpos
, status
);
409 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
415 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
416 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!");
418 ParsePosition parse_pos
= 0;
420 UnicodeString
parsetext("third");
421 form_pat
.parse( parsetext
, result
, parse_pos
);
422 double rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
424 it_logln("parse( ..., ParsePos ) tested.");
426 it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
429 form_pat
.parse( parsetext
, result
, status
);
430 rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
432 it_logln("parse( ..., UErrorCode ) tested.");
434 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
438 UClassID classID = ChoiceFormat::getStaticClassID();
439 if (classID == form_pat.getDynamicClassID()) {
440 it_out << "getStaticClassID and getDynamicClassID tested." << endl;
442 it_errln("*** getStaticClassID and getDynamicClassID!");
455 * Test new closure API
457 void TestChoiceFormat::TestClosures(void) {
458 // Construct open, half-open, half-open (the other way), and closed
459 // intervals. Do this both using arrays and using a pattern.
461 // 'fmt1' is created using arrays
462 UBool T
= TRUE
, F
= FALSE
;
469 double limits
[] = { 0, 1, 2, 3, 4, 5 };
470 UBool closures
[] = { F
, F
, T
, T
, F
, F
};
471 UnicodeString fmts
[] = {
472 ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5,"
474 ChoiceFormat
fmt1(limits
, closures
, fmts
, 6);
476 // 'fmt2' is created using a pattern; it should be equivalent
477 UErrorCode status
= U_ZERO_ERROR
;
478 const char* PAT
= "0#,1)|1#[1,2]|2<(2,3]|3<(3,4)|4#[4,5)|5#[5,";
479 ChoiceFormat
fmt2(PAT
, status
);
480 if (U_FAILURE(status
)) {
481 errln("FAIL: ChoiceFormat constructor failed");
485 // Check the patterns
491 errln("FAIL: " + str
+ ", expected " + PAT
);
497 errln("FAIL: fmt1 != fmt2");
500 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
503 const double *limits2
= fmt2
.getLimits(count2
);
504 const UBool
*closures2
= fmt2
.getClosures(count2
);
506 if((count2
!= 6) || !limits2
|| !closures2
) {
507 errln("FAIL: couldn't get limits or closures");
509 for(i
=0;i
<count2
;i
++) {
510 logln("#%d/%d: limit %g closed %s\n",
513 closures2
[i
] ?"T":"F");
514 if(limits2
[i
] != limits
[i
]) {
515 errln("FAIL: limit #%d = %g, should be %g\n", i
, limits2
[i
], limits
[i
]);
517 if((closures2
[i
]!=0) != (closures
[i
]!=0)) {
518 errln("FAIL: closure #%d = %s, should be %s\n", i
, closures2
[i
]?"T":"F", closures
[i
]?"T":"F");
524 // Now test both format objects
525 UnicodeString exp
[] = {
529 /* 1.0 => */ "[1,2]",
530 /* 1.5 => */ "[1,2]",
531 /* 2.0 => */ "[1,2]",
532 /* 2.5 => */ "(2,3]",
533 /* 3.0 => */ "(2,3]",
534 /* 3.5 => */ "(3,4)",
535 /* 4.0 => */ "[4,5)",
536 /* 4.5 => */ "[4,5)",
541 // Each format object should behave exactly the same
542 ChoiceFormat
* FMT
[] = { &fmt1
, &fmt2
};
543 for (int32_t pass
=0; pass
<2; ++pass
) {
545 for (int32_t ix
=-5; ix
<=55; ix
+=5) {
546 double x
= ix
/ 10.0; // -0.5 to 5.5 step +0.5
547 FMT
[pass
]->format(x
, str
);
549 logln((UnicodeString
)"Ok: " + x
+ " => " + str
);
551 errln((UnicodeString
)"FAIL: " + x
+ " => " + str
+
552 ", expected " + exp
[j
]);
561 * Helper for TestPatterns()
563 void TestChoiceFormat::_testPattern(const char* pattern
,
565 double v1
, const char* str1
,
566 double v2
, const char* str2
,
567 double v3
, const char* str3
) {
568 UErrorCode ec
= U_ZERO_ERROR
;
569 ChoiceFormat
fmt(pattern
, ec
);
572 logln((UnicodeString
)"Ok: " + pattern
+ " failed");
574 logln((UnicodeString
)"FAIL: " + pattern
+ " accepted");
579 errln((UnicodeString
)"FAIL: ChoiceFormat(" + pattern
+ ") failed");
582 logln((UnicodeString
)"Ok: Pattern: " + pattern
);
585 logln((UnicodeString
)" toPattern: " + fmt
.toPattern(out
));
587 double v
[] = {v1
, v2
, v3
};
588 const char* str
[] = {str1
, str2
, str3
};
589 for (int32_t i
=0; i
<3; ++i
) {
591 fmt
.format(v
[i
], out
);
593 logln((UnicodeString
)"Ok: " + v
[i
] + " => " + out
);
595 errln((UnicodeString
)"FAIL: " + v
[i
] + " => " + out
+
596 ", expected " + str
[i
]);
604 void TestChoiceFormat::TestPatterns(void) {
605 // Try a pattern that isolates a single value. Create
606 // three ranges: [-Inf,1.0) [1.0,1.0] (1.0,+Inf]
607 _testPattern("0.0#a|1.0#b|1.0<c", TRUE
,
612 #if 0 // ICU 4.8 only checks the pattern syntax, not whether the ranges make sense.
613 // Try an invalid pattern that isolates a single value.
614 // [-Inf,1.0) [1.0,1.0) [1.0,+Inf]
615 _testPattern("0.0#a|1.0#b|1.0#c", FALSE
,
619 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf]
620 _testPattern("0.0#a|1.0<b|1.0#c", FALSE
,
623 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf]
624 _testPattern("0.0#a|1.0<b|1.0<c", FALSE
,
627 // Try a grossly invalid pattern.
628 // [-Inf,2.0) [2.0,1.0) [1.0,+Inf]
629 _testPattern("0.0#a|2.0#b|1.0#c", FALSE
,
634 void TestChoiceFormat::TestChoiceFormatToPatternOverflow()
636 static const double limits
[] = {0.1e-78, 1e13
, 0.1e78
};
637 UnicodeString monthNames
[] = { "one", "two", "three" };
638 ChoiceFormat
fmt(limits
, monthNames
, UPRV_LENGTHOF(limits
));
639 UnicodeString patStr
, expectedPattern1("1e-79#one|10000000000000#two|1e+77#three"),
640 expectedPattern2("1e-079#one|10000000000000#two|1e+077#three");
641 fmt
.toPattern(patStr
);
642 if (patStr
!= expectedPattern1
&& patStr
!= expectedPattern2
) {
643 errln("ChoiceFormat returned \"" + patStr
+ "\" instead of \"" + expectedPattern1
+ " or " + expectedPattern2
+ "\"");
647 #endif /* #if !UCONFIG_NO_FORMATTING */