]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/msfmrgts.cpp
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / msfmrgts.cpp
CommitLineData
374ca955 1/***********************************************************************
b75a7d8f 2 * COPYRIGHT:
2ca993e8 3 * Copyright (c) 1997-2016, International Business Machines Corporation
374ca955
A
4 * and others. All Rights Reserved.
5 ***********************************************************************/
b75a7d8f
A
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_FORMATTING
10
11#include "msfmrgts.h"
12
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"
2ca993e8 20#include "cmemory.h"
374ca955 21#include "putilimp.h"
b75a7d8f
A
22
23// *****************************************************************************
24// class MessageFormatRegressionTest
25// *****************************************************************************
26
27#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
28
4388f060 29void
b75a7d8f
A
30MessageFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
31{
4388f060
A
32 TESTCASE_AUTO_BEGIN;
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)
55 TESTCASE_AUTO_END;
b75a7d8f
A
56}
57
58UBool
729e4ab9 59MessageFormatRegressionTest::failure(UErrorCode status, const char* msg, UBool possibleDataError)
b75a7d8f
A
60{
61 if(U_FAILURE(status)) {
729e4ab9
A
62 if (possibleDataError) {
63 dataerrln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
64 } else {
65 errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
66 }
b75a7d8f
A
67 return TRUE;
68 }
69
70 return FALSE;
71}
72
73/* @bug 4074764
74 * Null exception when formatting pattern with MessageFormat
75 * with no parameters.
76 */
77void MessageFormatRegressionTest::Test4074764() {
78 UnicodeString pattern [] = {
79 "Message without param",
80 "Message with param:{0}",
81 "Longer Message with param {0}"
82 };
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
86 //in the other case.
87
88 UErrorCode status = U_ZERO_ERROR;
89 MessageFormat *messageFormatter = new MessageFormat("", status);
90
91 failure(status, "couldn't create MessageFormat");
92
93 //try {
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)
101 };
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);
108
109 //Apply pattern without param and print the result
110 messageFormatter->applyPattern(pattern[0], status);
111 failure(status, "messageFormatter->applyPattern");
112
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
119
120 //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
121 tempBuffer.remove();
122 tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status);
123
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);
127
128 tempBuffer.remove();
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
140 //contain any param.
141 /*} catch (Exception foo) {
142 errln("Exception when formatting with no params.");
143 }*/
144
145 delete messageFormatter;
146}
147
148/* @bug 4058973
149 * MessageFormat.toPattern has weird rounding behavior.
4388f060
A
150 *
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.
b75a7d8f 154 */
4388f060
A
155/*
156void MessageFormatRegressionTest::Test4058973()
b75a7d8f
A
157{
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");
161
162 UnicodeString pat;
163 pat = fmt->toPattern(pat);
46f4442e 164 UnicodeString exp("{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
b75a7d8f
A
165 if (pat != exp) {
166 errln("MessageFormat.toPattern failed");
167 errln("Exp: " + exp);
168 errln("Got: " + pat);
169 }
170
171 delete fmt;
4388f060 172}*/
b75a7d8f
A
173/* @bug 4031438
174 * More robust message formats.
175 */
176void MessageFormatRegressionTest::Test4031438()
177{
178 UErrorCode status = U_ZERO_ERROR;
179
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'.");
182
183 MessageFormat *messageFormatter = new MessageFormat("", status);
184 failure(status, "new MessageFormat");
729e4ab9
A
185
186 const UBool possibleDataError = TRUE;
b75a7d8f
A
187
188 //try {
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)
195 };
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"))
729e4ab9 200 dataerrln("Tests arguments < substitution failed");
b75a7d8f
A
201 logln("Formatted with 7 : " + tempBuffer);
202 ParsePosition pp(0);
203 int32_t count = 0;
204 Formattable *objs = messageFormatter->parse(tempBuffer, pp, count);
205 //if(objs[7/*params.length*/] != NULL)
206 // errln("Parse failed with more than expected arguments");
207
208 NumberFormat *fmt = 0;
209 UnicodeString temp, temp1;
210
211 for (int i = 0; i < count; i++) {
212
213 // convert to string if not already
214 Formattable obj = objs[i];
215 temp.remove();
216 if(obj.getType() == Formattable::kString)
217 temp = obj.getString(temp);
218 else {
219 fmt = NumberFormat::createInstance(status);
374ca955
A
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;
224 default: break;
225 }
b75a7d8f
A
226 }
227
228 // convert to string if not already
229 Formattable obj1 = params[i];
230 temp1.remove();
374ca955
A
231 if(obj1.getType() == Formattable::kString)
232 temp1 = obj1.getString(temp1);
233 else {
b75a7d8f 234 fmt = NumberFormat::createInstance(status);
374ca955
A
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;
239 default: break;
240 }
b75a7d8f 241 }
b75a7d8f
A
242
243 //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
244 if (temp != temp1) {
245 errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i);
374ca955 246 }
b75a7d8f
A
247 }
248
249 delete fmt;
250 delete [] objs;
251
252 // {sfb} does this apply? no way to really pass a null Formattable,
253 // only a null array
254
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);
729e4ab9 261 failure(status, "messageFormatter->applyPattern", possibleDataError);
b75a7d8f
A
262 tempBuffer.remove();
263 tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status);
4388f060 264 if (tempBuffer != "Double ' Quotes 7 test and quoted {1} test plus 'other {2} stuff'.")
729e4ab9 265 dataerrln("quote format test (w/ params) failed. - %s", u_errorName(status));
b75a7d8f
A
266 logln("Formatted with params : " + tempBuffer);
267
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());
275 }*/
276 delete messageFormatter;
277}
278
279void MessageFormatRegressionTest::Test4052223()
280{
281
282 ParsePosition pos(0);
283 if (pos.getErrorIndex() != -1) {
284 errln("ParsePosition.getErrorIndex initialization failed.");
285 }
286
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.");
291
292 int32_t count = 0;
293 fmt->parse(str, pos, count);
294
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");
301
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");
305 pos.setIndex(0);
306 pos.setErrorIndex(-1);
307 Formattable obj;
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());
311 pos.setIndex(0);
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());
316 pos.setIndex(0);
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());
321
322 delete fmt;
323 delete f;
324}
325/* @bug 4104976
326 * ChoiceFormat.equals(null) throws NullPointerException
327 */
328
329// {sfb} not really applicable in C++?? (kind of silly)
330
331void MessageFormatRegressionTest::Test4104976()
332{
333 double limits [] = {1, 20};
334 UnicodeString formats [] = {
335 UnicodeString("xyz"),
336 UnicodeString("abc")
337 };
2ca993e8 338 int32_t formats_length = UPRV_LENGTHOF(formats);
b75a7d8f
A
339 UErrorCode status = U_ZERO_ERROR;
340 ChoiceFormat *cf = new ChoiceFormat(limits, formats, formats_length);
341 failure(status, "new ChoiceFormat");
342 //try {
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.");
347 }*/
348
349 delete cf;
350}
351
352/* @bug 4106659
353 * ChoiceFormat.ctor(double[], String[]) doesn't check
354 * whether lengths of input arrays are equal.
355 */
356
357// {sfb} again, not really applicable in C++
358
359void MessageFormatRegressionTest::Test4106659()
360{
361 /*
362 double limits [] = {
363 1, 2, 3
364 };
365 UnicodeString formats [] = {
366 "one", "two"
367 };
368 ChoiceFormat *cf = NULL;
369 //try {
370 // cf = new ChoiceFormat(limits, formats, 3);
371 //} catch (Exception foo) {
372 // logln("ChoiceFormat constructor should check for the array lengths");
373 // cf = null;
374 //}
375 //if (cf != null)
376 // errln(cf->format(5));
377 //
378 delete cf;
379 */
380}
381
382/* @bug 4106660
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.
386 */
387void MessageFormatRegressionTest::Test4106660()
388{
389 double limits [] = {3, 1, 2};
390 UnicodeString formats [] = {
391 UnicodeString("Three"),
392 UnicodeString("One"),
393 UnicodeString("Two")
394 };
395 ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3);
396 double d = 5.0;
397 UnicodeString str;
398 FieldPosition pos(FieldPosition::DONT_CARE);
399 str = cf->format(d, str, pos);
400 if (str != "Two")
401 errln( (UnicodeString) "format(" + d + ") = " + str);
402
403 delete cf;
404}
405
406/* @bug 4111739
407 * MessageFormat is incorrectly serialized/deserialized.
408 */
409
410// {sfb} doesn't apply in C++
411
412void MessageFormatRegressionTest::Test4111739()
413{
414 /*MessageFormat format1 = null;
415 MessageFormat format2 = null;
416 ObjectOutputStream ostream = null;
417 ByteArrayOutputStream baos = null;
418 ObjectInputStream istream = null;
419
420 try {
421 baos = new ByteArrayOutputStream();
422 ostream = new ObjectOutputStream(baos);
423 } catch(IOException e) {
424 errln("Unexpected exception : " + e.getMessage());
425 return;
426 }
427
428 try {
429 format1 = new MessageFormat("pattern{0}");
430 ostream.writeObject(format1);
431 ostream.flush();
432
433 byte bytes[] = baos.toByteArray();
434
435 istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
436 format2 = (MessageFormat)istream.readObject();
437 } catch(Exception e) {
438 errln("Unexpected exception : " + e.getMessage());
439 }
440
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() + ")");
445 } else {
446 logln("Serialization for MessageFormat is OK.");
447 }*/
448}
449/* @bug 4114743
450 * MessageFormat.applyPattern allows illegal patterns.
451 */
452void MessageFormatRegressionTest::Test4114743()
453{
454 UnicodeString originalPattern("initial pattern");
455 UErrorCode status = U_ZERO_ERROR;
456 MessageFormat *mf = new MessageFormat(originalPattern, status);
457 failure(status, "new MessageFormat");
458 //try {
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() + "\"");
466 }*/
467 delete mf;
468}
469
470/* @bug 4116444
471 * MessageFormat.parse has different behavior in case of null.
472 */
473void MessageFormatRegressionTest::Test4116444()
474{
475 UnicodeString patterns [] = {
476 (UnicodeString)"",
477 (UnicodeString)"one",
478 (UnicodeString) "{0,date,short}"
479 };
480
481 UErrorCode status = U_ZERO_ERROR;
482 MessageFormat *mf = new MessageFormat("", status);
483 failure(status, "new MessageFormat");
484
485 for (int i = 0; i < 3; i++) {
486 UnicodeString pattern = patterns[i];
487 mf->applyPattern(pattern, status);
729e4ab9 488 failure(status, "mf->applyPattern", TRUE);
b75a7d8f
A
489
490 //try {
491 int32_t count = 0;
492 ParsePosition pp(0);
493 Formattable *array = mf->parse(UnicodeString(""), pp, count);
494 logln("pattern: \"" + pattern + "\"");
495 log(" parsedObjects: ");
496 if (array != NULL) {
497 log("{");
498 for (int j = 0; j < count; j++) {
499 //if (array[j] != null)
500 UnicodeString dummy;
4388f060 501 dataerrln("\"" + array[j].getString(dummy) + "\"");
b75a7d8f
A
502 //else
503 // log("null");
504 if (j < count- 1)
505 log(",");
506 }
507 log("}") ;
508 delete[] array;
509 } else {
510 log("null");
511 }
512 logln("");
513 /*} catch (Exception e) {
514 errln("pattern: \"" + pattern + "\"");
515 errln(" Exception: " + e.getMessage());
516 }*/
517 }
518
519 delete mf;
520}
521/* @bug 4114739 (FIX and add javadoc)
522 * MessageFormat.format has undocumented behavior about empty format objects.
523 */
524
525// {sfb} doesn't apply in C++?
526void MessageFormatRegressionTest::Test4114739()
527{
528
529 UErrorCode status = U_ZERO_ERROR;
530 MessageFormat *mf = new MessageFormat("<{0}>", status);
531 failure(status, "new MessageFormat");
532
533 Formattable *objs1 = NULL;
534 //Formattable objs2 [] = {};
535 //Formattable *objs3 [] = {NULL};
536 //try {
537 UnicodeString pat;
538 UnicodeString res;
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.");
552 }*/
553
554 delete mf;
555}
556
557/* @bug 4113018
558 * MessageFormat.applyPattern works wrong with illegal patterns.
559 */
560void MessageFormatRegressionTest::Test4113018()
561{
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}");
567 UnicodeString pat;
568 logln("pattern before: \"" + mf->toPattern(pat) + "\"");
569 logln("illegal pattern: \"" + illegalPattern + "\"");
570 //try {
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() + "\"");
577 }*/
578 delete mf;
579}
580
581/* @bug 4106661
582 * ChoiceFormat is silent about the pattern usage in javadoc.
583 */
584void MessageFormatRegressionTest::Test4106661()
585{
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");
590 UnicodeString pat;
591 logln("Formatter Pattern : " + fmt->toPattern(pat));
592
593 FieldPosition bogus(FieldPosition::DONT_CARE);
594 UnicodeString str;
595
596 // Will this work for -inf?
597 logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
598 failure(status, "fmt->format");
599 str.remove();
600 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), 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 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
607 failure(status, "fmt->format");
608 str.remove();
609 logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
610 failure(status, "fmt->format");
611 str.remove();
612 logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
613 failure(status, "fmt->format");
614 str.remove();
615 logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
616 failure(status, "fmt->format");
617 str.remove();
618 logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
619 failure(status, "fmt->format");
620 str.remove();
621 logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
622 failure(status, "fmt->format");
623 str.remove();
624 logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
625 failure(status, "fmt->format");
626 str.remove();
627 logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
628 failure(status, "fmt->format");
629
630 delete fmt;
631}
632
633/* @bug 4094906
634 * ChoiceFormat should accept \u221E as eq. to INF.
635 */
636void MessageFormatRegressionTest::Test4094906()
637{
638 UErrorCode status = U_ZERO_ERROR;
639 UnicodeString pattern("-");
640 pattern += (UChar) 0x221E;
46f4442e 641 pattern += "<are negative|0<are no or fraction|1#is one|1<is 1+|";
b75a7d8f
A
642 pattern += (UChar) 0x221E;
643 pattern += "<are many.";
644
645 ChoiceFormat *fmt = new ChoiceFormat(pattern, status);
646 failure(status, "new ChoiceFormat");
647 UnicodeString pat;
648 if (fmt->toPattern(pat) != pattern) {
649 errln( (UnicodeString) "Formatter Pattern : " + pat);
650 errln( (UnicodeString) "Expected Pattern : " + pattern);
651 }
652 FieldPosition bogus(FieldPosition::DONT_CARE);
653 UnicodeString str;
654
655 // Will this work for -inf?
656 logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
657 failure(status, "fmt->format");
658 str.remove();
659 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), 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 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
666 failure(status, "fmt->format");
667 str.remove();
668 logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
669 failure(status, "fmt->format");
670 str.remove();
671 logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
672 failure(status, "fmt->format");
673 str.remove();
674 logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
675 failure(status, "fmt->format");
676 str.remove();
677 logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
678 failure(status, "fmt->format");
679 str.remove();
680 logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
681 failure(status, "fmt->format");
682 str.remove();
683 logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
684 failure(status, "fmt->format");
685 str.remove();
686 logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
687 failure(status, "fmt->format");
688
689 delete fmt;
690}
691
692/* @bug 4118592
693 * MessageFormat.parse fails with ChoiceFormat.
694 */
695void MessageFormatRegressionTest::Test4118592()
696{
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;
703
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");
709 prefix += "x";
710 //Object[] objs = mf.parse(formatted, new ParsePosition(0));
711 int32_t count = 0;
712 ParsePosition pp(0);
713 objs = mf->parse(formatted, pp, count);
714 UnicodeString pat;
715 logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\"");
716 log(" \"" + formatted + "\" parsed as ");
717 if (objs == NULL)
718 logln(" null");
719 else {
720 UnicodeString temp;
721 if(objs[0].getType() == Formattable::kString)
722 logln((UnicodeString)" " + objs[0].getString(temp));
723 else
724 logln((UnicodeString)" " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble()));
725 delete[] objs;
726
727 }
728 }
729
730 delete mf;
731}
732/* @bug 4118594
733 * MessageFormat.parse fails for some patterns.
734 */
735void MessageFormatRegressionTest::Test4118594()
736{
737 UErrorCode status = U_ZERO_ERROR;
729e4ab9 738 const UBool possibleDataError = TRUE;
b75a7d8f
A
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));
743 int32_t count = 0;
744 ParsePosition pp(0);
745 Formattable *objs = mf->parse(forParsing, pp, count);
746 UnicodeString pat;
747 logln("pattern: \"" + mf->toPattern(pat) + "\"");
748 logln("text for parsing: \"" + forParsing + "\"");
749 UnicodeString str;
750 if (objs[0].getString(str) != "z")
751 errln("argument0: \"" + objs[0].getString(str) + "\"");
752 mf->applyPattern("{0,number,#.##}, {0,number,#.#}", status);
729e4ab9 753 failure(status, "mf->applyPattern", possibleDataError);
b75a7d8f
A
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 );
729e4ab9 759 failure(status, "mf->format", possibleDataError);
b75a7d8f
A
760 pat.remove();
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")
729e4ab9 765 dataerrln("result = " + result + " - " + u_errorName(status));
b75a7d8f
A
766 //Object[] newobjs = mf.parse(result, new ParsePosition(0));
767 int32_t count1 = 0;
768 pp.setIndex(0);
769 Formattable *newobjs = mf->parse(result, pp, count1);
770 // newobjs now equals {new Double(3.1)}
73c04bcf
A
771 if (newobjs == NULL) {
772 dataerrln("Error calling MessageFormat::parse");
773 } else {
774 if (newobjs[0].getDouble() != 3.1)
775 errln( UnicodeString("newobjs[0] = ") + newobjs[0].getDouble());
776 }
b75a7d8f
A
777
778 delete [] objs;
779 delete [] newobjs;
780 delete mf;
781}
782/* @bug 4105380
783 * When using ChoiceFormat, MessageFormat is not good for I18n.
784 */
785void MessageFormatRegressionTest::Test4105380()
786{
787 UnicodeString patternText1("The disk \"{1}\" contains {0}.");
788 UnicodeString patternText2("There are {0} on the disk \"{1}\"");
789 UErrorCode status = U_ZERO_ERROR;
729e4ab9 790 const UBool possibleDataError = TRUE;
b75a7d8f
A
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"
800 };
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")
808 };
809
810 FieldPosition bogus(FieldPosition::DONT_CARE);
811
812 UnicodeString result;
813 logln(form1->format(testArgs, 2, result, bogus, status));
729e4ab9 814 failure(status, "form1->format", possibleDataError);
b75a7d8f
A
815 result.remove();
816 logln(form2->format(testArgs, 2, result, bogus, status));
729e4ab9 817 failure(status, "form1->format", possibleDataError);
b75a7d8f
A
818
819 delete form1;
820 delete form2;
821 delete fileform;
822}
823/* @bug 4120552
824 * MessageFormat.parse incorrectly sets errorIndex.
825 */
826void MessageFormatRegressionTest::Test4120552()
827{
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"
835 };
836 UnicodeString pat;
837 logln("pattern: \"" + mf->toPattern(pat) + "\"");
838 for (int i = 0; i < 3; i++) {
839 ParsePosition pp(0);
840 //Object[] objs = mf.parse(texts[i], pp);
841 int32_t count = 0;
842 Formattable *objs = mf->parse(texts[i], pp, count);
843 log(" text for parsing: \"" + texts[i] + "\"");
844 if (objs == NULL) {
845 logln(" (incorrectly formatted string)");
846 if (pp.getErrorIndex() == -1)
847 errln(UnicodeString("Incorrect error index: ") + pp.getErrorIndex());
848 } else {
849 logln(" (correctly formatted string)");
850 delete[] objs;
851 }
852 }
853 delete mf;
854}
855
856/**
857 * @bug 4142938
858 * MessageFormat handles single quotes in pattern wrong.
859 * This is actually a problem in ChoiceFormat; it doesn't
860 * understand single quotes.
861 */
862void MessageFormatRegressionTest::Test4142938()
863{
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");
870
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 ")
875 };
876 UnicodeString SUFFIX [] = {
877 UnicodeString(),
878 UNICODE_STRING(" client personnel.", 18),
879 UNICODE_STRING(" clients personnels.", 20)
880 };
881
882 for (int i=0; i<3; i++) {
883 UnicodeString out;
884 //out = mf->format(new Object[]{new Integer(i)});
885 Formattable objs [] = {
886 Formattable((int32_t)i)
887 };
888 FieldPosition pos(FieldPosition::DONT_CARE);
889 out = mf->format(objs, 1, out, pos, status);
729e4ab9
A
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] + "\"");
894 }
895 else {
896 if (!out.startsWith(PREFIX[i]) ||
897 !out.endsWith(SUFFIX[i]))
898 errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
899 SUFFIX[i] + "\"");
900 }
b75a7d8f
A
901 }
902 }
903
904 delete mf;
905}
906
907/**
908 * @bug 4142938
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.
914 */
915void MessageFormatRegressionTest::TestChoicePatternQuote()
916{
4388f060
A
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.)
b75a7d8f
A
926 UnicodeString DATA [] = {
927 // Pattern 0 value 1 value
928 // {sfb} hacked - changed \u2264 to = (copied from Character Map)
4388f060
A
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'", "'",
b75a7d8f
A
932 };
933 for (int i=0; i<9; i+=3) {
934 //try {
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) {
939 UnicodeString out;
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+" -> " +
4388f060 944 out + "; want \"" + DATA[i+1+j] + "\"");
b75a7d8f
A
945 }
946 UnicodeString pat;
947 pat = cf->toPattern(pat);
948 UnicodeString pat2;
949 ChoiceFormat *cf2 = new ChoiceFormat(pat, status);
950 pat2 = cf2->toPattern(pat2);
951 if (pat != pat2)
4388f060 952 errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + "\"");
b75a7d8f 953 else
4388f060 954 logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + "\"");
b75a7d8f
A
955 /*}
956 catch (IllegalArgumentException e) {
957 errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
958 }*/
959
960 delete cf;
961 delete cf2;
962 }
963}
964
965/**
966 * @bug 4112104
967 * MessageFormat.equals(null) throws a NullPointerException. The JLS states
968 * that it should return false.
969 */
970void MessageFormatRegressionTest::Test4112104()
971{
972 UErrorCode status = U_ZERO_ERROR;
973 MessageFormat *format = new MessageFormat("", status);
974 failure(status, "new MessageFormat");
975 //try {
976 // This should NOT throw an exception
977 if (format == NULL) {
978 // It also should return false
979 errln("MessageFormat.equals(null) returns false");
980 }
981 /*}
982 catch (NullPointerException e) {
983 errln("MessageFormat.equals(null) throws " + e);
984 }*/
985 delete format;
986}
987
729e4ab9
A
988void MessageFormatRegressionTest::TestAPI() {
989 UErrorCode status = U_ZERO_ERROR;
990 MessageFormat *format = new MessageFormat("", status);
991 failure(status, "new MessageFormat");
992
993 // Test adoptFormat
994 MessageFormat *fmt = new MessageFormat("",status);
4388f060 995 format->adoptFormat("some_name",fmt,status); // Must at least pass a valid identifier.
729e4ab9
A
996 failure(status, "adoptFormat");
997
998 // Test getFormat
999 format->setFormat((int32_t)0,*fmt);
4388f060 1000 format->getFormat("some_other_name",status); // Must at least pass a valid identifier.
729e4ab9
A
1001 failure(status, "getFormat");
1002 delete format;
1003}
1004
b75a7d8f 1005#endif /* #if !UCONFIG_NO_FORMATTING */