< prev index next >

test/jdk/java/text/Format/NumberFormat/IntlTestNumberFormatAPI.java

Print this page

  1 /*
  2  * Copyright (c) 1998, 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  * @library /java/text/testlib
 27  * @summary test International Number Format API
 28  * @modules jdk.localedata

 29  */
 30 /*
 31 (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
 32 (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
 33 
 34   The original version of this source code and documentation is copyrighted and
 35 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
 36 provided under terms of a License Agreement between Taligent and Sun. This
 37 technology is protected by multiple US and International patents. This notice and
 38 attribution to Taligent may not be removed.
 39   Taligent is a registered trademark of Taligent, Inc.
 40 */
 41 
 42 import java.text.*;
 43 import java.util.*;
 44 
 45 public class IntlTestNumberFormatAPI extends IntlTest
 46 {
 47     public static void main(String[] args) throws Exception {
 48         new IntlTestNumberFormatAPI().run(args);
 49     }
 50 




 51     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.

 52     public void TestAPI()
 53     {
 54         Locale reservedLocale = Locale.getDefault();
 55         try {
 56             logln("NumberFormat API test---"); logln("");
 57             Locale.setDefault(Locale.ENGLISH);
 58 
 59             // ======= Test constructors
 60 
 61             logln("Testing NumberFormat constructors");
 62 
 63             NumberFormat def = NumberFormat.getInstance();
 64 
 65             NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH);
 66 
 67             NumberFormat cur = NumberFormat.getCurrencyInstance();
 68 
 69             NumberFormat cur_fr =
 70                     NumberFormat.getCurrencyInstance(Locale.FRENCH);
 71 
 72             NumberFormat per = NumberFormat.getPercentInstance();
 73 
 74             NumberFormat per_fr =
 75                     NumberFormat.getPercentInstance(Locale.FRENCH);
 76 
 77             // ======= Test equality
 78 
 79             logln("Testing equality operator");
 80 
 81             if( per_fr.equals(cur_fr) ) {
 82                 errln("ERROR: == failed");
 83             }
 84 
 85             // ======= Test various format() methods
 86 
 87             logln("Testing various format() methods");
 88 
 89 //          final double d = -10456.0037; // this appears as
 90                                           // -10456.003700000001 on NT
 91 //          final double d = -1.04560037e-4; // this appears as
 92                                              // -1.0456003700000002E-4 on NT
 93             final double d = -10456.00370000000000; // this works!
 94             final long l = 100000000;
 95 
 96             String res1 = new String();
 97             String res2 = new String();
 98             StringBuffer res3 = new StringBuffer();
 99             StringBuffer res4 = new StringBuffer();
100             StringBuffer res5 = new StringBuffer();
101             StringBuffer res6 = new StringBuffer();
102             FieldPosition pos1 = new FieldPosition(0);
103             FieldPosition pos2 = new FieldPosition(0);
104             FieldPosition pos3 = new FieldPosition(0);
105             FieldPosition pos4 = new FieldPosition(0);
106 
107             res1 = cur_fr.format(d);
108             logln( "" + d + " formatted to " + res1);
109 
110             res2 = cur_fr.format(l);
111             logln("" + l + " formatted to " + res2);
112 
113             res3 = cur_fr.format(d, res3, pos1);
114             logln( "" + d + " formatted to " + res3);
115 
116             res4 = cur_fr.format(l, res4, pos2);
117             logln("" + l + " formatted to " + res4);
118 
119             res5 = cur_fr.format(d, res5, pos3);
120             logln("" + d + " formatted to " + res5);
121 
122             res6 = cur_fr.format(l, res6, pos4);
123             logln("" + l + " formatted to " + res6);
124 
125 
126             // ======= Test parse()
127 
128             logln("Testing parse()");
129 
130 //          String text = new String("-10,456.0037");
131             String text = new String("-10456,0037");
132             ParsePosition pos = new ParsePosition(0);
133             ParsePosition pos01 = new ParsePosition(0);
134             double d1 = ((Number)fr.parseObject(text, pos)).doubleValue();
135             if(d1 != d) {
136                 errln("ERROR: Roundtrip failed (via parse()) for " + text);
137             }
138             logln(text + " parsed into " + d1);
139 
140             double d2 = fr.parse(text, pos01).doubleValue();
141             if(d2 != d) {
142                 errln("ERROR: Roundtrip failed (via parse()) for " + text);
143             }
144             logln(text + " parsed into " + d2);
145 
146             double d3 = 0;
147             try {
148                 d3 = fr.parse(text).doubleValue();
149             }
150             catch (ParseException e) {
151                 errln("ERROR: parse() failed");
152             }
153             if(d3 != d) {
154                 errln("ERROR: Roundtrip failed (via parse()) for " + text);
155             }
156             logln(text + " parsed into " + d3);
157 
158 
159             // ======= Test getters and setters
160 
161             logln("Testing getters and setters");
162 
163             final Locale[] locales = NumberFormat.getAvailableLocales();
164             long count = locales.length;
165             logln("Got " + count + " locales" );
166             for(int i = 0; i < count; i++) {
167                 String name;
168                 name = locales[i].getDisplayName();
169                 logln(name);
170             }
171 
172             fr.setParseIntegerOnly( def.isParseIntegerOnly() );
173             if(fr.isParseIntegerOnly() != def.isParseIntegerOnly() ) {
174                     errln("ERROR: setParseIntegerOnly() failed");
175             }
176 
177             fr.setGroupingUsed( def.isGroupingUsed() );
178             if(fr.isGroupingUsed() != def.isGroupingUsed() ) {
179                     errln("ERROR: setGroupingUsed() failed");
180             }
181 
182             fr.setMaximumIntegerDigits( def.getMaximumIntegerDigits() );
183             if(fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits() ) {
184                     errln("ERROR: setMaximumIntegerDigits() failed");
185             }
186 
187             fr.setMinimumIntegerDigits( def.getMinimumIntegerDigits() );
188             if(fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits() ) {
189                     errln("ERROR: setMinimumIntegerDigits() failed");
190             }
191 
192             fr.setMaximumFractionDigits( def.getMaximumFractionDigits() );
193             if(fr.getMaximumFractionDigits() != def.getMaximumFractionDigits() ) {
194                     errln("ERROR: setMaximumFractionDigits() failed");
195             }
196 
197             fr.setMinimumFractionDigits( def.getMinimumFractionDigits() );
198             if(fr.getMinimumFractionDigits() != def.getMinimumFractionDigits() ) {
199                     errln("ERROR: setMinimumFractionDigits() failed");
200             }
201 
202             // ======= Test getStaticClassID()
203 
204 //          logln("Testing instanceof()");
205 
206 //          try {
207 //              NumberFormat test = new DecimalFormat();
208 
209 //              if (! (test instanceof DecimalFormat)) {
210 //                  errln("ERROR: instanceof failed");
211 //              }
212 //          }
213 //          catch (Exception e) {
214 //              errln("ERROR: Couldn't create a DecimalFormat");
215 //          }
216         } finally {
217             // restore the reserved locale
218             Locale.setDefault(reservedLocale);
219         }

  1 /*
  2  * Copyright (c) 1998, 2023, 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  * @summary test International Number Format API
 27  * @modules jdk.localedata
 28  * @run junit IntlTestNumberFormatAPI
 29  */
 30 /*
 31 (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
 32 (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
 33 
 34   The original version of this source code and documentation is copyrighted and
 35 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
 36 provided under terms of a License Agreement between Taligent and Sun. This
 37 technology is protected by multiple US and International patents. This notice and
 38 attribution to Taligent may not be removed.
 39   Taligent is a registered trademark of Taligent, Inc.
 40 */
 41 
 42 import java.text.*;
 43 import java.util.*;
 44 
 45 import org.junit.jupiter.api.Test;




 46 
 47 import static org.junit.jupiter.api.Assertions.fail;
 48 
 49 public class IntlTestNumberFormatAPI
 50 {
 51     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
 52     @Test
 53     public void TestAPI()
 54     {
 55         Locale reservedLocale = Locale.getDefault();
 56         try {
 57             System.out.println("NumberFormat API test---"); System.out.println("");
 58             Locale.setDefault(Locale.ENGLISH);
 59 
 60             // ======= Test constructors
 61 
 62             System.out.println("Testing NumberFormat constructors");
 63 
 64             NumberFormat def = NumberFormat.getInstance();
 65 
 66             NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH);
 67 
 68             NumberFormat cur = NumberFormat.getCurrencyInstance();
 69 
 70             NumberFormat cur_fr =
 71                     NumberFormat.getCurrencyInstance(Locale.FRENCH);
 72 
 73             NumberFormat per = NumberFormat.getPercentInstance();
 74 
 75             NumberFormat per_fr =
 76                     NumberFormat.getPercentInstance(Locale.FRENCH);
 77 
 78             // ======= Test equality
 79 
 80             System.out.println("Testing equality operator");
 81 
 82             if( per_fr.equals(cur_fr) ) {
 83                 fail("ERROR: == failed");
 84             }
 85 
 86             // ======= Test various format() methods
 87 
 88             System.out.println("Testing various format() methods");
 89 
 90 //          final double d = -10456.0037; // this appears as
 91                                           // -10456.003700000001 on NT
 92 //          final double d = -1.04560037e-4; // this appears as
 93                                              // -1.0456003700000002E-4 on NT
 94             final double d = -10456.00370000000000; // this works!
 95             final long l = 100000000;
 96 
 97             String res1 = new String();
 98             String res2 = new String();
 99             StringBuffer res3 = new StringBuffer();
100             StringBuffer res4 = new StringBuffer();
101             StringBuffer res5 = new StringBuffer();
102             StringBuffer res6 = new StringBuffer();
103             FieldPosition pos1 = new FieldPosition(0);
104             FieldPosition pos2 = new FieldPosition(0);
105             FieldPosition pos3 = new FieldPosition(0);
106             FieldPosition pos4 = new FieldPosition(0);
107 
108             res1 = cur_fr.format(d);
109             System.out.println( "" + d + " formatted to " + res1);
110 
111             res2 = cur_fr.format(l);
112             System.out.println("" + l + " formatted to " + res2);
113 
114             res3 = cur_fr.format(d, res3, pos1);
115             System.out.println( "" + d + " formatted to " + res3);
116 
117             res4 = cur_fr.format(l, res4, pos2);
118             System.out.println("" + l + " formatted to " + res4);
119 
120             res5 = cur_fr.format(d, res5, pos3);
121             System.out.println("" + d + " formatted to " + res5);
122 
123             res6 = cur_fr.format(l, res6, pos4);
124             System.out.println("" + l + " formatted to " + res6);
125 
126 
127             // ======= Test parse()
128 
129             System.out.println("Testing parse()");
130 
131 //          String text = new String("-10,456.0037");
132             String text = new String("-10456,0037");
133             ParsePosition pos = new ParsePosition(0);
134             ParsePosition pos01 = new ParsePosition(0);
135             double d1 = ((Number)fr.parseObject(text, pos)).doubleValue();
136             if(d1 != d) {
137                 fail("ERROR: Roundtrip failed (via parse()) for " + text);
138             }
139             System.out.println(text + " parsed into " + d1);
140 
141             double d2 = fr.parse(text, pos01).doubleValue();
142             if(d2 != d) {
143                 fail("ERROR: Roundtrip failed (via parse()) for " + text);
144             }
145             System.out.println(text + " parsed into " + d2);
146 
147             double d3 = 0;
148             try {
149                 d3 = fr.parse(text).doubleValue();
150             }
151             catch (ParseException e) {
152                 fail("ERROR: parse() failed");
153             }
154             if(d3 != d) {
155                 fail("ERROR: Roundtrip failed (via parse()) for " + text);
156             }
157             System.out.println(text + " parsed into " + d3);
158 
159 
160             // ======= Test getters and setters
161 
162             System.out.println("Testing getters and setters");
163 
164             final Locale[] locales = NumberFormat.getAvailableLocales();
165             long count = locales.length;
166             System.out.println("Got " + count + " locales" );
167             for(int i = 0; i < count; i++) {
168                 String name;
169                 name = locales[i].getDisplayName();
170                 System.out.println(name);
171             }
172 
173             fr.setParseIntegerOnly( def.isParseIntegerOnly() );
174             if(fr.isParseIntegerOnly() != def.isParseIntegerOnly() ) {
175                     fail("ERROR: setParseIntegerOnly() failed");
176             }
177 
178             fr.setGroupingUsed( def.isGroupingUsed() );
179             if(fr.isGroupingUsed() != def.isGroupingUsed() ) {
180                     fail("ERROR: setGroupingUsed() failed");
181             }
182 
183             fr.setMaximumIntegerDigits( def.getMaximumIntegerDigits() );
184             if(fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits() ) {
185                     fail("ERROR: setMaximumIntegerDigits() failed");
186             }
187 
188             fr.setMinimumIntegerDigits( def.getMinimumIntegerDigits() );
189             if(fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits() ) {
190                     fail("ERROR: setMinimumIntegerDigits() failed");
191             }
192 
193             fr.setMaximumFractionDigits( def.getMaximumFractionDigits() );
194             if(fr.getMaximumFractionDigits() != def.getMaximumFractionDigits() ) {
195                     fail("ERROR: setMaximumFractionDigits() failed");
196             }
197 
198             fr.setMinimumFractionDigits( def.getMinimumFractionDigits() );
199             if(fr.getMinimumFractionDigits() != def.getMinimumFractionDigits() ) {
200                     fail("ERROR: setMinimumFractionDigits() failed");
201             }
202 
203             // ======= Test getStaticClassID()
204 
205 //          logln("Testing instanceof()");
206 
207 //          try {
208 //              NumberFormat test = new DecimalFormat();
209 
210 //              if (! (test instanceof DecimalFormat)) {
211 //                  errln("ERROR: instanceof failed");
212 //              }
213 //          }
214 //          catch (Exception e) {
215 //              errln("ERROR: Couldn't create a DecimalFormat");
216 //          }
217         } finally {
218             // restore the reserved locale
219             Locale.setDefault(reservedLocale);
220         }
< prev index next >