1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_FORMATTING
16 #include "unicode/msgfmt.h"
17 #include "unicode/choicfmt.h"
21 // tests have obvious memory leaks!
23 void TestChoiceFormat::runIndexedTest(int32_t index
, UBool exec
,
24 const char* &name
, char* /*par*/) {
26 TESTCASE(0,TestSimpleExample
);
27 TESTCASE(1,TestComplexExample
);
28 TESTCASE(2,TestClosures
);
29 TESTCASE(3,TestPatterns
);
30 TESTCASE(4,TestChoiceFormatToPatternOverflow
);
31 default: name
= ""; break;
35 static UBool
chkstatus( UErrorCode
&status
, const char* msg
= NULL
)
37 UBool ok
= U_SUCCESS(status
);
38 if (!ok
) it_errln( msg
);
43 TestChoiceFormat::TestSimpleExample( void )
45 double limits
[] = {1,2,3,4,5,6,7};
46 UnicodeString monthNames
[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
47 ChoiceFormat
* form
= new ChoiceFormat(limits
, monthNames
, 7);
48 ParsePosition parse_pos
;
49 // TODO Fix this ParsePosition stuff...
51 UnicodeString res1
, res2
;
53 FieldPosition
fpos(FieldPosition::DONT_CARE
);
56 //for (double i = 0.0; i <= 8.0; ++i) {
57 for (ix
= 0; ix
<= 8; ++ix
) {
59 status
= U_ZERO_ERROR
;
62 res1
= form
->format(i
, str
, fpos
, status
);
63 if (!chkstatus( status
, "*** test_simple_example format" )) {
67 //form->parse(res1, f, parse_pos);
69 it_logln(UnicodeString("") + ix
+ UnicodeString(" -> ") + res1
+ UnicodeString(" -> ") + res2
);
72 const double filelimits
[] = {0,1,2};
73 const UnicodeString filepart
[] = {"are no files","is one file","are {2} files"};
74 ChoiceFormat
* formnew
=new ChoiceFormat(filelimits
, filepart
, 3);
75 ChoiceFormat
* formequal
=new ChoiceFormat(limits
, monthNames
, 7);
76 if(*formnew
== *form
){
77 errln("ERROR: ==operator failed\n");
79 if(!(*form
== *formequal
)){
80 errln("ERROR: ==operator failed\n");
87 const double *gotLimits
=form
->getLimits(count
);
88 #if 1 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
89 if(count
!= 0 || gotLimits
!= NULL
) {
90 errln("getLimits() returns something, should be disabled");
92 const UnicodeString
*gotFormats
=form
->getFormats(count
);
93 if(count
!= 0 || gotFormats
!= NULL
) {
94 errln("getFormats() returns something, should be disabled");
96 const UBool
*gotClosures
=form
->getClosures(count
);
97 if(count
!= 0 || gotClosures
!= NULL
) {
98 errln("getClosures() returns something, should be disabled");
102 errln("getLimits didn't update the count correctly\n");
104 for(ix
=0; ix
<count
; ix
++){
105 if(gotLimits
[ix
] != limits
[ix
]){
106 errln((UnicodeString
)"getLimits didn't get the limits correctly. Expected " + limits
[ix
] + " Got " + gotLimits
[ix
]);
109 //Testing getFormats()
111 const UnicodeString
*gotFormats
=form
->getFormats(count
);
113 errln("getFormats didn't update the count correctly\n");
115 for(ix
=0; ix
<count
; ix
++){
116 if(gotFormats
[ix
] != monthNames
[ix
]){
117 errln((UnicodeString
)"getFormats didn't get the Formats correctly. Expected " + monthNames
[ix
] + " Got " + gotFormats
[ix
]);
126 TestChoiceFormat::TestComplexExample( void )
128 UErrorCode status
= U_ZERO_ERROR
;
129 const double filelimits
[] = {-1, 0,1,2};
130 const UnicodeString filepart
[] = {"are corrupted files", "are no files","is one file","are {2} files"};
132 ChoiceFormat
* fileform
= new ChoiceFormat( filelimits
, filepart
, 4);
135 it_errln("*** test_complex_example fileform");
139 Format
* filenumform
= NumberFormat::createInstance( status
);
141 dataerrln((UnicodeString
)"*** test_complex_example filenumform - " + u_errorName(status
));
145 if (!chkstatus( status
, "*** test_simple_example filenumform" )) {
151 //const Format* testFormats[] = { fileform, NULL, filenumform };
152 //pattform->setFormats( testFormats, 3 );
154 MessageFormat
* pattform
= new MessageFormat("There {0} on {1}", status
);
156 it_errln("*** test_complex_example pattform");
161 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
168 pattform
->setFormat( 0, *fileform
);
169 pattform
->setFormat( 2, *filenumform
);
172 Formattable testArgs
[] = {(int32_t)0, "Disk_A", (int32_t)0};
174 UnicodeString res1
, res2
;
175 pattform
->toPattern( res1
);
176 it_logln("MessageFormat toPattern: " + res1
);
177 fileform
->toPattern( res1
);
178 it_logln("ChoiceFormat toPattern: " + res1
);
179 if (res1
== "-1#are corrupted files|0#are no files|1#is one file|2#are {2} files") {
180 it_logln("toPattern tested!");
182 it_errln("*** ChoiceFormat to Pattern result!");
185 FieldPosition
fpos(FieldPosition::DONT_CARE
);
187 UnicodeString checkstr
[] = {
188 "There are corrupted files on Disk_A",
189 "There are no files on Disk_A",
190 "There is one file on Disk_A",
191 "There are 2 files on Disk_A",
192 "There are 3 files on Disk_A"
195 // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a bad bail out here!
197 if (U_FAILURE(status
)) {
207 for (i
= start
; i
< 4; ++i
) {
209 status
= U_ZERO_ERROR
;
210 testArgs
[0] = Formattable((int32_t)i
);
211 testArgs
[2] = testArgs
[0];
212 res2
= pattform
->format(testArgs
, 3, str
, fpos
, status
);
213 if (!chkstatus( status
, "*** test_complex_example format" )) {
219 it_logln(i
+ UnicodeString(" -> ") + res2
);
220 if (res2
!= checkstr
[i
- start
]) {
221 it_errln("*** test_complex_example res string");
222 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr
[i
] + UnicodeString("' ! "));
227 it_logln("------ additional testing in complex test ------");
230 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
232 const double* retLimits
= fileform
->getLimits( retCount
);
233 if ((retCount
== 4) && (retLimits
)
234 && (retLimits
[0] == -1.0)
235 && (retLimits
[1] == 0.0)
236 && (retLimits
[2] == 1.0)
237 && (retLimits
[3] == 2.0)) {
238 it_logln("getLimits tested!");
240 it_errln("*** getLimits unexpected result!");
243 const UnicodeString
* retFormats
= fileform
->getFormats( retCount
);
244 if ((retCount
== 4) && (retFormats
)
245 && (retFormats
[0] == "are corrupted files")
246 && (retFormats
[1] == "are no files")
247 && (retFormats
[2] == "is one file")
248 && (retFormats
[3] == "are {2} files")) {
249 it_logln("getFormats tested!");
251 it_errln("*** getFormats unexpected result!");
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"
262 fileform
->applyPattern("0#is no folder|1#is one folder|2#are many folders", status
);
263 if (status
== U_ZERO_ERROR
)
264 it_logln("status applyPattern OK!");
265 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
271 pattform
->setFormat( 0, *fileform
);
273 for (i
= 0; i
< 4; ++i
) {
275 status
= U_ZERO_ERROR
;
276 testArgs
[0] = Formattable((int32_t)i
);
277 testArgs
[2] = testArgs
[0];
278 res2
= pattform
->format(testArgs
, 3, str
, fpos
, status
);
279 if (!chkstatus( status
, "*** test_complex_example format 2" )) {
285 it_logln(UnicodeString() + i
+ UnicodeString(" -> ") + res2
);
286 if (res2
!= checkstr2
[i
]) {
287 it_errln("*** test_complex_example res string");
288 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr2
[i
] + UnicodeString("' ! "));
292 const double limits_A
[] = {1,2,3,4,5,6,7};
293 const UnicodeString monthNames_A
[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
294 ChoiceFormat
* form_A
= new ChoiceFormat(limits_A
, monthNames_A
, 7);
295 ChoiceFormat
* form_A2
= new ChoiceFormat(limits_A
, monthNames_A
, 7);
296 const double limits_B
[] = {1,2,3,4,5,6,7};
297 const UnicodeString monthNames_B
[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat_BBB"};
298 ChoiceFormat
* form_B
= new ChoiceFormat(limits_B
, monthNames_B
, 7);
299 if (!form_A
|| !form_B
|| !form_A2
) {
300 it_errln("*** test-choiceFormat not allocatable!");
302 if (*form_A
== *form_A2
) {
303 it_logln("operator== tested.");
305 it_errln("*** operator==");
308 if (*form_A
!= *form_B
) {
309 it_logln("operator!= tested.");
311 it_errln("*** operator!=");
314 ChoiceFormat
* form_A3
= (ChoiceFormat
*) form_A
->clone();
316 it_errln("*** ChoiceFormat->clone is nil.");
318 if ((*form_A3
== *form_A
) && (*form_A3
!= *form_B
)) {
319 it_logln("method clone tested.");
321 it_errln("*** ChoiceFormat clone or operator==, or operator!= .");
325 ChoiceFormat
form_Assigned( *form_A
);
326 UBool ok
= (form_Assigned
== *form_A
) && (form_Assigned
!= *form_B
);
327 form_Assigned
= *form_B
;
328 ok
= ok
&& (form_Assigned
!= *form_A
) && (form_Assigned
== *form_B
);
330 it_logln("copy constructor and operator= tested.");
332 it_errln("*** copy constructor or operator= or operator == or operator != .");
338 delete form_A
; delete form_A2
; delete form_B
;
340 const char* testPattern
= "0#none|1#one|2#many";
341 ChoiceFormat
form_pat( testPattern
, status
);
342 if (!chkstatus( status
, "*** ChoiceFormat contructor( newPattern, status)" )) {
349 form_pat
.toPattern( res1
);
350 if (res1
== "0#none|1#one|2#many") {
351 it_logln("ChoiceFormat contructor( newPattern, status) tested");
353 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!");
356 double d_a2
[] = { 3.0, 4.0 };
357 UnicodeString s_a2
[] = { "third", "forth" };
359 form_pat
.setChoices( d_a2
, s_a2
, 2 );
360 form_pat
.toPattern( res1
);
361 it_logln(UnicodeString("ChoiceFormat adoptChoices toPattern: ") + res1
);
362 if (res1
== "3#third|4#forth") {
363 it_logln("ChoiceFormat adoptChoices tested");
365 it_errln("*** ChoiceFormat adoptChoices result!");
370 status
= U_ZERO_ERROR
;
371 double arg_double
= 3.0;
372 res1
= form_pat
.format( arg_double
, str
, fpos
);
373 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
374 if (res1
!= "third") it_errln("*** ChoiceFormat format (double, ...) result!");
378 status
= U_ZERO_ERROR
;
380 res1
= form_pat
.format( arg_64
, str
, fpos
);
381 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
382 if (res1
!= "third") it_errln("*** ChoiceFormat format (int64_t, ...) result!");
386 status
= U_ZERO_ERROR
;
387 int32_t arg_long
= 3;
388 res1
= form_pat
.format( arg_long
, str
, fpos
);
389 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
390 if (res1
!= "third") it_errln("*** ChoiceFormat format (int32_t, ...) result!");
392 Formattable
ft( (int32_t)3 );
395 status
= U_ZERO_ERROR
;
396 res1
= form_pat
.format( ft
, str
, fpos
, status
);
397 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
403 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
404 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!");
406 Formattable fta
[] = { (int32_t)3 };
409 status
= U_ZERO_ERROR
;
410 res1
= form_pat
.format( fta
, 1, str
, fpos
, status
);
411 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
417 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
418 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!");
420 ParsePosition parse_pos
= 0;
422 UnicodeString
parsetext("third");
423 form_pat
.parse( parsetext
, result
, parse_pos
);
424 double rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
426 it_logln("parse( ..., ParsePos ) tested.");
428 it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
431 form_pat
.parse( parsetext
, result
, status
);
432 rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
434 it_logln("parse( ..., UErrorCode ) tested.");
436 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
440 UClassID classID = ChoiceFormat::getStaticClassID();
441 if (classID == form_pat.getDynamicClassID()) {
442 it_out << "getStaticClassID and getDynamicClassID tested." << endl;
444 it_errln("*** getStaticClassID and getDynamicClassID!");
457 * Test new closure API
459 void TestChoiceFormat::TestClosures(void) {
460 // Construct open, half-open, half-open (the other way), and closed
461 // intervals. Do this both using arrays and using a pattern.
463 // 'fmt1' is created using arrays
464 UBool T
= TRUE
, F
= FALSE
;
471 double limits
[] = { 0, 1, 2, 3, 4, 5 };
472 UBool closures
[] = { F
, F
, T
, T
, F
, F
};
473 UnicodeString fmts
[] = {
474 ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5,"
476 ChoiceFormat
fmt1(limits
, closures
, fmts
, 6);
478 // 'fmt2' is created using a pattern; it should be equivalent
479 UErrorCode status
= U_ZERO_ERROR
;
480 const char* PAT
= "0#,1)|1#[1,2]|2<(2,3]|3<(3,4)|4#[4,5)|5#[5,";
481 ChoiceFormat
fmt2(PAT
, status
);
482 if (U_FAILURE(status
)) {
483 errln("FAIL: ChoiceFormat constructor failed");
487 // Check the patterns
493 errln("FAIL: " + str
+ ", expected " + PAT
);
499 errln("FAIL: fmt1 != fmt2");
502 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters.
505 const double *limits2
= fmt2
.getLimits(count2
);
506 const UBool
*closures2
= fmt2
.getClosures(count2
);
508 if((count2
!= 6) || !limits2
|| !closures2
) {
509 errln("FAIL: couldn't get limits or closures");
511 for(i
=0;i
<count2
;i
++) {
512 logln("#%d/%d: limit %g closed %s\n",
515 closures2
[i
] ?"T":"F");
516 if(limits2
[i
] != limits
[i
]) {
517 errln("FAIL: limit #%d = %g, should be %g\n", i
, limits2
[i
], limits
[i
]);
519 if((closures2
[i
]!=0) != (closures
[i
]!=0)) {
520 errln("FAIL: closure #%d = %s, should be %s\n", i
, closures2
[i
]?"T":"F", closures
[i
]?"T":"F");
526 // Now test both format objects
527 UnicodeString exp
[] = {
531 /* 1.0 => */ "[1,2]",
532 /* 1.5 => */ "[1,2]",
533 /* 2.0 => */ "[1,2]",
534 /* 2.5 => */ "(2,3]",
535 /* 3.0 => */ "(2,3]",
536 /* 3.5 => */ "(3,4)",
537 /* 4.0 => */ "[4,5)",
538 /* 4.5 => */ "[4,5)",
543 // Each format object should behave exactly the same
544 ChoiceFormat
* FMT
[] = { &fmt1
, &fmt2
};
545 for (int32_t pass
=0; pass
<2; ++pass
) {
547 for (int32_t ix
=-5; ix
<=55; ix
+=5) {
548 double x
= ix
/ 10.0; // -0.5 to 5.5 step +0.5
549 FMT
[pass
]->format(x
, str
);
551 logln((UnicodeString
)"Ok: " + x
+ " => " + str
);
553 errln((UnicodeString
)"FAIL: " + x
+ " => " + str
+
554 ", expected " + exp
[j
]);
563 * Helper for TestPatterns()
565 void TestChoiceFormat::_testPattern(const char* pattern
,
567 double v1
, const char* str1
,
568 double v2
, const char* str2
,
569 double v3
, const char* str3
) {
570 UErrorCode ec
= U_ZERO_ERROR
;
571 ChoiceFormat
fmt(pattern
, ec
);
574 logln((UnicodeString
)"Ok: " + pattern
+ " failed");
576 logln((UnicodeString
)"FAIL: " + pattern
+ " accepted");
581 errln((UnicodeString
)"FAIL: ChoiceFormat(" + pattern
+ ") failed");
584 logln((UnicodeString
)"Ok: Pattern: " + pattern
);
587 logln((UnicodeString
)" toPattern: " + fmt
.toPattern(out
));
589 double v
[] = {v1
, v2
, v3
};
590 const char* str
[] = {str1
, str2
, str3
};
591 for (int32_t i
=0; i
<3; ++i
) {
593 fmt
.format(v
[i
], out
);
595 logln((UnicodeString
)"Ok: " + v
[i
] + " => " + out
);
597 errln((UnicodeString
)"FAIL: " + v
[i
] + " => " + out
+
598 ", expected " + str
[i
]);
606 void TestChoiceFormat::TestPatterns(void) {
607 // Try a pattern that isolates a single value. Create
608 // three ranges: [-Inf,1.0) [1.0,1.0] (1.0,+Inf]
609 _testPattern("0.0#a|1.0#b|1.0<c", TRUE
,
614 #if 0 // ICU 4.8 only checks the pattern syntax, not whether the ranges make sense.
615 // Try an invalid pattern that isolates a single value.
616 // [-Inf,1.0) [1.0,1.0) [1.0,+Inf]
617 _testPattern("0.0#a|1.0#b|1.0#c", FALSE
,
621 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf]
622 _testPattern("0.0#a|1.0<b|1.0#c", FALSE
,
625 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf]
626 _testPattern("0.0#a|1.0<b|1.0<c", FALSE
,
629 // Try a grossly invalid pattern.
630 // [-Inf,2.0) [2.0,1.0) [1.0,+Inf]
631 _testPattern("0.0#a|2.0#b|1.0#c", FALSE
,
636 void TestChoiceFormat::TestChoiceFormatToPatternOverflow()
638 static const double limits
[] = {0.1e-78, 1e13
, 0.1e78
};
639 UnicodeString monthNames
[] = { "one", "two", "three" };
640 ChoiceFormat
fmt(limits
, monthNames
, UPRV_LENGTHOF(limits
));
641 UnicodeString patStr
, expectedPattern1("1e-79#one|10000000000000#two|1e+77#three"),
642 expectedPattern2("1e-079#one|10000000000000#two|1e+077#three");
643 fmt
.toPattern(patStr
);
644 if (patStr
!= expectedPattern1
&& patStr
!= expectedPattern2
) {
645 errln("ChoiceFormat returned \"" + patStr
+ "\" instead of \"" + expectedPattern1
+ " or " + expectedPattern2
+ "\"");
649 #endif /* #if !UCONFIG_NO_FORMATTING */