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