]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2001, International Business Machines Corporation and | |
4 | * others. All Rights Reserved. | |
5 | ********************************************************************/ | |
6 | ||
7 | #include "unicode/utypes.h" | |
8 | ||
9 | #if !UCONFIG_NO_FORMATTING | |
10 | ||
11 | #include "msfmrgts.h" | |
12 | ||
13 | #include "unicode/format.h" | |
14 | #include "unicode/decimfmt.h" | |
15 | #include "unicode/locid.h" | |
16 | #include "unicode/msgfmt.h" | |
17 | #include "unicode/numfmt.h" | |
18 | #include "unicode/choicfmt.h" | |
19 | #include "unicode/gregocal.h" | |
20 | ||
21 | // ***************************************************************************** | |
22 | // class MessageFormatRegressionTest | |
23 | // ***************************************************************************** | |
24 | ||
25 | #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; | |
26 | ||
27 | void | |
28 | MessageFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
29 | { | |
30 | // if (exec) logln((UnicodeString)"TestSuite MessageFormatRegressionTest"); | |
31 | switch (index) { | |
32 | CASE(0,Test4074764) | |
33 | CASE(1,Test4058973) | |
34 | CASE(2,Test4031438) | |
35 | CASE(3,Test4052223) | |
36 | CASE(4,Test4104976) | |
37 | CASE(5,Test4106659) | |
38 | CASE(6,Test4106660) | |
39 | CASE(7,Test4111739) | |
40 | CASE(8,Test4114743) | |
41 | CASE(9,Test4116444) | |
42 | CASE(10,Test4114739) | |
43 | CASE(11,Test4113018) | |
44 | CASE(12,Test4106661) | |
45 | CASE(13,Test4094906) | |
46 | CASE(14,Test4118592) | |
47 | CASE(15,Test4118594) | |
48 | CASE(16,Test4105380) | |
49 | CASE(17,Test4120552) | |
50 | CASE(18,Test4142938) | |
51 | CASE(19,TestChoicePatternQuote) | |
52 | CASE(20,Test4112104) | |
53 | ||
54 | default: name = ""; break; | |
55 | } | |
56 | } | |
57 | ||
58 | UBool | |
59 | MessageFormatRegressionTest::failure(UErrorCode status, const char* msg) | |
60 | { | |
61 | if(U_FAILURE(status)) { | |
62 | errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status)); | |
63 | return TRUE; | |
64 | } | |
65 | ||
66 | return FALSE; | |
67 | } | |
68 | ||
69 | /* @bug 4074764 | |
70 | * Null exception when formatting pattern with MessageFormat | |
71 | * with no parameters. | |
72 | */ | |
73 | void MessageFormatRegressionTest::Test4074764() { | |
74 | UnicodeString pattern [] = { | |
75 | "Message without param", | |
76 | "Message with param:{0}", | |
77 | "Longer Message with param {0}" | |
78 | }; | |
79 | //difference between the two param strings are that | |
80 | //in the first one, the param position is within the | |
81 | //length of the string without param while it is not so | |
82 | //in the other case. | |
83 | ||
84 | UErrorCode status = U_ZERO_ERROR; | |
85 | MessageFormat *messageFormatter = new MessageFormat("", status); | |
86 | ||
87 | failure(status, "couldn't create MessageFormat"); | |
88 | ||
89 | //try { | |
90 | //Apply pattern with param and print the result | |
91 | messageFormatter->applyPattern(pattern[1], status); | |
92 | failure(status, "messageFormat->applyPattern"); | |
93 | //Object[] params = {new UnicodeString("BUG"), new Date()}; | |
94 | Formattable params [] = { | |
95 | Formattable(UnicodeString("BUG")), | |
96 | Formattable(0, Formattable::kIsDate) | |
97 | }; | |
98 | UnicodeString tempBuffer; | |
99 | FieldPosition pos(FieldPosition::DONT_CARE); | |
100 | tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status); | |
101 | if( tempBuffer != "Message with param:BUG" || failure(status, "messageFormat->format")) | |
102 | errln("MessageFormat with one param test failed."); | |
103 | logln("Formatted with one extra param : " + tempBuffer); | |
104 | ||
105 | //Apply pattern without param and print the result | |
106 | messageFormatter->applyPattern(pattern[0], status); | |
107 | failure(status, "messageFormatter->applyPattern"); | |
108 | ||
109 | // {sfb} how much does this apply in C++? | |
110 | // do we want to verify that the Formattable* array is not NULL, | |
111 | // or is that the user's responsibility? | |
112 | // additionally, what should be the item count? | |
113 | // for bug testing purposes, assume that something was set to | |
114 | // NULL by mistake, and that the length should be non-zero | |
115 | ||
116 | //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status); | |
117 | tempBuffer.remove(); | |
118 | tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status); | |
119 | ||
120 | if( tempBuffer != "Message without param" || failure(status, "messageFormat->format")) | |
121 | errln("MessageFormat with no param test failed."); | |
122 | logln("Formatted with no params : " + tempBuffer); | |
123 | ||
124 | tempBuffer.remove(); | |
125 | tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status); | |
126 | if (tempBuffer != "Message without param" || failure(status, "messageFormat->format")) | |
127 | errln("Formatted with arguments > subsitution failed. result = " + tempBuffer); | |
128 | logln("Formatted with extra params : " + tempBuffer); | |
129 | //This statement gives an exception while formatting... | |
130 | //If we use pattern[1] for the message with param, | |
131 | //we get an NullPointerException in MessageFormat.java(617) | |
132 | //If we use pattern[2] for the message with param, | |
133 | //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614) | |
134 | //Both are due to maxOffset not being reset to -1 | |
135 | //in applyPattern() when the pattern does not | |
136 | //contain any param. | |
137 | /*} catch (Exception foo) { | |
138 | errln("Exception when formatting with no params."); | |
139 | }*/ | |
140 | ||
141 | delete messageFormatter; | |
142 | } | |
143 | ||
144 | /* @bug 4058973 | |
145 | * MessageFormat.toPattern has weird rounding behavior. | |
146 | */ | |
147 | void MessageFormatRegressionTest::Test4058973() | |
148 | { | |
149 | UErrorCode status = U_ZERO_ERROR; | |
150 | MessageFormat *fmt = new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}", status); | |
151 | failure(status, "new MessageFormat"); | |
152 | ||
153 | UnicodeString pat; | |
154 | pat = fmt->toPattern(pat); | |
155 | UnicodeString exp("{0,choice,0.0#no files|1.0#one file|1.0< {0,number,integer} files}"); | |
156 | if (pat != exp) { | |
157 | errln("MessageFormat.toPattern failed"); | |
158 | errln("Exp: " + exp); | |
159 | errln("Got: " + pat); | |
160 | } | |
161 | ||
162 | delete fmt; | |
163 | } | |
164 | /* @bug 4031438 | |
165 | * More robust message formats. | |
166 | */ | |
167 | void MessageFormatRegressionTest::Test4031438() | |
168 | { | |
169 | UErrorCode status = U_ZERO_ERROR; | |
170 | ||
171 | UnicodeString pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}."); | |
172 | UnicodeString pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'."); | |
173 | ||
174 | MessageFormat *messageFormatter = new MessageFormat("", status); | |
175 | failure(status, "new MessageFormat"); | |
176 | ||
177 | //try { | |
178 | logln("Apply with pattern : " + pattern1); | |
179 | messageFormatter->applyPattern(pattern1, status); | |
180 | failure(status, "messageFormat->applyPattern"); | |
181 | //Object[] params = {new Integer(7)}; | |
182 | Formattable params []= { | |
183 | Formattable((int32_t)7) | |
184 | }; | |
185 | UnicodeString tempBuffer; | |
186 | FieldPosition pos(FieldPosition::DONT_CARE); | |
187 | tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status); | |
188 | if(tempBuffer != "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status, "MessageFormat::format")) | |
189 | errln("Tests arguments < substitution failed"); | |
190 | logln("Formatted with 7 : " + tempBuffer); | |
191 | ParsePosition pp(0); | |
192 | int32_t count = 0; | |
193 | Formattable *objs = messageFormatter->parse(tempBuffer, pp, count); | |
194 | //if(objs[7/*params.length*/] != NULL) | |
195 | // errln("Parse failed with more than expected arguments"); | |
196 | ||
197 | NumberFormat *fmt = 0; | |
198 | UnicodeString temp, temp1; | |
199 | ||
200 | for (int i = 0; i < count; i++) { | |
201 | ||
202 | // convert to string if not already | |
203 | Formattable obj = objs[i]; | |
204 | temp.remove(); | |
205 | if(obj.getType() == Formattable::kString) | |
206 | temp = obj.getString(temp); | |
207 | else { | |
208 | fmt = NumberFormat::createInstance(status); | |
209 | fmt->format(obj.getType() == Formattable::kLong ? obj.getLong() : obj.getDouble(), temp); | |
210 | } | |
211 | ||
212 | // convert to string if not already | |
213 | Formattable obj1 = params[i]; | |
214 | temp1.remove(); | |
215 | if(obj1.getType() == Formattable::kDouble || obj1.getType() == Formattable::kLong) { | |
216 | fmt = NumberFormat::createInstance(status); | |
217 | fmt->format(obj1.getType() == Formattable::kLong ? obj1.getLong() : obj1.getDouble(), temp1); | |
218 | } | |
219 | else | |
220 | temp1 = obj1.getString(temp1); | |
221 | ||
222 | //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) { | |
223 | if (temp != temp1) { | |
224 | errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i); | |
225 | } | |
226 | ||
227 | } | |
228 | ||
229 | delete fmt; | |
230 | delete [] objs; | |
231 | ||
232 | // {sfb} does this apply? no way to really pass a null Formattable, | |
233 | // only a null array | |
234 | ||
235 | /*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status); | |
236 | if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format")) | |
237 | errln("Tests with no arguments failed"); | |
238 | logln("Formatted with null : " + tempBuffer);*/ | |
239 | logln("Apply with pattern : " + pattern2); | |
240 | messageFormatter->applyPattern(pattern2, status); | |
241 | failure(status, "messageFormatter->applyPattern"); | |
242 | tempBuffer.remove(); | |
243 | tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status); | |
244 | if (tempBuffer != "Double ' Quotes 7 test and quoted {1} test plus other {2} stuff.") | |
245 | errln("quote format test (w/ params) failed."); | |
246 | logln("Formatted with params : " + tempBuffer); | |
247 | ||
248 | /*tempBuffer = messageFormatter->format(null); | |
249 | if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff.")) | |
250 | errln("quote format test (w/ null) failed."); | |
251 | logln("Formatted with null : " + tempBuffer); | |
252 | logln("toPattern : " + messageFormatter.toPattern());*/ | |
253 | /*} catch (Exception foo) { | |
254 | errln("Exception when formatting in bug 4031438. "+foo.getMessage()); | |
255 | }*/ | |
256 | delete messageFormatter; | |
257 | } | |
258 | ||
259 | void MessageFormatRegressionTest::Test4052223() | |
260 | { | |
261 | ||
262 | ParsePosition pos(0); | |
263 | if (pos.getErrorIndex() != -1) { | |
264 | errln("ParsePosition.getErrorIndex initialization failed."); | |
265 | } | |
266 | ||
267 | UErrorCode status = U_ZERO_ERROR; | |
268 | MessageFormat *fmt = new MessageFormat("There are {0} apples growing on the {1} tree.", status); | |
269 | failure(status, "new MessageFormat"); | |
270 | UnicodeString str("There is one apple growing on the peach tree."); | |
271 | ||
272 | int32_t count = 0; | |
273 | fmt->parse(str, pos, count); | |
274 | ||
275 | logln(UnicodeString("unparsable string , should fail at ") + pos.getErrorIndex()); | |
276 | if (pos.getErrorIndex() == -1) | |
277 | errln("Bug 4052223 failed : parsing string " + str); | |
278 | pos.setErrorIndex(4); | |
279 | if (pos.getErrorIndex() != 4) | |
280 | errln(UnicodeString("setErrorIndex failed, got ") + pos.getErrorIndex() + " instead of 4"); | |
281 | ||
282 | ChoiceFormat *f = new ChoiceFormat( | |
283 | "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.", status); | |
284 | failure(status, "new ChoiceFormat"); | |
285 | pos.setIndex(0); | |
286 | pos.setErrorIndex(-1); | |
287 | Formattable obj; | |
288 | f->parse("are negative", obj, pos); | |
289 | if (pos.getErrorIndex() != -1 && obj.getDouble() == -1.0) | |
290 | errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos.getErrorIndex()); | |
291 | pos.setIndex(0); | |
292 | pos.setErrorIndex(-1); | |
293 | f->parse("are no or fraction ", obj, pos); | |
294 | if (pos.getErrorIndex() != -1 && obj.getDouble() == 0.0) | |
295 | errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos.getErrorIndex()); | |
296 | pos.setIndex(0); | |
297 | pos.setErrorIndex(-1); | |
298 | f->parse("go postal", obj, pos); | |
299 | if (pos.getErrorIndex() == -1 && ! uprv_isNaN(obj.getDouble())) | |
300 | errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos.getErrorIndex()); | |
301 | ||
302 | delete fmt; | |
303 | delete f; | |
304 | } | |
305 | /* @bug 4104976 | |
306 | * ChoiceFormat.equals(null) throws NullPointerException | |
307 | */ | |
308 | ||
309 | // {sfb} not really applicable in C++?? (kind of silly) | |
310 | ||
311 | void MessageFormatRegressionTest::Test4104976() | |
312 | { | |
313 | double limits [] = {1, 20}; | |
314 | UnicodeString formats [] = { | |
315 | UnicodeString("xyz"), | |
316 | UnicodeString("abc") | |
317 | }; | |
318 | int32_t formats_length = (int32_t)(sizeof(formats)/sizeof(formats[0])); | |
319 | UErrorCode status = U_ZERO_ERROR; | |
320 | ChoiceFormat *cf = new ChoiceFormat(limits, formats, formats_length); | |
321 | failure(status, "new ChoiceFormat"); | |
322 | //try { | |
323 | log("Compares to null is always false, returned : "); | |
324 | logln(cf == NULL ? "TRUE" : "FALSE"); | |
325 | /*} catch (Exception foo) { | |
326 | errln("ChoiceFormat.equals(null) throws exception."); | |
327 | }*/ | |
328 | ||
329 | delete cf; | |
330 | } | |
331 | ||
332 | /* @bug 4106659 | |
333 | * ChoiceFormat.ctor(double[], String[]) doesn't check | |
334 | * whether lengths of input arrays are equal. | |
335 | */ | |
336 | ||
337 | // {sfb} again, not really applicable in C++ | |
338 | ||
339 | void MessageFormatRegressionTest::Test4106659() | |
340 | { | |
341 | /* | |
342 | double limits [] = { | |
343 | 1, 2, 3 | |
344 | }; | |
345 | UnicodeString formats [] = { | |
346 | "one", "two" | |
347 | }; | |
348 | ChoiceFormat *cf = NULL; | |
349 | //try { | |
350 | // cf = new ChoiceFormat(limits, formats, 3); | |
351 | //} catch (Exception foo) { | |
352 | // logln("ChoiceFormat constructor should check for the array lengths"); | |
353 | // cf = null; | |
354 | //} | |
355 | //if (cf != null) | |
356 | // errln(cf->format(5)); | |
357 | // | |
358 | delete cf; | |
359 | */ | |
360 | } | |
361 | ||
362 | /* @bug 4106660 | |
363 | * ChoiceFormat.ctor(double[], String[]) allows unordered double array. | |
364 | * This is not a bug, added javadoc to emphasize the use of limit | |
365 | * array must be in ascending order. | |
366 | */ | |
367 | void MessageFormatRegressionTest::Test4106660() | |
368 | { | |
369 | double limits [] = {3, 1, 2}; | |
370 | UnicodeString formats [] = { | |
371 | UnicodeString("Three"), | |
372 | UnicodeString("One"), | |
373 | UnicodeString("Two") | |
374 | }; | |
375 | ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3); | |
376 | double d = 5.0; | |
377 | UnicodeString str; | |
378 | FieldPosition pos(FieldPosition::DONT_CARE); | |
379 | str = cf->format(d, str, pos); | |
380 | if (str != "Two") | |
381 | errln( (UnicodeString) "format(" + d + ") = " + str); | |
382 | ||
383 | delete cf; | |
384 | } | |
385 | ||
386 | /* @bug 4111739 | |
387 | * MessageFormat is incorrectly serialized/deserialized. | |
388 | */ | |
389 | ||
390 | // {sfb} doesn't apply in C++ | |
391 | ||
392 | void MessageFormatRegressionTest::Test4111739() | |
393 | { | |
394 | /*MessageFormat format1 = null; | |
395 | MessageFormat format2 = null; | |
396 | ObjectOutputStream ostream = null; | |
397 | ByteArrayOutputStream baos = null; | |
398 | ObjectInputStream istream = null; | |
399 | ||
400 | try { | |
401 | baos = new ByteArrayOutputStream(); | |
402 | ostream = new ObjectOutputStream(baos); | |
403 | } catch(IOException e) { | |
404 | errln("Unexpected exception : " + e.getMessage()); | |
405 | return; | |
406 | } | |
407 | ||
408 | try { | |
409 | format1 = new MessageFormat("pattern{0}"); | |
410 | ostream.writeObject(format1); | |
411 | ostream.flush(); | |
412 | ||
413 | byte bytes[] = baos.toByteArray(); | |
414 | ||
415 | istream = new ObjectInputStream(new ByteArrayInputStream(bytes)); | |
416 | format2 = (MessageFormat)istream.readObject(); | |
417 | } catch(Exception e) { | |
418 | errln("Unexpected exception : " + e.getMessage()); | |
419 | } | |
420 | ||
421 | if (!format1.equals(format2)) { | |
422 | errln("MessageFormats before and after serialization are not" + | |
423 | " equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " + | |
424 | format2 + "(" + format2.toPattern() + ")"); | |
425 | } else { | |
426 | logln("Serialization for MessageFormat is OK."); | |
427 | }*/ | |
428 | } | |
429 | /* @bug 4114743 | |
430 | * MessageFormat.applyPattern allows illegal patterns. | |
431 | */ | |
432 | void MessageFormatRegressionTest::Test4114743() | |
433 | { | |
434 | UnicodeString originalPattern("initial pattern"); | |
435 | UErrorCode status = U_ZERO_ERROR; | |
436 | MessageFormat *mf = new MessageFormat(originalPattern, status); | |
437 | failure(status, "new MessageFormat"); | |
438 | //try { | |
439 | UnicodeString illegalPattern("ab { '}' de"); | |
440 | mf->applyPattern(illegalPattern, status); | |
441 | if( ! U_FAILURE(status)) | |
442 | errln("illegal pattern: \"" + illegalPattern + "\""); | |
443 | /*} catch (IllegalArgumentException foo) { | |
444 | if (!originalPattern.equals(mf.toPattern())) | |
445 | errln("pattern after: \"" + mf.toPattern() + "\""); | |
446 | }*/ | |
447 | delete mf; | |
448 | } | |
449 | ||
450 | /* @bug 4116444 | |
451 | * MessageFormat.parse has different behavior in case of null. | |
452 | */ | |
453 | void MessageFormatRegressionTest::Test4116444() | |
454 | { | |
455 | UnicodeString patterns [] = { | |
456 | (UnicodeString)"", | |
457 | (UnicodeString)"one", | |
458 | (UnicodeString) "{0,date,short}" | |
459 | }; | |
460 | ||
461 | UErrorCode status = U_ZERO_ERROR; | |
462 | MessageFormat *mf = new MessageFormat("", status); | |
463 | failure(status, "new MessageFormat"); | |
464 | ||
465 | for (int i = 0; i < 3; i++) { | |
466 | UnicodeString pattern = patterns[i]; | |
467 | mf->applyPattern(pattern, status); | |
468 | failure(status, "mf->applyPattern"); | |
469 | ||
470 | //try { | |
471 | int32_t count = 0; | |
472 | ParsePosition pp(0); | |
473 | Formattable *array = mf->parse(UnicodeString(""), pp, count); | |
474 | logln("pattern: \"" + pattern + "\""); | |
475 | log(" parsedObjects: "); | |
476 | if (array != NULL) { | |
477 | log("{"); | |
478 | for (int j = 0; j < count; j++) { | |
479 | //if (array[j] != null) | |
480 | UnicodeString dummy; | |
481 | err("\"" + array[j].getString(dummy) + "\""); | |
482 | //else | |
483 | // log("null"); | |
484 | if (j < count- 1) | |
485 | log(","); | |
486 | } | |
487 | log("}") ; | |
488 | delete[] array; | |
489 | } else { | |
490 | log("null"); | |
491 | } | |
492 | logln(""); | |
493 | /*} catch (Exception e) { | |
494 | errln("pattern: \"" + pattern + "\""); | |
495 | errln(" Exception: " + e.getMessage()); | |
496 | }*/ | |
497 | } | |
498 | ||
499 | delete mf; | |
500 | } | |
501 | /* @bug 4114739 (FIX and add javadoc) | |
502 | * MessageFormat.format has undocumented behavior about empty format objects. | |
503 | */ | |
504 | ||
505 | // {sfb} doesn't apply in C++? | |
506 | void MessageFormatRegressionTest::Test4114739() | |
507 | { | |
508 | ||
509 | UErrorCode status = U_ZERO_ERROR; | |
510 | MessageFormat *mf = new MessageFormat("<{0}>", status); | |
511 | failure(status, "new MessageFormat"); | |
512 | ||
513 | Formattable *objs1 = NULL; | |
514 | //Formattable objs2 [] = {}; | |
515 | //Formattable *objs3 [] = {NULL}; | |
516 | //try { | |
517 | UnicodeString pat; | |
518 | UnicodeString res; | |
519 | logln("pattern: \"" + mf->toPattern(pat) + "\""); | |
520 | log("format(null) : "); | |
521 | FieldPosition pos(FieldPosition::DONT_CARE); | |
522 | logln("\"" + mf->format(objs1, 0, res, pos, status) + "\""); | |
523 | failure(status, "mf->format"); | |
524 | /*log("format({}) : "); | |
525 | logln("\"" + mf->format(objs2, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\""); | |
526 | failure(status, "mf->format"); | |
527 | log("format({null}) :"); | |
528 | logln("\"" + mf->format(objs3, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\""); | |
529 | failure(status, "mf->format");*/ | |
530 | /*} catch (Exception e) { | |
531 | errln("Exception thrown for null argument tests."); | |
532 | }*/ | |
533 | ||
534 | delete mf; | |
535 | } | |
536 | ||
537 | /* @bug 4113018 | |
538 | * MessageFormat.applyPattern works wrong with illegal patterns. | |
539 | */ | |
540 | void MessageFormatRegressionTest::Test4113018() | |
541 | { | |
542 | UnicodeString originalPattern("initial pattern"); | |
543 | UErrorCode status = U_ZERO_ERROR; | |
544 | MessageFormat *mf = new MessageFormat(originalPattern, status); | |
545 | failure(status, "new messageFormat"); | |
546 | UnicodeString illegalPattern("format: {0, xxxYYY}"); | |
547 | UnicodeString pat; | |
548 | logln("pattern before: \"" + mf->toPattern(pat) + "\""); | |
549 | logln("illegal pattern: \"" + illegalPattern + "\""); | |
550 | //try { | |
551 | mf->applyPattern(illegalPattern, status); | |
552 | if( ! U_FAILURE(status)) | |
553 | errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern); | |
554 | /*} catch (IllegalArgumentException e) { | |
555 | if (!originalPattern.equals(mf.toPattern())) | |
556 | errln("pattern after: \"" + mf.toPattern() + "\""); | |
557 | }*/ | |
558 | delete mf; | |
559 | } | |
560 | ||
561 | /* @bug 4106661 | |
562 | * ChoiceFormat is silent about the pattern usage in javadoc. | |
563 | */ | |
564 | void MessageFormatRegressionTest::Test4106661() | |
565 | { | |
566 | UErrorCode status = U_ZERO_ERROR; | |
567 | ChoiceFormat *fmt = new ChoiceFormat( | |
568 | "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status); | |
569 | failure(status, "new ChoiceFormat"); | |
570 | UnicodeString pat; | |
571 | logln("Formatter Pattern : " + fmt->toPattern(pat)); | |
572 | ||
573 | FieldPosition bogus(FieldPosition::DONT_CARE); | |
574 | UnicodeString str; | |
575 | ||
576 | // Will this work for -inf? | |
577 | logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status)); | |
578 | failure(status, "fmt->format"); | |
579 | str.remove(); | |
580 | logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); | |
581 | failure(status, "fmt->format"); | |
582 | str.remove(); | |
583 | logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); | |
584 | failure(status, "fmt->format"); | |
585 | str.remove(); | |
586 | logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status)); | |
587 | failure(status, "fmt->format"); | |
588 | str.remove(); | |
589 | logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status)); | |
590 | failure(status, "fmt->format"); | |
591 | str.remove(); | |
592 | logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status)); | |
593 | failure(status, "fmt->format"); | |
594 | str.remove(); | |
595 | logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status)); | |
596 | failure(status, "fmt->format"); | |
597 | str.remove(); | |
598 | logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status)); | |
599 | failure(status, "fmt->format"); | |
600 | str.remove(); | |
601 | logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status)); | |
602 | failure(status, "fmt->format"); | |
603 | str.remove(); | |
604 | logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status)); | |
605 | failure(status, "fmt->format"); | |
606 | str.remove(); | |
607 | logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status)); | |
608 | failure(status, "fmt->format"); | |
609 | ||
610 | delete fmt; | |
611 | } | |
612 | ||
613 | /* @bug 4094906 | |
614 | * ChoiceFormat should accept \u221E as eq. to INF. | |
615 | */ | |
616 | void MessageFormatRegressionTest::Test4094906() | |
617 | { | |
618 | UErrorCode status = U_ZERO_ERROR; | |
619 | UnicodeString pattern("-"); | |
620 | pattern += (UChar) 0x221E; | |
621 | pattern += "<are negative|0.0<are no or fraction|1.0#is one|1.0<is 1+|"; | |
622 | pattern += (UChar) 0x221E; | |
623 | pattern += "<are many."; | |
624 | ||
625 | ChoiceFormat *fmt = new ChoiceFormat(pattern, status); | |
626 | failure(status, "new ChoiceFormat"); | |
627 | UnicodeString pat; | |
628 | if (fmt->toPattern(pat) != pattern) { | |
629 | errln( (UnicodeString) "Formatter Pattern : " + pat); | |
630 | errln( (UnicodeString) "Expected Pattern : " + pattern); | |
631 | } | |
632 | FieldPosition bogus(FieldPosition::DONT_CARE); | |
633 | UnicodeString str; | |
634 | ||
635 | // Will this work for -inf? | |
636 | logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status)); | |
637 | failure(status, "fmt->format"); | |
638 | str.remove(); | |
639 | logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); | |
640 | failure(status, "fmt->format"); | |
641 | str.remove(); | |
642 | logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); | |
643 | failure(status, "fmt->format"); | |
644 | str.remove(); | |
645 | logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status)); | |
646 | failure(status, "fmt->format"); | |
647 | str.remove(); | |
648 | logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status)); | |
649 | failure(status, "fmt->format"); | |
650 | str.remove(); | |
651 | logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status)); | |
652 | failure(status, "fmt->format"); | |
653 | str.remove(); | |
654 | logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status)); | |
655 | failure(status, "fmt->format"); | |
656 | str.remove(); | |
657 | logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status)); | |
658 | failure(status, "fmt->format"); | |
659 | str.remove(); | |
660 | logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status)); | |
661 | failure(status, "fmt->format"); | |
662 | str.remove(); | |
663 | logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status)); | |
664 | failure(status, "fmt->format"); | |
665 | str.remove(); | |
666 | logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status)); | |
667 | failure(status, "fmt->format"); | |
668 | ||
669 | delete fmt; | |
670 | } | |
671 | ||
672 | /* @bug 4118592 | |
673 | * MessageFormat.parse fails with ChoiceFormat. | |
674 | */ | |
675 | void MessageFormatRegressionTest::Test4118592() | |
676 | { | |
677 | UErrorCode status = U_ZERO_ERROR; | |
678 | MessageFormat *mf = new MessageFormat("", status); | |
679 | failure(status, "new messageFormat"); | |
680 | UnicodeString pattern("{0,choice,1#YES|2#NO}"); | |
681 | UnicodeString prefix(""); | |
682 | Formattable *objs = 0; | |
683 | ||
684 | for (int i = 0; i < 5; i++) { | |
685 | UnicodeString formatted; | |
686 | formatted = prefix + "YES"; | |
687 | mf->applyPattern(prefix + pattern, status); | |
688 | failure(status, "mf->applyPattern"); | |
689 | prefix += "x"; | |
690 | //Object[] objs = mf.parse(formatted, new ParsePosition(0)); | |
691 | int32_t count = 0; | |
692 | ParsePosition pp(0); | |
693 | objs = mf->parse(formatted, pp, count); | |
694 | UnicodeString pat; | |
695 | logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\""); | |
696 | log(" \"" + formatted + "\" parsed as "); | |
697 | if (objs == NULL) | |
698 | logln(" null"); | |
699 | else { | |
700 | UnicodeString temp; | |
701 | if(objs[0].getType() == Formattable::kString) | |
702 | logln((UnicodeString)" " + objs[0].getString(temp)); | |
703 | else | |
704 | logln((UnicodeString)" " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble())); | |
705 | delete[] objs; | |
706 | ||
707 | } | |
708 | } | |
709 | ||
710 | delete mf; | |
711 | } | |
712 | /* @bug 4118594 | |
713 | * MessageFormat.parse fails for some patterns. | |
714 | */ | |
715 | void MessageFormatRegressionTest::Test4118594() | |
716 | { | |
717 | UErrorCode status = U_ZERO_ERROR; | |
718 | MessageFormat *mf = new MessageFormat("{0}, {0}, {0}", status); | |
719 | failure(status, "new MessageFormat"); | |
720 | UnicodeString forParsing("x, y, z"); | |
721 | //Object[] objs = mf.parse(forParsing, new ParsePosition(0)); | |
722 | int32_t count = 0; | |
723 | ParsePosition pp(0); | |
724 | Formattable *objs = mf->parse(forParsing, pp, count); | |
725 | UnicodeString pat; | |
726 | logln("pattern: \"" + mf->toPattern(pat) + "\""); | |
727 | logln("text for parsing: \"" + forParsing + "\""); | |
728 | UnicodeString str; | |
729 | if (objs[0].getString(str) != "z") | |
730 | errln("argument0: \"" + objs[0].getString(str) + "\""); | |
731 | mf->applyPattern("{0,number,#.##}, {0,number,#.#}", status); | |
732 | failure(status, "mf->applyPattern"); | |
733 | //Object[] oldobjs = {new Double(3.1415)}; | |
734 | Formattable oldobjs [] = {Formattable(3.1415)}; | |
735 | UnicodeString result; | |
736 | FieldPosition pos(FieldPosition::DONT_CARE); | |
737 | result = mf->format( oldobjs, 1, result, pos, status ); | |
738 | failure(status, "mf->format"); | |
739 | pat.remove(); | |
740 | logln("pattern: \"" + mf->toPattern(pat) + "\""); | |
741 | logln("text for parsing: \"" + result + "\""); | |
742 | // result now equals "3.14, 3.1" | |
743 | if (result != "3.14, 3.1") | |
744 | errln("result = " + result); | |
745 | //Object[] newobjs = mf.parse(result, new ParsePosition(0)); | |
746 | int32_t count1 = 0; | |
747 | pp.setIndex(0); | |
748 | Formattable *newobjs = mf->parse(result, pp, count1); | |
749 | // newobjs now equals {new Double(3.1)} | |
750 | if (newobjs[0].getDouble() != 3.1) | |
751 | errln( UnicodeString("newobjs[0] = ") + newobjs[0].getDouble()); | |
752 | ||
753 | delete [] objs; | |
754 | delete [] newobjs; | |
755 | delete mf; | |
756 | } | |
757 | /* @bug 4105380 | |
758 | * When using ChoiceFormat, MessageFormat is not good for I18n. | |
759 | */ | |
760 | void MessageFormatRegressionTest::Test4105380() | |
761 | { | |
762 | UnicodeString patternText1("The disk \"{1}\" contains {0}."); | |
763 | UnicodeString patternText2("There are {0} on the disk \"{1}\""); | |
764 | UErrorCode status = U_ZERO_ERROR; | |
765 | MessageFormat *form1 = new MessageFormat(patternText1, status); | |
766 | failure(status, "new MessageFormat"); | |
767 | MessageFormat *form2 = new MessageFormat(patternText2, status); | |
768 | failure(status, "new MessageFormat"); | |
769 | double filelimits [] = {0,1,2}; | |
770 | UnicodeString filepart [] = { | |
771 | (UnicodeString)"no files", | |
772 | (UnicodeString)"one file", | |
773 | (UnicodeString)"{0,number} files" | |
774 | }; | |
775 | ChoiceFormat *fileform = new ChoiceFormat(filelimits, filepart, 3); | |
776 | form1->setFormat(1, *fileform); | |
777 | form2->setFormat(0, *fileform); | |
778 | //Object[] testArgs = {new Long(12373), "MyDisk"}; | |
779 | Formattable testArgs [] = { | |
780 | Formattable((int32_t)12373), | |
781 | Formattable((UnicodeString)"MyDisk") | |
782 | }; | |
783 | ||
784 | FieldPosition bogus(FieldPosition::DONT_CARE); | |
785 | ||
786 | UnicodeString result; | |
787 | logln(form1->format(testArgs, 2, result, bogus, status)); | |
788 | failure(status, "form1->format"); | |
789 | result.remove(); | |
790 | logln(form2->format(testArgs, 2, result, bogus, status)); | |
791 | failure(status, "form1->format"); | |
792 | ||
793 | delete form1; | |
794 | delete form2; | |
795 | delete fileform; | |
796 | } | |
797 | /* @bug 4120552 | |
798 | * MessageFormat.parse incorrectly sets errorIndex. | |
799 | */ | |
800 | void MessageFormatRegressionTest::Test4120552() | |
801 | { | |
802 | UErrorCode status = U_ZERO_ERROR; | |
803 | MessageFormat *mf = new MessageFormat("pattern", status); | |
804 | failure(status, "new MessageFormat"); | |
805 | UnicodeString texts[] = { | |
806 | (UnicodeString)"pattern", | |
807 | (UnicodeString)"pat", | |
808 | (UnicodeString)"1234" | |
809 | }; | |
810 | UnicodeString pat; | |
811 | logln("pattern: \"" + mf->toPattern(pat) + "\""); | |
812 | for (int i = 0; i < 3; i++) { | |
813 | ParsePosition pp(0); | |
814 | //Object[] objs = mf.parse(texts[i], pp); | |
815 | int32_t count = 0; | |
816 | Formattable *objs = mf->parse(texts[i], pp, count); | |
817 | log(" text for parsing: \"" + texts[i] + "\""); | |
818 | if (objs == NULL) { | |
819 | logln(" (incorrectly formatted string)"); | |
820 | if (pp.getErrorIndex() == -1) | |
821 | errln(UnicodeString("Incorrect error index: ") + pp.getErrorIndex()); | |
822 | } else { | |
823 | logln(" (correctly formatted string)"); | |
824 | delete[] objs; | |
825 | } | |
826 | } | |
827 | delete mf; | |
828 | } | |
829 | ||
830 | /** | |
831 | * @bug 4142938 | |
832 | * MessageFormat handles single quotes in pattern wrong. | |
833 | * This is actually a problem in ChoiceFormat; it doesn't | |
834 | * understand single quotes. | |
835 | */ | |
836 | void MessageFormatRegressionTest::Test4142938() | |
837 | { | |
838 | UnicodeString pat = CharsToUnicodeString("''Vous'' {0,choice,0#n''|1#}avez s\\u00E9lectionn\\u00E9 " | |
839 | "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} " | |
840 | "personnel{0,choice,0#s|1#|2#s}."); | |
841 | UErrorCode status = U_ZERO_ERROR; | |
842 | MessageFormat *mf = new MessageFormat(pat, status); | |
843 | failure(status, "new MessageFormat"); | |
844 | ||
845 | UnicodeString PREFIX [] = { | |
846 | CharsToUnicodeString("'Vous' n'avez s\\u00E9lectionn\\u00E9 aucun clients personnels."), | |
847 | CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 "), | |
848 | CharsToUnicodeString("'Vous' avez s\\u00E9lectionn\\u00E9 ") | |
849 | }; | |
850 | UnicodeString SUFFIX [] = { | |
851 | UnicodeString(), | |
852 | UNICODE_STRING(" client personnel.", 18), | |
853 | UNICODE_STRING(" clients personnels.", 20) | |
854 | }; | |
855 | ||
856 | for (int i=0; i<3; i++) { | |
857 | UnicodeString out; | |
858 | //out = mf->format(new Object[]{new Integer(i)}); | |
859 | Formattable objs [] = { | |
860 | Formattable((int32_t)i) | |
861 | }; | |
862 | FieldPosition pos(FieldPosition::DONT_CARE); | |
863 | out = mf->format(objs, 1, out, pos, status); | |
864 | failure(status, "mf->format"); | |
865 | if (SUFFIX[i] == "") { | |
866 | if (out != PREFIX[i]) | |
867 | errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\""); | |
868 | } | |
869 | else { | |
870 | if (!out.startsWith(PREFIX[i]) || | |
871 | !out.endsWith(SUFFIX[i])) | |
872 | errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" + | |
873 | SUFFIX[i] + "\""); | |
874 | } | |
875 | } | |
876 | ||
877 | delete mf; | |
878 | } | |
879 | ||
880 | /** | |
881 | * @bug 4142938 | |
882 | * Test the applyPattern and toPattern handling of single quotes | |
883 | * by ChoiceFormat. (This is in here because this was a bug reported | |
884 | * against MessageFormat.) The single quote is used to quote the | |
885 | * pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row | |
886 | * is a quote literal. | |
887 | */ | |
888 | void MessageFormatRegressionTest::TestChoicePatternQuote() | |
889 | { | |
890 | UnicodeString DATA [] = { | |
891 | // Pattern 0 value 1 value | |
892 | // {sfb} hacked - changed \u2264 to = (copied from Character Map) | |
893 | (UnicodeString)"0#can''t|1#can", (UnicodeString)"can't", (UnicodeString)"can", | |
894 | (UnicodeString)"0#'pound(#)=''#'''|1#xyz", (UnicodeString)"pound(#)='#'", (UnicodeString)"xyz", | |
895 | (UnicodeString)"0#'1<2 | 1=1'|1#''", (UnicodeString)"1<2 | 1=1", (UnicodeString)"'", | |
896 | }; | |
897 | for (int i=0; i<9; i+=3) { | |
898 | //try { | |
899 | UErrorCode status = U_ZERO_ERROR; | |
900 | ChoiceFormat *cf = new ChoiceFormat(DATA[i], status); | |
901 | failure(status, "new ChoiceFormat"); | |
902 | for (int j=0; j<=1; ++j) { | |
903 | UnicodeString out; | |
904 | FieldPosition pos(FieldPosition::DONT_CARE); | |
905 | out = cf->format((double)j, out, pos); | |
906 | if (out != DATA[i+1+j]) | |
907 | errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " + | |
908 | out + "; want \"" + DATA[i+1+j] + '"'); | |
909 | } | |
910 | UnicodeString pat; | |
911 | pat = cf->toPattern(pat); | |
912 | UnicodeString pat2; | |
913 | ChoiceFormat *cf2 = new ChoiceFormat(pat, status); | |
914 | pat2 = cf2->toPattern(pat2); | |
915 | if (pat != pat2) | |
916 | errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"'); | |
917 | else | |
918 | logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"'); | |
919 | /*} | |
920 | catch (IllegalArgumentException e) { | |
921 | errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e); | |
922 | }*/ | |
923 | ||
924 | delete cf; | |
925 | delete cf2; | |
926 | } | |
927 | } | |
928 | ||
929 | /** | |
930 | * @bug 4112104 | |
931 | * MessageFormat.equals(null) throws a NullPointerException. The JLS states | |
932 | * that it should return false. | |
933 | */ | |
934 | void MessageFormatRegressionTest::Test4112104() | |
935 | { | |
936 | UErrorCode status = U_ZERO_ERROR; | |
937 | MessageFormat *format = new MessageFormat("", status); | |
938 | failure(status, "new MessageFormat"); | |
939 | //try { | |
940 | // This should NOT throw an exception | |
941 | if (format == NULL) { | |
942 | // It also should return false | |
943 | errln("MessageFormat.equals(null) returns false"); | |
944 | } | |
945 | /*} | |
946 | catch (NullPointerException e) { | |
947 | errln("MessageFormat.equals(null) throws " + e); | |
948 | }*/ | |
949 | delete format; | |
950 | } | |
951 | ||
952 | #endif /* #if !UCONFIG_NO_FORMATTING */ |