< prev index next >

test/jdk/java/util/Formatter/BasicDoubleObject.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2019, 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  */


  35 import java.math.BigInteger;
  36 import java.text.DateFormatSymbols;
  37 import java.util.*;
  38 
  39 import static java.util.Calendar.*;
  40 
  41 
  42 
  43 
  44 
  45 public class BasicDoubleObject extends Basic {
  46 
  47     private static void test(String fs, String exp, Object ... args) {
  48         Formatter f = new Formatter(new StringBuilder(), Locale.US);
  49         f.format(fs, args);
  50         ck(fs, exp, f.toString());
  51 
  52         f = new Formatter(new StringBuilder(), Locale.US);
  53         f.format("foo " + fs + " bar", args);
  54         ck(fs, "foo " + exp + " bar", f.toString());



  55     }
  56 
  57     private static void test(Locale l, String fs, String exp, Object ... args)
  58     {
  59         Formatter f = new Formatter(new StringBuilder(), l);
  60         f.format(fs, args);
  61         ck(fs, exp, f.toString());
  62 
  63         f = new Formatter(new StringBuilder(), l);
  64         f.format("foo " + fs + " bar", args);
  65         ck(fs, "foo " + exp + " bar", f.toString());



  66     }
  67 
  68     private static void test(String fs, Object ... args) {
  69         Formatter f = new Formatter(new StringBuilder(), Locale.US);
  70         f.format(fs, args);
  71         ck(fs, "fail", f.toString());
  72     }
  73 
  74     private static void test(String fs) {
  75         Formatter f = new Formatter(new StringBuilder(), Locale.US);
  76         f.format(fs, "fail");
  77         ck(fs, "fail", f.toString());
  78     }
  79 
  80     private static void testSysOut(String fs, String exp, Object ... args) {
  81         FileOutputStream fos = null;
  82         FileInputStream fis = null;
  83         try {
  84             PrintStream saveOut = System.out;
  85             fos = new FileOutputStream("testSysOut");


 107                 if (fis != null)
 108                     fis.close();
 109             } catch (IOException ex) {
 110                 fail(fs, ex.getClass());
 111             }
 112         }
 113     }
 114 
 115     private static void tryCatch(String fs, Class<?> ex) {
 116         boolean caught = false;
 117         try {
 118             test(fs);
 119         } catch (Throwable x) {
 120             if (ex.isAssignableFrom(x.getClass()))
 121                 caught = true;
 122         }
 123         if (!caught)
 124             fail(fs, ex);
 125         else
 126             pass();


 127     }
 128 
 129     private static void tryCatch(String fs, Class<?> ex, Object ... args) {
 130         boolean caught = false;
 131         try {
 132             test(fs, args);
 133         } catch (Throwable x) {
 134             if (ex.isAssignableFrom(x.getClass()))
 135                 caught = true;
 136         }
 137         if (!caught)
 138             fail(fs, ex);
 139         else
 140             pass();






























 141     }
 142 
 143 
 144 
 145 
 146 
 147 
 148 
 149 
 150 
 151 
 152 
 153 
 154 
 155 
 156 
 157 
 158 
 159 
 160 


