001: //##header
002: /*
003: *******************************************************************************
004: * Copyright (C) 2001-2005, International Business Machines Corporation and *
005: * others. All Rights Reserved. *
006: *******************************************************************************
007: */
008:
009: /**
010: * Port From: ICU4C v1.8.1 : format : NumberFormatRegressionTest
011: * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp
012: **/package com.ibm.icu.dev.test.format;
013:
014: import com.ibm.icu.text.*;
015: import com.ibm.icu.util.*;
016: import java.util.Locale;
017: import java.util.Date;
018: import java.text.ParseException;
019: import java.io.*;
020:
021: /**
022: * Performs regression test for MessageFormat
023: **/
024: public class NumberFormatRegressionTest extends
025: com.ibm.icu.dev.test.TestFmwk {
026:
027: public static void main(String[] args) throws Exception {
028: new NumberFormatRegressionTest().run(args);
029: }
030:
031: /**
032: * alphaWorks upgrade
033: */
034: public void Test4161100() {
035: NumberFormat nf = NumberFormat.getInstance(Locale.US);
036: nf.setMinimumFractionDigits(1);
037: nf.setMaximumFractionDigits(1);
038: double a = -0.09;
039: String s = nf.format(a);
040: logln(a + " x " + ((DecimalFormat) nf).toPattern() + " = " + s);
041: if (!s.equals("-0.1")) {
042: errln("FAIL");
043: }
044: }
045:
046: /**
047: * DateFormat should call setIntegerParseOnly(TRUE) on adopted
048: * NumberFormat objects.
049: */
050: public void TestJ691() {
051:
052: Locale loc = new Locale("fr", "CH");
053:
054: // set up the input date string & expected output
055: String udt = "11.10.2000";
056: String exp = "11.10.00";
057:
058: // create a Calendar for this locale
059: Calendar cal = Calendar.getInstance(loc);
060:
061: // create a NumberFormat for this locale
062: NumberFormat nf = NumberFormat.getInstance(loc);
063:
064: // *** Here's the key: We don't want to have to do THIS:
065: //nf.setParseIntegerOnly(true);
066:
067: // create the DateFormat
068: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT,
069: loc);
070:
071: df.setCalendar(cal);
072: df.setNumberFormat(nf);
073:
074: // set parsing to lenient & parse
075: Date ulocdat = new Date();
076: df.setLenient(true);
077: try {
078: ulocdat = df.parse(udt);
079: } catch (java.text.ParseException pe) {
080: errln(pe.getMessage());
081: }
082: // format back to a string
083: String outString = df.format(ulocdat);
084:
085: if (!outString.equals(exp)) {
086: errln("FAIL: " + udt + " => " + outString);
087: }
088: }
089:
090: /**
091: * Test getIntegerInstance();
092: */
093: public void Test4408066() {
094:
095: NumberFormat nf1 = NumberFormat.getIntegerInstance();
096: NumberFormat nf2 = NumberFormat
097: .getIntegerInstance(Locale.CHINA);
098:
099: //test isParseIntegerOnly
100: if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) {
101: errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)");
102: }
103:
104: //Test format
105: {
106: double[] data = { -3.75, -2.5, -1.5, -1.25, 0, 1.0, 1.25,
107: 1.5, 2.5, 3.75, 10.0, 255.5 };
108: String[] expected = { "-4", "-2", "-2", "-1", "0", "1",
109: "1", "2", "2", "4", "10", "256" };
110:
111: for (int i = 0; i < data.length; ++i) {
112: String result = nf1.format(data[i]);
113: if (!result.equals(expected[i])) {
114: errln("Failed => Source: "
115: + Double.toString(data[i])
116: + ";Formatted : " + result
117: + ";but expectted: " + expected[i]);
118: }
119: }
120: }
121: //Test parse, Parsing should stop at "."
122: {
123: String data[] = { "-3.75", "-2.5", "-1.5", "-1.25", "0",
124: "1.0", "1.25", "1.5", "2.5", "3.75", "10.0",
125: "255.5" };
126: long[] expected = { -3, -2, -1, -1, 0, 1, 1, 1, 2, 3, 10,
127: 255 };
128:
129: for (int i = 0; i < data.length; ++i) {
130: Number n = null;
131: try {
132: n = nf1.parse(data[i]);
133: } catch (ParseException e) {
134: errln("Failed: " + e.getMessage());
135: }
136: if (!(n instanceof Long) || (n instanceof Integer)) {
137: errln("Failed: Integer Number Format should parse string to Long/Integer");
138: }
139: if (n.longValue() != expected[i]) {
140: errln("Failed=> Source: " + data[i] + ";result : "
141: + n.toString() + ";expected :"
142: + Long.toString(expected[i]));
143: }
144: }
145: }
146: }
147:
148: //Test New serialized DecimalFormat(2.0) read old serialized forms of DecimalFormat(1.3.1.1)
149: public void TestSerialization() throws IOException,
150: ClassNotFoundException {
151: //#ifndef FOUNDATION
152: byte[][] contents = NumberFormatSerialTestData.getContent();
153: double data = 1234.56;
154: String[] expected = { "1,234.56", "$1,234.56", "123,456%",
155: "1.23456E3" };
156: for (int i = 0; i < 4; ++i) {
157: ObjectInputStream ois = new ObjectInputStream(
158: new ByteArrayInputStream(contents[i]));
159: try {
160: NumberFormat format = (NumberFormat) ois.readObject();
161: String result = format.format(data);
162: if (result.equals(expected[i])) {
163: logln("OK: Deserialized bogus NumberFormat(new version read old version)");
164: } else {
165: errln("FAIL: the test data formats are not euqal");
166: }
167: } catch (Exception e) {
168: warnln("FAIL: " + e.getMessage());
169: }
170: }
171: //#endif
172: }
173: }
|