~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/test/intltest/tmsgfmt.cpp

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/********************************************************************
2
 
 * COPYRIGHT: 
3
 
 * Copyright (c) 1997-2001, International Business Machines Corporation and
4
 
 * others. All Rights Reserved.
5
 
 ********************************************************************/
6
 
/*
7
 
* File TMSGFMT.CPP
8
 
*
9
 
* Modification History:
10
 
*
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
 
********************************************************************************
16
 
*/
17
 
#include "tmsgfmt.h"
18
 
 
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"
26
 
 
27
 
 
28
 
 
29
 
void TestMessageFormat::testBug3()
30
 
{
31
 
    double myNumber = -123456;
32
 
    DecimalFormat *form = 0;
33
 
    Locale locale[] = {
34
 
        Locale("ar", "", ""),
35
 
        Locale("be", "", ""),
36
 
        Locale("bg", "", ""),
37
 
        Locale("ca", "", ""),
38
 
        Locale("cs", "", ""),
39
 
        Locale("da", "", ""),
40
 
        Locale("de", "", ""),
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", ""),
48
 
        Locale("es", "", ""),
49
 
        Locale("et", "", ""),
50
 
        Locale("fi", "", ""),
51
 
        Locale("fr", "", ""),
52
 
        Locale("fr", "BE", ""),
53
 
        Locale("fr", "CA", ""),     // 20
54
 
        Locale("fr", "CH", ""),
55
 
        Locale("he", "", ""),
56
 
        Locale("hr", "", ""),
57
 
        Locale("hu", "", ""),
58
 
        Locale("is", "", ""),
59
 
        Locale("it", "", ""),
60
 
        Locale("it", "CH", ""),
61
 
        Locale("ja", "", ""),
62
 
        Locale("ko", "", ""),
63
 
        Locale("lt", "", ""),       // 30
64
 
        Locale("lv", "", ""),
65
 
        Locale("mk", "", ""),
66
 
        Locale("nl", "", ""),
67
 
        Locale("nl", "BE", ""),
68
 
        Locale("no", "", ""),
69
 
        Locale("pl", "", ""),
70
 
        Locale("pt", "", ""),
71
 
        Locale("ro", "", ""),
72
 
        Locale("ru", "", ""),
73
 
        Locale("sh", "", ""),       // 40
74
 
        Locale("sk", "", ""),
75
 
        Locale("sl", "", ""),
76
 
        Locale("sq", "", ""),
77
 
        Locale("sr", "", ""),
78
 
        Locale("sv", "", ""),
79
 
        Locale("tr", "", ""),
80
 
        Locale("uk", "", ""),
81
 
        Locale("zh", "", ""),
82
 
        Locale("zh", "TW", "")      // 49
83
 
    };
84
 
    int32_t i;
85
 
    for (i= 0; i < 49; i++) {
86
 
        UnicodeString buffer;
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.");
94
 
            continue;
95
 
        }
96
 
        Formattable result;
97
 
        FieldPosition pos(0);
98
 
        buffer.remove();
99
 
        form->format(myNumber, buffer, pos);
100
 
        success = U_ZERO_ERROR;
101
 
        ParsePosition parsePos;
102
 
        form->parse(buffer, result, parsePos);
103
 
#ifdef _DEBUG
104
 
        it_out << " -> " /*<< dec*/ /*<< result*/ << "[supposed output for result]" <<endl;
105
 
#endif
106
 
        if (U_FAILURE(success)) {
107
 
            errln("Err: Number Format parse");
108
 
            logln("Number format parse failed.");
109
 
        }
110
 
        delete form;
111
 
    }
112
 
}
113
 
 
114
 
void TestMessageFormat::testBug1()
115
 
{
116
 
    const double limit[] = {0.0, 1.0, 2.0};
117
 
    const UnicodeString formats[] = {"0.0<=Arg<1.0",
118
 
                               "1.0<=Arg<2.0",
119
 
                               "2.0<-Arg"};
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");
126
 
    }
