2 /********************************************************************
4 * Copyright (c) 1997-2003, 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_out
<< ix
<< " -> " << res1
<< " -> " << res2
<< endl
;
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_out
<< "MessageFormat toPattern: " << res1
<< endl
;
180 fileform
->toPattern( res1
);
181 it_out
<< "ChoiceFormat toPattern: " << res1
<< endl
;
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_out
<< "toPattern tested!" << endl
;
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_out
<< i
<< " -> " << res2
<< endl
;
223 if (res2
!= checkstr
[i
- start
]) {
224 it_errln("*** test_complex_example res string");
225 it_out
<< "*** " << i
<< " -> '" << res2
<< "' unlike '" << checkstr
[i
] << "' ! " << endl
;
230 it_out
<< "------ additional testing in complex test ------" << endl
<< endl
;
233 const double* retLimits
= fileform
->getLimits( retCount
);
234 if ((retCount
== 4) && (retLimits
)
235 && (retLimits
[0] == -1.0)
236 && (retLimits
[1] == 0.0)
237 && (retLimits
[2] == 1.0)
238 && (retLimits
[3] == 2.0)) {
239 it_out
<< "getLimits tested!" << endl
;
241 it_errln("*** getLimits unexpected result!");
244 const UnicodeString
* retFormats
= fileform
->getFormats( retCount
);
245 if ((retCount
== 4) && (retFormats
)
246 && (retFormats
[0] == "are corrupted files")
247 && (retFormats
[1] == "are no files")
248 && (retFormats
[2] == "is one file")
249 && (retFormats
[3] == "are {2} files")) {
250 it_out
<< "getFormats tested!" << endl
;
252 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
) it_out
<< "status applyPattern OK!" << endl
;
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_out
<< i
<< " -> " << res2
<< endl
;
285 if (res2
!= checkstr2
[i
]) {
286 it_errln("*** test_complex_example res string");
287 it_out
<< "*** " << i
<< " -> '" << res2
<< "' unlike '" << checkstr2
[i
] << "' ! " << endl
;
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_out
<< "operator== tested." << endl
;
304 it_errln("*** operator==");
307 if (*form_A
!= *form_B
) {
308 it_out
<< "operator!= tested." << endl
;
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_out
<< "method clone tested." << endl
;
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_out
<< "copy constructor and operator= tested." << endl
;
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.0#none|1.0#one|2.0#many") {
350 it_out
<< "ChoiceFormat contructor( newPattern, status) tested" << endl
;
352 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!");
355 #ifdef U_USE_CHOICE_FORMAT_DEPRECATES
356 double* d_a
= (double *)uprv_malloc(2 * sizeof(double));
357 if (!d_a
) { it_errln("*** allocation error."); return; }
358 d_a
[0] = 1.0; d_a
[1] = 2.0;
360 UnicodeString
* s_a
= new UnicodeString
[2];
361 if (!s_a
) { it_errln("*** allocation error."); return; }
362 s_a
[0] = "first"; s_a
[1] = "second";
364 form_pat
.adoptChoices( d_a
, s_a
, 2 );
365 form_pat
.toPattern( res1
);
366 it_out
<< "ChoiceFormat adoptChoices toPattern: " << res1
<< endl
;
367 if (res1
== "1.0#first|2.0#second") {
368 it_out
<< "ChoiceFormat adoptChoices tested" << endl
;
370 it_errln("*** ChoiceFormat adoptChoices result!");
374 double d_a2
[] = { 3.0, 4.0 };
375 UnicodeString s_a2
[] = { "third", "forth" };
377 form_pat
.setChoices( d_a2
, s_a2
, 2 );
378 form_pat
.toPattern( res1
);
379 it_out
<< "ChoiceFormat adoptChoices toPattern: " << res1
<< endl
;
380 if (res1
== "3.0#third|4.0#forth") {
381 it_out
<< "ChoiceFormat adoptChoices tested" << endl
;
383 it_errln("*** ChoiceFormat adoptChoices result!");
388 status
= U_ZERO_ERROR
;
389 double arg_double
= 3.0;
390 res1
= form_pat
.format( arg_double
, str
, fpos
);
391 it_out
<< "ChoiceFormat format:" << res1
<< endl
;
392 if (res1
!= "third") it_errln("*** ChoiceFormat format (double, ...) result!");
396 status
= U_ZERO_ERROR
;
397 int32_t arg_long
= 3;
398 res1
= form_pat
.format( arg_long
, str
, fpos
);
399 it_out
<< "ChoiceFormat format:" << res1
<< endl
;
400 if (res1
!= "third") it_errln("*** ChoiceFormat format (int32_t, ...) result!");
402 Formattable
ft( (int32_t)3 );
405 status
= U_ZERO_ERROR
;
406 res1
= form_pat
.format( ft
, str
, fpos
, status
);
407 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
413 it_out
<< "ChoiceFormat format:" << res1
<< endl
;
414 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!");
416 Formattable fta
[] = { (int32_t)3 };
419 status
= U_ZERO_ERROR
;
420 res1
= form_pat
.format( fta
, 1, str
, fpos
, status
);
421 if (!chkstatus( status
, "*** test_complex_example format (int32_t, ...)" )) {
427 it_out
<< "ChoiceFormat format:" << res1
<< endl
;
428 if (res1
!= "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!");
430 ParsePosition parse_pos
= 0;
432 UnicodeString
parsetext("third");
433 form_pat
.parse( parsetext
, result
, parse_pos
);
434 double rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
436 it_out
<< "parse( ..., ParsePos ) tested." << endl
;
438 it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
441 form_pat
.parse( parsetext
, result
, status
);
442 rd
= (result
.getType() == Formattable::kLong
) ? result
.getLong() : result
.getDouble();
444 it_out
<< "parse( ..., UErrorCode ) tested." << endl
;
446 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
450 UClassID classID = ChoiceFormat::getStaticClassID();
451 if (classID == form_pat.getDynamicClassID()) {
452 it_out << "getStaticClassID and getDynamicClassID tested." << endl;
454 it_errln("*** getStaticClassID and getDynamicClassID!");
467 * Test new closure API
469 void TestChoiceFormat::TestClosures(void) {
470 // Construct open, half-open, half-open (the other way), and closed
471 // intervals. Do this both using arrays and using a pattern.
473 // 'fmt1' is created using arrays
474 UBool T
= TRUE
, F
= FALSE
;
481 double limits
[] = { 0, 1, 2, 3, 4, 5 };
482 UBool closures
[] = { F
, F
, T
, T
, F
, F
};
483 UnicodeString fmts
[] = {
484 ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5,"
486 ChoiceFormat
fmt1(limits
, closures
, fmts
, 6);
488 // 'fmt2' is created using a pattern; it should be equivalent
489 UErrorCode status
= U_ZERO_ERROR
;
490 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,";
491 ChoiceFormat
fmt2(PAT
, status
);
492 if (U_FAILURE(status
)) {
493 errln("FAIL: ChoiceFormat constructor failed");
497 // Check the patterns
503 errln("FAIL: " + str
+ ", expected " + PAT
);
509 errln("FAIL: fmt1 != fmt2");
514 const double *limits2
= fmt2
.getLimits(count2
);
515 const UBool
*closures2
= fmt2
.getClosures(count2
);
517 if((count2
!= 6) || !limits2
|| !closures2
) {
518 errln("FAIL: couldn't get limits or closures");
520 for(i
=0;i
<count2
;i
++) {
521 logln("#%d/%d: limit %g closed %s\n",
524 closures2
[i
] ?"T":"F");
525 if(limits2
[i
] != limits
[i
]) {
526 errln("FAIL: limit #%d = %g, should be %g\n", i
, limits2
[i
], limits
[i
]);
528 if((closures2
[i
]!=0) != (closures
[i
]!=0)) {
529 errln("FAIL: closure #%d = %s, should be %s\n", i
, closures2
[i
]?"T":"F", closures
[i
]?"T":"F");
534 // Now test both format objects
535 UnicodeString exp
[] = {
539 /* 1.0 => */ "[1,2]",
540 /* 1.5 => */ "[1,2]",
541 /* 2.0 => */ "[1,2]",
542 /* 2.5 => */ "(2,3]",
543 /* 3.0 => */ "(2,3]",
544 /* 3.5 => */ "(3,4)",
545 /* 4.0 => */ "[4,5)",
546 /* 4.5 => */ "[4,5)",
551 // Each format object should behave exactly the same
552 ChoiceFormat
* FMT
[] = { &fmt1
, &fmt2
};
553 for (int32_t pass
=0; pass
<2; ++pass
) {
555 for (int32_t ix
=-5; ix
<=55; ix
+=5) {
556 double x
= ix
/ 10.0; // -0.5 to 5.5 step +0.5
557 FMT
[pass
]->format(x
, str
);
559 logln((UnicodeString
)"Ok: " + x
+ " => " + str
);
561 errln((UnicodeString
)"FAIL: " + x
+ " => " + str
+
562 ", expected " + exp
[j
]);
571 * Helper for TestPatterns()
573 void TestChoiceFormat::_testPattern(const char* pattern
,
575 double v1
, const char* str1
,
576 double v2
, const char* str2
,
577 double v3
, const char* str3
) {
578 UErrorCode ec
= U_ZERO_ERROR
;
579 ChoiceFormat
fmt(pattern
, ec
);
582 logln((UnicodeString
)"Ok: " + pattern
+ " failed");
584 logln((UnicodeString
)"FAIL: " + pattern
+ " accepted");
589 errln((UnicodeString
)"FAIL: ChoiceFormat(" + pattern
+ ") failed");
592 logln((UnicodeString
)"Ok: Pattern: " + pattern
);
595 logln((UnicodeString
)" toPattern: " + fmt
.toPattern(out
));
597 double v
[] = {v1
, v2
, v3
};
598 const char* str
[] = {str1
, str2
, str3
};
599 for (int32_t i
=0; i
<3; ++i
) {
601 fmt
.format(v
[i
], out
);
603 logln((UnicodeString
)"Ok: " + v
[i
] + " => " + out
);
605 errln((UnicodeString
)"FAIL: " + v
[i
] + " => " + out
+
606 ", expected " + str
[i
]);
614 void TestChoiceFormat::TestPatterns(void) {
615 // Try a pattern that isolates a single value. Create
616 // three ranges: [-Inf,1.0) [1.0,1.0] (1.0,+Inf]
617 _testPattern("0.0#a|1.0#b|1.0<c", TRUE
,
622 // Try an invalid pattern that isolates a single value.
623 // [-Inf,1.0) [1.0,1.0) [1.0,+Inf]
624 _testPattern("0.0#a|1.0#b|1.0#c", FALSE
,
628 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf]
629 _testPattern("0.0#a|1.0<b|1.0#c", FALSE
,
632 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf]
633 _testPattern("0.0#a|1.0<b|1.0<c", FALSE
,
636 // Try a grossly invalid pattern.
637 // [-Inf,2.0) [2.0,1.0) [1.0,+Inf]
638 _testPattern("0.0#a|2.0#b|1.0#c", FALSE
,
642 #endif /* #if !UCONFIG_NO_FORMATTING */