~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

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
#include "msfmrgts.h"
 
8
 
 
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"
 
16
 
 
17
// *****************************************************************************
 
18
// class MessageFormatRegressionTest
 
19
// *****************************************************************************
 
20
 
 
21
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
 
22
 
 
23
void 
 
24
MessageFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
 
25
{
 
26
    // if (exec) logln((UnicodeString)"TestSuite MessageFormatRegressionTest");
 
27
    switch (index) {
 
28
        CASE(0,Test4074764)
 
29
        CASE(1,Test4058973)
 
30
        CASE(2,Test4031438)
 
31
        CASE(3,Test4052223)
 
32
        CASE(4,Test4104976)
 
33
        CASE(5,Test4106659)
 
34
        CASE(6,Test4106660)
 
35
        CASE(7,Test4111739)
 
36
        CASE(8,Test4114743)
 
37
        CASE(9,Test4116444)
 
38
        CASE(10,Test4114739)
 
39
        CASE(11,Test4113018)
 
40
        CASE(12,Test4106661)
 
41
        CASE(13,Test4094906)
 
42
        CASE(14,Test4118592)
 
43
        CASE(15,Test4118594)
 
44
        CASE(16,Test4105380)
 
45
        CASE(17,Test4120552)
 
46
        CASE(18,Test4142938)
 
47
        CASE(19,TestChoicePatternQuote)
 
48
        CASE(20,Test4112104)
 
49
 
 
50
        default: name = ""; break;
 
51
    }
 
52
}
 
53
 
 
54
UBool 
 
55
MessageFormatRegressionTest::failure(UErrorCode status, const char* msg)
 
56
{
 
57
    if(U_FAILURE(status)) {
 
58
        errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
 
59
        return TRUE;
 
60
    }
 
61
 
 
62
    return FALSE;
 
63
}
 
64
 
 
65
/* @bug 4074764
 
66
 * Null exception when formatting pattern with MessageFormat
 
67
 * with no parameters.
 
68
 */
 
69
void MessageFormatRegressionTest::Test4074764() {
 
70
    UnicodeString pattern [] = {
 
71
        "Message without param",
 
72
        "Message with param:{0}",
 
73
        "Longer Message with param {0}"
 
74
    };
 
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
 
78
    //in the other case.
 
79
 
 
80
    UErrorCode status = U_ZERO_ERROR;
 
81
    MessageFormat *messageFormatter = new MessageFormat("", status);
 
82
 
 
83
    failure(status, "couldn't create MessageFormat");
 
84
 
 
85
    //try {
 
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)
 
93
        };
 
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);
 
100
 
 
101
        //Apply pattern without param and print the result
 
102
        messageFormatter->applyPattern(pattern[0], status);
 
103
        failure(status, "messageFormatter->applyPattern");
 
104
        
 
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
 
111
        
 
112
        //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
 
113
        tempBuffer.remove();
 
114
        tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status);
 
115
 
 
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);
 
119
 
 
120
        tempBuffer.remove();
 
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
 
132
        //contain any param.
 
133
    /*} catch (Exception foo) {
 
134
        errln("Exception when formatting with no params.");
 
135
    }*/
 
136
 
 
137
    delete messageFormatter;
 
138
}
 
139
 
 
140
/* @bug 4058973
 
141
 * MessageFormat.toPattern has weird rounding behavior.
 
142
 */
 
143
void MessageFormatRegressionTest::Test4058973() 
 
144
{
 
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");
 
148
 
 
149
    UnicodeString pat;
 
150
    pat = fmt->toPattern(pat);
 
151
    UnicodeString exp("{0,choice,0.0#no files|1.0#one file|1.0< {0,number,integer} files}");
 
152
    if (pat != exp) {
 
153
        errln("MessageFormat.toPattern failed");
 
154
        errln("Exp: " + exp);
 
155
        errln("Got: " + pat);
 
156
    }
 
157
 
 
158
    delete fmt;
 
159
}
 
160
/* @bug 4031438
 
161
 * More robust message formats.
 
162
 */
 
163
void MessageFormatRegressionTest::Test4031438() 
 
164
{
 
165
    UErrorCode status = U_ZERO_ERROR;
 
166
    
 
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'.");
 
169
 
 
170
    MessageFormat *messageFormatter = new MessageFormat("", status);
 
171
    failure(status, "new MessageFormat");
 
172
 
 
173
    //try {
 
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)
 