127
 
    logln(toAppendTo);
128
 
    delete cf;
129
 
}
130
 
 
131
 
void TestMessageFormat::testBug2()
132
 
{
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.");
142
 
        return;
143
 
    }
144
 
    logln("The output pattern is : " + fmt->toPattern(result));
145
 
    if (pattern != result) {
146
 
        errln("MessageFormat::toPattern() failed.");
147
 
        logln("MessageFormat::toPattern() failed.");
148
 
    }
149
 
    delete fmt;
150
 
}
151
 
 
152
 
#if 0
153
 
#if defined(_DEBUG) && U_IOSTREAM_SOURCE!=0
154
 
//----------------------------------------------------
155
 
// console I/O
156
 
//----------------------------------------------------
157
 
 
158
 
#if U_IOSTREAM_SOURCE >= 199711
159
 
#   include <iostream>
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);
164
 
#endif
165
 
 
166
 
#include "unicode/datefmt.h"
167
 
#include <stdlib.h>
168
 
#include <stdio.h>
169
 
#include <string.h>
170
 
 
171
 
IntlTest&
172
 
operator<<( IntlTest&           stream,
173
 
            const Formattable&  obj)
174
 
{
175
 
    static DateFormat *defDateFormat = 0;
176
 
 
177
 
    UnicodeString buffer;
178
 
    switch(obj.getType()) {
179
 
        case Formattable::kDate : 
180
 
            if (defDateFormat == 0) {
181
 
                defDateFormat = DateFormat::createInstance();
182
 
            }
183
 
            defDateFormat->format(obj.getDate(), buffer);
184
 
            stream << buffer;
185
 
            break;
186
 
        case Formattable::kDouble :
187
 
            char convert[20];
188
 
            sprintf( convert, "%lf", obj.getDouble() );
189
 
            stream << convert << "D";
190
 
            break;
191
 
        case Formattable::kLong :
192
 
            stream << obj.getLong() << "L";
193
 
            break;
194
 
        case Formattable::kString:
195
 
            stream << "\"" << obj.getString(buffer) << "\"";
196
 
            break;
197
 
        case Formattable::kArray:
198
 
            int32_t i, count;
199
 
            const Formattable* array;
200
 
            array = obj.getArray(count);
201
 
            stream << "[";
202
 
            for (i=0; i<count; ++i) stream << array[i] << ( (i==(count-1)) ? "" : ", " );
203
 
            stream << "]";
204
 
            break;
205
 
        default:
206
 
            stream << "INVALID_Formattable";
207
 
    }
208
 
    return stream;
209
 
}
210
 
#endif /* defined(_DEBUG) && U_IOSTREAM_SOURCE!=0 */
211
 
#endif
212
 
 
213
 
void TestMessageFormat::PatternTest() 
214
 
{
215
 
    Formattable testArgs[] = {
216
 
        Formattable(double(1)), Formattable(double(3456)),
217
 
            Formattable("Disk"), Formattable(UDate((int32_t)1000000000L), Formattable::kIsDate)
218
 
    };
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,#,##}",
229
 
    };
230
 
 
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,#,##}"
241
 
    };
242
 
 
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"
253
 
    };
254
 
 
255
 
 
256
 
    for (int32_t i = 0; i < 9; ++i) {
257
 
        //it_out << "\nPat in:  " << testCases[i]);
258
 
 
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");
266
 
            continue;
267
 
        }
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);
274
 
        }
275
 
 
276
 
        //it_out << "Pat out: " << form->toPattern(buffer));
277
 
        UnicodeString result;
278
 
        int32_t count = 4;
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");
284
 
            continue;
285
 
        }
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] );
291
 
        }
292
 
        
293
 
 
294
 
        //it_out << "Result:  " << result);
295
 
#if 0
296
 
        /* TODO: Look at this test and see if this is still a valid test */
297
 
        logln("---------------- test parse ----------------");
