• 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-2012, International Business Machines
7  *   Corporation and others.  All Rights Reserved.
8  **/
9 
10 /**
11  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestDecimalFormatAPI
12  * Source File: java/text/format/IntlTestDecimalFormatAPI.java
13  **/
14 
15 /*
16     @test 1.4 98/03/06
17     @summary test International Decimal Format API
18 */
19 
20 package ohos.global.icu.dev.test.format;
21 
22 import java.text.FieldPosition;
23 import java.text.Format;
24 import java.text.ParseException;
25 import java.text.ParsePosition;
26 import java.util.Locale;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 
32 import ohos.global.icu.dev.test.TestFmwk;
33 import ohos.global.icu.math.BigDecimal;
34 import ohos.global.icu.math.MathContext;
35 import ohos.global.icu.text.DecimalFormat;
36 import ohos.global.icu.text.DecimalFormatSymbols;
37 import ohos.global.icu.text.NumberFormat;
38 
39 
40 
41 @RunWith(JUnit4.class)
42 public class IntlTestDecimalFormatAPI extends TestFmwk
43 {
44     /**
45      * Problem 1: simply running
46      * decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work
47      * as decF4.setRoundingIncrement(.0001) must also be run.
48      * Problem 2: decF4.format(8.88885) does not return 8.8889 as expected.
49      * You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in
50      * order for this to work as expected.
51      * Problem 3: There seems to be no way to set half up to be the default
52      * rounding mode.
53      * We solved the problem with the code at the bottom of this page however
54      * this is not quite general purpose enough to include in icu4j. A static
55      * setDefaultRoundingMode function would solve the problem nicely. Also
56      * decimal places past 20 are not handled properly. A small ammount of work
57      * would make bring this up to snuff.
58      */
59     @Test
testJB1871()60     public void testJB1871()
61     {
62         // problem 2
63         double number = 8.88885;
64         String expected = "8.8889";
65 
66         String pat = ",##0.0000";
67         DecimalFormat dec = new DecimalFormat(pat);
68         dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
69         dec.setRoundingIncrement(new java.math.BigDecimal("0.0001"));
70         String str = dec.format(number);
71         if (!str.equals(expected)) {
72             errln("Fail: " + number + " x \"" + pat + "\" = \"" +
73                   str + "\", expected \"" + expected + "\"");
74         }
75 
76         pat = ",##0.0001";
77         dec = new DecimalFormat(pat);
78         dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
79         str = dec.format(number);
80         if (!str.equals(expected)) {
81             errln("Fail: " + number + " x \"" + pat + "\" = \"" +
82                   str + "\", expected \"" + expected + "\"");
83         }
84 
85         // testing 20 decimal places
86         pat = ",##0.00000000000000000001";
87         dec = new DecimalFormat(pat);
88         BigDecimal bignumber = new BigDecimal("8.888888888888888888885");
89         expected = "8.88888888888888888889";
90 
91         dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
92         str = dec.format(bignumber);
93         if (!str.equals(expected)) {
94             errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" +
95                   str + "\", expected \"" + expected + "\"");
96         }
97 
98     }
99 
100     /**
101      * This test checks various generic API methods in DecimalFormat to achieve
102      * 100% API coverage.
103      */
104     @Test
TestAPI()105     public void TestAPI()
106     {
107         logln("DecimalFormat API test---"); logln("");
108         Locale.setDefault(Locale.ENGLISH);
109 
110         // ======= Test constructors
111 
112         logln("Testing DecimalFormat constructors");
113 
114         DecimalFormat def = new DecimalFormat();
115 
116         final String pattern = new String("#,##0.# FF");
117         DecimalFormat pat = null;
118         try {
119             pat = new DecimalFormat(pattern);
120         }
121         catch (IllegalArgumentException e) {
122             errln("ERROR: Could not create DecimalFormat (pattern)");
123         }
124 
125         DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
126 
127         DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
128 
129         // ======= Test clone(), assignment, and equality
130 
131         logln("Testing clone() and equality operators");
132 
133         Format clone = (Format) def.clone();
134         if( ! def.equals(clone)) {
135             errln("ERROR: Clone() failed");
136         }
137 
138         // ======= Test various format() methods
139 
140         logln("Testing various format() methods");
141 
142 //        final double d = -10456.0037; // this appears as -10456.003700000001 on NT
143 //        final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
144         final double d = -10456.00370000000000; // this works!
145         final long l = 100000000;
146         logln("" + d + " is the double value");
147 
148         StringBuffer res1 = new StringBuffer();
149         StringBuffer res2 = new StringBuffer();
150         StringBuffer res3 = new StringBuffer();
151         StringBuffer res4 = new StringBuffer();
152         FieldPosition pos1 = new FieldPosition(0);
153         FieldPosition pos2 = new FieldPosition(0);
154         FieldPosition pos3 = new FieldPosition(0);
155         FieldPosition pos4 = new FieldPosition(0);
156 
157         res1 = def.format(d, res1, pos1);
158         logln("" + d + " formatted to " + res1);
159 
160         res2 = pat.format(l, res2, pos2);
161         logln("" + l + " formatted to " + res2);
162 
163         res3 = cust1.format(d, res3, pos3);
164         logln("" + d + " formatted to " + res3);
165 
166         res4 = cust1.format(l, res4, pos4);
167         logln("" + l + " formatted to " + res4);
168 
169         // ======= Test parse()
170 
171         logln("Testing parse()");
172 
173         String text = new String("-10,456.0037");
174         ParsePosition pos = new ParsePosition(0);
175         String patt = new String("#,##0.#");
176         pat.applyPattern(patt);
177         double d2 = pat.parse(text, pos).doubleValue();
178         if(d2 != d) {
179             errln("ERROR: Roundtrip failed (via parse(" + d2 + " != " + d + ")) for " + text);
180         }
181         logln(text + " parsed into " + (long) d2);
182 
183         // ======= Test getters and setters
184 
185         logln("Testing getters and setters");
186 
187         final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
188         def.setDecimalFormatSymbols(syms);
189         if( ! pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
190             errln("ERROR: set DecimalFormatSymbols() failed");
191         }
192 
193         String posPrefix;
194         pat.setPositivePrefix("+");
195         posPrefix = pat.getPositivePrefix();
196         logln("Positive prefix (should be +): " + posPrefix);
197         assertEquals("ERROR: setPositivePrefix() failed", "+", posPrefix);
198 
199         String negPrefix;
200         pat.setNegativePrefix("-");
201         negPrefix = pat.getNegativePrefix();
202         logln("Negative prefix (should be -): " + negPrefix);
203         assertEquals("ERROR: setNegativePrefix() failed", "-", negPrefix);
204 
205         String posSuffix;
206         pat.setPositiveSuffix("_");
207         posSuffix = pat.getPositiveSuffix();
208         logln("Positive suffix (should be _): " + posSuffix);
209         assertEquals("ERROR: setPositiveSuffix() failed", "_", posSuffix);
210 
211         String negSuffix;
212         pat.setNegativeSuffix("~");
213         negSuffix = pat.getNegativeSuffix();
214         logln("Negative suffix (should be ~): " + negSuffix);
215         assertEquals("ERROR: setNegativeSuffix() failed", "~", negSuffix);
216 
217         long multiplier = 0;
218         pat.setMultiplier(8);
219         multiplier = pat.getMultiplier();
220         logln("Multiplier (should be 8): " + multiplier);
221         if(multiplier != 8) {
222             errln("ERROR: setMultiplier() failed");
223         }
224 
225         int groupingSize = 0;
226         pat.setGroupingSize(2);
227         groupingSize = pat.getGroupingSize();
228         logln("Grouping size (should be 2): " + (long) groupingSize);
229         if(groupingSize != 2) {
230             errln("ERROR: setGroupingSize() failed");
231         }
232 
233         pat.setDecimalSeparatorAlwaysShown(true);
234         boolean tf = pat.isDecimalSeparatorAlwaysShown();
235         logln("DecimalSeparatorIsAlwaysShown (should be true) is " +  (tf ? "true" : "false"));
236         if(tf != true) {
237             errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
238         }
239 
240         String funkyPat;
241         funkyPat = pat.toPattern();
242         logln("Pattern is " + funkyPat);
243 
244         String locPat;
245         locPat = pat.toLocalizedPattern();
246         logln("Localized pattern is " + locPat);
247 
248         // ======= Test applyPattern()
249 
250         logln("Testing applyPattern()");
251 
252         String p1 = new String("#,##0.0#;(#,##0.0#)");
253         logln("Applying pattern " + p1);
254         pat.applyPattern(p1);
255         String s2;
256         s2 = pat.toPattern();
257         logln("Extracted pattern is " + s2);
258         if( ! s2.equals(p1) ) {
259             errln("ERROR: toPattern() result did not match pattern applied");
260         }
261 
262         String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
263         logln("Applying pattern " + p2);
264         pat.applyLocalizedPattern(p2);
265         String s3;
266         s3 = pat.toLocalizedPattern();
267         logln("Extracted pattern is " + s3);
268         if( ! s3.equals(p2) ) {
269             errln("ERROR: toLocalizedPattern() result did not match pattern applied");
270         }
271     }
272 
273     @Test
testJB6134()274     public void testJB6134()
275     {
276         DecimalFormat decfmt = new DecimalFormat();
277         StringBuffer buf = new StringBuffer();
278 
279         FieldPosition fposByInt = new FieldPosition(NumberFormat.INTEGER_FIELD);
280         decfmt.format(123, buf, fposByInt);
281 
282         buf.setLength(0);
283         FieldPosition fposByField = new FieldPosition(NumberFormat.Field.INTEGER);
284         decfmt.format(123, buf, fposByField);
285 
286         if (fposByInt.getEndIndex() != fposByField.getEndIndex())
287         {
288             errln("ERROR: End index for integer field - fposByInt:" + fposByInt.getEndIndex() +
289                 " / fposByField: " + fposByField.getEndIndex());
290         }
291     }
292 
293     @Test
testJB4971()294     public void testJB4971()
295     {
296         DecimalFormat decfmt = new DecimalFormat();
297         MathContext resultICU;
298 
299         MathContext comp1 = new MathContext(0, MathContext.PLAIN, false, MathContext.ROUND_HALF_EVEN);
300         resultICU = decfmt.getMathContextICU();
301         if ((comp1.getDigits() != resultICU.getDigits()) ||
302             (comp1.getForm() != resultICU.getForm()) ||
303             (comp1.getLostDigits() != resultICU.getLostDigits()) ||
304             (comp1.getRoundingMode() != resultICU.getRoundingMode()))
305         {
306             errln("ERROR: Math context 1 not equal - result: " + resultICU.toString() +
307                 " / expected: " + comp1.toString());
308         }
309 
310         MathContext comp2 = new MathContext(5, MathContext.ENGINEERING, false, MathContext.ROUND_HALF_EVEN);
311         decfmt.setMathContextICU(comp2);
312         resultICU = decfmt.getMathContextICU();
313         if ((comp2.getDigits() != resultICU.getDigits()) ||
314             (comp2.getForm() != resultICU.getForm()) ||
315             (comp2.getLostDigits() != resultICU.getLostDigits()) ||
316             (comp2.getRoundingMode() != resultICU.getRoundingMode()))
317         {
318             errln("ERROR: Math context 2 not equal - result: " + resultICU.toString() +
319                 " / expected: " + comp2.toString());
320         }
321 
322         java.math.MathContext result;
323 
324         java.math.MathContext comp3 = new java.math.MathContext(3, java.math.RoundingMode.DOWN);
325         decfmt.setMathContext(comp3);
326         result = decfmt.getMathContext();
327         if ((comp3.getPrecision() != result.getPrecision()) ||
328             (comp3.getRoundingMode() != result.getRoundingMode()))
329         {
330             errln("ERROR: Math context 3 not equal - result: " + result.toString() +
331                 " / expected: " + comp3.toString());
332         }
333 
334     }
335 
336     @Test
testJB6354()337     public void testJB6354()
338     {
339         DecimalFormat pat = new DecimalFormat("#,##0.00");
340         java.math.BigDecimal r1, r2;
341 
342         // get default rounding increment
343         r1 = pat.getRoundingIncrement();
344 
345         // set rounding mode with zero increment.  Rounding
346         // increment should be set by this operation
347         pat.setRoundingMode(BigDecimal.ROUND_UP);
348         r2 = pat.getRoundingIncrement();
349 
350         // check for different values
351         if ((r1 != null) && (r2 != null))
352         {
353             if (r1.compareTo(r2) == 0)
354             {
355                 errln("ERROR: Rounding increment did not change");
356             }
357         }
358     }
359 
360     @Test
testJB6648()361     public void testJB6648()
362     {
363         DecimalFormat df = new DecimalFormat();
364         df.setParseStrict(true);
365 
366         String numstr = new String();
367 
368         String[] patterns = {
369             "0",
370             "00",
371             "000",
372             "0,000",
373             "0.0",
374             "#000.0"
375         };
376 
377         for(int i=0; i < patterns.length; i++) {
378             df.applyPattern(patterns[i]);
379             numstr = df.format(5);
380             try {
381                 Number n = df.parse(numstr);
382                 logln("INFO: Parsed " + numstr + " -> " + n);
383             } catch (ParseException pe) {
384                 errln("ERROR: Failed round trip with strict parsing.");
385             }
386         }
387 
388         df.applyPattern(patterns[1]);
389         numstr = "005";
390         try {
391             Number n = df.parse(numstr);
392             logln("INFO: Successful parse for " + numstr + " with strict parse enabled. Number is " + n);
393         } catch (ParseException pe) {
394             errln("ERROR: Parse Exception encountered in strict mode: numstr -> " + numstr);
395         }
396 
397     }
398 }
399