]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/static_unisets_test.cpp
ICU-62109.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / static_unisets_test.cpp
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
28 extern IntlTest *createStaticUnicodeSetsTest() {
29 return new StaticUnicodeSetsTest();
30 }
31
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 TESTCASE_AUTO(testSetCoverage);
38 TESTCASE_AUTO(testNonEmpty);
39 TESTCASE_AUTO_END;
40 }
41
42 void StaticUnicodeSetsTest::testSetCoverage() {
43 UErrorCode status = U_ZERO_ERROR;
44
45 // Lenient comma/period should be supersets of strict comma/period;
46 // it also makes the coverage logic cheaper.
47 assertTrue(
48 "COMMA should be superset of STRICT_COMMA",
49 get(unisets::COMMA)->containsAll(*get(unisets::STRICT_COMMA)));
50 assertTrue(
51 "PERIOD should be superset of STRICT_PERIOD",
52 get(unisets::PERIOD)->containsAll(*get(unisets::STRICT_PERIOD)));
53
54 UnicodeSet decimals;
55 decimals.addAll(*get(unisets::STRICT_COMMA));
56 decimals.addAll(*get(unisets::STRICT_PERIOD));
57 decimals.freeze();
58 UnicodeSet grouping;
59 grouping.addAll(decimals);
60 grouping.addAll(*get(unisets::OTHER_GROUPING_SEPARATORS));
61 decimals.freeze();
62
63 const UnicodeSet &plusSign = *get(unisets::PLUS_SIGN);
64 const UnicodeSet &minusSign = *get(unisets::MINUS_SIGN);
65 const UnicodeSet &percent = *get(unisets::PERCENT_SIGN);
66 const UnicodeSet &permille = *get(unisets::PERMILLE_SIGN);
67 const UnicodeSet &infinity = *get(unisets::INFINITY_KEY);
68
69 int32_t localeCount;
70 const Locale* allAvailableLocales = Locale::getAvailableLocales(localeCount);
71 for (int32_t i = 0; i < localeCount; i++) {
72 Locale locale = allAvailableLocales[i];
73 DecimalFormatSymbols dfs(locale, status);
74 UnicodeString localeName;
75 locale.getDisplayName(localeName);
76 assertSuccess(UnicodeString("Making DFS for ") + localeName, status);
77
78 #define ASSERT_IN_SET(name, foo) assertInSet(localeName, UnicodeString("" #name ""), name, foo)
79 ASSERT_IN_SET(decimals, dfs.getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
80 ASSERT_IN_SET(grouping, dfs.getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
81 ASSERT_IN_SET(plusSign, dfs.getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol));
82 ASSERT_IN_SET(minusSign, dfs.getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol));
83 ASSERT_IN_SET(percent, dfs.getConstSymbol(DecimalFormatSymbols::kPercentSymbol));
84 ASSERT_IN_SET(permille, dfs.getConstSymbol(DecimalFormatSymbols::kPerMillSymbol));
85 ASSERT_IN_SET(infinity, dfs.getConstSymbol(DecimalFormatSymbols::kInfinitySymbol));
86 }
87 }
88
89 void StaticUnicodeSetsTest::testNonEmpty() {
90 for (int32_t i=0; i<unisets::COUNT; i++) {
91 if (i == unisets::EMPTY) {
92 continue;
93 }
94 const UnicodeSet* uset = get(static_cast<unisets::Key>(i));
95 // Can fail if no data:
96 assertFalse(UnicodeString("Set should not be empty: ") + i, uset->isEmpty(), FALSE, TRUE);
97 }
98 }
99
100 void StaticUnicodeSetsTest::assertInSet(const UnicodeString &localeName, const UnicodeString &setName,
101 const UnicodeSet &set, const UnicodeString &str) {
102 if (str.countChar32(0, str.length()) != 1) {
103 // Ignore locale strings with more than one code point (usually a bidi mark)
104 return;
105 }
106 assertInSet(localeName, setName, set, str.char32At(0));
107 }
108
109 void StaticUnicodeSetsTest::assertInSet(const UnicodeString &localeName, const UnicodeString &setName,
110 const UnicodeSet &set, UChar32 cp) {
111 // If this test case fails, add the specified code point to the corresponding set in
112 // UnicodeSetStaticCache.java and numparse_unisets.cpp
113 assertTrue(
114 localeName + UnicodeString(u" ") + UnicodeString(cp) + UnicodeString(u" is missing in ") +
115 setName, set.contains(cp));
116 }
117
118
119 #endif