Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Formatter/BasicLong.java
38812 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* Type-specific source code for unit test24*25* Regenerate the BasicX classes via genBasic.sh whenever this file changes.26* We check in the generated source files so that the test tree can be used27* independently of the rest of the source tree.28*/2930// -- This file was mechanically generated: Do not edit! -- //3132import java.io.*;33import java.math.BigDecimal;34import java.math.BigInteger;35import java.text.DateFormatSymbols;36import java.util.*;3738394041import static java.util.Calendar.*;424344454647public class BasicLong extends Basic {4849private static void test(String fs, String exp, Object ... args) {50Formatter f = new Formatter(new StringBuilder(), Locale.US);51f.format(fs, args);52ck(fs, exp, f.toString());53}5455private static void test(Locale l, String fs, String exp, Object ... args)56{57Formatter f = new Formatter(new StringBuilder(), l);58f.format(fs, args);59ck(fs, exp, f.toString());60}6162private static void test(String fs, Object ... args) {63Formatter f = new Formatter(new StringBuilder(), Locale.US);64f.format(fs, args);65ck(fs, "fail", f.toString());66}6768private static void test(String fs) {69Formatter f = new Formatter(new StringBuilder(), Locale.US);70f.format(fs, "fail");71ck(fs, "fail", f.toString());72}7374private static void testSysOut(String fs, String exp, Object ... args) {75FileOutputStream fos = null;76FileInputStream fis = null;77try {78PrintStream saveOut = System.out;79fos = new FileOutputStream("testSysOut");80System.setOut(new PrintStream(fos));81System.out.format(Locale.US, fs, args);82fos.close();8384fis = new FileInputStream("testSysOut");85byte [] ba = new byte[exp.length()];86int len = fis.read(ba);87String got = new String(ba);88if (len != ba.length)89fail(fs, exp, got);90ck(fs, exp, got);9192System.setOut(saveOut);93} catch (FileNotFoundException ex) {94fail(fs, ex.getClass());95} catch (IOException ex) {96fail(fs, ex.getClass());97} finally {98try {99if (fos != null)100fos.close();101if (fis != null)102fis.close();103} catch (IOException ex) {104fail(fs, ex.getClass());105}106}107}108109private static void tryCatch(String fs, Class<?> ex) {110boolean caught = false;111try {112test(fs);113} catch (Throwable x) {114if (ex.isAssignableFrom(x.getClass()))115caught = true;116}117if (!caught)118fail(fs, ex);119else120pass();121}122123private static void tryCatch(String fs, Class<?> ex, Object ... args) {124boolean caught = false;125try {126test(fs, args);127} catch (Throwable x) {128if (ex.isAssignableFrom(x.getClass()))129caught = true;130}131if (!caught)132fail(fs, ex);133else134pass();135}136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219private static long negate(long v) {220return (long) -v;221}222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332public static void test() {333TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));334335// Any characters not explicitly defined as conversions, date/time336// conversion suffixes, or flags are illegal and are reserved for337// future extensions. Use of such a character in a format string will338// cause an UnknownFormatConversionException or339// UnknownFormatFlagsException to be thrown.340tryCatch("%q", UnknownFormatConversionException.class);341tryCatch("%t&", UnknownFormatConversionException.class);342tryCatch("%&d", UnknownFormatConversionException.class);343tryCatch("%^b", UnknownFormatConversionException.class);344345//---------------------------------------------------------------------346// Formatter.java class javadoc examples347//---------------------------------------------------------------------348test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);349test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");350test("Amount gained or lost since last statement: $ %,(.2f",351"Amount gained or lost since last statement: $ (6,217.58)",352(new BigDecimal("-6217.58")));353Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);354testSysOut("Local time: %tT", "Local time: 16:17:00", c);355356test("Unable to open file '%1$s': %2$s",357"Unable to open file 'food': No such file or directory",358"food", "No such file or directory");359Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);360duke.set(Calendar.MILLISECOND, 584);361test("Duke's Birthday: %1$tB %1$te, %1$tY",362"Duke's Birthday: May 23, 1995",363duke);364test("Duke's Birthday: %1$tB %1$te, %1$tY",365"Duke's Birthday: May 23, 1995",366duke.getTime());367test("Duke's Birthday: %1$tB %1$te, %1$tY",368"Duke's Birthday: May 23, 1995",369duke.getTimeInMillis());370371test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",372"d c b a d c b a", "a", "b", "c", "d");373test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");374test("%s %s %s %s", "a b c d", "a", "b", "c", "d");375test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");376377//---------------------------------------------------------------------378// %b379//380// General conversion applicable to any argument.381//---------------------------------------------------------------------382test("%b", "true", true);383test("%b", "false", false);384test("%B", "TRUE", true);385test("%B", "FALSE", false);386test("%b", "true", Boolean.TRUE);387test("%b", "false", Boolean.FALSE);388test("%B", "TRUE", Boolean.TRUE);389test("%B", "FALSE", Boolean.FALSE);390test("%14b", " true", true);391test("%-14b", "true ", true);392test("%5.1b", " f", false);393test("%-5.1b", "f ", false);394395test("%b", "true", "foo");396test("%b", "false", (Object)null);397398// Boolean.java hardcodes the Strings for "true" and "false", so no399// localization is possible.400test(Locale.FRANCE, "%b", "true", true);401test(Locale.FRANCE, "%b", "false", false);402403// If you pass in a single array to a varargs method, the compiler404// uses it as the array of arguments rather than treating it as a405// single array-type argument.406test("%b", "false", (Object[])new String[2]);407test("%b", "true", new String[2], new String[2]);408409int [] ia = { 1, 2, 3 };410test("%b", "true", ia);411412//---------------------------------------------------------------------413// %b - errors414//---------------------------------------------------------------------415tryCatch("%#b", FormatFlagsConversionMismatchException.class);416tryCatch("%-b", MissingFormatWidthException.class);417// correct or side-effect of implementation?418tryCatch("%.b", UnknownFormatConversionException.class);419tryCatch("%,b", FormatFlagsConversionMismatchException.class);420421//---------------------------------------------------------------------422// %c423//424// General conversion applicable to any argument.425//---------------------------------------------------------------------426test("%c", "i", 'i');427test("%C", "I", 'i');428test("%4c", " i", 'i');429test("%-4c", "i ", 'i');430test("%4C", " I", 'i');431test("%-4C", "I ", 'i');432test("%c", "i", new Character('i'));433test("%c", "H", (byte) 72);434test("%c", "i", (short) 105);435test("%c", "!", (int) 33);436test("%c", "\u007F", Byte.MAX_VALUE);437test("%c", new String(Character.toChars(Short.MAX_VALUE)),438Short.MAX_VALUE);439test("%c", "null", (Object) null);440441//---------------------------------------------------------------------442// %c - errors443//---------------------------------------------------------------------444tryCatch("%c", IllegalFormatConversionException.class,445Boolean.TRUE);446tryCatch("%c", IllegalFormatConversionException.class,447(float) 0.1);448tryCatch("%c", IllegalFormatConversionException.class,449new Object());450tryCatch("%c", IllegalFormatCodePointException.class,451Byte.MIN_VALUE);452tryCatch("%c", IllegalFormatCodePointException.class,453Short.MIN_VALUE);454tryCatch("%c", IllegalFormatCodePointException.class,455Integer.MIN_VALUE);456tryCatch("%c", IllegalFormatCodePointException.class,457Integer.MAX_VALUE);458459tryCatch("%#c", FormatFlagsConversionMismatchException.class);460tryCatch("%,c", FormatFlagsConversionMismatchException.class);461tryCatch("%(c", FormatFlagsConversionMismatchException.class);462tryCatch("%$c", UnknownFormatConversionException.class);463tryCatch("%.2c", IllegalFormatPrecisionException.class);464465//---------------------------------------------------------------------466// %s467//468// General conversion applicable to any argument.469//---------------------------------------------------------------------470test("%s", "Hello, Duke", "Hello, Duke");471test("%S", "HELLO, DUKE", "Hello, Duke");472test("%20S", " HELLO, DUKE", "Hello, Duke");473test("%20s", " Hello, Duke", "Hello, Duke");474test("%-20s", "Hello, Duke ", "Hello, Duke");475test("%-20.5s", "Hello ", "Hello, Duke");476test("%s", "null", (Object)null);477478StringBuffer sb = new StringBuffer("foo bar");479test("%s", sb.toString(), sb);480test("%S", sb.toString().toUpperCase(), sb);481482//---------------------------------------------------------------------483// %s - errors484//---------------------------------------------------------------------485tryCatch("%-s", MissingFormatWidthException.class);486tryCatch("%--s", DuplicateFormatFlagsException.class);487tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);488tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);489tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");490tryCatch("%#s", FormatFlagsConversionMismatchException.class, null);491492//---------------------------------------------------------------------493// %h494//495// General conversion applicable to any argument.496//---------------------------------------------------------------------497test("%h", Integer.toHexString("Hello, Duke".hashCode()),498"Hello, Duke");499test("%10h", " ddf63471", "Hello, Duke");500test("%-10h", "ddf63471 ", "Hello, Duke");501test("%-10H", "DDF63471 ", "Hello, Duke");502test("%10h", " 402e0000", 15.0);503test("%10H", " 402E0000", 15.0);504505//---------------------------------------------------------------------506// %h - errors507//---------------------------------------------------------------------508tryCatch("%#h", FormatFlagsConversionMismatchException.class);509510//---------------------------------------------------------------------511// flag/conversion errors512//---------------------------------------------------------------------513tryCatch("%F", UnknownFormatConversionException.class);514515tryCatch("%#g", FormatFlagsConversionMismatchException.class);516517518519520long minByte = Byte.MIN_VALUE; // -128521522523524525//---------------------------------------------------------------------526// %d527//528// Numeric conversion applicable to byte, short, int, long, and529// BigInteger.530//---------------------------------------------------------------------531test("%d", "null", (Object)null);532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577//---------------------------------------------------------------------578// %d - int and long579//---------------------------------------------------------------------580long oneToSeven = (long) 1234567;581test("%d", "1234567", oneToSeven);582test("%,d", "1,234,567", oneToSeven);583test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", oneToSeven);584test("%,d", "-1,234,567", negate(oneToSeven));585test("%(d", "1234567", oneToSeven);586test("%(d", "(1234567)", negate(oneToSeven));587test("% d", " 1234567", oneToSeven);588test("% d", "-1234567", negate(oneToSeven));589test("%+d", "+1234567", oneToSeven);590test("%+d", "-1234567", negate(oneToSeven));591test("%010d", "0001234567", oneToSeven);592test("%010d", "-001234567", negate(oneToSeven));593test("%(10d", " (1234567)", negate(oneToSeven));594test("%-10d", "1234567 ", oneToSeven);595test("%-10d", "-1234567 ", negate(oneToSeven));596597598599//---------------------------------------------------------------------600// %d - errors601//---------------------------------------------------------------------602tryCatch("%#d", FormatFlagsConversionMismatchException.class);603tryCatch("%D", UnknownFormatConversionException.class);604tryCatch("%0d", MissingFormatWidthException.class);605tryCatch("%-d", MissingFormatWidthException.class);606tryCatch("%7.3d", IllegalFormatPrecisionException.class);607608//---------------------------------------------------------------------609// %o610//611// Numeric conversion applicable to byte, short, int, long, and612// BigInteger.613//---------------------------------------------------------------------614test("%o", "null", (Object)null);615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648//---------------------------------------------------------------------649// %o - long650//---------------------------------------------------------------------651test("%024o", "001777777777777777777600", minByte);652test("%-24o", "1777777777777777777600 ", minByte);653test("%#24o", " 01777777777777777777600", minByte);654655long oneToSevenOct = (long) 1234567;656test("%o", "4553207", oneToSevenOct);657test("%010o", "0004553207", oneToSevenOct);658test("%-10o", "4553207 ", oneToSevenOct);659test("%#10o", " 04553207", oneToSevenOct);660661662//---------------------------------------------------------------------663// %o - errors664//---------------------------------------------------------------------665tryCatch("%(o", FormatFlagsConversionMismatchException.class,666minByte);667tryCatch("%+o", FormatFlagsConversionMismatchException.class,668minByte);669tryCatch("% o", FormatFlagsConversionMismatchException.class,670minByte);671tryCatch("%0o", MissingFormatWidthException.class);672tryCatch("%-o", MissingFormatWidthException.class);673tryCatch("%,o", FormatFlagsConversionMismatchException.class);674tryCatch("%O", UnknownFormatConversionException.class);675676//---------------------------------------------------------------------677// %x678//679// Numeric conversion applicable to byte, short, int, long, and680// BigInteger.681//---------------------------------------------------------------------682test("%x", "null", (Object)null);683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727//---------------------------------------------------------------------728// %x - long729//---------------------------------------------------------------------730long oneToSevenHex = (long)1234567;731test("%x", "null", (Object)null);732test("%x", "12d687", oneToSevenHex);733test("%010x", "000012d687", oneToSevenHex);734test("%-10x", "12d687 ", oneToSevenHex);735test("%#10x", " 0x12d687", oneToSevenHex);736test("%#10X", " 0X12D687",oneToSevenHex);737test("%X", "12D687", oneToSevenHex);738739test("%018x", "00ffffffffffffff80", minByte);740test("%-18x", "ffffffffffffff80 ", minByte);741test("%#20x", " 0xffffffffffffff80", minByte);742test("%0#20x", "0x00ffffffffffffff80", minByte);743test("%#20X", " 0XFFFFFFFFFFFFFF80", minByte);744test("%X", "FFFFFFFFFFFFFF80", minByte);745746//---------------------------------------------------------------------747// %x - errors748//---------------------------------------------------------------------749tryCatch("%,x", FormatFlagsConversionMismatchException.class);750tryCatch("%0x", MissingFormatWidthException.class);751tryCatch("%-x", MissingFormatWidthException.class);752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575//---------------------------------------------------------------------1576// %t1577//1578// Date/Time conversions applicable to Calendar, Date, and long.1579//---------------------------------------------------------------------1580test("%tA", "null", (Object)null);1581test("%TA", "NULL", (Object)null);15821583//---------------------------------------------------------------------1584// %t - errors1585//---------------------------------------------------------------------1586tryCatch("%t", UnknownFormatConversionException.class);1587tryCatch("%T", UnknownFormatConversionException.class);1588tryCatch("%tP", UnknownFormatConversionException.class);1589tryCatch("%TP", UnknownFormatConversionException.class);1590tryCatch("%.5tB", IllegalFormatPrecisionException.class);1591tryCatch("%#tB", FormatFlagsConversionMismatchException.class);1592tryCatch("%-tB", MissingFormatWidthException.class);159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688//---------------------------------------------------------------------1689// %n1690//---------------------------------------------------------------------1691test("%n", System.getProperty("line.separator"), (Object)null);1692test("%n", System.getProperty("line.separator"), "");16931694tryCatch("%,n", IllegalFormatFlagsException.class);1695tryCatch("%.n", UnknownFormatConversionException.class);1696tryCatch("%5.n", UnknownFormatConversionException.class);1697tryCatch("%5n", IllegalFormatWidthException.class);1698tryCatch("%.7n", IllegalFormatPrecisionException.class);1699tryCatch("%<n", IllegalFormatFlagsException.class);17001701//---------------------------------------------------------------------1702// %%1703//---------------------------------------------------------------------1704test("%%", "%", (Object)null);1705test("%%", "%", "");1706tryCatch("%%%", UnknownFormatConversionException.class);1707// perhaps an IllegalFormatArgumentIndexException should be defined?1708tryCatch("%<%", IllegalFormatFlagsException.class);1709}1710}171117121713