2018-03-24 05:46:28 +00:00
|
|
|
// © 2017 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2018-04-23 23:02:26 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2018-03-24 05:46:28 +00:00
|
|
|
|
|
|
|
#include "unicode/dcfmtsym.h"
|
2018-06-01 01:19:38 +00:00
|
|
|
|
|
|
|
#include "cstr.h"
|
2018-03-24 05:46:28 +00:00
|
|
|
#include "numbertest.h"
|
|
|
|
#include "number_utils.h"
|
|
|
|
#include "number_skeletons.h"
|
2018-06-01 01:19:38 +00:00
|
|
|
#include "putilimp.h"
|
2018-03-24 05:46:28 +00:00
|
|
|
|
|
|
|
using namespace icu::number::impl;
|
|
|
|
|
|
|
|
|
|
|
|
void NumberSkeletonTest::runIndexedTest(int32_t index, UBool exec, const char*& name, char*) {
|
|
|
|
if (exec) {
|
|
|
|
logln("TestSuite AffixUtilsTest: ");
|
|
|
|
}
|
|
|
|
TESTCASE_AUTO_BEGIN;
|
|
|
|
TESTCASE_AUTO(validTokens);
|
|
|
|
TESTCASE_AUTO(invalidTokens);
|
|
|
|
TESTCASE_AUTO(unknownTokens);
|
|
|
|
TESTCASE_AUTO(unexpectedTokens);
|
|
|
|
TESTCASE_AUTO(duplicateValues);
|
|
|
|
TESTCASE_AUTO(stemsRequiringOption);
|
|
|
|
TESTCASE_AUTO(defaultTokens);
|
|
|
|
TESTCASE_AUTO(flexibleSeparators);
|
2020-03-25 21:22:23 +00:00
|
|
|
TESTCASE_AUTO(wildcardCharacters);
|
2018-03-24 05:46:28 +00:00
|
|
|
TESTCASE_AUTO_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::validTokens() {
|
2018-05-30 03:34:41 +00:00
|
|
|
IcuTestErrorCode status(*this, "validTokens");
|
|
|
|
|
2018-03-24 05:46:28 +00:00
|
|
|
// This tests only if the tokens are valid, not their behavior.
|
|
|
|
// Most of these are from the design doc.
|
|
|
|
static const char16_t* cases[] = {
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-integer",
|
|
|
|
u"precision-unlimited",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"@@@##",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"@@*",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"@@+",
|
|
|
|
u".000##",
|
2020-03-25 21:22:23 +00:00
|
|
|
u".00*",
|
2018-03-24 05:46:28 +00:00
|
|
|
u".00+",
|
|
|
|
u".",
|
2020-03-25 21:22:23 +00:00
|
|
|
u".*",
|
2018-03-24 05:46:28 +00:00
|
|
|
u".+",
|
|
|
|
u".######",
|
2020-03-25 21:22:23 +00:00
|
|
|
u".00/@@*",
|
2018-03-24 05:46:28 +00:00
|
|
|
u".00/@@+",
|
|
|
|
u".00/@##",
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-increment/3.14",
|
|
|
|
u"precision-currency-standard",
|
|
|
|
u"precision-integer rounding-mode-half-up",
|
|
|
|
u".00# rounding-mode-ceiling",
|
2020-03-25 21:22:23 +00:00
|
|
|
u".00/@@* rounding-mode-floor",
|
2018-05-05 06:32:29 +00:00
|
|
|
u".00/@@+ rounding-mode-floor",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"scientific",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"scientific/*ee",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"scientific/+ee",
|
|
|
|
u"scientific/sign-always",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"scientific/*ee/sign-always",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"scientific/+ee/sign-always",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"scientific/sign-always/*ee",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"scientific/sign-always/+ee",
|
|
|
|
u"scientific/sign-except-zero",
|
|
|
|
u"engineering",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"engineering/*eee",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"engineering/+eee",
|
|
|
|
u"compact-short",
|
|
|
|
u"compact-long",
|
|
|
|
u"notation-simple",
|
|
|
|
u"percent",
|
|
|
|
u"permille",
|
|
|
|
u"measure-unit/length-meter",
|
|
|
|
u"measure-unit/area-square-meter",
|
|
|
|
u"measure-unit/energy-joule per-measure-unit/length-meter",
|
2019-12-12 04:45:54 +00:00
|
|
|
u"unit/square-meter-per-square-meter",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"currency/XXX",
|
|
|
|
u"currency/ZZZ",
|
2018-04-19 01:13:17 +00:00
|
|
|
u"currency/usd",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"group-off",
|
|
|
|
u"group-min2",
|
|
|
|
u"group-auto",
|
|
|
|
u"group-on-aligned",
|
|
|
|
u"group-thousands",
|
|
|
|
u"integer-width/00",
|
|
|
|
u"integer-width/#0",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"integer-width/*00",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"integer-width/+00",
|
|
|
|
u"sign-always",
|
|
|
|
u"sign-auto",
|
|
|
|
u"sign-never",
|
|
|
|
u"sign-accounting",
|
|
|
|
u"sign-accounting-always",
|
|
|
|
u"sign-except-zero",
|
|
|
|
u"sign-accounting-except-zero",
|
|
|
|
u"unit-width-narrow",
|
|
|
|
u"unit-width-short",
|
|
|
|
u"unit-width-iso-code",
|
|
|
|
u"unit-width-full-name",
|
|
|
|
u"unit-width-hidden",
|
|
|
|
u"decimal-auto",
|
|
|
|
u"decimal-always",
|
2018-04-14 09:45:39 +00:00
|
|
|
u"scale/5.2",
|
|
|
|
u"scale/-5.2",
|
|
|
|
u"scale/100",
|
|
|
|
u"scale/1E2",
|
|
|
|
u"scale/1",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"latin",
|
|
|
|
u"numbering-system/arab",
|
|
|
|
u"numbering-system/latn",
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-integer/@##",
|
|
|
|
u"precision-integer rounding-mode-ceiling",
|
2019-12-12 04:45:54 +00:00
|
|
|
u"precision-currency-cash rounding-mode-ceiling",
|
|
|
|
u"0",
|
|
|
|
u"00",
|
|
|
|
u"000",
|
|
|
|
u"E0",
|
|
|
|
u"E00",
|
|
|
|
u"E000",
|
|
|
|
u"EE0",
|
|
|
|
u"EE00",
|
|
|
|
u"EE+?0",
|
|
|
|
u"EE+?00",
|
|
|
|
u"EE+!0",
|
|
|
|
u"EE+!00",
|
|
|
|
};
|
2018-03-24 05:46:28 +00:00
|
|
|
|
|
|
|
for (auto& cas : cases) {
|
|
|
|
UnicodeString skeletonString(cas);
|
2018-05-30 03:34:41 +00:00
|
|
|
status.setScope(skeletonString);
|
2019-01-19 06:30:12 +00:00
|
|
|
UParseError perror;
|
|
|
|
NumberFormatter::forSkeleton(skeletonString, perror, status);
|
2018-06-01 01:19:38 +00:00
|
|
|
assertSuccess(CStr(skeletonString)(), status, true);
|
2019-01-19 06:30:12 +00:00
|
|
|
assertEquals(skeletonString, -1, perror.offset);
|
2018-05-30 03:34:41 +00:00
|
|
|
status.errIfFailureAndReset();
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::invalidTokens() {
|
|
|
|
static const char16_t* cases[] = {
|
|
|
|
u".00x",
|
|
|
|
u".00##0",
|
2020-03-25 21:22:23 +00:00
|
|
|
u".##*",
|
|
|
|
u".00##*",
|
|
|
|
u".0#*",
|
|
|
|
u"@#*",
|
2018-03-24 05:46:28 +00:00
|
|
|
u".##+",
|
|
|
|
u".00##+",
|
|
|
|
u".0#+",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"@#+",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"@@x",
|
|
|
|
u"@@##0",
|
|
|
|
u".00/@",
|
|
|
|
u".00/@@",
|
|
|
|
u".00/@@x",
|
|
|
|
u".00/@@#",
|
2020-03-25 21:22:23 +00:00
|
|
|
u".00/@@#*",
|
|
|
|
u".00/floor/@@*", // wrong order
|
2018-03-24 05:46:28 +00:00
|
|
|
u".00/@@#+",
|
|
|
|
u".00/floor/@@+", // wrong order
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-increment/français", // non-invariant characters for C++
|
2018-03-24 05:46:28 +00:00
|
|
|
u"scientific/ee",
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-increment/xxx",
|
|
|
|
u"precision-increment/NaN",
|
|
|
|
u"precision-increment/0.1.2",
|
2018-04-14 09:45:39 +00:00
|
|
|
u"scale/xxx",
|
|
|
|
u"scale/NaN",
|
|
|
|
u"scale/0.1.2",
|
|
|
|
u"scale/français", // non-invariant characters for C++
|
2018-03-24 05:46:28 +00:00
|
|
|
u"currency/dummy",
|
2018-04-19 01:13:17 +00:00
|
|
|
u"currency/ççç", // three characters but not ASCII
|
2018-03-24 05:46:28 +00:00
|
|
|
u"measure-unit/foo",
|
|
|
|
u"integer-width/xxx",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"integer-width/0*",
|
|
|
|
u"integer-width/*0#",
|
|
|
|
u"integer-width/*#",
|
|
|
|
u"integer-width/*#0",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"integer-width/0+",
|
|
|
|
u"integer-width/+0#",
|
2019-03-08 07:00:04 +00:00
|
|
|
u"integer-width/+#",
|
|
|
|
u"integer-width/+#0",
|
2019-12-12 04:45:54 +00:00
|
|
|
u"scientific/foo",
|
|
|
|
u"E",
|
|
|
|
u"E1",
|
|
|
|
u"E+",
|
|
|
|
u"E+?",
|
|
|
|
u"E+!",
|
|
|
|
u"E+0",
|
|
|
|
u"EE",
|
|
|
|
u"EE+",
|
|
|
|
u"EEE",
|
|
|
|
u"EEE0",
|
|
|
|
u"001",
|
2020-03-25 21:22:23 +00:00
|
|
|
u"00*",
|
2019-12-12 04:45:54 +00:00
|
|
|
u"00+",
|
|
|
|
};
|
2018-03-24 05:46:28 +00:00
|
|
|
|
2018-05-30 03:34:41 +00:00
|
|
|
expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::unknownTokens() {
|
|
|
|
static const char16_t* cases[] = {
|
|
|
|
u"maesure-unit",
|
|
|
|
u"measure-unit/foo-bar",
|
|
|
|
u"numbering-system/dummy",
|
|
|
|
u"français",
|
|
|
|
u"measure-unit/français-français", // non-invariant characters for C++
|
|
|
|
u"numbering-system/français", // non-invariant characters for C++
|
|
|
|
u"currency-USD"};
|
|
|
|
|
2018-05-30 03:34:41 +00:00
|
|
|
expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::unexpectedTokens() {
|
|
|
|
static const char16_t* cases[] = {
|
|
|
|
u"group-thousands/foo",
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-integer//@## group-off",
|
|
|
|
u"precision-integer//@## group-off",
|
|
|
|
u"precision-integer/ group-off",
|
|
|
|
u"precision-integer// group-off"};
|
2018-03-24 05:46:28 +00:00
|
|
|
|
2018-05-30 03:34:41 +00:00
|
|
|
expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::duplicateValues() {
|
|
|
|
static const char16_t* cases[] = {
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-integer precision-integer",
|
|
|
|
u"precision-integer .00+",
|
|
|
|
u"precision-integer precision-unlimited",
|
|
|
|
u"precision-integer @@@",
|
2018-03-24 05:46:28 +00:00
|
|
|
u"scientific engineering",
|
|
|
|
u"engineering compact-long",
|
|
|
|
u"sign-auto sign-always"};
|
|
|
|
|
2018-05-30 03:34:41 +00:00
|
|
|
expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::stemsRequiringOption() {
|
2018-04-04 01:26:18 +00:00
|
|
|
static const char16_t* stems[] = {
|
2018-05-05 06:32:29 +00:00
|
|
|
u"precision-increment",
|
2018-04-04 01:26:18 +00:00
|
|
|
u"measure-unit",
|
2019-01-19 06:30:12 +00:00
|
|
|
u"per-measure-unit",
|
2018-04-04 01:26:18 +00:00
|
|
|
u"currency",
|
|
|
|
u"integer-width",
|
|
|
|
u"numbering-system",
|
2018-04-14 09:45:39 +00:00
|
|
|
u"scale"};
|
2018-05-05 06:32:29 +00:00
|
|
|
static const char16_t* suffixes[] = {u"", u"/@##", u" scientific", u"/@## scientific"};
|
2018-03-24 05:46:28 +00:00
|
|
|
|
|
|
|
for (auto& stem : stems) {
|
|
|
|
for (auto& suffix : suffixes) {
|
|
|
|
UnicodeString skeletonString = UnicodeString(stem) + suffix;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2019-01-19 06:30:12 +00:00
|
|
|
UParseError perror;
|
|
|
|
NumberFormatter::forSkeleton(skeletonString, perror, status);
|
2018-03-24 05:46:28 +00:00
|
|
|
assertEquals(skeletonString, U_NUMBER_SKELETON_SYNTAX_ERROR, status);
|
2019-01-19 06:30:12 +00:00
|
|
|
|
|
|
|
// Check the UParseError for integrity.
|
|
|
|
// If an option is present, the option is wrong; error offset is at the start of the option
|
|
|
|
// If an option is not present, the error offset is at the token separator (end of stem)
|
|
|
|
int32_t expectedOffset = u_strlen(stem) + ((suffix[0] == u'/') ? 1 : 0);
|
|
|
|
assertEquals(skeletonString, expectedOffset, perror.offset);
|
|
|
|
UnicodeString expectedPreContext = skeletonString.tempSubString(0, expectedOffset);
|
|
|
|
if (expectedPreContext.length() >= U_PARSE_CONTEXT_LEN - 1) {
|
|
|
|
expectedPreContext = expectedPreContext.tempSubString(expectedOffset - U_PARSE_CONTEXT_LEN + 1);
|
|
|
|
}
|
|
|
|
assertEquals(skeletonString, expectedPreContext, perror.preContext);
|
|
|
|
UnicodeString expectedPostContext = skeletonString.tempSubString(expectedOffset);
|
|
|
|
// None of the postContext strings in this test exceed U_PARSE_CONTEXT_LEN
|
|
|
|
assertEquals(skeletonString, expectedPostContext, perror.postContext);
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::defaultTokens() {
|
|
|
|
IcuTestErrorCode status(*this, "defaultTokens");
|
|
|
|
|
|
|
|
static const char16_t* cases[] = {
|
|
|
|
u"notation-simple",
|
|
|
|
u"base-unit",
|
|
|
|
u"group-auto",
|
|
|
|
u"integer-width/+0",
|
|
|
|
u"sign-auto",
|
|
|
|
u"unit-width-short",
|
|
|
|
u"decimal-auto"};
|
|
|
|
|
|
|
|
for (auto& cas : cases) {
|
|
|
|
UnicodeString skeletonString(cas);
|
|
|
|
status.setScope(skeletonString);
|
2018-05-17 00:43:12 +00:00
|
|
|
UnicodeString normalized = NumberFormatter::forSkeleton(
|
2018-03-24 05:46:28 +00:00
|
|
|
skeletonString, status).toSkeleton(status);
|
|
|
|
// Skeleton should become empty when normalized
|
|
|
|
assertEquals(skeletonString, u"", normalized);
|
2018-05-30 03:34:41 +00:00
|
|
|
status.errIfFailureAndReset();
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberSkeletonTest::flexibleSeparators() {
|
|
|
|
IcuTestErrorCode status(*this, "flexibleSeparators");
|
|
|
|
|
|
|
|
static struct TestCase {
|
|
|
|
const char16_t* skeleton;
|
|
|
|
const char16_t* expected;
|
2018-05-05 06:32:29 +00:00
|
|
|
} cases[] = {{u"precision-integer group-off", u"5142"},
|
|
|
|
{u"precision-integer group-off", u"5142"},
|
|
|
|
{u"precision-integer/@## group-off", u"5140"},
|
|
|
|
{u"precision-integer/@## group-off", u"5140"}};
|
2018-03-24 05:46:28 +00:00
|
|
|
|
|
|
|
for (auto& cas : cases) {
|
|
|
|
UnicodeString skeletonString(cas.skeleton);
|
|
|
|
UnicodeString expected(cas.expected);
|
|
|
|
status.setScope(skeletonString);
|
2018-05-17 00:43:12 +00:00
|
|
|
UnicodeString actual = NumberFormatter::forSkeleton(skeletonString, status).locale("en")
|
2018-06-01 01:19:38 +00:00
|
|
|
.formatDouble(5142.3, status)
|
2019-01-19 03:01:30 +00:00
|
|
|
.toString(status);
|
2018-06-01 01:19:38 +00:00
|
|
|
if (!status.errDataIfFailureAndReset()) {
|
|
|
|
assertEquals(skeletonString, expected, actual);
|
|
|
|
}
|
2018-05-30 03:34:41 +00:00
|
|
|
status.errIfFailureAndReset();
|
2018-03-24 05:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 21:22:23 +00:00
|
|
|
void NumberSkeletonTest::wildcardCharacters() {
|
|
|
|
IcuTestErrorCode status(*this, "wildcardCharacters");
|
|
|
|
|
|
|
|
struct TestCase {
|
|
|
|
const char16_t* star;
|
|
|
|
const char16_t* plus;
|
|
|
|
} cases[] = {
|
|
|
|
{ u".00*", u".00+" },
|
|
|
|
{ u"@@*", u"@@+" },
|
|
|
|
{ u".00/@@*", u".00/@@+" },
|
|
|
|
{ u"scientific/*ee", u"scientific/+ee" },
|
|
|
|
{ u"integer-width/*00", u"integer-width/+00" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& cas : cases) {
|
|
|
|
UnicodeString star(cas.star);
|
|
|
|
UnicodeString plus(cas.plus);
|
|
|
|
status.setScope(star);
|
|
|
|
|
|
|
|
UnicodeString normalized = NumberFormatter::forSkeleton(plus, status)
|
|
|
|
.toSkeleton(status);
|
|
|
|
assertEquals("Plus should normalize to star", star, normalized);
|
|
|
|
status.errIfFailureAndReset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 05:46:28 +00:00
|
|
|
// In C++, there is no distinguishing between "invalid", "unknown", and "unexpected" tokens.
|
|
|
|
void NumberSkeletonTest::expectedErrorSkeleton(const char16_t** cases, int32_t casesLen) {
|
|
|
|
for (int32_t i = 0; i < casesLen; i++) {
|
|
|
|
UnicodeString skeletonString(cases[i]);
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2018-05-17 00:43:12 +00:00
|
|
|
NumberFormatter::forSkeleton(skeletonString, status);
|
2018-03-24 05:46:28 +00:00
|
|
|
assertEquals(skeletonString, U_NUMBER_SKELETON_SYNTAX_ERROR, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|