Path: blob/master/test/jdk/java/util/Formatter/BasicLongObject.java
41149 views
/*1* Copyright (c) 2003, 2016, 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.*;3738import static java.util.Calendar.*;394041424344public class BasicLongObject extends Basic {4546private static void test(String fs, String exp, Object ... args) {47Formatter f = new Formatter(new StringBuilder(), Locale.US);48f.format(fs, args);49ck(fs, exp, f.toString());5051f = new Formatter(new StringBuilder(), Locale.US);52f.format("foo " + fs + " bar", args);53ck(fs, "foo " + exp + " bar", f.toString());54}5556private static void test(Locale l, String fs, String exp, Object ... args)57{58Formatter f = new Formatter(new StringBuilder(), l);59f.format(fs, args);60ck(fs, exp, f.toString());6162f = new Formatter(new StringBuilder(), l);63f.format("foo " + fs + " bar", args);64ck(fs, "foo " + exp + " bar", f.toString());65}6667private static void test(String fs, Object ... args) {68Formatter f = new Formatter(new StringBuilder(), Locale.US);69f.format(fs, args);70ck(fs, "fail", f.toString());71}7273private static void test(String fs) {74Formatter f = new Formatter(new StringBuilder(), Locale.US);75f.format(fs, "fail");76ck(fs, "fail", f.toString());77}7879private static void testSysOut(String fs, String exp, Object ... args) {80FileOutputStream fos = null;81FileInputStream fis = null;82try {83PrintStream saveOut = System.out;84fos = new FileOutputStream("testSysOut");85System.setOut(new PrintStream(fos));86System.out.format(Locale.US, fs, args);87fos.close();8889fis = new FileInputStream("testSysOut");90byte [] ba = new byte[exp.length()];91int len = fis.read(ba);92String got = new String(ba);93if (len != ba.length)94fail(fs, exp, got);95ck(fs, exp, got);9697System.setOut(saveOut);98} catch (FileNotFoundException ex) {99fail(fs, ex.getClass());100} catch (IOException ex) {101fail(fs, ex.getClass());102} finally {103try {104if (fos != null)105fos.close();106if (fis != null)107fis.close();108} catch (IOException ex) {109fail(fs, ex.getClass());110}111}112}113114private static void tryCatch(String fs, Class<?> ex) {115boolean caught = false;116try {117test(fs);118} catch (Throwable x) {119if (ex.isAssignableFrom(x.getClass()))120caught = true;121}122if (!caught)123fail(fs, ex);124else125pass();126}127128private static void tryCatch(String fs, Class<?> ex, Object ... args) {129boolean caught = false;130try {131test(fs, args);132} catch (Throwable x) {133if (ex.isAssignableFrom(x.getClass()))134caught = true;135}136if (!caught)137fail(fs, ex);138else139pass();140}141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245private static Long negate(Long v) {246return new Long(-v.longValue());247}248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337public static void test() {338TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));339340// Any characters not explicitly defined as conversions, date/time341// conversion suffixes, or flags are illegal and are reserved for342// future extensions. Use of such a character in a format string will343// cause an UnknownFormatConversionException or344// UnknownFormatFlagsException to be thrown.345tryCatch("%q", UnknownFormatConversionException.class);346tryCatch("%t&", UnknownFormatConversionException.class);347tryCatch("%&d", UnknownFormatConversionException.class);348tryCatch("%^b", UnknownFormatConversionException.class);349350//---------------------------------------------------------------------351// Formatter.java class javadoc examples352//---------------------------------------------------------------------353test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);354test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");355test("Amount gained or lost since last statement: $ %,(.2f",356"Amount gained or lost since last statement: $ (6,217.58)",357(new BigDecimal("-6217.58")));358Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);359testSysOut("Local time: %tT", "Local time: 16:17:00", c);360361test("Unable to open file '%1$s': %2$s",362"Unable to open file 'food': No such file or directory",363"food", "No such file or directory");364Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);365duke.set(Calendar.MILLISECOND, 584);366test("Duke's Birthday: %1$tB %1$te, %1$tY",367"Duke's Birthday: May 23, 1995",368duke);369test("Duke's Birthday: %1$tB %1$te, %1$tY",370"Duke's Birthday: May 23, 1995",371duke.getTime());372test("Duke's Birthday: %1$tB %1$te, %1$tY",373"Duke's Birthday: May 23, 1995",374duke.getTimeInMillis());375376test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",377"d c b a d c b a", "a", "b", "c", "d");378test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");379test("%s %s %s %s", "a b c d", "a", "b", "c", "d");380test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");381382//---------------------------------------------------------------------383// %b384//385// General conversion applicable to any argument.386//---------------------------------------------------------------------387test("%b", "true", true);388test("%b", "false", false);389test("%B", "TRUE", true);390test("%B", "FALSE", false);391test("%b", "true", Boolean.TRUE);392test("%b", "false", Boolean.FALSE);393test("%B", "TRUE", Boolean.TRUE);394test("%B", "FALSE", Boolean.FALSE);395test("%14b", " true", true);396test("%-14b", "true ", true);397test("%5.1b", " f", false);398test("%-5.1b", "f ", false);399400test("%b", "true", "foo");401test("%b", "false", (Object)null);402403// Boolean.java hardcodes the Strings for "true" and "false", so no404// localization is possible.405test(Locale.FRANCE, "%b", "true", true);406test(Locale.FRANCE, "%b", "false", false);407408// If you pass in a single array to a varargs method, the compiler409// uses it as the array of arguments rather than treating it as a410// single array-type argument.411test("%b", "false", (Object[])new String[2]);412test("%b", "true", new String[2], new String[2]);413414int [] ia = { 1, 2, 3 };415test("%b", "true", ia);416417//---------------------------------------------------------------------418// %b - errors419//---------------------------------------------------------------------420tryCatch("%#b", FormatFlagsConversionMismatchException.class);421tryCatch("%-b", MissingFormatWidthException.class);422// correct or side-effect of implementation?423tryCatch("%.b", UnknownFormatConversionException.class);424tryCatch("%,b", FormatFlagsConversionMismatchException.class);425426//---------------------------------------------------------------------427// %c428//429// General conversion applicable to any argument.430//---------------------------------------------------------------------431test("%c", "i", 'i');432test("%C", "I", 'i');433test("%4c", " i", 'i');434test("%-4c", "i ", 'i');435test("%4C", " I", 'i');436test("%-4C", "I ", 'i');437test("%c", "i", new Character('i'));438test("%c", "H", (byte) 72);439test("%c", "i", (short) 105);440test("%c", "!", (int) 33);441test("%c", "\u007F", Byte.MAX_VALUE);442test("%c", new String(Character.toChars(Short.MAX_VALUE)),443Short.MAX_VALUE);444test("%c", "null", (Object) null);445446//---------------------------------------------------------------------447// %c - errors448//---------------------------------------------------------------------449tryCatch("%c", IllegalFormatConversionException.class,450Boolean.TRUE);451tryCatch("%c", IllegalFormatConversionException.class,452(float) 0.1);453tryCatch("%c", IllegalFormatConversionException.class,454new Object());455tryCatch("%c", IllegalFormatCodePointException.class,456Byte.MIN_VALUE);457tryCatch("%c", IllegalFormatCodePointException.class,458Short.MIN_VALUE);459tryCatch("%c", IllegalFormatCodePointException.class,460Integer.MIN_VALUE);461tryCatch("%c", IllegalFormatCodePointException.class,462Integer.MAX_VALUE);463464tryCatch("%#c", FormatFlagsConversionMismatchException.class);465tryCatch("%,c", FormatFlagsConversionMismatchException.class);466tryCatch("%(c", FormatFlagsConversionMismatchException.class);467tryCatch("%$c", UnknownFormatConversionException.class);468tryCatch("%.2c", IllegalFormatPrecisionException.class);469470//---------------------------------------------------------------------471// %s472//473// General conversion applicable to any argument.474//---------------------------------------------------------------------475test("%s", "Hello, Duke", "Hello, Duke");476test("%S", "HELLO, DUKE", "Hello, Duke");477test("%20S", " HELLO, DUKE", "Hello, Duke");478test("%20s", " Hello, Duke", "Hello, Duke");479test("%-20s", "Hello, Duke ", "Hello, Duke");480test("%-20.5s", "Hello ", "Hello, Duke");481test("%s", "null", (Object)null);482483StringBuffer sb = new StringBuffer("foo bar");484test("%s", sb.toString(), sb);485test("%S", sb.toString().toUpperCase(), sb);486487//---------------------------------------------------------------------488// %s - errors489//---------------------------------------------------------------------490tryCatch("%-s", MissingFormatWidthException.class);491tryCatch("%--s", DuplicateFormatFlagsException.class);492tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);493tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);494tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");495tryCatch("%#s", FormatFlagsConversionMismatchException.class, null);496497//---------------------------------------------------------------------498// %h499//500// General conversion applicable to any argument.501//---------------------------------------------------------------------502test("%h", Integer.toHexString("Hello, Duke".hashCode()),503"Hello, Duke");504test("%10h", " ddf63471", "Hello, Duke");505test("%-10h", "ddf63471 ", "Hello, Duke");506test("%-10H", "DDF63471 ", "Hello, Duke");507test("%10h", " 402e0000", 15.0);508test("%10H", " 402E0000", 15.0);509510//---------------------------------------------------------------------511// %h - errors512//---------------------------------------------------------------------513tryCatch("%#h", FormatFlagsConversionMismatchException.class);514515//---------------------------------------------------------------------516// flag/conversion errors517//---------------------------------------------------------------------518tryCatch("%F", UnknownFormatConversionException.class);519520tryCatch("%#g", FormatFlagsConversionMismatchException.class);521522523524525526527Long minByte = new Long(Byte.MIN_VALUE);528529530//---------------------------------------------------------------------531// %d532//533// Numeric conversion applicable to byte, short, int, long, and534// BigInteger.535//---------------------------------------------------------------------536test("%d", "null", (Object)null);537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622//---------------------------------------------------------------------623// %d - errors624//---------------------------------------------------------------------625tryCatch("%#d", FormatFlagsConversionMismatchException.class);626tryCatch("%D", UnknownFormatConversionException.class);627tryCatch("%0d", MissingFormatWidthException.class);628tryCatch("%-d", MissingFormatWidthException.class);629tryCatch("%7.3d", IllegalFormatPrecisionException.class);630631//---------------------------------------------------------------------632// %o633//634// Numeric conversion applicable to byte, short, int, long, and635// BigInteger.636//---------------------------------------------------------------------637test("%o", "null", (Object)null);638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685//---------------------------------------------------------------------686// %o - errors687//---------------------------------------------------------------------688tryCatch("%(o", FormatFlagsConversionMismatchException.class,689minByte);690tryCatch("%+o", FormatFlagsConversionMismatchException.class,691minByte);692tryCatch("% o", FormatFlagsConversionMismatchException.class,693minByte);694tryCatch("%0o", MissingFormatWidthException.class);695tryCatch("%-o", MissingFormatWidthException.class);696tryCatch("%,o", FormatFlagsConversionMismatchException.class);697tryCatch("%O", UnknownFormatConversionException.class);698699//---------------------------------------------------------------------700// %x701//702// Numeric conversion applicable to byte, short, int, long, and703// BigInteger.704//---------------------------------------------------------------------705test("%x", "null", (Object)null);706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769//---------------------------------------------------------------------770// %x - errors771//---------------------------------------------------------------------772tryCatch("%,x", FormatFlagsConversionMismatchException.class);773tryCatch("%0x", MissingFormatWidthException.class);774tryCatch("%-x", MissingFormatWidthException.class);7757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663//---------------------------------------------------------------------1664// %t1665//1666// Date/Time conversions applicable to Calendar, Date, and long.1667//---------------------------------------------------------------------1668test("%tA", "null", (Object)null);1669test("%TA", "NULL", (Object)null);16701671//---------------------------------------------------------------------1672// %t - errors1673//---------------------------------------------------------------------1674tryCatch("%t", UnknownFormatConversionException.class);1675tryCatch("%T", UnknownFormatConversionException.class);1676tryCatch("%tP", UnknownFormatConversionException.class);1677tryCatch("%TP", UnknownFormatConversionException.class);1678tryCatch("%.5tB", IllegalFormatPrecisionException.class);1679tryCatch("%#tB", FormatFlagsConversionMismatchException.class);1680tryCatch("%-tB", MissingFormatWidthException.class);168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776//---------------------------------------------------------------------1777// %n1778//---------------------------------------------------------------------1779test("%n", System.getProperty("line.separator"), (Object)null);1780test("%n", System.getProperty("line.separator"), "");17811782tryCatch("%,n", IllegalFormatFlagsException.class);1783tryCatch("%.n", UnknownFormatConversionException.class);1784tryCatch("%5.n", UnknownFormatConversionException.class);1785tryCatch("%5n", IllegalFormatWidthException.class);1786tryCatch("%.7n", IllegalFormatPrecisionException.class);1787tryCatch("%<n", IllegalFormatFlagsException.class);17881789//---------------------------------------------------------------------1790// %%1791//---------------------------------------------------------------------1792test("%%", "%", (Object)null);1793test("%%", "%", "");17941795test("%5%", " %", (Object)null);1796test("%5%", " %", "");1797test("%-5%", "% ", (Object)null);1798test("%-5%", "% ", "");17991800tryCatch("%.5%", IllegalFormatPrecisionException.class);1801tryCatch("%5.5%", IllegalFormatPrecisionException.class);18021803tryCatch("%%%", UnknownFormatConversionException.class);1804// perhaps an IllegalFormatArgumentIndexException should be defined?1805tryCatch("%<%", IllegalFormatFlagsException.class);1806}1807}180818091810