298
 
 
299
 
        form->toPattern(buffer);
300
 
        logln("MSG pattern for parse: " + buffer);
301
 
 
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);
309
 
        }
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]));
315
 
                failed = TRUE;
316
 
             }
317
 
        }
318
 
        if (failed)
319
 
            errln("MessageFormat failed test #6");
320
 
#endif
321
 
        delete form;
322
 
    }
323
 
}
324
 
 
325
 
void TestMessageFormat::sample() 
326
 
{
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.");
334
 
        return;
335
 
    }
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));
341
 
    delete form;
342
 
}
343
 
 
344
 
 
345
 
void TestMessageFormat::testStaticFormat(/* char* par */)
346
 
{
347
 
    logln("running TestMessageFormat::testStaticFormat");
348
 
 
349
 
    UErrorCode err = U_ZERO_ERROR;
350
 
    GregorianCalendar cal(err);   
351
 
    Formattable arguments[] = {
352
 
        (int32_t)7,
353
 
        Formattable(UDate(8.71068e+011), Formattable::kIsDate),
354
 
        "a disturbance in the Force"
355
 
        };
356
 
   
357
 
        UnicodeString result;
358
 
        result = MessageFormat::format(
359
 
            "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
360
 
            arguments,
361
 
            3,
362
 
            result,
363
 
            err);
364
 
 
365
 
        if (U_FAILURE(err)) {
366
 
            errln("TestMessageFormat::testStaticFormat #1");
367
 
            logln(UnicodeString("TestMessageFormat::testStaticFormat failed test #1 with error code ")+(int32_t)err);
368
 
            return;
369
 
        }
370
 
 
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 );
377
 
        }
378
 
}
379
 
 
380
 
 
381
 
void TestMessageFormat::testSimpleFormat(/* char* par */)
382
 
{
383
 
    logln("running TestMessageFormat::testSimpleFormat");
384
 
 
385
 
    UErrorCode err = U_ZERO_ERROR;
386
 
 
387
 
    Formattable testArgs1[] = {(int32_t)0, "MyDisk"};
388
 
    Formattable testArgs2[] = {(int32_t)1, "MyDisk"};
389
 
    Formattable testArgs3[] = {(int32_t)12, "MyDisk"};
390
 
   
391
 
    MessageFormat* form = new MessageFormat(
392
 
        "The disk \"{1}\" contains {0} file(s).", err);
393
 
    
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"));
399
 
    }
400
 
 
401
 
    ignore.setField(FieldPosition::DONT_CARE);
402
 
    string.remove();
403
 
    form->format(testArgs2, 2, string, ignore, err);
404
 
    if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 1 file(s).") {
405
 
        logln(string);
406
 
        errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #2")+string);
407
 
    }
408
 
 
409
 
    ignore.setField(FieldPosition::DONT_CARE);
410
 
    string.remove();
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);
414
 
    }
415
 
 
416
 
    delete form;
417
 
 }
418
 
 
419
 
void TestMessageFormat::testMsgFormatChoice(/* char* par */)
420
 
{
421
 
    logln("running TestMessageFormat::testMsgFormatChoice");
422
 
 
423
 
    UErrorCode err = U_ZERO_ERROR;
424
 
 
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?
431
 
 
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");
438
 
    }
439
 
 
440
 
    ignore.setField(FieldPosition::DONT_CARE);
441
 
    string.remove();
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");
446
 
    }
447
 
 
448
 
    ignore.setField(FieldPosition::DONT_CARE);
449
 
    string.remove();
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");
454
 
    }
455
 
 
456
 
    delete form;
457
 
    delete fileform;
458
 
}
459
 
 
460
 
 
461
 
//---------------------------------
462
 
//  API Tests
463
 
//---------------------------------
464
 
 
465
 
void TestMessageFormat::testCopyConstructor() 
466
 
{
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);
473
 
    if ( (*x == *y) && 
474
 
         (*x != *z) && 
475
 
         (*y != *z) )