180
        };
 
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);
 
187
        ParsePosition pp(0);
 
188
        int32_t count = 0;
 
189
        Formattable *objs = messageFormatter->parse(tempBuffer, pp, count);
 
190
        //if(objs[7/*params.length*/] != NULL)
 
191
        //    errln("Parse failed with more than expected arguments");
 
192
 
 
193
        NumberFormat *fmt = 0;
 
194
        UnicodeString temp, temp1;
 
195
        
 
196
        for (int i = 0; i < count; i++) {
 
197
            
 
198
            // convert to string if not already
 
199
            Formattable obj = objs[i];
 
200
            temp.remove();
 
201
            if(obj.getType() == Formattable::kString)
 
202
                temp = obj.getString(temp);
 
203
            else {
 
204
                fmt = NumberFormat::createInstance(status);
 
205
                fmt->format(obj.getType() == Formattable::kLong ? obj.getLong() : obj.getDouble(), temp);
 
206
            }
 
207
 
 
208
            // convert to string if not already
 
209
            Formattable obj1 = params[i];
 
210
            temp1.remove();
 
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);
 
214
            }
 
215
            else
 
216
                temp1 = obj1.getString(temp1);
 
217
 
 
218
            //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
 
219
            if (temp != temp1) {
 
220
                errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i);
 
221
            }
 
222
        
 
223
        }
 
224
 
 
225
        delete fmt;
 
226
        delete [] objs;
 
227
 
 
228
        // {sfb} does this apply?  no way to really pass a null Formattable, 
 
229
        // only a null array
 
230
 
 
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");
 
238
        tempBuffer.remove();
 
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);
 
243
        
 
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());
 
251
    }*/
 
252
        delete messageFormatter;
 
253
}
 
254
 
 
255
void MessageFormatRegressionTest::Test4052223()
 
256
{
 
257
 
 
258
    ParsePosition pos(0);
 
259
    if (pos.getErrorIndex() != -1) {
 
260
        errln("ParsePosition.getErrorIndex initialization failed.");
 
261
    }
 
262
 
 
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.");
 
267
    
 
268
    int32_t count = 0;
 
269
    fmt->parse(str, pos, count);
 
270
 
 
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");
 
277
    
 
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");
 
281
    pos.setIndex(0); 
 
282
    pos.setErrorIndex(-1);
 
283
    Formattable obj;
 
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());
 
287
    pos.setIndex(0); 
 
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());
 
292
    pos.setIndex(0); 
 
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());
 
297
    
 
298
    delete fmt;
 
299
    delete f;
 
300
}
 
301
/* @bug 4104976
 
302
 * ChoiceFormat.equals(null) throws NullPointerException
 
303
 */
 
304
 
 
305
// {sfb} not really applicable in C++?? (kind of silly)
 
306
 
 
307
void MessageFormatRegressionTest::Test4104976()
 
308
{
 
309
    double limits [] = {1, 20};
 
310
    UnicodeString formats [] = {
 
311
        UnicodeString("xyz"), 
 
312
        UnicodeString("abc")
 
313
    };
 
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");
 
318
    //try {
 
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.");
 
323
    }*/
 
324
 
 
325
    delete cf;
 
326
}
 
327
 
 
328
/* @bug 4106659
 
329
 * ChoiceFormat.ctor(double[], String[]) doesn't check
 
330
 * whether lengths of input arrays are equal.
 
331
 */
 
332
 
 
333
// {sfb} again, not really applicable in C++
 
334
 
 
335
void MessageFormatRegressionTest::Test4106659()
 
336
{
 
337
    /*
 
338
    double limits [] = {
 
339
        1, 2, 3
 
340
    };
 
341
    UnicodeString formats [] = {
 
342
        "one", "two"
 
343
    };
 
344
    ChoiceFormat *cf = NULL;
 
345
    //try {
 
346
    //    cf = new ChoiceFormat(limits, formats, 3);
 
347
    //} catch (Exception foo) {
 
348
    //    logln("ChoiceFormat constructor should check for the array lengths");
 
349
    //    cf = null;
 
350
    //}
 
351
    //if (cf != null) 
 
352
    //    errln(cf->format(5));
 
353
    //
 
354
    delete cf;
 
355
    */
 
356
}
 
