Home | History | Annotate | Download | only in format
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2008-2016, International Business Machines Corporation and
      7  * others. All Rights Reserved.
      8  *******************************************************************************
      9  */
     10 package android.icu.dev.test.format;
     11 
     12 import java.util.Arrays;
     13 
     14 import org.junit.Test;
     15 import org.junit.runner.RunWith;
     16 import org.junit.runners.JUnit4;
     17 
     18 import android.icu.dev.test.TestFmwk;
     19 import android.icu.impl.SimpleFormatterImpl;
     20 import android.icu.impl.StandardPlural;
     21 import android.icu.text.MeasureFormat;
     22 import android.icu.text.MeasureFormat.FormatWidth;
     23 import android.icu.text.PluralRanges;
     24 import android.icu.text.PluralRules.Factory;
     25 import android.icu.util.Currency;
     26 import android.icu.util.Measure;
     27 import android.icu.util.MeasureUnit;
     28 import android.icu.util.ULocale;
     29 import android.icu.testsharding.MainTestShard;
     30 
     31 /**
     32  * @author markdavis
     33  *
     34  */
     35 @MainTestShard
     36 @RunWith(JUnit4.class)
     37 public class PluralRangesTest extends TestFmwk {
     38     @Test
     39     public void TestLocaleData() {
     40         String[][] tests = {
     41                 {"de", "other", "one", "one"},
     42                 {"xxx", "few", "few", "few" },
     43                 {"de", "one", "other", "other"},
     44                 {"de", "other", "one", "one"},
     45                 {"de", "other", "other", "other"},
     46                 {"ro", "one", "few", "few"},
     47                 {"ro", "one", "other", "other"},
     48                 {"ro", "few", "one", "few"},
     49         };
     50         for (String[] test : tests) {
     51             final ULocale locale = new ULocale(test[0]);
     52             final StandardPlural start = StandardPlural.fromString(test[1]);
     53             final StandardPlural end = StandardPlural.fromString(test[2]);
     54             final StandardPlural expected = StandardPlural.fromString(test[3]);
     55             final PluralRanges pluralRanges = Factory.getDefaultFactory().getPluralRanges(locale);
     56 
     57             StandardPlural actual = pluralRanges.get(start, end);
     58             assertEquals("Deriving range category", expected, actual);
     59         }
     60     }
     61 
     62     @Test
     63     public void TestRangePattern() {
     64         String[][] tests = {
     65                 {"de", "SHORT", "{0}{1}"},
     66                 {"ja", "NARROW", "{0}{1}"},
     67         };
     68         for (String[] test : tests) {
     69             ULocale ulocale = new ULocale(test[0]);
     70             FormatWidth width = FormatWidth.valueOf(test[1]);
     71             String expected = test[2];
     72             String formatter = MeasureFormat.getRangeFormat(ulocale, width);
     73             String actual = SimpleFormatterImpl.formatCompiledPattern(formatter, "{0}", "{1}");
     74             assertEquals("range pattern " + Arrays.asList(test), expected, actual);
     75         }
     76     }
     77 
     78     @Test
     79     public void TestFormatting() {
     80         Object[][] tests = {
     81                 {0.0, 1.0, ULocale.FRANCE, FormatWidth.WIDE, MeasureUnit.FAHRENHEIT, "01 degr Fahrenheit"},
     82                 {1.0, 2.0, ULocale.FRANCE, FormatWidth.WIDE, MeasureUnit.FAHRENHEIT, "12 degrs Fahrenheit"},
     83                 {3.1, 4.25, ULocale.FRANCE, FormatWidth.SHORT, MeasureUnit.FAHRENHEIT, "3,14,25 F"},
     84                 {3.1, 4.25, ULocale.ENGLISH, FormatWidth.SHORT, MeasureUnit.FAHRENHEIT, "3.14.25F"},
     85                 {3.1, 4.25, ULocale.CHINESE, FormatWidth.WIDE, MeasureUnit.INCH, "3.1-4.25"},
     86                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.WIDE, MeasureUnit.INCH, "01 inches"},
     87 
     88                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.NARROW, Currency.getInstance("EUR"), "0.001.00"},
     89                 {0.0, 1.0, ULocale.FRENCH, FormatWidth.NARROW, Currency.getInstance("EUR"), "0,001,00"},
     90                 {0.0, 100.0, ULocale.FRENCH, FormatWidth.NARROW, Currency.getInstance("JPY"), "0100\u00a0JPY"},
     91 
     92                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.SHORT, Currency.getInstance("EUR"), "EUR0.001.00"},
     93                 {0.0, 1.0, ULocale.FRENCH, FormatWidth.SHORT, Currency.getInstance("EUR"), "0,001,00\u00a0EUR"},
     94                 {0.0, 100.0, ULocale.FRENCH, FormatWidth.SHORT, Currency.getInstance("JPY"), "0100\u00a0JPY"},
     95 
     96                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0.001.00 euros"},
     97                 {0.0, 1.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0,001,00 euro"},
     98                 {0.0, 2.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0,002,00 euros"},
     99                 {0.0, 100.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("JPY"), "0100 yens japonais"},
    100         };
    101         int i = 0;
    102         for (Object[] test : tests) {
    103             ++i;
    104             double low = (Double) test[0];
    105             double high = (Double) test[1];
    106             final ULocale locale = (ULocale) test[2];
    107             final FormatWidth width = (FormatWidth) test[3];
    108             final MeasureUnit unit = (MeasureUnit) test[4];
    109             final Object expected = test[5];
    110 
    111             MeasureFormat mf = MeasureFormat.getInstance(locale, width);
    112             Object actual;
    113             try {
    114                 actual = mf.formatMeasureRange(new Measure(low, unit), new Measure(high, unit));
    115             } catch (Exception e) {
    116                 actual = e.getClass();
    117             }
    118             assertEquals(i + " Formatting unit", expected, actual);
    119         }
    120     }
    121 
    122     @Test
    123     public void TestBasic() {
    124         PluralRanges a = new PluralRanges();
    125         a.add(StandardPlural.ONE, StandardPlural.OTHER, StandardPlural.ONE);
    126         StandardPlural actual = a.get(StandardPlural.ONE, StandardPlural.OTHER);
    127         assertEquals("range", StandardPlural.ONE, actual);
    128         a.freeze();
    129         try {
    130             a.add(StandardPlural.ONE, StandardPlural.ONE, StandardPlural.ONE);
    131             errln("Failed to cause exception on frozen instance");
    132         } catch (UnsupportedOperationException e) {
    133         }
    134     }
    135 }
    136