476
 
         logln("First test (operator ==): Passed!");
477
 
    else {
478
 
        errln("TestMessageFormat::testCopyConstructor failed #1");
479
 
        logln("First test (operator ==): Failed!");
480
 
    }
481
 
    if ( ((*x == *y) && (*y == *x)) &&
482
 
         ((*x != *z) && (*z != *x)) &&
483
 
         ((*y != *z) && (*z != *y)) )
484
 
        logln("Second test (equals): Passed!");
485
 
    else {
486
 
        errln("TestMessageFormat::testCopyConstructor failed #2");
487
 
        logln("Second test (equals): Failed!");
488
 
    }
489
 
 
490
 
    delete x;
491
 
    delete y;
492
 
    delete z;
493
 
}
494
 
 
495
 
 
496
 
void TestMessageFormat::testAssignment() 
497
 
{
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);
503
 
    *y = *x;
504
 
    if ( (*x == *y) && 
505
 
         (*x != *z) && 
506
 
         (*y != *z) )
507
 
        logln("First test (operator ==): Passed!");
508
 
    else {
509
 
        errln( "TestMessageFormat::testAssignment failed #1");
510
 
        logln("First test (operator ==): Failed!");
511
 
    }
512
 
    if ( ((*x == *y) && (*y == *x)) &&
513
 
         ((*x != *z) && (*z != *x)) &&
514
 
         ((*y != *z) && (*z != *y)) )
515
 
        logln("Second test (equals): Passed!");
516
 
    else {
517
 
        errln("TestMessageFormat::testAssignment failed #2");
518
 
        logln("Second test (equals): Failed!");
519
 
    }
520
 
 
521
 
    delete x;
522
 
    delete y;
523
 
    delete z;
524
 
}
525
 
 
526
 
void TestMessageFormat::testClone() 
527
 
{
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();
534
 
    if ( (*x == *y) && 
535
 
         (*x != *z) && 
536
 
         (*y != *z) )
537
 
        logln("First test (operator ==): Passed!");
538
 
    else {
539
 
        errln("TestMessageFormat::testClone failed #1");
540
 
        logln("First test (operator ==): Failed!");
541
 
    }
542
 
    if ( ((*x == *y) && (*y == *x)) &&
543
 
         ((*x != *z) && (*z != *x)) &&
544
 
         ((*y != *z) && (*z != *y)) )
545
 
        logln("Second test (equals): Passed!");
546
 
    else {
547
 
        errln("TestMessageFormat::testClone failed #2");
548
 
        logln("Second test (equals): Failed!");
549
 
    }
550
 
 
551
 
    delete x;
552
 
    delete y;
553
 
    delete z;
554
 
}
555
 
 
556
 
void TestMessageFormat::testEquals() 
557
 
{
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);
562
 
    if (!(x == y)) {
563
 
        errln( "TestMessageFormat::testEquals failed #1");
564
 
        logln("First test (operator ==): Failed!");
565
 
    }
566
 
 
567
 
}
568
 
 
569
 
void TestMessageFormat::testNotEquals() 
570
 
{
571
 
    UErrorCode success = U_ZERO_ERROR;
572
 
    MessageFormat x("There are {0} files on {1}", success);
573
 
    MessageFormat y(x);
574
 
    y.setLocale(Locale("fr"));
575
 
    if (!(x != y)) {
576
 
        errln( "TestMessageFormat::testEquals failed #1");
577
 
        logln("First test (operator !=): Failed!");
578
 
    }
579
 
    y = x;
580
 
    y.applyPattern("There are {0} files on {1} the disk", success);
581
 
    if (!(x != y)) {
582
 
        errln( "TestMessageFormat::testEquals failed #1");
583
 
        logln("First test (operator !=): Failed!");
584
 
    }
585
 
}
586
 
 
587
 
 
588
 
void TestMessageFormat::testSetLocale()
589
 