357
 
 
358
/* @bug 4106660
 
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.
 
362
 */
 
363
void MessageFormatRegressionTest::Test4106660()
 
364
{
 
365
    double limits [] = {3, 1, 2};
 
366
    UnicodeString formats [] = {
 
367
        UnicodeString("Three"), 
 
368
            UnicodeString("One"), 
 
369
            UnicodeString("Two")
 
370
    };
 
371
    ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3);
 
372
    double d = 5.0;
 
373
    UnicodeString str;
 
374
    FieldPosition pos(FieldPosition::DONT_CARE);
 
375
    str = cf->format(d, str, pos);
 
376
    if (str != "Two")
 
377
        errln( (UnicodeString) "format(" + d + ") = " + str);
 
378
 
 
379
    delete cf;
 
380
}
 
381
 
 
382
/* @bug 4111739
 
383
 * MessageFormat is incorrectly serialized/deserialized.
 
384
 */
 
385
 
 
386
// {sfb} doesn't apply in C++
 
387
 
 
388
void MessageFormatRegressionTest::Test4111739()
 
389
{
 
390
    /*MessageFormat format1 = null;
 
391
    MessageFormat format2 = null;
 
392
    ObjectOutputStream ostream = null;
 
393
    ByteArrayOutputStream baos = null;
 
394
    ObjectInputStream istream = null;
 
395
 
 
396
    try {
 
397
        baos = new ByteArrayOutputStream();
 
398
        ostream = new ObjectOutputStream(baos);
 
399
    } catch(IOException e) {
 
400
        errln("Unexpected exception : " + e.getMessage());
 
401
        return;
 
402
    }
 
403
 
 
404
    try {
 
405
        format1 = new MessageFormat("pattern{0}");
 
406
        ostream.writeObject(format1);
 
407
        ostream.flush();
 
408
 
 
409
        byte bytes[] = baos.toByteArray();
 
410
 
 
411
        istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
 
412
        format2 = (MessageFormat)istream.readObject();
 
413
    } catch(Exception e) {
 
414
        errln("Unexpected exception : " + e.getMessage());
 
415
    }
 
416
 
 
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() + ")");
 
421
    } else {
 
422
        logln("Serialization for MessageFormat is OK.");
 
423
    }*/
 
424
}
 
425
/* @bug 4114743
 
426
 * MessageFormat.applyPattern allows illegal patterns.
 
427
 */
 
428
void MessageFormatRegressionTest::Test4114743()
 
429
{
 
430
    UnicodeString originalPattern("initial pattern");
 
431
    UErrorCode status = U_ZERO_ERROR;
 
432
    MessageFormat *mf = new MessageFormat(originalPattern, status);
 
433
    failure(status, "new MessageFormat");
 
434
    //try {
 
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() + "\"");
 
442
    }*/
 
443
    delete mf;
 
444
}
 
445
 
 
446
/* @bug 4116444
 
447
 * MessageFormat.parse has different behavior in case of null.
 
448
 */
 
449
void MessageFormatRegressionTest::Test4116444()
 
450
{
 
451
    UnicodeString patterns [] = {
 
452
        (UnicodeString)"", 
 
453
        (UnicodeString)"one", 
 
454
        (UnicodeString) "{0,date,short}"
 
455
    };
 
456
    
 
457
    UErrorCode status = U_ZERO_ERROR;    
 
458
    MessageFormat *mf = new MessageFormat("", status);
 
459
    failure(status, "new MessageFormat");
 
460
 
 
461
    for (int i = 0; i < 3; i++) {
 
462
        UnicodeString pattern = patterns[i];
 
463
        mf->applyPattern(pattern, status);
 
464
        failure(status, "mf->applyPattern");
 
465
 
 
466
        //try {
 
467
        int32_t count = 0;    
 
468
        ParsePosition pp(0);
 
469
        Formattable *array = mf->parse(UnicodeString(""), pp, count);
 
470
            logln("pattern: \"" + pattern + "\"");
 
471
            log(" parsedObjects: ");
 
472
            if (array != NULL) {
 
473
                log("{");
 
474
                for (int j = 0; j < count; j++) {
 
475
                    //if (array[j] != null)
 
476
                    UnicodeString dummy;
 
477
                    err("\"" + array[j].getString(dummy) + "\"");
 
478
                    //else
 
479
                     //   log("null");
 
480
                    if (j < count- 1) 
 
481
                        log(",");
 
482
                }
 
483
                log("}") ;
 
484
                delete[] array;
 
485
            } else {
 
486
                log("null");
 
487
            }
 
488
            logln("");
 
489
        /*} catch (Exception e) {
 
490
            errln("pattern: \"" + pattern + "\"");
 
491
            errln("  Exception: " + e.getMessage());
 
492
        }*/
 
493
    }
 
494
 
 
495
    delete mf;
 
496
}
 
