]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/msfmrgts.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2011, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/format.h"
14 #include "unicode/decimfmt.h"
15 #include "unicode/locid.h"
16 #include "unicode/msgfmt.h"
17 #include "unicode/numfmt.h"
18 #include "unicode/choicfmt.h"
19 #include "unicode/gregocal.h"
22 // *****************************************************************************
23 // class MessageFormatRegressionTest
24 // *****************************************************************************
26 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
29 MessageFormatRegressionTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
32 TESTCASE_AUTO(Test4074764
)
33 //TESTCASE_AUTO(Test4058973) -- disabled/obsolete in ICU 4.8
34 TESTCASE_AUTO(Test4031438
)
35 TESTCASE_AUTO(Test4052223
)
36 TESTCASE_AUTO(Test4104976
)
37 TESTCASE_AUTO(Test4106659
)
38 TESTCASE_AUTO(Test4106660
)
39 TESTCASE_AUTO(Test4111739
)
40 TESTCASE_AUTO(Test4114743
)
41 TESTCASE_AUTO(Test4116444
)
42 TESTCASE_AUTO(Test4114739
)
43 TESTCASE_AUTO(Test4113018
)
44 TESTCASE_AUTO(Test4106661
)
45 TESTCASE_AUTO(Test4094906
)
46 TESTCASE_AUTO(Test4118592
)
47 TESTCASE_AUTO(Test4118594
)
48 TESTCASE_AUTO(Test4105380
)
49 TESTCASE_AUTO(Test4120552
)
50 TESTCASE_AUTO(Test4142938
)
51 TESTCASE_AUTO(TestChoicePatternQuote
)
52 TESTCASE_AUTO(Test4112104
)
53 TESTCASE_AUTO(TestAPI
)
58 MessageFormatRegressionTest::failure(UErrorCode status
, const char* msg
, UBool possibleDataError
)
60 if(U_FAILURE(status
)) {
61 if (possibleDataError
) {
62 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
64 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
73 * Null exception when formatting pattern with MessageFormat
76 void MessageFormatRegressionTest::Test4074764() {
77 UnicodeString pattern
[] = {
78 "Message without param",
79 "Message with param:{0}",
80 "Longer Message with param {0}"
82 //difference between the two param strings are that
83 //in the first one, the param position is within the
84 //length of the string without param while it is not so
87 UErrorCode status
= U_ZERO_ERROR
;
88 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
90 failure(status
, "couldn't create MessageFormat");
93 //Apply pattern with param and print the result
94 messageFormatter
->applyPattern(pattern
[1], status
);
95 failure(status
, "messageFormat->applyPattern");
96 //Object[] params = {new UnicodeString("BUG"), new Date()};
97 Formattable params
[] = {
98 Formattable(UnicodeString("BUG")),
99 Formattable(0, Formattable::kIsDate
)
101 UnicodeString tempBuffer
;
102 FieldPosition
pos(FieldPosition::DONT_CARE
);
103 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
104 if( tempBuffer
!= "Message with param:BUG" || failure(status
, "messageFormat->format"))
105 errln("MessageFormat with one param test failed.");
106 logln("Formatted with one extra param : " + tempBuffer
);
108 //Apply pattern without param and print the result
109 messageFormatter
->applyPattern(pattern
[0], status
);
110 failure(status
, "messageFormatter->applyPattern");
112 // {sfb} how much does this apply in C++?
113 // do we want to verify that the Formattable* array is not NULL,
114 // or is that the user's responsibility?
115 // additionally, what should be the item count?
116 // for bug testing purposes, assume that something was set to
117 // NULL by mistake, and that the length should be non-zero
119 //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
121 tempBuffer
= messageFormatter
->format(NULL
, 0, tempBuffer
, pos
, status
);
123 if( tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
124 errln("MessageFormat with no param test failed.");
125 logln("Formatted with no params : " + tempBuffer
);
128 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
129 if (tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
130 errln("Formatted with arguments > subsitution failed. result = " + tempBuffer
);
131 logln("Formatted with extra params : " + tempBuffer
);
132 //This statement gives an exception while formatting...
133 //If we use pattern[1] for the message with param,
134 //we get an NullPointerException in MessageFormat.java(617)
135 //If we use pattern[2] for the message with param,
136 //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
137 //Both are due to maxOffset not being reset to -1
138 //in applyPattern() when the pattern does not
140 /*} catch (Exception foo) {
141 errln("Exception when formatting with no params.");
144 delete messageFormatter
;
148 * MessageFormat.toPattern has weird rounding behavior.
150 * ICU 4.8: This test is commented out because toPattern() has been changed to return
151 * the original pattern string, rather than reconstituting a new (equivalent) one.
152 * This trivially eliminates issues with rounding or any other pattern string differences.
155 void MessageFormatRegressionTest::Test4058973()
157 UErrorCode status = U_ZERO_ERROR;
158 MessageFormat *fmt = new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}", status);
159 failure(status, "new MessageFormat");
162 pat = fmt->toPattern(pat);
163 UnicodeString exp("{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
165 errln("MessageFormat.toPattern failed");
166 errln("Exp: " + exp);
167 errln("Got: " + pat);
173 * More robust message formats.
175 void MessageFormatRegressionTest::Test4031438()
177 UErrorCode status
= U_ZERO_ERROR
;
179 UnicodeString
pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}.");
180 UnicodeString
pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.");
182 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
183 failure(status
, "new MessageFormat");
185 const UBool possibleDataError
= TRUE
;
188 logln("Apply with pattern : " + pattern1
);
189 messageFormatter
->applyPattern(pattern1
, status
);
190 failure(status
, "messageFormat->applyPattern");
191 //Object[] params = {new Integer(7)};
192 Formattable params
[]= {
193 Formattable((int32_t)7)
195 UnicodeString tempBuffer
;
196 FieldPosition
pos(FieldPosition::DONT_CARE
);
197 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
198 if(tempBuffer
!= "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status
, "MessageFormat::format"))
199 dataerrln("Tests arguments < substitution failed");
200 logln("Formatted with 7 : " + tempBuffer
);
203 Formattable
*objs
= messageFormatter
->parse(tempBuffer
, pp
, count
);
204 //if(objs[7/*params.length*/] != NULL)
205 // errln("Parse failed with more than expected arguments");
207 NumberFormat
*fmt
= 0;
208 UnicodeString temp
, temp1
;
210 for (int i
= 0; i
< count
; i
++) {
212 // convert to string if not already
213 Formattable obj
= objs
[i
];
215 if(obj
.getType() == Formattable::kString
)
216 temp
= obj
.getString(temp
);
218 fmt
= NumberFormat::createInstance(status
);
219 switch (obj
.getType()) {
220 case Formattable::kLong
: fmt
->format(obj
.getLong(), temp
); break;
221 case Formattable::kInt64
: fmt
->format(obj
.getInt64(), temp
); break;
222 case Formattable::kDouble
: fmt
->format(obj
.getDouble(), temp
); break;
227 // convert to string if not already
228 Formattable obj1
= params
[i
];
230 if(obj1
.getType() == Formattable::kString
)
231 temp1
= obj1
.getString(temp1
);
233 fmt
= NumberFormat::createInstance(status
);
234 switch (obj1
.getType()) {
235 case Formattable::kLong
: fmt
->format(obj1
.getLong(), temp1
); break;
236 case Formattable::kInt64
: fmt
->format(obj1
.getInt64(), temp1
); break;
237 case Formattable::kDouble
: fmt
->format(obj1
.getDouble(), temp1
); break;
242 //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
244 errln("Parse failed on object " + objs
[i
].getString(temp1
) + " at index : " + i
);
251 // {sfb} does this apply? no way to really pass a null Formattable,
254 /*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
255 if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format"))
256 errln("Tests with no arguments failed");
257 logln("Formatted with null : " + tempBuffer);*/
258 logln("Apply with pattern : " + pattern2
);
259 messageFormatter
->applyPattern(pattern2
, status
);
260 failure(status
, "messageFormatter->applyPattern", possibleDataError
);
262 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
263 if (tempBuffer
!= "Double ' Quotes 7 test and quoted {1} test plus 'other {2} stuff'.")
264 dataerrln("quote format test (w/ params) failed. - %s", u_errorName(status
));
265 logln("Formatted with params : " + tempBuffer
);
267 /*tempBuffer = messageFormatter->format(null);
268 if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
269 errln("quote format test (w/ null) failed.");
270 logln("Formatted with null : " + tempBuffer);
271 logln("toPattern : " + messageFormatter.toPattern());*/
272 /*} catch (Exception foo) {
273 errln("Exception when formatting in bug 4031438. "+foo.getMessage());
275 delete messageFormatter
;
278 void MessageFormatRegressionTest::Test4052223()
281 ParsePosition
pos(0);
282 if (pos
.getErrorIndex() != -1) {
283 errln("ParsePosition.getErrorIndex initialization failed.");
286 UErrorCode status
= U_ZERO_ERROR
;
287 MessageFormat
*fmt
= new MessageFormat("There are {0} apples growing on the {1} tree.", status
);
288 failure(status
, "new MessageFormat");
289 UnicodeString
str("There is one apple growing on the peach tree.");
292 fmt
->parse(str
, pos
, count
);
294 logln(UnicodeString("unparsable string , should fail at ") + pos
.getErrorIndex());
295 if (pos
.getErrorIndex() == -1)
296 errln("Bug 4052223 failed : parsing string " + str
);
297 pos
.setErrorIndex(4);
298 if (pos
.getErrorIndex() != 4)
299 errln(UnicodeString("setErrorIndex failed, got ") + pos
.getErrorIndex() + " instead of 4");
301 ChoiceFormat
*f
= new ChoiceFormat(
302 "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.", status
);
303 failure(status
, "new ChoiceFormat");
305 pos
.setErrorIndex(-1);
307 f
->parse("are negative", obj
, pos
);
308 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == -1.0)
309 errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos
.getErrorIndex());
311 pos
.setErrorIndex(-1);
312 f
->parse("are no or fraction ", obj
, pos
);
313 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == 0.0)
314 errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos
.getErrorIndex());
316 pos
.setErrorIndex(-1);
317 f
->parse("go postal", obj
, pos
);
318 if (pos
.getErrorIndex() == -1 && ! uprv_isNaN(obj
.getDouble()))
319 errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos
.getErrorIndex());
325 * ChoiceFormat.equals(null) throws NullPointerException
328 // {sfb} not really applicable in C++?? (kind of silly)
330 void MessageFormatRegressionTest::Test4104976()
332 double limits
[] = {1, 20};
333 UnicodeString formats
[] = {
334 UnicodeString("xyz"),
337 int32_t formats_length
= (int32_t)(sizeof(formats
)/sizeof(formats
[0]));
338 UErrorCode status
= U_ZERO_ERROR
;
339 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, formats_length
);
340 failure(status
, "new ChoiceFormat");
342 log("Compares to null is always false, returned : ");
343 logln(cf
== NULL
? "TRUE" : "FALSE");
344 /*} catch (Exception foo) {
345 errln("ChoiceFormat.equals(null) throws exception.");
352 * ChoiceFormat.ctor(double[], String[]) doesn't check
353 * whether lengths of input arrays are equal.
356 // {sfb} again, not really applicable in C++
358 void MessageFormatRegressionTest::Test4106659()
364 UnicodeString formats [] = {
367 ChoiceFormat *cf = NULL;
369 // cf = new ChoiceFormat(limits, formats, 3);
370 //} catch (Exception foo) {
371 // logln("ChoiceFormat constructor should check for the array lengths");
375 // errln(cf->format(5));
382 * ChoiceFormat.ctor(double[], String[]) allows unordered double array.
383 * This is not a bug, added javadoc to emphasize the use of limit
384 * array must be in ascending order.
386 void MessageFormatRegressionTest::Test4106660()
388 double limits
[] = {3, 1, 2};
389 UnicodeString formats
[] = {
390 UnicodeString("Three"),
391 UnicodeString("One"),
394 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, 3);
397 FieldPosition
pos(FieldPosition::DONT_CARE
);
398 str
= cf
->format(d
, str
, pos
);
400 errln( (UnicodeString
) "format(" + d
+ ") = " + str
);
406 * MessageFormat is incorrectly serialized/deserialized.
409 // {sfb} doesn't apply in C++
411 void MessageFormatRegressionTest::Test4111739()
413 /*MessageFormat format1 = null;
414 MessageFormat format2 = null;
415 ObjectOutputStream ostream = null;
416 ByteArrayOutputStream baos = null;
417 ObjectInputStream istream = null;
420 baos = new ByteArrayOutputStream();
421 ostream = new ObjectOutputStream(baos);
422 } catch(IOException e) {
423 errln("Unexpected exception : " + e.getMessage());
428 format1 = new MessageFormat("pattern{0}");
429 ostream.writeObject(format1);
432 byte bytes[] = baos.toByteArray();
434 istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
435 format2 = (MessageFormat)istream.readObject();
436 } catch(Exception e) {
437 errln("Unexpected exception : " + e.getMessage());
440 if (!format1.equals(format2)) {
441 errln("MessageFormats before and after serialization are not" +
442 " equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
443 format2 + "(" + format2.toPattern() + ")");
445 logln("Serialization for MessageFormat is OK.");
449 * MessageFormat.applyPattern allows illegal patterns.
451 void MessageFormatRegressionTest::Test4114743()
453 UnicodeString
originalPattern("initial pattern");
454 UErrorCode status
= U_ZERO_ERROR
;
455 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
456 failure(status
, "new MessageFormat");
458 UnicodeString
illegalPattern("ab { '}' de");
459 mf
->applyPattern(illegalPattern
, status
);
460 if( ! U_FAILURE(status
))
461 errln("illegal pattern: \"" + illegalPattern
+ "\"");
462 /*} catch (IllegalArgumentException foo) {
463 if (!originalPattern.equals(mf.toPattern()))
464 errln("pattern after: \"" + mf.toPattern() + "\"");
470 * MessageFormat.parse has different behavior in case of null.
472 void MessageFormatRegressionTest::Test4116444()
474 UnicodeString patterns
[] = {
476 (UnicodeString
)"one",
477 (UnicodeString
) "{0,date,short}"
480 UErrorCode status
= U_ZERO_ERROR
;
481 MessageFormat
*mf
= new MessageFormat("", status
);
482 failure(status
, "new MessageFormat");
484 for (int i
= 0; i
< 3; i
++) {
485 UnicodeString pattern
= patterns
[i
];
486 mf
->applyPattern(pattern
, status
);
487 failure(status
, "mf->applyPattern", TRUE
);
492 Formattable
*array
= mf
->parse(UnicodeString(""), pp
, count
);
493 logln("pattern: \"" + pattern
+ "\"");
494 log(" parsedObjects: ");
497 for (int j
= 0; j
< count
; j
++) {
498 //if (array[j] != null)
500 dataerrln("\"" + array
[j
].getString(dummy
) + "\"");
512 /*} catch (Exception e) {
513 errln("pattern: \"" + pattern + "\"");
514 errln(" Exception: " + e.getMessage());
520 /* @bug 4114739 (FIX and add javadoc)
521 * MessageFormat.format has undocumented behavior about empty format objects.
524 // {sfb} doesn't apply in C++?
525 void MessageFormatRegressionTest::Test4114739()
528 UErrorCode status
= U_ZERO_ERROR
;
529 MessageFormat
*mf
= new MessageFormat("<{0}>", status
);
530 failure(status
, "new MessageFormat");
532 Formattable
*objs1
= NULL
;
533 //Formattable objs2 [] = {};
534 //Formattable *objs3 [] = {NULL};
538 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
539 log("format(null) : ");
540 FieldPosition
pos(FieldPosition::DONT_CARE
);
541 logln("\"" + mf
->format(objs1
, 0, res
, pos
, status
) + "\"");
542 failure(status
, "mf->format");
543 /*log("format({}) : ");
544 logln("\"" + mf->format(objs2, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
545 failure(status, "mf->format");
546 log("format({null}) :");
547 logln("\"" + mf->format(objs3, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
548 failure(status, "mf->format");*/
549 /*} catch (Exception e) {
550 errln("Exception thrown for null argument tests.");
557 * MessageFormat.applyPattern works wrong with illegal patterns.
559 void MessageFormatRegressionTest::Test4113018()
561 UnicodeString
originalPattern("initial pattern");
562 UErrorCode status
= U_ZERO_ERROR
;
563 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
564 failure(status
, "new messageFormat");
565 UnicodeString
illegalPattern("format: {0, xxxYYY}");
567 logln("pattern before: \"" + mf
->toPattern(pat
) + "\"");
568 logln("illegal pattern: \"" + illegalPattern
+ "\"");
570 mf
->applyPattern(illegalPattern
, status
);
571 if( ! U_FAILURE(status
))
572 errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern
);
573 /*} catch (IllegalArgumentException e) {
574 if (!originalPattern.equals(mf.toPattern()))
575 errln("pattern after: \"" + mf.toPattern() + "\"");
581 * ChoiceFormat is silent about the pattern usage in javadoc.
583 void MessageFormatRegressionTest::Test4106661()
585 UErrorCode status
= U_ZERO_ERROR
;
586 ChoiceFormat
*fmt
= new ChoiceFormat(
587 "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status
);
588 failure(status
, "new ChoiceFormat");
590 logln("Formatter Pattern : " + fmt
->toPattern(pat
));
592 FieldPosition
bogus(FieldPosition::DONT_CARE
);
595 // Will this work for -inf?
596 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
597 failure(status
, "fmt->format");
599 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
600 failure(status
, "fmt->format");
602 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
603 failure(status
, "fmt->format");
605 logln("Format with 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
606 failure(status
, "fmt->format");
608 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), str
, bogus
, status
));
609 failure(status
, "fmt->format");
611 logln("Format with 1.0 : " + fmt
->format(Formattable(1.0), str
, bogus
, status
));
612 failure(status
, "fmt->format");
614 logln("Format with 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
615 failure(status
, "fmt->format");
617 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
618 failure(status
, "fmt->format");
620 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
621 failure(status
, "fmt->format");
623 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
624 failure(status
, "fmt->format");
626 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
627 failure(status
, "fmt->format");
633 * ChoiceFormat should accept \u221E as eq. to INF.
635 void MessageFormatRegressionTest::Test4094906()
637 UErrorCode status
= U_ZERO_ERROR
;
638 UnicodeString
pattern("-");
639 pattern
+= (UChar
) 0x221E;
640 pattern
+= "<are negative|0<are no or fraction|1#is one|1<is 1+|";
641 pattern
+= (UChar
) 0x221E;
642 pattern
+= "<are many.";
644 ChoiceFormat
*fmt
= new ChoiceFormat(pattern
, status
);
645 failure(status
, "new ChoiceFormat");
647 if (fmt
->toPattern(pat
) != pattern
) {
648 errln( (UnicodeString
) "Formatter Pattern : " + pat
);
649 errln( (UnicodeString
) "Expected Pattern : " + pattern
);
651 FieldPosition
bogus(FieldPosition::DONT_CARE
);
654 // Will this work for -inf?
655 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
656 failure(status
, "fmt->format");
658 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
659 failure(status
, "fmt->format");
661 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
662 failure(status
, "fmt->format");
664 logln("Format with 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
665 failure(status
, "fmt->format");
667 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), str
, bogus
, status
));
668 failure(status
, "fmt->format");
670 logln("Format with 1.0 : " + fmt
->format(Formattable(1.0), str
, bogus
, status
));
671 failure(status
, "fmt->format");
673 logln("Format with 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
674 failure(status
, "fmt->format");
676 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
677 failure(status
, "fmt->format");
679 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
680 failure(status
, "fmt->format");
682 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
683 failure(status
, "fmt->format");
685 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
686 failure(status
, "fmt->format");
692 * MessageFormat.parse fails with ChoiceFormat.
694 void MessageFormatRegressionTest::Test4118592()
696 UErrorCode status
= U_ZERO_ERROR
;
697 MessageFormat
*mf
= new MessageFormat("", status
);
698 failure(status
, "new messageFormat");
699 UnicodeString
pattern("{0,choice,1#YES|2#NO}");
700 UnicodeString
prefix("");
701 Formattable
*objs
= 0;
703 for (int i
= 0; i
< 5; i
++) {
704 UnicodeString formatted
;
705 formatted
= prefix
+ "YES";
706 mf
->applyPattern(prefix
+ pattern
, status
);
707 failure(status
, "mf->applyPattern");
709 //Object[] objs = mf.parse(formatted, new ParsePosition(0));
712 objs
= mf
->parse(formatted
, pp
, count
);
714 logln(UnicodeString("") + i
+ ". pattern :\"" + mf
->toPattern(pat
) + "\"");
715 log(" \"" + formatted
+ "\" parsed as ");
720 if(objs
[0].getType() == Formattable::kString
)
721 logln((UnicodeString
)" " + objs
[0].getString(temp
));
723 logln((UnicodeString
)" " + (objs
[0].getType() == Formattable::kLong
? objs
[0].getLong() : objs
[0].getDouble()));
732 * MessageFormat.parse fails for some patterns.
734 void MessageFormatRegressionTest::Test4118594()
736 UErrorCode status
= U_ZERO_ERROR
;
737 const UBool possibleDataError
= TRUE
;
738 MessageFormat
*mf
= new MessageFormat("{0}, {0}, {0}", status
);
739 failure(status
, "new MessageFormat");
740 UnicodeString
forParsing("x, y, z");
741 //Object[] objs = mf.parse(forParsing, new ParsePosition(0));
744 Formattable
*objs
= mf
->parse(forParsing
, pp
, count
);
746 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
747 logln("text for parsing: \"" + forParsing
+ "\"");
749 if (objs
[0].getString(str
) != "z")
750 errln("argument0: \"" + objs
[0].getString(str
) + "\"");
751 mf
->applyPattern("{0,number,#.##}, {0,number,#.#}", status
);
752 failure(status
, "mf->applyPattern", possibleDataError
);
753 //Object[] oldobjs = {new Double(3.1415)};
754 Formattable oldobjs
[] = {Formattable(3.1415)};
755 UnicodeString result
;
756 FieldPosition
pos(FieldPosition::DONT_CARE
);
757 result
= mf
->format( oldobjs
, 1, result
, pos
, status
);
758 failure(status
, "mf->format", possibleDataError
);
760 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
761 logln("text for parsing: \"" + result
+ "\"");
762 // result now equals "3.14, 3.1"
763 if (result
!= "3.14, 3.1")
764 dataerrln("result = " + result
+ " - " + u_errorName(status
));
765 //Object[] newobjs = mf.parse(result, new ParsePosition(0));
768 Formattable
*newobjs
= mf
->parse(result
, pp
, count1
);
769 // newobjs now equals {new Double(3.1)}
770 if (newobjs
== NULL
) {
771 dataerrln("Error calling MessageFormat::parse");
773 if (newobjs
[0].getDouble() != 3.1)
774 errln( UnicodeString("newobjs[0] = ") + newobjs
[0].getDouble());
782 * When using ChoiceFormat, MessageFormat is not good for I18n.
784 void MessageFormatRegressionTest::Test4105380()
786 UnicodeString
patternText1("The disk \"{1}\" contains {0}.");
787 UnicodeString
patternText2("There are {0} on the disk \"{1}\"");
788 UErrorCode status
= U_ZERO_ERROR
;
789 const UBool possibleDataError
= TRUE
;
790 MessageFormat
*form1
= new MessageFormat(patternText1
, status
);
791 failure(status
, "new MessageFormat");
792 MessageFormat
*form2
= new MessageFormat(patternText2
, status
);
793 failure(status
, "new MessageFormat");
794 double filelimits
[] = {0,1,2};
795 UnicodeString filepart
[] = {
796 (UnicodeString
)"no files",
797 (UnicodeString
)"one file",
798 (UnicodeString
)"{0,number} files"
800 ChoiceFormat
*fileform
= new ChoiceFormat(filelimits
, filepart
, 3);
801 form1
->setFormat(1, *fileform
);
802 form2
->setFormat(0, *fileform
);
803 //Object[] testArgs = {new Long(12373), "MyDisk"};
804 Formattable testArgs
[] = {
805 Formattable((int32_t)12373),
806 Formattable((UnicodeString
)"MyDisk")
809 FieldPosition
bogus(FieldPosition::DONT_CARE
);
811 UnicodeString result
;
812 logln(form1
->format(testArgs
, 2, result
, bogus
, status
));
813 failure(status
, "form1->format", possibleDataError
);
815 logln(form2
->format(testArgs
, 2, result
, bogus
, status
));
816 failure(status
, "form1->format", possibleDataError
);
823 * MessageFormat.parse incorrectly sets errorIndex.
825 void MessageFormatRegressionTest::Test4120552()
827 UErrorCode status
= U_ZERO_ERROR
;
828 MessageFormat
*mf
= new MessageFormat("pattern", status
);
829 failure(status
, "new MessageFormat");
830 UnicodeString texts
[] = {
831 (UnicodeString
)"pattern",
832 (UnicodeString
)"pat",
833 (UnicodeString
)"1234"
836 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
837 for (int i
= 0; i
< 3; i
++) {
839 //Object[] objs = mf.parse(texts[i], pp);
841 Formattable
*objs
= mf
->parse(texts
[i
], pp
, count
);
842 log(" text for parsing: \"" + texts
[i
] + "\"");
844 logln(" (incorrectly formatted string)");
845 if (pp
.getErrorIndex() == -1)
846 errln(UnicodeString("Incorrect error index: ") + pp
.getErrorIndex());
848 logln(" (correctly formatted string)");
857 * MessageFormat handles single quotes in pattern wrong.
858 * This is actually a problem in ChoiceFormat; it doesn't
859 * understand single quotes.
861 void MessageFormatRegressionTest::Test4142938()
863 UnicodeString pat
= CharsToUnicodeString("''Vous'' {0,choice,0#n''|1#}avez s\\u00E9lectionn\\u00E9 "
864 "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} "
865 "personnel{0,choice,0#s|1#|2#s}.");
866 UErrorCode status
= U_ZERO_ERROR
;
867 MessageFormat
*mf
= new MessageFormat(pat
, status
);
868 failure(status
, "new MessageFormat");
870 UnicodeString PREFIX
[] = {
871 CharsToUnicodeString("'Vous' n'avez s\\u00E9lectionn\\u00E9 aucun clients personnels."),
872 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 "),
873 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 ")
875 UnicodeString SUFFIX
[] = {
877 UNICODE_STRING(" client personnel.", 18),
878 UNICODE_STRING(" clients personnels.", 20)
881 for (int i
=0; i
<3; i
++) {
883 //out = mf->format(new Object[]{new Integer(i)});
884 Formattable objs
[] = {
885 Formattable((int32_t)i
)
887 FieldPosition
pos(FieldPosition::DONT_CARE
);
888 out
= mf
->format(objs
, 1, out
, pos
, status
);
889 if (!failure(status
, "mf->format", TRUE
)) {
890 if (SUFFIX
[i
] == "") {
891 if (out
!= PREFIX
[i
])
892 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"");
895 if (!out
.startsWith(PREFIX
[i
]) ||
896 !out
.endsWith(SUFFIX
[i
]))
897 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"...\"" +
908 * Test the applyPattern and toPattern handling of single quotes
909 * by ChoiceFormat. (This is in here because this was a bug reported
910 * against MessageFormat.) The single quote is used to quote the
911 * pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row
912 * is a quote literal.
914 void MessageFormatRegressionTest::TestChoicePatternQuote()
916 // ICU 4.8 ChoiceFormat (like PluralFormat & SelectFormat)
917 // returns the chosen string unmodified, so that it is usable in a MessageFormat.
918 // We modified the test strings accordingly.
919 // Note: Without further formatting/trimming/etc., it is not possible
920 // to get a single apostrophe as the last character of a non-final choice sub-message
921 // because the single apostrophe before the pipe '|' would start quoted text.
922 // Normally, ChoiceFormat is used inside a MessageFormat, where a double apostrophe
923 // can be used in that case and will be formatted as a single one.
924 // (Better: Use a "real" apostrophe, U+2019.)
925 UnicodeString DATA
[] = {
926 // Pattern 0 value 1 value
927 // {sfb} hacked - changed \u2264 to = (copied from Character Map)
928 "0#can't|1#can", "can't", "can",
929 "0#pound(#)='#''|1#xyz", "pound(#)='#''", "xyz",
930 "0#1<2 '| 1=1'|1#'", "1<2 '| 1=1'", "'",
932 for (int i
=0; i
<9; i
+=3) {
934 UErrorCode status
= U_ZERO_ERROR
;
935 ChoiceFormat
*cf
= new ChoiceFormat(DATA
[i
], status
);
936 failure(status
, "new ChoiceFormat");
937 for (int j
=0; j
<=1; ++j
) {
939 FieldPosition
pos(FieldPosition::DONT_CARE
);
940 out
= cf
->format((double)j
, out
, pos
);
941 if (out
!= DATA
[i
+1+j
])
942 errln("Fail: Pattern \"" + DATA
[i
] + "\" x "+j
+" -> " +
943 out
+ "; want \"" + DATA
[i
+1+j
] + "\"");
946 pat
= cf
->toPattern(pat
);
948 ChoiceFormat
*cf2
= new ChoiceFormat(pat
, status
);
949 pat2
= cf2
->toPattern(pat2
);
951 errln("Fail: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ "\"");
953 logln("Ok: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ "\"");
955 catch (IllegalArgumentException e) {
956 errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
966 * MessageFormat.equals(null) throws a NullPointerException. The JLS states
967 * that it should return false.
969 void MessageFormatRegressionTest::Test4112104()
971 UErrorCode status
= U_ZERO_ERROR
;
972 MessageFormat
*format
= new MessageFormat("", status
);
973 failure(status
, "new MessageFormat");
975 // This should NOT throw an exception
976 if (format
== NULL
) {
977 // It also should return false
978 errln("MessageFormat.equals(null) returns false");
981 catch (NullPointerException e) {
982 errln("MessageFormat.equals(null) throws " + e);
987 void MessageFormatRegressionTest::TestAPI() {
988 UErrorCode status
= U_ZERO_ERROR
;
989 MessageFormat
*format
= new MessageFormat("", status
);
990 failure(status
, "new MessageFormat");
993 MessageFormat
*fmt
= new MessageFormat("",status
);
994 format
->adoptFormat("some_name",fmt
,status
); // Must at least pass a valid identifier.
995 failure(status
, "adoptFormat");
998 format
->setFormat((int32_t)0,*fmt
);
999 format
->getFormat("some_other_name",status
); // Must at least pass a valid identifier.
1000 failure(status
, "getFormat");
1004 #endif /* #if !UCONFIG_NO_FORMATTING */