Path: blob/master/test/jdk/java/util/Formatter/BasicFloat.java
41152 views
/*1* Copyright (c) 2003, 2019, 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 BasicFloat 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}141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268private static float create(double v) {269return (float) v;270}271272private static float negate(double v) {273return (float) -v;274}275276private static float mult(float v, double mul) {277return v * (float) mul;278}279280private static float recip(float v) {281return 1.0f / v;282}283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337public 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);521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875//---------------------------------------------------------------------876// %s - float877//---------------------------------------------------------------------878float one = 1.0f;879float ten = 10.0f;880float pi = (float) Math.PI;881882test("%s", "3.1415927", pi);883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915//---------------------------------------------------------------------916// flag/conversion errors917//---------------------------------------------------------------------918tryCatch("%d", IllegalFormatConversionException.class, one);919tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one);920921//---------------------------------------------------------------------922// %e923//924// Floating-point conversions applicable to float, double, and925// BigDecimal.926//---------------------------------------------------------------------927test("%e", "null", (Object)null);928929//---------------------------------------------------------------------930// %e - float and double931//---------------------------------------------------------------------932// double PI = 3.141 592 653 589 793 238 46;933test("%e", "3.141593e+00", pi);934test("%.0e", "1e+01", ten);935test("%#.0e", "1.e+01", ten);936test("%E", "3.141593E+00", pi);937test("%10.3e", " 3.142e+00", pi);938test("%10.3e", "-3.142e+00", negate(pi));939test("%010.3e", "03.142e+00", pi);940test("%010.3e", "-3.142e+00", negate(pi));941test("%-12.3e", "3.142e+00 ", pi);942test("%-12.3e", "-3.142e+00 ", negate(pi));943test("%.3e", "3.142e+00", pi);944test("%.3e", "-3.142e+00", negate(pi));945test("%.3e", "3.142e+06", mult(pi, 1000000.0));946test("%.3e", "-3.142e+06", mult(pi, -1000000.0));947948test(Locale.FRANCE, "%e", "3,141593e+00", pi);949950// double PI^300951// = 13962455701329742638131355433930076081862072808 ... e+149952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983test("%10.3e", " 1.000e+00", one);984test("%+.3e", "+3.142e+00", pi);985test("%+.3e", "-3.142e+00", negate(pi));986test("% .3e", " 3.142e+00", pi);987test("% .3e", "-3.142e+00", negate(pi));988test("%#.0e", "3.e+00", create(3.0));989test("%#.0e", "-3.e+00", create(-3.0));990test("%.0e", "3e+00", create(3.0));991test("%.0e", "-3e+00", create(-3.0));992993test("%(.4e", "3.1416e+06", mult(pi, 1000000.0));994test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0));995996//---------------------------------------------------------------------997// %e - boundary problems998//---------------------------------------------------------------------999test("%3.0e", "1e-06", 0.000001);1000test("%3.0e", "1e-05", 0.00001);1001test("%3.0e", "1e-04", 0.0001);1002test("%3.0e", "1e-03", 0.001);1003test("%3.0e", "1e-02", 0.01);1004test("%3.0e", "1e-01", 0.1);1005test("%3.0e", "9e-01", 0.9);1006test("%3.1e", "9.0e-01", 0.9);1007test("%3.0e", "1e+00", 1.00);1008test("%3.0e", "1e+01", 10.00);1009test("%3.0e", "1e+02", 99.19);1010test("%3.1e", "9.9e+01", 99.19);1011test("%3.0e", "1e+02", 99.99);1012test("%3.0e", "1e+02", 100.00);1013test("%#3.0e", "1.e+03", 1000.00);1014test("%3.0e", "1e+04", 10000.00);1015test("%3.0e", "1e+05", 100000.00);1016test("%3.0e", "1e+06", 1000000.00);1017test("%3.0e", "1e+07", 10000000.00);1018test("%3.0e", "1e+08", 100000000.00);10191020//---------------------------------------------------------------------1021// %f1022//1023// Floating-point conversions applicable to float, double, and1024// BigDecimal.1025//---------------------------------------------------------------------1026test("%f", "null", (Object)null);1027test("%f", "3.141593", pi);1028test(Locale.FRANCE, "%f", "3,141593", pi);1029test("%10.3f", " 3.142", pi);1030test("%10.3f", " -3.142", negate(pi));1031test("%010.3f", "000003.142", pi);1032test("%010.3f", "-00003.142", negate(pi));1033test("%-10.3f", "3.142 ", pi);1034test("%-10.3f", "-3.142 ", negate(pi));1035test("%.3f", "3.142", pi);1036test("%.3f", "-3.142", negate(pi));1037test("%+.3f", "+3.142", pi);1038test("%+.3f", "-3.142", negate(pi));1039test("% .3f", " 3.142", pi);1040test("% .3f", "-3.142", negate(pi));1041test("%#.0f", "3.", create(3.0));1042test("%#.0f", "-3.", create(-3.0));1043test("%.0f", "3", create(3.0));1044test("%.0f", "-3", create(-3.0));1045test("%.3f", "10.000", ten);1046test("%.3f", "1.000", one);1047test("%10.3f", " 1.000", one);10481049//---------------------------------------------------------------------1050// %f - boundary problems1051//---------------------------------------------------------------------1052test("%3.0f", " 0", 0.000001);1053test("%3.0f", " 0", 0.00001);1054test("%3.0f", " 0", 0.0001);1055test("%3.0f", " 0", 0.001);1056test("%3.0f", " 0", 0.01);1057test("%3.0f", " 0", 0.1);1058test("%3.0f", " 1", 0.9);1059test("%3.1f", "0.9", 0.9);1060test("%3.0f", " 1", 1.00);1061test("%3.0f", " 10", 10.00);1062test("%3.0f", " 99", 99.19);1063test("%3.1f", "99.2", 99.19);1064test("%3.0f", "100", 99.99);1065test("%3.0f", "100", 100.00);1066test("%#3.0f", "1000.", 1000.00);1067test("%3.0f", "10000", 10000.00);1068test("%3.0f", "100000", 100000.00);1069test("%3.0f", "1000000", 1000000.00);1070test("%3.0f", "10000000", 10000000.00);1071test("%3.0f", "100000000", 100000000.00);1072test("%10.0f", " 1000000", 1000000.00);1073test("%,10.0f", " 1,000,000", 1000000.00);1074test("%,10.1f", "1,000,000.0", 1000000.00);1075test("%,3.0f", "1,000,000", 1000000.00);1076test("%,3.0f", "10,000,000", 10000000.00);1077test("%,3.0f", "100,000,000", 100000000.00);1078test("%,3.0f", "10,000,000", 10000000.00);1079test("%,3.0f", "100,000,000", 100000000.00);108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160//---------------------------------------------------------------------1161// %f - float1162//---------------------------------------------------------------------1163// Float can not accurately store 1e6 * PI.1164test("%.3f", "3141.593", mult(pi, 1000.0));1165test("%.3f", "-3141.593", mult(pi, -1000.0));11661167test("%,.2f", "3,141.59", mult(pi, 1000.0));1168test(Locale.FRANCE, "%,.2f", "3\u202f141,59", mult(pi, 1000.0));1169test("%,.2f", "-3,141.59", mult(pi, -1000.0));1170test("%(.2f", "3141.59", mult(pi, 1000.0));1171test("%(.2f", "(3141.59)", mult(pi, -1000.0));1172test("%(,.2f", "3,141.59", mult(pi, 1000.0));1173test("%(,.2f", "(3,141.59)", mult(pi, -1000.0));11741175117611771178117911801181118211831184118511861187118811891190119111921193//---------------------------------------------------------------------1194// %g1195//1196// Floating-point conversions applicable to float, double, and1197// BigDecimal.1198//---------------------------------------------------------------------1199test("%g", "null", (Object)null);1200test("%g", "3.14159", pi);1201test(Locale.FRANCE, "%g", "3,14159", pi);1202test("%.0g", "1e+01", ten);1203test("%G", "3.14159", pi);1204test("%10.3g", " 3.14", pi);1205test("%10.3g", " -3.14", negate(pi));1206test("%010.3g", "0000003.14", pi);1207test("%010.3g", "-000003.14", negate(pi));1208test("%-12.3g", "3.14 ", pi);1209test("%-12.3g", "-3.14 ", negate(pi));1210test("%.3g", "3.14", pi);1211test("%.3g", "-3.14", negate(pi));1212test("%.3g", "3.14e+08", mult(pi, 100000000.0));1213test("%.3g", "-3.14e+08", mult(pi, -100000000.0));12141215test("%.3g", "1.00e-05", recip(create(100000.0)));1216test("%.3g", "-1.00e-05", recip(create(-100000.0)));1217test("%.0g", "-1e-05", recip(create(-100000.0)));1218test("%.0g", "1e+05", create(100000.0));1219test("%.3G", "1.00E-05", recip(create(100000.0)));1220test("%.3G", "-1.00E-05", recip(create(-100000.0)));12211222test("%.1g", "-0", -0.0);1223test("%3.0g", " -0", -0.0);1224test("%.1g", "0", 0.0);1225test("%3.0g", " 0", 0.0);1226test("%.1g", "0", +0.0);1227test("%3.0g", " 0", +0.0);12281229test("%3.0g", "1e-06", 0.000001);1230test("%3.0g", "1e-05", 0.00001);1231test("%3.0g", "1e-05", 0.0000099);1232test("%3.1g", "1e-05", 0.0000099);1233test("%3.2g", "9.9e-06", 0.0000099);1234test("%3.0g", "0.0001", 0.0001);1235test("%3.0g", "9e-05", 0.00009);1236test("%3.0g", "0.0001", 0.000099);1237test("%3.1g", "0.0001", 0.000099);1238test("%3.2g", "9.9e-05", 0.000099);1239test("%3.0g", "0.001", 0.001);1240test("%3.0g", "0.001", 0.00099);1241test("%3.1g", "0.001", 0.00099);1242test("%3.2g", "0.00099", 0.00099);1243test("%3.3g", "0.00100", 0.001);1244test("%3.4g", "0.001000", 0.001);1245test("%3.0g", "0.01", 0.01);1246test("%3.0g", "0.1", 0.1);1247test("%3.0g", "0.9", 0.9);1248test("%3.1g", "0.9", 0.9);1249test("%3.0g", " 1", 1.00);1250test("%3.2g", " 10", 10.00);1251test("%3.0g", "1e+01", 10.00);1252test("%3.0g", "1e+02", 99.19);1253test("%3.1g", "1e+02", 99.19);1254test("%3.2g", " 99", 99.19);1255test("%3.0g", "1e+02", 99.9);1256test("%3.1g", "1e+02", 99.9);1257test("%3.2g", "1.0e+02", 99.9);1258test("%3.0g", "1e+02", 99.99);1259test("%3.0g", "1e+02", 100.00);1260test("%3.0g", "1e+03", 999.9);1261test("%3.1g", "1e+03", 999.9);1262test("%3.2g", "1.0e+03", 999.9);1263test("%3.3g", "1.00e+03", 999.9);1264test("%3.4g", "999.9", 999.9);1265test("%3.4g", "1000", 999.99);1266test("%3.0g", "1e+03", 1000.00);1267test("%3.0g", "1e+04", 10000.00);1268test("%3.0g", "1e+05", 100000.00);1269test("%3.0g", "1e+06", 1000000.00);1270test("%3.0g", "1e+07", 10000000.00);1271test("%3.9g", "100000000", 100000000.00);1272test("%3.10g", "100000000.0", 100000000.00);12731274tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00);12751276// double PI^3001277// = 13962455701329742638131355433930076081862072808 ... e+14912781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315test("%.3g", "10.0", ten);1316test("%.3g", "1.00", one);1317test("%10.3g", " 1.00", one);1318test("%+10.3g", " +3.14", pi);1319test("%+10.3g", " -3.14", negate(pi));1320test("% .3g", " 3.14", pi);1321test("% .3g", "-3.14", negate(pi));1322test("%.0g", "3", create(3.0));1323test("%.0g", "-3", create(-3.0));13241325test("%(.4g", "3.142e+08", mult(pi, 100000000.0));1326test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0));132713281329// Float can not accurately store 1e6 * PI.1330test("%,.6g", "3,141.59", mult(pi, 1000.0));1331test("%(,.6g", "(3,141.59)", mult(pi, -1000.0));1332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425//---------------------------------------------------------------------1426// %f, %e, %g, %a - Boundaries1427//---------------------------------------------------------------------14281429//---------------------------------------------------------------------1430// %f, %e, %g, %a - NaN1431//---------------------------------------------------------------------1432test("%f", "NaN", Float.NaN);1433// s1434test("%+f", "NaN", Float.NaN);1435// test("%F", "NAN", Float.NaN);1436test("%e", "NaN", Float.NaN);1437test("%+e", "NaN", Float.NaN);1438test("%E", "NAN", Float.NaN);1439test("%g", "NaN", Float.NaN);1440test("%+g", "NaN", Float.NaN);1441test("%G", "NAN", Float.NaN);1442test("%a", "NaN", Float.NaN);1443test("%+a", "NaN", Float.NaN);1444test("%A", "NAN", Float.NaN);14451446//---------------------------------------------------------------------1447// %f, %e, %g, %a - +0.01448//---------------------------------------------------------------------1449test("%f", "0.000000", +0.0);1450test("%+f", "+0.000000", +0.0);1451test("% f", " 0.000000", +0.0);1452// test("%F", "0.000000", +0.0);1453test("%e", "0.000000e+00", 0e0);1454test("%e", "0.000000e+00", +0.0);1455test("%+e", "+0.000000e+00", +0.0);1456test("% e", " 0.000000e+00", +0.0);1457test("%E", "0.000000E+00", 0e0);1458test("%E", "0.000000E+00", +0.0);1459test("%+E", "+0.000000E+00", +0.0);1460test("% E", " 0.000000E+00", +0.0);1461test("%g", "0.00000", +0.0);1462test("%+g", "+0.00000", +0.0);1463test("% g", " 0.00000", +0.0);1464test("%G", "0.00000", +0.0);1465test("% G", " 0.00000", +0.0);1466test("%a", "0x0.0p0", +0.0);1467test("%+a", "+0x0.0p0", +0.0);1468test("% a", " 0x0.0p0", +0.0);1469test("%A", "0X0.0P0", +0.0);1470test("% A", " 0X0.0P0", +0.0);14711472//---------------------------------------------------------------------1473// %f, %e, %g, %a - -0.01474//---------------------------------------------------------------------1475test("%f", "-0.000000", -0.0);1476test("%+f", "-0.000000", -0.0);1477// test("%F", "-0.000000", -0.0);1478test("%e", "-0.000000e+00", -0.0);1479test("%+e", "-0.000000e+00", -0.0);1480test("%E", "-0.000000E+00", -0.0);1481test("%+E", "-0.000000E+00", -0.0);1482test("%g", "-0.00000", -0.0);1483test("%+g", "-0.00000", -0.0);1484test("%G", "-0.00000", -0.0);1485test("%a", "-0x0.0p0", -0.0);1486test("%+a", "-0x0.0p0", -0.0);1487test("%+A", "-0X0.0P0", -0.0);14881489//---------------------------------------------------------------------1490// %f, %e, %g, %a - +Infinity1491//---------------------------------------------------------------------1492test("%f", "Infinity", Float.POSITIVE_INFINITY);1493test("%+f", "+Infinity", Float.POSITIVE_INFINITY);1494test("% f", " Infinity", Float.POSITIVE_INFINITY);1495// test("%F", "INFINITY", Float.POSITIVE_INFINITY);1496test("%e", "Infinity", Float.POSITIVE_INFINITY);1497test("%+e", "+Infinity", Float.POSITIVE_INFINITY);1498test("% e", " Infinity", Float.POSITIVE_INFINITY);1499test("%E", "INFINITY", Float.POSITIVE_INFINITY);1500test("%+E", "+INFINITY", Float.POSITIVE_INFINITY);1501test("% E", " INFINITY", Float.POSITIVE_INFINITY);1502test("%g", "Infinity", Float.POSITIVE_INFINITY);1503test("%+g", "+Infinity", Float.POSITIVE_INFINITY);1504test("%G", "INFINITY", Float.POSITIVE_INFINITY);1505test("% G", " INFINITY", Float.POSITIVE_INFINITY);1506test("%+G", "+INFINITY", Float.POSITIVE_INFINITY);1507test("%a", "Infinity", Float.POSITIVE_INFINITY);1508test("%+a", "+Infinity", Float.POSITIVE_INFINITY);1509test("% a", " Infinity", Float.POSITIVE_INFINITY);1510test("%A", "INFINITY", Float.POSITIVE_INFINITY);1511test("%+A", "+INFINITY", Float.POSITIVE_INFINITY);1512test("% A", " INFINITY", Float.POSITIVE_INFINITY);15131514//---------------------------------------------------------------------1515// %f, %e, %g, %a - -Infinity1516//---------------------------------------------------------------------1517test("%f", "-Infinity", Float.NEGATIVE_INFINITY);1518test("%+f", "-Infinity", Float.NEGATIVE_INFINITY);1519test("%(f", "(Infinity)", Float.NEGATIVE_INFINITY);1520// test("%F", "-INFINITY", Float.NEGATIVE_INFINITY);1521test("%e", "-Infinity", Float.NEGATIVE_INFINITY);1522test("%+e", "-Infinity", Float.NEGATIVE_INFINITY);1523test("%E", "-INFINITY", Float.NEGATIVE_INFINITY);1524test("%+E", "-INFINITY", Float.NEGATIVE_INFINITY);1525test("%g", "-Infinity", Float.NEGATIVE_INFINITY);1526test("%+g", "-Infinity", Float.NEGATIVE_INFINITY);1527test("%G", "-INFINITY", Float.NEGATIVE_INFINITY);1528test("%+G", "-INFINITY", Float.NEGATIVE_INFINITY);1529test("%a", "-Infinity", Float.NEGATIVE_INFINITY);1530test("%+a", "-Infinity", Float.NEGATIVE_INFINITY);1531test("%A", "-INFINITY", Float.NEGATIVE_INFINITY);1532test("%+A", "-INFINITY", Float.NEGATIVE_INFINITY);15331534//---------------------------------------------------------------------1535// %f, %e, %g, %a - Float.MIN_VALUE1536//---------------------------------------------------------------------1537test("%f", "0.000000", Float.MIN_VALUE);1538test("%,f", "0.000000", Float.MIN_VALUE);1539test("%(f", "(0.000000)", -Float.MIN_VALUE);1540test("%30.0f", " 0", Float.MIN_VALUE);1541test("%30.5f", " 0.00000", Float.MIN_VALUE);1542test("%30.13f", " 0.0000000000000", Float.MIN_VALUE);1543test("%30.20f", " 0.00000000000000000000", Float.MIN_VALUE);1544test("%e", "1.401298e-45", Float.MIN_VALUE);1545test("%E", "1.401298E-45", Float.MIN_VALUE);1546test("%(.1e", "1.4e-45", Float.MIN_VALUE);1547test("%(E", "(1.401298E-45)", -Float.MIN_VALUE);1548test("%30.5e", " 1.40130e-45", Float.MIN_VALUE);1549test("%30.13e", " 1.4012984643248e-45", Float.MIN_VALUE);1550test("%30.20e", " 1.40129846432481700000e-45", Float.MIN_VALUE);1551test("%g", "1.40130e-45", Float.MIN_VALUE);1552test("%G", "1.40130E-45", Float.MIN_VALUE);1553test("%(g", "1.40130e-45", Float.MIN_VALUE);1554test("%,g", "1.40130e-45", Float.MIN_VALUE);1555test("%(G", "(1.40130E-45)", -Float.MIN_VALUE);1556test("%30.5g", " 1.4013e-45", Float.MIN_VALUE);1557test("%30.13g", " 1.401298464325e-45", Float.MIN_VALUE);1558test("%30.20g", " 1.4012984643248170000e-45", Float.MIN_VALUE);1559test("%a", "0x1.0p-149", Float.MIN_VALUE);1560test("%A", "0X1.0P-149", Float.MIN_VALUE);1561test("%20a", " 0x1.0p-149", Float.MIN_VALUE);15621563//---------------------------------------------------------------------1564// %f, %e, %g, %a - Float.MAX_VALUE1565//---------------------------------------------------------------------1566test("%f", "340282346638528860000000000000000000000.000000", Float.MAX_VALUE);1567test("%,f","340,282,346,638,528,860,000,000,000,000,000,000,000.000000",1568Float.MAX_VALUE);1569test("%(f", "(340282346638528860000000000000000000000.000000)", -Float.MAX_VALUE);1570test("%60.5f", " 340282346638528860000000000000000000000.00000",1571Float.MAX_VALUE);1572test("%60.13f", " 340282346638528860000000000000000000000.0000000000000",1573Float.MAX_VALUE);1574test("%61.20f", " 340282346638528860000000000000000000000.00000000000000000000",1575Float.MAX_VALUE);1576test("%e", "3.402823e+38", Float.MAX_VALUE);1577test("%E", "3.402823E+38", Float.MAX_VALUE);1578test("%(e", "3.402823e+38", Float.MAX_VALUE);1579test("%(e", "(3.402823e+38)", -Float.MAX_VALUE);1580test("%30.5e", " 3.40282e+38", Float.MAX_VALUE);1581test("%30.13e", " 3.4028234663853e+38", Float.MAX_VALUE);1582test("%30.20e", " 3.40282346638528860000e+38", Float.MAX_VALUE);1583test("%g", "3.40282e+38", Float.MAX_VALUE);1584test("%G", "3.40282E+38", Float.MAX_VALUE);1585test("%,g", "3.40282e+38", Float.MAX_VALUE);1586test("%(g", "(3.40282e+38)", -Float.MAX_VALUE);1587test("%30.5g", " 3.4028e+38", Float.MAX_VALUE);1588test("%30.13g", " 3.402823466385e+38", Float.MAX_VALUE);1589test("%30.20G", " 3.4028234663852886000E+38", Float.MAX_VALUE);1590test("%a", "0x1.fffffep127", Float.MAX_VALUE);1591test("%A", "0X1.FFFFFEP127", Float.MAX_VALUE);1592test("%20a"," 0x1.fffffep127", Float.MAX_VALUE);15931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663//---------------------------------------------------------------------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