{
590
 
    UErrorCode err = U_ZERO_ERROR;
591
 
    GregorianCalendar cal(err);   
592
 
    Formattable arguments[] = {
593
 
        456.83,
594
 
        Formattable(UDate(8.71068e+011), Formattable::kIsDate),
595
 
        "deposit"
596
 
        };
597
 
   
598
 
    UnicodeString result;
599
 
 
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.";
614
 
 
615
 
    MessageFormat msg( formatStr, err);
616
 
    result = "";
617
 
    FieldPosition pos(0);
618
 
    result = msg.format(
619
 
        arguments,
620
 
        3,
621
 
        result,
622
 
        pos,
623
 
        err);
624
 
 
625
 
    logln(result);
626
 
    if (result != compareStrEng) {
627
 
        errln("***  MSG format err.");
628
 
    }
629
 
 
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;
635
 
    }
636
 
 
637
 
    msg.setLocale(Locale::getGerman());
638
 
 
639
 
    if (msg.getLocale() != Locale::GERMAN) {
640
 
        errln("*** MSG getLocal err.");
641
 
        getLocale_ok = FALSE;
642
 
    }
643
 
 
644
 
    msg.applyPattern( formatStr, err);
645
 
 
646
 
    pos.setField(0);
647
 
    result = "";
648
 
    result = msg.format(
649
 
        arguments,
650
 
        3,
651
 
        result,
652
 
        pos,
653
 
        err);
654
 
 
655
 
    logln(result);
656
 
    if (result == compareStrGer) {
657
 
        logln("MSG setLocale tested.");
658
 
    }else{
659
 
        errln( "*** MSG setLocale err.");
660
 
    }
661
 
 
662
 
    if (getLocale_ok) { 
663
 
        logln("MSG getLocale tested.");
664
 
    }
665
 
}
666
 
 
667
 
void TestMessageFormat::testFormat()
668
 
{
669
 
    UErrorCode err = U_ZERO_ERROR;
670
 
    GregorianCalendar cal(err);   
671
 
 
672
 
    const Formattable ftarray[] = 
673
 
    {
674
 
        Formattable( UDate(8.71068e+011), Formattable::kIsDate )
675
 
    };
676
 
    const int32_t ft_cnt = sizeof(ftarray) / sizeof(Formattable);
677
 
    Formattable ft_arr( ftarray, ft_cnt );
678
 
 
679
 
    Formattable* fmt = new Formattable(UDate(8.71068e+011), Formattable::kIsDate);
680
 
   
681
 
    UnicodeString result;
682
 
 
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.";
686
 
 
687
 
    err = U_ZERO_ERROR;
688
 
    MessageFormat msg( formatStr, err);
689
 
    FieldPosition fp(0);
690
 
 
691
 
    result = "";
692
 
    fp = 0;
693
 
    result = msg.format(
694
 
        *fmt,
695
 
        result,
696
 
        //FieldPosition(0),
697
 
        fp,
698
 
        err);
699
 
 
700
 
    if (err != U_ILLEGAL_ARGUMENT_ERROR) {
701
 
        errln("*** MSG format without expected error code.");
702
 
    }
703
 
    err = U_ZERO_ERROR;
704
 
 
705
 
    result = "";
706
 
    fp = 0;
707
 
    result = msg.format(
708
 
        ft_arr,
709
 
        result,
710
 
        //FieldPosition(0),
711
 
        fp,
712
 
        err);
713
 
 
714
 
    logln("MSG format( Formattable&, ... ) expected:" + compareStr);
715
 
    logln("MSG format( Formattable&, ... )   result:" + result);
716
 
    if (result != compareStr) {
717
 
        errln("***  MSG format( Formattable&, .... ) err.");
718
 
    }else{
719
 
        logln("MSG format( Formattable&, ... ) tested.");
720
 
    }
721
 
 
722
 
    delete fmt;
723
 
 
724
 
}
725
 
 
726
 
void TestMessageFormat::testParse()
727
 