1164 
1165 
1166 
1167 
1168 
1169 
1170 
1171 
1172 
1173 
1174 
1175 
1176 
1177 
1178         //---------------------------------------------------------------------
1179         // %f - float, double, Double, BigDecimal
1180         //---------------------------------------------------------------------
1181         test("%.3f", "3141592.654", mult(pi, 1000000.0));
1182         test("%.3f", "-3141592.654", mult(pi, -1000000.0));
1183         test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0));
1184         test(Locale.FRANCE, "%,.4f", "3\u202f141\u202f592,6536", mult(pi, 1000000.0));
1185         test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0));
1186         test("%(.4f", "3141592.6536", mult(pi, 1000000.0));
1187         test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0));
1188         test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0));
1189         test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0));
1190 
1191 
1192 
1193 
1194         //---------------------------------------------------------------------
1195         // %g
1196         //
1197         // Floating-point conversions applicable to float, double, and
1198         // BigDecimal.
1199         //---------------------------------------------------------------------
1200         test("%g", "null", (Object)null);
1201         test("%g", "3.14159", pi);
1202         test(Locale.FRANCE, "%g", "3,14159", pi);
1203         test("%.0g", "1e+01", ten);
1204         test("%G", "3.14159", pi);


   1 /*
   2  * Copyright (c) 2003, 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  */


  35 import java.math.BigInteger;
  36 import java.text.DateFormatSymbols;
  37 import java.util.*;
  38 
  39 import static java.util.Calendar.*;
  40 
  41 
  42 
  43 
  44 
  45 public class BasicDoubleObject extends Basic {
  46 
  47     private static void test(String fs, String exp, Object ... args) {
  48         Formatter f = new Formatter(new StringBuilder(), Locale.US);
  49         f.format(fs, args);
  50         ck(fs, exp, f.toString());
  51 
  52         f = new Formatter(new StringBuilder(), Locale.US);
  53         f.format("foo " + fs + " bar", args);
  54         ck(fs, "foo " + exp + " bar", f.toString());
  55 
  56         JavacIntrinsicsSupport.formatAndCheck(fs, exp, Locale.US, args);
  57         JavacIntrinsicsSupport.formatAndCheck("foo " + fs + " bar", "foo " + exp + " bar", Locale.US, args);
  58     }
  59 
  60     private static void test(Locale l, String fs, String exp, Object ... args)
  61     {
  62         Formatter f = new Formatter(new StringBuilder(), l);
  63         f.format(fs, args);
  64         ck(fs, exp, f.toString());
  65 
  66         f = new Formatter(new StringBuilder(), l);
  67         f.format("foo " + fs + " bar", args);
  68         ck(fs, "foo " + exp + " bar", f.toString());
  69 
  70         JavacIntrinsicsSupport.formatAndCheck(fs, exp, l, args);
  71         JavacIntrinsicsSupport.formatAndCheck("foo " + fs + " bar", "foo " + exp + " bar", l, args);
  72     }
  73 
  74     private static void test(String fs, Object ... args) {
  75         Formatter f = new Formatter(new StringBuilder(), Locale.US);
  76         f.format(fs, args);
  77         ck(fs, "fail", f.toString());
  78     }
  79 
  80     private static void test(String fs) {
  81         Formatter f = new Formatter(new StringBuilder(), Locale.US);
  82         f.format(fs, "fail");
  83         ck(fs, "fail", f.toString());
  84     }
  85 
  86     private static void testSysOut(String fs, String exp, Object ... args) {
  87         FileOutputStream fos = null;
  88         FileInputStream fis = null;
  89         try {
  90             PrintStream saveOut = System.out;
  91             fos = new FileOutputStream("testSysOut");


 113                 if (fis != null)
 114                     fis.close();
 115             } catch (IOException ex) {
 116                 fail(fs, ex.getClass());
 117             }
 118         }
 119     }
 120 
 121     private static void tryCatch(String fs, Class<?> ex) {
 122         boolean caught = false;
 123         try {
 124             test(fs);
 125         } catch (Throwable x) {
 126             if (ex.isAssignableFrom(x.getClass()))
 127                 caught = true;
 128         }
 129         if (!caught)
 130             fail(fs, ex);
 131         else
 132             pass();
 133 
 134         tryCatchIntrinsic(fs, ex);
 135     }
 136 
 137     private static void tryCatch(String fs, Class<?> ex, Object ... args) {
 138         boolean caught = false;
 139         try {
 140             test(fs, args);
 141         } catch (Throwable x) {
 142             if (ex.isAssignableFrom(x.getClass()))
 143                 caught = true;
 144         }
 145         if (!caught)
 146             fail(fs, ex);
 147         else
 148             pass();
 149 
 150         tryCatchIntrinsic(fs, ex, args);
 151     }
 152 
 153     private static void tryCatchIntrinsic(String fs, Class<?> ex) {
 154         boolean caught = false;
 155         try {
 156             JavacIntrinsicsSupport.formatAndCheck(fs, "fail", Locale.US, "fail");
 157         } catch (Throwable x) {
 158             if (ex.isAssignableFrom(x.getClass()))
 159                 caught = true;
 160         }
 161         if (!caught)
 162             fail(fs, ex);
 163         else
 164             pass();
 165     }
 166 
 167     private static void tryCatchIntrinsic(String fs, Class<?> ex, Object ... args) {
 168         boolean caught = false;
 169         try {
 170             JavacIntrinsicsSupport.formatAndCheck(fs, "fail", Locale.US, args);
 171         } catch (Throwable x) {
 172             if (ex.isAssignableFrom(x.getClass()))
 173                 caught = true;
 174         }
 175         if (!caught)
 176             fail(fs, ex);
 177         else
 178             pass();
 179     }
 180 
 181 
 182 
 183 
 184 
 185 
 186 
 187 
 188 
 189 
 190 
 191 
 192 
 193 
 194 
 195 
 196 
 197 
 198 


1202 
1203 
1204 
1205 
1206 
1207 
1208 
1209 
1210 
1211 
1212 
1213 
1214 
1215 
1216         //---------------------------------------------------------------------
1217         // %f - float, double, Double, BigDecimal
1218         //---------------------------------------------------------------------
1219         test("%.3f", "3141592.654", mult(pi, 1000000.0));
1220         test("%.3f", "-3141592.654", mult(pi, -1000000.0));
1221         test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0));
1222         test(Locale.FRANCE, "%,.4f", "3\u00a0141\u00a0592,6536", mult(pi, 1000000.0));
1223         test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0));
1224         test("%(.4f", "3141592.6536", mult(pi, 1000000.0));
1225         test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0));
1226         test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0));
1227         test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0));
1228 
1229 
1230 
1231 
1232         //---------------------------------------------------------------------
1233         // %g
1234         //
1235         // Floating-point conversions applicable to float, double, and
1236         // BigDecimal.
1237         //---------------------------------------------------------------------
1238         test("%g", "null", (Object)null);
1239         test("%g", "3.14159", pi);
1240         test(Locale.FRANCE, "%g", "3,14159", pi);
1241         test("%.0g", "1e+01", ten);
1242         test("%G", "3.14159", pi);


< prev index next >