2017-09-27 00:25:20 +00:00
|
|
|
// © 2017 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
2017-10-04 22:51:06 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2018-04-23 23:02:26 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2017-09-27 05:31:57 +00:00
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
#include "numbertest.h"
|
2018-05-25 17:57:30 +00:00
|
|
|
#include "number_microprops.h"
|
2017-09-27 02:16:44 +00:00
|
|
|
#include "number_patternmodifier.h"
|
2017-09-27 00:25:20 +00:00
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
void PatternModifierTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char *) {
|
|
|
|
if (exec) {
|
|
|
|
logln("TestSuite PatternModifierTest: ");
|
|
|
|
}
|
|
|
|
TESTCASE_AUTO_BEGIN;
|
|
|
|
TESTCASE_AUTO(testBasic);
|
2018-02-06 03:08:17 +00:00
|
|
|
TESTCASE_AUTO(testPatternWithNoPlaceholder);
|
2017-09-27 02:16:44 +00:00
|
|
|
TESTCASE_AUTO(testMutableEqualsImmutable);
|
|
|
|
TESTCASE_AUTO_END;
|
|
|
|
}
|
2017-09-27 00:25:20 +00:00
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
void PatternModifierTest::testBasic() {
|
2017-09-27 00:25:20 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
MutablePatternModifier mod(false);
|
|
|
|
ParsedPatternInfo patternInfo;
|
|
|
|
PatternParser::parseToPatternInfo(u"a0b", patternInfo, status);
|
|
|
|
assertSuccess("Spot 1", status);
|
2019-11-20 02:22:20 +00:00
|
|
|
mod.setPatternInfo(&patternInfo, kUndefinedField);
|
2017-09-27 00:25:20 +00:00
|
|
|
mod.setPatternAttributes(UNUM_SIGN_AUTO, false);
|
|
|
|
DecimalFormatSymbols symbols(Locale::getEnglish(), status);
|
2018-03-21 05:17:28 +00:00
|
|
|
CurrencySymbols currencySymbols({u"USD", status}, "en", status);
|
2018-06-01 01:19:38 +00:00
|
|
|
if (!assertSuccess("Spot 2", status, true)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-17 01:31:52 +00:00
|
|
|
mod.setSymbols(&symbols, ¤cySymbols, UNUM_UNIT_WIDTH_SHORT, nullptr);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
2019-07-23 04:41:43 +00:00
|
|
|
mod.setNumberProperties(SIGNUM_POS, StandardPlural::Form::COUNT);
|
2017-09-27 00:25:20 +00:00
|
|
|
assertEquals("Pattern a0b", u"a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
|
|
|
|
mod.setPatternAttributes(UNUM_SIGN_ALWAYS, false);
|
|
|
|
assertEquals("Pattern a0b", u"+a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
|
2019-09-27 23:57:52 +00:00
|
|
|
mod.setNumberProperties(SIGNUM_NEG_ZERO, StandardPlural::Form::COUNT);
|
|
|
|
assertEquals("Pattern a0b", u"-a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
|
|
|
|
mod.setNumberProperties(SIGNUM_POS_ZERO, StandardPlural::Form::COUNT);
|
2018-01-30 02:49:07 +00:00
|
|
|
assertEquals("Pattern a0b", u"+a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
|
|
|
|
mod.setPatternAttributes(UNUM_SIGN_EXCEPT_ZERO, false);
|
|
|
|
assertEquals("Pattern a0b", u"a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
|
2019-07-23 04:41:43 +00:00
|
|
|
mod.setNumberProperties(SIGNUM_NEG, StandardPlural::Form::COUNT);
|
2017-09-27 00:25:20 +00:00
|
|
|
assertEquals("Pattern a0b", u"-a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
|
|
|
|
mod.setPatternAttributes(UNUM_SIGN_NEVER, false);
|
|
|
|
assertEquals("Pattern a0b", u"a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b", u"b", getSuffix(mod, status));
|
|
|
|
assertSuccess("Spot 3", status);
|
|
|
|
|
|
|
|
ParsedPatternInfo patternInfo2;
|
|
|
|
PatternParser::parseToPatternInfo(u"a0b;c-0d", patternInfo2, status);
|
|
|
|
assertSuccess("Spot 4", status);
|
2019-11-20 02:22:20 +00:00
|
|
|
mod.setPatternInfo(&patternInfo2, kUndefinedField);
|
2017-09-27 00:25:20 +00:00
|
|
|
mod.setPatternAttributes(UNUM_SIGN_AUTO, false);
|
2019-07-23 04:41:43 +00:00
|
|
|
mod.setNumberProperties(SIGNUM_POS, StandardPlural::Form::COUNT);
|
2017-09-27 00:25:20 +00:00
|
|
|
assertEquals("Pattern a0b;c-0d", u"a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"b", getSuffix(mod, status));
|
|
|
|
mod.setPatternAttributes(UNUM_SIGN_ALWAYS, false);
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"c+", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"d", getSuffix(mod, status));
|
2019-09-27 23:57:52 +00:00
|
|
|
mod.setNumberProperties(SIGNUM_NEG_ZERO, StandardPlural::Form::COUNT);
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"c-", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"d", getSuffix(mod, status));
|
|
|
|
mod.setNumberProperties(SIGNUM_POS_ZERO, StandardPlural::Form::COUNT);
|
2018-01-30 02:49:07 +00:00
|
|
|
assertEquals("Pattern a0b;c-0d", u"c+", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"d", getSuffix(mod, status));
|
|
|
|
mod.setPatternAttributes(UNUM_SIGN_EXCEPT_ZERO, false);
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"b", getSuffix(mod, status));
|
2019-07-23 04:41:43 +00:00
|
|
|
mod.setNumberProperties(SIGNUM_NEG, StandardPlural::Form::COUNT);
|
2017-09-27 00:25:20 +00:00
|
|
|
assertEquals("Pattern a0b;c-0d", u"c-", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"d", getSuffix(mod, status));
|
|
|
|
mod.setPatternAttributes(UNUM_SIGN_NEVER, false);
|
2019-09-27 23:57:52 +00:00
|
|
|
assertEquals("Pattern a0b;c-0d", u"a", getPrefix(mod, status));
|
|
|
|
assertEquals("Pattern a0b;c-0d", u"b", getSuffix(mod, status));
|
2017-09-27 00:25:20 +00:00
|
|
|
assertSuccess("Spot 5", status);
|
|
|
|
}
|
|
|
|
|
2018-02-06 03:08:17 +00:00
|
|
|
void PatternModifierTest::testPatternWithNoPlaceholder() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
MutablePatternModifier mod(false);
|
|
|
|
ParsedPatternInfo patternInfo;
|
|
|
|
PatternParser::parseToPatternInfo(u"abc", patternInfo, status);
|
|
|
|
assertSuccess("Spot 1", status);
|
2019-11-20 02:22:20 +00:00
|
|
|
mod.setPatternInfo(&patternInfo, kUndefinedField);
|
2018-02-06 03:08:17 +00:00
|
|
|
mod.setPatternAttributes(UNUM_SIGN_AUTO, false);
|
|
|
|
DecimalFormatSymbols symbols(Locale::getEnglish(), status);
|
2018-03-21 05:17:28 +00:00
|
|
|
CurrencySymbols currencySymbols({u"USD", status}, "en", status);
|
2018-06-01 01:19:38 +00:00
|
|
|
if (!assertSuccess("Spot 2", status, true)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-17 01:31:52 +00:00
|
|
|
mod.setSymbols(&symbols, ¤cySymbols, UNUM_UNIT_WIDTH_SHORT, nullptr);
|
2019-07-23 04:41:43 +00:00
|
|
|
mod.setNumberProperties(SIGNUM_POS, StandardPlural::Form::COUNT);
|
2018-02-06 03:08:17 +00:00
|
|
|
|
|
|
|
// Unsafe Code Path
|
2019-04-05 23:43:19 +00:00
|
|
|
FormattedStringBuilder nsb;
|
2019-11-20 02:22:20 +00:00
|
|
|
nsb.append(u"x123y", kUndefinedField, status);
|
2018-02-06 03:08:17 +00:00
|
|
|
assertSuccess("Spot 3", status);
|
|
|
|
mod.apply(nsb, 1, 4, status);
|
|
|
|
assertSuccess("Spot 4", status);
|
|
|
|
assertEquals("Unsafe Path", u"xabcy", nsb.toUnicodeString());
|
|
|
|
|
|
|
|
// Safe Code Path
|
|
|
|
nsb.clear();
|
2019-11-20 02:22:20 +00:00
|
|
|
nsb.append(u"x123y", kUndefinedField, status);
|
2018-02-06 03:08:17 +00:00
|
|
|
assertSuccess("Spot 5", status);
|
|
|
|
MicroProps micros;
|
2018-03-06 23:13:49 +00:00
|
|
|
LocalPointer<ImmutablePatternModifier> imod(mod.createImmutable(status), status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
dataerrln("%s %d Error in ImmutablePatternModifier creation",
|
|
|
|
__FILE__, __LINE__);
|
|
|
|
assertSuccess("Spot 6", status);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-06 03:08:17 +00:00
|
|
|
DecimalQuantity quantity;
|
2019-03-13 23:08:09 +00:00
|
|
|
imod->applyToMicros(micros, quantity, status);
|
2018-02-06 03:08:17 +00:00
|
|
|
micros.modMiddle->apply(nsb, 1, 4, status);
|
|
|
|
assertSuccess("Spot 7", status);
|
|
|
|
assertEquals("Safe Path", u"xabcy", nsb.toUnicodeString());
|
|
|
|
}
|
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
void PatternModifierTest::testMutableEqualsImmutable() {
|
2017-09-27 00:25:20 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
MutablePatternModifier mod(false);
|
|
|
|
ParsedPatternInfo patternInfo;
|
|
|
|
PatternParser::parseToPatternInfo("a0b;c-0d", patternInfo, status);
|
|
|
|
assertSuccess("Spot 1", status);
|
2019-11-20 02:22:20 +00:00
|
|
|
mod.setPatternInfo(&patternInfo, kUndefinedField);
|
2017-09-27 00:25:20 +00:00
|
|
|
mod.setPatternAttributes(UNUM_SIGN_AUTO, false);
|
|
|
|
DecimalFormatSymbols symbols(Locale::getEnglish(), status);
|
2018-03-21 05:17:28 +00:00
|
|
|
CurrencySymbols currencySymbols({u"USD", status}, "en", status);
|
2017-09-27 00:25:20 +00:00
|
|
|
assertSuccess("Spot 2", status);
|
2017-10-06 23:11:11 +00:00
|
|
|
if (U_FAILURE(status)) { return; }
|
2018-03-17 01:31:52 +00:00
|
|
|
mod.setSymbols(&symbols, ¤cySymbols, UNUM_UNIT_WIDTH_SHORT, nullptr);
|
2017-09-27 00:25:20 +00:00
|
|
|
DecimalQuantity fq;
|
|
|
|
fq.setToInt(1);
|
|
|
|
|
2019-04-05 23:43:19 +00:00
|
|
|
FormattedStringBuilder nsb1;
|
2017-09-27 00:25:20 +00:00
|
|
|
MicroProps micros1;
|
|
|
|
mod.addToChain(µs1);
|
|
|
|
mod.processQuantity(fq, micros1, status);
|
|
|
|
micros1.modMiddle->apply(nsb1, 0, 0, status);
|
|
|
|
assertSuccess("Spot 3", status);
|
|
|
|
|
2019-04-05 23:43:19 +00:00
|
|
|
FormattedStringBuilder nsb2;
|
2017-09-27 00:25:20 +00:00
|
|
|
MicroProps micros2;
|
2017-09-27 03:52:08 +00:00
|
|
|
LocalPointer<ImmutablePatternModifier> immutable(mod.createImmutable(status));
|
2019-03-13 23:08:09 +00:00
|
|
|
immutable->applyToMicros(micros2, fq, status);
|
2017-09-27 00:25:20 +00:00
|
|
|
micros2.modMiddle->apply(nsb2, 0, 0, status);
|
|
|
|
assertSuccess("Spot 4", status);
|
|
|
|
|
2019-04-05 23:43:19 +00:00
|
|
|
FormattedStringBuilder nsb3;
|
2017-09-27 00:25:20 +00:00
|
|
|
MicroProps micros3;
|
|
|
|
mod.addToChain(µs3);
|
|
|
|
mod.setPatternAttributes(UNUM_SIGN_ALWAYS, false);
|
|
|
|
mod.processQuantity(fq, micros3, status);
|
|
|
|
micros3.modMiddle->apply(nsb3, 0, 0, status);
|
|
|
|
assertSuccess("Spot 5", status);
|
|
|
|
|
|
|
|
assertTrue(nsb1.toUnicodeString() + " vs " + nsb2.toUnicodeString(), nsb1.contentEquals(nsb2));
|
|
|
|
assertFalse(nsb1.toUnicodeString() + " vs " + nsb3.toUnicodeString(), nsb1.contentEquals(nsb3));
|
|
|
|
}
|
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
UnicodeString PatternModifierTest::getPrefix(const MutablePatternModifier &mod, UErrorCode &status) {
|
2019-04-05 23:43:19 +00:00
|
|
|
FormattedStringBuilder nsb;
|
2017-09-27 00:25:20 +00:00
|
|
|
mod.apply(nsb, 0, 0, status);
|
2018-09-06 01:16:32 +00:00
|
|
|
int32_t prefixLength = mod.getPrefixLength();
|
2017-09-27 00:25:20 +00:00
|
|
|
return UnicodeString(nsb.toUnicodeString(), 0, prefixLength);
|
|
|
|
}
|
|
|
|
|
2017-09-27 02:16:44 +00:00
|
|
|
UnicodeString PatternModifierTest::getSuffix(const MutablePatternModifier &mod, UErrorCode &status) {
|
2019-04-05 23:43:19 +00:00
|
|
|
FormattedStringBuilder nsb;
|
2017-09-27 00:25:20 +00:00
|
|
|
mod.apply(nsb, 0, 0, status);
|
2018-09-06 01:16:32 +00:00
|
|
|
int32_t prefixLength = mod.getPrefixLength();
|
2017-09-27 00:25:20 +00:00
|
|
|
return UnicodeString(nsb.toUnicodeString(), prefixLength, nsb.length() - prefixLength);
|
|
|
|
}
|
2017-09-27 05:31:57 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|