• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2018 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
7 
8 #include "numbertest.h"
9 #include "static_unicode_sets.h"
10 #include "unicode/dcfmtsym.h"
11 
12 using icu::unisets::get;
13 
14 class StaticUnicodeSetsTest : public IntlTest {
15   public:
16     void testSetCoverage();
17     void testNonEmpty();
18 
19     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
20 
21   private:
22     void assertInSet(const UnicodeString& localeName, const UnicodeString &setName,
23                      const UnicodeSet& set, const UnicodeString& str);
24     void assertInSet(const UnicodeString& localeName, const UnicodeString &setName,
25                      const UnicodeSet& set, UChar32 cp);
26 };
27 
createStaticUnicodeSetsTest()28 extern IntlTest *createStaticUnicodeSetsTest() {
29     return new StaticUnicodeSetsTest();
30 }
31 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)32 void StaticUnicodeSetsTest::runIndexedTest(int32_t index, UBool exec, const char*&name, char*) {
33     if (exec) {
34         logln("TestSuite StaticUnicodeSetsTest: ");
35     }
36     TESTCASE_AUTO_BEGIN;
37         if (!quick) {
38             // Slow test: run in exhaustive mode only
39             TESTCASE_AUTO(testSetCoverage);
40         }
41         TESTCASE_AUTO(testNonEmpty);
42     TESTCASE_AUTO_END;
43 }
44 
testSetCoverage()45 void StaticUnicodeSetsTest::testSetCoverage() {
46     UErrorCode status = U_ZERO_ERROR;
47 
48     // Lenient comma/period should be supersets of strict comma/period;
49     // it also makes the coverage logic cheaper.
50     assertTrue(
51             "COMMA should be superset of STRICT_COMMA",
52             get(unisets::COMMA)->containsAll(*get(unisets::STRICT_COMMA)));
53     assertTrue(
54             "PERIOD should be superset of STRICT_PERIOD",
55             get(unisets::PERIOD)->containsAll(*get(unisets::STRICT_PERIOD)));
56 
57     UnicodeSet decimals;
58     decimals.addAll(*get(unisets::STRICT_COMMA));
59     decimals.addAll(*get(unisets::STRICT_PERIOD));
60     decimals.freeze();
61     UnicodeSet grouping;
62     grouping.addAll(decimals);
63     grouping.addAll(*get(unisets::OTHER_GROUPING_SEPARATORS));
64     decimals.freeze();
65 
66     const UnicodeSet &plusSign = *get(unisets::PLUS_SIGN);
67     const UnicodeSet &minusSign = *get(unisets::MINUS_SIGN);
68     const UnicodeSet &percent = *get(unisets::PERCENT_SIGN);
69     const UnicodeSet &permille = *get(unisets::PERMILLE_SIGN);
70     const UnicodeSet &infinity = *get(unisets::INFINITY_SIGN);
71 
72     int32_t localeCount;
73     const Locale* allAvailableLocales = Locale::getAvailableLocales(localeCount);
74     for (int32_t i = 0; i < localeCount; i++) {
75         Locale locale = allAvailableLocales[i];
76         DecimalFormatSymbols dfs(locale, status);
77         UnicodeString localeName;
78         locale.getDisplayName(localeName);
79         assertSuccess(UnicodeString("Making DFS for ") + localeName, status);
80 
81 #define ASSERT_IN_SET(name, foo) assertInSet(localeName, UnicodeString("" #name ""), name, foo)
82         ASSERT_IN_SET(decimals, dfs.getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
83         ASSERT_IN_SET(grouping, dfs.getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
84         ASSERT_IN_SET(plusSign, dfs.getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol));
85         ASSERT_IN_SET(minusSign, dfs.getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol));
86         ASSERT_IN_SET(percent, dfs.getConstSymbol(DecimalFormatSymbols::kPercentSymbol));
87         ASSERT_IN_SET(permille, dfs.getConstSymbol(DecimalFormatSymbols::kPerMillSymbol));
88         ASSERT_IN_SET(infinity, dfs.getConstSymbol(DecimalFormatSymbols::kInfinitySymbol));
89     }
90 }
91 
testNonEmpty()92 void StaticUnicodeSetsTest::testNonEmpty() {
93     for (int32_t i=0; i<unisets::UNISETS_KEY_COUNT; i++) {
94         if (i == unisets::EMPTY) {
95             continue;
96         }
97         const UnicodeSet* uset = get(static_cast<unisets::Key>(i));
98         // Can fail if no data:
99         assertFalse(UnicodeString("Set should not be empty: ") + i, uset->isEmpty(), FALSE, TRUE);
100     }
101 }
102 
assertInSet(const UnicodeString & localeName,const UnicodeString & setName,const UnicodeSet & set,const UnicodeString & str)103 void StaticUnicodeSetsTest::assertInSet(const UnicodeString &localeName, const UnicodeString &setName,
104                               const UnicodeSet &set, const UnicodeString &str) {
105     if (str.countChar32(0, str.length()) != 1) {
106         // Ignore locale strings with more than one code point (usually a bidi mark)
107         return;
108     }
109     assertInSet(localeName, setName, set, str.char32At(0));
110 }
111 
assertInSet(const UnicodeString & localeName,const UnicodeString & setName,const UnicodeSet & set,UChar32 cp)112 void StaticUnicodeSetsTest::assertInSet(const UnicodeString &localeName, const UnicodeString &setName,
113                               const UnicodeSet &set, UChar32 cp) {
114     // If this test case fails, add the specified code point to the corresponding set in either:
115     // - parseLenients in CLDR root.xml
116     // - harded-coded sets in StaticUnicodeSets.java and static_unicode_sets.cpp
117     assertTrue(
118             localeName + UnicodeString(u" ") + UnicodeString(cp) + UnicodeString(u" is missing in ") +
119             setName, set.contains(cp));
120 }
121 
122 
123 #endif
124