ICU-11250 Add tests for QuantityFormatter add test for SimplePatternFormatter::getPatternWithNoPlaceholders.

X-SVN-Rev: 36468
This commit is contained in:
Travis Keep 2014-09-11 19:09:54 +00:00
parent 420125c1cc
commit 0951252dad
8 changed files with 174 additions and 3 deletions

1
.gitattributes vendored
View file

@ -144,6 +144,7 @@ icu4c/source/test/cintltst/cintltst.vcxproj.filters -text
icu4c/source/test/depstest/icu-dependencies-mode.el -text
icu4c/source/test/intltest/intltest.vcxproj -text
icu4c/source/test/intltest/intltest.vcxproj.filters -text
icu4c/source/test/intltest/quantityformattertest.cpp -text
icu4c/source/test/iotest/iotest.vcxproj -text
icu4c/source/test/iotest/iotest.vcxproj.filters -text
icu4c/source/test/letest/cletest.vcxproj -text

View file

@ -35,7 +35,6 @@ class FieldPosition;
*
*/
class U_I18N_API QuantityFormatter : public UMemory {
// TODO(Travis Keep): Add test for copy constructor, assignment, and reset.
public:
/**
* Default constructor.
@ -68,7 +67,7 @@ public:
* @param variant "zero", "one", "two", "few", "many", "other"
* @param rawPattern the pattern for the variant e.g "{0} meters"
* @param status any error returned here.
* @return TRUE on success; FALSE otherwise.
* @return TRUE on success; FALSE if status was set to a non zero error.
*/
UBool add(
const char *variant,

View file

@ -57,7 +57,7 @@ uobjtest.o idnaref.o idnaconf.o nptrans.o punyref.o testidn.o testidna.o uts46te
incaltst.o calcasts.o v32test.o uvectest.o textfile.o tokiter.o utxttest.o \
windttst.o winnmtst.o winutil.o csdetest.o tzrulets.o tzoffloc.o tzfmttst.o ssearch.o dtifmtts.o \
tufmtts.o itspoof.o simplethread.o bidiconf.o locnmtst.o dcfmtest.o alphaindextst.o listformattertest.o genderinfotest.o compactdecimalformattest.o regiontst.o \
reldatefmttest.o simplepatternformattertest.o measfmttest.o scientificformathelpertest.o numfmtspectest.o unifiedcachetest.o
reldatefmttest.o simplepatternformattertest.o measfmttest.o scientificformathelpertest.o numfmtspectest.o unifiedcachetest.o quantityformattertest.o
DEPS = $(OBJECTS:.o=.d)

View file

@ -319,6 +319,7 @@
<ClCompile Include="plurfmts.cpp" />
<ClCompile Include="plurults.cpp" />
<ClCompile Include="pptest.cpp" />
<ClCompile Include="quantityformattertest.cpp" />
<ClCompile Include="reldatefmttest.cpp">
<DisableLanguageExtensions>false</DisableLanguageExtensions>
</ClCompile>

View file

@ -265,6 +265,9 @@
<ClCompile Include="pptest.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="quantityformattertest.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="reldatefmttest.cpp">
<Filter>formatting</Filter>
</ClCompile>

View file

@ -35,6 +35,7 @@ extern IntlTest *createUCharsTrieTest();
static IntlTest *createEnumSetTest();
extern IntlTest *createSimplePatternFormatterTest();
extern IntlTest *createUnifiedCacheTest();
extern IntlTest *createQuantityFormatterTest();
#define CASE(id, test) case id: \
name = #test; \
@ -113,6 +114,14 @@ void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &
callTest(*test, par);
}
break;
case 22:
name = "QuantityFormatterTest";
if (exec) {
logln("TestSuite QuantityFormatterTest---"); logln();
LocalPointer<IntlTest> test(createQuantityFormatterTest());
callTest(*test, par);
}
break;
default: name = ""; break; //needed to end loop
}
}

View file