{
728
 
    UErrorCode err = U_ZERO_ERROR;
729
 
    int32_t count;
730
 
    UnicodeString msgFormatString = "{0} =sep= {1}";
731
 
    MessageFormat msg( msgFormatString, err);
732
 
    UnicodeString source = "abc =sep= def";
733
 
    UnicodeString tmp1, tmp2;
734
 
 
735
 
    Formattable* fmt_arr = msg.parse( source, count, err );
736
 
    if (U_FAILURE(err) || (!fmt_arr)) {
737
 
        errln("*** MSG parse (ustring, count, err) error.");
738
 
    }else{
739
 
        logln("MSG parse -- count: %d", count);
740
 
        if (count != 2) {
741
 
            errln("*** MSG parse (ustring, count, err) count err.");
742
 
        }else{
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.");
748
 
            }else{
749
 
                errln("*** MSG parse (ustring, count, err) result err.");
750
 
            }
751
 
        }
752
 
    }
753
 
    delete[] fmt_arr;
754
 
 
755
 
    ParsePosition pp(0);
756
 
 
757
 
    fmt_arr = msg.parse( source, pp, count );
758
 
    if ((pp == 0) || (!fmt_arr)) {
759
 
        errln("*** MSG parse (ustring, parsepos., count) error.");
760
 
    }else{
761
 
        logln("MSG parse -- count: %d", count);
762
 
        if (count != 2) {
763
 
            errln("*** MSG parse (ustring, parsepos., count) count err.");
764
 
        }else{
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.");
770
 
            }else{
771
 
                errln("*** MSG parse (ustring, parsepos., count) result err.");
772
 
            }
773
 
        }
774
 
    }
775
 
    delete[] fmt_arr;
776
 
 
777
 
    pp = 0;
778
 
    Formattable fmta;
779
 
 
780
 
    msg.parseObject( source, fmta, pp );
781
 
    if (pp == 0) {
782
 
        errln("*** MSG parse (ustring, Formattable, parsepos ) error.");
783
 
    }else{
784
 
        logln("MSG parse -- count: %d", count);
785
 
        fmta.getArray(count);
786
 
        if (count != 2) {
787
 
            errln("*** MSG parse (ustring, Formattable, parsepos ) count err.");
788
 
        }else{
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.");
794
 
            }else{
795
 
                errln("*** MSG parse (ustring, Formattable, parsepos ) result err.");
796
 
            }
797
 
        }
798
 
    }
799
 
}
800
 
 
801
 
 
802
 
void TestMessageFormat::testAdopt()
803
 
{
804
 
    UErrorCode err = U_ZERO_ERROR;
805
 
 
806
 
    UnicodeString formatStr("{0,date},{1},{2,number}", "");
807
 
    UnicodeString formatStrChange("{0,number},{1,number},{2,date}", "");
808
 
    err = U_ZERO_ERROR;
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;
816
 
    int32_t countAct;
817
 
    const Format* a;
818
 
    const Format* b;
819
 
    UnicodeString patCmp;
820
 
    UnicodeString patAct;
821
 
    Format** formatsToAdopt;
822
 
 
823
 
    if (!formats || !formatsCmp || (count <= 0) || (count != countCmp)) {
824
 
        errln("Error getting Formats");
825
 
        return;
826
 
    }
827
 
 
828
 
    int32_t i;
829
 
 
830
 
    for (i = 0; i < count; i++) {
831
 
        a = formats[i];
832
 
        b = formatsCmp[i];
833
 
        if ((a != NULL) && (b != NULL)) {
834
 
            if (*a != *b) {
835
 
                errln("a != b");
836
 
                return;
837
 
            }
838
 
        }else if ((a != NULL) || (b != NULL)) {
839
 
            errln("(a != NULL) || (b != NULL)");
840
 
            return;
841
 
        }
842
 
    }
843
 
 
844
 
    msg.applyPattern( formatStrChange, err ); //set msg formats to something different
845
 
    int32_t countChg;
846
 
    formatsChg = msg.getFormats(countChg); // tested function
847
 
    if (!formatsChg || (countChg != count)) {
848
 
        errln("Error getting Formats");
849
 
        return;
850
 
    }
851
 
 
852
 
    UBool diff;
853
 
    diff = TRUE;
854
 
    for (i = 0; i < count; i++) {
855
 
        a = formatsChg[i];
856
 
        b = formatsCmp[i];
857
 
        if ((a != NULL) && (b != NULL)) {
858
 
            if (*a == *b) {
859
 
                logln("formatsChg != formatsCmp at index %d", i);
860
 
                diff = FALSE;
861
 
            }
862
 
        }
863
 
    }
864
 
    if (!diff) {
865
 
        errln("*** MSG getFormats diff err.");
866
 
        return;
867
 
    }
868
 
 
869
 
    logln("MSG getFormats tested.");
870
 
 
871
 
    msg.setFormats( formatsCmp, countCmp ); //tested function
872
 
 
873
 
    formatsAct = msg.getFormats(countAct);
874
 
    if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
875
 
        errln("Error getting Formats");
876
 
        return;
877
 
    }
