• 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 = nullptr) override;
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     grouping.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         const char* locName = locale.getName();
77         DecimalFormatSymbols dfs(locale, status);
78         UnicodeString localeName;
79         locale.getDisplayName(localeName);
80         assertSuccess(UnicodeString("Making DFS for ") + localeName, status);
81 
82 #define ASSERT_IN_SET(name, foo) assertInSet(localeName, UnicodeString("" #name ""), name, foo)
83 
84         ASSERT_IN_SET(decimals, dfs.getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
85         if (locName==nullptr || uprv_strncmp(locName,"nqo",3) != 0 ||
86                 !logKnownIssue("CLDR-17023", "Number symbols and/or parseLenients messed up for N’Ko")) {
87             ASSERT_IN_SET(grouping, dfs.getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
88         }
89         ASSERT_IN_SET(plusSign, dfs.getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol));
90         ASSERT_IN_SET(minusSign, dfs.getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol));
91         ASSERT_IN_SET(percent, dfs.getConstSymbol(DecimalFormatSymbols::kPercentSymbol));
92         ASSERT_IN_SET(permille, dfs.getConstSymbol(DecimalFormatSymbols::kPerMillSymbol));
93         ASSERT_IN_SET(infinity, dfs.getConstSymbol(DecimalFormatSymbols::kInfinitySymbol));
94     }
95 }
96 
testNonEmpty()97 void StaticUnicodeSetsTest::testNonEmpty() {
98     for (int32_t i=0; i<unisets::UNISETS_KEY_COUNT; i++) {
99         if (i == unisets::EMPTY) {
100             continue;
101         }
102         const UnicodeSet* uset = get(static_cast<unisets::Key>(i));
103         // Can fail if no data:
104         assertFalse(UnicodeString("Set should not be empty: ") + i, uset->isEmpty(), false, true);
105     }
106 }
107 
assertInSet(const UnicodeString & localeName,const UnicodeString & setName,const UnicodeSet & set,const UnicodeString & str)108 void StaticUnicodeSetsTest::assertInSet(const UnicodeString &localeName, const UnicodeString &setName,
109                               const UnicodeSet &set, const UnicodeString &str) {
110     if (str.countChar32(0, str.length()) != 1) {
111         // Ignore locale strings with more than one code point (usually a bidi mark)
112         return;
113     }
114     assertInSet(localeName, setName, set, str.char32At(0));
115 }
116 
assertInSet(const UnicodeString & localeName,const UnicodeString & setName,const UnicodeSet & set,UChar32 cp)117 void StaticUnicodeSetsTest::assertInSet(const UnicodeString &localeName, const UnicodeString &setName,
118                               const UnicodeSet &set, UChar32 cp) {
119     // If this test case fails, add the specified code point to the corresponding set in either:
120     // - parseLenients in CLDR root.xml
121     // - harded-coded sets in StaticUnicodeSets.java and static_unicode_sets.cpp
122     assertTrue(
123             localeName + UnicodeString(u" ") + UnicodeString(cp) + UnicodeString(u" is missing in ") +
124             setName, set.contains(cp));
125 }
126 
127 
128 #endif
129