1 /* 2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24 /* Type-specific source code for unit test 25 * 26 * Regenerate the BasicX classes via genBasic.sh whenever this file changes. 27 * We check in the generated source files so that the test tree can be used 28 * independently of the rest of the source tree. 29 */ 30 31 #warn This file is preprocessed before being compiled 32 33 import java.io.*; 34 import java.math.BigDecimal; 35 import java.math.BigInteger; 36 import java.text.DateFormatSymbols; 37 import java.util.*; 38 #if[double] 39 import sun.misc.FpUtils; 40 import sun.misc.DoubleConsts; 41 #end[double] 42 43 import static java.util.Calendar.*; 44 #if[datetime] 45 import static java.util.SimpleTimeZone.*; 46 import java.util.regex.Pattern; 47 #end[datetime] 48 49 public class Basic$Type$ extends Basic { 50 51 private static void test(String fs, String exp, Object ... args) { 52 Formatter f = new Formatter(new StringBuilder(), Locale.US); 53 f.format(fs, args); 54 ck(fs, exp, f.toString()); 55 } 56 57 private static void test(Locale l, String fs, String exp, Object ... args) 58 { 59 Formatter f = new Formatter(new StringBuilder(), l); 60 f.format(fs, args); 61 ck(fs, exp, f.toString()); 62 } 63 64 private static void test(String fs, Object ... args) { 65 Formatter f = new Formatter(new StringBuilder(), Locale.US); 66 f.format(fs, args); 67 ck(fs, "fail", f.toString()); 68 } 69 70 private static void test(String fs) { 71 Formatter f = new Formatter(new StringBuilder(), Locale.US); 72 f.format(fs, "fail"); 73 ck(fs, "fail", f.toString()); 74 } 75 76 private static void testSysOut(String fs, String exp, Object ... args) { 77 FileOutputStream fos = null; 78 FileInputStream fis = null; 79 try { 80 PrintStream saveOut = System.out; 81 fos = new FileOutputStream("testSysOut"); 82 System.setOut(new PrintStream(fos)); 83 System.out.format(Locale.US, fs, args); 84 fos.close(); 85 86 fis = new FileInputStream("testSysOut"); 87 byte [] ba = new byte[exp.length()]; 88 int len = fis.read(ba); 89 String got = new String(ba); 90 if (len != ba.length) 91 fail(fs, exp, got); 92 ck(fs, exp, got); 93 94 System.setOut(saveOut); 95 } catch (FileNotFoundException ex) { 96 fail(fs, ex.getClass()); 97 } catch (IOException ex) { 98 fail(fs, ex.getClass()); 99 } finally { 100 try { 101 if (fos != null) 102 fos.close(); 103 if (fis != null) 104 fis.close(); 105 } catch (IOException ex) { 106 fail(fs, ex.getClass()); 107 } 108 } 109 } 110 111 private static void tryCatch(String fs, Class<?> ex) { 112 boolean caught = false; 113 try { 114 test(fs); 115 } catch (Throwable x) { 116 if (ex.isAssignableFrom(x.getClass())) 117 caught = true; 118 } 119 if (!caught) 120 fail(fs, ex); 121 else 122 pass(); 123 } 124 125 private static void tryCatch(String fs, Class<?> ex, Object ... args) { 126 boolean caught = false; 127 try { 128 test(fs, args); 129 } catch (Throwable x) { 130 if (ex.isAssignableFrom(x.getClass())) 131 caught = true; 132 } 133 if (!caught) 134 fail(fs, ex); 135 else 136 pass(); 137 } 138 139 #if[datetime] 140 private static void testDateTime(String fs, String exp, Calendar c) { 141 testDateTime(fs, exp, c, true); 142 } 143 144 private static void testDateTime(String fs, String exp, Calendar c, boolean upper) { 145 //--------------------------------------------------------------------- 146 // Date/Time conversions applicable to Calendar, Date, and long. 147 //--------------------------------------------------------------------- 148 149 // Calendar 150 test(fs, exp, c); 151 test((Locale)null, fs, exp, c); 152 test(Locale.US, fs, exp, c); 153 154 // Date/long do not have timezone information so they will always use 155 // the default timezone. 156 String nexp = (fs.equals("%tZ") || fs.equals("%TZ") 157 || fs.equals("%tc") || fs.equals("%Tc") 158 ? exp.replace("PST", "GMT-08:00") 159 : exp); 160 161 // Date (implemented via conversion to Calendar) 162 Date d = c.getTime(); 163 test(fs, nexp, d); 164 test((Locale)null, fs, nexp, d); 165 test(Locale.US, fs, nexp, d); 166 167 // long (implemented via conversion to Calendar) 168 long l = c.getTimeInMillis(); 169 test(fs, nexp, l); 170 test((Locale)null, fs, nexp, l); 171 test(Locale.US, fs, nexp, l); 172 173 if (upper) 174 // repeat all tests for upper case variant (%T) 175 testDateTime(Pattern.compile("t").matcher(fs).replaceFirst("T"), 176 exp.toUpperCase(), c, false); 177 } 178 179 private static void testHours() { 180 for (int i = 0; i < 24; i++) { 181 // GregorianCalendar(int year, int month, int dayOfMonth, 182 // int hourOfDay, int minute, int second); 183 Calendar c = new GregorianCalendar(1995, MAY, 23, i, 48, 34); 184 185 //----------------------------------------------------------------- 186 // DateTime.HOUR_OF_DAY - 'k' (0 - 23) -- like H 187 //----------------------------------------------------------------- 188 String exp = Integer.toString(i); 189 testDateTime("%tk", exp, c); 190 191 //----------------------------------------------------------------- 192 // DateTime.HOUR - 'l' (1 - 12) -- like I 193 //----------------------------------------------------------------- 194 int v = i % 12; 195 v = (v == 0 ? 12 : v); 196 String exp2 = Integer.toString(v); 197 testDateTime("%tl", exp2, c); 198 199 //----------------------------------------------------------------- 200 // DateTime.HOUR_OF_DAY_0 - 'H' (00 - 23) [zero padded] 201 //----------------------------------------------------------------- 202 if (exp.length() < 2) exp = "0" + exp; 203 testDateTime("%tH", exp, c); 204 205 //----------------------------------------------------------------- 206 // DateTime.HOUR_0 - 'I' (01 - 12) 207 //----------------------------------------------------------------- 208 if (exp2.length() < 2) exp2 = "0" + exp2; 209 testDateTime("%tI", exp2, c); 210 211 //----------------------------------------------------------------- 212 // DateTime.AM_PM - (am or pm) 213 //----------------------------------------------------------------- 214 testDateTime("%tp", (i <12 ? "am" : "pm"), c); 215 } 216 } 217 #end[datetime] 218 219 #if[dec] 220 #if[prim] 221 private static $type$ negate($type$ v) { 222 return ($type$) -v; 223 } 224 #end[prim] 225 #end[dec] 226 #if[Byte] 227 private static $type$ negate($type$ v) { 228 return new $type$((byte) -v.byteValue()); 229 } 230 #end[Byte] 231 #if[Short] 232 private static $type$ negate($type$ v) { 233 return new $type$((short) -v.shortValue()); 234 } 235 #end[Short] 236 #if[Integer] 237 private static $type$ negate($type$ v) { 238 return new $type$(-v.intValue()); 239 } 240 #end[Integer] 241 #if[Long] 242 private static $type$ negate($type$ v) { 243 return new $type$(-v.longValue()); 244 } 245 #end[Long] 246 247 #if[BigDecimal] 248 private static $type$ create(double v) { 249 return new $type$(v); 250 } 251 252 private static $type$ negate($type$ v) { 253 return v.negate(); 254 } 255 256 private static $type$ mult($type$ v, double mul) { 257 return v.multiply(new $type$(mul)); 258 } 259 260 private static $type$ recip($type$ v) { 261 return BigDecimal.ONE.divide(v); 262 } 263 #end[BigDecimal] 264 #if[float] 265 private static $type$ create(double v) { 266 return ($type$) v; 267 } 268 269 private static $type$ negate(double v) { 270 return ($type$) -v; 271 } 272 273 private static $type$ mult($type$ v, double mul) { 274 return v * ($type$) mul; 275 } 276 277 private static $type$ recip($type$ v) { 278 return 1.0f / v; 279 } 280 #end[float] 281 #if[Float] 282 private static $type$ create(double v) { 283 return new $type$(v); 284 } 285 286 private static $type$ negate($type$ v) { 287 return new $type$(-v.floatValue()); 288 } 289 290 private static $type$ mult($type$ v, double mul) { 291 return new $type$(v.floatValue() * (float) mul); 292 } 293 294 private static $type$ recip($type$ v) { 295 return new $type$(1.0f / v.floatValue()); 296 } 297 #end[Float] 298 #if[double] 299 private static $type$ create(double v) { 300 return ($type$) v; 301 } 302 303 304 private static $type$ negate(double v) { 305 return -v; 306 } 307 308 private static $type$ mult($type$ v, double mul) { 309 return v * mul; 310 } 311 312 private static $type$ recip($type$ v) { 313 return 1.0 / v; 314 } 315 #end[double] 316 #if[Double] 317 private static $type$ create(double v) { 318 return new $type$(v); 319 } 320 321 private static $type$ negate($type$ v) { 322 return new $type$(-v.doubleValue()); 323 } 324 325 private static $type$ mult($type$ v, double mul) { 326 return new $type$(v.doubleValue() * mul); 327 } 328 329 private static $type$ recip($type$ v) { 330 return new $type$(1.0 / v.doubleValue()); 331 } 332 #end[Double] 333 334 public static void test() { 335 TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800")); 336 337 // Any characters not explicitly defined as conversions, date/time 338 // conversion suffixes, or flags are illegal and are reserved for 339 // future extensions. Use of such a character in a format string will 340 // cause an UnknownFormatConversionException or 341 // UnknownFormatFlagsException to be thrown. 342 tryCatch("%q", UnknownFormatConversionException.class); 343 tryCatch("%t&", UnknownFormatConversionException.class); 344 tryCatch("%&d", UnknownFormatConversionException.class); 345 tryCatch("%^b", UnknownFormatConversionException.class); 346 347 //--------------------------------------------------------------------- 348 // Formatter.java class javadoc examples 349 //--------------------------------------------------------------------- 350 test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E); 351 test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d"); 352 test("Amount gained or lost since last statement: $ %,(.2f", 353 "Amount gained or lost since last statement: $ (6,217.58)", 354 (new BigDecimal("-6217.58"))); 355 Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0); 356 testSysOut("Local time: %tT", "Local time: 16:17:00", c); 357 358 test("Unable to open file '%1$s': %2$s", 359 "Unable to open file 'food': No such file or directory", 360 "food", "No such file or directory"); 361 Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34); 362 duke.set(Calendar.MILLISECOND, 584); 363 test("Duke's Birthday: %1$tB %1$te, %1$tY", 364 "Duke's Birthday: May 23, 1995", 365 duke); 366 test("Duke's Birthday: %1$tB %1$te, %1$tY", 367 "Duke's Birthday: May 23, 1995", 368 duke.getTime()); 369 test("Duke's Birthday: %1$tB %1$te, %1$tY", 370 "Duke's Birthday: May 23, 1995", 371 duke.getTimeInMillis()); 372 373 test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", 374 "d c b a d c b a", "a", "b", "c", "d"); 375 test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d"); 376 test("%s %s %s %s", "a b c d", "a", "b", "c", "d"); 377 test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d"); 378 379 //--------------------------------------------------------------------- 380 // %b 381 // 382 // General conversion applicable to any argument. 383 //--------------------------------------------------------------------- 384 test("%b", "true", true); 385 test("%b", "false", false); 386 test("%B", "TRUE", true); 387 test("%B", "FALSE", false); 388 test("%b", "true", Boolean.TRUE); 389 test("%b", "false", Boolean.FALSE); 390 test("%B", "TRUE", Boolean.TRUE); 391 test("%B", "FALSE", Boolean.FALSE); 392 test("%14b", " true", true); 393 test("%-14b", "true ", true); 394 test("%5.1b", " f", false); 395 test("%-5.1b", "f ", false); 396 397 test("%b", "true", "foo"); 398 test("%b", "false", (Object)null); 399 400 // Boolean.java hardcodes the Strings for "true" and "false", so no 401 // localization is possible. 402 test(Locale.FRANCE, "%b", "true", true); 403 test(Locale.FRANCE, "%b", "false", false); 404 405 // If you pass in a single array to a varargs method, the compiler 406 // uses it as the array of arguments rather than treating it as a 407 // single array-type argument. 408 test("%b", "false", (Object[])new String[2]); 409 test("%b", "true", new String[2], new String[2]); 410 411 int [] ia = { 1, 2, 3 }; 412 test("%b", "true", ia); 413 414 //--------------------------------------------------------------------- 415 // %b - errors 416 //--------------------------------------------------------------------- 417 tryCatch("%#b", FormatFlagsConversionMismatchException.class); 418 tryCatch("%-b", MissingFormatWidthException.class); 419 // correct or side-effect of implementation? 420 tryCatch("%.b", UnknownFormatConversionException.class); 421 tryCatch("%,b", FormatFlagsConversionMismatchException.class); 422 423 //--------------------------------------------------------------------- 424 // %c 425 // 426 // General conversion applicable to any argument. 427 //--------------------------------------------------------------------- 428 test("%c", "i", 'i'); 429 test("%C", "I", 'i'); 430 test("%4c", " i", 'i'); 431 test("%-4c", "i ", 'i'); 432 test("%4C", " I", 'i'); 433 test("%-4C", "I ", 'i'); 434 test("%c", "i", new Character('i')); 435 test("%c", "H", (byte) 72); 436 test("%c", "i", (short) 105); 437 test("%c", "!", (int) 33); 438 test("%c", "\u007F", Byte.MAX_VALUE); 439 test("%c", new String(Character.toChars(Short.MAX_VALUE)), 440 Short.MAX_VALUE); 441 test("%c", "null", (Object) null); 442 443 //--------------------------------------------------------------------- 444 // %c - errors 445 //--------------------------------------------------------------------- 446 tryCatch("%c", IllegalFormatConversionException.class, 447 Boolean.TRUE); 448 tryCatch("%c", IllegalFormatConversionException.class, 449 (float) 0.1); 450 tryCatch("%c", IllegalFormatConversionException.class, 451 new Object()); 452 tryCatch("%c", IllegalFormatCodePointException.class, 453 Byte.MIN_VALUE); 454 tryCatch("%c", IllegalFormatCodePointException.class, 455 Short.MIN_VALUE); 456 tryCatch("%c", IllegalFormatCodePointException.class, 457 Integer.MIN_VALUE); 458 tryCatch("%c", IllegalFormatCodePointException.class, 459 Integer.MAX_VALUE); 460 461 tryCatch("%#c", FormatFlagsConversionMismatchException.class); 462 tryCatch("%,c", FormatFlagsConversionMismatchException.class); 463 tryCatch("%(c", FormatFlagsConversionMismatchException.class); 464 tryCatch("%$c", UnknownFormatConversionException.class); 465 tryCatch("%.2c", IllegalFormatPrecisionException.class); 466 467 //--------------------------------------------------------------------- 468 // %s 469 // 470 // General conversion applicable to any argument. 471 //--------------------------------------------------------------------- 472 test("%s", "Hello, Duke", "Hello, Duke"); 473 test("%S", "HELLO, DUKE", "Hello, Duke"); 474 test("%20S", " HELLO, DUKE", "Hello, Duke"); 475 test("%20s", " Hello, Duke", "Hello, Duke"); 476 test("%-20s", "Hello, Duke ", "Hello, Duke"); 477 test("%-20.5s", "Hello ", "Hello, Duke"); 478 test("%s", "null", (Object)null); 479 480 StringBuffer sb = new StringBuffer("foo bar"); 481 test("%s", sb.toString(), sb); 482 test("%S", sb.toString().toUpperCase(), sb); 483 484 //--------------------------------------------------------------------- 485 // %s - errors 486 //--------------------------------------------------------------------- 487 tryCatch("%-s", MissingFormatWidthException.class); 488 tryCatch("%--s", DuplicateFormatFlagsException.class); 489 tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0); 490 tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f); 491 tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello"); 492 tryCatch("%#s", FormatFlagsConversionMismatchException.class, null); 493 494 //--------------------------------------------------------------------- 495 // %h 496 // 497 // General conversion applicable to any argument. 498 //--------------------------------------------------------------------- 499 test("%h", Integer.toHexString("Hello, Duke".hashCode()), 500 "Hello, Duke"); 501 test("%10h", " ddf63471", "Hello, Duke"); 502 test("%-10h", "ddf63471 ", "Hello, Duke"); 503 test("%-10H", "DDF63471 ", "Hello, Duke"); 504 test("%10h", " 402e0000", 15.0); 505 test("%10H", " 402E0000", 15.0); 506 507 //--------------------------------------------------------------------- 508 // %h - errors 509 //--------------------------------------------------------------------- 510 tryCatch("%#h", FormatFlagsConversionMismatchException.class); 511 512 //--------------------------------------------------------------------- 513 // flag/conversion errors 514 //--------------------------------------------------------------------- 515 tryCatch("%F", UnknownFormatConversionException.class); 516 517 tryCatch("%#g", FormatFlagsConversionMismatchException.class); 518 519 #if[dec] 520 521 #if[prim] 522 $type$ minByte = Byte.MIN_VALUE; // -128 523 #else[prim] 524 $type$ minByte = new $type$(Byte.MIN_VALUE); 525 #end[prim] 526 527 //--------------------------------------------------------------------- 528 // %d 529 // 530 // Numeric conversion applicable to byte, short, int, long, and 531 // BigInteger. 532 //--------------------------------------------------------------------- 533 test("%d", "null", (Object)null); 534 535 #if[byte] 536 #if[prim] 537 //--------------------------------------------------------------------- 538 // %d - byte 539 //--------------------------------------------------------------------- 540 $type$ seventeen = ($type$) 17; 541 test("%d", "17", seventeen); 542 test("%,d", "17", seventeen); 543 test("%,d", "-17", negate(seventeen)); 544 test("%(d", "17", seventeen); 545 test("%(d", "(17)", negate(seventeen)); 546 test("% d", " 17", seventeen); 547 test("% d", "-17", negate(seventeen)); 548 test("%+d", "+17", seventeen); 549 test("%+d", "-17", negate(seventeen)); 550 test("%010d", "0000000017", seventeen); 551 test("%010d", "-000000017", negate(seventeen)); 552 test("%(10d", " (17)", negate(seventeen)); 553 test("%-10d", "17 ", seventeen); 554 test("%-10d", "-17 ", negate(seventeen)); 555 #end[prim] 556 #else[byte] 557 #if[short] 558 //--------------------------------------------------------------------- 559 // %d - short 560 //--------------------------------------------------------------------- 561 $type$ oneToFive = ($type$) 12345; 562 test("%d", "12345", oneToFive); 563 test("%,d", "12,345", oneToFive); 564 test("%,d", "-12,345", negate(oneToFive)); 565 test("%(d", "12345", oneToFive); 566 test("%(d", "(12345)", negate(oneToFive)); 567 test("% d", " 12345", oneToFive); 568 test("% d", "-12345", negate(oneToFive)); 569 test("%+d", "+12345", oneToFive); 570 test("%+d", "-12345", negate(oneToFive)); 571 test("%010d", "0000012345", oneToFive); 572 test("%010d", "-000012345", negate(oneToFive)); 573 test("%(10d", " (12345)", negate(oneToFive)); 574 test("%-10d", "12345 ", oneToFive); 575 test("%-10d", "-12345 ", negate(oneToFive)); 576 577 #else[short] 578 #if[prim] 579 //--------------------------------------------------------------------- 580 // %d - int and long 581 //--------------------------------------------------------------------- 582 $type$ oneToSeven = ($type$) 1234567; 583 test("%d", "1234567", oneToSeven); 584 test("%,d", "1,234,567", oneToSeven); 585 test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", oneToSeven); 586 test("%,d", "-1,234,567", negate(oneToSeven)); 587 test("%(d", "1234567", oneToSeven); 588 test("%(d", "(1234567)", negate(oneToSeven)); 589 test("% d", " 1234567", oneToSeven); 590 test("% d", "-1234567", negate(oneToSeven)); 591 test("%+d", "+1234567", oneToSeven); 592 test("%+d", "-1234567", negate(oneToSeven)); 593 test("%010d", "0001234567", oneToSeven); 594 test("%010d", "-001234567", negate(oneToSeven)); 595 test("%(10d", " (1234567)", negate(oneToSeven)); 596 test("%-10d", "1234567 ", oneToSeven); 597 test("%-10d", "-1234567 ", negate(oneToSeven)); 598 #end[prim] 599 #end[short] 600 #end[byte] 601 //--------------------------------------------------------------------- 602 // %d - errors 603 //--------------------------------------------------------------------- 604 tryCatch("%#d", FormatFlagsConversionMismatchException.class); 605 tryCatch("%D", UnknownFormatConversionException.class); 606 tryCatch("%0d", MissingFormatWidthException.class); 607 tryCatch("%-d", MissingFormatWidthException.class); 608 tryCatch("%7.3d", IllegalFormatPrecisionException.class); 609 610 //--------------------------------------------------------------------- 611 // %o 612 // 613 // Numeric conversion applicable to byte, short, int, long, and 614 // BigInteger. 615 //--------------------------------------------------------------------- 616 test("%o", "null", (Object)null); 617 618 #if[byte] 619 //--------------------------------------------------------------------- 620 // %o - byte 621 //--------------------------------------------------------------------- 622 test("%010o", "0000000200", minByte); 623 test("%-10o", "200 ", minByte); 624 test("%#10o", " 0200", minByte); 625 #end[byte] 626 #if[short] 627 //--------------------------------------------------------------------- 628 // %o - short 629 //--------------------------------------------------------------------- 630 631 test("%010o", "0000177600", minByte); 632 test("%-10o", "177600 ", minByte); 633 test("%#10o", " 0177600", minByte); 634 #end[short] 635 #if[int] 636 //--------------------------------------------------------------------- 637 // %o - int 638 //--------------------------------------------------------------------- 639 test("%014o", "00037777777600", minByte); 640 test("%-14o", "37777777600 ", minByte); 641 test("%#14o", " 037777777600", minByte); 642 643 $type$ oneToSevenOct = ($type$) 1234567; 644 test("%o", "4553207", oneToSevenOct); 645 test("%010o", "0004553207", oneToSevenOct); 646 test("%-10o", "4553207 ", oneToSevenOct); 647 test("%#10o", " 04553207", oneToSevenOct); 648 #end[int] 649 #if[long] 650 //--------------------------------------------------------------------- 651 // %o - long 652 //--------------------------------------------------------------------- 653 test("%024o", "001777777777777777777600", minByte); 654 test("%-24o", "1777777777777777777600 ", minByte); 655 test("%#24o", " 01777777777777777777600", minByte); 656 657 $type$ oneToSevenOct = ($type$) 1234567; 658 test("%o", "4553207", oneToSevenOct); 659 test("%010o", "0004553207", oneToSevenOct); 660 test("%-10o", "4553207 ", oneToSevenOct); 661 test("%#10o", " 04553207", oneToSevenOct); 662 #end[long] 663 664 //--------------------------------------------------------------------- 665 // %o - errors 666 //--------------------------------------------------------------------- 667 tryCatch("%(o", FormatFlagsConversionMismatchException.class, 668 minByte); 669 tryCatch("%+o", FormatFlagsConversionMismatchException.class, 670 minByte); 671 tryCatch("% o", FormatFlagsConversionMismatchException.class, 672 minByte); 673 tryCatch("%0o", MissingFormatWidthException.class); 674 tryCatch("%-o", MissingFormatWidthException.class); 675 tryCatch("%,o", FormatFlagsConversionMismatchException.class); 676 tryCatch("%O", UnknownFormatConversionException.class); 677 678 //--------------------------------------------------------------------- 679 // %x 680 // 681 // Numeric conversion applicable to byte, short, int, long, and 682 // BigInteger. 683 //--------------------------------------------------------------------- 684 test("%x", "null", (Object)null); 685 686 #if[byte] 687 //--------------------------------------------------------------------- 688 // %x - byte 689 //--------------------------------------------------------------------- 690 test("%010x", "0000000080", minByte); 691 test("%-10x", "80 ", minByte); 692 test("%#10x", " 0x80", minByte); 693 test("%0#10x","0x00000080", minByte); 694 test("%#10X", " 0X80", minByte); 695 test("%X", "80", minByte); 696 #end[byte] 697 #if[short] 698 //--------------------------------------------------------------------- 699 // %x - short 700 //--------------------------------------------------------------------- 701 test("%010x", "000000ff80", minByte); 702 test("%-10x", "ff80 ", minByte); 703 test("%#10x", " 0xff80", minByte); 704 test("%0#10x","0x0000ff80", minByte); 705 test("%#10X", " 0XFF80", minByte); 706 test("%X", "FF80", minByte); 707 #end[short] 708 #if[int] 709 //--------------------------------------------------------------------- 710 // %x - int 711 //--------------------------------------------------------------------- 712 $type$ oneToSevenHex = ($type$)1234567; 713 test("%x", "null", (Object)null); 714 test("%x", "12d687", oneToSevenHex); 715 test("%010x", "000012d687", oneToSevenHex); 716 test("%-10x", "12d687 ", oneToSevenHex); 717 test("%#10x", " 0x12d687", oneToSevenHex); 718 test("%#10X", " 0X12D687",oneToSevenHex); 719 test("%X", "12D687", oneToSevenHex); 720 721 test("%010x", "00ffffff80", minByte); 722 test("%-10x", "ffffff80 ", minByte); 723 test("%#10x", "0xffffff80", minByte); 724 test("%0#12x","0x00ffffff80", minByte); 725 test("%#12X", " 0XFFFFFF80", minByte); 726 test("%X", "FFFFFF80", minByte); 727 #end[int] 728 #if[long] 729 //--------------------------------------------------------------------- 730 // %x - long 731 //--------------------------------------------------------------------- 732 $type$ oneToSevenHex = ($type$)1234567; 733 test("%x", "null", (Object)null); 734 test("%x", "12d687", oneToSevenHex); 735 test("%010x", "000012d687", oneToSevenHex); 736 test("%-10x", "12d687 ", oneToSevenHex); 737 test("%#10x", " 0x12d687", oneToSevenHex); 738 test("%#10X", " 0X12D687",oneToSevenHex); 739 test("%X", "12D687", oneToSevenHex); 740 741 test("%018x", "00ffffffffffffff80", minByte); 742 test("%-18x", "ffffffffffffff80 ", minByte); 743 test("%#20x", " 0xffffffffffffff80", minByte); 744 test("%0#20x", "0x00ffffffffffffff80", minByte); 745 test("%#20X", " 0XFFFFFFFFFFFFFF80", minByte); 746 test("%X", "FFFFFFFFFFFFFF80", minByte); 747 #end[long] 748 //--------------------------------------------------------------------- 749 // %x - errors 750 //--------------------------------------------------------------------- 751 tryCatch("%,x", FormatFlagsConversionMismatchException.class); 752 tryCatch("%0x", MissingFormatWidthException.class); 753 tryCatch("%-x", MissingFormatWidthException.class); 754 755 #end[dec] 756 757 #if[BigInteger] 758 //--------------------------------------------------------------------- 759 // BigInteger - errors 760 //--------------------------------------------------------------------- 761 tryCatch("%f", IllegalFormatConversionException.class, 762 new BigInteger("1")); 763 764 //--------------------------------------------------------------------- 765 // %d - BigInteger 766 //--------------------------------------------------------------------- 767 test("%d", "null", (Object)null); 768 test("%d", "1234567", new BigInteger("1234567", 10)); 769 test("%,d", "1,234,567", new BigInteger("1234567", 10)); 770 test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", new BigInteger("1234567", 10)); 771 test("%,d", "-1,234,567", new BigInteger("-1234567", 10)); 772 test("%(d", "1234567", new BigInteger("1234567", 10)); 773 test("%(d", "(1234567)", new BigInteger("-1234567", 10)); 774 test("% d", " 1234567", new BigInteger("1234567", 10)); 775 test("% d", "-1234567", new BigInteger("-1234567", 10)); 776 test("%+d", "+1234567", new BigInteger("1234567", 10)); 777 test("%+d", "-1234567", new BigInteger("-1234567", 10)); 778 test("%010d", "0001234567", new BigInteger("1234567", 10)); 779 test("%010d", "-001234567", new BigInteger("-1234567", 10)); 780 test("%(10d", " (1234567)", new BigInteger("-1234567", 10)); 781 test("%+d", "+1234567", new BigInteger("1234567", 10)); 782 test("%+d", "-1234567", new BigInteger("-1234567", 10)); 783 test("%-10d", "1234567 ", new BigInteger("1234567", 10)); 784 test("%-10d", "-1234567 ", new BigInteger("-1234567", 10)); 785 786 //--------------------------------------------------------------------- 787 // %o - BigInteger 788 //--------------------------------------------------------------------- 789 test("%o", "null", (Object)null); 790 test("%o", "1234567", new BigInteger("1234567", 8)); 791 test("%(o", "1234567", new BigInteger("1234567", 8)); 792 test("%(o", "(1234567)", new BigInteger("-1234567", 8)); 793 test("% o", " 1234567", new BigInteger("1234567", 8)); 794 test("% o", "-1234567", new BigInteger("-1234567", 8)); 795 test("%+o", "+1234567", new BigInteger("1234567", 8)); 796 test("%+o", "-1234567", new BigInteger("-1234567", 8)); 797 test("%010o", "0001234567", new BigInteger("1234567", 8)); 798 test("%010o", "-001234567", new BigInteger("-1234567", 8)); 799 test("%(10o", " (1234567)", new BigInteger("-1234567", 8)); 800 test("%+o", "+1234567", new BigInteger("1234567", 8)); 801 test("%+o", "-1234567", new BigInteger("-1234567", 8)); 802 test("%-10o", "1234567 ", new BigInteger("1234567", 8)); 803 test("%-10o", "-1234567 ", new BigInteger("-1234567", 8)); 804 test("%#10o", " 01234567", new BigInteger("1234567", 8)); 805 test("%#10o", " -01234567", new BigInteger("-1234567", 8)); 806 807 //--------------------------------------------------------------------- 808 // %x - BigInteger 809 //--------------------------------------------------------------------- 810 test("%x", "null", (Object)null); 811 test("%x", "1234567", new BigInteger("1234567", 16)); 812 test("%(x", "1234567", new BigInteger("1234567", 16)); 813 test("%(x", "(1234567)", new BigInteger("-1234567", 16)); 814 test("% x", " 1234567", new BigInteger("1234567", 16)); 815 test("% x", "-1234567", new BigInteger("-1234567", 16)); 816 test("%+x", "+1234567", new BigInteger("1234567", 16)); 817 test("%+x", "-1234567", new BigInteger("-1234567", 16)); 818 test("%010x", "0001234567", new BigInteger("1234567", 16)); 819 test("%010x", "-001234567", new BigInteger("-1234567", 16)); 820 test("%(10x", " (1234567)", new BigInteger("-1234567", 16)); 821 test("%+x", "+1234567", new BigInteger("1234567", 16)); 822 test("%+x", "-1234567", new BigInteger("-1234567", 16)); 823 test("%-10x", "1234567 ", new BigInteger("1234567", 16)); 824 test("%-10x", "-1234567 ", new BigInteger("-1234567", 16)); 825 test("%#10x", " 0x1234567", new BigInteger("1234567", 16)); 826 test("%#10x", "-0x1234567", new BigInteger("-1234567", 16)); 827 test("%#10X", " 0X1234567", new BigInteger("1234567", 16)); 828 test("%#10X", "-0X1234567", new BigInteger("-1234567", 16)); 829 test("%X", "1234567A", new BigInteger("1234567a", 16)); 830 test("%X", "-1234567A", new BigInteger("-1234567a", 16)); 831 #end[BigInteger] 832 833 #if[fp] 834 #if[BigDecimal] 835 //--------------------------------------------------------------------- 836 // %s - BigDecimal 837 //--------------------------------------------------------------------- 838 $type$ one = BigDecimal.ONE; 839 $type$ ten = BigDecimal.TEN; 840 $type$ pi = new $type$(Math.PI); 841 $type$ piToThe300 = pi.pow(300); 842 843 test("%s", "3.141592653589793115997963468544185161590576171875", pi); 844 #end[BigDecimal] 845 #if[float] 846 //--------------------------------------------------------------------- 847 // %s - float 848 //--------------------------------------------------------------------- 849 $type$ one = 1.0f; 850 $type$ ten = 10.0f; 851 $type$ pi = (float) Math.PI; 852 853 test("%s", "3.1415927", pi); 854 #end[float] 855 #if[Float] 856 //--------------------------------------------------------------------- 857 // %s - Float 858 //--------------------------------------------------------------------- 859 $type$ one = new $type$(1.0f); 860 $type$ ten = new $type$(10.0f); 861 $type$ pi = new $type$(Math.PI); 862 863 test("%s", "3.1415927", pi); 864 #end[Float] 865 #if[double] 866 //--------------------------------------------------------------------- 867 // %s - double 868 //--------------------------------------------------------------------- 869 $type$ one = 1.0; 870 $type$ ten = 10.0; 871 $type$ pi = Math.PI; 872 873 test("%s", "3.141592653589793", pi); 874 #end[double] 875 #if[Double] 876 //--------------------------------------------------------------------- 877 // %s - Double 878 //--------------------------------------------------------------------- 879 $type$ one = new $type$(1.0); 880 $type$ ten = new $type$(10.0); 881 $type$ pi = new $type$(Math.PI); 882 883 test("%s", "3.141592653589793", pi); 884 #end[Double] 885 886 //--------------------------------------------------------------------- 887 // flag/conversion errors 888 //--------------------------------------------------------------------- 889 tryCatch("%d", IllegalFormatConversionException.class, one); 890 tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one); 891 892 //--------------------------------------------------------------------- 893 // %e 894 // 895 // Floating-point conversions applicable to float, double, and 896 // BigDecimal. 897 //--------------------------------------------------------------------- 898 test("%e", "null", (Object)null); 899 900 //--------------------------------------------------------------------- 901 // %e - float and double 902 //--------------------------------------------------------------------- 903 // double PI = 3.141 592 653 589 793 238 46; 904 test("%e", "3.141593e+00", pi); 905 test("%.0e", "1e+01", ten); 906 test("%#.0e", "1.e+01", ten); 907 test("%E", "3.141593E+00", pi); 908 test("%10.3e", " 3.142e+00", pi); 909 test("%10.3e", "-3.142e+00", negate(pi)); 910 test("%010.3e", "03.142e+00", pi); 911 test("%010.3e", "-3.142e+00", negate(pi)); 912 test("%-12.3e", "3.142e+00 ", pi); 913 test("%-12.3e", "-3.142e+00 ", negate(pi)); 914 test("%.3e", "3.142e+00", pi); 915 test("%.3e", "-3.142e+00", negate(pi)); 916 test("%.3e", "3.142e+06", mult(pi, 1000000.0)); 917 test("%.3e", "-3.142e+06", mult(pi, -1000000.0)); 918 919 test(Locale.FRANCE, "%e", "3,141593e+00", pi); 920 921 // double PI^300 922 // = 13962455701329742638131355433930076081862072808 ... e+149 923 #if[BigDecimal] 924 //--------------------------------------------------------------------- 925 // %e - BigDecimal 926 //--------------------------------------------------------------------- 927 test("%.3e", "1.396e+149", piToThe300); 928 test("%.3e", "-1.396e+149", piToThe300.negate()); 929 test("%.3e", "1.000e-100", recip(ten.pow(100))); 930 test("%.3e", "-1.000e-100", negate(recip(ten.pow(100)))); 931 932 test("%3.0e", "1e-06", new BigDecimal("0.000001")); 933 test("%3.0e", "1e-05", new BigDecimal("0.00001")); 934 test("%3.0e", "1e-04", new BigDecimal("0.0001")); 935 test("%3.0e", "1e-03", new BigDecimal("0.001")); 936 test("%3.0e", "1e-02", new BigDecimal("0.01")); 937 test("%3.0e", "1e-01", new BigDecimal("0.1")); 938 test("%3.0e", "9e-01", new BigDecimal("0.9")); 939 test("%3.1e", "9.0e-01", new BigDecimal("0.9")); 940 test("%3.0e", "1e+00", new BigDecimal("1.00")); 941 test("%3.0e", "1e+01", new BigDecimal("10.00")); 942 test("%3.0e", "1e+02", new BigDecimal("99.19")); 943 test("%3.1e", "9.9e+01", new BigDecimal("99.19")); 944 test("%3.0e", "1e+02", new BigDecimal("99.99")); 945 test("%3.0e", "1e+02", new BigDecimal("100.00")); 946 test("%#3.0e", "1.e+03", new BigDecimal("1000.00")); 947 test("%3.0e", "1e+04", new BigDecimal("10000.00")); 948 test("%3.0e", "1e+05", new BigDecimal("100000.00")); 949 test("%3.0e", "1e+06", new BigDecimal("1000000.00")); 950 test("%3.0e", "1e+07", new BigDecimal("10000000.00")); 951 test("%3.0e", "1e+08", new BigDecimal("100000000.00")); 952 #end[BigDecimal] 953 954 test("%10.3e", " 1.000e+00", one); 955 test("%+.3e", "+3.142e+00", pi); 956 test("%+.3e", "-3.142e+00", negate(pi)); 957 test("% .3e", " 3.142e+00", pi); 958 test("% .3e", "-3.142e+00", negate(pi)); 959 test("%#.0e", "3.e+00", create(3.0)); 960 test("%#.0e", "-3.e+00", create(-3.0)); 961 test("%.0e", "3e+00", create(3.0)); 962 test("%.0e", "-3e+00", create(-3.0)); 963 964 test("%(.4e", "3.1416e+06", mult(pi, 1000000.0)); 965 test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0)); 966 967 //--------------------------------------------------------------------- 968 // %e - boundary problems 969 //--------------------------------------------------------------------- 970 test("%3.0e", "1e-06", 0.000001); 971 test("%3.0e", "1e-05", 0.00001); 972 test("%3.0e", "1e-04", 0.0001); 973 test("%3.0e", "1e-03", 0.001); 974 test("%3.0e", "1e-02", 0.01); 975 test("%3.0e", "1e-01", 0.1); 976 test("%3.0e", "9e-01", 0.9); 977 test("%3.1e", "9.0e-01", 0.9); 978 test("%3.0e", "1e+00", 1.00); 979 test("%3.0e", "1e+01", 10.00); 980 test("%3.0e", "1e+02", 99.19); 981 test("%3.1e", "9.9e+01", 99.19); 982 test("%3.0e", "1e+02", 99.99); 983 test("%3.0e", "1e+02", 100.00); 984 test("%#3.0e", "1.e+03", 1000.00); 985 test("%3.0e", "1e+04", 10000.00); 986 test("%3.0e", "1e+05", 100000.00); 987 test("%3.0e", "1e+06", 1000000.00); 988 test("%3.0e", "1e+07", 10000000.00); 989 test("%3.0e", "1e+08", 100000000.00); 990 991 //--------------------------------------------------------------------- 992 // %f 993 // 994 // Floating-point conversions applicable to float, double, and 995 // BigDecimal. 996 //--------------------------------------------------------------------- 997 test("%f", "null", (Object)null); 998 test("%f", "3.141593", pi); 999 test(Locale.FRANCE, "%f", "3,141593", pi); 1000 test("%10.3f", " 3.142", pi); 1001 test("%10.3f", " -3.142", negate(pi)); 1002 test("%010.3f", "000003.142", pi); 1003 test("%010.3f", "-00003.142", negate(pi)); 1004 test("%-10.3f", "3.142 ", pi); 1005 test("%-10.3f", "-3.142 ", negate(pi)); 1006 test("%.3f", "3.142", pi); 1007 test("%.3f", "-3.142", negate(pi)); 1008 test("%+.3f", "+3.142", pi); 1009 test("%+.3f", "-3.142", negate(pi)); 1010 test("% .3f", " 3.142", pi); 1011 test("% .3f", "-3.142", negate(pi)); 1012 test("%#.0f", "3.", create(3.0)); 1013 test("%#.0f", "-3.", create(-3.0)); 1014 test("%.0f", "3", create(3.0)); 1015 test("%.0f", "-3", create(-3.0)); 1016 test("%.3f", "10.000", ten); 1017 test("%.3f", "1.000", one); 1018 test("%10.3f", " 1.000", one); 1019 1020 //--------------------------------------------------------------------- 1021 // %f - boundary problems 1022 //--------------------------------------------------------------------- 1023 test("%3.0f", " 0", 0.000001); 1024 test("%3.0f", " 0", 0.00001); 1025 test("%3.0f", " 0", 0.0001); 1026 test("%3.0f", " 0", 0.001); 1027 test("%3.0f", " 0", 0.01); 1028 test("%3.0f", " 0", 0.1); 1029 test("%3.0f", " 1", 0.9); 1030 test("%3.1f", "0.9", 0.9); 1031 test("%3.0f", " 1", 1.00); 1032 test("%3.0f", " 10", 10.00); 1033 test("%3.0f", " 99", 99.19); 1034 test("%3.1f", "99.2", 99.19); 1035 test("%3.0f", "100", 99.99); 1036 test("%3.0f", "100", 100.00); 1037 test("%#3.0f", "1000.", 1000.00); 1038 test("%3.0f", "10000", 10000.00); 1039 test("%3.0f", "100000", 100000.00); 1040 test("%3.0f", "1000000", 1000000.00); 1041 test("%3.0f", "10000000", 10000000.00); 1042 test("%3.0f", "100000000", 100000000.00); 1043 #if[BigDecimal] 1044 //--------------------------------------------------------------------- 1045 // %f - BigDecimal 1046 //--------------------------------------------------------------------- 1047 test("%4.0f", " 99", new BigDecimal("99.19")); 1048 test("%4.1f", "99.2", new BigDecimal("99.19")); 1049 1050 BigDecimal val = new BigDecimal("99.95"); 1051 test("%4.0f", " 100", val); 1052 test("%#4.0f", "100.", val); 1053 test("%4.1f", "100.0", val); 1054 test("%4.2f", "99.95", val); 1055 test("%4.3f", "99.950", val); 1056 1057 val = new BigDecimal(".99"); 1058 test("%4.1f", " 1.0", val); 1059 test("%4.2f", "0.99", val); 1060 test("%4.3f", "0.990", val); 1061 1062 // #6476425 1063 val = new BigDecimal("0.00001"); 1064 test("%.0f", "0", val); 1065 test("%.1f", "0.0", val); 1066 test("%.2f", "0.00", val); 1067 test("%.3f", "0.000", val); 1068 test("%.4f", "0.0000", val); 1069 test("%.5f", "0.00001", val); 1070 1071 val = new BigDecimal("1.00001"); 1072 test("%.0f", "1", val); 1073 test("%.1f", "1.0", val); 1074 test("%.2f", "1.00", val); 1075 test("%.3f", "1.000", val); 1076 test("%.4f", "1.0000", val); 1077 test("%.5f", "1.00001", val); 1078 1079 val = new BigDecimal("1.23456"); 1080 test("%.0f", "1", val); 1081 test("%.1f", "1.2", val); 1082 test("%.2f", "1.23", val); 1083 test("%.3f", "1.235", val); 1084 test("%.4f", "1.2346", val); 1085 test("%.5f", "1.23456", val); 1086 test("%.6f", "1.234560", val); 1087 1088 val = new BigDecimal("9.99999"); 1089 test("%.0f", "10", val); 1090 test("%.1f", "10.0", val); 1091 test("%.2f", "10.00", val); 1092 test("%.3f", "10.000", val); 1093 test("%.4f", "10.0000", val); 1094 test("%.5f", "9.99999", val); 1095 test("%.6f", "9.999990", val); 1096 1097 1098 val = new BigDecimal("1.99999"); 1099 test("%.0f", "2", val); 1100 test("%.1f", "2.0", val); 1101 test("%.2f", "2.00", val); 1102 test("%.3f", "2.000", val); 1103 test("%.4f", "2.0000", val); 1104 test("%.5f", "1.99999", val); 1105 test("%.6f", "1.999990", val); 1106 1107 #end[BigDecimal] 1108 1109 #if[float] 1110 //--------------------------------------------------------------------- 1111 // %f - float 1112 //--------------------------------------------------------------------- 1113 // Float can not accurately store 1e6 * PI. 1114 test("%.3f", "3141.593", mult(pi, 1000.0)); 1115 test("%.3f", "-3141.593", mult(pi, -1000.0)); 1116 1117 test("%,.2f", "3,141.59", mult(pi, 1000.0)); 1118 test(Locale.FRANCE, "%,.2f", "3\u00a0141,59", mult(pi, 1000.0)); 1119 test("%,.2f", "-3,141.59", mult(pi, -1000.0)); 1120 test("%(.2f", "3141.59", mult(pi, 1000.0)); 1121 test("%(.2f", "(3141.59)", mult(pi, -1000.0)); 1122 test("%(,.2f", "3,141.59", mult(pi, 1000.0)); 1123 test("%(,.2f", "(3,141.59)", mult(pi, -1000.0)); 1124 1125 #else[float] 1126 #if[!Float] 1127 //--------------------------------------------------------------------- 1128 // %f - float, double, Double, BigDecimal 1129 //--------------------------------------------------------------------- 1130 test("%.3f", "3141592.654", mult(pi, 1000000.0)); 1131 test("%.3f", "-3141592.654", mult(pi, -1000000.0)); 1132 test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0)); 1133 test(Locale.FRANCE, "%,.4f", "3\u00a0141\u00a0592,6536", mult(pi, 1000000.0)); 1134 test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0)); 1135 test("%(.4f", "3141592.6536", mult(pi, 1000000.0)); 1136 test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0)); 1137 test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0)); 1138 test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0)); 1139 #end[!Float] 1140 #end[float] 1141 1142 1143 //--------------------------------------------------------------------- 1144 // %g 1145 // 1146 // Floating-point conversions applicable to float, double, and 1147 // BigDecimal. 1148 //--------------------------------------------------------------------- 1149 test("%g", "null", (Object)null); 1150 test("%g", "3.14159", pi); 1151 test(Locale.FRANCE, "%g", "3,14159", pi); 1152 test("%.0g", "1e+01", ten); 1153 test("%G", "3.14159", pi); 1154 test("%10.3g", " 3.14", pi); 1155 test("%10.3g", " -3.14", negate(pi)); 1156 test("%010.3g", "0000003.14", pi); 1157 test("%010.3g", "-000003.14", negate(pi)); 1158 test("%-12.3g", "3.14 ", pi); 1159 test("%-12.3g", "-3.14 ", negate(pi)); 1160 test("%.3g", "3.14", pi); 1161 test("%.3g", "-3.14", negate(pi)); 1162 test("%.3g", "3.14e+08", mult(pi, 100000000.0)); 1163 test("%.3g", "-3.14e+08", mult(pi, -100000000.0)); 1164 1165 test("%.3g", "1.00e-05", recip(create(100000.0))); 1166 test("%.3g", "-1.00e-05", recip(create(-100000.0))); 1167 test("%.0g", "-1e-05", recip(create(-100000.0))); 1168 test("%.0g", "1e+05", create(100000.0)); 1169 test("%.3G", "1.00E-05", recip(create(100000.0))); 1170 test("%.3G", "-1.00E-05", recip(create(-100000.0))); 1171 1172 test("%3.0g", "1e-06", 0.000001); 1173 test("%3.0g", "1e-05", 0.00001); 1174 test("%3.0g", "1e-05", 0.0000099); 1175 test("%3.1g", "1e-05", 0.0000099); 1176 test("%3.2g", "9.9e-06", 0.0000099); 1177 test("%3.0g", "0.0001", 0.0001); 1178 test("%3.0g", "9e-05", 0.00009); 1179 test("%3.0g", "0.0001", 0.000099); 1180 test("%3.1g", "0.0001", 0.000099); 1181 test("%3.2g", "9.9e-05", 0.000099); 1182 test("%3.0g", "0.001", 0.001); 1183 test("%3.0g", "0.001", 0.00099); 1184 test("%3.1g", "0.001", 0.00099); 1185 test("%3.2g", "0.00099", 0.00099); 1186 test("%3.3g", "0.00100", 0.001); 1187 test("%3.4g", "0.001000", 0.001); 1188 test("%3.0g", "0.01", 0.01); 1189 test("%3.0g", "0.1", 0.1); 1190 test("%3.0g", "0.9", 0.9); 1191 test("%3.1g", "0.9", 0.9); 1192 test("%3.0g", " 1", 1.00); 1193 test("%3.2g", " 10", 10.00); 1194 test("%3.0g", "1e+01", 10.00); 1195 test("%3.0g", "1e+02", 99.19); 1196 test("%3.1g", "1e+02", 99.19); 1197 test("%3.2g", " 99", 99.19); 1198 test("%3.0g", "1e+02", 99.9); 1199 test("%3.1g", "1e+02", 99.9); 1200 test("%3.2g", "1.0e+02", 99.9); 1201 test("%3.0g", "1e+02", 99.99); 1202 test("%3.0g", "1e+02", 100.00); 1203 test("%3.0g", "1e+03", 999.9); 1204 test("%3.1g", "1e+03", 999.9); 1205 test("%3.2g", "1.0e+03", 999.9); 1206 test("%3.3g", "1.00e+03", 999.9); 1207 test("%3.4g", "999.9", 999.9); 1208 test("%3.4g", "1000", 999.99); 1209 test("%3.0g", "1e+03", 1000.00); 1210 test("%3.0g", "1e+04", 10000.00); 1211 test("%3.0g", "1e+05", 100000.00); 1212 test("%3.0g", "1e+06", 1000000.00); 1213 test("%3.0g", "1e+07", 10000000.00); 1214 test("%3.9g", "100000000", 100000000.00); 1215 test("%3.10g", "100000000.0", 100000000.00); 1216 1217 tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00); 1218 1219 // double PI^300 1220 // = 13962455701329742638131355433930076081862072808 ... e+149 1221 #if[BigDecimal] 1222 //--------------------------------------------------------------------- 1223 // %g - BigDecimal 1224 //--------------------------------------------------------------------- 1225 test("%.3g", "1.40e+149", piToThe300); 1226 test("%.3g", "-1.40e+149", piToThe300.negate()); 1227 test(Locale.FRANCE, "%.3g", "-1,40e+149", piToThe300.negate()); 1228 test("%.3g", "1.00e-100", recip(ten.pow(100))); 1229 test("%.3g", "-1.00e-100", negate(recip(ten.pow(100)))); 1230 1231 test("%3.0g", "1e-06", new BigDecimal("0.000001")); 1232 test("%3.0g", "1e-05", new BigDecimal("0.00001")); 1233 test("%3.0g", "0.0001", new BigDecimal("0.0001")); 1234 test("%3.0g", "0.001", new BigDecimal("0.001")); 1235 test("%3.3g", "0.00100", new BigDecimal("0.001")); 1236 test("%3.4g", "0.001000", new BigDecimal("0.001")); 1237 test("%3.0g", "0.01", new BigDecimal("0.01")); 1238 test("%3.0g", "0.1", new BigDecimal("0.1")); 1239 test("%3.0g", "0.9", new BigDecimal("0.9")); 1240 test("%3.1g", "0.9", new BigDecimal("0.9")); 1241 test("%3.0g", " 1", new BigDecimal("1.00")); 1242 test("%3.2g", " 10", new BigDecimal("10.00")); 1243 test("%3.0g", "1e+01", new BigDecimal("10.00")); 1244 test("%3.0g", "1e+02", new BigDecimal("99.19")); 1245 test("%3.1g", "1e+02", new BigDecimal("99.19")); 1246 test("%3.2g", " 99", new BigDecimal("99.19")); 1247 test("%3.0g", "1e+02", new BigDecimal("99.99")); 1248 test("%3.0g", "1e+02", new BigDecimal("100.00")); 1249 test("%3.0g", "1e+03", new BigDecimal("1000.00")); 1250 test("%3.0g", "1e+04", new BigDecimal("10000.00")); 1251 test("%3.0g", "1e+05", new BigDecimal("100000.00")); 1252 test("%3.0g", "1e+06", new BigDecimal("1000000.00")); 1253 test("%3.0g", "1e+07", new BigDecimal("10000000.00")); 1254 test("%3.9g", "100000000", new BigDecimal("100000000.00")); 1255 test("%3.10g", "100000000.0", new BigDecimal("100000000.00")); 1256 #end[BigDecimal] 1257 1258 test("%.3g", "10.0", ten); 1259 test("%.3g", "1.00", one); 1260 test("%10.3g", " 1.00", one); 1261 test("%+10.3g", " +3.14", pi); 1262 test("%+10.3g", " -3.14", negate(pi)); 1263 test("% .3g", " 3.14", pi); 1264 test("% .3g", "-3.14", negate(pi)); 1265 test("%.0g", "3", create(3.0)); 1266 test("%.0g", "-3", create(-3.0)); 1267 1268 test("%(.4g", "3.142e+08", mult(pi, 100000000.0)); 1269 test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0)); 1270 1271 #if[float] 1272 // Float can not accurately store 1e6 * PI. 1273 test("%,.6g", "3,141.59", mult(pi, 1000.0)); 1274 test("%(,.6g", "(3,141.59)", mult(pi, -1000.0)); 1275 #else[float] 1276 #if[!Float] 1277 test("%,.11g", "3,141,592.6536", mult(pi, 1000000.0)); 1278 test("%(,.11g", "(3,141,592.6536)", mult(pi, -1000000.0)); 1279 #end[!Float] 1280 #end[float] 1281 1282 #if[double] 1283 //--------------------------------------------------------------------- 1284 // %a 1285 // 1286 // Floating-point conversions applicable to float, double, and 1287 // BigDecimal. 1288 //--------------------------------------------------------------------- 1289 test("%a", "null", (Object)null); 1290 test("%.11a", "0x0.00000000000p0", 0.0); 1291 test(Locale.FRANCE, "%.11a", "0x0.00000000000p0", 0.0); // no localization 1292 test("%.1a", "0x0.0p0", 0.0); 1293 test("%.11a", "-0x0.00000000000p0", -0.0); 1294 test("%.1a", "-0x0.0p0", -0.0); 1295 test("%.11a", "0x1.00000000000p0", 1.0); 1296 test("%.1a", "0x1.0p0", 1.0); 1297 test("%.11a", "-0x1.00000000000p0", -1.0); 1298 test("%.1a", "-0x1.0p0", -1.0); 1299 test("%.11a", "0x1.80000000000p1", 3.0); 1300 test("%.1a", "0x1.8p1", 3.0); 1301 test("%.11a", "0x1.00000000000p-1022", DoubleConsts.MIN_NORMAL); 1302 test("%.1a", "0x1.0p-1022", DoubleConsts.MIN_NORMAL); 1303 test("%.11a", "0x1.00000000000p-1022", 1304 FpUtils.nextDown(DoubleConsts.MIN_NORMAL)); 1305 test("%.1a", "0x1.0p-1022", 1306 FpUtils.nextDown(DoubleConsts.MIN_NORMAL)); 1307 test("%.11a", "0x1.ffffffffffep-1023", 1308 Double.parseDouble("0x0.fffffffffffp-1022")); 1309 test("%.1a", "0x1.0p-1022", 1310 Double.parseDouble("0x0.fffffffffffp-1022")); 1311 test("%.30a", "0x0.000000000000100000000000000000p-1022", Double.MIN_VALUE); 1312 test("%.13a", "0x0.0000000000001p-1022", Double.MIN_VALUE); 1313 test("%.11a", "0x1.00000000000p-1074", Double.MIN_VALUE); 1314 test("%.1a", "0x1.0p-1074", Double.MIN_VALUE); 1315 1316 test("%.11a", "0x1.08000000000p-1069", 1317 Double.MIN_VALUE + Double.MIN_VALUE*32); 1318 test("%.1a", "0x1.0p-1069", 1319 Double.MIN_VALUE + Double.MIN_VALUE*32); 1320 test("%.30a", "0x1.fffffffffffff00000000000000000p1023", Double.MAX_VALUE); 1321 test("%.13a", "0x1.fffffffffffffp1023", Double.MAX_VALUE); 1322 test("%.11a", "0x1.00000000000p1024", Double.MAX_VALUE); 1323 test("%.1a", "0x1.0p1024", Double.MAX_VALUE); 1324 test("%.11a", "0x1.18000000000p0", Double.parseDouble("0x1.18p0")); 1325 test("%.1a", "0x1.2p0", Double.parseDouble("0x1.18p0")); 1326 1327 test("%.11a", "0x1.18000000000p0", 1328 Double.parseDouble("0x1.180000000001p0")); 1329 test("%.1a", "0x1.2p0", 1330 Double.parseDouble("0x1.180000000001p0")); 1331 test("%.11a", "0x1.28000000000p0", Double.parseDouble("0x1.28p0")); 1332 test("%.1a", "0x1.2p0", Double.parseDouble("0x1.28p0")); 1333 1334 test("%.11a", "0x1.28000000000p0", 1335 Double.parseDouble("0x1.280000000001p0")); 1336 test("%.1a", "0x1.3p0", Double.parseDouble("0x1.280000000001p0")); 1337 #end[double] 1338 1339 //--------------------------------------------------------------------- 1340 // %f, %e, %g, %a - Boundaries 1341 //--------------------------------------------------------------------- 1342 #if[float] 1343 //--------------------------------------------------------------------- 1344 // %f, %e, %g, %a - NaN 1345 //--------------------------------------------------------------------- 1346 test("%f", "NaN", Float.NaN); 1347 // s 1348 test("%+f", "NaN", Float.NaN); 1349 // test("%F", "NAN", Float.NaN); 1350 test("%e", "NaN", Float.NaN); 1351 test("%+e", "NaN", Float.NaN); 1352 test("%E", "NAN", Float.NaN); 1353 test("%g", "NaN", Float.NaN); 1354 test("%+g", "NaN", Float.NaN); 1355 test("%G", "NAN", Float.NaN); 1356 test("%a", "NaN", Float.NaN); 1357 test("%+a", "NaN", Float.NaN); 1358 test("%A", "NAN", Float.NaN); 1359 1360 //--------------------------------------------------------------------- 1361 // %f, %e, %g, %a - +0.0 1362 //--------------------------------------------------------------------- 1363 test("%f", "0.000000", +0.0); 1364 test("%+f", "+0.000000", +0.0); 1365 test("% f", " 0.000000", +0.0); 1366 // test("%F", "0.000000", +0.0); 1367 test("%e", "0.000000e+00", 0e0); 1368 test("%e", "0.000000e+00", +0.0); 1369 test("%+e", "+0.000000e+00", +0.0); 1370 test("% e", " 0.000000e+00", +0.0); 1371 test("%E", "0.000000E+00", 0e0); 1372 test("%E", "0.000000E+00", +0.0); 1373 test("%+E", "+0.000000E+00", +0.0); 1374 test("% E", " 0.000000E+00", +0.0); 1375 test("%g", "0.00000", +0.0); 1376 test("%+g", "+0.00000", +0.0); 1377 test("% g", " 0.00000", +0.0); 1378 test("%G", "0.00000", +0.0); 1379 test("% G", " 0.00000", +0.0); 1380 test("%a", "0x0.0p0", +0.0); 1381 test("%+a", "+0x0.0p0", +0.0); 1382 test("% a", " 0x0.0p0", +0.0); 1383 test("%A", "0X0.0P0", +0.0); 1384 test("% A", " 0X0.0P0", +0.0); 1385 1386 //--------------------------------------------------------------------- 1387 // %f, %e, %g, %a - -0.0 1388 //--------------------------------------------------------------------- 1389 test("%f", "-0.000000", -0.0); 1390 test("%+f", "-0.000000", -0.0); 1391 // test("%F", "-0.000000", -0.0); 1392 test("%e", "-0.000000e+00", -0.0); 1393 test("%+e", "-0.000000e+00", -0.0); 1394 test("%E", "-0.000000E+00", -0.0); 1395 test("%+E", "-0.000000E+00", -0.0); 1396 test("%g", "-0.00000", -0.0); 1397 test("%+g", "-0.00000", -0.0); 1398 test("%G", "-0.00000", -0.0); 1399 test("%a", "-0x0.0p0", -0.0); 1400 test("%+a", "-0x0.0p0", -0.0); 1401 test("%+A", "-0X0.0P0", -0.0); 1402 1403 //--------------------------------------------------------------------- 1404 // %f, %e, %g, %a - +Infinity 1405 //--------------------------------------------------------------------- 1406 test("%f", "Infinity", Float.POSITIVE_INFINITY); 1407 test("%+f", "+Infinity", Float.POSITIVE_INFINITY); 1408 test("% f", " Infinity", Float.POSITIVE_INFINITY); 1409 // test("%F", "INFINITY", Float.POSITIVE_INFINITY); 1410 test("%e", "Infinity", Float.POSITIVE_INFINITY); 1411 test("%+e", "+Infinity", Float.POSITIVE_INFINITY); 1412 test("% e", " Infinity", Float.POSITIVE_INFINITY); 1413 test("%E", "INFINITY", Float.POSITIVE_INFINITY); 1414 test("%+E", "+INFINITY", Float.POSITIVE_INFINITY); 1415 test("% E", " INFINITY", Float.POSITIVE_INFINITY); 1416 test("%g", "Infinity", Float.POSITIVE_INFINITY); 1417 test("%+g", "+Infinity", Float.POSITIVE_INFINITY); 1418 test("%G", "INFINITY", Float.POSITIVE_INFINITY); 1419 test("% G", " INFINITY", Float.POSITIVE_INFINITY); 1420 test("%+G", "+INFINITY", Float.POSITIVE_INFINITY); 1421 test("%a", "Infinity", Float.POSITIVE_INFINITY); 1422 test("%+a", "+Infinity", Float.POSITIVE_INFINITY); 1423 test("% a", " Infinity", Float.POSITIVE_INFINITY); 1424 test("%A", "INFINITY", Float.POSITIVE_INFINITY); 1425 test("%+A", "+INFINITY", Float.POSITIVE_INFINITY); 1426 test("% A", " INFINITY", Float.POSITIVE_INFINITY); 1427 1428 //--------------------------------------------------------------------- 1429 // %f, %e, %g, %a - -Infinity 1430 //--------------------------------------------------------------------- 1431 test("%f", "-Infinity", Float.NEGATIVE_INFINITY); 1432 test("%+f", "-Infinity", Float.NEGATIVE_INFINITY); 1433 test("%(f", "(Infinity)", Float.NEGATIVE_INFINITY); 1434 // test("%F", "-INFINITY", Float.NEGATIVE_INFINITY); 1435 test("%e", "-Infinity", Float.NEGATIVE_INFINITY); 1436 test("%+e", "-Infinity", Float.NEGATIVE_INFINITY); 1437 test("%E", "-INFINITY", Float.NEGATIVE_INFINITY); 1438 test("%+E", "-INFINITY", Float.NEGATIVE_INFINITY); 1439 test("%g", "-Infinity", Float.NEGATIVE_INFINITY); 1440 test("%+g", "-Infinity", Float.NEGATIVE_INFINITY); 1441 test("%G", "-INFINITY", Float.NEGATIVE_INFINITY); 1442 test("%+G", "-INFINITY", Float.NEGATIVE_INFINITY); 1443 test("%a", "-Infinity", Float.NEGATIVE_INFINITY); 1444 test("%+a", "-Infinity", Float.NEGATIVE_INFINITY); 1445 test("%A", "-INFINITY", Float.NEGATIVE_INFINITY); 1446 test("%+A", "-INFINITY", Float.NEGATIVE_INFINITY); 1447 1448 //--------------------------------------------------------------------- 1449 // %f, %e, %g, %a - Float.MIN_VALUE 1450 //--------------------------------------------------------------------- 1451 test("%f", "0.000000", Float.MIN_VALUE); 1452 test("%,f", "0.000000", Float.MIN_VALUE); 1453 test("%(f", "(0.000000)", -Float.MIN_VALUE); 1454 test("%30.0f", " 0", Float.MIN_VALUE); 1455 test("%30.5f", " 0.00000", Float.MIN_VALUE); 1456 test("%30.13f", " 0.0000000000000", Float.MIN_VALUE); 1457 test("%30.20f", " 0.00000000000000000000", Float.MIN_VALUE); 1458 test("%e", "1.401298e-45", Float.MIN_VALUE); 1459 test("%E", "1.401298E-45", Float.MIN_VALUE); 1460 test("%(.1e", "1.4e-45", Float.MIN_VALUE); 1461 test("%(E", "(1.401298E-45)", -Float.MIN_VALUE); 1462 test("%30.5e", " 1.40130e-45", Float.MIN_VALUE); 1463 test("%30.13e", " 1.4012984643248e-45", Float.MIN_VALUE); 1464 test("%30.20e", " 1.40129846432481700000e-45", Float.MIN_VALUE); 1465 test("%g", "1.40130e-45", Float.MIN_VALUE); 1466 test("%G", "1.40130E-45", Float.MIN_VALUE); 1467 test("%(g", "1.40130e-45", Float.MIN_VALUE); 1468 test("%,g", "1.40130e-45", Float.MIN_VALUE); 1469 test("%(G", "(1.40130E-45)", -Float.MIN_VALUE); 1470 test("%30.5g", " 1.4013e-45", Float.MIN_VALUE); 1471 test("%30.13g", " 1.401298464325e-45", Float.MIN_VALUE); 1472 test("%30.20g", " 1.4012984643248170000e-45", Float.MIN_VALUE); 1473 test("%a", "0x1.0p-149", Float.MIN_VALUE); 1474 test("%A", "0X1.0P-149", Float.MIN_VALUE); 1475 test("%20a", " 0x1.0p-149", Float.MIN_VALUE); 1476 1477 //--------------------------------------------------------------------- 1478 // %f, %e, %g, %a - Float.MAX_VALUE 1479 //--------------------------------------------------------------------- 1480 test("%f", "340282346638528860000000000000000000000.000000", Float.MAX_VALUE); 1481 test("%,f","340,282,346,638,528,860,000,000,000,000,000,000,000.000000", 1482 Float.MAX_VALUE); 1483 test("%(f", "(340282346638528860000000000000000000000.000000)", -Float.MAX_VALUE); 1484 test("%60.5f", " 340282346638528860000000000000000000000.00000", 1485 Float.MAX_VALUE); 1486 test("%60.13f", " 340282346638528860000000000000000000000.0000000000000", 1487 Float.MAX_VALUE); 1488 test("%61.20f", " 340282346638528860000000000000000000000.00000000000000000000", 1489 Float.MAX_VALUE); 1490 test("%e", "3.402823e+38", Float.MAX_VALUE); 1491 test("%E", "3.402823E+38", Float.MAX_VALUE); 1492 test("%(e", "3.402823e+38", Float.MAX_VALUE); 1493 test("%(e", "(3.402823e+38)", -Float.MAX_VALUE); 1494 test("%30.5e", " 3.40282e+38", Float.MAX_VALUE); 1495 test("%30.13e", " 3.4028234663853e+38", Float.MAX_VALUE); 1496 test("%30.20e", " 3.40282346638528860000e+38", Float.MAX_VALUE); 1497 test("%g", "3.40282e+38", Float.MAX_VALUE); 1498 test("%G", "3.40282E+38", Float.MAX_VALUE); 1499 test("%,g", "3.40282e+38", Float.MAX_VALUE); 1500 test("%(g", "(3.40282e+38)", -Float.MAX_VALUE); 1501 test("%30.5g", " 3.4028e+38", Float.MAX_VALUE); 1502 test("%30.13g", " 3.402823466385e+38", Float.MAX_VALUE); 1503 test("%30.20G", " 3.4028234663852886000E+38", Float.MAX_VALUE); 1504 test("%a", "0x1.fffffep127", Float.MAX_VALUE); 1505 test("%A", "0X1.FFFFFEP127", Float.MAX_VALUE); 1506 test("%20a"," 0x1.fffffep127", Float.MAX_VALUE); 1507 1508 #end[float] 1509 1510 #if[double] 1511 //--------------------------------------------------------------------- 1512 // %f, %e, %g, %a - Double.MIN_VALUE 1513 //--------------------------------------------------------------------- 1514 test("%f", "0.000000", Double.MIN_VALUE); 1515 test("%,f", "0.000000", Double.MIN_VALUE); 1516 test("%(f", "(0.000000)", -Double.MIN_VALUE); 1517 test("%30.0f", " 0", Double.MIN_VALUE); 1518 test("%30.5f", " 0.00000", Double.MIN_VALUE); 1519 test("%30.13f", " 0.0000000000000", Double.MIN_VALUE); 1520 test("%30.20f", " 0.00000000000000000000", Double.MIN_VALUE); 1521 test("%30.350f","0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000490000000000000000000000000", 1522 Double.MIN_VALUE); 1523 test("%e", "4.900000e-324", Double.MIN_VALUE); 1524 test("%E", "4.900000E-324", Double.MIN_VALUE); 1525 test("%(.1e", "4.9e-324", Double.MIN_VALUE); 1526 test("%(E", "(4.900000E-324)", -Double.MIN_VALUE); 1527 test("%30.5e", " 4.90000e-324", Double.MIN_VALUE); 1528 test("%30.13e", " 4.9000000000000e-324", Double.MIN_VALUE); 1529 test("%30.20e", " 4.90000000000000000000e-324", Double.MIN_VALUE); 1530 test("%g", "4.90000e-324", Double.MIN_VALUE); 1531 test("%G", "4.90000E-324", Double.MIN_VALUE); 1532 test("%(g", "4.90000e-324", Double.MIN_VALUE); 1533 test("%,g", "4.90000e-324", Double.MIN_VALUE); 1534 test("%30.5g", " 4.9000e-324", Double.MIN_VALUE); 1535 test("%30.13g", " 4.900000000000e-324", Double.MIN_VALUE); 1536 test("%30.20g", " 4.9000000000000000000e-324", Double.MIN_VALUE); 1537 test("%a", "0x0.0000000000001p-1022", Double.MIN_VALUE); 1538 test("%A", "0X0.0000000000001P-1022", Double.MIN_VALUE); 1539 test("%30a", " 0x0.0000000000001p-1022", Double.MIN_VALUE); 1540 1541 //--------------------------------------------------------------------- 1542 // %f, %e, %g, %a - Double.MAX_VALUE 1543 //--------------------------------------------------------------------- 1544 test("%f", "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000", 1545 Double.MAX_VALUE); 1546 test("%,f", "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000000", 1547 Double.MAX_VALUE); 1548 test("%,(f", "(179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000000)", 1549 -Double.MAX_VALUE); 1550 test("%,30.5f", "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.00000", 1551 Double.MAX_VALUE); 1552 test("%30.13f", "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000000000000", 1553 Double.MAX_VALUE); 1554 test("%30.20f", "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000000000000000000", 1555 Double.MAX_VALUE); 1556 test("%e", "1.797693e+308", Double.MAX_VALUE); 1557 test("%E", "1.797693E+308", Double.MAX_VALUE); 1558 test("%(e", "1.797693e+308", Double.MAX_VALUE); 1559 test("%(e", "(1.797693e+308)", -Double.MAX_VALUE); 1560 test("%30.5e", " 1.79769e+308", Double.MAX_VALUE); 1561 test("%30.13e", " 1.7976931348623e+308", Double.MAX_VALUE); 1562 test("%30.20e", " 1.79769313486231570000e+308", Double.MAX_VALUE); 1563 test("%g", "1.79769e+308", Double.MAX_VALUE); 1564 test("%G", "1.79769E+308", Double.MAX_VALUE); 1565 test("%,g", "1.79769e+308", Double.MAX_VALUE); 1566 test("%(g", "(1.79769e+308)", -Double.MAX_VALUE); 1567 test("%30.5g", " 1.7977e+308", Double.MAX_VALUE); 1568 test("%30.13g", " 1.797693134862e+308", Double.MAX_VALUE); 1569 test("%30.20g", " 1.7976931348623157000e+308", Double.MAX_VALUE); 1570 test("%a", "0x1.fffffffffffffp1023", Double.MAX_VALUE); 1571 test("%A", "0X1.FFFFFFFFFFFFFP1023", Double.MAX_VALUE); 1572 test("%30a", " 0x1.fffffffffffffp1023", Double.MAX_VALUE); 1573 #end[double] 1574 1575 #end[fp] 1576 1577 //--------------------------------------------------------------------- 1578 // %t 1579 // 1580 // Date/Time conversions applicable to Calendar, Date, and long. 1581 //--------------------------------------------------------------------- 1582 test("%tA", "null", (Object)null); 1583 test("%TA", "NULL", (Object)null); 1584 1585 //--------------------------------------------------------------------- 1586 // %t - errors 1587 //--------------------------------------------------------------------- 1588 tryCatch("%t", UnknownFormatConversionException.class); 1589 tryCatch("%T", UnknownFormatConversionException.class); 1590 tryCatch("%tP", UnknownFormatConversionException.class); 1591 tryCatch("%TP", UnknownFormatConversionException.class); 1592 tryCatch("%.5tB", IllegalFormatPrecisionException.class); 1593 tryCatch("%#tB", FormatFlagsConversionMismatchException.class); 1594 tryCatch("%-tB", MissingFormatWidthException.class); 1595 1596 #if[datetime] 1597 //--------------------------------------------------------------------- 1598 // %t - create test Calendar 1599 //--------------------------------------------------------------------- 1600 1601 // Get the supported ids for GMT-08:00 (Pacific Standard Time) 1602 String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); 1603 // Create a Pacific Standard Time time zone 1604 SimpleTimeZone tz = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]); 1605 // public GregorianCalendar(TimeZone zone, Locale aLocale); 1606 Calendar c0 = new GregorianCalendar(tz, Locale.US); 1607 // public final void set(int year, int month, int date, 1608 // int hourOfDay, int minute, int second); 1609 c0.set(1995, MAY, 23, 19, 48, 34); 1610 c0.set(Calendar.MILLISECOND, 584); 1611 1612 //--------------------------------------------------------------------- 1613 // %t - Minutes, {nano,milli}*seconds 1614 // 1615 // testDateTime() verifies the expected output for all applicable types 1616 // (Calendar, Date, and long). It also verifies output for "%t" and 1617 // "%T". Thus it is sufficient to invoke that method once per 1618 // conversion/expected output. 1619 //--------------------------------------------------------------------- 1620 testDateTime("%tM", "48", c0); 1621 testDateTime("%tN", "584000000", c0); 1622 testDateTime("%tL", "584", c0); 1623 // testDateTime("%tQ", "801283714584", c0); 1624 1625 testDateTime("%ts", String.valueOf(c0.getTimeInMillis() / 1000), c0); 1626 testDateTime("%tS", "34", c0); 1627 testDateTime("%tT", "19:48:34", c0); 1628 1629 //--------------------------------------------------------------------- 1630 // %t - Hours, morning/afternoon markers 1631 // 1632 // testHours() iterates through all twenty-four hours to verify 1633 // numeric return value and morning/afternoon markers. 1634 //--------------------------------------------------------------------- 1635 testHours(); 1636 1637 //--------------------------------------------------------------------- 1638 // %t - Portions of date [ day, month, dates, weeks ] 1639 //--------------------------------------------------------------------- 1640 testDateTime("%ta", "Tue", c0); 1641 testDateTime("%tA", "Tuesday", c0); 1642 testDateTime("%tb", "May", c0); 1643 testDateTime("%tB", "May", c0); 1644 testDateTime("%tC", "19", c0); 1645 testDateTime("%td", "23", c0); 1646 testDateTime("%te", "23", c0); 1647 testDateTime("%th", "May", c0); 1648 testDateTime("%tj", "143", c0); 1649 testDateTime("%tm", "05", c0); 1650 testDateTime("%ty", "95", c0); 1651 testDateTime("%tY", "1995", c0); 1652 1653 //--------------------------------------------------------------------- 1654 // %t - TimeZone 1655 //--------------------------------------------------------------------- 1656 testDateTime("%tz", "-0800", c0); 1657 testDateTime("%tZ", "PST", c0); 1658 1659 //--------------------------------------------------------------------- 1660 // %tz should always adjust for DST 1661 //--------------------------------------------------------------------- 1662 TimeZone dtz = TimeZone.getDefault(); 1663 1664 // Artificial TimeZone based on PST with 3:15 DST always in effect 1665 TimeZone atz = new SimpleTimeZone(-8 * 60 * 60 * 1000, "AlwaysDST", 1666 JANUARY, 1, 0, 0, STANDARD_TIME, 1667 // 24hrs - 1m = 60 * 60 * 1000 * 24 - 1 1668 DECEMBER, 31, 0, 60 * 60 * 1000 * 24 - 1, STANDARD_TIME, 1669 (int)(60 * 60 * 1000 * 3.25)); 1670 TimeZone.setDefault(atz); 1671 testDateTime("%tz", "-0445", Calendar.getInstance(atz)); 1672 1673 // Restore the TimeZone and verify 1674 TimeZone.setDefault(dtz); 1675 if (atz.hasSameRules(TimeZone.getDefault())) 1676 throw new RuntimeException("Default TimeZone not restored"); 1677 1678 //--------------------------------------------------------------------- 1679 // %t - Composites 1680 //--------------------------------------------------------------------- 1681 testDateTime("%tr", "07:48:34 PM", c0); 1682 testDateTime("%tR", "19:48", c0); 1683 testDateTime("%tc", "Tue May 23 19:48:34 PST 1995", c0); 1684 testDateTime("%tD", "05/23/95", c0); 1685 testDateTime("%tF", "1995-05-23", c0); 1686 testDateTime("%-12tF", "1995-05-23 ", c0); 1687 testDateTime("%12tF", " 1995-05-23", c0); 1688 #end[datetime] 1689 1690 //--------------------------------------------------------------------- 1691 // %n 1692 //--------------------------------------------------------------------- 1693 test("%n", System.getProperty("line.separator"), (Object)null); 1694 test("%n", System.getProperty("line.separator"), ""); 1695 1696 tryCatch("%,n", IllegalFormatFlagsException.class); 1697 tryCatch("%.n", UnknownFormatConversionException.class); 1698 tryCatch("%5.n", UnknownFormatConversionException.class); 1699 tryCatch("%5n", IllegalFormatWidthException.class); 1700 tryCatch("%.7n", IllegalFormatPrecisionException.class); 1701 tryCatch("%<n", IllegalFormatFlagsException.class); 1702 1703 //--------------------------------------------------------------------- 1704 // %% 1705 //--------------------------------------------------------------------- 1706 test("%%", "%", (Object)null); 1707 test("%%", "%", ""); 1708 tryCatch("%%%", UnknownFormatConversionException.class); 1709 // perhaps an IllegalFormatArgumentIndexException should be defined? 1710 tryCatch("%<%", IllegalFormatFlagsException.class); 1711 } 1712 }