1 package org.unicode.cldr.unittest; 2 3 import com.ibm.icu.impl.Row; 4 import com.ibm.icu.impl.Row.R2; 5 import com.ibm.icu.impl.Row.R3; 6 import java.util.Arrays; 7 import java.util.HashSet; 8 import java.util.Iterator; 9 import java.util.List; 10 import java.util.Set; 11 import java.util.TreeSet; 12 import org.unicode.cldr.util.CLDRConfig; 13 import org.unicode.cldr.util.CLDRFile; 14 import org.unicode.cldr.util.DayPeriodInfo; 15 import org.unicode.cldr.util.DayPeriodInfo.DayPeriod; 16 import org.unicode.cldr.util.DayPeriodInfo.Type; 17 import org.unicode.cldr.util.Factory; 18 import org.unicode.cldr.util.SupplementalDataInfo; 19 import org.unicode.cldr.util.XPathParts; 20 21 public class TestDayPeriods extends TestFmwkPlus { 22 23 CLDRConfig CONFIG = CLDRConfig.getInstance(); 24 SupplementalDataInfo SUPPLEMENTAL = CONFIG.getSupplementalDataInfo(); 25 main(String[] args)26 public static void main(String[] args) { 27 new TestDayPeriods().run(args); 28 } 29 30 @SuppressWarnings("unchecked") TestBasicDayPeriods()31 public void TestBasicDayPeriods() { 32 int count = 0; 33 for (String locale : SUPPLEMENTAL.getDayPeriodLocales(DayPeriodInfo.Type.format)) { 34 DayPeriodInfo dayPeriodFormat = 35 SUPPLEMENTAL.getDayPeriods(DayPeriodInfo.Type.format, locale); 36 DayPeriodInfo dayPeriodSelection = 37 SUPPLEMENTAL.getDayPeriods(DayPeriodInfo.Type.selection, locale); 38 39 Set<R2<Integer, DayPeriod>> sortedFormat = getSet(dayPeriodFormat); 40 Set<R2<Integer, DayPeriod>> sortedSelection = getSet(dayPeriodSelection); 41 42 assertRelation( 43 locale + " format ⊇ selection", 44 true, 45 sortedFormat, 46 CONTAINS_ALL, 47 sortedSelection); // 48 logln( 49 locale 50 + "\t" 51 + CONFIG.getEnglish().getName(locale) 52 + "\t" 53 + dayPeriodFormat 54 + "\t" 55 + dayPeriodSelection); 56 count += dayPeriodFormat.getPeriodCount(); 57 } 58 assertTrue("At least some day periods exist", count > 5); 59 } 60 getSet(DayPeriodInfo dayPeriodInfo)61 private Set<R2<Integer, DayPeriod>> getSet(DayPeriodInfo dayPeriodInfo) { 62 Set<R2<Integer, DayPeriod>> sorted = new TreeSet<>(); 63 for (int i = 0; i < dayPeriodInfo.getPeriodCount(); ++i) { 64 R3<Integer, Boolean, DayPeriod> info = dayPeriodInfo.getPeriod(i); 65 int start = info.get0(); 66 assertEquals( 67 "Time is even hours", start / DayPeriodInfo.HOUR * DayPeriodInfo.HOUR, start); 68 R2<Integer, DayPeriod> row = Row.of(start, info.get2()); 69 sorted.add(row); 70 } 71 return sorted; 72 } 73 74 @SuppressWarnings("unchecked") TestAttributes()75 public void TestAttributes() { 76 Factory factory = CONFIG.getCldrFactory(); 77 HashSet<String> AMPM = new HashSet<>(Arrays.asList("am", "pm")); 78 for (String locale : factory.getAvailableLanguages()) { 79 DayPeriodInfo periodInfo = SUPPLEMENTAL.getDayPeriods(Type.format, locale); 80 List<DayPeriod> periods = periodInfo.getPeriods(); 81 CLDRFile cldrFile = CONFIG.getCLDRFile(locale, false); 82 for (Iterator<String> it = 83 cldrFile.iterator( 84 "//ldml/dates/calendars/calendar[@type=\"gregorian\"]/dayPeriods/"); 85 it.hasNext(); ) { 86 String path = it.next(); 87 if (path.endsWith("alias")) { 88 continue; 89 } 90 XPathParts parts = XPathParts.getFrozenInstance(path); 91 String type = parts.getAttributeValue(-1, "type"); 92 if (AMPM.contains(type)) { 93 continue; 94 } 95 DayPeriod period; 96 try { 97 period = DayPeriodInfo.DayPeriod.fromString(type); 98 } catch (Exception e) { 99 assertNull(locale + " : " + type, e); 100 continue; 101 } 102 assertRelation(locale, true, periods, TestFmwkPlus.CONTAINS, period); 103 } 104 } 105 } 106 } 107