xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestCoverage.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.unittest;
2 
3 import com.google.common.collect.ImmutableMultimap;
4 import com.google.common.collect.ImmutableSet;
5 import com.google.common.collect.LinkedHashMultimap;
6 import com.google.common.collect.Multimap;
7 import com.google.common.collect.Sets;
8 import com.google.common.collect.TreeMultimap;
9 import java.util.Collections;
10 import java.util.EnumSet;
11 import java.util.Set;
12 import org.unicode.cldr.test.CoverageLevel2;
13 import org.unicode.cldr.util.CLDRConfig;
14 import org.unicode.cldr.util.CLDRFile;
15 import org.unicode.cldr.util.CLDRLocale;
16 import org.unicode.cldr.util.CoreCoverageInfo;
17 import org.unicode.cldr.util.CoreCoverageInfo.CoreItems;
18 import org.unicode.cldr.util.Factory;
19 import org.unicode.cldr.util.Level;
20 import org.unicode.cldr.util.PathHeader;
21 import org.unicode.cldr.util.StandardCodes;
22 import org.unicode.cldr.util.SupplementalDataInfo;
23 
24 public class TestCoverage extends TestFmwkPlus {
25     private static final boolean DEBUG = false;
26     private static final boolean SHOW_LSR_DATA = false;
27 
28     static final StandardCodes sc = StandardCodes.make();
29     static final CLDRConfig testInfo = CLDRConfig.getInstance();
30     static final SupplementalDataInfo sdi = testInfo.getSupplementalDataInfo();
31 
main(String[] args)32     public static void main(String[] args) {
33         new TestCoverage().run(args);
34     }
35 
36     static Set<CoreItems> all = Collections.unmodifiableSet(EnumSet.allOf(CoreItems.class));
37     static Set<CoreItems> none = Collections.unmodifiableSet(EnumSet.noneOf(CoreItems.class));
38 
TestBasic()39     public void TestBasic() {
40         CLDRFile engCldrFile = testInfo.getEnglish();
41         Multimap<CoreItems, String> errors = LinkedHashMultimap.create();
42         Set<CoreItems> coreCoverage = CoreCoverageInfo.getCoreCoverageInfo(engCldrFile, errors);
43         if (!assertEquals("English should be complete", all, coreCoverage)) {
44             showDiff("Missing", all, coreCoverage);
45         }
46         CLDRFile skimpyLocale = testInfo.getCldrFactory().make("asa", false);
47         errors.clear();
48         coreCoverage = CoreCoverageInfo.getCoreCoverageInfo(skimpyLocale, errors);
49         if (!assertEquals("Skimpy locale should not be complete", none, coreCoverage)) {
50             showDiff("Missing", all, coreCoverage);
51             showDiff("Extra", coreCoverage, none);
52         }
53     }
54 
TestSelected()55     public void TestSelected() {
56         Object[][] tests = {
57             {
58                 "en",
59                 "//ldml/localeDisplayNames/subdivisions/subdivision[@type=\"gbeng\"]",
60                 Level.MODERN,
61                 8
62             },
63             {
64                 "en",
65                 "//ldml/numbers/minimalPairs/ordinalMinimalPairs[@ordinal=\"other\"]",
66                 Level.MODERATE,
67                 20
68             },
69             {
70                 "en",
71                 "//ldml/numbers/minimalPairs/pluralMinimalPairs[@count=\"other\"]",
72                 Level.MODERATE,
73                 20
74             },
75         };
76         PathHeader.Factory phf = PathHeader.getFactory(testInfo.getEnglish());
77         for (Object[] test : tests) {
78             String localeId = (String) test[0];
79             String path = (String) test[1];
80             Level expectedLevel = (Level) test[2];
81             int expectedVotes = (Integer) test[3];
82             CoverageLevel2 coverageLevel = CoverageLevel2.getInstance(sdi, localeId);
83             Level level = coverageLevel.getLevel(path);
84             PathHeader ph = phf.fromPath(path);
85             assertEquals(localeId + " : " + path + " : ", expectedLevel, level);
86             CLDRLocale loc = CLDRLocale.getInstance(localeId);
87             int actualVotes = sdi.getRequiredVotes(loc, ph);
88             assertEquals(localeId + " : " + path + " : ", expectedVotes, actualVotes);
89         }
90     }
91 
92     final ImmutableSet<CoreItems> nonCore =
93             ImmutableSet.copyOf(
94                     Sets.difference(
95                             CoreItems.ALL, Set.copyOf(CoreItems.LEVEL_TO_ITEMS.get(Level.CORE))));
96 
TestLocales()97     public void TestLocales() {
98         long start = System.currentTimeMillis();
99 
100         Factory fullCldrFactory = testInfo.getFullCldrFactory();
101         CLDRFile testFile0 = fullCldrFactory.make("yi", true);
102         checkLocale(testFile0);
103 
104         for (String locale : fullCldrFactory.getAvailable()) {
105             if (CLDRLocale.getInstance(locale).getParent() != CLDRLocale.ROOT) {
106                 continue;
107             }
108             CLDRFile testFile = fullCldrFactory.make(locale, true);
109             checkLocale(testFile);
110         }
111         long end = System.currentTimeMillis();
112         logln("Elapsed:\t" + (end - start));
113     }
114 
checkLocale(CLDRFile testFile)115     public Multimap<CoreItems, String> checkLocale(CLDRFile testFile) {
116         Multimap<CoreItems, String> errors = TreeMultimap.create();
117         String locale = testFile.getLocaleID();
118 
119         try {
120             CoreCoverageInfo.getCoreCoverageInfo(testFile, errors);
121         } catch (Exception e) {
122             errln(
123                     "Failure for locale: "
124                             + getLocaleAndName(locale)
125                             + ", can't access CoreCoverageInfo.getCoreCoverageInfo");
126             e.printStackTrace();
127             return errors;
128         }
129         // Tried errors.removeAll(nonCore), but doesn't remove all keys; rather all values for 1 key
130         for (CoreItems x : nonCore) {
131             errors.removeAll(x);
132         }
133         assertEquals("Core items " + getLocaleAndName(locale), ImmutableMultimap.of(), errors);
134         return errors;
135     }
136 
getLocaleAndName(String locale)137     private String getLocaleAndName(String locale) {
138         return locale + "\t" + testInfo.getEnglish().getName(locale);
139     }
140 
showColumn(Set items)141     private String showColumn(Set items) {
142         StringBuilder result = new StringBuilder();
143         for (CoreItems x : CoreItems.values()) {
144             result.append("\t");
145             if (items.contains(x)) {
146                 result.append(x);
147             }
148         }
149         return result.toString();
150     }
151 
showDiff(String title, Set<CoreItems> all, Set<CoreItems> coreCoverage)152     public void showDiff(String title, Set<CoreItems> all, Set<CoreItems> coreCoverage) {
153         Set<CoreItems> diff = EnumSet.copyOf(all);
154         diff.removeAll(coreCoverage);
155         if (diff.size() != 0) {
156             errln("\t" + title + ": " + diff);
157         }
158     }
159 
testBeaufort()160     public void testBeaufort() {
161         // locale, path, expected coverage
162         String[][] tests = {
163             {
164                 "am",
165                 "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"speed-beaufort\"]/displayName",
166                 "comprehensive"
167             },
168             {
169                 "de",
170                 "//ldml/units/unitLength[@type=\"long\"]/unit[@type=\"speed-beaufort\"]/displayName",
171                 "modern"
172             },
173         };
174         for (String[] test : tests) {
175             String locale = test[0];
176             String path = test[1];
177             Level expected = Level.fromString(test[2]);
178             CoverageLevel2 coverageLevel = CoverageLevel2.getInstance(sdi, locale);
179             Level actual = coverageLevel.getLevel(path);
180             assertEquals(String.format("locale:%s, path:%s", locale, path), expected, actual);
181         }
182     }
183 }
184