]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/msfmrgts.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2004, 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*/ )
31 // if (exec) logln((UnicodeString)"TestSuite MessageFormatRegressionTest");
52 CASE(19,TestChoicePatternQuote
)
55 default: name
= ""; break;
60 MessageFormatRegressionTest::failure(UErrorCode status
, const char* msg
)
62 if(U_FAILURE(status
)) {
63 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
71 * Null exception when formatting pattern with MessageFormat
74 void MessageFormatRegressionTest::Test4074764() {
75 UnicodeString pattern
[] = {
76 "Message without param",
77 "Message with param:{0}",
78 "Longer Message with param {0}"
80 //difference between the two param strings are that
81 //in the first one, the param position is within the
82 //length of the string without param while it is not so
85 UErrorCode status
= U_ZERO_ERROR
;
86 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
88 failure(status
, "couldn't create MessageFormat");
91 //Apply pattern with param and print the result
92 messageFormatter
->applyPattern(pattern
[1], status
);
93 failure(status
, "messageFormat->applyPattern");
94 //Object[] params = {new UnicodeString("BUG"), new Date()};
95 Formattable params
[] = {
96 Formattable(UnicodeString("BUG")),
97 Formattable(0, Formattable::kIsDate
)
99 UnicodeString tempBuffer
;
100 FieldPosition
pos(FieldPosition::DONT_CARE
);
101 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
102 if( tempBuffer
!= "Message with param:BUG" || failure(status
, "messageFormat->format"))
103 errln("MessageFormat with one param test failed.");
104 logln("Formatted with one extra param : " + tempBuffer
);
106 //Apply pattern without param and print the result
107 messageFormatter
->applyPattern(pattern
[0], status
);
108 failure(status
, "messageFormatter->applyPattern");
110 // {sfb} how much does this apply in C++?
111 // do we want to verify that the Formattable* array is not NULL,
112 // or is that the user's responsibility?
113 // additionally, what should be the item count?
114 // for bug testing purposes, assume that something was set to
115 // NULL by mistake, and that the length should be non-zero
117 //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
119 tempBuffer
= messageFormatter
->format(NULL
, 0, tempBuffer
, pos
, status
);
121 if( tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
122 errln("MessageFormat with no param test failed.");
123 logln("Formatted with no params : " + tempBuffer
);
126 tempBuffer
= messageFormatter
->format(params
, 2, tempBuffer
, pos
, status
);
127 if (tempBuffer
!= "Message without param" || failure(status
, "messageFormat->format"))
128 errln("Formatted with arguments > subsitution failed. result = " + tempBuffer
);
129 logln("Formatted with extra params : " + tempBuffer
);
130 //This statement gives an exception while formatting...
131 //If we use pattern[1] for the message with param,
132 //we get an NullPointerException in MessageFormat.java(617)
133 //If we use pattern[2] for the message with param,
134 //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
135 //Both are due to maxOffset not being reset to -1
136 //in applyPattern() when the pattern does not
138 /*} catch (Exception foo) {
139 errln("Exception when formatting with no params.");
142 delete messageFormatter
;
146 * MessageFormat.toPattern has weird rounding behavior.
148 void MessageFormatRegressionTest::Test4058973()
150 UErrorCode status
= U_ZERO_ERROR
;
151 MessageFormat
*fmt
= new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}", status
);
152 failure(status
, "new MessageFormat");
155 pat
= fmt
->toPattern(pat
);
156 UnicodeString
exp("{0,choice,0.0#no files|1.0#one file|1.0< {0,number,integer} files}");
158 errln("MessageFormat.toPattern failed");
159 errln("Exp: " + exp
);
160 errln("Got: " + pat
);
166 * More robust message formats.
168 void MessageFormatRegressionTest::Test4031438()
170 UErrorCode status
= U_ZERO_ERROR
;
172 UnicodeString
pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}.");
173 UnicodeString
pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.");
175 MessageFormat
*messageFormatter
= new MessageFormat("", status
);
176 failure(status
, "new MessageFormat");
179 logln("Apply with pattern : " + pattern1
);
180 messageFormatter
->applyPattern(pattern1
, status
);
181 failure(status
, "messageFormat->applyPattern");
182 //Object[] params = {new Integer(7)};
183 Formattable params
[]= {
184 Formattable((int32_t)7)
186 UnicodeString tempBuffer
;
187 FieldPosition
pos(FieldPosition::DONT_CARE
);
188 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
189 if(tempBuffer
!= "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status
, "MessageFormat::format"))
190 errln("Tests arguments < substitution failed");
191 logln("Formatted with 7 : " + tempBuffer
);
194 Formattable
*objs
= messageFormatter
->parse(tempBuffer
, pp
, count
);
195 //if(objs[7/*params.length*/] != NULL)
196 // errln("Parse failed with more than expected arguments");
198 NumberFormat
*fmt
= 0;
199 UnicodeString temp
, temp1
;
201 for (int i
= 0; i
< count
; i
++) {
203 // convert to string if not already
204 Formattable obj
= objs
[i
];
206 if(obj
.getType() == Formattable::kString
)
207 temp
= obj
.getString(temp
);
209 fmt
= NumberFormat::createInstance(status
);
210 switch (obj
.getType()) {
211 case Formattable::kLong
: fmt
->format(obj
.getLong(), temp
); break;
212 case Formattable::kInt64
: fmt
->format(obj
.getInt64(), temp
); break;
213 case Formattable::kDouble
: fmt
->format(obj
.getDouble(), temp
); break;
218 // convert to string if not already
219 Formattable obj1
= params
[i
];
221 if(obj1
.getType() == Formattable::kString
)
222 temp1
= obj1
.getString(temp1
);
224 fmt
= NumberFormat::createInstance(status
);
225 switch (obj1
.getType()) {
226 case Formattable::kLong
: fmt
->format(obj1
.getLong(), temp1
); break;
227 case Formattable::kInt64
: fmt
->format(obj1
.getInt64(), temp1
); break;
228 case Formattable::kDouble
: fmt
->format(obj1
.getDouble(), temp1
); break;
233 //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
235 errln("Parse failed on object " + objs
[i
].getString(temp1
) + " at index : " + i
);
242 // {sfb} does this apply? no way to really pass a null Formattable,
245 /*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
246 if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format"))
247 errln("Tests with no arguments failed");
248 logln("Formatted with null : " + tempBuffer);*/
249 logln("Apply with pattern : " + pattern2
);
250 messageFormatter
->applyPattern(pattern2
, status
);
251 failure(status
, "messageFormatter->applyPattern");
253 tempBuffer
= messageFormatter
->format(params
, 1, tempBuffer
, pos
, status
);
254 if (tempBuffer
!= "Double ' Quotes 7 test and quoted {1} test plus other {2} stuff.")
255 errln("quote format test (w/ params) failed.");
256 logln("Formatted with params : " + tempBuffer
);
258 /*tempBuffer = messageFormatter->format(null);
259 if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
260 errln("quote format test (w/ null) failed.");
261 logln("Formatted with null : " + tempBuffer);
262 logln("toPattern : " + messageFormatter.toPattern());*/
263 /*} catch (Exception foo) {
264 errln("Exception when formatting in bug 4031438. "+foo.getMessage());
266 delete messageFormatter
;
269 void MessageFormatRegressionTest::Test4052223()
272 ParsePosition
pos(0);
273 if (pos
.getErrorIndex() != -1) {
274 errln("ParsePosition.getErrorIndex initialization failed.");
277 UErrorCode status
= U_ZERO_ERROR
;
278 MessageFormat
*fmt
= new MessageFormat("There are {0} apples growing on the {1} tree.", status
);
279 failure(status
, "new MessageFormat");
280 UnicodeString
str("There is one apple growing on the peach tree.");
283 fmt
->parse(str
, pos
, count
);
285 logln(UnicodeString("unparsable string , should fail at ") + pos
.getErrorIndex());
286 if (pos
.getErrorIndex() == -1)
287 errln("Bug 4052223 failed : parsing string " + str
);
288 pos
.setErrorIndex(4);
289 if (pos
.getErrorIndex() != 4)
290 errln(UnicodeString("setErrorIndex failed, got ") + pos
.getErrorIndex() + " instead of 4");
292 ChoiceFormat
*f
= new ChoiceFormat(
293 "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.", status
);
294 failure(status
, "new ChoiceFormat");
296 pos
.setErrorIndex(-1);
298 f
->parse("are negative", obj
, pos
);
299 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == -1.0)
300 errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos
.getErrorIndex());
302 pos
.setErrorIndex(-1);
303 f
->parse("are no or fraction ", obj
, pos
);
304 if (pos
.getErrorIndex() != -1 && obj
.getDouble() == 0.0)
305 errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos
.getErrorIndex());
307 pos
.setErrorIndex(-1);
308 f
->parse("go postal", obj
, pos
);
309 if (pos
.getErrorIndex() == -1 && ! uprv_isNaN(obj
.getDouble()))
310 errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos
.getErrorIndex());
316 * ChoiceFormat.equals(null) throws NullPointerException
319 // {sfb} not really applicable in C++?? (kind of silly)
321 void MessageFormatRegressionTest::Test4104976()
323 double limits
[] = {1, 20};
324 UnicodeString formats
[] = {
325 UnicodeString("xyz"),
328 int32_t formats_length
= (int32_t)(sizeof(formats
)/sizeof(formats
[0]));
329 UErrorCode status
= U_ZERO_ERROR
;
330 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, formats_length
);
331 failure(status
, "new ChoiceFormat");
333 log("Compares to null is always false, returned : ");
334 logln(cf
== NULL
? "TRUE" : "FALSE");
335 /*} catch (Exception foo) {
336 errln("ChoiceFormat.equals(null) throws exception.");
343 * ChoiceFormat.ctor(double[], String[]) doesn't check
344 * whether lengths of input arrays are equal.
347 // {sfb} again, not really applicable in C++
349 void MessageFormatRegressionTest::Test4106659()
355 UnicodeString formats [] = {
358 ChoiceFormat *cf = NULL;
360 // cf = new ChoiceFormat(limits, formats, 3);
361 //} catch (Exception foo) {
362 // logln("ChoiceFormat constructor should check for the array lengths");
366 // errln(cf->format(5));
373 * ChoiceFormat.ctor(double[], String[]) allows unordered double array.
374 * This is not a bug, added javadoc to emphasize the use of limit
375 * array must be in ascending order.
377 void MessageFormatRegressionTest::Test4106660()
379 double limits
[] = {3, 1, 2};
380 UnicodeString formats
[] = {
381 UnicodeString("Three"),
382 UnicodeString("One"),
385 ChoiceFormat
*cf
= new ChoiceFormat(limits
, formats
, 3);
388 FieldPosition
pos(FieldPosition::DONT_CARE
);
389 str
= cf
->format(d
, str
, pos
);
391 errln( (UnicodeString
) "format(" + d
+ ") = " + str
);
397 * MessageFormat is incorrectly serialized/deserialized.
400 // {sfb} doesn't apply in C++
402 void MessageFormatRegressionTest::Test4111739()
404 /*MessageFormat format1 = null;
405 MessageFormat format2 = null;
406 ObjectOutputStream ostream = null;
407 ByteArrayOutputStream baos = null;
408 ObjectInputStream istream = null;
411 baos = new ByteArrayOutputStream();
412 ostream = new ObjectOutputStream(baos);
413 } catch(IOException e) {
414 errln("Unexpected exception : " + e.getMessage());
419 format1 = new MessageFormat("pattern{0}");
420 ostream.writeObject(format1);
423 byte bytes[] = baos.toByteArray();
425 istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
426 format2 = (MessageFormat)istream.readObject();
427 } catch(Exception e) {
428 errln("Unexpected exception : " + e.getMessage());
431 if (!format1.equals(format2)) {
432 errln("MessageFormats before and after serialization are not" +
433 " equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
434 format2 + "(" + format2.toPattern() + ")");
436 logln("Serialization for MessageFormat is OK.");
440 * MessageFormat.applyPattern allows illegal patterns.
442 void MessageFormatRegressionTest::Test4114743()
444 UnicodeString
originalPattern("initial pattern");
445 UErrorCode status
= U_ZERO_ERROR
;
446 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
447 failure(status
, "new MessageFormat");
449 UnicodeString
illegalPattern("ab { '}' de");
450 mf
->applyPattern(illegalPattern
, status
);
451 if( ! U_FAILURE(status
))
452 errln("illegal pattern: \"" + illegalPattern
+ "\"");
453 /*} catch (IllegalArgumentException foo) {
454 if (!originalPattern.equals(mf.toPattern()))
455 errln("pattern after: \"" + mf.toPattern() + "\"");
461 * MessageFormat.parse has different behavior in case of null.
463 void MessageFormatRegressionTest::Test4116444()
465 UnicodeString patterns
[] = {
467 (UnicodeString
)"one",
468 (UnicodeString
) "{0,date,short}"
471 UErrorCode status
= U_ZERO_ERROR
;
472 MessageFormat
*mf
= new MessageFormat("", status
);
473 failure(status
, "new MessageFormat");
475 for (int i
= 0; i
< 3; i
++) {
476 UnicodeString pattern
= patterns
[i
];
477 mf
->applyPattern(pattern
, status
);
478 failure(status
, "mf->applyPattern");
483 Formattable
*array
= mf
->parse(UnicodeString(""), pp
, count
);
484 logln("pattern: \"" + pattern
+ "\"");
485 log(" parsedObjects: ");
488 for (int j
= 0; j
< count
; j
++) {
489 //if (array[j] != null)
491 err("\"" + array
[j
].getString(dummy
) + "\"");
503 /*} catch (Exception e) {
504 errln("pattern: \"" + pattern + "\"");
505 errln(" Exception: " + e.getMessage());
511 /* @bug 4114739 (FIX and add javadoc)
512 * MessageFormat.format has undocumented behavior about empty format objects.
515 // {sfb} doesn't apply in C++?
516 void MessageFormatRegressionTest::Test4114739()
519 UErrorCode status
= U_ZERO_ERROR
;
520 MessageFormat
*mf
= new MessageFormat("<{0}>", status
);
521 failure(status
, "new MessageFormat");
523 Formattable
*objs1
= NULL
;
524 //Formattable objs2 [] = {};
525 //Formattable *objs3 [] = {NULL};
529 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
530 log("format(null) : ");
531 FieldPosition
pos(FieldPosition::DONT_CARE
);
532 logln("\"" + mf
->format(objs1
, 0, res
, pos
, status
) + "\"");
533 failure(status
, "mf->format");
534 /*log("format({}) : ");
535 logln("\"" + mf->format(objs2, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
536 failure(status, "mf->format");
537 log("format({null}) :");
538 logln("\"" + mf->format(objs3, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
539 failure(status, "mf->format");*/
540 /*} catch (Exception e) {
541 errln("Exception thrown for null argument tests.");
548 * MessageFormat.applyPattern works wrong with illegal patterns.
550 void MessageFormatRegressionTest::Test4113018()
552 UnicodeString
originalPattern("initial pattern");
553 UErrorCode status
= U_ZERO_ERROR
;
554 MessageFormat
*mf
= new MessageFormat(originalPattern
, status
);
555 failure(status
, "new messageFormat");
556 UnicodeString
illegalPattern("format: {0, xxxYYY}");
558 logln("pattern before: \"" + mf
->toPattern(pat
) + "\"");
559 logln("illegal pattern: \"" + illegalPattern
+ "\"");
561 mf
->applyPattern(illegalPattern
, status
);
562 if( ! U_FAILURE(status
))
563 errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern
);
564 /*} catch (IllegalArgumentException e) {
565 if (!originalPattern.equals(mf.toPattern()))
566 errln("pattern after: \"" + mf.toPattern() + "\"");
572 * ChoiceFormat is silent about the pattern usage in javadoc.
574 void MessageFormatRegressionTest::Test4106661()
576 UErrorCode status
= U_ZERO_ERROR
;
577 ChoiceFormat
*fmt
= new ChoiceFormat(
578 "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status
);
579 failure(status
, "new ChoiceFormat");
581 logln("Formatter Pattern : " + fmt
->toPattern(pat
));
583 FieldPosition
bogus(FieldPosition::DONT_CARE
);
586 // Will this work for -inf?
587 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
588 failure(status
, "fmt->format");
590 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
591 failure(status
, "fmt->format");
593 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
594 failure(status
, "fmt->format");
596 logln("Format with 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
597 failure(status
, "fmt->format");
599 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), 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 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
606 failure(status
, "fmt->format");
608 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
609 failure(status
, "fmt->format");
611 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
612 failure(status
, "fmt->format");
614 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
615 failure(status
, "fmt->format");
617 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
618 failure(status
, "fmt->format");
624 * ChoiceFormat should accept \u221E as eq. to INF.
626 void MessageFormatRegressionTest::Test4094906()
628 UErrorCode status
= U_ZERO_ERROR
;
629 UnicodeString
pattern("-");
630 pattern
+= (UChar
) 0x221E;
631 pattern
+= "<are negative|0.0<are no or fraction|1.0#is one|1.0<is 1+|";
632 pattern
+= (UChar
) 0x221E;
633 pattern
+= "<are many.";
635 ChoiceFormat
*fmt
= new ChoiceFormat(pattern
, status
);
636 failure(status
, "new ChoiceFormat");
638 if (fmt
->toPattern(pat
) != pattern
) {
639 errln( (UnicodeString
) "Formatter Pattern : " + pat
);
640 errln( (UnicodeString
) "Expected Pattern : " + pattern
);
642 FieldPosition
bogus(FieldPosition::DONT_CARE
);
645 // Will this work for -inf?
646 logln("Format with -INF : " + fmt
->format(Formattable(-uprv_getInfinity()), str
, bogus
, status
));
647 failure(status
, "fmt->format");
649 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
650 failure(status
, "fmt->format");
652 logln("Format with -1.0 : " + fmt
->format(Formattable(-1.0), str
, bogus
, status
));
653 failure(status
, "fmt->format");
655 logln("Format with 0 : " + fmt
->format(Formattable((int32_t)0), str
, bogus
, status
));
656 failure(status
, "fmt->format");
658 logln("Format with 0.9 : " + fmt
->format(Formattable(0.9), 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 1.5 : " + fmt
->format(Formattable(1.5), str
, bogus
, status
));
665 failure(status
, "fmt->format");
667 logln("Format with 2 : " + fmt
->format(Formattable((int32_t)2), str
, bogus
, status
));
668 failure(status
, "fmt->format");
670 logln("Format with 2.1 : " + fmt
->format(Formattable(2.1), str
, bogus
, status
));
671 failure(status
, "fmt->format");
673 logln("Format with NaN : " + fmt
->format(Formattable(uprv_getNaN()), str
, bogus
, status
));
674 failure(status
, "fmt->format");
676 logln("Format with +INF : " + fmt
->format(Formattable(uprv_getInfinity()), str
, bogus
, status
));
677 failure(status
, "fmt->format");
683 * MessageFormat.parse fails with ChoiceFormat.
685 void MessageFormatRegressionTest::Test4118592()
687 UErrorCode status
= U_ZERO_ERROR
;
688 MessageFormat
*mf
= new MessageFormat("", status
);
689 failure(status
, "new messageFormat");
690 UnicodeString
pattern("{0,choice,1#YES|2#NO}");
691 UnicodeString
prefix("");
692 Formattable
*objs
= 0;
694 for (int i
= 0; i
< 5; i
++) {
695 UnicodeString formatted
;
696 formatted
= prefix
+ "YES";
697 mf
->applyPattern(prefix
+ pattern
, status
);
698 failure(status
, "mf->applyPattern");
700 //Object[] objs = mf.parse(formatted, new ParsePosition(0));
703 objs
= mf
->parse(formatted
, pp
, count
);
705 logln(UnicodeString("") + i
+ ". pattern :\"" + mf
->toPattern(pat
) + "\"");
706 log(" \"" + formatted
+ "\" parsed as ");
711 if(objs
[0].getType() == Formattable::kString
)
712 logln((UnicodeString
)" " + objs
[0].getString(temp
));
714 logln((UnicodeString
)" " + (objs
[0].getType() == Formattable::kLong
? objs
[0].getLong() : objs
[0].getDouble()));
723 * MessageFormat.parse fails for some patterns.
725 void MessageFormatRegressionTest::Test4118594()
727 UErrorCode status
= U_ZERO_ERROR
;
728 MessageFormat
*mf
= new MessageFormat("{0}, {0}, {0}", status
);
729 failure(status
, "new MessageFormat");
730 UnicodeString
forParsing("x, y, z");
731 //Object[] objs = mf.parse(forParsing, new ParsePosition(0));
734 Formattable
*objs
= mf
->parse(forParsing
, pp
, count
);
736 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
737 logln("text for parsing: \"" + forParsing
+ "\"");
739 if (objs
[0].getString(str
) != "z")
740 errln("argument0: \"" + objs
[0].getString(str
) + "\"");
741 mf
->applyPattern("{0,number,#.##}, {0,number,#.#}", status
);
742 failure(status
, "mf->applyPattern");
743 //Object[] oldobjs = {new Double(3.1415)};
744 Formattable oldobjs
[] = {Formattable(3.1415)};
745 UnicodeString result
;
746 FieldPosition
pos(FieldPosition::DONT_CARE
);
747 result
= mf
->format( oldobjs
, 1, result
, pos
, status
);
748 failure(status
, "mf->format");
750 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
751 logln("text for parsing: \"" + result
+ "\"");
752 // result now equals "3.14, 3.1"
753 if (result
!= "3.14, 3.1")
754 errln("result = " + result
);
755 //Object[] newobjs = mf.parse(result, new ParsePosition(0));
758 Formattable
*newobjs
= mf
->parse(result
, pp
, count1
);
759 // newobjs now equals {new Double(3.1)}
760 if (newobjs
[0].getDouble() != 3.1)
761 errln( UnicodeString("newobjs[0] = ") + newobjs
[0].getDouble());
768 * When using ChoiceFormat, MessageFormat is not good for I18n.
770 void MessageFormatRegressionTest::Test4105380()
772 UnicodeString
patternText1("The disk \"{1}\" contains {0}.");
773 UnicodeString
patternText2("There are {0} on the disk \"{1}\"");
774 UErrorCode status
= U_ZERO_ERROR
;
775 MessageFormat
*form1
= new MessageFormat(patternText1
, status
);
776 failure(status
, "new MessageFormat");
777 MessageFormat
*form2
= new MessageFormat(patternText2
, status
);
778 failure(status
, "new MessageFormat");
779 double filelimits
[] = {0,1,2};
780 UnicodeString filepart
[] = {
781 (UnicodeString
)"no files",
782 (UnicodeString
)"one file",
783 (UnicodeString
)"{0,number} files"
785 ChoiceFormat
*fileform
= new ChoiceFormat(filelimits
, filepart
, 3);
786 form1
->setFormat(1, *fileform
);
787 form2
->setFormat(0, *fileform
);
788 //Object[] testArgs = {new Long(12373), "MyDisk"};
789 Formattable testArgs
[] = {
790 Formattable((int32_t)12373),
791 Formattable((UnicodeString
)"MyDisk")
794 FieldPosition
bogus(FieldPosition::DONT_CARE
);
796 UnicodeString result
;
797 logln(form1
->format(testArgs
, 2, result
, bogus
, status
));
798 failure(status
, "form1->format");
800 logln(form2
->format(testArgs
, 2, result
, bogus
, status
));
801 failure(status
, "form1->format");
808 * MessageFormat.parse incorrectly sets errorIndex.
810 void MessageFormatRegressionTest::Test4120552()
812 UErrorCode status
= U_ZERO_ERROR
;
813 MessageFormat
*mf
= new MessageFormat("pattern", status
);
814 failure(status
, "new MessageFormat");
815 UnicodeString texts
[] = {
816 (UnicodeString
)"pattern",
817 (UnicodeString
)"pat",
818 (UnicodeString
)"1234"
821 logln("pattern: \"" + mf
->toPattern(pat
) + "\"");
822 for (int i
= 0; i
< 3; i
++) {
824 //Object[] objs = mf.parse(texts[i], pp);
826 Formattable
*objs
= mf
->parse(texts
[i
], pp
, count
);
827 log(" text for parsing: \"" + texts
[i
] + "\"");
829 logln(" (incorrectly formatted string)");
830 if (pp
.getErrorIndex() == -1)
831 errln(UnicodeString("Incorrect error index: ") + pp
.getErrorIndex());
833 logln(" (correctly formatted string)");
842 * MessageFormat handles single quotes in pattern wrong.
843 * This is actually a problem in ChoiceFormat; it doesn't
844 * understand single quotes.
846 void MessageFormatRegressionTest::Test4142938()
848 UnicodeString pat
= CharsToUnicodeString("''Vous'' {0,choice,0#n''|1#}avez s\\u00E9lectionn\\u00E9 "
849 "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} "
850 "personnel{0,choice,0#s|1#|2#s}.");
851 UErrorCode status
= U_ZERO_ERROR
;
852 MessageFormat
*mf
= new MessageFormat(pat
, status
);
853 failure(status
, "new MessageFormat");
855 UnicodeString PREFIX
[] = {
856 CharsToUnicodeString("'Vous' n'avez s\\u00E9lectionn\\u00E9 aucun clients personnels."),
857 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 "),
858 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 ")
860 UnicodeString SUFFIX
[] = {
862 UNICODE_STRING(" client personnel.", 18),
863 UNICODE_STRING(" clients personnels.", 20)
866 for (int i
=0; i
<3; i
++) {
868 //out = mf->format(new Object[]{new Integer(i)});
869 Formattable objs
[] = {
870 Formattable((int32_t)i
)
872 FieldPosition
pos(FieldPosition::DONT_CARE
);
873 out
= mf
->format(objs
, 1, out
, pos
, status
);
874 failure(status
, "mf->format");
875 if (SUFFIX
[i
] == "") {
876 if (out
!= PREFIX
[i
])
877 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"");
880 if (!out
.startsWith(PREFIX
[i
]) ||
881 !out
.endsWith(SUFFIX
[i
]))
882 errln((UnicodeString
)"" + i
+ ": Got \"" + out
+ "\"; Want \"" + PREFIX
[i
] + "\"...\"" +
892 * Test the applyPattern and toPattern handling of single quotes
893 * by ChoiceFormat. (This is in here because this was a bug reported
894 * against MessageFormat.) The single quote is used to quote the
895 * pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row
896 * is a quote literal.
898 void MessageFormatRegressionTest::TestChoicePatternQuote()
900 UnicodeString DATA
[] = {
901 // Pattern 0 value 1 value
902 // {sfb} hacked - changed \u2264 to = (copied from Character Map)
903 (UnicodeString
)"0#can''t|1#can", (UnicodeString
)"can't", (UnicodeString
)"can",
904 (UnicodeString
)"0#'pound(#)=''#'''|1#xyz", (UnicodeString
)"pound(#)='#'", (UnicodeString
)"xyz",
905 (UnicodeString
)"0#'1<2 | 1=1'|1#''", (UnicodeString
)"1<2 | 1=1", (UnicodeString
)"'",
907 for (int i
=0; i
<9; i
+=3) {
909 UErrorCode status
= U_ZERO_ERROR
;
910 ChoiceFormat
*cf
= new ChoiceFormat(DATA
[i
], status
);
911 failure(status
, "new ChoiceFormat");
912 for (int j
=0; j
<=1; ++j
) {
914 FieldPosition
pos(FieldPosition::DONT_CARE
);
915 out
= cf
->format((double)j
, out
, pos
);
916 if (out
!= DATA
[i
+1+j
])
917 errln("Fail: Pattern \"" + DATA
[i
] + "\" x "+j
+" -> " +
918 out
+ "; want \"" + DATA
[i
+1+j
] + '"');
921 pat
= cf
->toPattern(pat
);
923 ChoiceFormat
*cf2
= new ChoiceFormat(pat
, status
);
924 pat2
= cf2
->toPattern(pat2
);
926 errln("Fail: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ '"');
928 logln("Ok: Pattern \"" + DATA
[i
] + "\" x toPattern -> \"" + pat
+ '"');
930 catch (IllegalArgumentException e) {
931 errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
941 * MessageFormat.equals(null) throws a NullPointerException. The JLS states
942 * that it should return false.
944 void MessageFormatRegressionTest::Test4112104()
946 UErrorCode status
= U_ZERO_ERROR
;
947 MessageFormat
*format
= new MessageFormat("", status
);
948 failure(status
, "new MessageFormat");
950 // This should NOT throw an exception
951 if (format
== NULL
) {
952 // It also should return false
953 errln("MessageFormat.equals(null) returns false");
956 catch (NullPointerException e) {
957 errln("MessageFormat.equals(null) throws " + e);
962 #endif /* #if !UCONFIG_NO_FORMATTING */