@ -0,0 +1,146 @@
/*
*******************************************************************************
* Copyright (C) 2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* File QUANTITYFORMATTERTEST.CPP
*
********************************************************************************
*/
#include "cstring.h"
#include "intltest.h"
#include "quantityformatter.h"
#include "simplepatternformatter.h"
#include "unicode/numfmt.h"
#include "unicode/plurrule.h"
class QuantityFormatterTest : public IntlTest {
public:
QuantityFormatterTest() {
}
void TestBasic();
void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
private:
};
void QuantityFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestBasic);
TESTCASE_AUTO_END;
}
void QuantityFormatterTest::TestBasic() {
UErrorCode status = U_ZERO_ERROR;
QuantityFormatter fmt;
assertFalse(
"adding bad variant",
fmt.add("a bad variant", "{0} pounds", status));
assertEquals("adding bad variant status", U_ILLEGAL_ARGUMENT_ERROR, status);
status = U_ZERO_ERROR;
assertFalse(
"Adding bad pattern",
fmt.add("other", "{0} {1} too many placeholders", status));
assertEquals("adding bad pattern status", U_ILLEGAL_ARGUMENT_ERROR, status);
status = U_ZERO_ERROR;
assertFalse("isValid with no patterns", fmt.isValid());
assertTrue(
"Adding good pattern with no placeholders",
fmt.add("other", "no placeholder", status));
assertTrue(
"Adding good pattern",
fmt.add("other", "{0} pounds", status));
assertTrue("isValid with other", fmt.isValid());
assertTrue(
"Adding good pattern",
fmt.add("one", "{0} pound", status));
assertEquals(
"getByVariant",
fmt.getByVariant("bad variant")->getPatternWithNoPlaceholders(),
" pounds");
assertEquals(
"getByVariant",
fmt.getByVariant("other")->getPatternWithNoPlaceholders(),
" pounds");
assertEquals(
"getByVariant",
fmt.getByVariant("one")->getPatternWithNoPlaceholders(),
" pound");
assertEquals(
"getByVariant",
fmt.getByVariant("few")->getPatternWithNoPlaceholders(),
" pounds");
// Test copy constructor
{
QuantityFormatter copied(fmt);
assertEquals(
"copied getByVariant",
copied.getByVariant("other")->getPatternWithNoPlaceholders(),
" pounds");
assertEquals(
"copied getByVariant",
copied.getByVariant("one")->getPatternWithNoPlaceholders(),
" pound");
assertEquals(
"copied getByVariant",
copied.getByVariant("few")->getPatternWithNoPlaceholders(),
" pounds");
}
// Test assignment
{
QuantityFormatter assigned;
assigned = fmt;
assertEquals(
"assigned getByVariant",
assigned.getByVariant("other")->getPatternWithNoPlaceholders(),
" pounds");
assertEquals(
"assigned getByVariant",
assigned.getByVariant("one")->getPatternWithNoPlaceholders(),
" pound");
assertEquals(
"assigned getByVariant",
assigned.getByVariant("few")->getPatternWithNoPlaceholders(),
" pounds");
}
// Test format.
{
LocalPointer<NumberFormat> numfmt(NumberFormat::createInstance(status));
LocalPointer<PluralRules> plurrule(
PluralRules::forLocale("en", status));
FieldPosition pos(FieldPosition::DONT_CARE);
UnicodeString appendTo;
assertEquals(
"format singular",
"1 pound",
fmt.format(
1,
*numfmt,
*plurrule,
appendTo,
pos,
status));
appendTo.remove();
assertEquals(
"format plural",
"2 pounds",
fmt.format(
2,
*numfmt,
*plurrule,
appendTo,
pos,
status));
}
fmt.reset();
assertFalse("isValid after reset", fmt.isValid());
assertSuccess("", status);
}
extern IntlTest *createQuantityFormatterTest() {
return new QuantityFormatterTest();
}

View file

@ -19,6 +19,7 @@ public:
void TestNoPlaceholders();
void TestOnePlaceholder();
void TestManyPlaceholders();
void TestGetPatternWithNoPlaceholders();
void TestOptimization();
void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
private:
@ -29,6 +30,7 @@ void SimplePatternFormatterTest::runIndexedTest(int32_t index, UBool exec, const
TESTCASE_AUTO(TestNoPlaceholders);
TESTCASE_AUTO(TestOnePlaceholder);
TESTCASE_AUTO(TestManyPlaceholders);
TESTCASE_AUTO(TestGetPatternWithNoPlaceholders);
TESTCASE_AUTO(TestOptimization);
TESTCASE_AUTO_END;
}
@ -115,6 +117,8 @@ void SimplePatternFormatterTest::TestManyPlaceholders() {
}
}
appendTo.remove();
// Not having enough placeholder params results in error.
fmt.format(
params,
UPRV_LENGTHOF(params) - 1,
@ -125,6 +129,8 @@ void SimplePatternFormatterTest::TestManyPlaceholders() {
if (status != U_ILLEGAL_ARGUMENT_ERROR) {
errln("Expected U_ILLEGAL_ARGUMENT_ERROR");
}
// Ensure we don't write to offsets array beyond its length.
status = U_ZERO_ERROR;
offsets[UPRV_LENGTHOF(offsets) - 1] = 289;
appendTo.remove();
@ -189,6 +195,12 @@ void SimplePatternFormatterTest::TestManyPlaceholders() {
assertSuccess("Status", status);
}
void SimplePatternFormatterTest::TestGetPatternWithNoPlaceholders() {
SimplePatternFormatter fmt("{0} has no {1} placeholders.");
assertEquals(
"", " has no placeholders.", fmt.getPatternWithNoPlaceholders());
}
void SimplePatternFormatterTest::TestOptimization() {
UErrorCode status = U_ZERO_ERROR;
SimplePatternFormatter fmt;