• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING && !UPRV_INCOMPLETE_CPP11_SUPPORT
7 
8 #include "numbertest.h"
9 #include "number_patternmodifier.h"
10 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)11 void PatternModifierTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *) {
12     if (exec) {
13         logln("TestSuite PatternModifierTest: ");
14     }
15     TESTCASE_AUTO_BEGIN;
16         TESTCASE_AUTO(testBasic);
17         TESTCASE_AUTO(testMutableEqualsImmutable);
18     TESTCASE_AUTO_END;
19 }
20 
testBasic()21 void PatternModifierTest::testBasic() {
22     UErrorCode status = U_ZERO_ERROR;
23     MutablePatternModifier mod(false);
24     ParsedPatternInfo patternInfo;
25     PatternParser::parseToPatternInfo(u"a0b", patternInfo, status);
26     assertSuccess("Spot 1", status);
27     mod.setPatternInfo(&patternInfo);
28     mod.setPatternAttributes(UNUM_SIGN_AUTO, false);
29     DecimalFormatSymbols symbols(Locale::getEnglish(), status);
30     CurrencyUnit currency(u"USD", status);
31     assertSuccess("Spot 2", status);
32     mod.setSymbols(&symbols, currency, UNUM_UNIT_WIDTH_SHORT, nullptr);
33 
34     mod.setNumberProperties(false, StandardPlural::Form::COUNT);
35     assertEquals("Pattern a0b", u"a", getPrefix(mod, status));
36     assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
37     mod.setPatternAttributes(UNUM_SIGN_ALWAYS, false);
38     assertEquals("Pattern a0b", u"+a", getPrefix(mod, status));
39     assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
40     mod.setNumberProperties(true, StandardPlural::Form::COUNT);
41     assertEquals("Pattern a0b", u"-a", getPrefix(mod, status));
42     assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
43     mod.setPatternAttributes(UNUM_SIGN_NEVER, false);
44     assertEquals("Pattern a0b", u"a", getPrefix(mod, status));
45     assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
46     assertSuccess("Spot 3", status);
47 
48     ParsedPatternInfo patternInfo2;
49     PatternParser::parseToPatternInfo(u"a0b;c-0d", patternInfo2, status);
50     assertSuccess("Spot 4", status);
51     mod.setPatternInfo(&patternInfo2);
52     mod.setPatternAttributes(UNUM_SIGN_AUTO, false);
53     mod.setNumberProperties(false, StandardPlural::Form::COUNT);
54     assertEquals("Pattern a0b;c-0d", u"a", getPrefix(mod, status));
55     assertEquals("Pattern a0b;c-0d", u"b", getSuffix(mod, status));
56     mod.setPatternAttributes(UNUM_SIGN_ALWAYS, false);
57     assertEquals("Pattern a0b;c-0d", u"c+", getPrefix(mod, status));
58     assertEquals("Pattern a0b;c-0d", u"d", getSuffix(mod, status));
59     mod.setNumberProperties(true, StandardPlural::Form::COUNT);
60     assertEquals("Pattern a0b;c-0d", u"c-", getPrefix(mod, status));
61     assertEquals("Pattern a0b;c-0d", u"d", getSuffix(mod, status));
62     mod.setPatternAttributes(UNUM_SIGN_NEVER, false);
63     assertEquals(
64             "Pattern a0b;c-0d",
65             u"c-",
66             getPrefix(mod, status)); // TODO: What should this behavior be?
67     assertEquals("Pattern a0b;c-0d", u"d", getSuffix(mod, status));
68     assertSuccess("Spot 5", status);
69 }
70 
testMutableEqualsImmutable()71 void PatternModifierTest::testMutableEqualsImmutable() {
72     UErrorCode status = U_ZERO_ERROR;
73     MutablePatternModifier mod(false);
74     ParsedPatternInfo patternInfo;
75     PatternParser::parseToPatternInfo("a0b;c-0d", patternInfo, status);
76     assertSuccess("Spot 1", status);
77     mod.setPatternInfo(&patternInfo);
78     mod.setPatternAttributes(UNUM_SIGN_AUTO, false);
79     DecimalFormatSymbols symbols(Locale::getEnglish(), status);
80     CurrencyUnit currency(u"USD", status);
81     assertSuccess("Spot 2", status);
82     if (U_FAILURE(status)) { return; }
83     mod.setSymbols(&symbols, currency, UNUM_UNIT_WIDTH_SHORT, nullptr);
84     DecimalQuantity fq;
85     fq.setToInt(1);
86 
87     NumberStringBuilder nsb1;
88     MicroProps micros1;
89     mod.addToChain(&micros1);
90     mod.processQuantity(fq, micros1, status);
91     micros1.modMiddle->apply(nsb1, 0, 0, status);
92     assertSuccess("Spot 3", status);
93 
94     NumberStringBuilder nsb2;
95     MicroProps micros2;
96     LocalPointer<ImmutablePatternModifier> immutable(mod.createImmutable(status));
97     immutable->applyToMicros(micros2, fq);
98     micros2.modMiddle->apply(nsb2, 0, 0, status);
99     assertSuccess("Spot 4", status);
100 
101     NumberStringBuilder nsb3;
102     MicroProps micros3;
103     mod.addToChain(&micros3);
104     mod.setPatternAttributes(UNUM_SIGN_ALWAYS, false);
105     mod.processQuantity(fq, micros3, status);
106     micros3.modMiddle->apply(nsb3, 0, 0, status);
107     assertSuccess("Spot 5", status);
108 
109     assertTrue(nsb1.toUnicodeString() + " vs " + nsb2.toUnicodeString(), nsb1.contentEquals(nsb2));
110     assertFalse(nsb1.toUnicodeString() + " vs " + nsb3.toUnicodeString(), nsb1.contentEquals(nsb3));
111 }
112 
getPrefix(const MutablePatternModifier & mod,UErrorCode & status)113 UnicodeString PatternModifierTest::getPrefix(const MutablePatternModifier &mod, UErrorCode &status) {
114     NumberStringBuilder nsb;
115     mod.apply(nsb, 0, 0, status);
116     int32_t prefixLength = mod.getPrefixLength(status);
117     return UnicodeString(nsb.toUnicodeString(), 0, prefixLength);
118 }
119 
getSuffix(const MutablePatternModifier & mod,UErrorCode & status)120 UnicodeString PatternModifierTest::getSuffix(const MutablePatternModifier &mod, UErrorCode &status) {
121     NumberStringBuilder nsb;
122     mod.apply(nsb, 0, 0, status);
123     int32_t prefixLength = mod.getPrefixLength(status);
124     return UnicodeString(nsb.toUnicodeString(), prefixLength, nsb.length() - prefixLength);
125 }
126 
127 #endif /* #if !UCONFIG_NO_FORMATTING */
128