1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html#License 3/***************************************************************************************** 4 * 5 * Copyright (C) 1996-2009, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 **/ 8/** 9 * Port From: JDK 1.4b1 : java.text.Format.IntlTestNumberFormatAPI 10 * Source File: java/text/format/IntlTestNumberFormatAPI.java 11 **/ 12 13/* 14 @test 1.4 98/03/06 15 @summary test International Number Format API 16*/ 17 18package com.ibm.icu.dev.test.format; 19 20import java.math.BigInteger; 21import java.text.FieldPosition; 22import java.text.ParseException; 23import java.text.ParsePosition; 24import java.util.Locale; 25 26import org.junit.Test; 27 28import com.ibm.icu.text.NumberFormat; 29import com.ibm.icu.util.ULocale; 30 31public class IntlTestNumberFormatAPI extends com.ibm.icu.dev.test.TestFmwk 32{ 33 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage. 34 @Test 35 public void TestAPI() 36 { 37 logln("NumberFormat API test---"); logln(""); 38 Locale.setDefault(Locale.ENGLISH); 39 40 // ======= Test constructors 41 42 logln("Testing NumberFormat constructors"); 43 44 NumberFormat def = NumberFormat.getInstance(); 45 46 NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH); 47 48 NumberFormat cur = NumberFormat.getCurrencyInstance(); 49 50 NumberFormat cur_fr = NumberFormat.getCurrencyInstance(Locale.FRENCH); 51 52 NumberFormat per = NumberFormat.getPercentInstance(); 53 54 NumberFormat per_fr = NumberFormat.getPercentInstance(Locale.FRENCH); 55 56 NumberFormat integer = NumberFormat.getIntegerInstance(); 57 58 NumberFormat int_fr = NumberFormat.getIntegerInstance(Locale.FRENCH); 59 60 //Fix "The variable is never used" compilation warnings 61 logln("Currency : " + cur.format(1234.5)); 62 logln("Percent : " + per.format(1234.5)); 63 logln("Integer : " + integer.format(1234.5)); 64 logln("Int_fr : " + int_fr.format(1234.5)); 65 66 // ======= Test equality 67 68 logln("Testing equality operator"); 69 70 if( per_fr.equals(cur_fr) ) { 71 errln("ERROR: == failed"); 72 } 73 74 // ======= Test various format() methods 75 76 logln("Testing various format() methods"); 77 78// final double d = -10456.0037; // this appears as -10456.003700000001 on NT 79// final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT 80 final double d = -10456.00370000000000; // this works! 81 final long l = 100000000; 82 83 String res1 = new String(); 84 String res2 = new String(); 85 StringBuffer res3 = new StringBuffer(); 86 StringBuffer res4 = new StringBuffer(); 87 StringBuffer res5 = new StringBuffer(); 88 StringBuffer res6 = new StringBuffer(); 89 FieldPosition pos1 = new FieldPosition(0); 90 FieldPosition pos2 = new FieldPosition(0); 91 FieldPosition pos3 = new FieldPosition(0); 92 FieldPosition pos4 = new FieldPosition(0); 93 94 res1 = cur_fr.format(d); 95 logln( "" + d + " formatted to " + res1); 96 97 res2 = cur_fr.format(l); 98 logln("" + l + " formatted to " + res2); 99 100 res3 = cur_fr.format(d, res3, pos1); 101 logln( "" + d + " formatted to " + res3); 102 103 res4 = cur_fr.format(l, res4, pos2); 104 logln("" + l + " formatted to " + res4); 105 106 res5 = cur_fr.format(d, res5, pos3); 107 logln("" + d + " formatted to " + res5); 108 109 res6 = cur_fr.format(l, res6, pos4); 110 logln("" + l + " formatted to " + res6); 111 112 113 // ======= Test parse() 114 115 logln("Testing parse()"); 116 117// String text = new String("-10,456.0037"); 118 String text = new String("-10456,0037"); 119 ParsePosition pos = new ParsePosition(0); 120 ParsePosition pos01 = new ParsePosition(0); 121 double d1 = ((Number)fr.parseObject(text, pos)).doubleValue(); 122 if(d1 != d) { 123 errln("ERROR: Roundtrip failed (via parse()) for " + text); 124 } 125 logln(text + " parsed into " + d1); 126 127 double d2 = fr.parse(text, pos01).doubleValue(); 128 if(d2 != d) { 129 errln("ERROR: Roundtrip failed (via parse()) for " + text); 130 } 131 logln(text + " parsed into " + d2); 132 133 double d3 = 0; 134 try { 135 d3 = fr.parse(text).doubleValue(); 136 } 137 catch (ParseException e) { 138 errln("ERROR: parse() failed"); 139 } 140 if(d3 != d) { 141 errln("ERROR: Roundtrip failed (via parse()) for " + text); 142 } 143 logln(text + " parsed into " + d3); 144 145 146 // ======= Test getters and setters 147 148 logln("Testing getters and setters"); 149 150 final Locale[] locales = NumberFormat.getAvailableLocales(); 151 long count = locales.length; 152 logln("Got " + count + " locales" ); 153 for(int i = 0; i < count; i++) { 154 String name; 155 name = locales[i].getDisplayName(); 156 logln(name); 157 } 158 159 fr.setParseIntegerOnly( def.isParseIntegerOnly() ); 160 if(fr.isParseIntegerOnly() != def.isParseIntegerOnly() ) { 161 errln("ERROR: setParseIntegerOnly() failed"); 162 } 163 164 fr.setGroupingUsed( def.isGroupingUsed() ); 165 if(fr.isGroupingUsed() != def.isGroupingUsed() ) { 166 errln("ERROR: setGroupingUsed() failed"); 167 } 168 169 fr.setMaximumIntegerDigits( def.getMaximumIntegerDigits() ); 170 if(fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits() ) { 171 errln("ERROR: setMaximumIntegerDigits() failed"); 172 } 173 174 fr.setMinimumIntegerDigits( def.getMinimumIntegerDigits() ); 175 if(fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits() ) { 176 errln("ERROR: setMinimumIntegerDigits() failed"); 177 } 178 179 fr.setMaximumFractionDigits( def.getMaximumFractionDigits() ); 180 if(fr.getMaximumFractionDigits() != def.getMaximumFractionDigits() ) { 181 errln("ERROR: setMaximumFractionDigits() failed"); 182 } 183 184 fr.setMinimumFractionDigits( def.getMinimumFractionDigits() ); 185 if(fr.getMinimumFractionDigits() != def.getMinimumFractionDigits() ) { 186 errln("ERROR: setMinimumFractionDigits() failed"); 187 } 188 189 // ======= Test getStaticClassID() 190 191// logln("Testing instanceof()"); 192 193// try { 194// NumberFormat test = new DecimalFormat(); 195 196// if (! (test instanceof DecimalFormat)) { 197// errln("ERROR: instanceof failed"); 198// } 199// } 200// catch (Exception e) { 201// errln("ERROR: Couldn't create a DecimalFormat"); 202// } 203 } 204 205 // Jitterbug 4451, for coverage 206 @Test 207 public void TestCoverage(){ 208 class StubNumberFormat extends NumberFormat{ 209 /** 210 * For serialization 211 */ 212 private static final long serialVersionUID = 3768385020503005993L; 213 public void run(){ 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,)"); 217 } 218 } 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 public StringBuffer format(java.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;} 223 public StringBuffer format(com.ibm.icu.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;} 224 public Number parse(String text, ParsePosition parsePosition) {return null;} 225 } 226 new StubNumberFormat().run(); 227 } 228} 229