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.Ignore;
     15 import org.junit.Test;
     16 import org.junit.runner.RunWith;
     17 import org.junit.runners.JUnit4;
     18 
     19 import android.icu.dev.test.TestFmwk;
     20 import android.icu.impl.SimpleFormatterImpl;
     21 import android.icu.impl.StandardPlural;
     22 import android.icu.text.MeasureFormat;
     23 import android.icu.text.MeasureFormat.FormatWidth;
     24 import android.icu.text.PluralRanges;
     25 import android.icu.text.PluralRules.Factory;
     26 import android.icu.util.Currency;
     27 import android.icu.util.Measure;
     28 import android.icu.util.MeasureUnit;
     29 import android.icu.util.ULocale;
     30 import android.icu.testsharding.MainTestShard;
     31 
     32 /**
     33  * @author markdavis
     34  *
     35  */
     36 @MainTestShard
     37 @RunWith(JUnit4.class)
     38 public class PluralRangesTest extends TestFmwk {
     39     @Test
     40     public void TestLocaleData() {
     41         String[][] tests = {
     42                 {"de", "other", "one", "one"},
     43                 {"xxx", "few", "few", "few" },
     44                 {"de", "one", "other", "other"},
     45                 {"de", "other", "one", "one"},
     46                 {"de", "other", "other", "other"},
     47                 {"ro", "one", "few", "few"},
     48                 {"ro", "one", "other", "other"},
     49                 {"ro", "few", "one", "few"},
     50         };
     51         for (String[] test : tests) {
     52             final ULocale locale = new ULocale(test[0]);
     53             final StandardPlural start = StandardPlural.fromString(test[1]);
     54             final StandardPlural end = StandardPlural.fromString(test[2]);
     55             final StandardPlural expected = StandardPlural.fromString(test[3]);
     56             final PluralRanges pluralRanges = Factory.getDefaultFactory().getPluralRanges(locale);
     57 
     58             StandardPlural actual = pluralRanges.get(start, end);
     59             assertEquals("Deriving range category", expected, actual);
     60         }
     61     }
     62 
     63     @Test
     64     public void TestRangePattern() {
     65         String[][] tests = {
     66                 {"de", "SHORT", "{0}{1}"},
     67                 {"ja", "NARROW", "{0}{1}"},
     68         };
     69         for (String[] test : tests) {
     70             ULocale ulocale = new ULocale(test[0]);
     71             FormatWidth width = FormatWidth.valueOf(test[1]);
     72             String expected = test[2];
     73             String formatter = MeasureFormat.getRangeFormat(ulocale, width);
     74             String actual = SimpleFormatterImpl.formatCompiledPattern(formatter, "{0}", "{1}");
     75             assertEquals("range pattern " + Arrays.asList(test), expected, actual);
     76         }
     77     }
     78 
     79     // TODO: Re-enable this test when #12454 is fixed.
     80     @Ignore("http://bugs.icu-project.org/trac/ticket/12454")
     81     @Test
     82     public void TestFormatting() {
     83         Object[][] tests = {
     84                 {0.0, 1.0, ULocale.FRANCE, FormatWidth.WIDE, MeasureUnit.FAHRENHEIT, "01 degr Fahrenheit"},
     85                 {1.0, 2.0, ULocale.FRANCE, FormatWidth.WIDE, MeasureUnit.FAHRENHEIT, "12 degrs Fahrenheit"},
     86                 {3.1, 4.25, ULocale.FRANCE, FormatWidth.SHORT, MeasureUnit.FAHRENHEIT, "3,14,25 F"},
     87                 {3.1, 4.25, ULocale.ENGLISH, FormatWidth.SHORT, MeasureUnit.FAHRENHEIT, "3.14.25F"},
     88                 {3.1, 4.25, ULocale.CHINESE, FormatWidth.WIDE, MeasureUnit.INCH, "3.1-4.25"},
     89                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.WIDE, MeasureUnit.INCH, "01 inches"},
     90 
     91                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.NARROW, Currency.getInstance("EUR"), "0.001.00"},
     92                 {0.0, 1.0, ULocale.FRENCH, FormatWidth.NARROW, Currency.getInstance("EUR"), "0,001,00"},
     93                 {0.0, 100.0, ULocale.FRENCH, FormatWidth.NARROW, Currency.getInstance("JPY"), "0100\u00a0JPY"},
     94 
     95                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.SHORT, Currency.getInstance("EUR"), "EUR0.001.00"},
     96                 {0.0, 1.0, ULocale.FRENCH, FormatWidth.SHORT, Currency.getInstance("EUR"), "0,001,00\u00a0EUR"},
     97                 {0.0, 100.0, ULocale.FRENCH, FormatWidth.SHORT, Currency.getInstance("JPY"), "0100\u00a0JPY"},
     98 
     99                 {0.0, 1.0, ULocale.ENGLISH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0.001.00 euros"},
    100                 {0.0, 1.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0,001,00 euro"},
    101                 {0.0, 2.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0,002,00 euros"},
    102                 {0.0, 100.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("JPY"), "0100 yens japonais"},
    103         };
    104         int i = 0;
    105         for (Object[] test : tests) {
    106             ++i;
    107             double low = (Double) test[0];
    108             double high = (Double) test[1];
    109             final ULocale locale = (ULocale) test[2];
    110             final FormatWidth width = (FormatWidth) test[3];
    111             final MeasureUnit unit = (MeasureUnit) test[4];
    112             final Object expected = test[5];
    113 
    114             MeasureFormat mf = MeasureFormat.getInstance(locale, width);
    115             Object actual;
    116             try {
    117                 // TODO: Fix this when range formatting is added again.
    118                 // To let the code compile, the following line does list formatting.
    119                 actual = mf.formatMeasures(new Measure(low, unit), new Measure(high, unit));
    120             } catch (Exception e) {
    121                 actual = e.getClass();
    122             }
    123             assertEquals(i + " Formatting unit", expected, actual);
    124         }
    125     }
    126 
    127     @Test
    128     public void TestBasic() {
    129         PluralRanges a = new PluralRanges();
    130         a.add(StandardPlural.ONE, StandardPlural.OTHER, StandardPlural.ONE);
    131         StandardPlural actual = a.get(StandardPlural.ONE, StandardPlural.OTHER);
    132         assertEquals("range", StandardPlural.ONE, actual);
    133         a.freeze();
    134         try {
    135             a.add(StandardPlural.ONE, StandardPlural.ONE, StandardPlural.ONE);
    136             errln("Failed to cause exception on frozen instance");
    137         } catch (UnsupportedOperationException e) {
    138         }
    139     }
    140 }
    141