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