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