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