2 /*****************************************************************************************
4 * Copyright (C) 1996-2009, International Business Machines
5 * Corporation and others. All Rights Reserved.
8 * Port From: JDK 1.4b1 : java.text.Format.IntlTestNumberFormatAPI
9 * Source File: java/text/format/IntlTestNumberFormatAPI.java
14 @summary test International Number Format API
17 package com.ibm.icu.dev.test.format;
19 import com.ibm.icu.text.*;
20 import com.ibm.icu.util.ULocale;
22 import java.util.Locale;
24 import java.math.BigInteger;
25 import java.text.FieldPosition;
26 import java.text.ParsePosition;
27 import java.text.ParseException;
29 public class IntlTestNumberFormatAPI extends com.ibm.icu.dev.test.TestFmwk
31 public static void main(String[] args) throws Exception {
32 new IntlTestNumberFormatAPI().run(args);
35 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
38 logln("NumberFormat API test---"); logln("");
39 Locale.setDefault(Locale.ENGLISH);
41 // ======= Test constructors
43 logln("Testing NumberFormat constructors");
45 NumberFormat def = NumberFormat.getInstance();
47 NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH);
49 NumberFormat cur = NumberFormat.getCurrencyInstance();
51 NumberFormat cur_fr = NumberFormat.getCurrencyInstance(Locale.FRENCH);
53 NumberFormat per = NumberFormat.getPercentInstance();
55 NumberFormat per_fr = NumberFormat.getPercentInstance(Locale.FRENCH);
57 NumberFormat integer = NumberFormat.getIntegerInstance();
59 NumberFormat int_fr = NumberFormat.getIntegerInstance(Locale.FRENCH);
61 //Fix "The variable is never used" compilation warnings
62 logln("Currency : " + cur.format(1234.5));
63 logln("Percent : " + per.format(1234.5));
64 logln("Integer : " + integer.format(1234.5));
65 logln("Int_fr : " + int_fr.format(1234.5));
67 // ======= Test equality
69 logln("Testing equality operator");
71 if( per_fr.equals(cur_fr) ) {
72 errln("ERROR: == failed");
75 // ======= Test various format() methods
77 logln("Testing various format() methods");
79 // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
80 // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
81 final double d = -10456.00370000000000; // this works!
82 final long l = 100000000;
84 String res1 = new String();
85 String res2 = new String();
86 StringBuffer res3 = new StringBuffer();
87 StringBuffer res4 = new StringBuffer();
88 StringBuffer res5 = new StringBuffer();
89 StringBuffer res6 = new StringBuffer();
90 FieldPosition pos1 = new FieldPosition(0);
91 FieldPosition pos2 = new FieldPosition(0);
92 FieldPosition pos3 = new FieldPosition(0);
93 FieldPosition pos4 = new FieldPosition(0);
95 res1 = cur_fr.format(d);
96 logln( "" + d + " formatted to " + res1);
98 res2 = cur_fr.format(l);
99 logln("" + l + " formatted to " + res2);
101 res3 = cur_fr.format(d, res3, pos1);
102 logln( "" + d + " formatted to " + res3);
104 res4 = cur_fr.format(l, res4, pos2);
105 logln("" + l + " formatted to " + res4);
107 res5 = cur_fr.format(d, res5, pos3);
108 logln("" + d + " formatted to " + res5);
110 res6 = cur_fr.format(l, res6, pos4);
111 logln("" + l + " formatted to " + res6);
114 // ======= Test parse()
116 logln("Testing parse()");
118 // String text = new String("-10,456.0037");
119 String text = new String("-10456,0037");
120 ParsePosition pos = new ParsePosition(0);
121 ParsePosition pos01 = new ParsePosition(0);
122 double d1 = ((Number)fr.parseObject(text, pos)).doubleValue();
124 errln("ERROR: Roundtrip failed (via parse()) for " + text);
126 logln(text + " parsed into " + d1);
128 double d2 = fr.parse(text, pos01).doubleValue();
130 errln("ERROR: Roundtrip failed (via parse()) for " + text);
132 logln(text + " parsed into " + d2);
136 d3 = fr.parse(text).doubleValue();
138 catch (ParseException e) {
139 errln("ERROR: parse() failed");
142 errln("ERROR: Roundtrip failed (via parse()) for " + text);
144 logln(text + " parsed into " + d3);
147 // ======= Test getters and setters
149 logln("Testing getters and setters");
151 final Locale[] locales = NumberFormat.getAvailableLocales();
152 long count = locales.length;
153 logln("Got " + count + " locales" );
154 for(int i = 0; i < count; i++) {
156 name = locales[i].getDisplayName();
160 fr.setParseIntegerOnly( def.isParseIntegerOnly() );
161 if(fr.isParseIntegerOnly() != def.isParseIntegerOnly() ) {
162 errln("ERROR: setParseIntegerOnly() failed");
165 fr.setGroupingUsed( def.isGroupingUsed() );
166 if(fr.isGroupingUsed() != def.isGroupingUsed() ) {
167 errln("ERROR: setGroupingUsed() failed");
170 fr.setMaximumIntegerDigits( def.getMaximumIntegerDigits() );
171 if(fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits() ) {
172 errln("ERROR: setMaximumIntegerDigits() failed");
175 fr.setMinimumIntegerDigits( def.getMinimumIntegerDigits() );
176 if(fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits() ) {
177 errln("ERROR: setMinimumIntegerDigits() failed");
180 fr.setMaximumFractionDigits( def.getMaximumFractionDigits() );
181 if(fr.getMaximumFractionDigits() != def.getMaximumFractionDigits() ) {
182 errln("ERROR: setMaximumFractionDigits() failed");
185 fr.setMinimumFractionDigits( def.getMinimumFractionDigits() );
186 if(fr.getMinimumFractionDigits() != def.getMinimumFractionDigits() ) {
187 errln("ERROR: setMinimumFractionDigits() failed");
190 // ======= Test getStaticClassID()
192 // logln("Testing instanceof()");
195 // NumberFormat test = new DecimalFormat();
197 // if (! (test instanceof DecimalFormat)) {
198 // errln("ERROR: instanceof failed");
201 // catch (Exception e) {
202 // errln("ERROR: Couldn't create a DecimalFormat");
206 // Jitterbug 4451, for coverage
207 public void TestCoverage(){
208 class StubNumberFormat extends NumberFormat{
212 private static final long serialVersionUID = 3768385020503005993L;
214 String p = NumberFormat.getPattern(ULocale.getDefault().toLocale(),0);
215 if (!p.equals(NumberFormat.getPattern(ULocale.getDefault(),0))){
216 errln("NumberFormat.getPattern(Locale, int) should delegate to (ULocale,)");
219 public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
220 public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
221 public StringBuffer format(BigInteger number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
222 //#if defined(FOUNDATION10)
224 public StringBuffer format(java.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
226 public StringBuffer format(com.ibm.icu.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
227 public Number parse(String text, ParsePosition parsePosition) {return null;}
229 new StubNumberFormat().run();