497
/* @bug 4114739 (FIX and add javadoc)
 
498
 * MessageFormat.format has undocumented behavior about empty format objects.
 
499
 */
 
500
 
 
501
// {sfb} doesn't apply in C++?
 
502
void MessageFormatRegressionTest::Test4114739()
 
503
{
 
504
 
 
505
    UErrorCode status = U_ZERO_ERROR;    
 
506
    MessageFormat *mf = new MessageFormat("<{0}>", status);
 
507
    failure(status, "new MessageFormat");
 
508
 
 
509
    Formattable *objs1 = NULL;
 
510
    //Formattable objs2 [] = {};
 
511
    //Formattable *objs3 [] = {NULL};
 
512
    //try {
 
513
    UnicodeString pat;
 
514
    UnicodeString res;
 
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.");
 
528
    }*/
 
529
 
 
530
    delete mf;
 
531
}
 
532
 
 
533
/* @bug 4113018
 
534
 * MessageFormat.applyPattern works wrong with illegal patterns.
 
535
 */
 
536
void MessageFormatRegressionTest::Test4113018()
 
537
{
 
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}");
 
543
    UnicodeString pat;
 
544
    logln("pattern before: \"" + mf->toPattern(pat) + "\"");
 
545
    logln("illegal pattern: \"" + illegalPattern + "\"");
 
546
    //try {
 
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() + "\"");
 
553
    }*/
 
554
    delete mf;
 
555
}
 
556
 
 
557
/* @bug 4106661
 
558
 * ChoiceFormat is silent about the pattern usage in javadoc.
 
559
 */
 
560
void MessageFormatRegressionTest::Test4106661()
 
561
{
 
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");
 
566
    UnicodeString pat;
 
567
    logln("Formatter Pattern : " + fmt->toPattern(pat));
 
568
 
 
569
    FieldPosition bogus(FieldPosition::DONT_CARE);
 
570
    UnicodeString str;
 
571
 
 
572
    // Will this work for -inf?
 
573
    logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
 
574
    failure(status, "fmt->format");
 
575
    str.remove();
 
576
    logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
 
577
    failure(status, "fmt->format");
 
578
    str.remove();
 
579
    logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
 
580
    failure(status, "fmt->format");
 
581
    str.remove();
 
582
    logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
 
583
    failure(status, "fmt->format");
 
584
    str.remove();
 
585
    logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
 
586
    failure(status, "fmt->format");
 
587
    str.remove();
 
588
    logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
 
589
    failure(status, "fmt->format");
 
590
    str.remove();
 
591
    logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
 
592
    failure(status, "fmt->format");
 
593
    str.remove();
 
594
    logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
 
595
    failure(status, "fmt->format");
 
596
    str.remove();
 
597
    logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
 
598
    failure(status, "fmt->format");
 
599
    str.remove();
 
600
    logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
 
601
    failure(status, "fmt->format");
 
602
    str.remove();
 
603
    logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
 
604
    failure(status, "fmt->format");
 
605
 
 
606
    delete fmt;
 
607
}
 
608
 
 
609
/* @bug 4094906
 
610
 * ChoiceFormat should accept \u221E as eq. to INF.
 
611
 */
 
612
void MessageFormatRegressionTest::Test4094906()
 
