1 /*
   2  * Copyright (c) 2003, 2014, 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 // -- This file was mechanically generated: Do not edit! -- //
  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 
  41 public class BasicLong extends Basic {
  42 
  43     private static void test(String fs, String exp, Object ... args) {
  44         Formatter f = new Formatter(new StringBuilder(), Locale.US);
  45         f.format(fs, args);
  46         ck(fs, exp, f.toString());
  47 
  48         f = new Formatter(new StringBuilder(), Locale.US);
  49         f.format("foo " + fs + " bar", args);
  50         ck(fs, "foo " + exp + " bar", f.toString());
  51     }
  52 
  53     private static void test(Locale l, String fs, String exp, Object ... args)
  54     {
  55         Formatter f = new Formatter(new StringBuilder(), l);
  56         f.format(fs, args);
  57         ck(fs, exp, f.toString());
  58 
  59         f = new Formatter(new StringBuilder(), l);
  60         f.format("foo " + fs + " bar", args);
  61         ck(fs, "foo " + exp + " bar", 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     private static long negate(long v) {
 140         return (long) -v;
 141     }
 142 
 143     public static void test() {
 144         TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
 145 
 146         // Any characters not explicitly defined as conversions, date/time
 147         // conversion suffixes, or flags are illegal and are reserved for
 148         // future extensions.  Use of such a character in a format string will
 149         // cause an UnknownFormatConversionException or
 150         // UnknownFormatFlagsException to be thrown.
 151         tryCatch("%q", UnknownFormatConversionException.class);
 152         tryCatch("%t&", UnknownFormatConversionException.class);
 153         tryCatch("%&d", UnknownFormatConversionException.class);
 154         tryCatch("%^b", UnknownFormatConversionException.class);
 155 
 156         //---------------------------------------------------------------------
 157         // Formatter.java class javadoc examples
 158         //---------------------------------------------------------------------
 159         test(Locale.FRANCE, "e = %+10.4f", "e =    +2,7183", Math.E);
 160         test("%4$2s %3$2s %2$2s %1$2s", " d  c  b  a", "a", "b", "c", "d");
 161         test("Amount gained or lost since last statement: $ %,(.2f",
 162              "Amount gained or lost since last statement: $ (6,217.58)",
 163              (new BigDecimal("-6217.58")));
 164         Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
 165         testSysOut("Local time: %tT", "Local time: 16:17:00", c);
 166 
 167         test("Unable to open file '%1$s': %2$s",
 168              "Unable to open file 'food': No such file or directory",
 169              "food", "No such file or directory");
 170         Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
 171         duke.set(Calendar.MILLISECOND, 584);
 172         test("Duke's Birthday: %1$tB %1$te, %1$tY",
 173              "Duke's Birthday: May 23, 1995",
 174              duke);
 175         test("Duke's Birthday: %1$tB %1$te, %1$tY",
 176              "Duke's Birthday: May 23, 1995",
 177              duke.getTime());
 178         test("Duke's Birthday: %1$tB %1$te, %1$tY",
 179              "Duke's Birthday: May 23, 1995",
 180              duke.getTimeInMillis());
 181 
 182         test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
 183              "d c b a d c b a", "a", "b", "c", "d");
 184         test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
 185         test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
 186         test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
 187 
 188         //---------------------------------------------------------------------
 189         // %b
 190         //
 191         // General conversion applicable to any argument.
 192         //---------------------------------------------------------------------
 193         test("%b", "true", true);
 194         test("%b", "false", false);
 195         test("%B", "TRUE", true);
 196         test("%B", "FALSE", false);
 197         test("%b", "true", Boolean.TRUE);
 198         test("%b", "false", Boolean.FALSE);
 199         test("%B", "TRUE", Boolean.TRUE);
 200         test("%B", "FALSE", Boolean.FALSE);
 201         test("%14b", "          true", true);
 202         test("%-14b", "true          ", true);
 203         test("%5.1b", "    f", false);
 204         test("%-5.1b", "f    ", false);
 205 
 206         test("%b", "true", "foo");
 207         test("%b", "false", (Object)null);
 208 
 209         // Boolean.java hardcodes the Strings for "true" and "false", so no
 210         // localization is possible.
 211         test(Locale.FRANCE, "%b", "true", true);
 212         test(Locale.FRANCE, "%b", "false", false);
 213 
 214         // If you pass in a single array to a varargs method, the compiler
 215         // uses it as the array of arguments rather than treating it as a
 216         // single array-type argument.
 217         test("%b", "false", (Object[])new String[2]);
 218         test("%b", "true", new String[2], new String[2]);
 219 
 220         int [] ia = { 1, 2, 3 };
 221         test("%b", "true", ia);
 222 
 223         //---------------------------------------------------------------------
 224         // %b - errors
 225         //---------------------------------------------------------------------
 226         tryCatch("%#b", FormatFlagsConversionMismatchException.class);
 227         tryCatch("%-b", MissingFormatWidthException.class);
 228         // correct or side-effect of implementation?
 229         tryCatch("%.b", UnknownFormatConversionException.class);
 230         tryCatch("%,b", FormatFlagsConversionMismatchException.class);
 231 
 232         //---------------------------------------------------------------------
 233         // %c
 234         //
 235         // General conversion applicable to any argument.
 236         //---------------------------------------------------------------------
 237         test("%c", "i", 'i');
 238         test("%C", "I", 'i');
 239         test("%4c",  "   i", 'i');
 240         test("%-4c", "i   ", 'i');
 241         test("%4C",  "   I", 'i');
 242         test("%-4C", "I   ", 'i');
 243         test("%c", "i", new Character('i'));
 244         test("%c", "H", (byte) 72);
 245         test("%c", "i", (short) 105);
 246         test("%c", "!", (int) 33);
 247         test("%c", "\u007F", Byte.MAX_VALUE);
 248         test("%c", new String(Character.toChars(Short.MAX_VALUE)),
 249              Short.MAX_VALUE);
 250         test("%c", "null", (Object) null);
 251 
 252         //---------------------------------------------------------------------
 253         // %c - errors
 254         //---------------------------------------------------------------------
 255         tryCatch("%c", IllegalFormatConversionException.class,
 256                  Boolean.TRUE);
 257         tryCatch("%c", IllegalFormatConversionException.class,
 258                  (float) 0.1);
 259         tryCatch("%c", IllegalFormatConversionException.class,
 260                  new Object());
 261         tryCatch("%c", IllegalFormatCodePointException.class,
 262                  Byte.MIN_VALUE);
 263         tryCatch("%c", IllegalFormatCodePointException.class,
 264                  Short.MIN_VALUE);
 265         tryCatch("%c", IllegalFormatCodePointException.class,
 266                  Integer.MIN_VALUE);
 267         tryCatch("%c", IllegalFormatCodePointException.class,
 268                  Integer.MAX_VALUE);
 269 
 270         tryCatch("%#c", FormatFlagsConversionMismatchException.class);
 271         tryCatch("%,c", FormatFlagsConversionMismatchException.class);
 272         tryCatch("%(c", FormatFlagsConversionMismatchException.class);
 273         tryCatch("%$c", UnknownFormatConversionException.class);
 274         tryCatch("%.2c", IllegalFormatPrecisionException.class);
 275 
 276         //---------------------------------------------------------------------
 277         // %s
 278         //
 279         // General conversion applicable to any argument.
 280         //---------------------------------------------------------------------
 281         test("%s", "Hello, Duke", "Hello, Duke");
 282         test("%S", "HELLO, DUKE", "Hello, Duke");
 283         test("%20S", "         HELLO, DUKE", "Hello, Duke");
 284         test("%20s", "         Hello, Duke", "Hello, Duke");
 285         test("%-20s", "Hello, Duke         ", "Hello, Duke");
 286         test("%-20.5s", "Hello               ", "Hello, Duke");
 287         test("%s", "null", (Object)null);
 288 
 289         StringBuffer sb = new StringBuffer("foo bar");
 290         test("%s", sb.toString(), sb);
 291         test("%S", sb.toString().toUpperCase(), sb);
 292 
 293         //---------------------------------------------------------------------
 294         // %s - errors
 295         //---------------------------------------------------------------------
 296         tryCatch("%-s", MissingFormatWidthException.class);
 297         tryCatch("%--s", DuplicateFormatFlagsException.class);
 298         tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
 299         tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
 300         tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
 301         tryCatch("%#s", FormatFlagsConversionMismatchException.class, null);
 302 
 303         //---------------------------------------------------------------------
 304         // %h
 305         //
 306         // General conversion applicable to any argument.
 307         //---------------------------------------------------------------------
 308         test("%h", Integer.toHexString("Hello, Duke".hashCode()),
 309              "Hello, Duke");
 310         test("%10h", "  ddf63471", "Hello, Duke");
 311         test("%-10h", "ddf63471  ", "Hello, Duke");
 312         test("%-10H", "DDF63471  ", "Hello, Duke");
 313         test("%10h", "  402e0000", 15.0);
 314         test("%10H", "  402E0000", 15.0);
 315 
 316         //---------------------------------------------------------------------
 317         // %h - errors
 318         //---------------------------------------------------------------------
 319         tryCatch("%#h", FormatFlagsConversionMismatchException.class);
 320 
 321         //---------------------------------------------------------------------
 322         // flag/conversion errors
 323         //---------------------------------------------------------------------
 324         tryCatch("%F", UnknownFormatConversionException.class);
 325 
 326         tryCatch("%#g", FormatFlagsConversionMismatchException.class);
 327 
 328         long minByte = Byte.MIN_VALUE;   // -128
 329 
 330         //---------------------------------------------------------------------
 331         // %d
 332         //
 333         // Numeric conversion applicable to byte, short, int, long, and
 334         // BigInteger.
 335         //---------------------------------------------------------------------
 336         test("%d", "null", (Object)null);
 337 
 338         //---------------------------------------------------------------------
 339         // %d - int and long
 340         //---------------------------------------------------------------------
 341         long oneToSeven = (long) 1234567;
 342         test("%d", "1234567", oneToSeven);
 343         test("%,d", "1,234,567", oneToSeven);
 344         test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", oneToSeven);
 345         test("%,d", "-1,234,567", negate(oneToSeven));
 346         test("%(d", "1234567", oneToSeven);
 347         test("%(d", "(1234567)", negate(oneToSeven));
 348         test("% d", " 1234567", oneToSeven);
 349         test("% d", "-1234567", negate(oneToSeven));
 350         test("%+d", "+1234567", oneToSeven);
 351         test("%+d", "-1234567", negate(oneToSeven));
 352         test("%010d", "0001234567", oneToSeven);
 353         test("%010d", "-001234567", negate(oneToSeven));
 354         test("%(10d", " (1234567)", negate(oneToSeven));
 355         test("%-10d", "1234567   ", oneToSeven);
 356         test("%-10d", "-1234567  ", negate(oneToSeven));
 357         // , variations:
 358         test("% ,d", " 1,234,567", oneToSeven);
 359         test("% ,d", "-1,234,567", negate(oneToSeven));
 360         test("%+,10d", "+1,234,567", oneToSeven);
 361         test("%0,10d", "01,234,567", oneToSeven);
 362         test("%0,10d", "-1,234,567", negate(oneToSeven));
 363         test("%(,10d", "(1,234,567)", negate(oneToSeven));
 364         test("%-,10d", "1,234,567 ", oneToSeven);
 365         test("%-,10d", "-1,234,567", negate(oneToSeven));
 366 
 367         //---------------------------------------------------------------------
 368         // %d - errors
 369         //---------------------------------------------------------------------
 370         tryCatch("%#d", FormatFlagsConversionMismatchException.class);
 371         tryCatch("%D", UnknownFormatConversionException.class);
 372         tryCatch("%0d", MissingFormatWidthException.class);
 373         tryCatch("%-d", MissingFormatWidthException.class);
 374         tryCatch("%7.3d", IllegalFormatPrecisionException.class);
 375 
 376         //---------------------------------------------------------------------
 377         // %o
 378         //
 379         // Numeric conversion applicable to byte, short, int, long, and
 380         // BigInteger.
 381         //---------------------------------------------------------------------
 382         test("%o", "null", (Object)null);
 383 
 384         //---------------------------------------------------------------------
 385         // %o - long
 386         //---------------------------------------------------------------------
 387         test("%024o", "001777777777777777777600", minByte);
 388         test("%-24o", "1777777777777777777600  ", minByte);
 389         test("%#24o", " 01777777777777777777600", minByte);
 390 
 391         long oneToSevenOct = (long) 1234567;
 392         test("%o", "4553207", oneToSevenOct);
 393         test("%010o", "0004553207", oneToSevenOct);
 394         test("%-10o", "4553207   ", oneToSevenOct);
 395         test("%#10o", "  04553207", oneToSevenOct);
 396 
 397         //---------------------------------------------------------------------
 398         // %o - errors
 399         //---------------------------------------------------------------------
 400         tryCatch("%(o", FormatFlagsConversionMismatchException.class,
 401                  minByte);
 402         tryCatch("%+o", FormatFlagsConversionMismatchException.class,
 403                  minByte);
 404         tryCatch("% o", FormatFlagsConversionMismatchException.class,
 405                  minByte);
 406         tryCatch("%0o", MissingFormatWidthException.class);
 407         tryCatch("%-o", MissingFormatWidthException.class);
 408         tryCatch("%,o", FormatFlagsConversionMismatchException.class);
 409         tryCatch("%O", UnknownFormatConversionException.class);
 410 
 411         //---------------------------------------------------------------------
 412         // %x
 413         //
 414         // Numeric conversion applicable to byte, short, int, long, and
 415         // BigInteger.
 416         //---------------------------------------------------------------------
 417         test("%x", "null", (Object)null);
 418 
 419         //---------------------------------------------------------------------
 420         // %x - long
 421         //---------------------------------------------------------------------
 422         long oneToSevenHex = (long)1234567;
 423         test("%x", "null", (Object)null);
 424         test("%x", "12d687", oneToSevenHex);
 425         test("%010x", "000012d687", oneToSevenHex);
 426         test("%-10x", "12d687    ", oneToSevenHex);
 427         test("%#10x", "  0x12d687", oneToSevenHex);
 428         test("%#10X", "  0X12D687",oneToSevenHex);
 429         test("%X", "12D687", oneToSevenHex);
 430 
 431         test("%018x",    "00ffffffffffffff80", minByte);
 432         test("%-18x",      "ffffffffffffff80  ", minByte);
 433         test("%#20x",  "  0xffffffffffffff80", minByte);
 434         test("%0#20x", "0x00ffffffffffffff80", minByte);
 435         test("%#20X", "  0XFFFFFFFFFFFFFF80", minByte);
 436         test("%X",        "FFFFFFFFFFFFFF80", minByte);
 437 
 438         //---------------------------------------------------------------------
 439         // %x - errors
 440         //---------------------------------------------------------------------
 441         tryCatch("%,x", FormatFlagsConversionMismatchException.class);
 442         tryCatch("%0x", MissingFormatWidthException.class);
 443         tryCatch("%-x", MissingFormatWidthException.class);
 444 
 445         //---------------------------------------------------------------------
 446         // %t
 447         //
 448         // Date/Time conversions applicable to Calendar, Date, and long.
 449         //---------------------------------------------------------------------
 450         test("%tA", "null", (Object)null);
 451         test("%TA", "NULL", (Object)null);
 452 
 453         //---------------------------------------------------------------------
 454         // %t - errors
 455         //---------------------------------------------------------------------
 456         tryCatch("%t", UnknownFormatConversionException.class);
 457         tryCatch("%T", UnknownFormatConversionException.class);
 458         tryCatch("%tP", UnknownFormatConversionException.class);
 459         tryCatch("%TP", UnknownFormatConversionException.class);
 460         tryCatch("%.5tB", IllegalFormatPrecisionException.class);
 461         tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
 462         tryCatch("%-tB", MissingFormatWidthException.class);
 463 
 464         //---------------------------------------------------------------------
 465         // %n
 466         //---------------------------------------------------------------------
 467         test("%n", System.getProperty("line.separator"), (Object)null);
 468         test("%n", System.getProperty("line.separator"), "");
 469 
 470         tryCatch("%,n", IllegalFormatFlagsException.class);
 471         tryCatch("%.n", UnknownFormatConversionException.class);
 472         tryCatch("%5.n", UnknownFormatConversionException.class);
 473         tryCatch("%5n", IllegalFormatWidthException.class);
 474         tryCatch("%.7n", IllegalFormatPrecisionException.class);
 475         tryCatch("%<n", IllegalFormatFlagsException.class);
 476 
 477         //---------------------------------------------------------------------
 478         // %%
 479         //---------------------------------------------------------------------
 480         test("%%", "%", (Object)null);
 481         test("%%", "%", "");
 482         tryCatch("%%%", UnknownFormatConversionException.class);
 483         // perhaps an IllegalFormatArgumentIndexException should be defined?
 484         tryCatch("%<%", IllegalFormatFlagsException.class);
 485     }
 486 }