Home | History | Annotate | Download | only in unittest
      1 package org.unicode.cldr.unittest;
      2 
      3 import java.util.HashSet;
      4 import java.util.Set;
      5 import java.util.TreeSet;
      6 
      7 import org.unicode.cldr.util.CLDRConfig;
      8 import org.unicode.cldr.util.SupplementalDataInfo;
      9 import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo;
     10 import org.unicode.cldr.util.SupplementalDataInfo.PluralType;
     11 
     12 import com.ibm.icu.text.PluralRules;
     13 import com.ibm.icu.text.PluralRules.SampleType;
     14 
     15 public class TestPluralRuleGeneration extends TestFmwkPlus {
     16     public static void main(String[] args) {
     17         new TestPluralRuleGeneration().run(args);
     18     }
     19 
     20     public void TestGeneration() {
     21         /*
     22          * <pluralRules locales="am bn fa gu hi kn mr zu"> <pluralRule
     23          * count="one">i = 0 or n = 1 @integer 0, 1 @decimal 0.0~1.0,
     24          * 0.00~0.04</pluralRule> <pluralRule count="other"> @integer 2~17, 100,
     25          * 1000, 10000, 100000, 1000000,  @decimal 1.1~2.6, 10.0, 100.0,
     26          * 1000.0, 10000.0, 100000.0, 1000000.0, </pluralRule> </pluralRules>
     27          */
     28         PluralRules rules = PluralRules.createRules("one:i = 0 or n = 1");
     29         boolean i = rules.computeLimited("one", SampleType.INTEGER);
     30         boolean d = rules.computeLimited("one", SampleType.DECIMAL);
     31         System.out.println(i + ", " + d);
     32     }
     33 
     34     public void TestFilipino() {
     35         /*
     36          * <pluralRules locales="am bn fa gu hi kn mr zu"> <pluralRule
     37          * count="one">i = 0 or n = 1 @integer 0, 1 @decimal 0.0~1.0,
     38          * 0.00~0.04</pluralRule> <pluralRule count="other"> @integer 2~17, 100,
     39          * 1000, 10000, 100000, 1000000,  @decimal 1.1~2.6, 10.0, 100.0,
     40          * 1000.0, 10000.0, 100000.0, 1000000.0, </pluralRule> </pluralRules>
     41          */
     42         PluralRules rules = PluralRules
     43             .createRules("one:v = 0 and i = 1,2,3 or v = 0 and i % 10 != 4,6,9 or v != 0 and f % 10 != 4,6,9");
     44         Object[][] test = { { 1, "one" }, { 3, "one" }, { 5, "one" },
     45             { 15, "one" }, { 4.5, "one" }, { 4, "other" },
     46             { 1.4, "other" }, };
     47     }
     48 
     49     public void TestAtoms() {
     50         CLDRConfig testInfo = CLDRConfig.getInstance();
     51         SupplementalDataInfo supp = testInfo.getSupplementalDataInfo();
     52         Set<PluralRules> seen = new HashSet();
     53         Set<String> atoms = new TreeSet();
     54         Set<String> limitedSet = new TreeSet();
     55         for (PluralType type : SupplementalDataInfo.PluralType.values()) {
     56             for (String locale : supp.getPluralLocales(type)) {
     57                 PluralInfo info = supp.getPlurals(locale, false);
     58                 if (info == null) {
     59                     continue;
     60                 }
     61                 PluralRules rules = info.getPluralRules();
     62                 if (seen.contains(rules)) {
     63                     continue;
     64                 }
     65                 seen.add(rules);
     66                 for (String keyword : rules.getKeywords()) {
     67                     String rulesString = rules.getRules(keyword);
     68                     if (rulesString.isEmpty()) {
     69                         continue;
     70                     }
     71 
     72                     for (String orPart : rulesString.split("\\s*or\\s*")) {
     73                         for (String andPart : orPart.split("\\s*and\\s*")) {
     74                             PluralRules rules2 = PluralRules
     75                                 .createRules("one: " + andPart);
     76                             boolean i = rules.computeLimited("one",
     77                                 SampleType.INTEGER);
     78                             boolean d = rules.computeLimited("one",
     79                                 SampleType.DECIMAL);
     80                             limitedSet.add((i ? "in" : "i") + " "
     81                                 + (d ? "dn" : "d") + " : " + andPart);
     82                         }
     83                     }
     84                 }
     85             }
     86         }
     87         for (String item : limitedSet) {
     88             System.out.println(item);
     89         }
     90 
     91     }
     92 }