1
/********************************************************************
3
* Copyright (c) 1997-2001, International Business Machines Corporation and
4
* others. All Rights Reserved.
5
********************************************************************/
9
* Modification History:
11
* Date Name Description
12
* 03/24/97 helena Converted from Java.
13
* 07/11/97 helena Updated to work on AIX.
14
* 08/04/97 jfitz Updated to intltest
15
********************************************************************************
19
#include "unicode/format.h"
20
#include "unicode/decimfmt.h"
21
#include "unicode/locid.h"
22
#include "unicode/msgfmt.h"
23
#include "unicode/numfmt.h"
24
#include "unicode/choicfmt.h"
25
#include "unicode/gregocal.h"
29
void TestMessageFormat::testBug3()
31
double myNumber = -123456;
32
DecimalFormat *form = 0;
41
Locale("de", "AT", ""),
42
Locale("de", "CH", ""),
43
Locale("el", "", ""), // 10
44
Locale("en", "CA", ""),
45
Locale("en", "GB", ""),
46
Locale("en", "IE", ""),
47
Locale("en", "US", ""),
52
Locale("fr", "BE", ""),
53
Locale("fr", "CA", ""), // 20
54
Locale("fr", "CH", ""),
60
Locale("it", "CH", ""),
63
Locale("lt", "", ""), // 30
67
Locale("nl", "BE", ""),
73
Locale("sh", "", ""), // 40
82
Locale("zh", "TW", "") // 49
85
for (i= 0; i < 49; i++) {
87
logln(locale[i].getDisplayName(buffer));
88
UErrorCode success = U_ZERO_ERROR;
89
// form = (DecimalFormat*)NumberFormat::createCurrencyInstance(locale[i], success);
90
form = (DecimalFormat*)NumberFormat::createInstance(locale[i], success);
91
if (U_FAILURE(success)) {
92
errln("Err: Number Format ");
93
logln("Number format creation failed.");
99
form->format(myNumber, buffer, pos);
100
success = U_ZERO_ERROR;
101
ParsePosition parsePos;
102
form->parse(buffer, result, parsePos);
104
it_out << " -> " /*<< dec*/ /*<< result*/ << "[supposed output for result]" <<endl;
106
if (U_FAILURE(success)) {
107
errln("Err: Number Format parse");
108
logln("Number format parse failed.");
114
void TestMessageFormat::testBug1()
116
const double limit[] = {0.0, 1.0, 2.0};
117
const UnicodeString formats[] = {"0.0<=Arg<1.0",
120
ChoiceFormat *cf = new ChoiceFormat(limit, formats, 3);
121
FieldPosition status(0);
122
UnicodeString toAppendTo;
123
cf->format((int32_t)1, toAppendTo, status);
124
if (toAppendTo != "1.0<=Arg<2.0") {
125
errln("ChoiceFormat cmp in testBug1");
131
void TestMessageFormat::testBug2()
133
UErrorCode status = U_ZERO_ERROR;
134
UnicodeString result;
135
// {sfb} use double format in pattern, so result will match (not strictly necessary)
136
const UnicodeString pattern = "There {0,choice,0.0#are no files|1.0#is one file|1.0<are {0, number} files} on disk {1}. ";
137
logln("The input pattern : " + pattern);
138
MessageFormat *fmt = new MessageFormat(pattern, status);
139
if (U_FAILURE(status)) {
140
errln("MessageFormat pattern creation failed.");
141
logln("MessageFormat pattern creation failed.");
144
logln("The output pattern is : " + fmt->toPattern(result));
145
if (pattern != result) {
146
errln("MessageFormat::toPattern() failed.");
147
logln("MessageFormat::toPattern() failed.");
153
#if defined(_DEBUG) && U_IOSTREAM_SOURCE!=0
154
//----------------------------------------------------
156
//----------------------------------------------------
158
#if U_IOSTREAM_SOURCE >= 199711
160
std::ostream& operator<<(std::ostream& stream, const Formattable& obj);
161
#elif U_IOSTREAM_SOURCE >= 198506
162
# include <iostream.h>
163
ostream& operator<<(ostream& stream, const Formattable& obj);
166
#include "unicode/datefmt.h"
172
operator<<( IntlTest& stream,
173
const Formattable& obj)
175
static DateFormat *defDateFormat = 0;
177
UnicodeString buffer;
178
switch(obj.getType()) {
179
case Formattable::kDate :
180
if (defDateFormat == 0) {
181
defDateFormat = DateFormat::createInstance();
183
defDateFormat->format(obj.getDate(), buffer);
186
case Formattable::kDouble :
188
sprintf( convert, "%lf", obj.getDouble() );
189
stream << convert << "D";
191
case Formattable::kLong :
192
stream << obj.getLong() << "L";
194
case Formattable::kString:
195
stream << "\"" << obj.getString(buffer) << "\"";
197
case Formattable::kArray:
199
const Formattable* array;
200
array = obj.getArray(count);
202
for (i=0; i<count; ++i) stream << array[i] << ( (i==(count-1)) ? "" : ", " );
206
stream << "INVALID_Formattable";
210
#endif /* defined(_DEBUG) && U_IOSTREAM_SOURCE!=0 */
213
void TestMessageFormat::PatternTest()
215
Formattable testArgs[] = {
216
Formattable(double(1)), Formattable(double(3456)),
217
Formattable("Disk"), Formattable(UDate((int32_t)1000000000L), Formattable::kIsDate)
219
UnicodeString testCases[] = {
220
"Quotes '', '{', 'a' {0} '{0}'",
221
"Quotes '', '{', 'a' {0,number} '{0}'",
222
"'{'1,number,'#',##} {1,number,'#',##}",
223
"There are {1} files on {2} at {3}.",
224
"On {2}, there are {1} files, with {0,number,currency}.",
225
"'{1,number,percent}', {1,number,percent},",
226
"'{1,date,full}', {1,date,full},",
227
"'{3,date,full}', {3,date,full},",
228
"'{1,number,#,##}' {1,number,#,##}",
231
UnicodeString testResultPatterns[] = {
232
"Quotes '', '{', a {0} '{'0}",
233
"Quotes '', '{', a {0,number} '{'0}",
234
"'{'1,number,#,##} {1,number,'#'#,##}",
235
"There are {1} files on {2} at {3}.",
236
"On {2}, there are {1} files, with {0,number,currency}.",
237
"'{'1,number,percent}, {1,number,percent},",
238
"'{'1,date,full}, {1,date,full},",
239
"'{'3,date,full}, {3,date,full},",
240
"'{'1,number,#,##} {1,number,#,##}"
243
UnicodeString testResultStrings[] = {
244
"Quotes ', {, a 1 {0}",
245
"Quotes ', {, a 1 {0}",
246
"{1,number,#,##} #34,56",
247
"There are 3,456 files on Disk at 1/12/70 5:46 AM.",
248
"On Disk, there are 3,456 files, with $1.00.",
249
"{1,number,percent}, 345,600%,",
250
"{1,date,full}, Wednesday, December 31, 1969,",
251
"{3,date,full}, Monday, January 12, 1970,",
252
"{1,number,#,##} 34,56"
256
for (int32_t i = 0; i < 9; ++i) {
257
//it_out << "\nPat in: " << testCases[i]);
259
MessageFormat *form = 0;
260
UErrorCode success = U_ZERO_ERROR;
261
UnicodeString buffer;
262
form = new MessageFormat(testCases[i], Locale::getUS(), success);
263
if (U_FAILURE(success)) {
264
errln("MessageFormat creation failed.#1");
265
logln(((UnicodeString)"MessageFormat for ") + testCases[i] + " creation failed.\n");
268
if (form->toPattern(buffer) != testResultPatterns[i]) {
269
errln(UnicodeString("TestMessageFormat::PatternTest failed test #2, i = ") + i);
270
//form->toPattern(buffer);
271
errln(((UnicodeString)" Orig: ") + testCases[i]);
272
errln(((UnicodeString)" Exp: ") + testResultPatterns[i]);
273
errln(((UnicodeString)" Got: ") + buffer);
276
//it_out << "Pat out: " << form->toPattern(buffer));
277
UnicodeString result;
279
FieldPosition fieldpos(0);
280
form->format(testArgs, count, result, fieldpos, success);
281
if (U_FAILURE(success)) {
282
errln("MessageFormat failed test #3");
283
logln("TestMessageFormat::PatternTest failed test #3");
286
if (result != testResultStrings[i]) {
287
errln("TestMessageFormat::PatternTest failed test #4");
288
logln("TestMessageFormat::PatternTest failed #4.");
289
logln(UnicodeString(" Result: ") + result );
290
logln(UnicodeString(" Expected: ") + testResultStrings[i] );
294
//it_out << "Result: " << result);
296
/* TODO: Look at this test and see if this is still a valid test */
297
logln("---------------- test parse ----------------");
299
form->toPattern(buffer);
300
logln("MSG pattern for parse: " + buffer);
302
int32_t parseCount = 0;
303
Formattable* values = form->parse(result, parseCount, success);
304
if (U_FAILURE(success)) {
305
errln("MessageFormat failed test #5");
306
logln(UnicodeString("MessageFormat failed test #5 with error code ")+(int32_t)success);
307
} else if (parseCount != count) {
308
errln("MSG count not %d as expected. Got %d", count, parseCount);
310
UBool failed = FALSE;
311
for (int32_t j = 0; j < parseCount; ++j) {
312
if (values == 0 || testArgs[j] != values[j]) {
313
errln(((UnicodeString)"MSG testargs[") + j + "]: " + toString(testArgs[j]));
314
errln(((UnicodeString)"MSG values[") + j + "] : " + toString(values[j]));
319
errln("MessageFormat failed test #6");
325
void TestMessageFormat::sample()
327
MessageFormat *form = 0;
328
UnicodeString buffer1, buffer2;
329
UErrorCode success = U_ZERO_ERROR;
330
form = new MessageFormat("There are {0} files on {1}", success);
331
if (U_FAILURE(success)) {
332
errln("Err: Message format creation failed");
333
logln("Sample message format creation failed.");
336
UnicodeString abc("abc");
337
UnicodeString def("def");
338
Formattable testArgs1[] = { abc, def };
339
FieldPosition fieldpos(0);
340
logln(form->toPattern(buffer1) + "; " + form->format(testArgs1, 2, buffer2, fieldpos, success));
345
void TestMessageFormat::testStaticFormat(/* char* par */)
347
logln("running TestMessageFormat::testStaticFormat");
349
UErrorCode err = U_ZERO_ERROR;
350
GregorianCalendar cal(err);
351
Formattable arguments[] = {
353
Formattable(UDate(8.71068e+011), Formattable::kIsDate),
354
"a disturbance in the Force"
357
UnicodeString result;
358
result = MessageFormat::format(
359
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
365
if (U_FAILURE(err)) {
366
errln("TestMessageFormat::testStaticFormat #1");
367
logln(UnicodeString("TestMessageFormat::testStaticFormat failed test #1 with error code ")+(int32_t)err);
371
static const UnicodeString expected =
372
"At 12:20:00 PM on Aug 8, 1997, there was a disturbance in the Force on planet 7.";
373
if (result != expected) {
374
errln("TestMessageFormat::testStaticFormat failed on test");
375
logln( UnicodeString(" Result: ") + result );
376
logln( UnicodeString(" Expected: ") + expected );
381
void TestMessageFormat::testSimpleFormat(/* char* par */)
383
logln("running TestMessageFormat::testSimpleFormat");
385
UErrorCode err = U_ZERO_ERROR;
387
Formattable testArgs1[] = {(int32_t)0, "MyDisk"};
388
Formattable testArgs2[] = {(int32_t)1, "MyDisk"};
389
Formattable testArgs3[] = {(int32_t)12, "MyDisk"};
391
MessageFormat* form = new MessageFormat(
392
"The disk \"{1}\" contains {0} file(s).", err);
394
UnicodeString string;
395
FieldPosition ignore(FieldPosition::DONT_CARE);
396
form->format(testArgs1, 2, string, ignore, err);
397
if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 0 file(s).") {
398
errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #1"));
401
ignore.setField(FieldPosition::DONT_CARE);
403
form->format(testArgs2, 2, string, ignore, err);
404
if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 1 file(s).") {
406
errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #2")+string);
409
ignore.setField(FieldPosition::DONT_CARE);
411
form->format(testArgs3, 2, string, ignore, err);
412
if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 12 file(s).") {
413
errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #3")+string);
419
void TestMessageFormat::testMsgFormatChoice(/* char* par */)
421
logln("running TestMessageFormat::testMsgFormatChoice");
423
UErrorCode err = U_ZERO_ERROR;
425
MessageFormat* form = new MessageFormat("The disk \"{1}\" contains {0}.", err);
426
double filelimits[] = {0,1,2};
427
UnicodeString filepart[] = {"no files","one file","{0,number} files"};
428
ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3);
429
form->setFormat(1,*fileform); // NOT zero, see below
430
//is the format adopted?
432
FieldPosition ignore(FieldPosition::DONT_CARE);
433
UnicodeString string;
434
Formattable testArgs1[] = {(int32_t)0, "MyDisk"};
435
form->format(testArgs1, 2, string, ignore, err);
436
if (string != "The disk \"MyDisk\" contains no files.") {
437
errln("TestMessageFormat::testMsgFormatChoice failed on test #1");
440
ignore.setField(FieldPosition::DONT_CARE);
442
Formattable testArgs2[] = {(int32_t)1, "MyDisk"};
443
form->format(testArgs2, 2, string, ignore, err);
444
if (string != "The disk \"MyDisk\" contains one file.") {
445
errln("TestMessageFormat::testMsgFormatChoice failed on test #2");
448
ignore.setField(FieldPosition::DONT_CARE);
450
Formattable testArgs3[] = {(int32_t)1273, "MyDisk"};
451
form->format(testArgs3, 2, string, ignore, err);
452
if (string != "The disk \"MyDisk\" contains 1,273 files.") {
453
errln("TestMessageFormat::testMsgFormatChoice failed on test #3");
461
//---------------------------------
463
//---------------------------------
465
void TestMessageFormat::testCopyConstructor()
467
logln("TestMessageFormat::testCopyConstructor");
468
UErrorCode success = U_ZERO_ERROR;
469
MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
470
MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
471
MessageFormat *y = 0;
472
y = new MessageFormat(*x);
476
logln("First test (operator ==): Passed!");
478
errln("TestMessageFormat::testCopyConstructor failed #1");
479
logln("First test (operator ==): Failed!");
481
if ( ((*x == *y) && (*y == *x)) &&
482
((*x != *z) && (*z != *x)) &&
483
((*y != *z) && (*z != *y)) )
484
logln("Second test (equals): Passed!");
486
errln("TestMessageFormat::testCopyConstructor failed #2");
487
logln("Second test (equals): Failed!");
496
void TestMessageFormat::testAssignment()
498
logln("TestMessageFormat::testAssignment");
499
UErrorCode success = U_ZERO_ERROR;
500
MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
501
MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
502
MessageFormat *y = new MessageFormat("There are {0} files on {1} created", success);
507
logln("First test (operator ==): Passed!");
509
errln( "TestMessageFormat::testAssignment failed #1");
510
logln("First test (operator ==): Failed!");
512
if ( ((*x == *y) && (*y == *x)) &&
513
((*x != *z) && (*z != *x)) &&
514
((*y != *z) && (*z != *y)) )
515
logln("Second test (equals): Passed!");
517
errln("TestMessageFormat::testAssignment failed #2");
518
logln("Second test (equals): Failed!");
526
void TestMessageFormat::testClone()
528
logln("TestMessageFormat::testClone");
529
UErrorCode success = U_ZERO_ERROR;
530
MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
531
MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
532
MessageFormat *y = 0;
533
y = (MessageFormat*)x->clone();
537
logln("First test (operator ==): Passed!");
539
errln("TestMessageFormat::testClone failed #1");
540
logln("First test (operator ==): Failed!");
542
if ( ((*x == *y) && (*y == *x)) &&
543
((*x != *z) && (*z != *x)) &&
544
((*y != *z) && (*z != *y)) )
545
logln("Second test (equals): Passed!");
547
errln("TestMessageFormat::testClone failed #2");
548
logln("Second test (equals): Failed!");
556
void TestMessageFormat::testEquals()
558
logln("TestMessageFormat::testClone");
559
UErrorCode success = U_ZERO_ERROR;
560
MessageFormat x("There are {0} files on {1}", success);
561
MessageFormat y("There are {0} files on {1}", success);
563
errln( "TestMessageFormat::testEquals failed #1");
564
logln("First test (operator ==): Failed!");
569
void TestMessageFormat::testNotEquals()
571
UErrorCode success = U_ZERO_ERROR;
572
MessageFormat x("There are {0} files on {1}", success);
574
y.setLocale(Locale("fr"));
576
errln( "TestMessageFormat::testEquals failed #1");
577
logln("First test (operator !=): Failed!");
580
y.applyPattern("There are {0} files on {1} the disk", success);
582
errln( "TestMessageFormat::testEquals failed #1");
583
logln("First test (operator !=): Failed!");
588
void TestMessageFormat::testSetLocale()
590
UErrorCode err = U_ZERO_ERROR;
591
GregorianCalendar cal(err);
592
Formattable arguments[] = {
594
Formattable(UDate(8.71068e+011), Formattable::kIsDate),
598
UnicodeString result;
600
//UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
601
UnicodeString formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
602
// {sfb} to get $, would need Locale::US, not Locale::ENGLISH
603
// Just use unlocalized currency symbol.
604
//UnicodeString compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
605
UnicodeString compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of ";
606
compareStrEng += (UChar) 0x00a4;
607
compareStrEng += "456.83.";
608
// {sfb} to get DM, would need Locale::GERMANY, not Locale::GERMAN
609
// Just use unlocalized currency symbol.
610
//UnicodeString compareStrGer = "At <time> on 08.08.1997, you made a deposit of 456,83 DM.";
611
UnicodeString compareStrGer = "At <time> on 08.08.1997, you made a deposit of ";
612
compareStrGer += (UChar) 0x00a4;
613
compareStrGer += " 456,83.";
615
MessageFormat msg( formatStr, err);
617
FieldPosition pos(0);
626
if (result != compareStrEng) {
627
errln("*** MSG format err.");
630
msg.setLocale(Locale::getEnglish());
631
UBool getLocale_ok = TRUE;
632
if (msg.getLocale() != Locale::ENGLISH) {
633
errln("*** MSG getLocal err.");
634
getLocale_ok = FALSE;
637
msg.setLocale(Locale::getGerman());
639
if (msg.getLocale() != Locale::GERMAN) {
640
errln("*** MSG getLocal err.");
641
getLocale_ok = FALSE;
644
msg.applyPattern( formatStr, err);
656
if (result == compareStrGer) {
657
logln("MSG setLocale tested.");
659
errln( "*** MSG setLocale err.");
663
logln("MSG getLocale tested.");
667
void TestMessageFormat::testFormat()
669
UErrorCode err = U_ZERO_ERROR;
670
GregorianCalendar cal(err);
672
const Formattable ftarray[] =
674
Formattable( UDate(8.71068e+011), Formattable::kIsDate )
676
const int32_t ft_cnt = sizeof(ftarray) / sizeof(Formattable);
677
Formattable ft_arr( ftarray, ft_cnt );
679
Formattable* fmt = new Formattable(UDate(8.71068e+011), Formattable::kIsDate);
681
UnicodeString result;
683
//UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
684
UnicodeString formatStr = "On {0,date}, it began.";
685
UnicodeString compareStr = "On Aug 8, 1997, it began.";
688
MessageFormat msg( formatStr, err);
700
if (err != U_ILLEGAL_ARGUMENT_ERROR) {
701
errln("*** MSG format without expected error code.");
714
logln("MSG format( Formattable&, ... ) expected:" + compareStr);
715
logln("MSG format( Formattable&, ... ) result:" + result);
716
if (result != compareStr) {
717
errln("*** MSG format( Formattable&, .... ) err.");
719
logln("MSG format( Formattable&, ... ) tested.");
726
void TestMessageFormat::testParse()
728
UErrorCode err = U_ZERO_ERROR;
730
UnicodeString msgFormatString = "{0} =sep= {1}";
731
MessageFormat msg( msgFormatString, err);
732
UnicodeString source = "abc =sep= def";
733
UnicodeString tmp1, tmp2;
735
Formattable* fmt_arr = msg.parse( source, count, err );
736
if (U_FAILURE(err) || (!fmt_arr)) {
737
errln("*** MSG parse (ustring, count, err) error.");
739
logln("MSG parse -- count: %d", count);
741
errln("*** MSG parse (ustring, count, err) count err.");
743
if ((fmt_arr[0].getType() == Formattable::kString)
744
&& (fmt_arr[1].getType() == Formattable::kString)
745
&& (fmt_arr[0].getString(tmp1) == "abc")
746
&& (fmt_arr[1].getString(tmp2) == "def")) {
747
logln("MSG parse (ustring, count, err) tested.");
749
errln("*** MSG parse (ustring, count, err) result err.");
757
fmt_arr = msg.parse( source, pp, count );
758
if ((pp == 0) || (!fmt_arr)) {
759
errln("*** MSG parse (ustring, parsepos., count) error.");
761
logln("MSG parse -- count: %d", count);
763
errln("*** MSG parse (ustring, parsepos., count) count err.");
765
if ((fmt_arr[0].getType() == Formattable::kString)
766
&& (fmt_arr[1].getType() == Formattable::kString)
767
&& (fmt_arr[0].getString(tmp1) == "abc")
768
&& (fmt_arr[1].getString(tmp2) == "def")) {
769
logln("MSG parse (ustring, parsepos., count) tested.");
771
errln("*** MSG parse (ustring, parsepos., count) result err.");
780
msg.parseObject( source, fmta, pp );
782
errln("*** MSG parse (ustring, Formattable, parsepos ) error.");
784
logln("MSG parse -- count: %d", count);
785
fmta.getArray(count);
787
errln("*** MSG parse (ustring, Formattable, parsepos ) count err.");
789
if ((fmta[0].getType() == Formattable::kString)
790
&& (fmta[1].getType() == Formattable::kString)
791
&& (fmta[0].getString(tmp1) == "abc")
792
&& (fmta[1].getString(tmp2) == "def")) {
793
logln("MSG parse (ustring, Formattable, parsepos ) tested.");
795
errln("*** MSG parse (ustring, Formattable, parsepos ) result err.");
802
void TestMessageFormat::testAdopt()
804
UErrorCode err = U_ZERO_ERROR;
806
UnicodeString formatStr("{0,date},{1},{2,number}", "");
807
UnicodeString formatStrChange("{0,number},{1,number},{2,date}", "");
809
MessageFormat msg( formatStr, err);
810
MessageFormat msgCmp( formatStr, err);
811
int32_t count, countCmp;
812
const Format** formats = msg.getFormats(count);
813
const Format** formatsCmp = msgCmp.getFormats(countCmp);
814
const Format** formatsChg = 0;
815
const Format** formatsAct = 0;
819
UnicodeString patCmp;
820
UnicodeString patAct;
821
Format** formatsToAdopt;
823
if (!formats || !formatsCmp || (count <= 0) || (count != countCmp)) {
824
errln("Error getting Formats");
830
for (i = 0; i < count; i++) {
833
if ((a != NULL) && (b != NULL)) {
838
}else if ((a != NULL) || (b != NULL)) {
839
errln("(a != NULL) || (b != NULL)");
844
msg.applyPattern( formatStrChange, err ); //set msg formats to something different
846
formatsChg = msg.getFormats(countChg); // tested function
847
if (!formatsChg || (countChg != count)) {
848
errln("Error getting Formats");
854
for (i = 0; i < count; i++) {
857
if ((a != NULL) && (b != NULL)) {
859
logln("formatsChg != formatsCmp at index %d", i);
865
errln("*** MSG getFormats diff err.");
869
logln("MSG getFormats tested.");
871
msg.setFormats( formatsCmp, countCmp ); //tested function
873
formatsAct = msg.getFormats(countAct);
874
if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
875
errln("Error getting Formats");
880
msgCmp.toPattern( patCmp );
881
logln("MSG patCmp: " + patCmp);
882
msg.toPattern( patAct );
883
logln("MSG patAct: " + patAct);
886
for (i = 0; i < countAct; i++) {
889
if ((a != NULL) && (b != NULL)) {
891
logln("formatsAct != formatsCmp at index %d", i);
895
}else if ((a != NULL) || (b != NULL)) {
896
errln("(a != NULL) || (b != NULL)");
900
logln("MSG setFormats tested.");
905
msg.applyPattern( formatStrChange, err ); //set msg formats to something different
907
formatsToAdopt = new Format* [countCmp];
908
if (!formatsToAdopt) {
909
errln("memory allocation error");
913
for (i = 0; i < countCmp; i++) {
914
if (formatsCmp[i] == NULL) {
915
formatsToAdopt[i] = NULL;
917
formatsToAdopt[i] = formatsCmp[i]->clone();
918
if (!formatsToAdopt[i]) {
919
errln("Can't clone format at index %d", i);
924
msg.adoptFormats( formatsToAdopt, countCmp ); // function to test
925
delete[] formatsToAdopt;
928
msgCmp.toPattern( patCmp );
929
logln("MSG patCmp: " + patCmp);
930
msg.toPattern( patAct );
931
logln("MSG patAct: " + patAct);
934
formatsAct = msg.getFormats(countAct);
935
if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
936
errln("Error getting Formats");
940
for (i = 0; i < countAct; i++) {
943
if ((a != NULL) && (b != NULL)) {
948
}else if ((a != NULL) || (b != NULL)) {
949
errln("(a != NULL) || (b != NULL)");
953
logln("MSG adoptFormats tested.");
957
msg.applyPattern( formatStrChange, err ); //set msg formats to something different
959
formatsToAdopt = new Format* [countCmp];
960
if (!formatsToAdopt) {
961
errln("memory allocation error");
965
for (i = 0; i < countCmp; i++) {
966
if (formatsCmp[i] == NULL) {
967
formatsToAdopt[i] = NULL;
969
formatsToAdopt[i] = formatsCmp[i]->clone();
970
if (!formatsToAdopt[i]) {
971
errln("Can't clone format at index %d", i);
977
for ( i = 0; i < countCmp; i++ ) {
978
msg.adoptFormat( i, formatsToAdopt[i] ); // function to test
980
delete[] formatsToAdopt; // array itself not needed in this case;
983
msgCmp.toPattern( patCmp );
984
logln("MSG patCmp: " + patCmp);
985
msg.toPattern( patAct );
986
logln("MSG patAct: " + patAct);
989
formatsAct = msg.getFormats(countAct);
990
if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
991
errln("Error getting Formats");
995
for (i = 0; i < countAct; i++) {
998
if ((a != NULL) && (b != NULL)) {
1003
}else if ((a != NULL) || (b != NULL)) {
1004
errln("(a != NULL) || (b != NULL)");
1008
logln("MSG adoptFormat tested.");
1011
// This test is a regression test for a fixed bug in the copy constructor.
1012
// It is kept as a global function rather than as a method since the test depends on memory values.
1013
// (At least before the bug was fixed, whether it showed up or not depended on memory contents,
1014
// which is probably why it didn't show up in the regular test for the copy constructor.)
1015
// For this reason, the test isn't changed even though it contains function calls whose results are
1016
// not tested and had no problems. Actually, the test failed by *crashing*.
1017
static void testCopyConstructor2()
1019
UErrorCode status = U_ZERO_ERROR;
1020
UnicodeString formatStr("Hello World on {0,date,full}", "");
1021
UnicodeString resultStr(" ", "");
1022
UnicodeString result;
1023
FieldPosition fp(0);
1024
UDate d = Calendar::getNow();
1025
const Formattable fargs( d, Formattable::kIsDate );
1027
MessageFormat* fmt1 = new MessageFormat( formatStr, status );
1028
MessageFormat* fmt2 = new MessageFormat( *fmt1 );
1029
MessageFormat* fmt3;
1030
MessageFormat* fmt4;
1032
if (fmt1 == NULL) it_err("testCopyConstructor2: (fmt1 != NULL)");
1034
result = fmt1->format( &fargs, 1, resultStr, fp, status );
1036
if (fmt2 == NULL) it_err("testCopyConstructor2: (fmt2 != NULL)");
1038
fmt3 = (MessageFormat*) fmt1->clone();
1039
fmt4 = (MessageFormat*) fmt2->clone();
1041
if (fmt3 == NULL) it_err("testCopyConstructor2: (fmt3 != NULL)");
1042
if (fmt4 == NULL) it_err("testCopyConstructor2: (fmt4 != NULL)");
1044
result = fmt1->format( &fargs, 1, resultStr, fp, status );
1045
result = fmt2->format( &fargs, 1, resultStr, fp, status );
1046
result = fmt3->format( &fargs, 1, resultStr, fp, status );
1047
result = fmt4->format( &fargs, 1, resultStr, fp, status );
1055
void TestMessageFormat::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
1057
if (exec) logln("TestSuite MessageFormat");
1060
case 0: name = "testBug1"; if (exec) testBug1(); break;
1061
case 1: name = "testBug2"; if (exec) testBug2(); break;
1062
case 2: name = "sample"; if (exec) sample(); break;
1063
case 3: name = "PatternTest"; if (exec) PatternTest(); break;
1064
case 4: name = "testStaticFormat"; if (exec) testStaticFormat(/* par */); break;
1065
case 5: name = "testSimpleFormat"; if (exec) testSimpleFormat(/* par */); break;
1066
case 6: name = "testMsgFormatChoice"; if (exec) testMsgFormatChoice(/* par */); break;
1068
case 7: name = "testCopyConstructor"; if (exec) testCopyConstructor(); break;
1069
case 8: name = "testAssignment"; if (exec) testAssignment(); break;
1070
case 9: name = "testClone"; if (exec) testClone(); break;
1071
case 10: name = "testEquals"; if (exec) testEquals(); break;
1072
case 11: name = "testNotEquals"; if (exec) testNotEquals(); break;
1073
case 12: name = "testSetLocale"; if (exec) testSetLocale(); break;
1074
case 13: name = "testFormat"; if (exec) testFormat(); break;
1075
case 14: name = "testParse"; if (exec) testParse(); break;
1076
case 15: name = "testAdopt"; if (exec) testAdopt(); break;
1077
case 16: name = "testCopyConstructor2"; if (exec) testCopyConstructor2(); break;
1080
default: name = ""; break; //needed to end loop