< prev index next >

test/jdk/java/util/Formatter/BasicInt.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 BasicInt 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 


 577 
 578 
 579 
 580 
 581 
 582 
 583 
 584 
 585 
 586 
 587 
 588 
 589 
 590 
 591         //---------------------------------------------------------------------
 592         // %d - int and long
 593         //---------------------------------------------------------------------
 594         int oneToSeven = (int) 1234567;
 595         test("%d", "1234567", oneToSeven);
 596         test("%,d", "1,234,567", oneToSeven);
 597         test(Locale.FRANCE, "%,d", "1\u202f234\u202f567", oneToSeven);
 598         test("%,d", "-1,234,567", negate(oneToSeven));
 599         test("%(d", "1234567", oneToSeven);
 600         test("%(d", "(1234567)", negate(oneToSeven));
 601         test("% d", " 1234567", oneToSeven);
 602         test("% d", "-1234567", negate(oneToSeven));
 603         test("%+d", "+1234567", oneToSeven);
 604         test("%+d", "-1234567", negate(oneToSeven));
 605         test("%010d", "0001234567", oneToSeven);
 606         test("%010d", "-001234567", negate(oneToSeven));
 607         test("%(10d", " (1234567)", negate(oneToSeven));
 608         test("%-10d", "1234567   ", oneToSeven);
 609         test("%-10d", "-1234567  ", negate(oneToSeven));
 610         // , variations:
 611         test("% ,d", " 1,234,567", oneToSeven);
 612         test("% ,d", "-1,234,567", negate(oneToSeven));
 613         test("%+,10d", "+1,234,567", oneToSeven);
 614         test("%0,10d", "01,234,567", oneToSeven);
 615         test("%0,10d", "-1,234,567", negate(oneToSeven));
 616         test("%(,10d", "(1,234,567)", negate(oneToSeven));
 617         test("%-,10d", "1,234,567 ", oneToSeven);


   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 BasicInt 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 


 615 
 616 
 617 
 618 
 619 
 620 
 621 
 622 
 623 
 624 
 625 
 626 
 627 
 628 
 629         //---------------------------------------------------------------------
 630         // %d - int and long
 631         //---------------------------------------------------------------------
 632         int oneToSeven = (int) 1234567;
 633         test("%d", "1234567", oneToSeven);
 634         test("%,d", "1,234,567", oneToSeven);
 635         test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", oneToSeven);
 636         test("%,d", "-1,234,567", negate(oneToSeven));
 637         test("%(d", "1234567", oneToSeven);
 638         test("%(d", "(1234567)", negate(oneToSeven));
 639         test("% d", " 1234567", oneToSeven);
 640         test("% d", "-1234567", negate(oneToSeven));
 641         test("%+d", "+1234567", oneToSeven);
 642         test("%+d", "-1234567", negate(oneToSeven));
 643         test("%010d", "0001234567", oneToSeven);
 644         test("%010d", "-001234567", negate(oneToSeven));
 645         test("%(10d", " (1234567)", negate(oneToSeven));
 646         test("%-10d", "1234567   ", oneToSeven);
 647         test("%-10d", "-1234567  ", negate(oneToSeven));
 648         // , variations:
 649         test("% ,d", " 1,234,567", oneToSeven);
 650         test("% ,d", "-1,234,567", negate(oneToSeven));
 651         test("%+,10d", "+1,234,567", oneToSeven);
 652         test("%0,10d", "01,234,567", oneToSeven);
 653         test("%0,10d", "-1,234,567", negate(oneToSeven));
 654         test("%(,10d", "(1,234,567)", negate(oneToSeven));
 655         test("%-,10d", "1,234,567 ", oneToSeven);


< prev index next >