1 /*****************************************************************************************
\r
3 * Copyright (C) 1996-2009, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
7 * Port From: JDK 1.4b1 : java.text.Format.IntlTestNumberFormatAPI
\r
8 * Source File: java/text/format/IntlTestNumberFormatAPI.java
\r
13 @summary test International Number Format API
\r
16 package com.ibm.icu.dev.test.format;
\r
18 import java.math.BigInteger;
\r
19 import java.text.FieldPosition;
\r
20 import java.text.ParseException;
\r
21 import java.text.ParsePosition;
\r
22 import java.util.Locale;
\r
24 import com.ibm.icu.text.NumberFormat;
\r
25 import com.ibm.icu.util.ULocale;
\r
27 public class IntlTestNumberFormatAPI extends com.ibm.icu.dev.test.TestFmwk
\r
29 public static void main(String[] args) throws Exception {
\r
30 new IntlTestNumberFormatAPI().run(args);
\r
33 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
\r
34 public void TestAPI()
\r
36 logln("NumberFormat API test---"); logln("");
\r
37 Locale.setDefault(Locale.ENGLISH);
\r
39 // ======= Test constructors
\r
41 logln("Testing NumberFormat constructors");
\r
43 NumberFormat def = NumberFormat.getInstance();
\r
45 NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH);
\r
47 NumberFormat cur = NumberFormat.getCurrencyInstance();
\r
49 NumberFormat cur_fr = NumberFormat.getCurrencyInstance(Locale.FRENCH);
\r
51 NumberFormat per = NumberFormat.getPercentInstance();
\r
53 NumberFormat per_fr = NumberFormat.getPercentInstance(Locale.FRENCH);
\r
55 NumberFormat integer = NumberFormat.getIntegerInstance();
\r
57 NumberFormat int_fr = NumberFormat.getIntegerInstance(Locale.FRENCH);
\r
59 //Fix "The variable is never used" compilation warnings
\r
60 logln("Currency : " + cur.format(1234.5));
\r
61 logln("Percent : " + per.format(1234.5));
\r
62 logln("Integer : " + integer.format(1234.5));
\r
63 logln("Int_fr : " + int_fr.format(1234.5));
\r
65 // ======= Test equality
\r
67 logln("Testing equality operator");
\r
69 if( per_fr.equals(cur_fr) ) {
\r
70 errln("ERROR: == failed");
\r
73 // ======= Test various format() methods
\r
75 logln("Testing various format() methods");
\r
77 // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
\r
78 // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
\r
79 final double d = -10456.00370000000000; // this works!
\r
80 final long l = 100000000;
\r
82 String res1 = new String();
\r
83 String res2 = new String();
\r
84 StringBuffer res3 = new StringBuffer();
\r
85 StringBuffer res4 = new StringBuffer();
\r
86 StringBuffer res5 = new StringBuffer();
\r
87 StringBuffer res6 = new StringBuffer();
\r
88 FieldPosition pos1 = new FieldPosition(0);
\r
89 FieldPosition pos2 = new FieldPosition(0);
\r
90 FieldPosition pos3 = new FieldPosition(0);
\r
91 FieldPosition pos4 = new FieldPosition(0);
\r
93 res1 = cur_fr.format(d);
\r
94 logln( "" + d + " formatted to " + res1);
\r
96 res2 = cur_fr.format(l);
\r
97 logln("" + l + " formatted to " + res2);
\r
99 res3 = cur_fr.format(d, res3, pos1);
\r
100 logln( "" + d + " formatted to " + res3);
\r
102 res4 = cur_fr.format(l, res4, pos2);
\r
103 logln("" + l + " formatted to " + res4);
\r
105 res5 = cur_fr.format(d, res5, pos3);
\r
106 logln("" + d + " formatted to " + res5);
\r
108 res6 = cur_fr.format(l, res6, pos4);
\r
109 logln("" + l + " formatted to " + res6);
\r
112 // ======= Test parse()
\r
114 logln("Testing parse()");
\r
116 // String text = new String("-10,456.0037");
\r
117 String text = new String("-10456,0037");
\r
118 ParsePosition pos = new ParsePosition(0);
\r
119 ParsePosition pos01 = new ParsePosition(0);
\r
120 double d1 = ((Number)fr.parseObject(text, pos)).doubleValue();
\r
122 errln("ERROR: Roundtrip failed (via parse()) for " + text);
\r
124 logln(text + " parsed into " + d1);
\r
126 double d2 = fr.parse(text, pos01).doubleValue();
\r
128 errln("ERROR: Roundtrip failed (via parse()) for " + text);
\r
130 logln(text + " parsed into " + d2);
\r
134 d3 = fr.parse(text).doubleValue();
\r
136 catch (ParseException e) {
\r
137 errln("ERROR: parse() failed");
\r
140 errln("ERROR: Roundtrip failed (via parse()) for " + text);
\r
142 logln(text + " parsed into " + d3);
\r
145 // ======= Test getters and setters
\r
147 logln("Testing getters and setters");
\r
149 final Locale[] locales = NumberFormat.getAvailableLocales();
\r
150 long count = locales.length;
\r
151 logln("Got " + count + " locales" );
\r
152 for(int i = 0; i < count; i++) {
\r
154 name = locales[i].getDisplayName();
\r
158 fr.setParseIntegerOnly( def.isParseIntegerOnly() );
\r
159 if(fr.isParseIntegerOnly() != def.isParseIntegerOnly() ) {
\r
160 errln("ERROR: setParseIntegerOnly() failed");
\r
163 fr.setGroupingUsed( def.isGroupingUsed() );
\r
164 if(fr.isGroupingUsed() != def.isGroupingUsed() ) {
\r
165 errln("ERROR: setGroupingUsed() failed");
\r
168 fr.setMaximumIntegerDigits( def.getMaximumIntegerDigits() );
\r
169 if(fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits() ) {
\r
170 errln("ERROR: setMaximumIntegerDigits() failed");
\r
173 fr.setMinimumIntegerDigits( def.getMinimumIntegerDigits() );
\r
174 if(fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits() ) {
\r
175 errln("ERROR: setMinimumIntegerDigits() failed");
\r
178 fr.setMaximumFractionDigits( def.getMaximumFractionDigits() );
\r
179 if(fr.getMaximumFractionDigits() != def.getMaximumFractionDigits() ) {
\r
180 errln("ERROR: setMaximumFractionDigits() failed");
\r
183 fr.setMinimumFractionDigits( def.getMinimumFractionDigits() );
\r
184 if(fr.getMinimumFractionDigits() != def.getMinimumFractionDigits() ) {
\r
185 errln("ERROR: setMinimumFractionDigits() failed");
\r
188 // ======= Test getStaticClassID()
\r
190 // logln("Testing instanceof()");
\r
193 // NumberFormat test = new DecimalFormat();
\r
195 // if (! (test instanceof DecimalFormat)) {
\r
196 // errln("ERROR: instanceof failed");
\r
199 // catch (Exception e) {
\r
200 // errln("ERROR: Couldn't create a DecimalFormat");
\r
204 // Jitterbug 4451, for coverage
\r
205 public void TestCoverage(){
\r
206 class StubNumberFormat extends NumberFormat{
\r
208 * For serialization
\r
210 private static final long serialVersionUID = 3768385020503005993L;
\r
212 String p = NumberFormat.getPattern(ULocale.getDefault().toLocale(),0);
\r
213 if (!p.equals(NumberFormat.getPattern(ULocale.getDefault(),0))){
\r
214 errln("NumberFormat.getPattern(Locale, int) should delegate to (ULocale,)");
\r
217 public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
\r
218 public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
\r
219 public StringBuffer format(BigInteger number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
\r
220 public StringBuffer format(java.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
\r
221 public StringBuffer format(com.ibm.icu.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
\r
222 public Number parse(String text, ParsePosition parsePosition) {return null;}
\r
224 new StubNumberFormat().run();
\r