2 /********************************************************************
4 * Copyright (c) 1997-2005, 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 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");
82 #ifdef U_USE_CHOICE_FORMAT_DEPRECATES
83 //Testing adoptChoices()
84 double *limitsToAdopt
= (double *)uprv_malloc(7 * sizeof(double));
85 UnicodeString
*monthNamesToAdopt
= new UnicodeString
[7];
87 uprv_arrayCopy(monthNames
, monthNamesToAdopt
, 7);
88 uprv_memcpy(limitsToAdopt
, limits
, (size_t)(7 * sizeof(limits
[0])));
90 formnew
->adoptChoices(limitsToAdopt
, monthNamesToAdopt
, 7);
91 if(!(*formnew
== *form
)){
92 errln("ERROR: ==Operator or adoptChoices failed\n");
101 gotLimits
=(double*)form
->getLimits(count
);
103 errln("getLimits didn't update the count correctly\n");
105 for(ix
=0; ix
<count
; ix
++){
106 if(gotLimits
[ix
] != limits
[ix
]){
107 errln((UnicodeString
)"getLimits didn't get the limits correctly. Expected " + limits
[ix
] + " Got " + gotLimits
[ix
]);
110 //Testing getFormat()
112 UnicodeString
*gotFormats
=0;
113 gotFormats
=(UnicodeString
*)form
->getFormats(count
);
115 errln("getFormats didn't update the count correctly\n");
117 for(ix
=0; ix
<count
; ix
++){
118 if(gotFormats
[ix
] != monthNames
[ix
]){
119 errln((UnicodeString
)"getFormats didn't get the Formats correctly. Expected " + monthNames
[ix
] + " Got " + gotFormats
[ix
]);
129 TestChoiceFormat::TestComplexExample( void )
131 UErrorCode status
= U_ZERO_ERROR
;
132 const double filelimits
[] = {-1, 0,1,2};
133 const UnicodeString filepart
[] = {"are corrupted files", "are no files","is one file","are {2} files"};
135 ChoiceFormat
* fileform
= new ChoiceFormat( filelimits
, filepart
, 4);
138 it_errln("*** test_complex_example fileform");
142 Format
* filenumform
= NumberFormat::createInstance( status
);
144 it_errln("*** test_complex_example filenumform");
148 if (!chkstatus( status
, "*** test_simple_example filenumform" )) {
154 //const Format* testFormats[] = { fileform, NULL, filenumform };
155 //pattform->setFormats( testFormats, 3 );
157 MessageFormat
* pattform
= new MessageFormat("There {0} on {1}", status
);
159 it_errln("*** test_complex_example pattform");
164 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
171 pattform
->setFormat( 0, *fileform
);
172 pattform
->setFormat( 2, *filenumform
);
175 Formattable testArgs
[] = {(int32_t)0, "Disk_A", (int32_t)0};
177 UnicodeString res1
, res2
;
178 pattform
->toPattern( res1
);
179 it_logln("MessageFormat toPattern: " + res1
);
180 fileform
->toPattern( res1
);
181 it_logln("ChoiceFormat toPattern: " + res1
);
182 if (res1
== "-1.0#are corrupted files|0.0#are no files|1.0#is one file|2.0#are {2} files") {
183 it_logln("toPattern tested!");
185 it_errln("*** ChoiceFormat to Pattern result!");
188 FieldPosition
fpos(0);
190 UnicodeString checkstr
[] = {
191 "There are corrupted files on Disk_A",
192 "There are no files on Disk_A",
193 "There is one file on Disk_A",
194 "There are 2 files on Disk_A",
195 "There are 3 files on Disk_A"
198 // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a bad bail out here!
200 if (U_FAILURE(status
)) {
210 for (i
= start
; i
< 4; ++i
) {
212 status
= U_ZERO_ERROR
;
213 testArgs
[0] = Formattable((int32_t)i
);
214 testArgs
[2] = testArgs
[0];
215 res2
= pattform
->format(testArgs
, 3, str
, fpos
, status
);
216 if (!chkstatus( status
, "*** test_complex_example format" )) {
222 it_logln(i
+ UnicodeString(" -> ") + res2
);
223 if (res2
!= checkstr
[i
- start
]) {
224 it_errln("*** test_complex_example res string");
225 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr
[i
] + UnicodeString("' ! "));
230 it_logln("------ additional testing in complex test ------");
234 const double* retLimits
= fileform
->getLimits( retCount
);
235 if ((retCount
== 4) && (retLimits
)
236 && (retLimits
[0] == -1.0)
237 && (retLimits
[1] == 0.0)
238 && (retLimits
[2] == 1.0)
239 && (retLimits
[3] == 2.0)) {
240 it_logln("getLimits tested!");
242 it_errln("*** getLimits unexpected result!");
245 const UnicodeString
* retFormats
= fileform
->getFormats( retCount
);
246 if ((retCount
== 4) && (retFormats
)
247 && (retFormats
[0] == "are corrupted files")
248 && (retFormats
[1] == "are no files")
249 && (retFormats
[2] == "is one file")
250 && (retFormats
[3] == "are {2} files")) {
251 it_logln("getFormats tested!");
253 it_errln("*** getFormats unexpected result!");
256 UnicodeString checkstr2
[] = {
257 "There is no folder on Disk_A",
258 "There is one folder on Disk_A",
259 "There are many folders on Disk_A",
260 "There are many folders on Disk_A"
263 fileform
->applyPattern("0#is no folder|1#is one folder|2#are many folders", status
);
264 if (status
== U_ZERO_ERROR
)
265 it_logln("status applyPattern OK!");
266 if (!chkstatus( status
, "*** test_complex_example pattform" )) {
272 pattform
->setFormat( 0, *fileform
);
274 for (i
= 0; i
< 4; ++i
) {
276 status
= U_ZERO_ERROR
;
277 testArgs
[0] = Formattable((int32_t)i
);
278 testArgs
[2] = testArgs
[0];
279 res2
= pattform
->format(testArgs
, 3, str
, fpos
, status
);
280 if (!chkstatus( status
, "*** test_complex_example format 2" )) {
286 it_logln(UnicodeString() + i
+ UnicodeString(" -> ") + res2
);
287 if (res2
!= checkstr2
[i
]) {
288 it_errln("*** test_complex_example res string");
289 it_errln(UnicodeString("*** ") + i
+ UnicodeString(" -> '") + res2
+ UnicodeString("' unlike '") + checkstr2
[i
] + UnicodeString("' ! "));
293 const double limits_A
[] = {1,2,3,4,5,6,7};
294 const UnicodeString monthNames_A
[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
295 ChoiceFormat
* form_A
= new ChoiceFormat(limits_A
, monthNames_A
, 7);
296 ChoiceFormat
* form_A2
= new ChoiceFormat(limits_A
, monthNames_A
, 7);
297 const double limits_B
[] = {1,2,3,4,5,6,7};
298 const UnicodeString monthNames_B
[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat_BBB"};
299 ChoiceFormat
* form_B
= new ChoiceFormat(limits_B
, monthNames_B
, 7);
300 if (!form_A
|| !form_B
|| !form_A2
) {
301 it_errln("*** test-choiceFormat not allocatable!");
303 if (*form_A
== *form_A2
) {
304 it_logln("operator== tested.");
306 it_errln("*** operator==");
309 if (*form_A
!= *form_B
) {
310 it_logln("operator!= tested.");
312 it_errln("*** operator!=");
315 ChoiceFormat
* form_A3
= (ChoiceFormat
*) form_A
->clone();
317 it_errln("*** ChoiceFormat->clone is nil.");
319 if ((*form_A3
== *form_A
) && (*form_A3
!= *form_B
)) {
320 it_logln("method clone tested.");
322 it_errln("*** ChoiceFormat clone or operator==, or operator!= .");
326 ChoiceFormat
form_Assigned( *form_A
);
327 UBool ok
= (form_Assigned
== *form_A
) && (form_Assigned
!= *form_B
);
328 form_Assigned
= *form_B
;
329 ok
= ok
&& (form_Assigned
!= *form_A
) && (form_Assigned
== *form_B
);
331 it_logln("copy constructor and operator= tested.");
333 it_errln("*** copy constructor or operator= or operator == or operator != .");
339 delete form_A
; delete form_A2
; delete form_B
;
341 const char* testPattern
= "0#none|1#one|2#many";
342 ChoiceFormat
form_pat( testPattern
, status
);
343 if (!chkstatus( status
, "*** ChoiceFormat contructor( newPattern, status)" )) {
350 form_pat
.toPattern( res1
);
351 if (res1
== "0.0#none|1.0#one|2.0#many") {
352 it_logln("ChoiceFormat contructor( newPattern, status) tested");
354 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!");
357 #ifdef U_USE_CHOICE_FORMAT_DEPRECATES
358 double* d_a
= (double *)uprv_malloc(2 * sizeof(double));
359 if (!d_a
) { it_errln("*** allocation error."); return; }
360 d_a
[0] = 1.0; d_a
[1] = 2.0;
362 UnicodeString
* s_a
= new UnicodeString
[2];
363 if (!s_a
) { it_errln("*** allocation error."); return; }
364 s_a
[0] = "first"; s_a
[1] = "second";
366 form_pat
.adoptChoices( d_a
, s_a
, 2 );
367 form_pat
.toPattern( res1
);
368 it_out
<< "ChoiceFormat adoptChoices toPattern: " << res1
<< endl
;
369 if (res1
== "1.0#first|2.0#second") {
370 it_logln("ChoiceFormat adoptChoices tested");
372 it_errln("*** ChoiceFormat adoptChoices result!");
376 double d_a2
[] = { 3.0, 4.0 };
377 UnicodeString s_a2
[] = { "third", "forth" };
379 form_pat
.setChoices( d_a2
, s_a2
, 2 );
380 form_pat
.toPattern( res1
);
381 it_logln(UnicodeString("ChoiceFormat adoptChoices toPattern: ") + res1
);
382 if (res1
== "3.0#third|4.0#forth") {
383 it_logln("ChoiceFormat adoptChoices tested");
385 it_errln("*** ChoiceFormat adoptChoices result!");
390 status
= U_ZERO_ERROR
;
391 double arg_double
= 3.0;
392 res1
= form_pat
.format( arg_double
, str
, fpos
);
393 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
394 if (res1
!= "third") it_errln("*** ChoiceFormat format (double, ...) result!");
398 status
= U_ZERO_ERROR
;
400 res1
= form_pat
.format( arg_64
, str
, fpos
);
401 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
402 if (res1
!= "third") it_errln("*** ChoiceFormat format (int64_t, ...) result!");
406 status
= U_ZERO_ERROR
;
407 int32_t arg_long
= 3;
408 res1
= form_pat
.format( arg_long
, str
, fpos
);
409 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
410 if (res1
!= "third") it_errln("*** ChoiceFormat format (int32_t, ...) result!");
412 Formattable
ft( (int32_t)3 );
415 status
= U_ZERO_ERROR
;
416 res1
= form_pat
.format( ft
, str
, fpos
, status
);
417 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
423 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
424 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!");
426 Formattable fta
[] = { (int32_t)3 };
429 status
= U_ZERO_ERROR
;
430 res1
= form_pat
.format( fta
, 1, str
, fpos
, status
);
431 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
437 it_logln(UnicodeString("ChoiceFormat format:") + res1
);
438 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!");
440 ParsePosition parse_pos
= 0;
442 UnicodeString
parsetext("third");
443 form_pat
.parse( parsetext
, result
, parse_pos
);
444 double rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
446 it_logln("parse( ..., ParsePos ) tested.");
448 it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
451 form_pat
.parse( parsetext
, result
, status
);
452 rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
454 it_logln("parse( ..., UErrorCode ) tested.");
456 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
460 UClassID classID = ChoiceFormat::getStaticClassID();
461 if (classID == form_pat.getDynamicClassID()) {
462 it_out << "getStaticClassID and getDynamicClassID tested." << endl;
464 it_errln("*** getStaticClassID and getDynamicClassID!");
477 * Test new closure API
479 void TestChoiceFormat::TestClosures(void) {
480 // Construct open, half-open, half-open (the other way), and closed
481 // intervals. Do this both using arrays and using a pattern.
483 // 'fmt1' is created using arrays
484 UBool T
= TRUE
, F
= FALSE
;
491 double limits
[] = { 0, 1, 2, 3, 4, 5 };
492 UBool closures
[] = { F
, F
, T
, T
, F
, F
};
493 UnicodeString fmts
[] = {
494 ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5,"
496 ChoiceFormat
fmt1(limits
, closures
, fmts
, 6);
498 // 'fmt2' is created using a pattern; it should be equivalent
499 UErrorCode status
= U_ZERO_ERROR
;
500 const char* PAT
= "0.0#,1)|1.0#[1,2]|2.0<(2,3]|3.0<(3,4)|4.0#[4,5)|5.0#[5,";
501 ChoiceFormat
fmt2(PAT
, status
);
502 if (U_FAILURE(status
)) {
503 errln("FAIL: ChoiceFormat constructor failed");
507 // Check the patterns
513 errln("FAIL: " + str
+ ", expected " + PAT
);
519 errln("FAIL: fmt1 != fmt2");
524 const double *limits2
= fmt2
.getLimits(count2
);
525 const UBool
*closures2
= fmt2
.getClosures(count2
);
527 if((count2
!= 6) || !limits2
|| !closures2
) {
528 errln("FAIL: couldn't get limits or closures");
530 for(i
=0;i
<count2
;i
++) {
531 logln("#%d/%d: limit %g closed %s\n",
534 closures2
[i
] ?"T":"F");
535 if(limits2
[i
] != limits
[i
]) {
536 errln("FAIL: limit #%d = %g, should be %g\n", i
, limits2
[i
], limits
[i
]);
538 if((closures2
[i
]!=0) != (closures
[i
]!=0)) {
539 errln("FAIL: closure #%d = %s, should be %s\n", i
, closures2
[i
]?"T":"F", closures
[i
]?"T":"F");
544 // Now test both format objects
545 UnicodeString exp
[] = {
549 /* 1.0 => */ "[1,2]",
550 /* 1.5 => */ "[1,2]",
551 /* 2.0 => */ "[1,2]",
552 /* 2.5 => */ "(2,3]",
553 /* 3.0 => */ "(2,3]",
554 /* 3.5 => */ "(3,4)",
555 /* 4.0 => */ "[4,5)",
556 /* 4.5 => */ "[4,5)",
561 // Each format object should behave exactly the same
562 ChoiceFormat
* FMT
[] = { &fmt1
, &fmt2
};
563 for (int32_t pass
=0; pass
<2; ++pass
) {
565 for (int32_t ix
=-5; ix
<=55; ix
+=5) {
566 double x
= ix
/ 10.0; // -0.5 to 5.5 step +0.5
567 FMT
[pass
]->format(x
, str
);
569 logln((UnicodeString
)"Ok: " + x
+ " => " + str
);
571 errln((UnicodeString
)"FAIL: " + x
+ " => " + str
+
572 ", expected " + exp
[j
]);
581 * Helper for TestPatterns()
583 void TestChoiceFormat::_testPattern(const char* pattern
,
585 double v1
, const char* str1
,
586 double v2
, const char* str2
,
587 double v3
, const char* str3
) {
588 UErrorCode ec
= U_ZERO_ERROR
;
589 ChoiceFormat
fmt(pattern
, ec
);
592 logln((UnicodeString
)"Ok: " + pattern
+ " failed");
594 logln((UnicodeString
)"FAIL: " + pattern
+ " accepted");
599 errln((UnicodeString
)"FAIL: ChoiceFormat(" + pattern
+ ") failed");
602 logln((UnicodeString
)"Ok: Pattern: " + pattern
);
605 logln((UnicodeString
)" toPattern: " + fmt
.toPattern(out
));
607 double v
[] = {v1
, v2
, v3
};
608 const char* str
[] = {str1
, str2
, str3
};
609 for (int32_t i
=0; i
<3; ++i
) {
611 fmt
.format(v
[i
], out
);
613 logln((UnicodeString
)"Ok: " + v
[i
] + " => " + out
);
615 errln((UnicodeString
)"FAIL: " + v
[i
] + " => " + out
+
616 ", expected " + str
[i
]);
624 void TestChoiceFormat::TestPatterns(void) {
625 // Try a pattern that isolates a single value. Create
626 // three ranges: [-Inf,1.0) [1.0,1.0] (1.0,+Inf]
627 _testPattern("0.0#a|1.0#b|1.0<c", TRUE
,
632 // Try an invalid pattern that isolates a single value.
633 // [-Inf,1.0) [1.0,1.0) [1.0,+Inf]
634 _testPattern("0.0#a|1.0#b|1.0#c", FALSE
,
638 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf]
639 _testPattern("0.0#a|1.0<b|1.0#c", FALSE
,
642 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf]
643 _testPattern("0.0#a|1.0<b|1.0<c", FALSE
,
646 // Try a grossly invalid pattern.
647 // [-Inf,2.0) [2.0,1.0) [1.0,+Inf]
648 _testPattern("0.0#a|2.0#b|1.0#c", FALSE
,
652 #endif /* #if !UCONFIG_NO_FORMATTING */