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) 2001-2011, International Business Machines Corporation and    *
      7  * others. All Rights Reserved.                                                *
      8  *******************************************************************************
      9  */
     10 
     11 /**
     12  * Port From:   ICU4C v1.8.1 : format : IntlTestNumberFormat
     13  * Source File: $ICU4CRoot/source/test/intltest/tsnmfmt.cpp
     14  **/
     15 
     16 package android.icu.dev.test.format;
     17 import java.util.Locale;
     18 import java.util.Random;
     19 
     20 import org.junit.Test;
     21 import org.junit.runner.RunWith;
     22 import org.junit.runners.JUnit4;
     23 
     24 import android.icu.dev.test.TestFmwk;
     25 import android.icu.text.DecimalFormat;
     26 import android.icu.text.NumberFormat;
     27 import android.icu.testsharding.MainTestShard;
     28 
     29 /**
     30  * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
     31  * NumberFormat.
     32  */
     33 @MainTestShard
     34 @RunWith(JUnit4.class)
     35 public class IntlTestNumberFormat extends TestFmwk {
     36 
     37     public NumberFormat fNumberFormat;
     38 
     39     /**
     40      * Internal use
     41      */
     42     private void _testLocale(Locale locale) {
     43         String localeName = locale + " (" + locale.getDisplayName() + ")";
     44 
     45         logln("Number test " + localeName);
     46         fNumberFormat = NumberFormat.getInstance(locale);
     47         _testFormat();
     48 
     49         logln("Currency test " + localeName);
     50         fNumberFormat = NumberFormat.getCurrencyInstance(locale);
     51         _testFormat();
     52 
     53         logln("Percent test " + localeName);
     54         fNumberFormat = NumberFormat.getPercentInstance(locale);
     55         _testFormat();
     56 
     57         if (locale.toString().compareTo("en_US_POSIX") != 0 ) {
     58            logln("Scientific test " + localeName);
     59            fNumberFormat = NumberFormat.getScientificInstance(locale);
     60            _testFormat();
     61         }
     62     }
     63 
     64     /**
     65      * call _testFormat for currency, percent and plain number instances
     66      */
     67     @Test
     68     public void TestLocale() {
     69         Locale locale = Locale.getDefault();
     70         String localeName = locale + " (" + locale.getDisplayName() + ")";
     71 
     72         logln("Number test " + localeName);
     73         fNumberFormat = NumberFormat.getInstance(locale);
     74         _testFormat();
     75 
     76         logln("Currency test " + localeName);
     77         fNumberFormat = NumberFormat.getCurrencyInstance(locale);
     78         _testFormat();
     79 
     80         logln("Percent test " + localeName);
     81         fNumberFormat = NumberFormat.getPercentInstance(locale);
     82         _testFormat();
     83     }
     84 
     85     /**
     86      * call tryIt with many variations, called by testLocale
     87      */
     88     private void _testFormat() {
     89 
     90         if (fNumberFormat == null){
     91             errln("**** FAIL: Null format returned by createXxxInstance.");
     92              return;
     93         }
     94         DecimalFormat s = (DecimalFormat)fNumberFormat;
     95         logln("pattern :" + s.toPattern());
     96 
     97         tryIt(-2.02147304840132e-68);
     98         tryIt(3.88057859588817e-68);
     99         tryIt(-2.64651110485945e+65);
    100         tryIt(9.29526819488338e+64);
    101 
    102         tryIt(-2.02147304840132e-100);
    103         tryIt(3.88057859588817e-096);
    104         tryIt(-2.64651110485945e+306);
    105         tryIt(9.29526819488338e+250);
    106 
    107         tryIt(-9.18228054496402e+64);
    108         tryIt(-9.69413034454191e+64);
    109 
    110         tryIt(-9.18228054496402e+255);
    111         tryIt(-9.69413034454191e+273);
    112 
    113 
    114         tryIt(1.234e-200);
    115         tryIt(-2.3e-168);
    116 
    117         tryIt(Double.NaN);
    118         tryIt(Double.POSITIVE_INFINITY);
    119         tryIt(Double.NEGATIVE_INFINITY);
    120 
    121         tryIt(251887531);
    122         tryIt(5e-20 / 9);
    123         tryIt(5e20 / 9);
    124         tryIt(1.234e-50);
    125         tryIt(9.99999999999996);
    126         tryIt(9.999999999999996);
    127 
    128         tryIt(5.06e-27);
    129 
    130         tryIt(Integer.MIN_VALUE);
    131         tryIt(Integer.MAX_VALUE);
    132         tryIt((double)Integer.MIN_VALUE);
    133         tryIt((double)Integer.MAX_VALUE);
    134         tryIt(Integer.MIN_VALUE - 1.0);
    135         tryIt(Integer.MAX_VALUE + 1.0);
    136 
    137         tryIt(5.0 / 9.0 * 1e-20);
    138         tryIt(4.0 / 9.0 * 1e-20);
    139         tryIt(5.0 / 9.0 * 1e+20);
    140         tryIt(4.0 / 9.0 * 1e+20);
    141 
    142         tryIt(2147483647.);
    143         tryIt(0);
    144         tryIt(0.0);
    145         tryIt(1);
    146         tryIt(10);
    147         tryIt(100);
    148         tryIt(-1);
    149         tryIt(-10);
    150         tryIt(-100);
    151         tryIt(-1913860352);
    152 
    153         Random random = createRandom(); // use test framework's random seed
    154         for (int j = 0; j < 10; j++) {
    155             double d = random.nextDouble()*2e10 - 1e10;
    156             tryIt(d);
    157 
    158         }
    159     }
    160 
    161     /**
    162      * Perform tests using aNumber and fNumberFormat, called in many variations
    163      */
    164     public void tryIt(double aNumber) {
    165         final int DEPTH = 10;
    166         double[] number = new double[DEPTH];
    167         String[] string = new String[DEPTH];
    168         int numberMatch = 0;
    169         int stringMatch = 0;
    170         boolean dump = false;
    171         int i;
    172 
    173         for (i = 0; i < DEPTH; i++) {
    174             if (i == 0) {
    175                 number[i] = aNumber;
    176             } else {
    177                 try {
    178                     number[i - 1] = fNumberFormat.parse(string[i - 1]).doubleValue();
    179                 } catch(java.text.ParseException pe) {
    180                     errln("**** FAIL: Parse of " + string[i-1] + " failed.");
    181                     dump = true;
    182                     break;
    183                 }
    184             }
    185 
    186             string[i] = fNumberFormat.format(number[i]);
    187             if (i > 0)
    188             {
    189                 if (numberMatch == 0 && number[i] == number[i-1])
    190                     numberMatch = i;
    191                 else if (numberMatch > 0 && number[i] != number[i-1])
    192                 {
    193                     errln("**** FAIL: Numeric mismatch after match.");
    194                     dump = true;
    195                     break;
    196                 }
    197                 if (stringMatch == 0 && string[i] == string[i-1])
    198                     stringMatch = i;
    199                 else if (stringMatch > 0 && string[i] != string[i-1])
    200                 {
    201                     errln("**** FAIL: String mismatch after match.");
    202                     dump = true;
    203                     break;
    204                 }
    205             }
    206             if (numberMatch > 0 && stringMatch > 0)
    207                 break;
    208 
    209             if (i == DEPTH)
    210             --i;
    211 
    212         if (stringMatch > 2 || numberMatch > 2)
    213         {
    214             errln("**** FAIL: No string and/or number match within 2 iterations.");
    215             dump = true;
    216         }
    217 
    218         if (dump)
    219         {
    220             for (int k=0; k<=i; ++k)
    221             {
    222                 logln(k + ": " + number[k] + " F> " +
    223                       string[k] + " P> ");
    224             }
    225         }
    226         }
    227     }
    228 
    229     /**
    230      *  perform tests using aNumber and fNumberFormat, called in many variations
    231      **/
    232     public void tryIt(int aNumber) {
    233         long number;
    234 
    235         String stringNum = fNumberFormat.format(aNumber);
    236         try {
    237             number = fNumberFormat.parse(stringNum).longValue();
    238         } catch (java.text.ParseException pe) {
    239             errln("**** FAIL: Parse of " + stringNum + " failed.");
    240             return;
    241         }
    242 
    243         if (number != aNumber) {
    244             errln("**** FAIL: Parse of " + stringNum + " failed. Got:" + number
    245                 + " Expected:" + aNumber);
    246         }
    247 
    248     }
    249 
    250     /**
    251      *  test NumberFormat::getAvailableLocales
    252      **/
    253     @Test
    254     public void TestAvailableLocales() {
    255         final Locale[] locales = NumberFormat.getAvailableLocales();
    256         int count = locales.length;
    257         logln(count + " available locales");
    258         if (count != 0)
    259         {
    260             String all = "";
    261             for (int i = 0; i< count; ++i)
    262             {
    263                 if (i!=0)
    264                     all += ", ";
    265                 all += locales[i].getDisplayName();
    266             }
    267             logln(all);
    268         }
    269         else
    270             errln("**** FAIL: Zero available locales or null array pointer");
    271     }
    272 
    273     /**
    274      *  call testLocale for all locales
    275      **/
    276     @Test
    277     public void TestMonster() {
    278         final String SEP = "============================================================\n";
    279         int count;
    280         final Locale[] allLocales = NumberFormat.getAvailableLocales();
    281         Locale[] locales = allLocales;
    282         count = locales.length;
    283         if (count != 0)
    284         {
    285             if (TestFmwk.getExhaustiveness() < 10 && count > 6) {
    286                 count = 6;
    287                 locales = new Locale[6];
    288                 locales[0] = allLocales[0];
    289                 locales[1] = allLocales[1];
    290                 locales[2] = allLocales[2];
    291                 // In a quick test, make sure we test locales that use
    292                 // currency prefix, currency suffix, and choice currency
    293                 // logic.  Otherwise bugs in these areas can slip through.
    294                 locales[3] = new Locale("ar", "AE", "");
    295                 locales[4] = new Locale("cs", "CZ", "");
    296                 locales[5] = new Locale("en", "IN", "");
    297             }
    298             for (int i=0; i<count; ++i)
    299             {
    300                 logln(SEP);
    301                 _testLocale(locales[i]);
    302             }
    303         }
    304 
    305         logln(SEP);
    306     }
    307 }
    308