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