613
{
 
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.";
 
620
 
 
621
    ChoiceFormat *fmt = new ChoiceFormat(pattern, status);
 
622
    failure(status, "new ChoiceFormat");
 
623
    UnicodeString pat;
 
624
    if (fmt->toPattern(pat) != pattern) {
 
625
        errln( (UnicodeString) "Formatter Pattern : " + pat);
 
626
        errln( (UnicodeString) "Expected Pattern  : " + pattern);
 
627
    }
 
628
    FieldPosition bogus(FieldPosition::DONT_CARE);
 
629
    UnicodeString str;
 
630
 
 
631
    // Will this work for -inf?
 
632
    logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status));
 
633
    failure(status, "fmt->format");
 
634
    str.remove();
 
635
    logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
 
636
    failure(status, "fmt->format");
 
637
    str.remove();
 
638
    logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
 
639
    failure(status, "fmt->format");
 
640
    str.remove();
 
641
    logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
 
642
    failure(status, "fmt->format");
 
643
    str.remove();
 
644
    logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
 
645
    failure(status, "fmt->format");
 
646
    str.remove();
 
647
    logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
 
648
    failure(status, "fmt->format");
 
649
    str.remove();
 
650
    logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
 
651
    failure(status, "fmt->format");
 
652
    str.remove();
 
653
    logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
 
654
    failure(status, "fmt->format");
 
655
    str.remove();
 
656
    logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
 
657
    failure(status, "fmt->format");
 
658
    str.remove();
 
659
    logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status));
 
660
    failure(status, "fmt->format");
 
661
    str.remove();
 
662
    logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status));
 
663
    failure(status, "fmt->format");
 
664
 
 
665
    delete fmt;
 
666
}
 
667
 
 
668
/* @bug 4118592
 
669
 * MessageFormat.parse fails with ChoiceFormat.
 
670
 */
 
671
void MessageFormatRegressionTest::Test4118592()
 
672
{
 
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;
 
679
 
 
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");
 
685
        prefix += "x";
 
686
        //Object[] objs = mf.parse(formatted, new ParsePosition(0));
 
687
        int32_t count = 0;
 
688
        ParsePosition pp(0);
 
689
        objs = mf->parse(formatted, pp, count);
 
690
        UnicodeString pat;
 
691
        logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\"");
 
692
        log(" \"" + formatted + "\" parsed as ");
 
693
        if (objs == NULL) 
 
694
            logln("  null");
 
695
        else {
 
696
            UnicodeString temp;
 
697
            if(objs[0].getType() == Formattable::kString)
 
698
                logln((UnicodeString)"  " + objs[0].getString(temp));
 
699
            else
 
700
                logln((UnicodeString)"  " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble()));
 
701
            delete[] objs;
 
702
 
 
703
        }
 
704
    }
 
705
 
 
706
    delete mf;
 
707
}
 
708
/* @bug 4118594
 
709
 * MessageFormat.parse fails for some patterns.
 
710
 */
 
711
void MessageFormatRegressionTest::Test4118594()
 
712
{
 
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));
 
718
    int32_t count = 0;
 
719
    ParsePosition pp(0);
 
720
    Formattable *objs = mf->parse(forParsing, pp, count);
 
721
    UnicodeString pat;
 
722
    logln("pattern: \"" + mf->toPattern(pat) + "\"");
 
723
    logln("text for parsing: \"" + forParsing + "\"");
 
724
    UnicodeString str;
 
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");
 
735
    pat.remove();
 
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));
 
742
    int32_t count1 = 0;
 
743
    pp.setIndex(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());
 
748
 
 
749
    delete [] objs;
 
750
    delete [] newobjs;
 
751
    delete mf;
 
752
}
 
753
/* @bug 4105380
 
754
 * When using ChoiceFormat, MessageFormat is not good for I18n.
 
755
 */
 
756
void MessageFormatRegressionTest::Test4105380()
 
757
{
 
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"
 
770
    };
 
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")
 
778
    };
 
779
    
 
780
    FieldPosition bogus(FieldPosition::DONT_CARE);
 
781
 
 
782
    UnicodeString result;
 
783
    logln(form1->format(testArgs, 2, result, bogus, status));
 
784
    failure(status, "form1->format");
 
785
    result.remove();
 
786
    logln(form2->format(testArgs, 2, result, bogus, status));
 
787
    failure(status, "form1->format");
 
788
 
 
789
    delete form1;
 
790
    delete form2;
 
791
    delete fileform;
 
792
}
 
