/** * @bug 4142938 * Test the applyPattern and toPattern handling of single quotes * by ChoiceFormat. (This is in here because this was a bug reported * against MessageFormat.) The single quote is used to quote the * pattern characters '|', '#', '<', and '\u2264'. Two quotes in a row * is a quote literal. */ void MessageFormatRegressionTest::TestChoicePatternQuote() { // ICU 4.8 ChoiceFormat (like PluralFormat & SelectFormat) // returns the chosen string unmodified, so that it is usable in a MessageFormat. // We modified the test strings accordingly. // Note: Without further formatting/trimming/etc., it is not possible // to get a single grave as the last character of a non-final choice sub-message // because the single grave before the pipe '|' would start quoted text. // Normally, ChoiceFormat is used inside a MessageFormat, where a double grave // can be used in that case and will be formatted as a single one. UnicodeString DATA [] = { // Pattern 0 value 1 value // {sfb} hacked - changed \u2264 to = (copied from Character Map) "0#can`t|1#can", "can`t", "can", "0#pound(#)=`#``|1#xyz", "pound(#)=`#``", "xyz", "0#1<2 `| 1=1`|1#`", "1<2 `| 1=1`", "`", }; for (int i=0; i<9; i+=3) { //try { UErrorCode status = U_ZERO_ERROR; ChoiceFormat *cf = new ChoiceFormat(DATA[i], status); failure(status, "new ChoiceFormat"); for (int j=0; j<=1; ++j) { UnicodeString out; FieldPosition pos(FieldPosition::DONT_CARE); out = cf->format((double)j, out, pos); if (out != DATA[i+1+j]) errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " + out + "; want \"" + DATA[i+1+j] + "\""); } UnicodeString pat; pat = cf->toPattern(pat); UnicodeString pat2; ChoiceFormat *cf2 = new ChoiceFormat(pat, status); pat2 = cf2->toPattern(pat2); if (pat != pat2) errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + "\""); else logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + "\""); /*} catch (IllegalArgumentException e) { errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e); }*/ delete cf; delete cf2; } }
void TestMessageFormat::testBug1() { const double limit[] = {0.0, 1.0, 2.0}; const UnicodeString formats[] = {"0.0<=Arg<1.0", "1.0<=Arg<2.0", "2.0<-Arg"}; ChoiceFormat *cf = new ChoiceFormat(limit, formats, 3); FieldPosition status(0); UnicodeString toAppendTo; cf->format((int32_t)1, toAppendTo, status); if (toAppendTo != "1.0<=Arg<2.0") { errln("ChoiceFormat cmp in testBug1"); } logln(toAppendTo); delete cf; }
void MessageFormatRegressionTest::Test4106660() { double limits [] = {3, 1, 2}; UnicodeString formats [] = { UnicodeString("Three"), UnicodeString("One"), UnicodeString("Two") }; ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3); double d = 5.0; UnicodeString str; FieldPosition pos(FieldPosition::DONT_CARE); str = cf->format(d, str, pos); if (str != "Two") errln( (UnicodeString) "format(" + d + ") = " + str); delete cf; }
void MessageFormatRegressionTest::TestChoicePatternQuote() { UnicodeString DATA [] = { // Pattern 0 value 1 value // {sfb} hacked - changed \u2264 to = (copied from Character Map) (UnicodeString)"0#can''t|1#can", (UnicodeString)"can't", (UnicodeString)"can", (UnicodeString)"0#'pound(#)=''#'''|1#xyz", (UnicodeString)"pound(#)='#'", (UnicodeString)"xyz", (UnicodeString)"0#'1<2 | 1=1'|1#''", (UnicodeString)"1<2 | 1=1", (UnicodeString)"'", }; for (int i=0; i<9; i+=3) { //try { UErrorCode status = U_ZERO_ERROR; ChoiceFormat *cf = new ChoiceFormat(DATA[i], status); failure(status, "new ChoiceFormat"); for (int j=0; j<=1; ++j) { UnicodeString out; FieldPosition pos(FieldPosition::DONT_CARE); out = cf->format((double)j, out, pos); if (out != DATA[i+1+j]) errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " + out + "; want \"" + DATA[i+1+j] + '"'); } UnicodeString pat; pat = cf->toPattern(pat); UnicodeString pat2; ChoiceFormat *cf2 = new ChoiceFormat(pat, status); pat2 = cf2->toPattern(pat2); if (pat != pat2) errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"'); else logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"'); /*} catch (IllegalArgumentException e) { errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e); }*/ delete cf; delete cf2; } }
void TestChoiceFormat::TestSimpleExample( void ) { double limits[] = {1,2,3,4,5,6,7}; UnicodeString monthNames[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; ChoiceFormat* form = new ChoiceFormat(limits, monthNames, 7); ParsePosition parse_pos; // TODO Fix this ParsePosition stuff... UnicodeString str; UnicodeString res1, res2; UErrorCode status; FieldPosition fpos(FieldPosition::DONT_CARE); Formattable f; int32_t ix; //for (double i = 0.0; i <= 8.0; ++i) { for (ix = 0; ix <= 8; ++ix) { double i = ix; //nos status = U_ZERO_ERROR; fpos = 0; str = ""; res1 = form->format(i, str, fpos, status ); if (!chkstatus( status, "*** test_simple_example format" )) { delete form; return; } //form->parse(res1, f, parse_pos); res2 = " ??? "; it_logln(UnicodeString("") + ix + UnicodeString(" -> ") + res1 + UnicodeString(" -> ") + res2); } //Testing ==operator const double filelimits[] = {0,1,2}; const UnicodeString filepart[] = {"are no files","is one file","are {2} files"}; ChoiceFormat* formnew=new ChoiceFormat(filelimits, filepart, 3); ChoiceFormat* formequal=new ChoiceFormat(limits, monthNames, 7); if(*formnew == *form){ errln("ERROR: ==operator failed\n"); } if(!(*form == *formequal)){ errln("ERROR: ==operator failed\n"); } delete formequal; delete formnew; //Testing getLimits() int32_t count=0; const double *gotLimits=form->getLimits(count); #if 1 // ICU 4.8 deprecates and disables the ChoiceFormat getters. if(count != 0 || gotLimits != NULL) { errln("getLimits() returns something, should be disabled"); } const UnicodeString *gotFormats=form->getFormats(count); if(count != 0 || gotFormats != NULL) { errln("getFormats() returns something, should be disabled"); } const UBool *gotClosures=form->getClosures(count); if(count != 0 || gotClosures != NULL) { errln("getClosures() returns something, should be disabled"); } #else if(count != 7){ errln("getLimits didn't update the count correctly\n"); } for(ix=0; ix<count; ix++){ if(gotLimits[ix] != limits[ix]){ errln((UnicodeString)"getLimits didn't get the limits correctly. Expected " + limits[ix] + " Got " + gotLimits[ix]); } } //Testing getFormats() count=0; const UnicodeString *gotFormats=form->getFormats(count); if(count != 7){ errln("getFormats didn't update the count correctly\n"); } for(ix=0; ix<count; ix++){ if(gotFormats[ix] != monthNames[ix]){ errln((UnicodeString)"getFormats didn't get the Formats correctly. Expected " + monthNames[ix] + " Got " + gotFormats[ix]); } } #endif delete form; }
void MessageFormatRegressionTest::Test4094906() { UErrorCode status = U_ZERO_ERROR; UnicodeString pattern("-"); pattern += (UChar) 0x221E; pattern += "<are negative|0<are no or fraction|1#is one|1<is 1+|"; pattern += (UChar) 0x221E; pattern += "<are many."; ChoiceFormat *fmt = new ChoiceFormat(pattern, status); failure(status, "new ChoiceFormat"); UnicodeString pat; if (fmt->toPattern(pat) != pattern) { errln( (UnicodeString) "Formatter Pattern : " + pat); errln( (UnicodeString) "Expected Pattern : " + pattern); } FieldPosition bogus(FieldPosition::DONT_CARE); UnicodeString str; // Will this work for -inf? logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status)); failure(status, "fmt->format"); delete fmt; }
void MessageFormatRegressionTest::Test4106661() { UErrorCode status = U_ZERO_ERROR; ChoiceFormat *fmt = new ChoiceFormat( "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status); failure(status, "new ChoiceFormat"); UnicodeString pat; logln("Formatter Pattern : " + fmt->toPattern(pat)); FieldPosition bogus(FieldPosition::DONT_CARE); UnicodeString str; // Will this work for -inf? logln("Format with -INF : " + fmt->format(Formattable(-uprv_getInfinity()), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with NaN : " + fmt->format(Formattable(uprv_getNaN()), str, bogus, status)); failure(status, "fmt->format"); str.remove(); logln("Format with +INF : " + fmt->format(Formattable(uprv_getInfinity()), str, bogus, status)); failure(status, "fmt->format"); delete fmt; }