1 /*
   2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4068067
  27  * @library /java/text/testlib
  28  * @summary test NumberFormat with exponential separator symbols. It also tests the new
  29  *          public methods in DecimalFormatSymbols, setExponentSeparator() and
  30  *          getExponentSeparator()
  31  */
  32 
  33 import java.util.*;
  34 import java.text.*;
  35 
  36 public class DFSExponential extends IntlTest
  37 {
  38 
  39     public static void main(String[] args) throws Exception {
  40         new DFSExponential().run(args);
  41     }
  42 
  43 
  44    public void DFSExponenTest() throws Exception {
  45         DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
  46         String pat[] = { "0.####E0", "00.000E00", "##0.####E000", "0.###E0;[0.###E0]"  };
  47         double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };
  48         long lval[] = { 0, -1, 1, 123456789 };
  49         String valFormat[][] = {
  50                 {"1.234x10^-2", "1.2346x10^8", "1.23x10^300", "-3.1416x10^-271"},
  51                 {"12.340x10^-03", "12.346x10^07", "12.300x10^299", "-31.416x10^-272"},
  52                 {"12.34x10^-003", "123.4568x10^006", "1.23x10^300", "-314.1593x10^-273"},
  53                 {"1.234x10^-2", "1.235x10^8", "1.23x10^300", "[3.142x10^-271]"},
  54         };
  55 
  56 
  57         int ival = 0, ilval = 0;
  58         logln("Default exponent separator: "+sym.getExponentSeparator());
  59         try {
  60             sym.setExponentSeparator("x10^");
  61         } catch (NullPointerException e){
  62             errln("null String was passed to set an exponent separator symbol");
  63             throw new RuntimeException("Test Malfunction: null String was passed to set an exponent separator symbol" );
  64         }
  65         logln("Current exponent separator: "+sym.getExponentSeparator());
  66 
  67         for (int p=0; p<pat.length; ++p){
  68             DecimalFormat fmt = new DecimalFormat(pat[p], sym);
  69             logln("     Pattern: " + fmt.toPattern());
  70             String locPattern = fmt.toLocalizedPattern();
  71             logln("     Localized pattern: "+locPattern);
  72             //fmt.applyLocalizedPattern(locPattern);
  73             //System.out.println("      fmt.applyLocalizedPattern(): "+fmt.toLocalizedPattern());
  74 
  75             for (int v=0; v<val.length; ++v) {
  76                 String s = fmt.format(val[v]);
  77                 logln("         " + val[v]+" --> "+s);
  78                 if(valFormat[p][v].equals(s)){
  79                     logln(": Passed");
  80                 }else{
  81                    errln(" Failed: Should be formatted as "+valFormat[p][v]+ "but got "+s);
  82                    throw new RuntimeException(" Failed: Should be formatted as "+valFormat[p][v]+ "but got "+s);
  83                 }
  84            }
  85          } //end of the first for loop
  86    }
  87 }