878
 
 
879
 
#if 1
880
 
    msgCmp.toPattern( patCmp );
881
 
    logln("MSG patCmp: " + patCmp);
882
 
    msg.toPattern( patAct );
883
 
    logln("MSG patAct: " + patAct);
884
 
#endif
885
 
 
886
 
    for (i = 0; i < countAct; i++) {
887
 
        a = formatsAct[i];
888
 
        b = formatsCmp[i];
889
 
        if ((a != NULL) && (b != NULL)) {
890
 
            if (*a != *b) {
891
 
                logln("formatsAct != formatsCmp at index %d", i);
892
 
                errln("a != b");
893
 
                return;
894
 
            }
895
 
        }else if ((a != NULL) || (b != NULL)) {
896
 
            errln("(a != NULL) || (b != NULL)");
897
 
            return;
898
 
        }
899
 
    }
900
 
    logln("MSG setFormats tested.");
901
 
 
902
 
 
903
 
    //----
904
 
 
905
 
    msg.applyPattern( formatStrChange, err ); //set msg formats to something different
906
 
 
907
 
    formatsToAdopt = new Format* [countCmp];
908
 
    if (!formatsToAdopt) {
909
 
        errln("memory allocation error");
910
 
        return;
911
 
    }
912
 
 
913
 
    for (i = 0; i < countCmp; i++) {
914
 
        if (formatsCmp[i] == NULL) {
915
 
            formatsToAdopt[i] = NULL;
916
 
        }else{
917
 
            formatsToAdopt[i] = formatsCmp[i]->clone();
918
 
            if (!formatsToAdopt[i]) {
919
 
                errln("Can't clone format at index %d", i);
920
 
                return;
921
 
            }
922
 
        }
923
 
    }
924
 
    msg.adoptFormats( formatsToAdopt, countCmp ); // function to test
925
 
    delete[] formatsToAdopt;
926
 
 
927
 
#if 1
928
 
    msgCmp.toPattern( patCmp );
929
 
    logln("MSG patCmp: " + patCmp);
930
 
    msg.toPattern( patAct );
931
 
    logln("MSG patAct: " + patAct);
932
 
#endif
933
 
 
934
 
    formatsAct = msg.getFormats(countAct);
935
 
    if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
936
 
        errln("Error getting Formats");
937
 
        return;
938
 
    }
939
 
 
940
 
    for (i = 0; i < countAct; i++) {
941
 
        a = formatsAct[i];
942
 
        b = formatsCmp[i];
943
 
        if ((a != NULL) && (b != NULL)) {
944
 
            if (*a != *b) {
945
 
                errln("a != b");
946
 
                return;
947
 
            }
948
 
        }else if ((a != NULL) || (b != NULL)) {
949
 
            errln("(a != NULL) || (b != NULL)");
950
 
            return;
951
 
        }
952
 
    }
