3 *******************************************************************************
4 * Copyright (C) 2001-2009, International Business Machines Corporation and *
5 * others. All Rights Reserved. *
6 *******************************************************************************
10 * Port From: ICU4C v1.8.1 : format : NumberFormatRegressionTest
11 * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp
14 package com.ibm.icu.dev.test.format;
16 import com.ibm.icu.text.*;
17 import com.ibm.icu.util.*;
18 import java.util.Locale;
19 import java.util.Date;
20 import java.text.ParseException;
24 * Performs regression test for MessageFormat
26 public class NumberFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk {
28 public static void main(String[] args) throws Exception{
29 new NumberFormatRegressionTest().run(args);
35 public void Test4161100() {
36 NumberFormat nf = NumberFormat.getInstance(Locale.US);
37 nf.setMinimumFractionDigits(1);
38 nf.setMaximumFractionDigits(1);
40 String s = nf.format(a);
42 ((DecimalFormat) nf).toPattern() + " = " + s);
43 if (!s.equals("-0.1")) {
49 * DateFormat should call setIntegerParseOnly(TRUE) on adopted
50 * NumberFormat objects.
52 public void TestJ691() {
54 Locale loc = new Locale("fr", "CH");
56 // set up the input date string & expected output
57 String udt = "11.10.2000";
58 String exp = "11.10.00";
60 // create a Calendar for this locale
61 Calendar cal = Calendar.getInstance(loc);
63 // create a NumberFormat for this locale
64 NumberFormat nf = NumberFormat.getInstance(loc);
66 // *** Here's the key: We don't want to have to do THIS:
67 //nf.setParseIntegerOnly(true);
69 // create the DateFormat
70 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);
73 df.setNumberFormat(nf);
75 // set parsing to lenient & parse
76 Date ulocdat = new Date();
79 ulocdat = df.parse(udt);
80 } catch (java.text.ParseException pe) {
81 errln(pe.getMessage());
83 // format back to a string
84 String outString = df.format(ulocdat);
86 if (!outString.equals(exp)) {
87 errln("FAIL: " + udt + " => " + outString);
92 * Test getIntegerInstance();
94 public void Test4408066() {
96 NumberFormat nf1 = NumberFormat.getIntegerInstance();
97 NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);
99 //test isParseIntegerOnly
100 if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) {
101 errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)");
112 String[] expected = {
119 for (int i = 0; i < data.length; ++i) {
120 String result = nf1.format(data[i]);
121 if (!result.equals(expected[i])) {
122 errln("Failed => Source: " + Double.toString(data[i])
123 + ";Formatted : " + result
124 + ";but expectted: " + expected[i]);
128 //Test parse, Parsing should stop at "."
131 "-3.75", "-2.5", "-1.5",
133 "1.25", "1.5", "2.5",
134 "3.75", "10.0", "255.5"
143 for (int i = 0; i < data.length; ++i) {
146 n = nf1.parse(data[i]);
147 } catch (ParseException e) {
148 errln("Failed: " + e.getMessage());
150 if (!(n instanceof Long) || (n instanceof Integer)) {
151 errln("Failed: Integer Number Format should parse string to Long/Integer");
153 if (n.longValue() != expected[i]) {
154 errln("Failed=> Source: " + data[i]
155 + ";result : " + n.toString()
156 + ";expected :" + Long.toString(expected[i]));
162 //Test New serialized DecimalFormat(2.0) read old serialized forms of DecimalFormat(1.3.1.1)
163 public void TestSerialization() throws IOException{
164 //#if defined(FOUNDATION10) || defined(J2SE13)
166 byte[][] contents = NumberFormatSerialTestData.getContent();
167 double data = 1234.56;
168 String[] expected = {
169 "1,234.56", "$1,234.56", "123,456%", "1.23456E3"};
170 for (int i = 0; i < 4; ++i) {
171 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(contents[i]));
173 NumberFormat format = (NumberFormat) ois.readObject();
174 String result = format.format(data);
175 if (result.equals(expected[i])) {
176 logln("OK: Deserialized bogus NumberFormat(new version read old version)");
178 errln("FAIL: the test data formats are not euqal");
180 } catch (Exception e) {
181 warnln("FAIL: " + e.getMessage());
188 * Test case for JB#5509, strict parsing issue
190 public void TestJB5509() {
204 boolean[] expected = { // false for expected parse failure
219 DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(new ULocale("en_US")));
220 df.setParseStrict(true);
221 for (int i = 0; i < data.length; i++) {
225 errln("Failed: ParseException must be thrown for string " + data[i]);
227 } catch (ParseException pe) {
229 errln("Failed: ParseException must not be thrown for string " + data[i]);
236 * Test case for ticket#5698 - parsing extremely large/small values
238 public void TestT5698() {
239 final String[] data = {
240 "12345679E66666666666666666",
241 "-12345679E66666666666666666",
242 ".1E2147483648", // exponent > max int
243 ".1E2147483647", // exponent == max int
244 ".1E-2147483648", // exponent == min int
245 ".1E-2147483649", // exponent < min int
246 "1.23E350", // value > max double
247 "1.23E300", // value < max double
248 "-1.23E350", // value < min double
249 "-1.23E300", // value > min double
250 "4.9E-324", // value = smallest non-zero double
251 "1.0E-325", // 0 < value < smallest non-zero positive double0
252 "-1.0E-325", // 0 > value > largest non-zero negative double
254 final double[] expected = {
255 Double.POSITIVE_INFINITY,
256 Double.NEGATIVE_INFINITY,
257 Double.POSITIVE_INFINITY,
258 Double.POSITIVE_INFINITY,
261 Double.POSITIVE_INFINITY,
263 Double.NEGATIVE_INFINITY,
270 NumberFormat nfmt = NumberFormat.getInstance();
272 for (int i = 0; i < data.length; i++) {
274 Number n = nfmt.parse(data[i]);
275 if (expected[i] != n.doubleValue()) {
276 errln("Failed: Parsed result for " + data[i] + ": "
277 + n.doubleValue() + " / expected: " + expected[i]);
279 } catch (ParseException pe) {
280 errln("Failed: ParseException is thrown for " + data[i]);
285 void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber) {
286 String myString = nf.format(myNumber);
290 aNumber = nf.parse(myString).doubleValue();
291 } catch (ParseException e) {
292 // TODO Auto-generated catch block
293 errln("FAIL: " + testcase +" - failed to parse. " + e.toString());
296 if(Math.abs(aNumber-myNumber)>.001) {
297 errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");
299 logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");
303 void checkNBSPPatternRT(String testcase, NumberFormat nf) {
304 checkNBSPPatternRtNum(testcase, nf, 12345.);
305 checkNBSPPatternRtNum(testcase, nf, -12345.);
308 public void TestNBSPInPattern() {
309 NumberFormat nf = null;
313 testcase="ar_AE UNUM_CURRENCY";
314 nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE"));
315 checkNBSPPatternRT(testcase, nf);
316 // if we don't have CLDR 1.6 data, bring out the problem anyways
318 String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00";
319 testcase = "ar_AE special pattern: " + SPECIAL_PATTERN;
320 nf = new DecimalFormat();
321 ((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN);
322 checkNBSPPatternRT(testcase, nf);