]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/quantityformattertest.cpp
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / quantityformattertest.cpp
CommitLineData
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
20
21class QuantityFormatterTest : public IntlTest {
22public:
23 QuantityFormatterTest() {
24 }
25 void TestBasic();
26 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
27private:
28};
29
30void QuantityFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
31 TESTCASE_AUTO_BEGIN;
32 TESTCASE_AUTO(TestBasic);
33 TESTCASE_AUTO_END;
34}
35
36void QuantityFormatterTest::TestBasic() {
37 UErrorCode status = U_ZERO_ERROR;
38#if !UCONFIG_NO_FORMATTING
39 QuantityFormatter fmt;
40 assertFalse(
41 "adding bad variant",
2ca993e8 42 fmt.addIfAbsent("a bad variant", "{0} pounds", status));
f3c0d7a5 43 assertEquals("adding bad variant status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status);
b331163b
A
44 status = U_ZERO_ERROR;
45 assertFalse(
46 "Adding bad pattern",
2ca993e8 47 fmt.addIfAbsent("other", "{0} {1} too many placeholders", status));
f3c0d7a5 48 assertEquals("adding bad pattern status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status);
b331163b
A
49 status = U_ZERO_ERROR;
50 assertFalse("isValid with no patterns", fmt.isValid());
51 assertTrue(
52 "Adding good pattern with no placeholders",
2ca993e8 53 fmt.addIfAbsent("zero", "no placeholder", status));
b331163b
A
54 assertTrue(
55 "Adding good pattern",
2ca993e8 56 fmt.addIfAbsent("other", "{0} pounds", status));
b331163b
A
57 assertTrue("isValid with other", fmt.isValid());
58 assertTrue(
59 "Adding good pattern",
2ca993e8 60 fmt.addIfAbsent("one", "{0} pound", status));
b331163b
A
61
62 assertEquals(
63 "getByVariant",
2ca993e8 64 fmt.getByVariant("bad variant")->getTextWithNoArguments(),
b331163b
A
65 " pounds");
66 assertEquals(
67 "getByVariant",
2ca993e8 68 fmt.getByVariant("other")->getTextWithNoArguments(),
b331163b
A
69 " pounds");
70 assertEquals(
71 "getByVariant",
2ca993e8 72 fmt.getByVariant("one")->getTextWithNoArguments(),
b331163b
A
73 " pound");
74 assertEquals(
75 "getByVariant",
2ca993e8 76 fmt.getByVariant("few")->getTextWithNoArguments(),
b331163b
A
77 " pounds");
78
79 // Test copy constructor
80 {
81 QuantityFormatter copied(fmt);
82 assertEquals(
83 "copied getByVariant",
2ca993e8 84 copied.getByVariant("other")->getTextWithNoArguments(),
b331163b
A
85 " pounds");
86 assertEquals(
87 "copied getByVariant",
2ca993e8 88 copied.getByVariant("one")->getTextWithNoArguments(),
b331163b
A
89 " pound");
90 assertEquals(
91 "copied getByVariant",
2ca993e8 92 copied.getByVariant("few")->getTextWithNoArguments(),
b331163b
A
93 " pounds");
94 }
95
96 // Test assignment
97 {
98 QuantityFormatter assigned;
99 assigned = fmt;
100 assertEquals(
101 "assigned getByVariant",
2ca993e8 102 assigned.getByVariant("other")->getTextWithNoArguments(),
b331163b
A
103 " pounds");
104 assertEquals(
105 "assigned getByVariant",
2ca993e8 106 assigned.getByVariant("one")->getTextWithNoArguments(),
b331163b
A
107 " pound");
108 assertEquals(
109 "assigned getByVariant",
2ca993e8 110 assigned.getByVariant("few")->getTextWithNoArguments(),
b331163b
A
111 " pounds");
112 }
113
114 // Test format.
115 {
116 LocalPointer<NumberFormat> numfmt(
117 NumberFormat::createInstance(Locale::getEnglish(), status));
118 LocalPointer<PluralRules> plurrule(
119 PluralRules::forLocale("en", status));
120 FieldPosition pos(FieldPosition::DONT_CARE);
121 UnicodeString appendTo;
122 assertEquals(
123 "format singular",
f3c0d7a5 124 UnicodeString("1 pound"),
b331163b 125 fmt.format(
f3c0d7a5 126 1.0,
b331163b
A
127 *numfmt,
128 *plurrule,
129 appendTo,
130 pos,
131 status), TRUE);
132 appendTo.remove();
133 assertEquals(
134 "format plural",
f3c0d7a5 135 UnicodeString("2 pounds"),
b331163b 136 fmt.format(
f3c0d7a5 137 2.0,
b331163b
A
138 *numfmt,
139 *plurrule,
140 appendTo,
141 pos,
142 status), TRUE);
143 }
144 fmt.reset();
145 assertFalse("isValid after reset", fmt.isValid());
146#endif
147 assertSuccess("", status);
148}
149
150extern IntlTest *createQuantityFormatterTest() {
151 return new QuantityFormatterTest();
152}