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