From 0951252dadeaa1aacf828f04b6e1f75120194d91 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Thu, 11 Sep 2014 19:09:54 +0000 Subject: [PATCH] ICU-11250 Add tests for QuantityFormatter add test for SimplePatternFormatter::getPatternWithNoPlaceholders. X-SVN-Rev: 36468 --- .gitattributes | 1 + icu4c/source/i18n/quantityformatter.h | 3 +- icu4c/source/test/intltest/Makefile.in | 2 +- icu4c/source/test/intltest/intltest.vcxproj | 1 + .../test/intltest/intltest.vcxproj.filters | 3 + icu4c/source/test/intltest/itutil.cpp | 9 ++ .../test/intltest/quantityformattertest.cpp | 146 ++++++++++++++++++ .../intltest/simplepatternformattertest.cpp | 12 ++ 8 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 icu4c/source/test/intltest/quantityformattertest.cpp diff --git a/.gitattributes b/.gitattributes index 06d1de52209..b4f21a18d45 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/icu4c/source/i18n/quantityformatter.h b/icu4c/source/i18n/quantityformatter.h index b71a1760760..18341a2e094 100644 --- a/icu4c/source/i18n/quantityformatter.h +++ b/icu4c/source/i18n/quantityformatter.h @@ -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, diff --git a/icu4c/source/test/intltest/Makefile.in b/icu4c/source/test/intltest/Makefile.in index 1f01d49ca0b..0e2c3fc21c5 100644 --- a/icu4c/source/test/intltest/Makefile.in +++ b/icu4c/source/test/intltest/Makefile.in @@ -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) diff --git a/icu4c/source/test/intltest/intltest.vcxproj b/icu4c/source/test/intltest/intltest.vcxproj index 47783d865c6..16c39a33b99 100644 --- a/icu4c/source/test/intltest/intltest.vcxproj +++ b/icu4c/source/test/intltest/intltest.vcxproj @@ -319,6 +319,7 @@ + false diff --git a/icu4c/source/test/intltest/intltest.vcxproj.filters b/icu4c/source/test/intltest/intltest.vcxproj.filters index 8bb629841b4..1e793a7a2b8 100644 --- a/icu4c/source/test/intltest/intltest.vcxproj.filters +++ b/icu4c/source/test/intltest/intltest.vcxproj.filters @@ -265,6 +265,9 @@ formatting + + formatting + formatting diff --git a/icu4c/source/test/intltest/itutil.cpp b/icu4c/source/test/intltest/itutil.cpp index 75db2526923..6ff3fab7777 100644 --- a/icu4c/source/test/intltest/itutil.cpp +++ b/icu4c/source/test/intltest/itutil.cpp @@ -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 test(createQuantityFormatterTest()); + callTest(*test, par); + } + break; default: name = ""; break; //needed to end loop } } diff --git a/icu4c/source/test/intltest/quantityformattertest.cpp b/icu4c/source/test/intltest/quantityformattertest.cpp new file mode 100644 index 00000000000..54ea8434742 --- /dev/null +++ b/icu4c/source/test/intltest/quantityformattertest.cpp @@ -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 numfmt(NumberFormat::createInstance(status)); + LocalPointer 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(); +} diff --git a/icu4c/source/test/intltest/simplepatternformattertest.cpp b/icu4c/source/test/intltest/simplepatternformattertest.cpp index c408d091ba0..a530c522918 100644 --- a/icu4c/source/test/intltest/simplepatternformattertest.cpp +++ b/icu4c/source/test/intltest/simplepatternformattertest.cpp @@ -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;