]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/msfmrgts.cpp
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
6 * and others. All Rights Reserved.
7 ***********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_FORMATTING
15 #include "unicode/format.h"
16 #include "unicode/decimfmt.h"
17 #include "unicode/locid.h"
18 #include "unicode/msgfmt.h"
19 #include "unicode/numfmt.h"
20 #include "unicode/choicfmt.h"
21 #include "unicode/gregocal.h"
25 // *****************************************************************************
26 // class MessageFormatRegressionTest
27 // *****************************************************************************
29 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
32 MessageFormatRegressionTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
35 TESTCASE_AUTO(Test4074764
);
36 //TESTCASE_AUTO(Test4058973) -- disabled/obsolete in ICU 4.8
37 TESTCASE_AUTO(Test4031438
);
38 TESTCASE_AUTO(Test4052223
);
39 TESTCASE_AUTO(Test4104976
);
40 TESTCASE_AUTO(Test4106659
);
41 TESTCASE_AUTO(Test4106660
);
42 TESTCASE_AUTO(Test4111739
);
43 TESTCASE_AUTO(Test4114743
);
44 TESTCASE_AUTO(Test4116444
);
45 TESTCASE_AUTO(Test4114739
);
46 TESTCASE_AUTO(Test4113018
);
47 TESTCASE_AUTO(Test4106661
);
48 TESTCASE_AUTO(Test4094906
);
49 TESTCASE_AUTO(Test4118592
);
50 TESTCASE_AUTO(Test4118594
);
51 TESTCASE_AUTO(Test4105380
);
52 TESTCASE_AUTO(Test4120552
);
53 TESTCASE_AUTO(Test4142938
);
54 TESTCASE_AUTO(TestChoicePatternQuote
);
55 TESTCASE_AUTO(Test4112104
);
56 TESTCASE_AUTO(TestICU12584
);
57 TESTCASE_AUTO(TestAPI
);
62 MessageFormatRegressionTest::failure(UErrorCode status
, const char* msg
, UBool possibleDataError
)
64 if(U_FAILURE(status
)) {
65 if (possibleDataError
) {
66 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
68 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
77 * Null exception when formatting pattern with MessageFormat
80 void MessageFormatRegressionTest::Test4074764() {
81 UnicodeString pattern
[] = {
82 "Message without param",
83 "Message with param:{0}",
84 "Longer Message with param {0}"
86 //difference between the two param strings are that
87 //in the first one, the param position is within the
88 //length of the string without param while it is not so
91 UErrorCode status
= U_ZERO_ERROR
;
92 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
94 failure(status
, "couldn't create MessageFormat");
97 //Apply pattern with param and print the result
98 messageFormatter
->applyPattern(pattern
[1], status
);
99 failure(status
, "messageFormat->applyPattern");
100 //Object[] params = {new UnicodeString("BUG"), new Date()};
101 Formattable params
[] = {
102 Formattable(UnicodeString("BUG")),
103 Formattable(0, Formattable::kIsDate
)
105 UnicodeString tempBuffer
;
106 FieldPosition
pos(FieldPosition::DONT_CARE
);
107 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
108 if( tempBuffer
!= "Message with param:BUG" || failure(status
, "messageFormat->format"))
109 errln("MessageFormat with one param test failed.");
110 logln("Formatted with one extra param : " + tempBuffer
);
112 //Apply pattern without param and print the result
113 messageFormatter
->applyPattern(pattern
[0], status
);
114 failure(status
, "messageFormatter->applyPattern");
116 // {sfb} how much does this apply in C++?
117 // do we want to verify that the Formattable* array is not NULL,
118 // or is that the user's responsibility?
119 // additionally, what should be the item count?
120 // for bug testing purposes, assume that something was set to
121 // NULL by mistake, and that the length should be non-zero
123 //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
125 tempBuffer
= messageFormatter
->format(NULL
, 0, tempBuffer
, pos
, status
);
127 if( tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
128 errln("MessageFormat with no param test failed.");
129 logln("Formatted with no params : " + tempBuffer
);
132 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
133 if (tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
134 errln("Formatted with arguments > subsitution failed. result = " + tempBuffer
);
135 logln("Formatted with extra params : " + tempBuffer
);
136 //This statement gives an exception while formatting...
137 //If we use pattern[1] for the message with param,
138 //we get an NullPointerException in MessageFormat.java(617)
139 //If we use pattern[2] for the message with param,
140 //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
141 //Both are due to maxOffset not being reset to -1
142 //in applyPattern() when the pattern does not
144 /*} catch (Exception foo) {
145 errln("Exception when formatting with no params.");
148 delete messageFormatter
;
152 * MessageFormat.toPattern has weird rounding behavior.
154 * ICU 4.8: This test is commented out because toPattern() has been changed to return
155 * the original pattern string, rather than reconstituting a new (equivalent) one.
156 * This trivially eliminates issues with rounding or any other pattern string differences.
159 void MessageFormatRegressionTest::Test4058973()
161 UErrorCode status = U_ZERO_ERROR;
162 MessageFormat *fmt = new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}", status);
163 failure(status, "new MessageFormat");
166 pat = fmt->toPattern(pat);
167 UnicodeString exp("{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
169 errln("MessageFormat.toPattern failed");
170 errln("Exp: " + exp);
171 errln("Got: " + pat);
177 * More robust message formats.
179 void MessageFormatRegressionTest::Test4031438()
181 UErrorCode status
= U_ZERO_ERROR
;
183 UnicodeString
pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}.");
184 UnicodeString
pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.");
186 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
187 failure(status
, "new MessageFormat");
189 const UBool possibleDataError
= TRUE
;
192 logln("Apply with pattern : " + pattern1
);
193 messageFormatter
->applyPattern(pattern1
, status
);
194 failure(status
, "messageFormat->applyPattern");
195 //Object[] params = {new Integer(7)};
196 Formattable params
[]= {
197 Formattable((int32_t)7)
199 UnicodeString tempBuffer
;
200 FieldPosition
pos(FieldPosition::DONT_CARE
);
201 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
202 if(tempBuffer
!= "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status
, "MessageFormat::format"))
203 dataerrln("Tests arguments < substitution failed");
204 logln("Formatted with 7 : " + tempBuffer
);
207 Formattable
*objs
= messageFormatter
->parse(tempBuffer
, pp
, count
);
208 //if(objs[7/*params.length*/] != NULL)
209 // errln("Parse failed with more than expected arguments");
211 NumberFormat
*fmt
= 0;
212 UnicodeString temp
, temp1
;
214 for (int i
= 0; i
< count
; i
++) {
216 // convert to string if not already
217 Formattable obj
= objs
[i
];
219 if(obj
.getType() == Formattable::kString
)
220 temp
= obj
.getString(temp
);
222 fmt
= NumberFormat::createInstance(status
);
223 switch (obj
.getType()) {
224 case Formattable::kLong
: fmt
->format(obj
.getLong(), temp
); break;
225 case Formattable::kInt64
: fmt
->format(obj
.getInt64(), temp
); break;
226 case Formattable::kDouble
: fmt
->format(obj
.getDouble(), temp
); break;
231 // convert to string if not already
232 Formattable obj1
= params
[i
];
234 if(obj1
.getType() == Formattable::kString
)
235 temp1
= obj1
.getString(temp1
);
237 fmt
= NumberFormat::createInstance(status
);
238 switch (obj1
.getType()) {
239 case Formattable::kLong
: fmt
->format(obj1
.getLong(), temp1
); break;
240 case Formattable::kInt64
: fmt
->format(obj1
.getInt64(), temp1
); break;
241 case Formattable::kDouble
: fmt
->format(obj1
.getDouble(), temp1
); break;
246 //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
248 errln("Parse failed on object " + objs
[i
].getString(temp1
) + " at index : " + i
);
255 // {sfb} does this apply? no way to really pass a null Formattable,
258 /*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
259 if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format"))
260 errln("Tests with no arguments failed");
261 logln("Formatted with null : " + tempBuffer);*/
262 logln("Apply with pattern : " + pattern2
);
263 messageFormatter
->applyPattern(pattern2
, status
);
264 failure(status
, "messageFormatter->applyPattern", possibleDataError
);
266 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
267 if (tempBuffer
!= "Double ' Quotes 7 test and quoted {1} test plus 'other {2} stuff'.")
268 dataerrln("quote format test (w/ params) failed. - %s", u_errorName(status
));
269 logln("Formatted with params : " + tempBuffer
);
271 /*tempBuffer = messageFormatter->format(null);
272 if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
273 errln("quote format test (w/ null) failed.");
274 logln("Formatted with null : " + tempBuffer);
275 logln("toPattern : " + messageFormatter.toPattern());*/
276 /*} catch (Exception foo) {
277 errln("Exception when formatting in bug 4031438. "+foo.getMessage());
279 delete messageFormatter
;
282 void MessageFormatRegressionTest::Test4052223()
285 ParsePosition
pos(0);
286 if (pos
.getErrorIndex() != -1) {
287 errln("ParsePosition.getErrorIndex initialization failed.");
290 UErrorCode status
= U_ZERO_ERROR
;
291 MessageFormat
*fmt
= new MessageFormat("There are {0} apples growing on the {1} tree.", status
);
292 failure(status
, "new MessageFormat");
293 UnicodeString
str("There is one apple growing on the peach tree.");
296 fmt
->parse(str
, pos
, count
);
298 logln(UnicodeString("unparsable string , should fail at ") + pos
.getErrorIndex());
299 if (pos
.getErrorIndex() == -1)
300 errln("Bug 4052223 failed : parsing string " + str
);
301 pos
.setErrorIndex(4);
302 if (pos
.getErrorIndex() != 4)
303 errln(UnicodeString("setErrorIndex failed, got ") + pos
.getErrorIndex() + " instead of 4");
305 ChoiceFormat
*f
= new ChoiceFormat(
306 "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.", status
);
307 failure(status
, "new ChoiceFormat");
309 pos
.setErrorIndex(-1);
311 f
->parse("are negative", obj
, pos
);
312 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == -1.0)
313 errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos
.getErrorIndex());
315 pos
.setErrorIndex(-1);
316 f
->parse("are no or fraction ", obj
, pos
);
317 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == 0.0)
318 errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos
.getErrorIndex());
320 pos
.setErrorIndex(-1);
321 f
->parse("go postal", obj
, pos
);
322 if (pos
.getErrorIndex() == -1 && ! uprv_isNaN(obj
.getDouble()))
323 errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos
.getErrorIndex());
329 * ChoiceFormat.equals(null) throws NullPointerException
332 // {sfb} not really applicable in C++?? (kind of silly)
334 void MessageFormatRegressionTest::Test4104976()
336 double limits
[] = {1, 20};
337 UnicodeString formats
[] = {
338 UnicodeString("xyz"),
341 int32_t formats_length
= UPRV_LENGTHOF(formats
);
342 UErrorCode status
= U_ZERO_ERROR
;
343 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, formats_length
);
344 failure(status
, "new ChoiceFormat");
346 log("Compares to null is always false, returned : ");
347 logln(cf
== NULL
? "TRUE" : "FALSE");
348 /*} catch (Exception foo) {
349 errln("ChoiceFormat.equals(null) throws exception.");
356 * ChoiceFormat.ctor(double[], String[]) doesn't check
357 * whether lengths of input arrays are equal.
360 // {sfb} again, not really applicable in C++
362 void MessageFormatRegressionTest::Test4106659()
368 UnicodeString formats [] = {
371 ChoiceFormat *cf = NULL;
373 // cf = new ChoiceFormat(limits, formats, 3);
374 //} catch (Exception foo) {
375 // logln("ChoiceFormat constructor should check for the array lengths");
379 // errln(cf->format(5));
386 * ChoiceFormat.ctor(double[], String[]) allows unordered double array.
387 * This is not a bug, added javadoc to emphasize the use of limit
388 * array must be in ascending order.
390 void MessageFormatRegressionTest::Test4106660()
392 double limits
[] = {3, 1, 2};
393 UnicodeString formats
[] = {
394 UnicodeString("Three"),
395 UnicodeString("One"),
398 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, 3);
401 FieldPosition
pos(FieldPosition::DONT_CARE
);
402 str
= cf
->format(d
, str
, pos
);
404 errln( (UnicodeString
) "format(" + d
+ ") = " + str
);
410 * MessageFormat is incorrectly serialized/deserialized.
413 // {sfb} doesn't apply in C++
415 void MessageFormatRegressionTest::Test4111739()
417 /*MessageFormat format1 = null;
418 MessageFormat format2 = null;
419 ObjectOutputStream ostream = null;
420 ByteArrayOutputStream baos = null;
421 ObjectInputStream istream = null;
424 baos = new ByteArrayOutputStream();
425 ostream = new ObjectOutputStream(baos);
426 } catch(IOException e) {
427 errln("Unexpected exception : " + e.getMessage());
432 format1 = new MessageFormat("pattern{0}");
433 ostream.writeObject(format1);
436 byte bytes[] = baos.toByteArray();
438 istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
439 format2 = (MessageFormat)istream.readObject();
440 } catch(Exception e) {
441 errln("Unexpected exception : " + e.getMessage());
444 if (!format1.equals(format2)) {
445 errln("MessageFormats before and after serialization are not" +
446 " equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
447 format2 + "(" + format2.toPattern() + ")");
449 logln("Serialization for MessageFormat is OK.");
453 * MessageFormat.applyPattern allows illegal patterns.
455 void MessageFormatRegressionTest::Test4114743()
457 UnicodeString
originalPattern("initial pattern");
458 UErrorCode status
= U_ZERO_ERROR
;
459 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
460 failure(status
, "new MessageFormat");
462 UnicodeString
illegalPattern("ab { '}' de");
463 mf
->applyPattern(illegalPattern
, status
);
464 if( ! U_FAILURE(status
))
465 errln("illegal pattern: \"" + illegalPattern
+ "\"");
466 /*} catch (IllegalArgumentException foo) {
467 if (!originalPattern.equals(mf.toPattern()))
468 errln("pattern after: \"" + mf.toPattern() + "\"");
474 * MessageFormat.parse has different behavior in case of null.
476 void MessageFormatRegressionTest::Test4116444()
478 UnicodeString patterns
[] = {
480 (UnicodeString
)"one",
481 (UnicodeString
) "{0,date,short}"
484 UErrorCode status
= U_ZERO_ERROR
;
485 MessageFormat
*mf
= new MessageFormat("", status
);
486 failure(status
, "new MessageFormat");
488 for (int i
= 0; i
< 3; i
++) {
489 UnicodeString pattern
= patterns
[i
];
490 mf
->applyPattern(pattern
, status
);
491 failure(status
, "mf->applyPattern", TRUE
);
496 Formattable
*array
= mf
->parse(UnicodeString(""), pp
, count
);
497 logln("pattern: \"" + pattern
+ "\"");
498 log(" parsedObjects: ");
501 for (int j
= 0; j
< count
; j
++) {
502 //if (array[j] != null)
504 dataerrln("\"" + array
[j
].getString(dummy
) + "\"");
516 /*} catch (Exception e) {
517 errln("pattern: \"" + pattern + "\"");
518 errln(" Exception: " + e.getMessage());
524 /* @bug 4114739 (FIX and add javadoc)
525 * MessageFormat.format has undocumented behavior about empty format objects.
528 // {sfb} doesn't apply in C++?
529 void MessageFormatRegressionTest::Test4114739()
532 UErrorCode status
= U_ZERO_ERROR
;
533 MessageFormat
*mf
= new MessageFormat("<{0}>", status
);
534 failure(status
, "new MessageFormat");
536 Formattable
*objs1
= NULL
;
537 //Formattable objs2 [] = {};
538 //Formattable *objs3 [] = {NULL};
542 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
543 log("format(null) : ");
544 FieldPosition
pos(FieldPosition::DONT_CARE
);
545 logln("\"" + mf
->format(objs1
, 0, res
, pos
, status
) + "\"");
546 failure(status
, "mf->format");
547 /*log("format({}) : ");
548 logln("\"" + mf->format(objs2, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
549 failure(status, "mf->format");
550 log("format({null}) :");
551 logln("\"" + mf->format(objs3, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
552 failure(status, "mf->format");*/
553 /*} catch (Exception e) {
554 errln("Exception thrown for null argument tests.");
561 * MessageFormat.applyPattern works wrong with illegal patterns.
563 void MessageFormatRegressionTest::Test4113018()
565 UnicodeString
originalPattern("initial pattern");
566 UErrorCode status
= U_ZERO_ERROR
;
567 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
568 failure(status
, "new messageFormat");
569 UnicodeString
illegalPattern("format: {0, xxxYYY}");
571 logln("pattern before: \"" + mf
->toPattern(pat
) + "\"");
572 logln("illegal pattern: \"" + illegalPattern
+ "\"");
574 mf
->applyPattern(illegalPattern
, status
);
575 if( ! U_FAILURE(status
))
576 errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern
);
577 /*} catch (IllegalArgumentException e) {
578 if (!originalPattern.equals(mf.toPattern()))
579 errln("pattern after: \"" + mf.toPattern() + "\"");
585 * ChoiceFormat is silent about the pattern usage in javadoc.
587 void MessageFormatRegressionTest::Test4106661()
589 UErrorCode status
= U_ZERO_ERROR
;
590 ChoiceFormat
*fmt
= new ChoiceFormat(
591 "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status
);
592 failure(status
, "new ChoiceFormat");
594 logln("Formatter Pattern : " + fmt
->toPattern(pat
));
596 FieldPosition
bogus(FieldPosition::DONT_CARE
);
599 // Will this work for -inf?
600 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
601 failure(status
, "fmt->format");
603 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
604 failure(status
, "fmt->format");
606 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
607 failure(status
, "fmt->format");
609 logln("Format with 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
610 failure(status
, "fmt->format");
612 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), str
, bogus
, status
));
613 failure(status
, "fmt->format");
615 logln("Format with 1.0 : " + fmt
->format(Formattable(1.0), str
, bogus
, status
));
616 failure(status
, "fmt->format");
618 logln("Format with 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
619 failure(status
, "fmt->format");
621 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
622 failure(status
, "fmt->format");
624 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
625 failure(status
, "fmt->format");
627 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
628 failure(status
, "fmt->format");
630 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
631 failure(status
, "fmt->format");
637 * ChoiceFormat should accept \u221E as eq. to INF.
639 void MessageFormatRegressionTest::Test4094906()
641 UErrorCode status
= U_ZERO_ERROR
;
642 UnicodeString
pattern("-");
643 pattern
+= (UChar
) 0x221E;
644 pattern
+= "<are negative|0<are no or fraction|1#is one|1<is 1+|";
645 pattern
+= (UChar
) 0x221E;
646 pattern
+= "<are many.";
648 ChoiceFormat
*fmt
= new ChoiceFormat(pattern
, status
);
649 failure(status
, "new ChoiceFormat");
651 if (fmt
->toPattern(pat
) != pattern
) {
652 errln( (UnicodeString
) "Formatter Pattern : " + pat
);
653 errln( (UnicodeString
) "Expected Pattern : " + pattern
);
655 FieldPosition
bogus(FieldPosition::DONT_CARE
);
658 // Will this work for -inf?
659 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
660 failure(status
, "fmt->format");
662 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
663 failure(status
, "fmt->format");
665 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
666 failure(status
, "fmt->format");
668 logln("Format with 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
669 failure(status
, "fmt->format");
671 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), str
, bogus
, status
));
672 failure(status
, "fmt->format");
674 logln("Format with 1.0 : " + fmt
->format(Formattable(1.0), str
, bogus
, status
));
675 failure(status
, "fmt->format");
677 logln("Format with 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
678 failure(status
, "fmt->format");
680 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
681 failure(status
, "fmt->format");
683 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
684 failure(status
, "fmt->format");
686 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
687 failure(status
, "fmt->format");
689 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
690 failure(status
, "fmt->format");
696 * MessageFormat.parse fails with ChoiceFormat.
698 void MessageFormatRegressionTest::Test4118592()
700 UErrorCode status
= U_ZERO_ERROR
;
701 MessageFormat
*mf
= new MessageFormat("", status
);
702 failure(status
, "new messageFormat");
703 UnicodeString
pattern("{0,choice,1#YES|2#NO}");
704 UnicodeString
prefix("");
705 Formattable
*objs
= 0;
707 for (int i
= 0; i
< 5; i
++) {
708 UnicodeString formatted
;
709 formatted
= prefix
+ "YES";
710 mf
->applyPattern(prefix
+ pattern
, status
);
711 failure(status
, "mf->applyPattern");
713 //Object[] objs = mf.parse(formatted, new ParsePosition(0));
716 objs
= mf
->parse(formatted
, pp
, count
);
718 logln(UnicodeString("") + i
+ ". pattern :\"" + mf
->toPattern(pat
) + "\"");
719 log(" \"" + formatted
+ "\" parsed as ");
724 if(objs
[0].getType() == Formattable::kString
)
725 logln((UnicodeString
)" " + objs
[0].getString(temp
));
727 logln((UnicodeString
)" " + (objs
[0].getType() == Formattable::kLong
? objs
[0].getLong() : objs
[0].getDouble()));
736 * MessageFormat.parse fails for some patterns.
738 void MessageFormatRegressionTest::Test4118594()
740 UErrorCode status
= U_ZERO_ERROR
;
741 const UBool possibleDataError
= TRUE
;
742 MessageFormat
*mf
= new MessageFormat("{0}, {0}, {0}", status
);
743 failure(status
, "new MessageFormat");
744 UnicodeString
forParsing("x, y, z");
745 //Object[] objs = mf.parse(forParsing, new ParsePosition(0));
748 Formattable
*objs
= mf
->parse(forParsing
, pp
, count
);
750 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
751 logln("text for parsing: \"" + forParsing
+ "\"");
753 if (objs
[0].getString(str
) != "z")
754 errln("argument0: \"" + objs
[0].getString(str
) + "\"");
755 mf
->applyPattern("{0,number,#.##}, {0,number,#.#}", status
);
756 failure(status
, "mf->applyPattern", possibleDataError
);
757 //Object[] oldobjs = {new Double(3.1415)};
758 Formattable oldobjs
[] = {Formattable(3.1415)};
759 UnicodeString result
;
760 FieldPosition
pos(FieldPosition::DONT_CARE
);
761 result
= mf
->format( oldobjs
, 1, result
, pos
, status
);
762 failure(status
, "mf->format", possibleDataError
);
764 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
765 logln("text for parsing: \"" + result
+ "\"");
766 // result now equals "3.14, 3.1"
767 if (result
!= "3.14, 3.1")
768 dataerrln("result = " + result
+ " - " + u_errorName(status
));
769 //Object[] newobjs = mf.parse(result, new ParsePosition(0));
772 Formattable
*newobjs
= mf
->parse(result
, pp
, count1
);
773 // newobjs now equals {new Double(3.1)}
774 if (newobjs
== NULL
) {
775 dataerrln("Error calling MessageFormat::parse");
777 if (newobjs
[0].getDouble() != 3.1)
778 errln( UnicodeString("newobjs[0] = ") + newobjs
[0].getDouble());
786 * When using ChoiceFormat, MessageFormat is not good for I18n.
788 void MessageFormatRegressionTest::Test4105380()
790 UnicodeString
patternText1("The disk \"{1}\" contains {0}.");
791 UnicodeString
patternText2("There are {0} on the disk \"{1}\"");
792 UErrorCode status
= U_ZERO_ERROR
;
793 const UBool possibleDataError
= TRUE
;
794 MessageFormat
*form1
= new MessageFormat(patternText1
, status
);
795 failure(status
, "new MessageFormat");
796 MessageFormat
*form2
= new MessageFormat(patternText2
, status
);
797 failure(status
, "new MessageFormat");
798 double filelimits
[] = {0,1,2};
799 UnicodeString filepart
[] = {
800 (UnicodeString
)"no files",
801 (UnicodeString
)"one file",
802 (UnicodeString
)"{0,number} files"
804 ChoiceFormat
*fileform
= new ChoiceFormat(filelimits
, filepart
, 3);
805 form1
->setFormat(1, *fileform
);
806 form2
->setFormat(0, *fileform
);
807 //Object[] testArgs = {new Long(12373), "MyDisk"};
808 Formattable testArgs
[] = {
809 Formattable((int32_t)12373),
810 Formattable((UnicodeString
)"MyDisk")
813 FieldPosition
bogus(FieldPosition::DONT_CARE
);
815 UnicodeString result
;
816 logln(form1
->format(testArgs
, 2, result
, bogus
, status
));
817 failure(status
, "form1->format", possibleDataError
);
819 logln(form2
->format(testArgs
, 2, result
, bogus
, status
));
820 failure(status
, "form1->format", possibleDataError
);
827 * MessageFormat.parse incorrectly sets errorIndex.
829 void MessageFormatRegressionTest::Test4120552()
831 UErrorCode status
= U_ZERO_ERROR
;
832 MessageFormat
*mf
= new MessageFormat("pattern", status
);
833 failure(status
, "new MessageFormat");
834 UnicodeString texts
[] = {
835 (UnicodeString
)"pattern",
836 (UnicodeString
)"pat",
837 (UnicodeString
)"1234"
840 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
841 for (int i
= 0; i
< 3; i
++) {
843 //Object[] objs = mf.parse(texts[i], pp);
845 Formattable
*objs
= mf
->parse(texts
[i
], pp
, count
);
846 log(" text for parsing: \"" + texts
[i
] + "\"");
848 logln(" (incorrectly formatted string)");
849 if (pp
.getErrorIndex() == -1)
850 errln(UnicodeString("Incorrect error index: ") + pp
.getErrorIndex());
852 logln(" (correctly formatted string)");
861 * MessageFormat handles single quotes in pattern wrong.
862 * This is actually a problem in ChoiceFormat; it doesn't
863 * understand single quotes.
865 void MessageFormatRegressionTest::Test4142938()
867 UnicodeString pat
= CharsToUnicodeString("''Vous'' {0,choice,0#n''|1#}avez s\\u00E9lectionn\\u00E9 "
868 "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} "
869 "personnel{0,choice,0#s|1#|2#s}.");
870 UErrorCode status
= U_ZERO_ERROR
;
871 MessageFormat
*mf
= new MessageFormat(pat
, status
);
872 failure(status
, "new MessageFormat");
874 UnicodeString PREFIX
[] = {
875 CharsToUnicodeString("'Vous' n'avez s\\u00E9lectionn\\u00E9 aucun clients personnels."),
876 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 "),
877 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 ")
879 UnicodeString SUFFIX
[] = {
881 UNICODE_STRING(" client personnel.", 18),
882 UNICODE_STRING(" clients personnels.", 20)
885 for (int i
=0; i
<3; i
++) {
887 //out = mf->format(new Object[]{new Integer(i)});
888 Formattable objs
[] = {
889 Formattable((int32_t)i
)
891 FieldPosition
pos(FieldPosition::DONT_CARE
);
892 out
= mf
->format(objs
, 1, out
, pos
, status
);
893 if (!failure(status
, "mf->format", TRUE
)) {
894 if (SUFFIX
[i
] == "") {
895 if (out
!= PREFIX
[i
])
896 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"");
899 if (!out
.startsWith(PREFIX
[i
]) ||
900 !out
.endsWith(SUFFIX
[i
]))
901 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"...\"" +
912 * Test the applyPattern and toPattern handling of single quotes
913 * by ChoiceFormat. (This is in here because this was a bug reported
914 * against MessageFormat.) The single quote is used to quote the
915 * pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row
916 * is a quote literal.
918 void MessageFormatRegressionTest::TestChoicePatternQuote()
920 // ICU 4.8 ChoiceFormat (like PluralFormat & SelectFormat)
921 // returns the chosen string unmodified, so that it is usable in a MessageFormat.
922 // We modified the test strings accordingly.
923 // Note: Without further formatting/trimming/etc., it is not possible
924 // to get a single apostrophe as the last character of a non-final choice sub-message
925 // because the single apostrophe before the pipe '|' would start quoted text.
926 // Normally, ChoiceFormat is used inside a MessageFormat, where a double apostrophe
927 // can be used in that case and will be formatted as a single one.
928 // (Better: Use a "real" apostrophe, U+2019.)
929 UnicodeString DATA
[] = {
930 // Pattern 0 value 1 value
931 // {sfb} hacked - changed \u2264 to = (copied from Character Map)
932 "0#can't|1#can", "can't", "can",
933 "0#pound(#)='#''|1#xyz", "pound(#)='#''", "xyz",
934 "0#1<2 '| 1=1'|1#'", "1<2 '| 1=1'", "'",
936 for (int i
=0; i
<9; i
+=3) {
938 UErrorCode status
= U_ZERO_ERROR
;
939 ChoiceFormat
*cf
= new ChoiceFormat(DATA
[i
], status
);
940 failure(status
, "new ChoiceFormat");
941 for (int j
=0; j
<=1; ++j
) {
943 FieldPosition
pos(FieldPosition::DONT_CARE
);
944 out
= cf
->format((double)j
, out
, pos
);
945 if (out
!= DATA
[i
+1+j
])
946 errln("Fail: Pattern \"" + DATA
[i
] + "\" x "+j
+" -> " +
947 out
+ "; want \"" + DATA
[i
+1+j
] + "\"");
950 pat
= cf
->toPattern(pat
);
952 ChoiceFormat
*cf2
= new ChoiceFormat(pat
, status
);
953 pat2
= cf2
->toPattern(pat2
);
955 errln("Fail: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ "\"");
957 logln("Ok: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ "\"");
959 catch (IllegalArgumentException e) {
960 errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
970 * MessageFormat.equals(null) throws a NullPointerException. The JLS states
971 * that it should return false.
973 void MessageFormatRegressionTest::Test4112104()
975 UErrorCode status
= U_ZERO_ERROR
;
976 MessageFormat
*format
= new MessageFormat("", status
);
977 failure(status
, "new MessageFormat");
979 // This should NOT throw an exception
980 if (format
== NULL
) {
981 // It also should return false
982 errln("MessageFormat.equals(null) returns false");
985 catch (NullPointerException e) {
986 errln("MessageFormat.equals(null) throws " + e);
991 void MessageFormatRegressionTest::TestICU12584() {
992 // We were able to handle only 10 due to the bug ICU-12584.
993 UnicodeString
pattern(
994 u
"{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}"
995 u
"{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}{1}{2}{3}");
997 UErrorCode status
= U_ZERO_ERROR
;
999 MessageFormat
msg(pattern
, status
);
1000 failure(status
, "Flat message");
1003 msg
.getFormats(count
);
1004 assertEquals("Plain placeholder match", 42, count
);
1006 // Make sure we iterate only over top level arguments (so we don't over allocate).
1007 UnicodeString
inner_pattern("{1}{1,select,fem {1} masc {2} other {3}}{2}");
1008 status
= U_ZERO_ERROR
;
1009 MessageFormat
inner_msg(inner_pattern
, status
);
1010 failure(status
, "Inner message");
1013 inner_msg
.getFormats(count
);
1014 assertEquals("Inner placeholder match", 3, count
);
1017 void MessageFormatRegressionTest::TestAPI() {
1018 UErrorCode status
= U_ZERO_ERROR
;
1019 MessageFormat
*format
= new MessageFormat("", status
);
1020 failure(status
, "new MessageFormat");
1023 MessageFormat
*fmt
= new MessageFormat("",status
);
1024 format
->adoptFormat("some_name",fmt
,status
); // Must at least pass a valid identifier.
1025 failure(status
, "adoptFormat");
1028 format
->setFormat((int32_t)0,*fmt
);
1029 format
->getFormat("some_other_name",status
); // Must at least pass a valid identifier.
1030 failure(status
, "getFormat");
1034 #endif /* #if !UCONFIG_NO_FORMATTING */