953
 
    logln("MSG adoptFormats tested.");
954
 
 
955
 
    //---- adoptFormat
956
 
 
957
 
    msg.applyPattern( formatStrChange, err ); //set msg formats to something different
958
 
 
959
 
    formatsToAdopt = new Format* [countCmp];
960
 
    if (!formatsToAdopt) {
961
 
        errln("memory allocation error");
962
 
        return;
963
 
    }
964
 
 
965
 
    for (i = 0; i < countCmp; i++) {
966
 
        if (formatsCmp[i] == NULL) {
967
 
            formatsToAdopt[i] = NULL;
968
 
        }else{
969
 
            formatsToAdopt[i] = formatsCmp[i]->clone();
970
 
            if (!formatsToAdopt[i]) {
971
 
                errln("Can't clone format at index %d", i);
972
 
                return;
973
 
            }
974
 
        }
975
 
    }
976
 
 
977
 
    for ( i = 0; i < countCmp; i++ ) {
978
 
        msg.adoptFormat( i, formatsToAdopt[i] ); // function to test
979
 
    }
980
 
    delete[] formatsToAdopt; // array itself not needed in this case;
981
 
 
982
 
#if 1
983
 
    msgCmp.toPattern( patCmp );
984
 
    logln("MSG patCmp: " + patCmp);
985
 
    msg.toPattern( patAct );
986
 
    logln("MSG patAct: " + patAct);
987
 
#endif
988
 
 
989
 
    formatsAct = msg.getFormats(countAct);
990
 
    if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
991
 
        errln("Error getting Formats");
992
 
        return;
993
 
    }
994
 
 
995
 
    for (i = 0; i < countAct; i++) {
996
 
        a = formatsAct[i];
997
 
        b = formatsCmp[i];
998
 
        if ((a != NULL) && (b != NULL)) {
999
 
            if (*a != *b) {
1000
 
                errln("a != b");
1001
 
                return;
1002
 
            }
1003
 
        }else if ((a != NULL) || (b != NULL)) {
1004
 
            errln("(a != NULL) || (b != NULL)");
1005
 
            return;
1006
 
        }
1007
 
    }
1008
 
    logln("MSG adoptFormat tested.");
1009
 
}
1010
 
 
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()
1018
 
{
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 );
1026
 
 
1027
 
    MessageFormat* fmt1 = new MessageFormat( formatStr, status );
1028
 
    MessageFormat* fmt2 = new MessageFormat( *fmt1 );
1029
 
    MessageFormat* fmt3;
1030
 
    MessageFormat* fmt4;
1031
 
 
1032
 
    if (fmt1 == NULL) it_err("testCopyConstructor2: (fmt1 != NULL)");
1033
 
 
1034
 
    result = fmt1->format( &fargs, 1, resultStr, fp, status );
1035
 
 
1036
 
    if (fmt2 == NULL) it_err("testCopyConstructor2: (fmt2 != NULL)");
1037
 
 
1038
 
    fmt3 = (MessageFormat*) fmt1->clone();
1039
 
    fmt4 = (MessageFormat*) fmt2->clone();
1040
 
 
1041
 
    if (fmt3 == NULL) it_err("testCopyConstructor2: (fmt3 != NULL)");
1042
 
    if (fmt4 == NULL) it_err("testCopyConstructor2: (fmt4 != NULL)");
1043
 
 
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 );
1048
 
    delete fmt1;
1049
 
    delete fmt2;
1050
 
    delete fmt3;
1051
 
    delete fmt4;
1052
 
}
1053
 
 
1054
 
 
1055
 
void TestMessageFormat::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
1056
 
{
1057
 
    if (exec) logln("TestSuite MessageFormat");
1058
 
 
1059
 
    switch (index) {
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;
1067
 
 
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;
1078
 
 
1079
 
 
1080
 
        default: name = ""; break; //needed to end loop
1081
 
    }
1082
 
}