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