]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b331163b A |
3 | /* |
4 | ******************************************************************************* | |
2ca993e8 A |
5 | * Copyright (C) 2014-2016, International Business Machines Corporation and |
6 | * others. All Rights Reserved. | |
b331163b A |
7 | ******************************************************************************* |
8 | * | |
9 | * File QUANTITYFORMATTERTEST.CPP | |
10 | * | |
11 | ******************************************************************************** | |
12 | */ | |
13 | #include "cstring.h" | |
14 | #include "intltest.h" | |
15 | #include "quantityformatter.h" | |
2ca993e8 | 16 | #include "unicode/simpleformatter.h" |
b331163b A |
17 | #include "unicode/numfmt.h" |
18 | #include "unicode/plurrule.h" | |
19 | ||
340931cb A |
20 | #define ASSERT_OK(status) UPRV_BLOCK_MACRO_BEGIN { \ |
21 | if(U_FAILURE(status)) { \ | |
22 | errcheckln(status, #status " = %s @ %s:%d", u_errorName(status), __FILE__, __LINE__); \ | |
23 | return; \ | |
24 | } \ | |
25 | } UPRV_BLOCK_MACRO_END | |
b331163b A |
26 | |
27 | class QuantityFormatterTest : public IntlTest { | |
28 | public: | |
29 | QuantityFormatterTest() { | |
30 | } | |
31 | void TestBasic(); | |
32 | void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0); | |
33 | private: | |
34 | }; | |
35 | ||
36 | void QuantityFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) { | |
37 | TESTCASE_AUTO_BEGIN; | |
38 | TESTCASE_AUTO(TestBasic); | |
39 | TESTCASE_AUTO_END; | |
40 | } | |
41 | ||
42 | void QuantityFormatterTest::TestBasic() { | |
43 | UErrorCode status = U_ZERO_ERROR; | |
44 | #if !UCONFIG_NO_FORMATTING | |
45 | QuantityFormatter fmt; | |
46 | assertFalse( | |
47 | "adding bad variant", | |
2ca993e8 | 48 | fmt.addIfAbsent("a bad variant", "{0} pounds", status)); |
f3c0d7a5 | 49 | assertEquals("adding bad variant status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status); |
b331163b A |
50 | status = U_ZERO_ERROR; |
51 | assertFalse( | |
52 | "Adding bad pattern", | |
2ca993e8 | 53 | fmt.addIfAbsent("other", "{0} {1} too many placeholders", status)); |
f3c0d7a5 | 54 | assertEquals("adding bad pattern status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status); |
b331163b A |
55 | status = U_ZERO_ERROR; |
56 | assertFalse("isValid with no patterns", fmt.isValid()); | |
57 | assertTrue( | |
58 | "Adding good pattern with no placeholders", | |
2ca993e8 | 59 | fmt.addIfAbsent("zero", "no placeholder", status)); |
b331163b A |
60 | assertTrue( |
61 | "Adding good pattern", | |
2ca993e8 | 62 | fmt.addIfAbsent("other", "{0} pounds", status)); |
b331163b A |
63 | assertTrue("isValid with other", fmt.isValid()); |
64 | assertTrue( | |
65 | "Adding good pattern", | |
2ca993e8 | 66 | fmt.addIfAbsent("one", "{0} pound", status)); |
b331163b A |
67 | |
68 | assertEquals( | |
69 | "getByVariant", | |
2ca993e8 | 70 | fmt.getByVariant("bad variant")->getTextWithNoArguments(), |
b331163b A |
71 | " pounds"); |
72 | assertEquals( | |
73 | "getByVariant", | |
2ca993e8 | 74 | fmt.getByVariant("other")->getTextWithNoArguments(), |
b331163b A |
75 | " pounds"); |
76 | assertEquals( | |
77 | "getByVariant", | |
2ca993e8 | 78 | fmt.getByVariant("one")->getTextWithNoArguments(), |
b331163b A |
79 | " pound"); |
80 | assertEquals( | |
81 | "getByVariant", | |
2ca993e8 | 82 | fmt.getByVariant("few")->getTextWithNoArguments(), |
b331163b A |
83 | " pounds"); |
84 | ||
85 | // Test copy constructor | |
86 | { | |
87 | QuantityFormatter copied(fmt); | |
88 | assertEquals( | |
89 | "copied getByVariant", | |
2ca993e8 | 90 | copied.getByVariant("other")->getTextWithNoArguments(), |
b331163b A |
91 | " pounds"); |
92 | assertEquals( | |
93 | "copied getByVariant", | |
2ca993e8 | 94 | copied.getByVariant("one")->getTextWithNoArguments(), |
b331163b A |
95 | " pound"); |
96 | assertEquals( | |
97 | "copied getByVariant", | |
2ca993e8 | 98 | copied.getByVariant("few")->getTextWithNoArguments(), |
b331163b A |
99 | " pounds"); |
100 | } | |
101 | ||
102 | // Test assignment | |
103 | { | |
104 | QuantityFormatter assigned; | |
105 | assigned = fmt; | |
106 | assertEquals( | |
107 | "assigned getByVariant", | |
2ca993e8 | 108 | assigned.getByVariant("other")->getTextWithNoArguments(), |
b331163b A |
109 | " pounds"); |
110 | assertEquals( | |
111 | "assigned getByVariant", | |
2ca993e8 | 112 | assigned.getByVariant("one")->getTextWithNoArguments(), |
b331163b A |
113 | " pound"); |
114 | assertEquals( | |
115 | "assigned getByVariant", | |
2ca993e8 | 116 | assigned.getByVariant("few")->getTextWithNoArguments(), |
b331163b A |
117 | " pounds"); |
118 | } | |
119 | ||
120 | // Test format. | |
121 | { | |
122 | LocalPointer<NumberFormat> numfmt( | |
123 | NumberFormat::createInstance(Locale::getEnglish(), status)); | |
124 | LocalPointer<PluralRules> plurrule( | |
125 | PluralRules::forLocale("en", status)); | |
340931cb | 126 | ASSERT_OK(status); |
b331163b A |
127 | FieldPosition pos(FieldPosition::DONT_CARE); |
128 | UnicodeString appendTo; | |
129 | assertEquals( | |
130 | "format singular", | |
f3c0d7a5 | 131 | UnicodeString("1 pound"), |
b331163b | 132 | fmt.format( |
f3c0d7a5 | 133 | 1.0, |
b331163b A |
134 | *numfmt, |
135 | *plurrule, | |
136 | appendTo, | |
137 | pos, | |
138 | status), TRUE); | |
139 | appendTo.remove(); | |
140 | assertEquals( | |
141 | "format plural", | |
f3c0d7a5 | 142 | UnicodeString("2 pounds"), |
b331163b | 143 | fmt.format( |
f3c0d7a5 | 144 | 2.0, |
b331163b A |
145 | *numfmt, |
146 | *plurrule, | |
147 | appendTo, | |
148 | pos, | |
149 | status), TRUE); | |
150 | } | |
151 | fmt.reset(); | |
152 | assertFalse("isValid after reset", fmt.isValid()); | |
153 | #endif | |
154 | assertSuccess("", status); | |
155 | } | |
156 | ||
157 | extern IntlTest *createQuantityFormatterTest() { | |
158 | return new QuantityFormatterTest(); | |
159 | } |