Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Formatter/BasicBigDecimal.java
38821 views
/*1* Copyright (c) 2003, 2013, 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 BasicBigDecimal 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}136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246private static BigDecimal create(double v) {247return new BigDecimal(v);248}249250private static BigDecimal negate(BigDecimal v) {251return v.negate();252}253254private static BigDecimal mult(BigDecimal v, double mul) {255return v.multiply(new BigDecimal(mul));256}257258private static BigDecimal recip(BigDecimal v) {259return BigDecimal.ONE.divide(v);260}261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332public 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);516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833//---------------------------------------------------------------------834// %s - BigDecimal835//---------------------------------------------------------------------836BigDecimal one = BigDecimal.ONE;837BigDecimal ten = BigDecimal.TEN;838BigDecimal pi = new BigDecimal(Math.PI);839BigDecimal piToThe300 = pi.pow(300);840841test("%s", "3.141592653589793115997963468544185161590576171875", pi);842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884//---------------------------------------------------------------------885// flag/conversion errors886//---------------------------------------------------------------------887tryCatch("%d", IllegalFormatConversionException.class, one);888tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one);889890//---------------------------------------------------------------------891// %e892//893// Floating-point conversions applicable to float, double, and894// BigDecimal.895//---------------------------------------------------------------------896test("%e", "null", (Object)null);897898//---------------------------------------------------------------------899// %e - float and double900//---------------------------------------------------------------------901// double PI = 3.141 592 653 589 793 238 46;902test("%e", "3.141593e+00", pi);903test("%.0e", "1e+01", ten);904test("%#.0e", "1.e+01", ten);905test("%E", "3.141593E+00", pi);906test("%10.3e", " 3.142e+00", pi);907test("%10.3e", "-3.142e+00", negate(pi));908test("%010.3e", "03.142e+00", pi);909test("%010.3e", "-3.142e+00", negate(pi));910test("%-12.3e", "3.142e+00 ", pi);911test("%-12.3e", "-3.142e+00 ", negate(pi));912test("%.3e", "3.142e+00", pi);913test("%.3e", "-3.142e+00", negate(pi));914test("%.3e", "3.142e+06", mult(pi, 1000000.0));915test("%.3e", "-3.142e+06", mult(pi, -1000000.0));916917test(Locale.FRANCE, "%e", "3,141593e+00", pi);918919// double PI^300920// = 13962455701329742638131355433930076081862072808 ... e+149921922//---------------------------------------------------------------------923// %e - BigDecimal924//---------------------------------------------------------------------925test("%.3e", "1.396e+149", piToThe300);926test("%.3e", "-1.396e+149", piToThe300.negate());927test("%.3e", "1.000e-100", recip(ten.pow(100)));928test("%.3e", "-1.000e-100", negate(recip(ten.pow(100))));929930test("%3.0e", "1e-06", new BigDecimal("0.000001"));931test("%3.0e", "1e-05", new BigDecimal("0.00001"));932test("%3.0e", "1e-04", new BigDecimal("0.0001"));933test("%3.0e", "1e-03", new BigDecimal("0.001"));934test("%3.0e", "1e-02", new BigDecimal("0.01"));935test("%3.0e", "1e-01", new BigDecimal("0.1"));936test("%3.0e", "9e-01", new BigDecimal("0.9"));937test("%3.1e", "9.0e-01", new BigDecimal("0.9"));938test("%3.0e", "1e+00", new BigDecimal("1.00"));939test("%3.0e", "1e+01", new BigDecimal("10.00"));940test("%3.0e", "1e+02", new BigDecimal("99.19"));941test("%3.1e", "9.9e+01", new BigDecimal("99.19"));942test("%3.0e", "1e+02", new BigDecimal("99.99"));943test("%3.0e", "1e+02", new BigDecimal("100.00"));944test("%#3.0e", "1.e+03", new BigDecimal("1000.00"));945test("%3.0e", "1e+04", new BigDecimal("10000.00"));946test("%3.0e", "1e+05", new BigDecimal("100000.00"));947test("%3.0e", "1e+06", new BigDecimal("1000000.00"));948test("%3.0e", "1e+07", new BigDecimal("10000000.00"));949test("%3.0e", "1e+08", new BigDecimal("100000000.00"));950951952test("%10.3e", " 1.000e+00", one);953test("%+.3e", "+3.142e+00", pi);954test("%+.3e", "-3.142e+00", negate(pi));955test("% .3e", " 3.142e+00", pi);956test("% .3e", "-3.142e+00", negate(pi));957test("%#.0e", "3.e+00", create(3.0));958test("%#.0e", "-3.e+00", create(-3.0));959test("%.0e", "3e+00", create(3.0));960test("%.0e", "-3e+00", create(-3.0));961962test("%(.4e", "3.1416e+06", mult(pi, 1000000.0));963test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0));964965//---------------------------------------------------------------------966// %e - boundary problems967//---------------------------------------------------------------------968test("%3.0e", "1e-06", 0.000001);969test("%3.0e", "1e-05", 0.00001);970test("%3.0e", "1e-04", 0.0001);971test("%3.0e", "1e-03", 0.001);972test("%3.0e", "1e-02", 0.01);973test("%3.0e", "1e-01", 0.1);974test("%3.0e", "9e-01", 0.9);975test("%3.1e", "9.0e-01", 0.9);976test("%3.0e", "1e+00", 1.00);977test("%3.0e", "1e+01", 10.00);978test("%3.0e", "1e+02", 99.19);979test("%3.1e", "9.9e+01", 99.19);980test("%3.0e", "1e+02", 99.99);981test("%3.0e", "1e+02", 100.00);982test("%#3.0e", "1.e+03", 1000.00);983test("%3.0e", "1e+04", 10000.00);984test("%3.0e", "1e+05", 100000.00);985test("%3.0e", "1e+06", 1000000.00);986test("%3.0e", "1e+07", 10000000.00);987test("%3.0e", "1e+08", 100000000.00);988989//---------------------------------------------------------------------990// %f991//992// Floating-point conversions applicable to float, double, and993// BigDecimal.994//---------------------------------------------------------------------995test("%f", "null", (Object)null);996test("%f", "3.141593", pi);997test(Locale.FRANCE, "%f", "3,141593", pi);998test("%10.3f", " 3.142", pi);999test("%10.3f", " -3.142", negate(pi));1000test("%010.3f", "000003.142", pi);1001test("%010.3f", "-00003.142", negate(pi));1002test("%-10.3f", "3.142 ", pi);1003test("%-10.3f", "-3.142 ", negate(pi));1004test("%.3f", "3.142", pi);1005test("%.3f", "-3.142", negate(pi));1006test("%+.3f", "+3.142", pi);1007test("%+.3f", "-3.142", negate(pi));1008test("% .3f", " 3.142", pi);1009test("% .3f", "-3.142", negate(pi));1010test("%#.0f", "3.", create(3.0));1011test("%#.0f", "-3.", create(-3.0));1012test("%.0f", "3", create(3.0));1013test("%.0f", "-3", create(-3.0));1014test("%.3f", "10.000", ten);1015test("%.3f", "1.000", one);1016test("%10.3f", " 1.000", one);10171018//---------------------------------------------------------------------1019// %f - boundary problems1020//---------------------------------------------------------------------1021test("%3.0f", " 0", 0.000001);1022test("%3.0f", " 0", 0.00001);1023test("%3.0f", " 0", 0.0001);1024test("%3.0f", " 0", 0.001);1025test("%3.0f", " 0", 0.01);1026test("%3.0f", " 0", 0.1);1027test("%3.0f", " 1", 0.9);1028test("%3.1f", "0.9", 0.9);1029test("%3.0f", " 1", 1.00);1030test("%3.0f", " 10", 10.00);1031test("%3.0f", " 99", 99.19);1032test("%3.1f", "99.2", 99.19);1033test("%3.0f", "100", 99.99);1034test("%3.0f", "100", 100.00);1035test("%#3.0f", "1000.", 1000.00);1036test("%3.0f", "10000", 10000.00);1037test("%3.0f", "100000", 100000.00);1038test("%3.0f", "1000000", 1000000.00);1039test("%3.0f", "10000000", 10000000.00);1040test("%3.0f", "100000000", 100000000.00);10411042//---------------------------------------------------------------------1043// %f - BigDecimal1044//---------------------------------------------------------------------1045test("%4.0f", " 99", new BigDecimal("99.19"));1046test("%4.1f", "99.2", new BigDecimal("99.19"));10471048BigDecimal val = new BigDecimal("99.95");1049test("%4.0f", " 100", val);1050test("%#4.0f", "100.", val);1051test("%4.1f", "100.0", val);1052test("%4.2f", "99.95", val);1053test("%4.3f", "99.950", val);10541055val = new BigDecimal(".99");1056test("%4.1f", " 1.0", val);1057test("%4.2f", "0.99", val);1058test("%4.3f", "0.990", val);10591060// #64764251061val = new BigDecimal("0.00001");1062test("%.0f", "0", val);1063test("%.1f", "0.0", val);1064test("%.2f", "0.00", val);1065test("%.3f", "0.000", val);1066test("%.4f", "0.0000", val);1067test("%.5f", "0.00001", val);10681069val = new BigDecimal("1.00001");1070test("%.0f", "1", val);1071test("%.1f", "1.0", val);1072test("%.2f", "1.00", val);1073test("%.3f", "1.000", val);1074test("%.4f", "1.0000", val);1075test("%.5f", "1.00001", val);10761077val = new BigDecimal("1.23456");1078test("%.0f", "1", val);1079test("%.1f", "1.2", val);1080test("%.2f", "1.23", val);1081test("%.3f", "1.235", val);1082test("%.4f", "1.2346", val);1083test("%.5f", "1.23456", val);1084test("%.6f", "1.234560", val);10851086val = new BigDecimal("9.99999");1087test("%.0f", "10", val);1088test("%.1f", "10.0", val);1089test("%.2f", "10.00", val);1090test("%.3f", "10.000", val);1091test("%.4f", "10.0000", val);1092test("%.5f", "9.99999", val);1093test("%.6f", "9.999990", val);109410951096val = new BigDecimal("1.99999");1097test("%.0f", "2", val);1098test("%.1f", "2.0", val);1099test("%.2f", "2.00", val);1100test("%.3f", "2.000", val);1101test("%.4f", "2.0000", val);1102test("%.5f", "1.99999", val);1103test("%.6f", "1.999990", val);11041105val = new BigDecimal(0.9996);1106test("%.0f", "1", val);1107test("%.1f", "1.0", val);1108test("%.2f", "1.00", val);1109test("%.3f", "1.000", val);1110test("%.4f", "0.9996", val);1111test("%.5f", "0.99960", val);1112test("%.6f", "0.999600", val);1113111411151116111711181119112011211122112311241125112611271128112911301131113211331134//---------------------------------------------------------------------1135// %f - float, double, Double, BigDecimal1136//---------------------------------------------------------------------1137test("%.3f", "3141592.654", mult(pi, 1000000.0));1138test("%.3f", "-3141592.654", mult(pi, -1000000.0));1139test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0));1140test(Locale.FRANCE, "%,.4f", "3\u00a0141\u00a0592,6536", mult(pi, 1000000.0));1141test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0));1142test("%(.4f", "3141592.6536", mult(pi, 1000000.0));1143test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0));1144test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0));1145test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0));11461147114811491150//---------------------------------------------------------------------1151// %g1152//1153// Floating-point conversions applicable to float, double, and1154// BigDecimal.1155//---------------------------------------------------------------------1156test("%g", "null", (Object)null);1157test("%g", "3.14159", pi);1158test(Locale.FRANCE, "%g", "3,14159", pi);1159test("%.0g", "1e+01", ten);1160test("%G", "3.14159", pi);1161test("%10.3g", " 3.14", pi);1162test("%10.3g", " -3.14", negate(pi));1163test("%010.3g", "0000003.14", pi);1164test("%010.3g", "-000003.14", negate(pi));1165test("%-12.3g", "3.14 ", pi);1166test("%-12.3g", "-3.14 ", negate(pi));1167test("%.3g", "3.14", pi);1168test("%.3g", "-3.14", negate(pi));1169test("%.3g", "3.14e+08", mult(pi, 100000000.0));1170test("%.3g", "-3.14e+08", mult(pi, -100000000.0));11711172test("%.3g", "1.00e-05", recip(create(100000.0)));1173test("%.3g", "-1.00e-05", recip(create(-100000.0)));1174test("%.0g", "-1e-05", recip(create(-100000.0)));1175test("%.0g", "1e+05", create(100000.0));1176test("%.3G", "1.00E-05", recip(create(100000.0)));1177test("%.3G", "-1.00E-05", recip(create(-100000.0)));11781179test("%.1g", "-0", -0.0);1180test("%3.0g", " -0", -0.0);1181test("%.1g", "0", 0.0);1182test("%3.0g", " 0", 0.0);1183test("%.1g", "0", +0.0);1184test("%3.0g", " 0", +0.0);11851186test("%3.0g", "1e-06", 0.000001);1187test("%3.0g", "1e-05", 0.00001);1188test("%3.0g", "1e-05", 0.0000099);1189test("%3.1g", "1e-05", 0.0000099);1190test("%3.2g", "9.9e-06", 0.0000099);1191test("%3.0g", "0.0001", 0.0001);1192test("%3.0g", "9e-05", 0.00009);1193test("%3.0g", "0.0001", 0.000099);1194test("%3.1g", "0.0001", 0.000099);1195test("%3.2g", "9.9e-05", 0.000099);1196test("%3.0g", "0.001", 0.001);1197test("%3.0g", "0.001", 0.00099);1198test("%3.1g", "0.001", 0.00099);1199test("%3.2g", "0.00099", 0.00099);1200test("%3.3g", "0.00100", 0.001);1201test("%3.4g", "0.001000", 0.001);1202test("%3.0g", "0.01", 0.01);1203test("%3.0g", "0.1", 0.1);1204test("%3.0g", "0.9", 0.9);1205test("%3.1g", "0.9", 0.9);1206test("%3.0g", " 1", 1.00);1207test("%3.2g", " 10", 10.00);1208test("%3.0g", "1e+01", 10.00);1209test("%3.0g", "1e+02", 99.19);1210test("%3.1g", "1e+02", 99.19);1211test("%3.2g", " 99", 99.19);1212test("%3.0g", "1e+02", 99.9);1213test("%3.1g", "1e+02", 99.9);1214test("%3.2g", "1.0e+02", 99.9);1215test("%3.0g", "1e+02", 99.99);1216test("%3.0g", "1e+02", 100.00);1217test("%3.0g", "1e+03", 999.9);1218test("%3.1g", "1e+03", 999.9);1219test("%3.2g", "1.0e+03", 999.9);1220test("%3.3g", "1.00e+03", 999.9);1221test("%3.4g", "999.9", 999.9);1222test("%3.4g", "1000", 999.99);1223test("%3.0g", "1e+03", 1000.00);1224test("%3.0g", "1e+04", 10000.00);1225test("%3.0g", "1e+05", 100000.00);1226test("%3.0g", "1e+06", 1000000.00);1227test("%3.0g", "1e+07", 10000000.00);1228test("%3.9g", "100000000", 100000000.00);1229test("%3.10g", "100000000.0", 100000000.00);12301231tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00);12321233// double PI^3001234// = 13962455701329742638131355433930076081862072808 ... e+14912351236//---------------------------------------------------------------------1237// %g - BigDecimal1238//---------------------------------------------------------------------1239test("%.3g", "1.40e+149", piToThe300);1240test("%.3g", "-1.40e+149", piToThe300.negate());1241test(Locale.FRANCE, "%.3g", "-1,40e+149", piToThe300.negate());1242test("%.3g", "1.00e-100", recip(ten.pow(100)));1243test("%.3g", "-1.00e-100", negate(recip(ten.pow(100))));12441245test("%3.0g", "1e-06", new BigDecimal("0.000001"));1246test("%3.0g", "1e-05", new BigDecimal("0.00001"));1247test("%3.0g", "0.0001", new BigDecimal("0.0001"));1248test("%3.0g", "0.001", new BigDecimal("0.001"));1249test("%3.3g", "0.00100", new BigDecimal("0.001"));1250test("%3.4g", "0.001000", new BigDecimal("0.001"));1251test("%3.0g", "0.01", new BigDecimal("0.01"));1252test("%3.0g", "0.1", new BigDecimal("0.1"));1253test("%3.0g", "0.9", new BigDecimal("0.9"));1254test("%3.1g", "0.9", new BigDecimal("0.9"));1255test("%3.0g", " 1", new BigDecimal("1.00"));1256test("%3.2g", " 10", new BigDecimal("10.00"));1257test("%3.0g", "1e+01", new BigDecimal("10.00"));1258test("%3.0g", "1e+02", new BigDecimal("99.19"));1259test("%3.1g", "1e+02", new BigDecimal("99.19"));1260test("%3.2g", " 99", new BigDecimal("99.19"));1261test("%3.0g", "1e+02", new BigDecimal("99.99"));1262test("%3.0g", "1e+02", new BigDecimal("100.00"));1263test("%3.0g", "1e+03", new BigDecimal("1000.00"));1264test("%3.0g", "1e+04", new BigDecimal("10000.00"));1265test("%3.0g", "1e+05", new BigDecimal("100000.00"));1266test("%3.0g", "1e+06", new BigDecimal("1000000.00"));1267test("%3.0g", "1e+07", new BigDecimal("10000000.00"));1268test("%3.9g", "100000000", new BigDecimal("100000000.00"));1269test("%3.10g", "100000000.0", new BigDecimal("100000000.00"));127012711272test("%.3g", "10.0", ten);1273test("%.3g", "1.00", one);1274test("%10.3g", " 1.00", one);1275test("%+10.3g", " +3.14", pi);1276test("%+10.3g", " -3.14", negate(pi));1277test("% .3g", " 3.14", pi);1278test("% .3g", "-3.14", negate(pi));1279test("%.0g", "3", create(3.0));1280test("%.0g", "-3", create(-3.0));12811282test("%(.4g", "3.142e+08", mult(pi, 100000000.0));1283test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0));12841285128612871288128912901291test("%,.11g", "3,141,592.6536", mult(pi, 1000000.0));1292test("%(,.11g", "(3,141,592.6536)", mult(pi, -1000000.0));1293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353//---------------------------------------------------------------------1354// %f, %e, %g, %a - Boundaries1355//---------------------------------------------------------------------13561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591//---------------------------------------------------------------------1592// %t1593//1594// Date/Time conversions applicable to Calendar, Date, and long.1595//---------------------------------------------------------------------1596test("%tA", "null", (Object)null);1597test("%TA", "NULL", (Object)null);15981599//---------------------------------------------------------------------1600// %t - errors1601//---------------------------------------------------------------------1602tryCatch("%t", UnknownFormatConversionException.class);1603tryCatch("%T", UnknownFormatConversionException.class);1604tryCatch("%tP", UnknownFormatConversionException.class);1605tryCatch("%TP", UnknownFormatConversionException.class);1606tryCatch("%.5tB", IllegalFormatPrecisionException.class);1607tryCatch("%#tB", FormatFlagsConversionMismatchException.class);1608tryCatch("%-tB", MissingFormatWidthException.class);160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704//---------------------------------------------------------------------1705// %n1706//---------------------------------------------------------------------1707test("%n", System.getProperty("line.separator"), (Object)null);1708test("%n", System.getProperty("line.separator"), "");17091710tryCatch("%,n", IllegalFormatFlagsException.class);1711tryCatch("%.n", UnknownFormatConversionException.class);1712tryCatch("%5.n", UnknownFormatConversionException.class);1713tryCatch("%5n", IllegalFormatWidthException.class);1714tryCatch("%.7n", IllegalFormatPrecisionException.class);1715tryCatch("%<n", IllegalFormatFlagsException.class);17161717//---------------------------------------------------------------------1718// %%1719//---------------------------------------------------------------------1720test("%%", "%", (Object)null);1721test("%%", "%", "");1722tryCatch("%%%", UnknownFormatConversionException.class);1723// perhaps an IllegalFormatArgumentIndexException should be defined?1724tryCatch("%<%", IllegalFormatFlagsException.class);1725}1726}172717281729