1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
// Test simple forms of DecimalFormat.format.
// Copyright (c) 1999, 2003 Cygnus Solutions
// Written by Tom Tromey <tromey@cygnus.com>
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// Tags: JDK1.1
package gnu.testlet.java.text.DecimalFormat;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.Locale;
public class format
implements Testlet
{
public void test(TestHarness harness)
{
testGeneral(harness);
testRounding(harness);
testMiscellaneous(harness);
testBigInteger(harness);
testNaN(harness);
testInfinity(harness);
testMaximumDigits(harness);
testLocale(harness);
}
public void apply(TestHarness harness, DecimalFormat df, String pattern)
{
harness.checkPoint("pattern " + pattern);
boolean ok = true;
try
{
df.applyPattern(pattern);
}
catch (IllegalArgumentException x)
{
ok = false;
}
harness.check(ok);
}
public void testGeneral(TestHarness harness)
{
// Just to be explicit: we're only testing the US locale here.
Locale loc = Locale.US;
Locale.setDefault(loc);
// Some tests taken from JCL book.
DecimalFormat df = new DecimalFormat("0.##;-0.##");
harness.check(df.format(- 1234.56), "-1234.56");
harness.check(df.format(1234.56), "1234.56");
apply(harness, df, "0.#");
harness.check(df.format(- 1234.56), "-1234.6");
harness.check(df.format(1234.56), "1234.6");
apply(harness, df, "#,##0.##;-#");
harness.check(df.format(- 1234.56), "-1,234.56");
harness.check(df.format(1234.56), "1,234.56");
apply(harness, df, "#,##0.###");
harness.check(df.format(Double.valueOf(80).doubleValue()), "80");
apply(harness, df, "00,000.000;-00,000.000");
harness.check(df.format(- 1234.56), "-01,234.560");
harness.check(df.format(1234.56), "01,234.560");
apply(harness, df, "##,###,####.");
df.setDecimalSeparatorAlwaysShown(true);
harness.check(df.format(- 1234.56), "-1235.");
harness.check(df.format(1234.56), "1235.");
harness.check(df.format(-1234567.890), "-123,4568.");
apply(harness, df, "#,###,###");
harness.check(df.format(-1234567.890), "-1,234,568");
apply(harness, df, "0");
harness.check(df.format(- 1234.56), "-1235");
harness.check(df.format(1234.56), "1235");
harness.check(df.format(Long.MIN_VALUE), "-9223372036854775808");
apply(harness, df, "#");
harness.check(df.format(0), "0");
harness.check(df.format(0.0), "0");
apply(harness, df, "###0.#;(###0.#)");
harness.check(df.format(- 1234.56), "(1234.6)");
harness.check(df.format(1234.56), "1234.6");
apply(harness, df, "###0.#;###0.#-");
harness.check(df.format(- 1234.56), "1234.6-");
harness.check(df.format(1234.56), "1234.6");
apply(harness, df, "#,##0%;-#,##0%");
harness.check(df.format(- 1234.56), "-123,456%");
harness.check(df.format(1234.56), "123,456%");
apply(harness, df, "#.#");
harness.check(df.format(0.2), "0.2");
apply(harness, df, "'#'#.#");
harness.check(df.format(30), "#30");
apply(harness, df, "000000");
harness.check(df.format(-1234.567), "-001235");
apply(harness, df, "##");
harness.check(df.format(-1234.567), "-1235");
harness.check(df.format(0), "0");
apply(harness, df, "##00");
harness.check(df.format(0), "00");
apply(harness, df, ".00");
harness.check(df.format(-.567), "-.57");
apply(harness, df, "0.00");
harness.check(df.format(-.567), "-0.57");
apply(harness, df, ".######");
harness.check(df.format(-1234.567), "-1234.567");
apply(harness, df, "#.000000");
harness.check(df.format(-1234.567), "-1234.567000");
apply(harness, df, "'#'#");
harness.check(df.format(-1234.567), "-#1235");
apply(harness, df, "'abc'#");
harness.check(df.format(-1234.567), "-abc1235");
apply(harness, df, "'positive'#;'negative' -");
harness.check(df.format(-1234.567), "negative -1235");
harness.check(df.format(1234.567), "positive1235");
apply(harness, df, "#,##0%");
harness.check(df.format(10000000.1234d), "1,000,000,012%");
apply(harness, df, "\u00A4#,##0.00;(\u00A4#,##0.00)");
harness.check(df.format(10000), "$10,000.00");
apply(harness, df, "$#,##0.00;($#,##0.00)");
harness.check(df.format(10000), "$10,000.00");
// grouping size of zero might cause a failure - see bug parade 4088503
harness.checkPoint("regression tests for setGroupingSize");
df = new DecimalFormat();
df.setGroupingSize(0);
harness.check(df.format(100000), "100000");
harness.check(df.isGroupingUsed());
harness.check(df.getGroupingSize(), 0);
// FIXME: we don't actually know the right result here, because
// neither the JCL book nor the JDK 1.2 docs explain what should
// happen. The below represents how I think things ought to
// work. However, Sun has a different (and more confusing)
// idea. E.g., JDK1.1 prints "200000.0000E" in the first case.
// apply (harness, df, "0.0000E#");
// harness.check (df.format (200000), "2.0000E+5");
// apply (harness, df, "00.00E00");
// harness.check (df.format (200000), "20.00E+04");
}
/**
* Checks that rounding behaviour follows "half-even" rounding. For example,
* see bug parade 4763975.
*
* @param harness the harness.
*/
private void testRounding(TestHarness harness)
{
harness.checkPoint("DecimalFormat rounding");
Locale original = Locale.getDefault();
Locale.setDefault(Locale.UK);
DecimalFormat f = new DecimalFormat("0.00");
harness.check(f.format(1.225), "1.22");
harness.check(f.format(1.235), "1.24");
Locale.setDefault(original);
}
private void testMiscellaneous(TestHarness harness)
{
harness.checkPoint("DecimalFormat: misc");
Locale original = Locale.getDefault();
Locale.setDefault(Locale.UK);
DecimalFormat f = new DecimalFormat("0");
// try formatting a null object
boolean pass = false;
try
{
f.format(null);
}
catch (IllegalArgumentException e)
{
pass = true;
}
harness.check(pass);
// try formatting an object that is not a Number
pass = false;
try
{
f.format("XYZ");
}
catch (IllegalArgumentException e)
{
pass = true;
}
harness.check(pass);
// some implementations can't handle custom subclasses of Number
pass = true;
try
{
f.format(new Number()
{
public float floatValue()
{
return 0.0f;
}
public double doubleValue()
{
return 0.0f;
}
public long longValue()
{
return 0l;
}
public int intValue()
{
return 0;
}
});
}
catch (Exception e)
{
pass = false;
}
harness.check(pass);
Locale.setDefault(original);
}
/**
* See PR 28462.
*/
private void testBigInteger(TestHarness harness)
{
Locale orig = Locale.getDefault();
Locale.setDefault(Locale.US);
harness.checkPoint("BigInteger format");
String expect = "123,456,789,012,345,678,901,234,567,890";
BigInteger bi = new BigInteger("123456789012345678901234567890", 10);
DecimalFormat df = new DecimalFormat();
harness.check(df.format(bi), expect);
Locale.setDefault(orig);
}
/**
* @param harness
*/
private void testInfinity(TestHarness harness)
{
Locale orig = Locale.getDefault();
Locale.setDefault(Locale.US);
harness.checkPoint("testInfinity");
String expectPositive = "\u221E";
String expectNegative = "-\u221E";
double positiveInf = Double.longBitsToDouble(0x7ff0000000000000L);
double negativeInf = Double.longBitsToDouble(0xfff0000000000000L);
DecimalFormat df = new DecimalFormat();
harness.check(df.format(positiveInf), expectPositive, "positive inf.");
harness.check(df.format(negativeInf), expectNegative, "negative inf.");
Locale.setDefault(orig);
}
/**
* @param harness
*/
private void testNaN(TestHarness harness)
{
Locale orig = Locale.getDefault();
Locale.setDefault(Locale.US);
harness.checkPoint("testNaN");
String expect = "\uFFFD";
double nan = Double.longBitsToDouble(0x7ff8000000000000L);
DecimalFormat df = new DecimalFormat();
// NaN does not have prefixes and suffixes
harness.check(df.format(nan), expect);
harness.check(df.format(-nan), expect, "NaN with a negative sign as pefix");
Locale.setDefault(orig);
}
private void testMaximumDigits(TestHarness harness)
{
Locale orig = Locale.getDefault();
Locale.setDefault(Locale.US);
harness.checkPoint("testMaxAndMinDigits");
double number = 123456789.987654321;
DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);
df.setGroupingSize(3);
df.setMaximumIntegerDigits(2);
df.setMaximumFractionDigits(4);
// NaN does not have prefixes and suffixes
harness.check(df.format(number), "89.9877");
df.setMaximumIntegerDigits(5);
df.setMaximumFractionDigits(0);
harness.check(df.format(number), "56790");
df.setMaximumIntegerDigits(0);
df.setMaximumFractionDigits(5);
harness.check(df.format(number), ".98765");
df.setMaximumIntegerDigits(-1);
df.setMaximumFractionDigits(-1);
harness.check(df.format(number), "0");
df.setMaximumIntegerDigits(390);
df.setMaximumFractionDigits(340);
harness.check(df.format(number), "123456789.98765433");
Locale.setDefault(orig);
}
private void testLocale(TestHarness harness)
{
// just two tests, other are in gnu.testlet.locales.LocaleTest
harness.checkPoint("locale: GERMANY");
// by default, calls DecimalFormat
java.text.NumberFormat nf
= java.text.NumberFormat.getCurrencyInstance(Locale.GERMANY);
harness.check(nf.format(5000.25), "5.000,25 €");
harness.checkPoint("locale: ITALY");
nf = java.text.NumberFormat.getCurrencyInstance(Locale.ITALY);
harness.check(nf.format(5000.25), "€ 5.000,25");
java.text.DecimalFormatSymbols symbols
= ((DecimalFormat)nf).getDecimalFormatSymbols();
harness.check(',', symbols.getDecimalSeparator());
harness.check(',', symbols.getMonetaryDecimalSeparator());
harness.check('.', symbols.getGroupingSeparator());
}
}
|