1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /***********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 1997-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***********************************************************************/
8
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_FORMATTING
12
13 #include "msfmrgts.h"
14
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"
22 #include "cmemory.h"
23 #include "putilimp.h"
24
25 // *****************************************************************************
26 // class MessageFormatRegressionTest
27 // *****************************************************************************
28
29 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
30
31 void
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)32 MessageFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
33 {
34 TESTCASE_AUTO_BEGIN;
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);
58 TESTCASE_AUTO_END;
59 }
60
61 UBool
failure(UErrorCode status,const char * msg,UBool possibleDataError)62 MessageFormatRegressionTest::failure(UErrorCode status, const char* msg, UBool possibleDataError)
63 {
64 if(U_FAILURE(status)) {
65 if (possibleDataError) {
66 dataerrln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
67 } else {
68 errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
69 }
70 return TRUE;
71 }
72
73 return FALSE;
74 }
75
76 /* @bug 4074764
77 * Null exception when formatting pattern with MessageFormat
78 * with no parameters.
79 */
Test4074764()80 void MessageFormatRegressionTest::Test4074764() {
81 UnicodeString pattern [] = {
82 "Message without param",
83 "Message with param:{0}",
84 "Longer Message with param {0}"
85 };
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
89 //in the other case.
90
91 UErrorCode status = U_ZERO_ERROR;
92 MessageFormat *messageFormatter = new MessageFormat("", status);
93
94 failure(status, "couldn't create MessageFormat");
95
96 //try {
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)
104 };
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);
111
112 //Apply pattern without param and print the result
113 messageFormatter->applyPattern(pattern[0], status);
114 failure(status, "messageFormatter->applyPattern");
115
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
122
123 //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
124 tempBuffer.remove();
125 tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status);
126
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);
130
131 tempBuffer.remove();
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
143 //contain any param.
144 /*} catch (Exception foo) {
145 errln("Exception when formatting with no params.");
146 }*/
147
148 delete messageFormatter;
149 }
150
151 /* @bug 4058973
152 * MessageFormat.toPattern has weird rounding behavior.
153 *
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.
157 */
158 /*
159 void MessageFormatRegressionTest::Test4058973()
160 {
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");
164
165 UnicodeString pat;
166 pat = fmt->toPattern(pat);
167 UnicodeString exp("{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
168 if (pat != exp) {
169 errln("MessageFormat.toPattern failed");
170 errln("Exp: " + exp);
171 errln("Got: " + pat);
172 }
173
174 delete fmt;
175 }*/
176 /* @bug 4031438
177 * More robust message formats.
178 */
Test4031438()179 void MessageFormatRegressionTest::Test4031438()
180 {
181 UErrorCode status = U_ZERO_ERROR;
182
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'.");
185
186 MessageFormat *messageFormatter = new MessageFormat("", status);
187 failure(status, "new MessageFormat");
188
189 const UBool possibleDataError = TRUE;
190
191 //try {
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)
198 };
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);
205 ParsePosition pp(0);
206 int32_t count = 0;
207 Formattable *objs = messageFormatter->parse(tempBuffer, pp, count);
208 //if(objs[7/*params.length*/] != NULL)
209 // errln("Parse failed with more than expected arguments");
210
211 NumberFormat *fmt = 0;
212 UnicodeString temp, temp1;
213
214 for (int i = 0; i < count; i++) {
215
216 // convert to string if not already
217 Formattable obj = objs[i];
218 temp.remove();
219 if(obj.getType() == Formattable::kString)
220 temp = obj.getString(temp);
221 else {
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;
227 default: break;
228 }
229 }
230
231 // convert to string if not already
232 Formattable obj1 = params[i];
233 temp1.remove();
234 if(obj1.getType() == Formattable::kString)
235 temp1 = obj1.getString(temp1);
236 else {
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;
242 default: break;
243 }
244 }
245
246 //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
247 if (temp != temp1) {
248 errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i);
249 }
250 }
251
252 delete fmt;
253 delete [] objs;
254
255 // {sfb} does this apply? no way to really pass a null Formattable,
256 // only a null array
257
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);
265 tempBuffer.remove();
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);
270
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());
278 }*/
279 delete messageFormatter;
280 }
281
Test4052223()282 void MessageFormatRegressionTest::Test4052223()
283 {
284
285 ParsePosition pos(0);
286 if (pos.getErrorIndex() != -1) {
287 errln("ParsePosition.getErrorIndex initialization failed.");
288 }
289
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.");
294
295 int32_t count = 0;
296 fmt->parse(str, pos, count);
297
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");
304
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");
308 pos.setIndex(0);
309 pos.setErrorIndex(-1);
310 Formattable obj;
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());
314 pos.setIndex(0);
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());
319 pos.setIndex(0);
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());
324
325 delete fmt;
326 delete f;
327 }
328 /* @bug 4104976
329 * ChoiceFormat.equals(null) throws NullPointerException
330 */
331
332 // {sfb} not really applicable in C++?? (kind of silly)
333
Test4104976()334 void MessageFormatRegressionTest::Test4104976()
335 {
336 double limits [] = {1, 20};
337 UnicodeString formats [] = {
338 UnicodeString("xyz"),
339 UnicodeString("abc")
340 };
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");
345 //try {
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.");
350 }*/
351
352 delete cf;
353 }
354
355 /* @bug 4106659
356 * ChoiceFormat.ctor(double[], String[]) doesn't check
357 * whether lengths of input arrays are equal.
358 */
359
360 // {sfb} again, not really applicable in C++
361
Test4106659()362 void MessageFormatRegressionTest::Test4106659()
363 {
364 /*
365 double limits [] = {
366 1, 2, 3
367 };
368 UnicodeString formats [] = {
369 "one", "two"
370 };
371 ChoiceFormat *cf = NULL;
372 //try {
373 // cf = new ChoiceFormat(limits, formats, 3);
374 //} catch (Exception foo) {
375 // logln("ChoiceFormat constructor should check for the array lengths");
376 // cf = null;
377 //}
378 //if (cf != null)
379 // errln(cf->format(5));
380 //
381 delete cf;
382 */
383 }
384
385 /* @bug 4106660
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.
389 */
Test4106660()390 void MessageFormatRegressionTest::Test4106660()
391 {
392 double limits [] = {3, 1, 2};
393 UnicodeString formats [] = {
394 UnicodeString("Three"),
395 UnicodeString("One"),
396 UnicodeString("Two")
397 };
398 ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3);
399 double d = 5.0;
400 UnicodeString str;
401 FieldPosition pos(FieldPosition::DONT_CARE);
402 str = cf->format(d, str, pos);
403 if (str != "Two")
404 errln( (UnicodeString) "format(" + d + ") = " + str);
405
406 delete cf;
407 }
408
409 /* @bug 4111739
410 * MessageFormat is incorrectly serialized/deserialized.
411 */
412
413 // {sfb} doesn't apply in C++
414
Test4111739()415 void MessageFormatRegressionTest::Test4111739()
416 {
417 /*MessageFormat format1 = null;
418 MessageFormat format2 = null;
419 ObjectOutputStream ostream = null;
420 ByteArrayOutputStream baos = null;
421 ObjectInputStream istream = null;
422
423 try {
424 baos = new ByteArrayOutputStream();
425 ostream = new ObjectOutputStream(baos);
426 } catch(IOException e) {
427 errln("Unexpected exception : " + e.getMessage());
428 return;
429 }
430
431 try {
432 format1 = new MessageFormat("pattern{0}");
433 ostream.writeObject(format1);
434 ostream.flush();
435
436 byte bytes[] = baos.toByteArray();
437
438 istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
439 format2 = (MessageFormat)istream.readObject();
440 } catch(Exception e) {
441 errln("Unexpected exception : " + e.getMessage());
442 }
443
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() + ")");
448 } else {
449 logln("Serialization for MessageFormat is OK.");
450 }*/
451 }
452 /* @bug 4114743
453 * MessageFormat.applyPattern allows illegal patterns.
454 */
Test4114743()455 void MessageFormatRegressionTest::Test4114743()
456 {
457 UnicodeString originalPattern("initial pattern");
458 UErrorCode status = U_ZERO_ERROR;
459 MessageFormat *mf = new MessageFormat(originalPattern, status);
460 failure(status, "new MessageFormat");
461 //try {
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() + "\"");
469 }*/
470 delete mf;
471 }
472
473 /* @bug 4116444
474 * MessageFormat.parse has different behavior in case of null.
475 */
Test4116444()476 void MessageFormatRegressionTest::Test4116444()
477 {
478 UnicodeString patterns [] = {
479 (UnicodeString)"",
480 (UnicodeString)"one",
481 (UnicodeString) "{0,date,short}"
482 };
483
484 UErrorCode status = U_ZERO_ERROR;
485 MessageFormat *mf = new MessageFormat("", status);
486 failure(status, "new MessageFormat");
487
488 for (int i = 0; i < 3; i++) {
489 UnicodeString pattern = patterns[i];
490 mf->applyPattern(pattern, status);
491 failure(status, "mf->applyPattern", TRUE);
492
493 //try {
494 int32_t count = 0;
495 ParsePosition pp(0);
496 Formattable *array = mf->parse(UnicodeString(""), pp, count);
497 logln("pattern: \"" + pattern + "\"");
498 log(" parsedObjects: ");
499 if (array != NULL) {
500 log("{");
501 for (int j = 0; j < count; j++) {
502 //if (array[j] != null)
503 UnicodeString dummy;
504 dataerrln("\"" + array[j].getString(dummy) + "\"");
505 //else
506 // log("null");
507 if (j < count- 1)
508 log(",");
509 }
510 log("}") ;
511 delete[] array;
512 } else {
513 log("null");
514 }
515 logln("");
516 /*} catch (Exception e) {
517 errln("pattern: \"" + pattern + "\"");
518 errln(" Exception: " + e.getMessage());
519 }*/
520 }
521
522 delete mf;
523 }
524 /* @bug 4114739 (FIX and add javadoc)
525 * MessageFormat.format has undocumented behavior about empty format objects.
526 */
527
528 // {sfb} doesn't apply in C++?
Test4114739()529 void MessageFormatRegressionTest::Test4114739()
530 {
531
532 UErrorCode status = U_ZERO_ERROR;
533 MessageFormat *mf = new MessageFormat("<{0}>", status);
534 failure(status, "new MessageFormat");
535
536 Formattable *objs1 = NULL;
537 //Formattable objs2 [] = {};
538 //Formattable *objs3 [] = {NULL};
539 //try {
540 UnicodeString pat;
541 UnicodeString res;
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.");
555 }*/
556
557 delete mf;
558 }
559
560 /* @bug 4113018
561 * MessageFormat.applyPattern works wrong with illegal patterns.
562 */
Test4113018()563 void MessageFormatRegressionTest::Test4113018()
564 {
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}");
570 UnicodeString pat;
571 logln("pattern before: \"" + mf->toPattern(pat) + "\"");
572 logln("illegal pattern: \"" + illegalPattern + "\"");
573 //try {
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() + "\"");
580 }*/
581 delete mf;
582 }
583
584 /* @bug 4106661
585 * ChoiceFormat is silent about the pattern usage in javadoc.
586 */
Test4106661()587 void MessageFormatRegressionTest::Test4106661()
588 {
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");
593 UnicodeString pat;
594 logln("Formatter Pattern : " + fmt->toPattern(pat));
595
596 FieldPosition bogus(FieldPosition::DONT_CARE);
597 UnicodeString str;
598
599 // Will this work for -inf?
600 logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
601 failure(status, "fmt->format");
602 str.remove();
603 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
604 failure(status, "fmt->format");
605 str.remove();
606 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
607 failure(status, "fmt->format");
608 str.remove();
609 logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
610 failure(status, "fmt->format");
611 str.remove();
612 logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
613 failure(status, "fmt->format");
614 str.remove();
615 logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
616 failure(status, "fmt->format");
617 str.remove();
618 logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
619 failure(status, "fmt->format");
620 str.remove();
621 logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
622 failure(status, "fmt->format");
623 str.remove();
624 logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
625 failure(status, "fmt->format");
626 str.remove();
627 logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
628 failure(status, "fmt->format");
629 str.remove();
630 logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
631 failure(status, "fmt->format");
632
633 delete fmt;
634 }
635
636 /* @bug 4094906
637 * ChoiceFormat should accept \u221E as eq. to INF.
638 */
Test4094906()639 void MessageFormatRegressionTest::Test4094906()
640 {
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.";
647
648 ChoiceFormat *fmt = new ChoiceFormat(pattern, status);
649 failure(status, "new ChoiceFormat");
650 UnicodeString pat;
651 if (fmt->toPattern(pat) != pattern) {
652 errln( (UnicodeString) "Formatter Pattern : " + pat);
653 errln( (UnicodeString) "Expected Pattern : " + pattern);
654 }
655 FieldPosition bogus(FieldPosition::DONT_CARE);
656 UnicodeString str;
657
658 // Will this work for -inf?
659 logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
660 failure(status, "fmt->format");
661 str.remove();
662 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
663 failure(status, "fmt->format");
664 str.remove();
665 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
666 failure(status, "fmt->format");
667 str.remove();
668 logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
669 failure(status, "fmt->format");
670 str.remove();
671 logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
672 failure(status, "fmt->format");
673 str.remove();
674 logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
675 failure(status, "fmt->format");
676 str.remove();
677 logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
678 failure(status, "fmt->format");
679 str.remove();
680 logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
681 failure(status, "fmt->format");
682 str.remove();
683 logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
684 failure(status, "fmt->format");
685 str.remove();
686 logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
687 failure(status, "fmt->format");
688 str.remove();
689 logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
690 failure(status, "fmt->format");
691
692 delete fmt;
693 }
694
695 /* @bug 4118592
696 * MessageFormat.parse fails with ChoiceFormat.
697 */
Test4118592()698 void MessageFormatRegressionTest::Test4118592()
699 {
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;
706
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");
712 prefix += "x";
713 //Object[] objs = mf.parse(formatted, new ParsePosition(0));
714 int32_t count = 0;
715 ParsePosition pp(0);
716 objs = mf->parse(formatted, pp, count);
717 UnicodeString pat;
718 logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\"");
719 log(" \"" + formatted + "\" parsed as ");
720 if (objs == NULL)
721 logln(" null");
722 else {
723 UnicodeString temp;
724 if(objs[0].getType() == Formattable::kString)
725 logln((UnicodeString)" " + objs[0].getString(temp));
726 else
727 logln((UnicodeString)" " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble()));
728 delete[] objs;
729
730 }
731 }
732
733 delete mf;
734 }
735 /* @bug 4118594
736 * MessageFormat.parse fails for some patterns.
737 */
Test4118594()738 void MessageFormatRegressionTest::Test4118594()
739 {
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));
746 int32_t count = 0;
747 ParsePosition pp(0);
748 Formattable *objs = mf->parse(forParsing, pp, count);
749 UnicodeString pat;
750 logln("pattern: \"" + mf->toPattern(pat) + "\"");
751 logln("text for parsing: \"" + forParsing + "\"");
752 UnicodeString str;
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);
763 pat.remove();
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));
770 int32_t count1 = 0;
771 pp.setIndex(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");
776 } else {
777 if (newobjs[0].getDouble() != 3.1)
778 errln( UnicodeString("newobjs[0] = ") + newobjs[0].getDouble());
779 }
780
781 delete [] objs;
782 delete [] newobjs;
783 delete mf;
784 }
785 /* @bug 4105380
786 * When using ChoiceFormat, MessageFormat is not good for I18n.
787 */
Test4105380()788 void MessageFormatRegressionTest::Test4105380()
789 {
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"
803 };
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")
811 };
812
813 FieldPosition bogus(FieldPosition::DONT_CARE);
814
815 UnicodeString result;
816 logln(form1->format(testArgs, 2, result, bogus, status));
817 failure(status, "form1->format", possibleDataError);
818 result.remove();
819 logln(form2->format(testArgs, 2, result, bogus, status));
820 failure(status, "form1->format", possibleDataError);
821
822 delete form1;
823 delete form2;
824 delete fileform;
825 }
826 /* @bug 4120552
827 * MessageFormat.parse incorrectly sets errorIndex.
828 */
Test4120552()829 void MessageFormatRegressionTest::Test4120552()
830 {
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"
838 };
839 UnicodeString pat;
840 logln("pattern: \"" + mf->toPattern(pat) + "\"");
841 for (int i = 0; i < 3; i++) {
842 ParsePosition pp(0);
843 //Object[] objs = mf.parse(texts[i], pp);
844 int32_t count = 0;
845 Formattable *objs = mf->parse(texts[i], pp, count);
846 log(" text for parsing: \"" + texts[i] + "\"");
847 if (objs == NULL) {
848 logln(" (incorrectly formatted string)");
849 if (pp.getErrorIndex() == -1)
850 errln(UnicodeString("Incorrect error index: ") + pp.getErrorIndex());
851 } else {
852 logln(" (correctly formatted string)");
853 delete[] objs;
854 }
855 }
856 delete mf;
857 }
858
859 /**
860 * @bug 4142938
861 * MessageFormat handles single quotes in pattern wrong.
862 * This is actually a problem in ChoiceFormat; it doesn't
863 * understand single quotes.
864 */
Test4142938()865 void MessageFormatRegressionTest::Test4142938()
866 {
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");
873
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 ")
878 };
879 UnicodeString SUFFIX [] = {
880 UnicodeString(),
881 UNICODE_STRING(" client personnel.", 18),
882 UNICODE_STRING(" clients personnels.", 20)
883 };
884
885 for (int i=0; i<3; i++) {
886 UnicodeString out;
887 //out = mf->format(new Object[]{new Integer(i)});
888 Formattable objs [] = {
889 Formattable((int32_t)i)
890 };
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] + "\"");
897 }
898 else {
899 if (!out.startsWith(PREFIX[i]) ||
900 !out.endsWith(SUFFIX[i]))
901 errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
902 SUFFIX[i] + "\"");
903 }
904 }
905 }
906
907 delete mf;
908 }
909
910 /**
911 * @bug 4142938
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.
917 */
TestChoicePatternQuote()918 void MessageFormatRegressionTest::TestChoicePatternQuote()
919 {
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'", "'",
935 };
936 for (int i=0; i<9; i+=3) {
937 //try {
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) {
942 UnicodeString out;
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] + "\"");
948 }
949 UnicodeString pat;
950 pat = cf->toPattern(pat);
951 UnicodeString pat2;
952 ChoiceFormat *cf2 = new ChoiceFormat(pat, status);
953 pat2 = cf2->toPattern(pat2);
954 if (pat != pat2)
955 errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + "\"");
956 else
957 logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + "\"");
958 /*}
959 catch (IllegalArgumentException e) {
960 errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
961 }*/
962
963 delete cf;
964 delete cf2;
965 }
966 }
967
968 /**
969 * @bug 4112104
970 * MessageFormat.equals(null) throws a NullPointerException. The JLS states
971 * that it should return false.
972 */
Test4112104()973 void MessageFormatRegressionTest::Test4112104()
974 {
975 UErrorCode status = U_ZERO_ERROR;
976 MessageFormat *format = new MessageFormat("", status);
977 failure(status, "new MessageFormat");
978 //try {
979 // This should NOT throw an exception
980 if (format == NULL) {
981 // It also should return false
982 errln("MessageFormat.equals(null) returns false");
983 }
984 /*}
985 catch (NullPointerException e) {
986 errln("MessageFormat.equals(null) throws " + e);
987 }*/
988 delete format;
989 }
990
TestICU12584()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}");
996
997 UErrorCode status = U_ZERO_ERROR;
998
999 MessageFormat msg(pattern, status);
1000 failure(status, "Flat message");
1001
1002 int32_t count;
1003 msg.getFormats(count);
1004 assertEquals("Plain placeholder match", 42, count);
1005
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");
1011
1012 count = 0;
1013 inner_msg.getFormats(count);
1014 assertEquals("Inner placeholder match", 3, count);
1015 }
1016
TestAPI()1017 void MessageFormatRegressionTest::TestAPI() {
1018 UErrorCode status = U_ZERO_ERROR;
1019 MessageFormat *format = new MessageFormat("", status);
1020 failure(status, "new MessageFormat");
1021
1022 // Test adoptFormat
1023 MessageFormat *fmt = new MessageFormat("",status);
1024 format->adoptFormat("some_name",fmt,status); // Must at least pass a valid identifier.
1025 failure(status, "adoptFormat");
1026
1027 // Test getFormat
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");
1031 delete format;
1032 }
1033
1034 #endif /* #if !UCONFIG_NO_FORMATTING */
1035