本文整理汇总了C++中ChoiceFormat类的典型用法代码示例。如果您正苦于以下问题:C++ ChoiceFormat类的具体用法?C++ ChoiceFormat怎么用?C++ ChoiceFormat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ChoiceFormat类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ChoiceFormat
/**
* @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;
}
}
示例2: ChoiceFormat
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;
}
示例3: pos
void MessageFormatRegressionTest::Test4052223()
{
ParsePosition pos(0);
if (pos.getErrorIndex() != -1) {
errln("ParsePosition.getErrorIndex initialization failed.");
}
UErrorCode status = U_ZERO_ERROR;
MessageFormat *fmt = new MessageFormat("There are {0} apples growing on the {1} tree.", status);
failure(status, "new MessageFormat");
UnicodeString str("There is one apple growing on the peach tree.");
int32_t count = 0;
fmt->parse(str, pos, count);
logln(UnicodeString("unparsable string , should fail at ") + pos.getErrorIndex());
if (pos.getErrorIndex() == -1)
errln("Bug 4052223 failed : parsing string " + str);
pos.setErrorIndex(4);
if (pos.getErrorIndex() != 4)
errln(UnicodeString("setErrorIndex failed, got ") + pos.getErrorIndex() + " instead of 4");
ChoiceFormat *f = 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");
pos.setIndex(0);
pos.setErrorIndex(-1);
Formattable obj;
f->parse("are negative", obj, pos);
if (pos.getErrorIndex() != -1 && obj.getDouble() == -1.0)
errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos.getErrorIndex());
pos.setIndex(0);
pos.setErrorIndex(-1);
f->parse("are no or fraction ", obj, pos);
if (pos.getErrorIndex() != -1 && obj.getDouble() == 0.0)
errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos.getErrorIndex());
pos.setIndex(0);
pos.setErrorIndex(-1);
f->parse("go postal", obj, pos);
if (pos.getErrorIndex() == -1 && ! uprv_isNaN(obj.getDouble()))
errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos.getErrorIndex());
delete fmt;
delete f;
}
示例4: UnicodeString
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;
}
示例5: ChoiceFormat
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;
}
}
示例6: ChoiceFormat
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;
}
示例7: pattern
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;
}