OLD | NEW |
| (Empty) |
1 | |
2 /******************************************************************** | |
3 * COPYRIGHT: | |
4 * Copyright (c) 1997-2011, International Business Machines Corporation and | |
5 * others. All Rights Reserved. | |
6 ********************************************************************/ | |
7 | |
8 #include "unicode/utypes.h" | |
9 | |
10 #if !UCONFIG_NO_FORMATTING | |
11 | |
12 #include "intltest.h" | |
13 #include "tchcfmt.h" | |
14 #include "cmemory.h" | |
15 #include "unicode/msgfmt.h" | |
16 #include "unicode/choicfmt.h" | |
17 | |
18 #include <float.h> | |
19 | |
20 // tests have obvious memory leaks! | |
21 | |
22 void TestChoiceFormat::runIndexedTest(int32_t index, UBool exec, | |
23 const char* &name, char* /*par*/) { | |
24 switch (index) { | |
25 TESTCASE(0,TestSimpleExample); | |
26 TESTCASE(1,TestComplexExample); | |
27 TESTCASE(2,TestClosures); | |
28 TESTCASE(3,TestPatterns); | |
29 TESTCASE(4,TestChoiceFormatToPatternOverflow); | |
30 default: name = ""; break; | |
31 } | |
32 } | |
33 | |
34 static UBool chkstatus( UErrorCode &status, const char* msg = NULL ) | |
35 { | |
36 UBool ok = U_SUCCESS(status); | |
37 if (!ok) it_errln( msg ); | |
38 return ok; | |
39 } | |
40 | |
41 void | |
42 TestChoiceFormat::TestSimpleExample( void ) | |
43 { | |
44 double limits[] = {1,2,3,4,5,6,7}; | |
45 UnicodeString monthNames[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; | |
46 ChoiceFormat* form = new ChoiceFormat(limits, monthNames, 7); | |
47 ParsePosition parse_pos; | |
48 // TODO Fix this ParsePosition stuff... | |
49 UnicodeString str; | |
50 UnicodeString res1, res2; | |
51 UErrorCode status; | |
52 FieldPosition fpos(0); | |
53 Formattable f; | |
54 int32_t ix; | |
55 //for (double i = 0.0; i <= 8.0; ++i) { | |
56 for (ix = 0; ix <= 8; ++ix) { | |
57 double i = ix; //nos | |
58 status = U_ZERO_ERROR; | |
59 fpos = 0; | |
60 str = ""; | |
61 res1 = form->format(i, str, fpos, status ); | |
62 if (!chkstatus( status, "*** test_simple_example format" )) { | |
63 delete form; | |
64 return; | |
65 } | |
66 //form->parse(res1, f, parse_pos); | |
67 res2 = " ??? "; | |
68 it_logln(UnicodeString("") + ix + UnicodeString(" -> ") + res1 + Unicode
String(" -> ") + res2); | |
69 } | |
70 //Testing ==operator | |
71 const double filelimits[] = {0,1,2}; | |
72 const UnicodeString filepart[] = {"are no files","is one file","are {2} file
s"}; | |
73 ChoiceFormat* formnew=new ChoiceFormat(filelimits, filepart, 3); | |
74 ChoiceFormat* formequal=new ChoiceFormat(limits, monthNames, 7); | |
75 if(*formnew == *form){ | |
76 errln("ERROR: ==operator failed\n"); | |
77 } | |
78 if(!(*form == *formequal)){ | |
79 errln("ERROR: ==operator failed\n"); | |
80 } | |
81 delete formequal; | |
82 delete formnew; | |
83 | |
84 //Testing getLimits() | |
85 int32_t count=0; | |
86 const double *gotLimits=form->getLimits(count); | |
87 #if 1 // ICU 4.8 deprecates and disables the ChoiceFormat getters. | |
88 if(count != 0 || gotLimits != NULL) { | |
89 errln("getLimits() returns something, should be disabled"); | |
90 } | |
91 const UnicodeString *gotFormats=form->getFormats(count); | |
92 if(count != 0 || gotFormats != NULL) { | |
93 errln("getFormats() returns something, should be disabled"); | |
94 } | |
95 const UBool *gotClosures=form->getClosures(count); | |
96 if(count != 0 || gotClosures != NULL) { | |
97 errln("getClosures() returns something, should be disabled"); | |
98 } | |
99 #else | |
100 if(count != 7){ | |
101 errln("getLimits didn't update the count correctly\n"); | |
102 } | |
103 for(ix=0; ix<count; ix++){ | |
104 if(gotLimits[ix] != limits[ix]){ | |
105 errln((UnicodeString)"getLimits didn't get the limits correctly. Ex
pected " + limits[ix] + " Got " + gotLimits[ix]); | |
106 } | |
107 } | |
108 //Testing getFormats() | |
109 count=0; | |
110 const UnicodeString *gotFormats=form->getFormats(count); | |
111 if(count != 7){ | |
112 errln("getFormats didn't update the count correctly\n"); | |
113 } | |
114 for(ix=0; ix<count; ix++){ | |
115 if(gotFormats[ix] != monthNames[ix]){ | |
116 errln((UnicodeString)"getFormats didn't get the Formats correctly.
Expected " + monthNames[ix] + " Got " + gotFormats[ix]); | |
117 } | |
118 } | |
119 #endif | |
120 | |
121 delete form; | |
122 } | |
123 | |
124 void | |
125 TestChoiceFormat::TestComplexExample( void ) | |
126 { | |
127 UErrorCode status = U_ZERO_ERROR; | |
128 const double filelimits[] = {-1, 0,1,2}; | |
129 const UnicodeString filepart[] = {"are corrupted files", "are no files","is
one file","are {2} files"}; | |
130 | |
131 ChoiceFormat* fileform = new ChoiceFormat( filelimits, filepart, 4); | |
132 | |
133 if (!fileform) { | |
134 it_errln("*** test_complex_example fileform"); | |
135 return; | |
136 } | |
137 | |
138 Format* filenumform = NumberFormat::createInstance( status ); | |
139 if (!filenumform) { | |
140 dataerrln((UnicodeString)"*** test_complex_example filenumform - " + u_
errorName(status)); | |
141 delete fileform; | |
142 return; | |
143 } | |
144 if (!chkstatus( status, "*** test_simple_example filenumform" )) { | |
145 delete fileform; | |
146 delete filenumform; | |
147 return; | |
148 } | |
149 | |
150 //const Format* testFormats[] = { fileform, NULL, filenumform }; | |
151 //pattform->setFormats( testFormats, 3 ); | |
152 | |
153 MessageFormat* pattform = new MessageFormat("There {0} on {1}", status ); | |
154 if (!pattform) { | |
155 it_errln("*** test_complex_example pattform"); | |
156 delete fileform; | |
157 delete filenumform; | |
158 return; | |
159 } | |
160 if (!chkstatus( status, "*** test_complex_example pattform" )) { | |
161 delete fileform; | |
162 delete filenumform; | |
163 delete pattform; | |
164 return; | |
165 } | |
166 | |
167 pattform->setFormat( 0, *fileform ); | |
168 pattform->setFormat( 2, *filenumform ); | |
169 | |
170 | |
171 Formattable testArgs[] = {(int32_t)0, "Disk_A", (int32_t)0}; | |
172 UnicodeString str; | |
173 UnicodeString res1, res2; | |
174 pattform->toPattern( res1 ); | |
175 it_logln("MessageFormat toPattern: " + res1); | |
176 fileform->toPattern( res1 ); | |
177 it_logln("ChoiceFormat toPattern: " + res1); | |
178 if (res1 == "-1#are corrupted files|0#are no files|1#is one file|2#are {2} f
iles") { | |
179 it_logln("toPattern tested!"); | |
180 }else{ | |
181 it_errln("*** ChoiceFormat to Pattern result!"); | |
182 } | |
183 | |
184 FieldPosition fpos(0); | |
185 | |
186 UnicodeString checkstr[] = { | |
187 "There are corrupted files on Disk_A", | |
188 "There are no files on Disk_A", | |
189 "There is one file on Disk_A", | |
190 "There are 2 files on Disk_A", | |
191 "There are 3 files on Disk_A" | |
192 }; | |
193 | |
194 // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a b
ad bail out here! | |
195 | |
196 if (U_FAILURE(status)) { | |
197 delete fileform; | |
198 delete filenumform; | |
199 delete pattform; | |
200 return; | |
201 } | |
202 | |
203 | |
204 int32_t i; | |
205 int32_t start = -1; | |
206 for (i = start; i < 4; ++i) { | |
207 str = ""; | |
208 status = U_ZERO_ERROR; | |
209 testArgs[0] = Formattable((int32_t)i); | |
210 testArgs[2] = testArgs[0]; | |
211 res2 = pattform->format(testArgs, 3, str, fpos, status ); | |
212 if (!chkstatus( status, "*** test_complex_example format" )) { | |
213 delete fileform; | |
214 delete filenumform; | |
215 delete pattform; | |
216 return; | |
217 } | |
218 it_logln(i + UnicodeString(" -> ") + res2); | |
219 if (res2 != checkstr[i - start]) { | |
220 it_errln("*** test_complex_example res string"); | |
221 it_errln(UnicodeString("*** ") + i + UnicodeString(" -> '") + res2 +
UnicodeString("' unlike '") + checkstr[i] + UnicodeString("' ! ")); | |
222 } | |
223 } | |
224 it_logln(); | |
225 | |
226 it_logln("------ additional testing in complex test ------"); | |
227 it_logln(); | |
228 // | |
229 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters. | |
230 int32_t retCount; | |
231 const double* retLimits = fileform->getLimits( retCount ); | |
232 if ((retCount == 4) && (retLimits) | |
233 && (retLimits[0] == -1.0) | |
234 && (retLimits[1] == 0.0) | |
235 && (retLimits[2] == 1.0) | |
236 && (retLimits[3] == 2.0)) { | |
237 it_logln("getLimits tested!"); | |
238 }else{ | |
239 it_errln("*** getLimits unexpected result!"); | |
240 } | |
241 | |
242 const UnicodeString* retFormats = fileform->getFormats( retCount ); | |
243 if ((retCount == 4) && (retFormats) | |
244 && (retFormats[0] == "are corrupted files") | |
245 && (retFormats[1] == "are no files") | |
246 && (retFormats[2] == "is one file") | |
247 && (retFormats[3] == "are {2} files")) { | |
248 it_logln("getFormats tested!"); | |
249 }else{ | |
250 it_errln("*** getFormats unexpected result!"); | |
251 } | |
252 #endif | |
253 | |
254 UnicodeString checkstr2[] = { | |
255 "There is no folder on Disk_A", | |
256 "There is one folder on Disk_A", | |
257 "There are many folders on Disk_A", | |
258 "There are many folders on Disk_A" | |
259 }; | |
260 | |
261 fileform->applyPattern("0#is no folder|1#is one folder|2#are many folders",
status ); | |
262 if (status == U_ZERO_ERROR) | |
263 it_logln("status applyPattern OK!"); | |
264 if (!chkstatus( status, "*** test_complex_example pattform" )) { | |
265 delete fileform; | |
266 delete filenumform; | |
267 delete pattform; | |
268 return; | |
269 } | |
270 pattform->setFormat( 0, *fileform ); | |
271 fpos = 0; | |
272 for (i = 0; i < 4; ++i) { | |
273 str = ""; | |
274 status = U_ZERO_ERROR; | |
275 testArgs[0] = Formattable((int32_t)i); | |
276 testArgs[2] = testArgs[0]; | |
277 res2 = pattform->format(testArgs, 3, str, fpos, status ); | |
278 if (!chkstatus( status, "*** test_complex_example format 2" )) { | |
279 delete fileform; | |
280 delete filenumform; | |
281 delete pattform; | |
282 return; | |
283 } | |
284 it_logln(UnicodeString() + i + UnicodeString(" -> ") + res2); | |
285 if (res2 != checkstr2[i]) { | |
286 it_errln("*** test_complex_example res string"); | |
287 it_errln(UnicodeString("*** ") + i + UnicodeString(" -> '") + res2 +
UnicodeString("' unlike '") + checkstr2[i] + UnicodeString("' ! ")); | |
288 } | |
289 } | |
290 | |
291 const double limits_A[] = {1,2,3,4,5,6,7}; | |
292 const UnicodeString monthNames_A[] = {"Sun","Mon","Tue","Wed","Thur","Fri","
Sat"}; | |
293 ChoiceFormat* form_A = new ChoiceFormat(limits_A, monthNames_A, 7); | |
294 ChoiceFormat* form_A2 = new ChoiceFormat(limits_A, monthNames_A, 7); | |
295 const double limits_B[] = {1,2,3,4,5,6,7}; | |
296 const UnicodeString monthNames_B[] = {"Sun","Mon","Tue","Wed","Thur","Fri","
Sat_BBB"}; | |
297 ChoiceFormat* form_B = new ChoiceFormat(limits_B, monthNames_B, 7); | |
298 if (!form_A || !form_B || !form_A2) { | |
299 it_errln("*** test-choiceFormat not allocatable!"); | |
300 }else{ | |
301 if (*form_A == *form_A2) { | |
302 it_logln("operator== tested."); | |
303 }else{ | |
304 it_errln("*** operator=="); | |
305 } | |
306 | |
307 if (*form_A != *form_B) { | |
308 it_logln("operator!= tested."); | |
309 }else{ | |
310 it_errln("*** operator!="); | |
311 } | |
312 | |
313 ChoiceFormat* form_A3 = (ChoiceFormat*) form_A->clone(); | |
314 if (!form_A3) { | |
315 it_errln("*** ChoiceFormat->clone is nil."); | |
316 }else{ | |
317 if ((*form_A3 == *form_A) && (*form_A3 != *form_B)) { | |
318 it_logln("method clone tested."); | |
319 }else{ | |
320 it_errln("*** ChoiceFormat clone or operator==, or operator!= .
"); | |
321 } | |
322 } | |
323 | |
324 ChoiceFormat form_Assigned( *form_A ); | |
325 UBool ok = (form_Assigned == *form_A) && (form_Assigned != *form_B); | |
326 form_Assigned = *form_B; | |
327 ok = ok && (form_Assigned != *form_A) && (form_Assigned == *form_B); | |
328 if (ok) { | |
329 it_logln("copy constructor and operator= tested."); | |
330 }else{ | |
331 it_errln("*** copy constructor or operator= or operator == or opera
tor != ."); | |
332 } | |
333 delete form_A3; | |
334 } | |
335 | |
336 | |
337 delete form_A; delete form_A2; delete form_B; | |
338 | |
339 const char* testPattern = "0#none|1#one|2#many"; | |
340 ChoiceFormat form_pat( testPattern, status ); | |
341 if (!chkstatus( status, "*** ChoiceFormat contructor( newPattern, status)"
)) { | |
342 delete fileform; | |
343 delete filenumform; | |
344 delete pattform; | |
345 return; | |
346 } | |
347 | |
348 form_pat.toPattern( res1 ); | |
349 if (res1 == "0#none|1#one|2#many") { | |
350 it_logln("ChoiceFormat contructor( newPattern, status) tested"); | |
351 }else{ | |
352 it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern
result!"); | |
353 } | |
354 | |
355 double d_a2[] = { 3.0, 4.0 }; | |
356 UnicodeString s_a2[] = { "third", "forth" }; | |
357 | |
358 form_pat.setChoices( d_a2, s_a2, 2 ); | |
359 form_pat.toPattern( res1 ); | |
360 it_logln(UnicodeString("ChoiceFormat adoptChoices toPattern: ") + res1); | |
361 if (res1 == "3#third|4#forth") { | |
362 it_logln("ChoiceFormat adoptChoices tested"); | |
363 }else{ | |
364 it_errln("*** ChoiceFormat adoptChoices result!"); | |
365 } | |
366 | |
367 str = ""; | |
368 fpos = 0; | |
369 status = U_ZERO_ERROR; | |
370 double arg_double = 3.0; | |
371 res1 = form_pat.format( arg_double, str, fpos ); | |
372 it_logln(UnicodeString("ChoiceFormat format:") + res1); | |
373 if (res1 != "third") it_errln("*** ChoiceFormat format (double, ...) result
!"); | |
374 | |
375 str = ""; | |
376 fpos = 0; | |
377 status = U_ZERO_ERROR; | |
378 int64_t arg_64 = 3; | |
379 res1 = form_pat.format( arg_64, str, fpos ); | |
380 it_logln(UnicodeString("ChoiceFormat format:") + res1); | |
381 if (res1 != "third") it_errln("*** ChoiceFormat format (int64_t, ...) resul
t!"); | |
382 | |
383 str = ""; | |
384 fpos = 0; | |
385 status = U_ZERO_ERROR; | |
386 int32_t arg_long = 3; | |
387 res1 = form_pat.format( arg_long, str, fpos ); | |
388 it_logln(UnicodeString("ChoiceFormat format:") + res1); | |
389 if (res1 != "third") it_errln("*** ChoiceFormat format (int32_t, ...) resul
t!"); | |
390 | |
391 Formattable ft( (int32_t)3 ); | |
392 str = ""; | |
393 fpos = 0; | |
394 status = U_ZERO_ERROR; | |
395 res1 = form_pat.format( ft, str, fpos, status ); | |
396 if (!chkstatus( status, "*** test_complex_example format (int32_t, ...)" ))
{ | |
397 delete fileform; | |
398 delete filenumform; | |
399 delete pattform; | |
400 return; | |
401 } | |
402 it_logln(UnicodeString("ChoiceFormat format:") + res1); | |
403 if (res1 != "third") it_errln("*** ChoiceFormat format (Formattable, ...) r
esult!"); | |
404 | |
405 Formattable fta[] = { (int32_t)3 }; | |
406 str = ""; | |
407 fpos = 0; | |
408 status = U_ZERO_ERROR; | |
409 res1 = form_pat.format( fta, 1, str, fpos, status ); | |
410 if (!chkstatus( status, "*** test_complex_example format (int32_t, ...)" ))
{ | |
411 delete fileform; | |
412 delete filenumform; | |
413 delete pattform; | |
414 return; | |
415 } | |
416 it_logln(UnicodeString("ChoiceFormat format:") + res1); | |
417 if (res1 != "third") it_errln("*** ChoiceFormat format (Formattable[], cnt,
...) result!"); | |
418 | |
419 ParsePosition parse_pos = 0; | |
420 Formattable result; | |
421 UnicodeString parsetext("third"); | |
422 form_pat.parse( parsetext, result, parse_pos ); | |
423 double rd = (result.getType() == Formattable::kLong) ? result.getLong() : re
sult.getDouble(); | |
424 if (rd == 3.0) { | |
425 it_logln("parse( ..., ParsePos ) tested."); | |
426 }else{ | |
427 it_errln("*** ChoiceFormat parse( ..., ParsePos )!"); | |
428 } | |
429 | |
430 form_pat.parse( parsetext, result, status ); | |
431 rd = (result.getType() == Formattable::kLong) ? result.getLong() : result.ge
tDouble(); | |
432 if (rd == 3.0) { | |
433 it_logln("parse( ..., UErrorCode ) tested."); | |
434 }else{ | |
435 it_errln("*** ChoiceFormat parse( ..., UErrorCode )!"); | |
436 } | |
437 | |
438 /* | |
439 UClassID classID = ChoiceFormat::getStaticClassID(); | |
440 if (classID == form_pat.getDynamicClassID()) { | |
441 it_out << "getStaticClassID and getDynamicClassID tested." << endl; | |
442 }else{ | |
443 it_errln("*** getStaticClassID and getDynamicClassID!"); | |
444 } | |
445 */ | |
446 | |
447 it_logln(); | |
448 | |
449 delete fileform; | |
450 delete filenumform; | |
451 delete pattform; | |
452 } | |
453 | |
454 | |
455 /** | |
456 * Test new closure API | |
457 */ | |
458 void TestChoiceFormat::TestClosures(void) { | |
459 // Construct open, half-open, half-open (the other way), and closed | |
460 // intervals. Do this both using arrays and using a pattern. | |
461 | |
462 // 'fmt1' is created using arrays | |
463 UBool T = TRUE, F = FALSE; | |
464 // 0: ,1) | |
465 // 1: [1,2] | |
466 // 2: (2,3] | |
467 // 3: (3,4) | |
468 // 4: [4,5) | |
469 // 5: [5, | |
470 double limits[] = { 0, 1, 2, 3, 4, 5 }; | |
471 UBool closures[] = { F, F, T, T, F, F }; | |
472 UnicodeString fmts[] = { | |
473 ",1)", "[1,2]", "(2,3]", "(3,4)", "[4,5)", "[5," | |
474 }; | |
475 ChoiceFormat fmt1(limits, closures, fmts, 6); | |
476 | |
477 // 'fmt2' is created using a pattern; it should be equivalent | |
478 UErrorCode status = U_ZERO_ERROR; | |
479 const char* PAT = "0#,1)|1#[1,2]|2<(2,3]|3<(3,4)|4#[4,5)|5#[5,"; | |
480 ChoiceFormat fmt2(PAT, status); | |
481 if (U_FAILURE(status)) { | |
482 errln("FAIL: ChoiceFormat constructor failed"); | |
483 return; | |
484 } | |
485 | |
486 // Check the patterns | |
487 UnicodeString str; | |
488 fmt1.toPattern(str); | |
489 if (str == PAT) { | |
490 logln("Ok: " + str); | |
491 } else { | |
492 errln("FAIL: " + str + ", expected " + PAT); | |
493 } | |
494 str.truncate(0); | |
495 | |
496 // Check equality | |
497 if (fmt1 != fmt2) { | |
498 errln("FAIL: fmt1 != fmt2"); | |
499 } | |
500 | |
501 #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters. | |
502 int32_t i; | |
503 int32_t count2 = 0; | |
504 const double *limits2 = fmt2.getLimits(count2); | |
505 const UBool *closures2 = fmt2.getClosures(count2); | |
506 | |
507 if((count2 != 6) || !limits2 || !closures2) { | |
508 errln("FAIL: couldn't get limits or closures"); | |
509 } else { | |
510 for(i=0;i<count2;i++) { | |
511 logln("#%d/%d: limit %g closed %s\n", | |
512 i, count2, | |
513 limits2[i], | |
514 closures2[i] ?"T":"F"); | |
515 if(limits2[i] != limits[i]) { | |
516 errln("FAIL: limit #%d = %g, should be %g\n", i, limits2[i], limits[
i]); | |
517 } | |
518 if((closures2[i]!=0) != (closures[i]!=0)) { | |
519 errln("FAIL: closure #%d = %s, should be %s\n", i, closures2[i]?"T":
"F", closures[i]?"T":"F"); | |
520 } | |
521 } | |
522 } | |
523 #endif | |
524 | |
525 // Now test both format objects | |
526 UnicodeString exp[] = { | |
527 /*-0.5 => */ ",1)", | |
528 /* 0.0 => */ ",1)", | |
529 /* 0.5 => */ ",1)", | |
530 /* 1.0 => */ "[1,2]", | |
531 /* 1.5 => */ "[1,2]", | |
532 /* 2.0 => */ "[1,2]", | |
533 /* 2.5 => */ "(2,3]", | |
534 /* 3.0 => */ "(2,3]", | |
535 /* 3.5 => */ "(3,4)", | |
536 /* 4.0 => */ "[4,5)", | |
537 /* 4.5 => */ "[4,5)", | |
538 /* 5.0 => */ "[5,", | |
539 /* 5.5 => */ "[5," | |
540 }; | |
541 | |
542 // Each format object should behave exactly the same | |
543 ChoiceFormat* FMT[] = { &fmt1, &fmt2 }; | |
544 for (int32_t pass=0; pass<2; ++pass) { | |
545 int32_t j=0; | |
546 for (int32_t ix=-5; ix<=55; ix+=5) { | |
547 double x = ix / 10.0; // -0.5 to 5.5 step +0.5 | |
548 FMT[pass]->format(x, str); | |
549 if (str == exp[j]) { | |
550 logln((UnicodeString)"Ok: " + x + " => " + str); | |
551 } else { | |
552 errln((UnicodeString)"FAIL: " + x + " => " + str + | |
553 ", expected " + exp[j]); | |
554 } | |
555 str.truncate(0); | |
556 ++j; | |
557 } | |
558 } | |
559 } | |
560 | |
561 /** | |
562 * Helper for TestPatterns() | |
563 */ | |
564 void TestChoiceFormat::_testPattern(const char* pattern, | |
565 UBool isValid, | |
566 double v1, const char* str1, | |
567 double v2, const char* str2, | |
568 double v3, const char* str3) { | |
569 UErrorCode ec = U_ZERO_ERROR; | |
570 ChoiceFormat fmt(pattern, ec); | |
571 if (!isValid) { | |
572 if (U_FAILURE(ec)) { | |
573 logln((UnicodeString)"Ok: " + pattern + " failed"); | |
574 } else { | |
575 logln((UnicodeString)"FAIL: " + pattern + " accepted"); | |
576 } | |
577 return; | |
578 } | |
579 if (U_FAILURE(ec)) { | |
580 errln((UnicodeString)"FAIL: ChoiceFormat(" + pattern + ") failed"); | |
581 return; | |
582 } else { | |
583 logln((UnicodeString)"Ok: Pattern: " + pattern); | |
584 } | |
585 UnicodeString out; | |
586 logln((UnicodeString)" toPattern: " + fmt.toPattern(out)); | |
587 | |
588 double v[] = {v1, v2, v3}; | |
589 const char* str[] = {str1, str2, str3}; | |
590 for (int32_t i=0; i<3; ++i) { | |
591 out.truncate(0); | |
592 fmt.format(v[i], out); | |
593 if (out == str[i]) { | |
594 logln((UnicodeString)"Ok: " + v[i] + " => " + out); | |
595 } else { | |
596 errln((UnicodeString)"FAIL: " + v[i] + " => " + out + | |
597 ", expected " + str[i]); | |
598 } | |
599 } | |
600 } | |
601 | |
602 /** | |
603 * Test applyPattern | |
604 */ | |
605 void TestChoiceFormat::TestPatterns(void) { | |
606 // Try a pattern that isolates a single value. Create | |
607 // three ranges: [-Inf,1.0) [1.0,1.0] (1.0,+Inf] | |
608 _testPattern("0.0#a|1.0#b|1.0<c", TRUE, | |
609 1.0 - 1e-9, "a", | |
610 1.0, "b", | |
611 1.0 + 1e-9, "c"); | |
612 | |
613 #if 0 // ICU 4.8 only checks the pattern syntax, not whether the ranges make se
nse. | |
614 // Try an invalid pattern that isolates a single value. | |
615 // [-Inf,1.0) [1.0,1.0) [1.0,+Inf] | |
616 _testPattern("0.0#a|1.0#b|1.0#c", FALSE, | |
617 0, 0, 0, 0, 0, 0); | |
618 | |
619 // Another | |
620 // [-Inf,1.0] (1.0,1.0) [1.0,+Inf] | |
621 _testPattern("0.0#a|1.0<b|1.0#c", FALSE, | |
622 0, 0, 0, 0, 0, 0); | |
623 // Another | |
624 // [-Inf,1.0] (1.0,1.0] (1.0,+Inf] | |
625 _testPattern("0.0#a|1.0<b|1.0<c", FALSE, | |
626 0, 0, 0, 0, 0, 0); | |
627 | |
628 // Try a grossly invalid pattern. | |
629 // [-Inf,2.0) [2.0,1.0) [1.0,+Inf] | |
630 _testPattern("0.0#a|2.0#b|1.0#c", FALSE, | |
631 0, 0, 0, 0, 0, 0); | |
632 #endif | |
633 } | |
634 | |
635 void TestChoiceFormat::TestChoiceFormatToPatternOverflow() | |
636 { | |
637 static const double limits[] = {0.1e-78, 1e13, 0.1e78}; | |
638 UnicodeString monthNames[] = { "one", "two", "three" }; | |
639 ChoiceFormat fmt(limits, monthNames, sizeof(limits)/sizeof(limits[0])); | |
640 UnicodeString patStr, expectedPattern1("1e-79#one|10000000000000#two|1e+77#t
hree"), | |
641 expectedPattern2("1e-079#one|10000000000000#two|1e+077#three"); | |
642 fmt.toPattern(patStr); | |
643 if (patStr != expectedPattern1 && patStr != expectedPattern2) { | |
644 errln("ChoiceFormat returned \"" + patStr + "\" instead of \"" + expecte
dPattern1 + " or " + expectedPattern2 + "\""); | |
645 } | |
646 } | |
647 | |
648 #endif /* #if !UCONFIG_NO_FORMATTING */ | |
OLD | NEW |