Path: blob/master/test/jdk/java/util/Formatter/BasicBigDecimal.java
41149 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 BasicBigDecimal 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}141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251private static BigDecimal create(double v) {252return new BigDecimal(v);253}254255private static BigDecimal negate(BigDecimal v) {256return v.negate();257}258259private static BigDecimal mult(BigDecimal v, double mul) {260return v.multiply(new BigDecimal(mul));261}262263private static BigDecimal recip(BigDecimal v) {264return BigDecimal.ONE.divide(v);265}266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337public 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);521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864//---------------------------------------------------------------------865// %s - BigDecimal866//---------------------------------------------------------------------867BigDecimal one = BigDecimal.ONE;868BigDecimal ten = BigDecimal.TEN;869BigDecimal pi = new BigDecimal(Math.PI);870BigDecimal piToThe300 = pi.pow(300);871872test("%s", "3.141592653589793115997963468544185161590576171875", pi);873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915//---------------------------------------------------------------------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+149952953//---------------------------------------------------------------------954// %e - BigDecimal955//---------------------------------------------------------------------956test("%.3e", "1.396e+149", piToThe300);957test("%.3e", "-1.396e+149", piToThe300.negate());958test("%.3e", "1.000e-100", recip(ten.pow(100)));959test("%.3e", "-1.000e-100", negate(recip(ten.pow(100))));960961test("%3.0e", "1e-06", new BigDecimal("0.000001"));962test("%3.0e", "1e-05", new BigDecimal("0.00001"));963test("%3.0e", "1e-04", new BigDecimal("0.0001"));964test("%3.0e", "1e-03", new BigDecimal("0.001"));965test("%3.0e", "1e-02", new BigDecimal("0.01"));966test("%3.0e", "1e-01", new BigDecimal("0.1"));967test("%3.0e", "9e-01", new BigDecimal("0.9"));968test("%3.1e", "9.0e-01", new BigDecimal("0.9"));969test("%3.0e", "1e+00", new BigDecimal("1.00"));970test("%3.0e", "1e+01", new BigDecimal("10.00"));971test("%3.0e", "1e+02", new BigDecimal("99.19"));972test("%3.1e", "9.9e+01", new BigDecimal("99.19"));973test("%3.0e", "1e+02", new BigDecimal("99.99"));974test("%3.0e", "1e+02", new BigDecimal("100.00"));975test("%#3.0e", "1.e+03", new BigDecimal("1000.00"));976test("%3.0e", "1e+04", new BigDecimal("10000.00"));977test("%3.0e", "1e+05", new BigDecimal("100000.00"));978test("%3.0e", "1e+06", new BigDecimal("1000000.00"));979test("%3.0e", "1e+07", new BigDecimal("10000000.00"));980test("%3.0e", "1e+08", new BigDecimal("100000000.00"));981982983test("%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);10801081//---------------------------------------------------------------------1082// %f - BigDecimal1083//---------------------------------------------------------------------1084test("%4.0f", " 99", new BigDecimal("99.19"));1085test("%4.1f", "99.2", new BigDecimal("99.19"));10861087BigDecimal val = new BigDecimal("99.95");1088test("%4.0f", " 100", val);1089test("%#4.0f", "100.", val);1090test("%4.1f", "100.0", val);1091test("%4.2f", "99.95", val);1092test("%4.3f", "99.950", val);10931094val = new BigDecimal(".99");1095test("%4.1f", " 1.0", val);1096test("%4.2f", "0.99", val);1097test("%4.3f", "0.990", val);10981099// #64764251100val = new BigDecimal("0.00001");1101test("%.0f", "0", val);1102test("%.1f", "0.0", val);1103test("%.2f", "0.00", val);1104test("%.3f", "0.000", val);1105test("%.4f", "0.0000", val);1106test("%.5f", "0.00001", val);11071108val = new BigDecimal("1.00001");1109test("%.0f", "1", val);1110test("%.1f", "1.0", val);1111test("%.2f", "1.00", val);1112test("%.3f", "1.000", val);1113test("%.4f", "1.0000", val);1114test("%.5f", "1.00001", val);11151116val = new BigDecimal("1.23456");1117test("%.0f", "1", val);1118test("%.1f", "1.2", val);1119test("%.2f", "1.23", val);1120test("%.3f", "1.235", val);1121test("%.4f", "1.2346", val);1122test("%.5f", "1.23456", val);1123test("%.6f", "1.234560", val);11241125val = new BigDecimal("9.99999");1126test("%.0f", "10", val);1127test("%.1f", "10.0", val);1128test("%.2f", "10.00", val);1129test("%.3f", "10.000", val);1130test("%.4f", "10.0000", val);1131test("%.5f", "9.99999", val);1132test("%.6f", "9.999990", val);113311341135val = new BigDecimal("1.99999");1136test("%.0f", "2", val);1137test("%.1f", "2.0", val);1138test("%.2f", "2.00", val);1139test("%.3f", "2.000", val);1140test("%.4f", "2.0000", val);1141test("%.5f", "1.99999", val);1142test("%.6f", "1.999990", val);11431144val = new BigDecimal(0.9996);1145test("%.0f", "1", val);1146test("%.1f", "1.0", val);1147test("%.2f", "1.00", val);1148test("%.3f", "1.000", val);1149test("%.4f", "0.9996", val);1150test("%.5f", "0.99960", val);1151test("%.6f", "0.999600", val);11521153val = new BigDecimal(BigInteger.ZERO, 6);1154test("%.4f", "0.0000", val);1155val = new BigDecimal(BigInteger.ZERO, -6);1156test("%.4f", "0.0000", val);115711581159116011611162116311641165116611671168116911701171117211731174117511761177//---------------------------------------------------------------------1178// %f - float, double, Double, BigDecimal1179//---------------------------------------------------------------------1180test("%.3f", "3141592.654", mult(pi, 1000000.0));1181test("%.3f", "-3141592.654", mult(pi, -1000000.0));1182test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0));1183test(Locale.FRANCE, "%,.4f", "3\u202f141\u202f592,6536", mult(pi, 1000000.0));1184test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0));1185test("%(.4f", "3141592.6536", mult(pi, 1000000.0));1186test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0));1187test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0));1188test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0));11891190119111921193//---------------------------------------------------------------------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+14912781279//---------------------------------------------------------------------1280// %g - BigDecimal1281//---------------------------------------------------------------------1282test("%.3g", "1.40e+149", piToThe300);1283test("%.3g", "-1.40e+149", piToThe300.negate());1284test(Locale.FRANCE, "%.3g", "-1,40e+149", piToThe300.negate());1285test("%.3g", "1.00e-100", recip(ten.pow(100)));1286test("%.3g", "-1.00e-100", negate(recip(ten.pow(100))));12871288test("%3.0g", "1e-06", new BigDecimal("0.000001"));1289test("%3.0g", "1e-05", new BigDecimal("0.00001"));1290test("%3.0g", "0.0001", new BigDecimal("0.0001"));1291test("%3.0g", "0.001", new BigDecimal("0.001"));1292test("%3.3g", "0.00100", new BigDecimal("0.001"));1293test("%3.4g", "0.001000", new BigDecimal("0.001"));1294test("%3.0g", "0.01", new BigDecimal("0.01"));1295test("%3.0g", "0.1", new BigDecimal("0.1"));1296test("%3.0g", "0.9", new BigDecimal("0.9"));1297test("%3.1g", "0.9", new BigDecimal("0.9"));1298test("%3.0g", " 1", new BigDecimal("1.00"));1299test("%3.2g", " 10", new BigDecimal("10.00"));1300test("%3.0g", "1e+01", new BigDecimal("10.00"));1301test("%3.0g", "1e+02", new BigDecimal("99.19"));1302test("%3.1g", "1e+02", new BigDecimal("99.19"));1303test("%3.2g", " 99", new BigDecimal("99.19"));1304test("%3.0g", "1e+02", new BigDecimal("99.99"));1305test("%3.0g", "1e+02", new BigDecimal("100.00"));1306test("%3.0g", "1e+03", new BigDecimal("1000.00"));1307test("%3.0g", "1e+04", new BigDecimal("10000.00"));1308test("%3.0g", "1e+05", new BigDecimal("100000.00"));1309test("%3.0g", "1e+06", new BigDecimal("1000000.00"));1310test("%3.0g", "1e+07", new BigDecimal("10000000.00"));1311test("%3.9g", "100000000", new BigDecimal("100000000.00"));1312test("%3.10g", "100000000.0", new BigDecimal("100000000.00"));131313141315test("%.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));13271328132913301331133213331334test("%,.11g", "3,141,592.6536", mult(pi, 1000000.0));1335test("%(,.11g", "(3,141,592.6536)", mult(pi, -1000000.0));133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425//---------------------------------------------------------------------1426// %f, %e, %g, %a - Boundaries1427//---------------------------------------------------------------------14281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663//---------------------------------------------------------------------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