1
/********************************************************************
3
* Copyright (c) 1997-2001, International Business Machines Corporation and
4
* others. All Rights Reserved.
5
********************************************************************/
9
#include "unicode/format.h"
10
#include "unicode/decimfmt.h"
11
#include "unicode/locid.h"
12
#include "unicode/msgfmt.h"
13
#include "unicode/numfmt.h"
14
#include "unicode/choicfmt.h"
15
#include "unicode/gregocal.h"
17
// *****************************************************************************
18
// class MessageFormatRegressionTest
19
// *****************************************************************************
21
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
24
MessageFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
26
// if (exec) logln((UnicodeString)"TestSuite MessageFormatRegressionTest");
47
CASE(19,TestChoicePatternQuote)
50
default: name = ""; break;
55
MessageFormatRegressionTest::failure(UErrorCode status, const char* msg)
57
if(U_FAILURE(status)) {
58
errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
66
* Null exception when formatting pattern with MessageFormat
69
void MessageFormatRegressionTest::Test4074764() {
70
UnicodeString pattern [] = {
71
"Message without param",
72
"Message with param:{0}",
73
"Longer Message with param {0}"
75
//difference between the two param strings are that
76
//in the first one, the param position is within the
77
//length of the string without param while it is not so
80
UErrorCode status = U_ZERO_ERROR;
81
MessageFormat *messageFormatter = new MessageFormat("", status);
83
failure(status, "couldn't create MessageFormat");
86
//Apply pattern with param and print the result
87
messageFormatter->applyPattern(pattern[1], status);
88
failure(status, "messageFormat->applyPattern");
89
//Object[] params = {new UnicodeString("BUG"), new Date()};
90
Formattable params [] = {
91
Formattable(UnicodeString("BUG")),
92
Formattable(0, Formattable::kIsDate)
94
UnicodeString tempBuffer;
95
FieldPosition pos(FieldPosition::DONT_CARE);
96
tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status);
97
if( tempBuffer != "Message with param:BUG" || failure(status, "messageFormat->format"))
98
errln("MessageFormat with one param test failed.");
99
logln("Formatted with one extra param : " + tempBuffer);
101
//Apply pattern without param and print the result
102
messageFormatter->applyPattern(pattern[0], status);
103
failure(status, "messageFormatter->applyPattern");
105
// {sfb} how much does this apply in C++?
106
// do we want to verify that the Formattable* array is not NULL,
107
// or is that the user's responsibility?
108
// additionally, what should be the item count?
109
// for bug testing purposes, assume that something was set to
110
// NULL by mistake, and that the length should be non-zero
112
//tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
114
tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status);
116
if( tempBuffer != "Message without param" || failure(status, "messageFormat->format"))
117
errln("MessageFormat with no param test failed.");
118
logln("Formatted with no params : " + tempBuffer);
121
tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status);
122
if (tempBuffer != "Message without param" || failure(status, "messageFormat->format"))
123
errln("Formatted with arguments > subsitution failed. result = " + tempBuffer);
124
logln("Formatted with extra params : " + tempBuffer);
125
//This statement gives an exception while formatting...
126
//If we use pattern[1] for the message with param,
127
//we get an NullPointerException in MessageFormat.java(617)
128
//If we use pattern[2] for the message with param,
129
//we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
130
//Both are due to maxOffset not being reset to -1
131
//in applyPattern() when the pattern does not
133
/*} catch (Exception foo) {
134
errln("Exception when formatting with no params.");
137
delete messageFormatter;
141
* MessageFormat.toPattern has weird rounding behavior.
143
void MessageFormatRegressionTest::Test4058973()
145
UErrorCode status = U_ZERO_ERROR;
146
MessageFormat *fmt = new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}", status);
147
failure(status, "new MessageFormat");
150
pat = fmt->toPattern(pat);
151
UnicodeString exp("{0,choice,0.0#no files|1.0#one file|1.0< {0,number,integer} files}");
153
errln("MessageFormat.toPattern failed");
154
errln("Exp: " + exp);
155
errln("Got: " + pat);
161
* More robust message formats.
163
void MessageFormatRegressionTest::Test4031438()
165
UErrorCode status = U_ZERO_ERROR;
167
UnicodeString pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}.");
168
UnicodeString pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.");
170
MessageFormat *messageFormatter = new MessageFormat("", status);
171
failure(status, "new MessageFormat");
174
logln("Apply with pattern : " + pattern1);
175
messageFormatter->applyPattern(pattern1, status);
176
failure(status, "messageFormat->applyPattern");
177
//Object[] params = {new Integer(7)};
178
Formattable params []= {
179
Formattable((int32_t)7)
181
UnicodeString tempBuffer;
182
FieldPosition pos(FieldPosition::DONT_CARE);
183
tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status);
184
if(tempBuffer != "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status, "MessageFormat::format"))
185
errln("Tests arguments < substitution failed");
186
logln("Formatted with 7 : " + tempBuffer);
189
Formattable *objs = messageFormatter->parse(tempBuffer, pp, count);
190
//if(objs[7/*params.length*/] != NULL)
191
// errln("Parse failed with more than expected arguments");
193
NumberFormat *fmt = 0;
194
UnicodeString temp, temp1;
196
for (int i = 0; i < count; i++) {
198
// convert to string if not already
199
Formattable obj = objs[i];
201
if(obj.getType() == Formattable::kString)
202
temp = obj.getString(temp);
204
fmt = NumberFormat::createInstance(status);
205
fmt->format(obj.getType() == Formattable::kLong ? obj.getLong() : obj.getDouble(), temp);
208
// convert to string if not already
209
Formattable obj1 = params[i];
211
if(obj1.getType() == Formattable::kDouble || obj1.getType() == Formattable::kLong) {
212
fmt = NumberFormat::createInstance(status);
213
fmt->format(obj1.getType() == Formattable::kLong ? obj1.getLong() : obj1.getDouble(), temp1);
216
temp1 = obj1.getString(temp1);
218
//if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
220
errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i);
228
// {sfb} does this apply? no way to really pass a null Formattable,
231
/*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
232
if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format"))
233
errln("Tests with no arguments failed");
234
logln("Formatted with null : " + tempBuffer);*/
235
logln("Apply with pattern : " + pattern2);
236
messageFormatter->applyPattern(pattern2, status);
237
failure(status, "messageFormatter->applyPattern");
239
tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status);
240
if (tempBuffer != "Double ' Quotes 7 test and quoted {1} test plus other {2} stuff.")
241
errln("quote format test (w/ params) failed.");
242
logln("Formatted with params : " + tempBuffer);
244
/*tempBuffer = messageFormatter->format(null);
245
if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
246
errln("quote format test (w/ null) failed.");
247
logln("Formatted with null : " + tempBuffer);
248
logln("toPattern : " + messageFormatter.toPattern());*/
249
/*} catch (Exception foo) {
250
errln("Exception when formatting in bug 4031438. "+foo.getMessage());
252
delete messageFormatter;
255
void MessageFormatRegressionTest::Test4052223()
258
ParsePosition pos(0);
259
if (pos.getErrorIndex() != -1) {
260
errln("ParsePosition.getErrorIndex initialization failed.");
263
UErrorCode status = U_ZERO_ERROR;
264
MessageFormat *fmt = new MessageFormat("There are {0} apples growing on the {1} tree.", status);
265
failure(status, "new MessageFormat");
266
UnicodeString str("There is one apple growing on the peach tree.");
269
fmt->parse(str, pos, count);
271
logln(UnicodeString("unparsable string , should fail at ") + pos.getErrorIndex());
272
if (pos.getErrorIndex() == -1)
273
errln("Bug 4052223 failed : parsing string " + str);
274
pos.setErrorIndex(4);
275
if (pos.getErrorIndex() != 4)
276
errln(UnicodeString("setErrorIndex failed, got ") + pos.getErrorIndex() + " instead of 4");
278
ChoiceFormat *f = new ChoiceFormat(
279
"-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.", status);
280
failure(status, "new ChoiceFormat");
282
pos.setErrorIndex(-1);
284
f->parse("are negative", obj, pos);
285
if (pos.getErrorIndex() != -1 && obj.getDouble() == -1.0)
286
errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos.getErrorIndex());
288
pos.setErrorIndex(-1);
289
f->parse("are no or fraction ", obj, pos);
290
if (pos.getErrorIndex() != -1 && obj.getDouble() == 0.0)
291
errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos.getErrorIndex());
293
pos.setErrorIndex(-1);
294
f->parse("go postal", obj, pos);
295
if (pos.getErrorIndex() == -1 && ! uprv_isNaN(obj.getDouble()))
296
errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos.getErrorIndex());
302
* ChoiceFormat.equals(null) throws NullPointerException
305
// {sfb} not really applicable in C++?? (kind of silly)
307
void MessageFormatRegressionTest::Test4104976()
309
double limits [] = {1, 20};
310
UnicodeString formats [] = {
311
UnicodeString("xyz"),
314
int32_t formats_length = (int32_t)(sizeof(formats)/sizeof(formats[0]));
315
UErrorCode status = U_ZERO_ERROR;
316
ChoiceFormat *cf = new ChoiceFormat(limits, formats, formats_length);
317
failure(status, "new ChoiceFormat");
319
log("Compares to null is always false, returned : ");
320
logln(cf == NULL ? "TRUE" : "FALSE");
321
/*} catch (Exception foo) {
322
errln("ChoiceFormat.equals(null) throws exception.");
329
* ChoiceFormat.ctor(double[], String[]) doesn't check
330
* whether lengths of input arrays are equal.
333
// {sfb} again, not really applicable in C++
335
void MessageFormatRegressionTest::Test4106659()
341
UnicodeString formats [] = {
344
ChoiceFormat *cf = NULL;
346
// cf = new ChoiceFormat(limits, formats, 3);
347
//} catch (Exception foo) {
348
// logln("ChoiceFormat constructor should check for the array lengths");
352
// errln(cf->format(5));
359
* ChoiceFormat.ctor(double[], String[]) allows unordered double array.
360
* This is not a bug, added javadoc to emphasize the use of limit
361
* array must be in ascending order.
363
void MessageFormatRegressionTest::Test4106660()
365
double limits [] = {3, 1, 2};
366
UnicodeString formats [] = {
367
UnicodeString("Three"),
368
UnicodeString("One"),
371
ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3);
374
FieldPosition pos(FieldPosition::DONT_CARE);
375
str = cf->format(d, str, pos);
377
errln( (UnicodeString) "format(" + d + ") = " + str);
383
* MessageFormat is incorrectly serialized/deserialized.
386
// {sfb} doesn't apply in C++
388
void MessageFormatRegressionTest::Test4111739()
390
/*MessageFormat format1 = null;
391
MessageFormat format2 = null;
392
ObjectOutputStream ostream = null;
393
ByteArrayOutputStream baos = null;
394
ObjectInputStream istream = null;
397
baos = new ByteArrayOutputStream();
398
ostream = new ObjectOutputStream(baos);
399
} catch(IOException e) {
400
errln("Unexpected exception : " + e.getMessage());
405
format1 = new MessageFormat("pattern{0}");
406
ostream.writeObject(format1);
409
byte bytes[] = baos.toByteArray();
411
istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
412
format2 = (MessageFormat)istream.readObject();
413
} catch(Exception e) {
414
errln("Unexpected exception : " + e.getMessage());
417
if (!format1.equals(format2)) {
418
errln("MessageFormats before and after serialization are not" +
419
" equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
420
format2 + "(" + format2.toPattern() + ")");
422
logln("Serialization for MessageFormat is OK.");
426
* MessageFormat.applyPattern allows illegal patterns.
428
void MessageFormatRegressionTest::Test4114743()
430
UnicodeString originalPattern("initial pattern");
431
UErrorCode status = U_ZERO_ERROR;
432
MessageFormat *mf = new MessageFormat(originalPattern, status);
433
failure(status, "new MessageFormat");
435
UnicodeString illegalPattern("ab { '}' de");
436
mf->applyPattern(illegalPattern, status);
437
if( ! U_FAILURE(status))
438
errln("illegal pattern: \"" + illegalPattern + "\"");
439
/*} catch (IllegalArgumentException foo) {
440
if (!originalPattern.equals(mf.toPattern()))
441
errln("pattern after: \"" + mf.toPattern() + "\"");
447
* MessageFormat.parse has different behavior in case of null.
449
void MessageFormatRegressionTest::Test4116444()
451
UnicodeString patterns [] = {
453
(UnicodeString)"one",
454
(UnicodeString) "{0,date,short}"
457
UErrorCode status = U_ZERO_ERROR;
458
MessageFormat *mf = new MessageFormat("", status);
459
failure(status, "new MessageFormat");
461
for (int i = 0; i < 3; i++) {
462
UnicodeString pattern = patterns[i];
463
mf->applyPattern(pattern, status);
464
failure(status, "mf->applyPattern");
469
Formattable *array = mf->parse(UnicodeString(""), pp, count);
470
logln("pattern: \"" + pattern + "\"");
471
log(" parsedObjects: ");
474
for (int j = 0; j < count; j++) {
475
//if (array[j] != null)
477
err("\"" + array[j].getString(dummy) + "\"");
489
/*} catch (Exception e) {
490
errln("pattern: \"" + pattern + "\"");
491
errln(" Exception: " + e.getMessage());
497
/* @bug 4114739 (FIX and add javadoc)
498
* MessageFormat.format has undocumented behavior about empty format objects.
501
// {sfb} doesn't apply in C++?
502
void MessageFormatRegressionTest::Test4114739()
505
UErrorCode status = U_ZERO_ERROR;
506
MessageFormat *mf = new MessageFormat("<{0}>", status);
507
failure(status, "new MessageFormat");
509
Formattable *objs1 = NULL;
510
//Formattable objs2 [] = {};
511
//Formattable *objs3 [] = {NULL};
515
logln("pattern: \"" + mf->toPattern(pat) + "\"");
516
log("format(null) : ");
517
FieldPosition pos(FieldPosition::DONT_CARE);
518
logln("\"" + mf->format(objs1, 0, res, pos, status) + "\"");
519
failure(status, "mf->format");
520
/*log("format({}) : ");
521
logln("\"" + mf->format(objs2, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
522
failure(status, "mf->format");
523
log("format({null}) :");
524
logln("\"" + mf->format(objs3, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
525
failure(status, "mf->format");*/
526
/*} catch (Exception e) {
527
errln("Exception thrown for null argument tests.");
534
* MessageFormat.applyPattern works wrong with illegal patterns.
536
void MessageFormatRegressionTest::Test4113018()
538
UnicodeString originalPattern("initial pattern");
539
UErrorCode status = U_ZERO_ERROR;
540
MessageFormat *mf = new MessageFormat(originalPattern, status);
541
failure(status, "new messageFormat");
542
UnicodeString illegalPattern("format: {0, xxxYYY}");
544
logln("pattern before: \"" + mf->toPattern(pat) + "\"");
545
logln("illegal pattern: \"" + illegalPattern + "\"");
547
mf->applyPattern(illegalPattern, status);
548
if( ! U_FAILURE(status))
549
errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern);
550
/*} catch (IllegalArgumentException e) {
551
if (!originalPattern.equals(mf.toPattern()))
552
errln("pattern after: \"" + mf.toPattern() + "\"");
558
* ChoiceFormat is silent about the pattern usage in javadoc.
560
void MessageFormatRegressionTest::Test4106661()
562
UErrorCode status = U_ZERO_ERROR;
563
ChoiceFormat *fmt = new ChoiceFormat(
564
"-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status);
565
failure(status, "new ChoiceFormat");
567
logln("Formatter Pattern : " + fmt->toPattern(pat));
569
FieldPosition bogus(FieldPosition::DONT_CARE);
572
// Will this work for -inf?
573
logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
574
failure(status, "fmt->format");
576
logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
577
failure(status, "fmt->format");
579
logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
580
failure(status, "fmt->format");
582
logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
583
failure(status, "fmt->format");
585
logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
586
failure(status, "fmt->format");
588
logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
589
failure(status, "fmt->format");
591
logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
592
failure(status, "fmt->format");
594
logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
595
failure(status, "fmt->format");
597
logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
598
failure(status, "fmt->format");
600
logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
601
failure(status, "fmt->format");
603
logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
604
failure(status, "fmt->format");
610
* ChoiceFormat should accept \u221E as eq. to INF.
612
void MessageFormatRegressionTest::Test4094906()
614
UErrorCode status = U_ZERO_ERROR;
615
UnicodeString pattern("-");
616
pattern += (UChar) 0x221E;
617
pattern += "<are negative|0.0<are no or fraction|1.0#is one|1.0<is 1+|";
618
pattern += (UChar) 0x221E;
619
pattern += "<are many.";
621
ChoiceFormat *fmt = new ChoiceFormat(pattern, status);
622
failure(status, "new ChoiceFormat");
624
if (fmt->toPattern(pat) != pattern) {
625
errln( (UnicodeString) "Formatter Pattern : " + pat);
626
errln( (UnicodeString) "Expected Pattern : " + pattern);
628
FieldPosition bogus(FieldPosition::DONT_CARE);
631
// Will this work for -inf?
632
logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
633
failure(status, "fmt->format");
635
logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
636
failure(status, "fmt->format");
638
logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
639
failure(status, "fmt->format");
641
logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
642
failure(status, "fmt->format");
644
logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
645
failure(status, "fmt->format");
647
logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
648
failure(status, "fmt->format");
650
logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
651
failure(status, "fmt->format");
653
logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
654
failure(status, "fmt->format");
656
logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
657
failure(status, "fmt->format");
659
logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
660
failure(status, "fmt->format");
662
logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
663
failure(status, "fmt->format");
669
* MessageFormat.parse fails with ChoiceFormat.
671
void MessageFormatRegressionTest::Test4118592()
673
UErrorCode status = U_ZERO_ERROR;
674
MessageFormat *mf = new MessageFormat("", status);
675
failure(status, "new messageFormat");
676
UnicodeString pattern("{0,choice,1#YES|2#NO}");
677
UnicodeString prefix("");
678
Formattable *objs = 0;
680
for (int i = 0; i < 5; i++) {
681
UnicodeString formatted;
682
formatted = prefix + "YES";
683
mf->applyPattern(prefix + pattern, status);
684
failure(status, "mf->applyPattern");
686
//Object[] objs = mf.parse(formatted, new ParsePosition(0));
689
objs = mf->parse(formatted, pp, count);
691
logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\"");
692
log(" \"" + formatted + "\" parsed as ");
697
if(objs[0].getType() == Formattable::kString)
698
logln((UnicodeString)" " + objs[0].getString(temp));
700
logln((UnicodeString)" " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble()));
709
* MessageFormat.parse fails for some patterns.
711
void MessageFormatRegressionTest::Test4118594()
713
UErrorCode status = U_ZERO_ERROR;
714
MessageFormat *mf = new MessageFormat("{0}, {0}, {0}", status);
715
failure(status, "new MessageFormat");
716
UnicodeString forParsing("x, y, z");
717
//Object[] objs = mf.parse(forParsing, new ParsePosition(0));
720
Formattable *objs = mf->parse(forParsing, pp, count);
722
logln("pattern: \"" + mf->toPattern(pat) + "\"");
723
logln("text for parsing: \"" + forParsing + "\"");
725
if (objs[0].getString(str) != "z")
726
errln("argument0: \"" + objs[0].getString(str) + "\"");
727
mf->applyPattern("{0,number,#.##}, {0,number,#.#}", status);
728
failure(status, "mf->applyPattern");
729
//Object[] oldobjs = {new Double(3.1415)};
730
Formattable oldobjs [] = {Formattable(3.1415)};
731
UnicodeString result;
732
FieldPosition pos(FieldPosition::DONT_CARE);
733
result = mf->format( oldobjs, 1, result, pos, status );
734
failure(status, "mf->format");
736
logln("pattern: \"" + mf->toPattern(pat) + "\"");
737
logln("text for parsing: \"" + result + "\"");
738
// result now equals "3.14, 3.1"
739
if (result != "3.14, 3.1")
740
errln("result = " + result);
741
//Object[] newobjs = mf.parse(result, new ParsePosition(0));
744
Formattable *newobjs = mf->parse(result, pp, count1);
745
// newobjs now equals {new Double(3.1)}
746
if (newobjs[0].getDouble() != 3.1)
747
errln( UnicodeString("newobjs[0] = ") + newobjs[0].getDouble());
754
* When using ChoiceFormat, MessageFormat is not good for I18n.
756
void MessageFormatRegressionTest::Test4105380()
758
UnicodeString patternText1("The disk \"{1}\" contains {0}.");
759
UnicodeString patternText2("There are {0} on the disk \"{1}\"");
760
UErrorCode status = U_ZERO_ERROR;
761
MessageFormat *form1 = new MessageFormat(patternText1, status);
762
failure(status, "new MessageFormat");
763
MessageFormat *form2 = new MessageFormat(patternText2, status);
764
failure(status, "new MessageFormat");
765
double filelimits [] = {0,1,2};
766
UnicodeString filepart [] = {
767
(UnicodeString)"no files",
768
(UnicodeString)"one file",
769
(UnicodeString)"{0,number} files"
771
ChoiceFormat *fileform = new ChoiceFormat(filelimits, filepart, 3);
772
form1->setFormat(1, *fileform);
773
form2->setFormat(0, *fileform);
774
//Object[] testArgs = {new Long(12373), "MyDisk"};
775
Formattable testArgs [] = {
776
Formattable((int32_t)12373),
777
Formattable((UnicodeString)"MyDisk")
780
FieldPosition bogus(FieldPosition::DONT_CARE);
782
UnicodeString result;
783
logln(form1->format(testArgs, 2, result, bogus, status));
784
failure(status, "form1->format");
786
logln(form2->format(testArgs, 2, result, bogus, status));
787
failure(status, "form1->format");
794
* MessageFormat.parse incorrectly sets errorIndex.
796
void MessageFormatRegressionTest::Test4120552()
798
UErrorCode status = U_ZERO_ERROR;
799
MessageFormat *mf = new MessageFormat("pattern", status);
800
failure(status, "new MessageFormat");
801
UnicodeString texts[] = {
802
(UnicodeString)"pattern",
803
(UnicodeString)"pat",
804
(UnicodeString)"1234"
807
logln("pattern: \"" + mf->toPattern(pat) + "\"");
808
for (int i = 0; i < 3; i++) {
810
//Object[] objs = mf.parse(texts[i], pp);
812
Formattable *objs = mf->parse(texts[i], pp, count);
813
log(" text for parsing: \"" + texts[i] + "\"");
815
logln(" (incorrectly formatted string)");
816
if (pp.getErrorIndex() == -1)
817
errln(UnicodeString("Incorrect error index: ") + pp.getErrorIndex());
819
logln(" (correctly formatted string)");
828
* MessageFormat handles single quotes in pattern wrong.
829
* This is actually a problem in ChoiceFormat; it doesn't
830
* understand single quotes.
832
void MessageFormatRegressionTest::Test4142938()
834
UnicodeString pat = CharsToUnicodeString("''Vous'' {0,choice,0#n''|1#}avez s\\u00E9lectionn\\u00E9 "
835
"{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} "
836
"personnel{0,choice,0#s|1#|2#s}.");
837
UErrorCode status = U_ZERO_ERROR;
838
MessageFormat *mf = new MessageFormat(pat, status);
839
failure(status, "new MessageFormat");
841
UnicodeString PREFIX [] = {
842
CharsToUnicodeString("'Vous' n'avez s\\u00E9lectionn\\u00E9 aucun clients personnels."),
843
CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 "),
844
CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 ")
846
UnicodeString SUFFIX [] = {
848
UNICODE_STRING(" client personnel.", 18),
849
UNICODE_STRING(" clients personnels.", 20)
852
for (int i=0; i<3; i++) {
854
//out = mf->format(new Object[]{new Integer(i)});
855
Formattable objs [] = {
856
Formattable((int32_t)i)
858
FieldPosition pos(FieldPosition::DONT_CARE);
859
out = mf->format(objs, 1, out, pos, status);
860
failure(status, "mf->format");
861
if (SUFFIX[i] == "") {
862
if (out != PREFIX[i])
863
errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
866
if (!out.startsWith(PREFIX[i]) ||
867
!out.endsWith(SUFFIX[i]))
868
errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
878
* Test the applyPattern and toPattern handling of single quotes
879
* by ChoiceFormat. (This is in here because this was a bug reported
880
* against MessageFormat.) The single quote is used to quote the
881
* pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row
882
* is a quote literal.
884
void MessageFormatRegressionTest::TestChoicePatternQuote()
886
UnicodeString DATA [] = {
887
// Pattern 0 value 1 value
888
// {sfb} hacked - changed \u2264 to = (copied from Character Map)
889
(UnicodeString)"0#can''t|1#can", (UnicodeString)"can't", (UnicodeString)"can",
890
(UnicodeString)"0#'pound(#)=''#'''|1#xyz", (UnicodeString)"pound(#)='#'", (UnicodeString)"xyz",
891
(UnicodeString)"0#'1<2 | 1=1'|1#''", (UnicodeString)"1<2 | 1=1", (UnicodeString)"'",
893
for (int i=0; i<9; i+=3) {
895
UErrorCode status = U_ZERO_ERROR;
896
ChoiceFormat *cf = new ChoiceFormat(DATA[i], status);
897
failure(status, "new ChoiceFormat");
898
for (int j=0; j<=1; ++j) {
900
FieldPosition pos(FieldPosition::DONT_CARE);
901
out = cf->format((double)j, out, pos);
902
if (out != DATA[i+1+j])
903
errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " +
904
out + "; want \"" + DATA[i+1+j] + '"');
907
pat = cf->toPattern(pat);
909
ChoiceFormat *cf2 = new ChoiceFormat(pat, status);
910
pat2 = cf2->toPattern(pat2);
912
errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
914
logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
916
catch (IllegalArgumentException e) {
917
errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
927
* MessageFormat.equals(null) throws a NullPointerException. The JLS states
928
* that it should return false.
930
void MessageFormatRegressionTest::Test4112104()
932
UErrorCode status = U_ZERO_ERROR;
933
MessageFormat *format = new MessageFormat("", status);
934
failure(status, "new MessageFormat");
936
// This should NOT throw an exception
937
if (format == NULL) {
938
// It also should return false
939
errln("MessageFormat.equals(null) returns false");
942
catch (NullPointerException e) {
943
errln("MessageFormat.equals(null) throws " + e);