< prev index next >

test/jdk/java/text/Format/NumberFormat/IntlTestDecimalFormatAPI.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 Decimal Format API

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




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

 51     public void TestAPI()
 52     {
 53         Locale reservedLocale = Locale.getDefault();
 54         try {
 55             logln("DecimalFormat API test---"); logln("");
 56             Locale.setDefault(Locale.ENGLISH);
 57 
 58             // ======= Test constructors
 59 
 60             logln("Testing DecimalFormat constructors");
 61 
 62             DecimalFormat def = new DecimalFormat();
 63 
 64             final String pattern = new String("#,##0.# FF");
 65             DecimalFormat pat = null;
 66             try {
 67                 pat = new DecimalFormat(pattern);
 68             }
 69             catch (IllegalArgumentException e) {
 70                 errln("ERROR: Could not create DecimalFormat (pattern)");
 71             }
 72 
 73             DecimalFormatSymbols symbols =
 74                     new DecimalFormatSymbols(Locale.FRENCH);
 75 
 76             DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
 77 
 78             // ======= Test clone(), assignment, and equality
 79 
 80             logln("Testing clone() and equality operators");
 81 
 82             Format clone = (Format) def.clone();
 83             if( ! def.equals(clone)) {
 84                 errln("ERROR: Clone() failed");
 85             }
 86 
 87             // ======= Test various format() methods
 88 
 89             logln("Testing various format() methods");
 90 
 91 //          final double d = -10456.0037; // this appears as
 92                                           // -10456.003700000001 on NT
 93 //          final double d = -1.04560037e-4; // this appears as
 94                                              // -1.0456003700000002E-4 on NT
 95             final double d = -10456.00370000000000; // this works!
 96             final long l = 100000000;
 97             logln("" + d + " is the double value");
 98 
 99             StringBuffer res1 = new StringBuffer();
100             StringBuffer res2 = new StringBuffer();
101             StringBuffer res3 = new StringBuffer();
102             StringBuffer res4 = 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 = def.format(d, res1, pos1);
109             logln("" + d + " formatted to " + res1);
110 
111             res2 = pat.format(l, res2, pos2);
112             logln("" + l + " formatted to " + res2);
113 
114             res3 = cust1.format(d, res3, pos3);
115             logln("" + d + " formatted to " + res3);
116 
117             res4 = cust1.format(l, res4, pos4);
118             logln("" + l + " formatted to " + res4);
119 
120             // ======= Test parse()
121 
122             logln("Testing parse()");
123 
124             String text = new String("-10,456.0037");
125             ParsePosition pos = new ParsePosition(0);
126             String patt = new String("#,##0.#");
127             pat.applyPattern(patt);
128             double d2 = pat.parse(text, pos).doubleValue();
129             if(d2 != d) {
130                 errln("ERROR: Roundtrip failed (via parse(" +
131                     d2 + " != " + d + ")) for " + text);
132             }
133             logln(text + " parsed into " + (long) d2);
134 
135             // ======= Test getters and setters
136 
137             logln("Testing getters and setters");
138 
139             final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
140             def.setDecimalFormatSymbols(syms);
141             if(!pat.getDecimalFormatSymbols().equals(
142                     def.getDecimalFormatSymbols())) {
143                 errln("ERROR: set DecimalFormatSymbols() failed");
144             }
145 
146             String posPrefix;
147             pat.setPositivePrefix("+");
148             posPrefix = pat.getPositivePrefix();
149             logln("Positive prefix (should be +): " + posPrefix);
150             if(posPrefix != "+") {
151                 errln("ERROR: setPositivePrefix() failed");
152             }
153 
154             String negPrefix;
155             pat.setNegativePrefix("-");
156             negPrefix = pat.getNegativePrefix();
157             logln("Negative prefix (should be -): " + negPrefix);
158             if(negPrefix != "-") {
159                 errln("ERROR: setNegativePrefix() failed");
160             }
161 
162             String posSuffix;
163             pat.setPositiveSuffix("_");
164             posSuffix = pat.getPositiveSuffix();
165             logln("Positive suffix (should be _): " + posSuffix);
166             if(posSuffix != "_") {
167                 errln("ERROR: setPositiveSuffix() failed");
168             }
169 
170             String negSuffix;
171             pat.setNegativeSuffix("~");
172             negSuffix = pat.getNegativeSuffix();
173             logln("Negative suffix (should be ~): " + negSuffix);
174             if(negSuffix != "~") {
175                 errln("ERROR: setNegativeSuffix() failed");
176             }
177 
178             long multiplier = 0;
179             pat.setMultiplier(8);
180             multiplier = pat.getMultiplier();
181             logln("Multiplier (should be 8): " + multiplier);
182             if(multiplier != 8) {
183                 errln("ERROR: setMultiplier() failed");
184             }
185 
186             int groupingSize = 0;
187             pat.setGroupingSize(2);
188             groupingSize = pat.getGroupingSize();
189             logln("Grouping size (should be 2): " + (long) groupingSize);
190             if(groupingSize != 2) {
191                 errln("ERROR: setGroupingSize() failed");
192             }
193 
194             pat.setDecimalSeparatorAlwaysShown(true);
195             boolean tf = pat.isDecimalSeparatorAlwaysShown();
196             logln("DecimalSeparatorIsAlwaysShown (should be true) is " +
197                                                 (tf ? "true" : "false"));
198             if(tf != true) {
199                 errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
200             }
201 
202             String funkyPat;
203             funkyPat = pat.toPattern();
204             logln("Pattern is " + funkyPat);
205 
206             String locPat;
207             locPat = pat.toLocalizedPattern();
208             logln("Localized pattern is " + locPat);
209 
210             // ======= Test applyPattern()
211 
212             logln("Testing applyPattern()");
213 
214             String p1 = new String("#,##0.0#;(#,##0.0#)");
215             logln("Applying pattern " + p1);
216             pat.applyPattern(p1);
217             String s2;
218             s2 = pat.toPattern();
219             logln("Extracted pattern is " + s2);
220             if( ! s2.equals(p1) ) {
221                 errln("ERROR: toPattern() result did not match " +
222                         "pattern applied");
223             }
224 
225             String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
226             logln("Applying pattern " + p2);
227             pat.applyLocalizedPattern(p2);
228             String s3;
229             s3 = pat.toLocalizedPattern();
230             logln("Extracted pattern is " + s3);
231             if( ! s3.equals(p2) ) {
232                 errln("ERROR: toLocalizedPattern() result did not match " +
233                         "pattern applied");
234             }
235 
236             // ======= Test getStaticClassID()
237 
238 //          logln("Testing instanceof()");
239 
240 //          try {
241 //             NumberFormat test = new DecimalFormat();
242 
243 //              if (! (test instanceof DecimalFormat)) {
244 //                  errln("ERROR: instanceof failed");
245 //              }
246 //          }
247 //          catch (Exception e) {
248 //              errln("ERROR: Couldn't create a DecimalFormat");
249 //          }
250         } finally {
251             // restore the reserved locale
252             Locale.setDefault(reservedLocale);

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




 45 
 46 import static org.junit.jupiter.api.Assertions.fail;
 47 
 48 public class IntlTestDecimalFormatAPI
 49 {
 50     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
 51     @Test
 52     public void TestAPI()
 53     {
 54         Locale reservedLocale = Locale.getDefault();
 55         try {
 56             System.out.println("DecimalFormat API test---"); System.out.println("");
 57             Locale.setDefault(Locale.ENGLISH);
 58 
 59             // ======= Test constructors
 60 
 61             System.out.println("Testing DecimalFormat constructors");
 62 
 63             DecimalFormat def = new DecimalFormat();
 64 
 65             final String pattern = new String("#,##0.# FF");
 66             DecimalFormat pat = null;
 67             try {
 68                 pat = new DecimalFormat(pattern);
 69             }
 70             catch (IllegalArgumentException e) {
 71                 fail("ERROR: Could not create DecimalFormat (pattern)");
 72             }
 73 
 74             DecimalFormatSymbols symbols =
 75                     new DecimalFormatSymbols(Locale.FRENCH);
 76 
 77             DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
 78 
 79             // ======= Test clone(), assignment, and equality
 80 
 81             System.out.println("Testing clone() and equality operators");
 82 
 83             Format clone = (Format) def.clone();
 84             if( ! def.equals(clone)) {
 85                 fail("ERROR: Clone() failed");
 86             }
 87 
 88             // ======= Test various format() methods
 89 
 90             System.out.println("Testing various format() methods");
 91 
 92 //          final double d = -10456.0037; // this appears as
 93                                           // -10456.003700000001 on NT
 94 //          final double d = -1.04560037e-4; // this appears as
 95                                              // -1.0456003700000002E-4 on NT
 96             final double d = -10456.00370000000000; // this works!
 97             final long l = 100000000;
 98             System.out.println("" + d + " is the double value");
 99 
100             StringBuffer res1 = new StringBuffer();
101             StringBuffer res2 = new StringBuffer();
102             StringBuffer res3 = new StringBuffer();
103             StringBuffer res4 = new StringBuffer();
104             FieldPosition pos1 = new FieldPosition(0);
105             FieldPosition pos2 = new FieldPosition(0);
106             FieldPosition pos3 = new FieldPosition(0);
107             FieldPosition pos4 = new FieldPosition(0);
108 
109             res1 = def.format(d, res1, pos1);
110             System.out.println("" + d + " formatted to " + res1);
111 
112             res2 = pat.format(l, res2, pos2);
113             System.out.println("" + l + " formatted to " + res2);
114 
115             res3 = cust1.format(d, res3, pos3);
116             System.out.println("" + d + " formatted to " + res3);
117 
118             res4 = cust1.format(l, res4, pos4);
119             System.out.println("" + l + " formatted to " + res4);
120 
121             // ======= Test parse()
122 
123             System.out.println("Testing parse()");
124 
125             String text = new String("-10,456.0037");
126             ParsePosition pos = new ParsePosition(0);
127             String patt = new String("#,##0.#");
128             pat.applyPattern(patt);
129             double d2 = pat.parse(text, pos).doubleValue();
130             if(d2 != d) {
131                 fail("ERROR: Roundtrip failed (via parse(" +
132                     d2 + " != " + d + ")) for " + text);
133             }
134             System.out.println(text + " parsed into " + (long) d2);
135 
136             // ======= Test getters and setters
137 
138             System.out.println("Testing getters and setters");
139 
140             final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
141             def.setDecimalFormatSymbols(syms);
142             if(!pat.getDecimalFormatSymbols().equals(
143                     def.getDecimalFormatSymbols())) {
144                 fail("ERROR: set DecimalFormatSymbols() failed");
145             }
146 
147             String posPrefix;
148             pat.setPositivePrefix("+");
149             posPrefix = pat.getPositivePrefix();
150             System.out.println("Positive prefix (should be +): " + posPrefix);
151             if(posPrefix != "+") {
152                 fail("ERROR: setPositivePrefix() failed");
153             }
154 
155             String negPrefix;
156             pat.setNegativePrefix("-");
157             negPrefix = pat.getNegativePrefix();
158             System.out.println("Negative prefix (should be -): " + negPrefix);
159             if(negPrefix != "-") {
160                 fail("ERROR: setNegativePrefix() failed");
161             }
162 
163             String posSuffix;
164             pat.setPositiveSuffix("_");
165             posSuffix = pat.getPositiveSuffix();
166             System.out.println("Positive suffix (should be _): " + posSuffix);
167             if(posSuffix != "_") {
168                 fail("ERROR: setPositiveSuffix() failed");
169             }
170 
171             String negSuffix;
172             pat.setNegativeSuffix("~");
173             negSuffix = pat.getNegativeSuffix();
174             System.out.println("Negative suffix (should be ~): " + negSuffix);
175             if(negSuffix != "~") {
176                 fail("ERROR: setNegativeSuffix() failed");
177             }
178 
179             long multiplier = 0;
180             pat.setMultiplier(8);
181             multiplier = pat.getMultiplier();
182             System.out.println("Multiplier (should be 8): " + multiplier);
183             if(multiplier != 8) {
184                 fail("ERROR: setMultiplier() failed");
185             }
186 
187             int groupingSize = 0;
188             pat.setGroupingSize(2);
189             groupingSize = pat.getGroupingSize();
190             System.out.println("Grouping size (should be 2): " + (long) groupingSize);
191             if(groupingSize != 2) {
192                 fail("ERROR: setGroupingSize() failed");
193             }
194 
195             pat.setDecimalSeparatorAlwaysShown(true);
196             boolean tf = pat.isDecimalSeparatorAlwaysShown();
197             System.out.println("DecimalSeparatorIsAlwaysShown (should be true) is " +
198                                                 (tf ? "true" : "false"));
199             if(tf != true) {
200                 fail("ERROR: setDecimalSeparatorAlwaysShown() failed");
201             }
202 
203             String funkyPat;
204             funkyPat = pat.toPattern();
205             System.out.println("Pattern is " + funkyPat);
206 
207             String locPat;
208             locPat = pat.toLocalizedPattern();
209             System.out.println("Localized pattern is " + locPat);
210 
211             // ======= Test applyPattern()
212 
213             System.out.println("Testing applyPattern()");
214 
215             String p1 = new String("#,##0.0#;(#,##0.0#)");
216             System.out.println("Applying pattern " + p1);
217             pat.applyPattern(p1);
218             String s2;
219             s2 = pat.toPattern();
220             System.out.println("Extracted pattern is " + s2);
221             if( ! s2.equals(p1) ) {
222                 fail("ERROR: toPattern() result did not match " +
223                         "pattern applied");
224             }
225 
226             String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
227             System.out.println("Applying pattern " + p2);
228             pat.applyLocalizedPattern(p2);
229             String s3;
230             s3 = pat.toLocalizedPattern();
231             System.out.println("Extracted pattern is " + s3);
232             if( ! s3.equals(p2) ) {
233                 fail("ERROR: toLocalizedPattern() result did not match " +
234                         "pattern applied");
235             }
236 
237             // ======= Test getStaticClassID()
238 
239 //          logln("Testing instanceof()");
240 
241 //          try {
242 //             NumberFormat test = new DecimalFormat();
243 
244 //              if (! (test instanceof DecimalFormat)) {
245 //                  errln("ERROR: instanceof failed");
246 //              }
247 //          }
248 //          catch (Exception e) {
249 //              errln("ERROR: Couldn't create a DecimalFormat");
250 //          }
251         } finally {
252             // restore the reserved locale
253             Locale.setDefault(reservedLocale);
< prev index next >