]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/msfmrgts.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2016, 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"
23 // *****************************************************************************
24 // class MessageFormatRegressionTest
25 // *****************************************************************************
27 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
30 MessageFormatRegressionTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
33 TESTCASE_AUTO(Test4074764
)
34 //TESTCASE_AUTO(Test4058973) -- disabled/obsolete in ICU 4.8
35 TESTCASE_AUTO(Test4031438
)
36 TESTCASE_AUTO(Test4052223
)
37 TESTCASE_AUTO(Test4104976
)
38 TESTCASE_AUTO(Test4106659
)
39 TESTCASE_AUTO(Test4106660
)
40 TESTCASE_AUTO(Test4111739
)
41 TESTCASE_AUTO(Test4114743
)
42 TESTCASE_AUTO(Test4116444
)
43 TESTCASE_AUTO(Test4114739
)
44 TESTCASE_AUTO(Test4113018
)
45 TESTCASE_AUTO(Test4106661
)
46 TESTCASE_AUTO(Test4094906
)
47 TESTCASE_AUTO(Test4118592
)
48 TESTCASE_AUTO(Test4118594
)
49 TESTCASE_AUTO(Test4105380
)
50 TESTCASE_AUTO(Test4120552
)
51 TESTCASE_AUTO(Test4142938
)
52 TESTCASE_AUTO(TestChoicePatternQuote
)
53 TESTCASE_AUTO(Test4112104
)
54 TESTCASE_AUTO(TestAPI
)
59 MessageFormatRegressionTest::failure(UErrorCode status
, const char* msg
, UBool possibleDataError
)
61 if(U_FAILURE(status
)) {
62 if (possibleDataError
) {
63 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
65 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
74 * Null exception when formatting pattern with MessageFormat
77 void MessageFormatRegressionTest::Test4074764() {
78 UnicodeString pattern
[] = {
79 "Message without param",
80 "Message with param:{0}",
81 "Longer Message with param {0}"
83 //difference between the two param strings are that
84 //in the first one, the param position is within the
85 //length of the string without param while it is not so
88 UErrorCode status
= U_ZERO_ERROR
;
89 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
91 failure(status
, "couldn't create MessageFormat");
94 //Apply pattern with param and print the result
95 messageFormatter
->applyPattern(pattern
[1], status
);
96 failure(status
, "messageFormat->applyPattern");
97 //Object[] params = {new UnicodeString("BUG"), new Date()};
98 Formattable params
[] = {
99 Formattable(UnicodeString("BUG")),
100 Formattable(0, Formattable::kIsDate
)
102 UnicodeString tempBuffer
;
103 FieldPosition
pos(FieldPosition::DONT_CARE
);
104 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
105 if( tempBuffer
!= "Message with param:BUG" || failure(status
, "messageFormat->format"))
106 errln("MessageFormat with one param test failed.");
107 logln("Formatted with one extra param : " + tempBuffer
);
109 //Apply pattern without param and print the result
110 messageFormatter
->applyPattern(pattern
[0], status
);
111 failure(status
, "messageFormatter->applyPattern");
113 // {sfb} how much does this apply in C++?
114 // do we want to verify that the Formattable* array is not NULL,
115 // or is that the user's responsibility?
116 // additionally, what should be the item count?
117 // for bug testing purposes, assume that something was set to
118 // NULL by mistake, and that the length should be non-zero
120 //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
122 tempBuffer
= messageFormatter
->format(NULL
, 0, tempBuffer
, pos
, status
);
124 if( tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
125 errln("MessageFormat with no param test failed.");
126 logln("Formatted with no params : " + tempBuffer
);
129 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
130 if (tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
131 errln("Formatted with arguments > subsitution failed. result = " + tempBuffer
);
132 logln("Formatted with extra params : " + tempBuffer
);
133 //This statement gives an exception while formatting...
134 //If we use pattern[1] for the message with param,
135 //we get an NullPointerException in MessageFormat.java(617)
136 //If we use pattern[2] for the message with param,
137 //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
138 //Both are due to maxOffset not being reset to -1
139 //in applyPattern() when the pattern does not
141 /*} catch (Exception foo) {
142 errln("Exception when formatting with no params.");
145 delete messageFormatter
;
149 * MessageFormat.toPattern has weird rounding behavior.
151 * ICU 4.8: This test is commented out because toPattern() has been changed to return
152 * the original pattern string, rather than reconstituting a new (equivalent) one.
153 * This trivially eliminates issues with rounding or any other pattern string differences.
156 void MessageFormatRegressionTest::Test4058973()
158 UErrorCode status = U_ZERO_ERROR;
159 MessageFormat *fmt = new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}", status);
160 failure(status, "new MessageFormat");
163 pat = fmt->toPattern(pat);
164 UnicodeString exp("{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
166 errln("MessageFormat.toPattern failed");
167 errln("Exp: " + exp);
168 errln("Got: " + pat);
174 * More robust message formats.
176 void MessageFormatRegressionTest::Test4031438()
178 UErrorCode status
= U_ZERO_ERROR
;
180 UnicodeString
pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}.");
181 UnicodeString
pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.");
183 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
184 failure(status
, "new MessageFormat");
186 const UBool possibleDataError
= TRUE
;
189 logln("Apply with pattern : " + pattern1
);
190 messageFormatter
->applyPattern(pattern1
, status
);
191 failure(status
, "messageFormat->applyPattern");
192 //Object[] params = {new Integer(7)};
193 Formattable params
[]= {
194 Formattable((int32_t)7)
196 UnicodeString tempBuffer
;
197 FieldPosition
pos(FieldPosition::DONT_CARE
);
198 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
199 if(tempBuffer
!= "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status
, "MessageFormat::format"))
200 dataerrln("Tests arguments < substitution failed");
201 logln("Formatted with 7 : " + tempBuffer
);
204 Formattable
*objs
= messageFormatter
->parse(tempBuffer
, pp
, count
);
205 //if(objs[7/*params.length*/] != NULL)
206 // errln("Parse failed with more than expected arguments");
208 NumberFormat
*fmt
= 0;
209 UnicodeString temp
, temp1
;
211 for (int i
= 0; i
< count
; i
++) {
213 // convert to string if not already
214 Formattable obj
= objs
[i
];
216 if(obj
.getType() == Formattable::kString
)
217 temp
= obj
.getString(temp
);
219 fmt
= NumberFormat::createInstance(status
);
220 switch (obj
.getType()) {
221 case Formattable::kLong
: fmt
->format(obj
.getLong(), temp
); break;
222 case Formattable::kInt64
: fmt
->format(obj
.getInt64(), temp
); break;
223 case Formattable::kDouble
: fmt
->format(obj
.getDouble(), temp
); break;
228 // convert to string if not already
229 Formattable obj1
= params
[i
];
231 if(obj1
.getType() == Formattable::kString
)
232 temp1
= obj1
.getString(temp1
);
234 fmt
= NumberFormat::createInstance(status
);
235 switch (obj1
.getType()) {
236 case Formattable::kLong
: fmt
->format(obj1
.getLong(), temp1
); break;
237 case Formattable::kInt64
: fmt
->format(obj1
.getInt64(), temp1
); break;
238 case Formattable::kDouble
: fmt
->format(obj1
.getDouble(), temp1
); break;
243 //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
245 errln("Parse failed on object " + objs
[i
].getString(temp1
) + " at index : " + i
);
252 // {sfb} does this apply? no way to really pass a null Formattable,
255 /*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
256 if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format"))
257 errln("Tests with no arguments failed");
258 logln("Formatted with null : " + tempBuffer);*/
259 logln("Apply with pattern : " + pattern2
);
260 messageFormatter
->applyPattern(pattern2
, status
);
261 failure(status
, "messageFormatter->applyPattern", possibleDataError
);
263 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
264 if (tempBuffer
!= "Double ' Quotes 7 test and quoted {1} test plus 'other {2} stuff'.")
265 dataerrln("quote format test (w/ params) failed. - %s", u_errorName(status
));
266 logln("Formatted with params : " + tempBuffer
);
268 /*tempBuffer = messageFormatter->format(null);
269 if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
270 errln("quote format test (w/ null) failed.");
271 logln("Formatted with null : " + tempBuffer);
272 logln("toPattern : " + messageFormatter.toPattern());*/
273 /*} catch (Exception foo) {
274 errln("Exception when formatting in bug 4031438. "+foo.getMessage());
276 delete messageFormatter
;
279 void MessageFormatRegressionTest::Test4052223()
282 ParsePosition
pos(0);
283 if (pos
.getErrorIndex() != -1) {
284 errln("ParsePosition.getErrorIndex initialization failed.");
287 UErrorCode status
= U_ZERO_ERROR
;
288 MessageFormat
*fmt
= new MessageFormat("There are {0} apples growing on the {1} tree.", status
);
289 failure(status
, "new MessageFormat");
290 UnicodeString
str("There is one apple growing on the peach tree.");
293 fmt
->parse(str
, pos
, count
);
295 logln(UnicodeString("unparsable string , should fail at ") + pos
.getErrorIndex());
296 if (pos
.getErrorIndex() == -1)
297 errln("Bug 4052223 failed : parsing string " + str
);
298 pos
.setErrorIndex(4);
299 if (pos
.getErrorIndex() != 4)
300 errln(UnicodeString("setErrorIndex failed, got ") + pos
.getErrorIndex() + " instead of 4");
302 ChoiceFormat
*f
= new ChoiceFormat(
303 "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.", status
);
304 failure(status
, "new ChoiceFormat");
306 pos
.setErrorIndex(-1);
308 f
->parse("are negative", obj
, pos
);
309 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == -1.0)
310 errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos
.getErrorIndex());
312 pos
.setErrorIndex(-1);
313 f
->parse("are no or fraction ", obj
, pos
);
314 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == 0.0)
315 errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos
.getErrorIndex());
317 pos
.setErrorIndex(-1);
318 f
->parse("go postal", obj
, pos
);
319 if (pos
.getErrorIndex() == -1 && ! uprv_isNaN(obj
.getDouble()))
320 errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos
.getErrorIndex());
326 * ChoiceFormat.equals(null) throws NullPointerException
329 // {sfb} not really applicable in C++?? (kind of silly)
331 void MessageFormatRegressionTest::Test4104976()
333 double limits
[] = {1, 20};
334 UnicodeString formats
[] = {
335 UnicodeString("xyz"),
338 int32_t formats_length
= UPRV_LENGTHOF(formats
);
339 UErrorCode status
= U_ZERO_ERROR
;
340 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, formats_length
);
341 failure(status
, "new ChoiceFormat");
343 log("Compares to null is always false, returned : ");
344 logln(cf
== NULL
? "TRUE" : "FALSE");
345 /*} catch (Exception foo) {
346 errln("ChoiceFormat.equals(null) throws exception.");
353 * ChoiceFormat.ctor(double[], String[]) doesn't check
354 * whether lengths of input arrays are equal.
357 // {sfb} again, not really applicable in C++
359 void MessageFormatRegressionTest::Test4106659()
365 UnicodeString formats [] = {
368 ChoiceFormat *cf = NULL;
370 // cf = new ChoiceFormat(limits, formats, 3);
371 //} catch (Exception foo) {
372 // logln("ChoiceFormat constructor should check for the array lengths");
376 // errln(cf->format(5));
383 * ChoiceFormat.ctor(double[], String[]) allows unordered double array.
384 * This is not a bug, added javadoc to emphasize the use of limit
385 * array must be in ascending order.
387 void MessageFormatRegressionTest::Test4106660()
389 double limits
[] = {3, 1, 2};
390 UnicodeString formats
[] = {
391 UnicodeString("Three"),
392 UnicodeString("One"),
395 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, 3);
398 FieldPosition
pos(FieldPosition::DONT_CARE
);
399 str
= cf
->format(d
, str
, pos
);
401 errln( (UnicodeString
) "format(" + d
+ ") = " + str
);
407 * MessageFormat is incorrectly serialized/deserialized.
410 // {sfb} doesn't apply in C++
412 void MessageFormatRegressionTest::Test4111739()
414 /*MessageFormat format1 = null;
415 MessageFormat format2 = null;
416 ObjectOutputStream ostream = null;
417 ByteArrayOutputStream baos = null;
418 ObjectInputStream istream = null;
421 baos = new ByteArrayOutputStream();
422 ostream = new ObjectOutputStream(baos);
423 } catch(IOException e) {
424 errln("Unexpected exception : " + e.getMessage());
429 format1 = new MessageFormat("pattern{0}");
430 ostream.writeObject(format1);
433 byte bytes[] = baos.toByteArray();
435 istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
436 format2 = (MessageFormat)istream.readObject();
437 } catch(Exception e) {
438 errln("Unexpected exception : " + e.getMessage());
441 if (!format1.equals(format2)) {
442 errln("MessageFormats before and after serialization are not" +
443 " equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
444 format2 + "(" + format2.toPattern() + ")");
446 logln("Serialization for MessageFormat is OK.");
450 * MessageFormat.applyPattern allows illegal patterns.
452 void MessageFormatRegressionTest::Test4114743()
454 UnicodeString
originalPattern("initial pattern");
455 UErrorCode status
= U_ZERO_ERROR
;
456 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
457 failure(status
, "new MessageFormat");
459 UnicodeString
illegalPattern("ab { '}' de");
460 mf
->applyPattern(illegalPattern
, status
);
461 if( ! U_FAILURE(status
))
462 errln("illegal pattern: \"" + illegalPattern
+ "\"");
463 /*} catch (IllegalArgumentException foo) {
464 if (!originalPattern.equals(mf.toPattern()))
465 errln("pattern after: \"" + mf.toPattern() + "\"");
471 * MessageFormat.parse has different behavior in case of null.
473 void MessageFormatRegressionTest::Test4116444()
475 UnicodeString patterns
[] = {
477 (UnicodeString
)"one",
478 (UnicodeString
) "{0,date,short}"
481 UErrorCode status
= U_ZERO_ERROR
;
482 MessageFormat
*mf
= new MessageFormat("", status
);
483 failure(status
, "new MessageFormat");
485 for (int i
= 0; i
< 3; i
++) {
486 UnicodeString pattern
= patterns
[i
];
487 mf
->applyPattern(pattern
, status
);
488 failure(status
, "mf->applyPattern", TRUE
);
493 Formattable
*array
= mf
->parse(UnicodeString(""), pp
, count
);
494 logln("pattern: \"" + pattern
+ "\"");
495 log(" parsedObjects: ");
498 for (int j
= 0; j
< count
; j
++) {
499 //if (array[j] != null)
501 dataerrln("\"" + array
[j
].getString(dummy
) + "\"");
513 /*} catch (Exception e) {
514 errln("pattern: \"" + pattern + "\"");
515 errln(" Exception: " + e.getMessage());
521 /* @bug 4114739 (FIX and add javadoc)
522 * MessageFormat.format has undocumented behavior about empty format objects.
525 // {sfb} doesn't apply in C++?
526 void MessageFormatRegressionTest::Test4114739()
529 UErrorCode status
= U_ZERO_ERROR
;
530 MessageFormat
*mf
= new MessageFormat("<{0}>", status
);
531 failure(status
, "new MessageFormat");
533 Formattable
*objs1
= NULL
;
534 //Formattable objs2 [] = {};
535 //Formattable *objs3 [] = {NULL};
539 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
540 log("format(null) : ");
541 FieldPosition
pos(FieldPosition::DONT_CARE
);
542 logln("\"" + mf
->format(objs1
, 0, res
, pos
, status
) + "\"");
543 failure(status
, "mf->format");
544 /*log("format({}) : ");
545 logln("\"" + mf->format(objs2, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
546 failure(status, "mf->format");
547 log("format({null}) :");
548 logln("\"" + mf->format(objs3, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
549 failure(status, "mf->format");*/
550 /*} catch (Exception e) {
551 errln("Exception thrown for null argument tests.");
558 * MessageFormat.applyPattern works wrong with illegal patterns.
560 void MessageFormatRegressionTest::Test4113018()
562 UnicodeString
originalPattern("initial pattern");
563 UErrorCode status
= U_ZERO_ERROR
;
564 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
565 failure(status
, "new messageFormat");
566 UnicodeString
illegalPattern("format: {0, xxxYYY}");
568 logln("pattern before: \"" + mf
->toPattern(pat
) + "\"");
569 logln("illegal pattern: \"" + illegalPattern
+ "\"");
571 mf
->applyPattern(illegalPattern
, status
);
572 if( ! U_FAILURE(status
))
573 errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern
);
574 /*} catch (IllegalArgumentException e) {
575 if (!originalPattern.equals(mf.toPattern()))
576 errln("pattern after: \"" + mf.toPattern() + "\"");
582 * ChoiceFormat is silent about the pattern usage in javadoc.
584 void MessageFormatRegressionTest::Test4106661()
586 UErrorCode status
= U_ZERO_ERROR
;
587 ChoiceFormat
*fmt
= new ChoiceFormat(
588 "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status
);
589 failure(status
, "new ChoiceFormat");
591 logln("Formatter Pattern : " + fmt
->toPattern(pat
));
593 FieldPosition
bogus(FieldPosition::DONT_CARE
);
596 // Will this work for -inf?
597 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
598 failure(status
, "fmt->format");
600 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), 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 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
607 failure(status
, "fmt->format");
609 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), str
, bogus
, status
));
610 failure(status
, "fmt->format");
612 logln("Format with 1.0 : " + fmt
->format(Formattable(1.0), str
, bogus
, status
));
613 failure(status
, "fmt->format");
615 logln("Format with 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
616 failure(status
, "fmt->format");
618 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
619 failure(status
, "fmt->format");
621 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
622 failure(status
, "fmt->format");
624 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
625 failure(status
, "fmt->format");
627 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
628 failure(status
, "fmt->format");
634 * ChoiceFormat should accept \u221E as eq. to INF.
636 void MessageFormatRegressionTest::Test4094906()
638 UErrorCode status
= U_ZERO_ERROR
;
639 UnicodeString
pattern("-");
640 pattern
+= (UChar
) 0x221E;
641 pattern
+= "<are negative|0<are no or fraction|1#is one|1<is 1+|";
642 pattern
+= (UChar
) 0x221E;
643 pattern
+= "<are many.";
645 ChoiceFormat
*fmt
= new ChoiceFormat(pattern
, status
);
646 failure(status
, "new ChoiceFormat");
648 if (fmt
->toPattern(pat
) != pattern
) {
649 errln( (UnicodeString
) "Formatter Pattern : " + pat
);
650 errln( (UnicodeString
) "Expected Pattern : " + pattern
);
652 FieldPosition
bogus(FieldPosition::DONT_CARE
);
655 // Will this work for -inf?
656 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
657 failure(status
, "fmt->format");
659 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), 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 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
666 failure(status
, "fmt->format");
668 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), str
, bogus
, status
));
669 failure(status
, "fmt->format");
671 logln("Format with 1.0 : " + fmt
->format(Formattable(1.0), str
, bogus
, status
));
672 failure(status
, "fmt->format");
674 logln("Format with 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
675 failure(status
, "fmt->format");
677 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
678 failure(status
, "fmt->format");
680 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
681 failure(status
, "fmt->format");
683 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
684 failure(status
, "fmt->format");
686 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
687 failure(status
, "fmt->format");
693 * MessageFormat.parse fails with ChoiceFormat.
695 void MessageFormatRegressionTest::Test4118592()
697 UErrorCode status
= U_ZERO_ERROR
;
698 MessageFormat
*mf
= new MessageFormat("", status
);
699 failure(status
, "new messageFormat");
700 UnicodeString
pattern("{0,choice,1#YES|2#NO}");
701 UnicodeString
prefix("");
702 Formattable
*objs
= 0;
704 for (int i
= 0; i
< 5; i
++) {
705 UnicodeString formatted
;
706 formatted
= prefix
+ "YES";
707 mf
->applyPattern(prefix
+ pattern
, status
);
708 failure(status
, "mf->applyPattern");
710 //Object[] objs = mf.parse(formatted, new ParsePosition(0));
713 objs
= mf
->parse(formatted
, pp
, count
);
715 logln(UnicodeString("") + i
+ ". pattern :\"" + mf
->toPattern(pat
) + "\"");
716 log(" \"" + formatted
+ "\" parsed as ");
721 if(objs
[0].getType() == Formattable::kString
)
722 logln((UnicodeString
)" " + objs
[0].getString(temp
));
724 logln((UnicodeString
)" " + (objs
[0].getType() == Formattable::kLong
? objs
[0].getLong() : objs
[0].getDouble()));
733 * MessageFormat.parse fails for some patterns.
735 void MessageFormatRegressionTest::Test4118594()
737 UErrorCode status
= U_ZERO_ERROR
;
738 const UBool possibleDataError
= TRUE
;
739 MessageFormat
*mf
= new MessageFormat("{0}, {0}, {0}", status
);
740 failure(status
, "new MessageFormat");
741 UnicodeString
forParsing("x, y, z");
742 //Object[] objs = mf.parse(forParsing, new ParsePosition(0));
745 Formattable
*objs
= mf
->parse(forParsing
, pp
, count
);
747 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
748 logln("text for parsing: \"" + forParsing
+ "\"");
750 if (objs
[0].getString(str
) != "z")
751 errln("argument0: \"" + objs
[0].getString(str
) + "\"");
752 mf
->applyPattern("{0,number,#.##}, {0,number,#.#}", status
);
753 failure(status
, "mf->applyPattern", possibleDataError
);
754 //Object[] oldobjs = {new Double(3.1415)};
755 Formattable oldobjs
[] = {Formattable(3.1415)};
756 UnicodeString result
;
757 FieldPosition
pos(FieldPosition::DONT_CARE
);
758 result
= mf
->format( oldobjs
, 1, result
, pos
, status
);
759 failure(status
, "mf->format", possibleDataError
);
761 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
762 logln("text for parsing: \"" + result
+ "\"");
763 // result now equals "3.14, 3.1"
764 if (result
!= "3.14, 3.1")
765 dataerrln("result = " + result
+ " - " + u_errorName(status
));
766 //Object[] newobjs = mf.parse(result, new ParsePosition(0));
769 Formattable
*newobjs
= mf
->parse(result
, pp
, count1
);
770 // newobjs now equals {new Double(3.1)}
771 if (newobjs
== NULL
) {
772 dataerrln("Error calling MessageFormat::parse");
774 if (newobjs
[0].getDouble() != 3.1)
775 errln( UnicodeString("newobjs[0] = ") + newobjs
[0].getDouble());
783 * When using ChoiceFormat, MessageFormat is not good for I18n.
785 void MessageFormatRegressionTest::Test4105380()
787 UnicodeString
patternText1("The disk \"{1}\" contains {0}.");
788 UnicodeString
patternText2("There are {0} on the disk \"{1}\"");
789 UErrorCode status
= U_ZERO_ERROR
;
790 const UBool possibleDataError
= TRUE
;
791 MessageFormat
*form1
= new MessageFormat(patternText1
, status
);
792 failure(status
, "new MessageFormat");
793 MessageFormat
*form2
= new MessageFormat(patternText2
, status
);
794 failure(status
, "new MessageFormat");
795 double filelimits
[] = {0,1,2};
796 UnicodeString filepart
[] = {
797 (UnicodeString
)"no files",
798 (UnicodeString
)"one file",
799 (UnicodeString
)"{0,number} files"
801 ChoiceFormat
*fileform
= new ChoiceFormat(filelimits
, filepart
, 3);
802 form1
->setFormat(1, *fileform
);
803 form2
->setFormat(0, *fileform
);
804 //Object[] testArgs = {new Long(12373), "MyDisk"};
805 Formattable testArgs
[] = {
806 Formattable((int32_t)12373),
807 Formattable((UnicodeString
)"MyDisk")
810 FieldPosition
bogus(FieldPosition::DONT_CARE
);
812 UnicodeString result
;
813 logln(form1
->format(testArgs
, 2, result
, bogus
, status
));
814 failure(status
, "form1->format", possibleDataError
);
816 logln(form2
->format(testArgs
, 2, result
, bogus
, status
));
817 failure(status
, "form1->format", possibleDataError
);
824 * MessageFormat.parse incorrectly sets errorIndex.
826 void MessageFormatRegressionTest::Test4120552()
828 UErrorCode status
= U_ZERO_ERROR
;
829 MessageFormat
*mf
= new MessageFormat("pattern", status
);
830 failure(status
, "new MessageFormat");
831 UnicodeString texts
[] = {
832 (UnicodeString
)"pattern",
833 (UnicodeString
)"pat",
834 (UnicodeString
)"1234"
837 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
838 for (int i
= 0; i
< 3; i
++) {
840 //Object[] objs = mf.parse(texts[i], pp);
842 Formattable
*objs
= mf
->parse(texts
[i
], pp
, count
);
843 log(" text for parsing: \"" + texts
[i
] + "\"");
845 logln(" (incorrectly formatted string)");
846 if (pp
.getErrorIndex() == -1)
847 errln(UnicodeString("Incorrect error index: ") + pp
.getErrorIndex());
849 logln(" (correctly formatted string)");
858 * MessageFormat handles single quotes in pattern wrong.
859 * This is actually a problem in ChoiceFormat; it doesn't
860 * understand single quotes.
862 void MessageFormatRegressionTest::Test4142938()
864 UnicodeString pat
= CharsToUnicodeString("''Vous'' {0,choice,0#n''|1#}avez s\\u00E9lectionn\\u00E9 "
865 "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} "
866 "personnel{0,choice,0#s|1#|2#s}.");
867 UErrorCode status
= U_ZERO_ERROR
;
868 MessageFormat
*mf
= new MessageFormat(pat
, status
);
869 failure(status
, "new MessageFormat");
871 UnicodeString PREFIX
[] = {
872 CharsToUnicodeString("'Vous' n'avez s\\u00E9lectionn\\u00E9 aucun clients personnels."),
873 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 "),
874 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 ")
876 UnicodeString SUFFIX
[] = {
878 UNICODE_STRING(" client personnel.", 18),
879 UNICODE_STRING(" clients personnels.", 20)
882 for (int i
=0; i
<3; i
++) {
884 //out = mf->format(new Object[]{new Integer(i)});
885 Formattable objs
[] = {
886 Formattable((int32_t)i
)
888 FieldPosition
pos(FieldPosition::DONT_CARE
);
889 out
= mf
->format(objs
, 1, out
, pos
, status
);
890 if (!failure(status
, "mf->format", TRUE
)) {
891 if (SUFFIX
[i
] == "") {
892 if (out
!= PREFIX
[i
])
893 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"");
896 if (!out
.startsWith(PREFIX
[i
]) ||
897 !out
.endsWith(SUFFIX
[i
]))
898 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"...\"" +
909 * Test the applyPattern and toPattern handling of single quotes
910 * by ChoiceFormat. (This is in here because this was a bug reported
911 * against MessageFormat.) The single quote is used to quote the
912 * pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row
913 * is a quote literal.
915 void MessageFormatRegressionTest::TestChoicePatternQuote()
917 // ICU 4.8 ChoiceFormat (like PluralFormat & SelectFormat)
918 // returns the chosen string unmodified, so that it is usable in a MessageFormat.
919 // We modified the test strings accordingly.
920 // Note: Without further formatting/trimming/etc., it is not possible
921 // to get a single apostrophe as the last character of a non-final choice sub-message
922 // because the single apostrophe before the pipe '|' would start quoted text.
923 // Normally, ChoiceFormat is used inside a MessageFormat, where a double apostrophe
924 // can be used in that case and will be formatted as a single one.
925 // (Better: Use a "real" apostrophe, U+2019.)
926 UnicodeString DATA
[] = {
927 // Pattern 0 value 1 value
928 // {sfb} hacked - changed \u2264 to = (copied from Character Map)
929 "0#can't|1#can", "can't", "can",
930 "0#pound(#)='#''|1#xyz", "pound(#)='#''", "xyz",
931 "0#1<2 '| 1=1'|1#'", "1<2 '| 1=1'", "'",
933 for (int i
=0; i
<9; i
+=3) {
935 UErrorCode status
= U_ZERO_ERROR
;
936 ChoiceFormat
*cf
= new ChoiceFormat(DATA
[i
], status
);
937 failure(status
, "new ChoiceFormat");
938 for (int j
=0; j
<=1; ++j
) {
940 FieldPosition
pos(FieldPosition::DONT_CARE
);
941 out
= cf
->format((double)j
, out
, pos
);
942 if (out
!= DATA
[i
+1+j
])
943 errln("Fail: Pattern \"" + DATA
[i
] + "\" x "+j
+" -> " +
944 out
+ "; want \"" + DATA
[i
+1+j
] + "\"");
947 pat
= cf
->toPattern(pat
);
949 ChoiceFormat
*cf2
= new ChoiceFormat(pat
, status
);
950 pat2
= cf2
->toPattern(pat2
);
952 errln("Fail: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ "\"");
954 logln("Ok: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ "\"");
956 catch (IllegalArgumentException e) {
957 errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
967 * MessageFormat.equals(null) throws a NullPointerException. The JLS states
968 * that it should return false.
970 void MessageFormatRegressionTest::Test4112104()
972 UErrorCode status
= U_ZERO_ERROR
;
973 MessageFormat
*format
= new MessageFormat("", status
);
974 failure(status
, "new MessageFormat");
976 // This should NOT throw an exception
977 if (format
== NULL
) {
978 // It also should return false
979 errln("MessageFormat.equals(null) returns false");
982 catch (NullPointerException e) {
983 errln("MessageFormat.equals(null) throws " + e);
988 void MessageFormatRegressionTest::TestAPI() {
989 UErrorCode status
= U_ZERO_ERROR
;
990 MessageFormat
*format
= new MessageFormat("", status
);
991 failure(status
, "new MessageFormat");
994 MessageFormat
*fmt
= new MessageFormat("",status
);
995 format
->adoptFormat("some_name",fmt
,status
); // Must at least pass a valid identifier.
996 failure(status
, "adoptFormat");
999 format
->setFormat((int32_t)0,*fmt
);
1000 format
->getFormat("some_other_name",status
); // Must at least pass a valid identifier.
1001 failure(status
, "getFormat");
1005 #endif /* #if !UCONFIG_NO_FORMATTING */