793
/* @bug 4120552
 
794
 * MessageFormat.parse incorrectly sets errorIndex.
 
795
 */
 
796
void MessageFormatRegressionTest::Test4120552()
 
797
{
 
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"
 
805
    };
 
806
    UnicodeString pat;
 
807
    logln("pattern: \"" + mf->toPattern(pat) + "\"");
 
808
    for (int i = 0; i < 3; i++) {
 
809
        ParsePosition pp(0);
 
810
        //Object[] objs = mf.parse(texts[i], pp);
 
811
        int32_t count = 0;
 
812
        Formattable *objs = mf->parse(texts[i], pp, count);
 
813
        log("  text for parsing: \"" + texts[i] + "\"");
 
814
        if (objs == NULL) {
 
815
            logln("  (incorrectly formatted string)");
 
816
            if (pp.getErrorIndex() == -1)
 
817
                errln(UnicodeString("Incorrect error index: ") + pp.getErrorIndex());
 
818
        } else {
 
819
            logln("  (correctly formatted string)");
 
820
            delete[] objs;
 
821
        }
 
822
    }
 
823
    delete mf;
 
824
}
 
825
 
 
826
/**
 
827
 * @bug 4142938
 
828
 * MessageFormat handles single quotes in pattern wrong.
 
829
 * This is actually a problem in ChoiceFormat; it doesn't
 
830
 * understand single quotes.
 
831
 */
 
832
void MessageFormatRegressionTest::Test4142938() 
 
833
{
 
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");
 
840
 
 
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 ")
 
845
    };  
 
846
    UnicodeString SUFFIX [] = {
 
847
        UnicodeString(),
 
848
        UNICODE_STRING(" client personnel.", 18),
 
849
        UNICODE_STRING(" clients personnels.", 20)
 
850
    };
 
851
 
 
852
    for (int i=0; i<3; i++) {
 
853
        UnicodeString out;
 
854
        //out = mf->format(new Object[]{new Integer(i)});
 
855
        Formattable objs [] = {
 
856
            Formattable((int32_t)i)
 
857
        };
 
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] + "\"");
 
864
        }
 
865
        else {
 
866
            if (!out.startsWith(PREFIX[i]) ||
 
867
                !out.endsWith(SUFFIX[i]))
 
868
                errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
 
869
                      SUFFIX[i] + "\"");
 
870
        }
 
871
    }
 
872
 
 
873
    delete mf;
 
874
}
 
875
 
 
876
/**
 
877
 * @bug 4142938
 
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.
 
883
 */
 
884
void MessageFormatRegressionTest::TestChoicePatternQuote() 
 
885
{
 
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)"'",
 
892
    };
 
893
    for (int i=0; i<9; i+=3) {
 
894
        //try {
 
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) {
 
899
                UnicodeString out;
 
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] + '"');
 
905
            }
 
906
            UnicodeString pat;
 
907
            pat = cf->toPattern(pat);
 
908
            UnicodeString pat2;
 
909
            ChoiceFormat *cf2 = new ChoiceFormat(pat, status);
 
910
            pat2 = cf2->toPattern(pat2);
 
911
            if (pat != pat2)
 
912
                errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
 
913
            else
 
914
                logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
 
915
        /*}
 
916
        catch (IllegalArgumentException e) {
 
917
            errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
 
918
        }*/
 
919
    
 
920
        delete cf;
 
921
        delete cf2;
 
922
    }
 
923
}
 
924
 
 
925
/**
 
926
 * @bug 4112104
 
927
 * MessageFormat.equals(null) throws a NullPointerException.  The JLS states
 
928
 * that it should return false.
 
929
 */
 
930
void MessageFormatRegressionTest::Test4112104() 
 
931
{
 
932
    UErrorCode status = U_ZERO_ERROR;
 
933
    MessageFormat *format = new MessageFormat("", status);
 
934
    failure(status, "new MessageFormat");
 
935
    //try {
 
936
        // This should NOT throw an exception
 
937
        if (format == NULL) {
 
938
            // It also should return false
 
939
            errln("MessageFormat.equals(null) returns false");
 
940
        }
 
941
    /*}
 
942
    catch (NullPointerException e) {
 
943
        errln("MessageFormat.equals(null) throws " + e);
 
944
    }*/
 
945
    delete format;
 
946
}
 
947