]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/msfmrgts.cpp
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / test / intltest / msfmrgts.cpp
CommitLineData
374ca955 1/***********************************************************************
b75a7d8f 2 * COPYRIGHT:
46f4442e 3 * Copyright (c) 1997-2008, 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"
374ca955 20#include "putilimp.h"
b75a7d8f
A
21
22// *****************************************************************************
23// class MessageFormatRegressionTest
24// *****************************************************************************
25
26#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
27
28void
29MessageFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
30{
31 // if (exec) logln((UnicodeString)"TestSuite MessageFormatRegressionTest");
32 switch (index) {
33 CASE(0,Test4074764)
34 CASE(1,Test4058973)
35 CASE(2,Test4031438)
36 CASE(3,Test4052223)
37 CASE(4,Test4104976)
38 CASE(5,Test4106659)
39 CASE(6,Test4106660)
40 CASE(7,Test4111739)
41 CASE(8,Test4114743)
42 CASE(9,Test4116444)
43 CASE(10,Test4114739)
44 CASE(11,Test4113018)
45 CASE(12,Test4106661)
46 CASE(13,Test4094906)
47 CASE(14,Test4118592)
48 CASE(15,Test4118594)
49 CASE(16,Test4105380)
50 CASE(17,Test4120552)
51 CASE(18,Test4142938)
52 CASE(19,TestChoicePatternQuote)
53 CASE(20,Test4112104)
54
55 default: name = ""; break;
56 }
57}
58
59UBool
60MessageFormatRegressionTest::failure(UErrorCode status, const char* msg)
61{
62 if(U_FAILURE(status)) {
63 errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
64 return TRUE;
65 }
66
67 return FALSE;
68}
69
70/* @bug 4074764
71 * Null exception when formatting pattern with MessageFormat
72 * with no parameters.
73 */
74void MessageFormatRegressionTest::Test4074764() {
75 UnicodeString pattern [] = {
76 "Message without param",
77 "Message with param:{0}",
78 "Longer Message with param {0}"
79 };
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
83 //in the other case.
84
85 UErrorCode status = U_ZERO_ERROR;
86 MessageFormat *messageFormatter = new MessageFormat("", status);
87
88 failure(status, "couldn't create MessageFormat");
89
90 //try {
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)
98 };
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);
105
106 //Apply pattern without param and print the result
107 messageFormatter->applyPattern(pattern[0], status);
108 failure(status, "messageFormatter->applyPattern");
109
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
116
117 //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
118 tempBuffer.remove();
119 tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status);
120
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);
124
125 tempBuffer.remove();
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
137 //contain any param.
138 /*} catch (Exception foo) {
139 errln("Exception when formatting with no params.");
140 }*/
141
142 delete messageFormatter;
143}
144
145/* @bug 4058973
146 * MessageFormat.toPattern has weird rounding behavior.
147 */
148void MessageFormatRegressionTest::Test4058973()
149{
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");
153
154 UnicodeString pat;
155 pat = fmt->toPattern(pat);
46f4442e 156 UnicodeString exp("{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
b75a7d8f
A
157 if (pat != exp) {
158 errln("MessageFormat.toPattern failed");
159 errln("Exp: " + exp);
160 errln("Got: " + pat);
161 }
162
163 delete fmt;
164}
165/* @bug 4031438
166 * More robust message formats.
167 */
168void MessageFormatRegressionTest::Test4031438()
169{
170 UErrorCode status = U_ZERO_ERROR;
171
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'.");
174
175 MessageFormat *messageFormatter = new MessageFormat("", status);
176 failure(status, "new MessageFormat");
177
178 //try {
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)
185 };
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);
192 ParsePosition pp(0);
193 int32_t count = 0;
194 Formattable *objs = messageFormatter->parse(tempBuffer, pp, count);
195 //if(objs[7/*params.length*/] != NULL)
196 // errln("Parse failed with more than expected arguments");
197
198 NumberFormat *fmt = 0;
199 UnicodeString temp, temp1;
200
201 for (int i = 0; i < count; i++) {
202
203 // convert to string if not already
204 Formattable obj = objs[i];
205 temp.remove();
206 if(obj.getType() == Formattable::kString)
207 temp = obj.getString(temp);
208 else {
209 fmt = NumberFormat::createInstance(status);
374ca955
A
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;
214 default: break;
215 }
b75a7d8f
A
216 }
217
218 // convert to string if not already
219 Formattable obj1 = params[i];
220 temp1.remove();
374ca955
A
221 if(obj1.getType() == Formattable::kString)
222 temp1 = obj1.getString(temp1);
223 else {
b75a7d8f 224 fmt = NumberFormat::createInstance(status);
374ca955
A
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;
229 default: break;
230 }
b75a7d8f 231 }
b75a7d8f
A
232
233 //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
234 if (temp != temp1) {
235 errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i);
374ca955 236 }
b75a7d8f
A
237 }
238
239 delete fmt;
240 delete [] objs;
241
242 // {sfb} does this apply? no way to really pass a null Formattable,
243 // only a null array
244
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");
252 tempBuffer.remove();
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);
257
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());
265 }*/
266 delete messageFormatter;
267}
268
269void MessageFormatRegressionTest::Test4052223()
270{
271
272 ParsePosition pos(0);
273 if (pos.getErrorIndex() != -1) {
274 errln("ParsePosition.getErrorIndex initialization failed.");
275 }
276
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.");
281
282 int32_t count = 0;
283 fmt->parse(str, pos, count);
284
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");
291
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");
295 pos.setIndex(0);
296 pos.setErrorIndex(-1);
297 Formattable obj;
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());
301 pos.setIndex(0);
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());
306 pos.setIndex(0);
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());
311
312 delete fmt;
313 delete f;
314}
315/* @bug 4104976
316 * ChoiceFormat.equals(null) throws NullPointerException
317 */
318
319// {sfb} not really applicable in C++?? (kind of silly)
320
321void MessageFormatRegressionTest::Test4104976()
322{
323 double limits [] = {1, 20};
324 UnicodeString formats [] = {
325 UnicodeString("xyz"),
326 UnicodeString("abc")
327 };
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");
332 //try {
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.");
337 }*/
338
339 delete cf;
340}
341
342/* @bug 4106659
343 * ChoiceFormat.ctor(double[], String[]) doesn't check
344 * whether lengths of input arrays are equal.
345 */
346
347// {sfb} again, not really applicable in C++
348
349void MessageFormatRegressionTest::Test4106659()
350{
351 /*
352 double limits [] = {
353 1, 2, 3
354 };
355 UnicodeString formats [] = {
356 "one", "two"
357 };
358 ChoiceFormat *cf = NULL;
359 //try {
360 // cf = new ChoiceFormat(limits, formats, 3);
361 //} catch (Exception foo) {
362 // logln("ChoiceFormat constructor should check for the array lengths");
363 // cf = null;
364 //}
365 //if (cf != null)
366 // errln(cf->format(5));
367 //
368 delete cf;
369 */
370}
371
372/* @bug 4106660
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.
376 */
377void MessageFormatRegressionTest::Test4106660()
378{
379 double limits [] = {3, 1, 2};
380 UnicodeString formats [] = {
381 UnicodeString("Three"),
382 UnicodeString("One"),
383 UnicodeString("Two")
384 };
385 ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3);
386 double d = 5.0;
387 UnicodeString str;
388 FieldPosition pos(FieldPosition::DONT_CARE);
389 str = cf->format(d, str, pos);
390 if (str != "Two")
391 errln( (UnicodeString) "format(" + d + ") = " + str);
392
393 delete cf;
394}
395
396/* @bug 4111739
397 * MessageFormat is incorrectly serialized/deserialized.
398 */
399
400// {sfb} doesn't apply in C++
401
402void MessageFormatRegressionTest::Test4111739()
403{
404 /*MessageFormat format1 = null;
405 MessageFormat format2 = null;
406 ObjectOutputStream ostream = null;
407 ByteArrayOutputStream baos = null;
408 ObjectInputStream istream = null;
409
410 try {
411 baos = new ByteArrayOutputStream();
412 ostream = new ObjectOutputStream(baos);
413 } catch(IOException e) {
414 errln("Unexpected exception : " + e.getMessage());
415 return;
416 }
417
418 try {
419 format1 = new MessageFormat("pattern{0}");
420 ostream.writeObject(format1);
421 ostream.flush();
422
423 byte bytes[] = baos.toByteArray();
424
425 istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
426 format2 = (MessageFormat)istream.readObject();
427 } catch(Exception e) {
428 errln("Unexpected exception : " + e.getMessage());
429 }
430
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() + ")");
435 } else {
436 logln("Serialization for MessageFormat is OK.");
437 }*/
438}
439/* @bug 4114743
440 * MessageFormat.applyPattern allows illegal patterns.
441 */
442void MessageFormatRegressionTest::Test4114743()
443{
444 UnicodeString originalPattern("initial pattern");
445 UErrorCode status = U_ZERO_ERROR;
446 MessageFormat *mf = new MessageFormat(originalPattern, status);
447 failure(status, "new MessageFormat");
448 //try {
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() + "\"");
456 }*/
457 delete mf;
458}
459
460/* @bug 4116444
461 * MessageFormat.parse has different behavior in case of null.
462 */
463void MessageFormatRegressionTest::Test4116444()
464{
465 UnicodeString patterns [] = {
466 (UnicodeString)"",
467 (UnicodeString)"one",
468 (UnicodeString) "{0,date,short}"
469 };
470
471 UErrorCode status = U_ZERO_ERROR;
472 MessageFormat *mf = new MessageFormat("", status);
473 failure(status, "new MessageFormat");
474
475 for (int i = 0; i < 3; i++) {
476 UnicodeString pattern = patterns[i];
477 mf->applyPattern(pattern, status);
478 failure(status, "mf->applyPattern");
479
480 //try {
481 int32_t count = 0;
482 ParsePosition pp(0);
483 Formattable *array = mf->parse(UnicodeString(""), pp, count);
484 logln("pattern: \"" + pattern + "\"");
485 log(" parsedObjects: ");
486 if (array != NULL) {
487 log("{");
488 for (int j = 0; j < count; j++) {
489 //if (array[j] != null)
490 UnicodeString dummy;
491 err("\"" + array[j].getString(dummy) + "\"");
492 //else
493 // log("null");
494 if (j < count- 1)
495 log(",");
496 }
497 log("}") ;
498 delete[] array;
499 } else {
500 log("null");
501 }
502 logln("");
503 /*} catch (Exception e) {
504 errln("pattern: \"" + pattern + "\"");
505 errln(" Exception: " + e.getMessage());
506 }*/
507 }
508
509 delete mf;
510}
511/* @bug 4114739 (FIX and add javadoc)
512 * MessageFormat.format has undocumented behavior about empty format objects.
513 */
514
515// {sfb} doesn't apply in C++?
516void MessageFormatRegressionTest::Test4114739()
517{
518
519 UErrorCode status = U_ZERO_ERROR;
520 MessageFormat *mf = new MessageFormat("<{0}>", status);
521 failure(status, "new MessageFormat");
522
523 Formattable *objs1 = NULL;
524 //Formattable objs2 [] = {};
525 //Formattable *objs3 [] = {NULL};
526 //try {
527 UnicodeString pat;
528 UnicodeString res;
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.");
542 }*/
543
544 delete mf;
545}
546
547/* @bug 4113018
548 * MessageFormat.applyPattern works wrong with illegal patterns.
549 */
550void MessageFormatRegressionTest::Test4113018()
551{
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}");
557 UnicodeString pat;
558 logln("pattern before: \"" + mf->toPattern(pat) + "\"");
559 logln("illegal pattern: \"" + illegalPattern + "\"");
560 //try {
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() + "\"");
567 }*/
568 delete mf;
569}
570
571/* @bug 4106661
572 * ChoiceFormat is silent about the pattern usage in javadoc.
573 */
574void MessageFormatRegressionTest::Test4106661()
575{
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");
580 UnicodeString pat;
581 logln("Formatter Pattern : " + fmt->toPattern(pat));
582
583 FieldPosition bogus(FieldPosition::DONT_CARE);
584 UnicodeString str;
585
586 // Will this work for -inf?
587 logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
588 failure(status, "fmt->format");
589 str.remove();
590 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
591 failure(status, "fmt->format");
592 str.remove();
593 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
594 failure(status, "fmt->format");
595 str.remove();
596 logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
597 failure(status, "fmt->format");
598 str.remove();
599 logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
600 failure(status, "fmt->format");
601 str.remove();
602 logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
603 failure(status, "fmt->format");
604 str.remove();
605 logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
606 failure(status, "fmt->format");
607 str.remove();
608 logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
609 failure(status, "fmt->format");
610 str.remove();
611 logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
612 failure(status, "fmt->format");
613 str.remove();
614 logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
615 failure(status, "fmt->format");
616 str.remove();
617 logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
618 failure(status, "fmt->format");
619
620 delete fmt;
621}
622
623/* @bug 4094906
624 * ChoiceFormat should accept \u221E as eq. to INF.
625 */
626void MessageFormatRegressionTest::Test4094906()
627{
628 UErrorCode status = U_ZERO_ERROR;
629 UnicodeString pattern("-");
630 pattern += (UChar) 0x221E;
46f4442e 631 pattern += "<are negative|0<are no or fraction|1#is one|1<is 1+|";
b75a7d8f
A
632 pattern += (UChar) 0x221E;
633 pattern += "<are many.";
634
635 ChoiceFormat *fmt = new ChoiceFormat(pattern, status);
636 failure(status, "new ChoiceFormat");
637 UnicodeString pat;
638 if (fmt->toPattern(pat) != pattern) {
639 errln( (UnicodeString) "Formatter Pattern : " + pat);
640 errln( (UnicodeString) "Expected Pattern : " + pattern);
641 }
642 FieldPosition bogus(FieldPosition::DONT_CARE);
643 UnicodeString str;
644
645 // Will this work for -inf?
646 logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
647 failure(status, "fmt->format");
648 str.remove();
649 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
650 failure(status, "fmt->format");
651 str.remove();
652 logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
653 failure(status, "fmt->format");
654 str.remove();
655 logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
656 failure(status, "fmt->format");
657 str.remove();
658 logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
659 failure(status, "fmt->format");
660 str.remove();
661 logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
662 failure(status, "fmt->format");
663 str.remove();
664 logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
665 failure(status, "fmt->format");
666 str.remove();
667 logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
668 failure(status, "fmt->format");
669 str.remove();
670 logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
671 failure(status, "fmt->format");
672 str.remove();
673 logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
674 failure(status, "fmt->format");
675 str.remove();
676 logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
677 failure(status, "fmt->format");
678
679 delete fmt;
680}
681
682/* @bug 4118592
683 * MessageFormat.parse fails with ChoiceFormat.
684 */
685void MessageFormatRegressionTest::Test4118592()
686{
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;
693
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");
699 prefix += "x";
700 //Object[] objs = mf.parse(formatted, new ParsePosition(0));
701 int32_t count = 0;
702 ParsePosition pp(0);
703 objs = mf->parse(formatted, pp, count);
704 UnicodeString pat;
705 logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\"");
706 log(" \"" + formatted + "\" parsed as ");
707 if (objs == NULL)
708 logln(" null");
709 else {
710 UnicodeString temp;
711 if(objs[0].getType() == Formattable::kString)
712 logln((UnicodeString)" " + objs[0].getString(temp));
713 else
714 logln((UnicodeString)" " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble()));
715 delete[] objs;
716
717 }
718 }
719
720 delete mf;
721}
722/* @bug 4118594
723 * MessageFormat.parse fails for some patterns.
724 */
725void MessageFormatRegressionTest::Test4118594()
726{
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));
732 int32_t count = 0;
733 ParsePosition pp(0);
734 Formattable *objs = mf->parse(forParsing, pp, count);
735 UnicodeString pat;
736 logln("pattern: \"" + mf->toPattern(pat) + "\"");
737 logln("text for parsing: \"" + forParsing + "\"");
738 UnicodeString str;
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");
749 pat.remove();
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));
756 int32_t count1 = 0;
757 pp.setIndex(0);
758 Formattable *newobjs = mf->parse(result, pp, count1);
759 // newobjs now equals {new Double(3.1)}
73c04bcf
A
760 if (newobjs == NULL) {
761 dataerrln("Error calling MessageFormat::parse");
762 } else {
763 if (newobjs[0].getDouble() != 3.1)
764 errln( UnicodeString("newobjs[0] = ") + newobjs[0].getDouble());
765 }
b75a7d8f
A
766
767 delete [] objs;
768 delete [] newobjs;
769 delete mf;
770}
771/* @bug 4105380
772 * When using ChoiceFormat, MessageFormat is not good for I18n.
773 */
774void MessageFormatRegressionTest::Test4105380()
775{
776 UnicodeString patternText1("The disk \"{1}\" contains {0}.");
777 UnicodeString patternText2("There are {0} on the disk \"{1}\"");
778 UErrorCode status = U_ZERO_ERROR;
779 MessageFormat *form1 = new MessageFormat(patternText1, status);
780 failure(status, "new MessageFormat");
781 MessageFormat *form2 = new MessageFormat(patternText2, status);
782 failure(status, "new MessageFormat");
783 double filelimits [] = {0,1,2};
784 UnicodeString filepart [] = {
785 (UnicodeString)"no files",
786 (UnicodeString)"one file",
787 (UnicodeString)"{0,number} files"
788 };
789 ChoiceFormat *fileform = new ChoiceFormat(filelimits, filepart, 3);
790 form1->setFormat(1, *fileform);
791 form2->setFormat(0, *fileform);
792 //Object[] testArgs = {new Long(12373), "MyDisk"};
793 Formattable testArgs [] = {
794 Formattable((int32_t)12373),
795 Formattable((UnicodeString)"MyDisk")
796 };
797
798 FieldPosition bogus(FieldPosition::DONT_CARE);
799
800 UnicodeString result;
801 logln(form1->format(testArgs, 2, result, bogus, status));
802 failure(status, "form1->format");
803 result.remove();
804 logln(form2->format(testArgs, 2, result, bogus, status));
805 failure(status, "form1->format");
806
807 delete form1;
808 delete form2;
809 delete fileform;
810}
811/* @bug 4120552
812 * MessageFormat.parse incorrectly sets errorIndex.
813 */
814void MessageFormatRegressionTest::Test4120552()
815{
816 UErrorCode status = U_ZERO_ERROR;
817 MessageFormat *mf = new MessageFormat("pattern", status);
818 failure(status, "new MessageFormat");
819 UnicodeString texts[] = {
820 (UnicodeString)"pattern",
821 (UnicodeString)"pat",
822 (UnicodeString)"1234"
823 };
824 UnicodeString pat;
825 logln("pattern: \"" + mf->toPattern(pat) + "\"");
826 for (int i = 0; i < 3; i++) {
827 ParsePosition pp(0);
828 //Object[] objs = mf.parse(texts[i], pp);
829 int32_t count = 0;
830 Formattable *objs = mf->parse(texts[i], pp, count);
831 log(" text for parsing: \"" + texts[i] + "\"");
832 if (objs == NULL) {
833 logln(" (incorrectly formatted string)");
834 if (pp.getErrorIndex() == -1)
835 errln(UnicodeString("Incorrect error index: ") + pp.getErrorIndex());
836 } else {
837 logln(" (correctly formatted string)");
838 delete[] objs;
839 }
840 }
841 delete mf;
842}
843
844/**
845 * @bug 4142938
846 * MessageFormat handles single quotes in pattern wrong.
847 * This is actually a problem in ChoiceFormat; it doesn't
848 * understand single quotes.
849 */
850void MessageFormatRegressionTest::Test4142938()
851{
852 UnicodeString pat = CharsToUnicodeString("''Vous'' {0,choice,0#n''|1#}avez s\\u00E9lectionn\\u00E9 "
853 "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} "
854 "personnel{0,choice,0#s|1#|2#s}.");
855 UErrorCode status = U_ZERO_ERROR;
856 MessageFormat *mf = new MessageFormat(pat, status);
857 failure(status, "new MessageFormat");
858
859 UnicodeString PREFIX [] = {
860 CharsToUnicodeString("'Vous' n'avez s\\u00E9lectionn\\u00E9 aucun clients personnels."),
861 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 "),
862 CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 ")
863 };
864 UnicodeString SUFFIX [] = {
865 UnicodeString(),
866 UNICODE_STRING(" client personnel.", 18),
867 UNICODE_STRING(" clients personnels.", 20)
868 };
869
870 for (int i=0; i<3; i++) {
871 UnicodeString out;
872 //out = mf->format(new Object[]{new Integer(i)});
873 Formattable objs [] = {
874 Formattable((int32_t)i)
875 };
876 FieldPosition pos(FieldPosition::DONT_CARE);
877 out = mf->format(objs, 1, out, pos, status);
878 failure(status, "mf->format");
879 if (SUFFIX[i] == "") {
880 if (out != PREFIX[i])
881 errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
882 }
883 else {
884 if (!out.startsWith(PREFIX[i]) ||
885 !out.endsWith(SUFFIX[i]))
886 errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
887 SUFFIX[i] + "\"");
888 }
889 }
890
891 delete mf;
892}
893
894/**
895 * @bug 4142938
896 * Test the applyPattern and toPattern handling of single quotes
897 * by ChoiceFormat. (This is in here because this was a bug reported
898 * against MessageFormat.) The single quote is used to quote the
899 * pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row
900 * is a quote literal.
901 */
902void MessageFormatRegressionTest::TestChoicePatternQuote()
903{
904 UnicodeString DATA [] = {
905 // Pattern 0 value 1 value
906 // {sfb} hacked - changed \u2264 to = (copied from Character Map)
907 (UnicodeString)"0#can''t|1#can", (UnicodeString)"can't", (UnicodeString)"can",
908 (UnicodeString)"0#'pound(#)=''#'''|1#xyz", (UnicodeString)"pound(#)='#'", (UnicodeString)"xyz",
909 (UnicodeString)"0#'1<2 | 1=1'|1#''", (UnicodeString)"1<2 | 1=1", (UnicodeString)"'",
910 };
911 for (int i=0; i<9; i+=3) {
912 //try {
913 UErrorCode status = U_ZERO_ERROR;
914 ChoiceFormat *cf = new ChoiceFormat(DATA[i], status);
915 failure(status, "new ChoiceFormat");
916 for (int j=0; j<=1; ++j) {
917 UnicodeString out;
918 FieldPosition pos(FieldPosition::DONT_CARE);
919 out = cf->format((double)j, out, pos);
920 if (out != DATA[i+1+j])
921 errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " +
922 out + "; want \"" + DATA[i+1+j] + '"');
923 }
924 UnicodeString pat;
925 pat = cf->toPattern(pat);
926 UnicodeString pat2;
927 ChoiceFormat *cf2 = new ChoiceFormat(pat, status);
928 pat2 = cf2->toPattern(pat2);
929 if (pat != pat2)
930 errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
931 else
932 logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
933 /*}
934 catch (IllegalArgumentException e) {
935 errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
936 }*/
937
938 delete cf;
939 delete cf2;
940 }
941}
942
943/**
944 * @bug 4112104
945 * MessageFormat.equals(null) throws a NullPointerException. The JLS states
946 * that it should return false.
947 */
948void MessageFormatRegressionTest::Test4112104()
949{
950 UErrorCode status = U_ZERO_ERROR;
951 MessageFormat *format = new MessageFormat("", status);
952 failure(status, "new MessageFormat");
953 //try {
954 // This should NOT throw an exception
955 if (format == NULL) {
956 // It also should return false
957 errln("MessageFormat.equals(null) returns false");
958 }
959 /*}
960 catch (NullPointerException e) {
961 errln("MessageFormat.equals(null) throws " + e);
962 }*/
963 delete format;
964}
965
966#endif /* #if !UCONFIG_NO_FORMATTING */