undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,211 @@
/*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.*;
import java.util.Formatter;
import java.util.Locale;
import java.util.UnknownFormatConversionException;
public class Basic {
private static int fail = 0;
private static int pass = 0;
private static Throwable first;
protected static void test(String fs, String exp, Object ... args) {
Formatter f = new Formatter(new StringBuilder(), Locale.US);
f.format(fs, args);
ck(fs, exp, f.toString());
f = new Formatter(new StringBuilder(), Locale.US);
f.format("foo " + fs + " bar", args);
ck(fs, "foo " + exp + " bar", f.toString());
}
protected static void test(Locale l, String fs, String exp, Object ... args)
{
Formatter f = new Formatter(new StringBuilder(), l);
f.format(fs, args);
ck(fs, exp, f.toString());
f = new Formatter(new StringBuilder(), l);
f.format("foo " + fs + " bar", args);
ck(fs, "foo " + exp + " bar", f.toString());
}
protected static void test(String fs, Object ... args) {
Formatter f = new Formatter(new StringBuilder(), Locale.US);
f.format(fs, args);
ck(fs, "fail", f.toString());
}
protected static void test(String fs) {
Formatter f = new Formatter(new StringBuilder(), Locale.US);
f.format(fs, "fail");
ck(fs, "fail", f.toString());
}
protected static void testSysOut(String fs, String exp, Object ... args) {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
PrintStream saveOut = System.out;
fos = new FileOutputStream("testSysOut");
System.setOut(new PrintStream(fos));
System.out.format(Locale.US, fs, args);
fos.close();
fis = new FileInputStream("testSysOut");
byte [] ba = new byte[exp.length()];
int len = fis.read(ba);
String got = new String(ba);
if (len != ba.length)
fail(fs, exp, got);
ck(fs, exp, got);
System.setOut(saveOut);
} catch (FileNotFoundException ex) {
fail(fs, ex.getClass());
} catch (IOException ex) {
fail(fs, ex.getClass());
} finally {
try {
if (fos != null)
fos.close();
if (fis != null)
fis.close();
} catch (IOException ex) {
fail(fs, ex.getClass());
}
}
}
protected static void tryCatch(String fs, Class<?> ex) {
boolean caught = false;
try {
test(fs);
} catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass()))
caught = true;
}
if (!caught)
fail(fs, ex);
else
pass();
}
protected static void tryCatch(String fs, Class<?> ex, Object ... args) {
boolean caught = false;
try {
test(fs, args);
} catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass()))
caught = true;
}
if (!caught)
fail(fs, ex);
else
pass();
}
private static void pass() {
pass++;
}
private static void fail(String fs, Class ex) {
String message = "'%s': %s not thrown".formatted(fs, ex.getName());
if (first == null) {
setFirst(message);
}
System.err.printf("FAILED: %s%n", message);
fail++;
}
private static void fail(String fs, String exp, String got) {
String message = "'%s': Expected '%s', got '%s'".formatted(fs, exp, got);
if (first == null) {
setFirst(message);
}
System.err.printf("FAILED: %s%n", message);
fail++;
}
private static void setFirst(String s) {
try {
throw new RuntimeException(s);
} catch (RuntimeException x) {
first = x;
}
}
private static void ck(String fs, String exp, String got) {
if (!exp.equals(got)) {
fail(fs, exp, got);
} else {
pass();
}
}
public static void main(String[] args) {
common();
BasicBoolean.test();
BasicBooleanObject.test();
BasicByte.test();
BasicByteObject.test();
BasicChar.test();
BasicCharObject.test();
BasicShort.test();
BasicShortObject.test();
BasicInt.test();
BasicIntObject.test();
BasicLong.test();
BasicLongObject.test();
BasicBigInteger.test();
BasicFloat.test();
BasicFloatObject.test();
BasicDouble.test();
BasicDoubleObject.test();
BasicBigDecimal.test();
BasicDateTime.test();
if (fail != 0) {
var tests_message = "%d tests: %d failure(s)%n".formatted(fail + pass, fail);
var trace_message = "Traceback of the first error located";
var message = "%s %s".formatted(tests_message, trace_message);
throw new RuntimeException(message, first);
} else {
System.out.printf("All %d tests passed", pass);
}
}
private static void common() {
// non-conversion
tryCatch("%12", UnknownFormatConversionException.class);
tryCatch("% ", UnknownFormatConversionException.class);
tryCatch("%,", UnknownFormatConversionException.class);
tryCatch("%03.2", UnknownFormatConversionException.class);
}
}

View file

@ -0,0 +1,733 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicBigDecimal extends Basic {
private static BigDecimal create(double v) {
return new BigDecimal(v);
}
private static BigDecimal negate(BigDecimal v) {
return v.negate();
}
private static BigDecimal mult(BigDecimal v, double mul) {
return v.multiply(new BigDecimal(mul));
}
private static BigDecimal recip(BigDecimal v) {
return BigDecimal.ONE.divide(v);
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %s - BigDecimal
//---------------------------------------------------------------------
BigDecimal one = BigDecimal.ONE;
BigDecimal ten = BigDecimal.TEN;
BigDecimal pi = new BigDecimal(Math.PI);
BigDecimal piToThe300 = pi.pow(300);
test("%s", "3.141592653589793115997963468544185161590576171875", pi);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%d", IllegalFormatConversionException.class, one);
tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one);
//---------------------------------------------------------------------
// %e
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%e", "null", (Object)null);
//---------------------------------------------------------------------
// %e - float and double
//---------------------------------------------------------------------
// double PI = 3.141 592 653 589 793 238 46;
test("%e", "3.141593e+00", pi);
test("%.0e", "1e+01", ten);
test("%#.0e", "1.e+01", ten);
test("%E", "3.141593E+00", pi);
test("%10.3e", " 3.142e+00", pi);
test("%10.3e", "-3.142e+00", negate(pi));
test("%010.3e", "03.142e+00", pi);
test("%010.3e", "-3.142e+00", negate(pi));
test("%-12.3e", "3.142e+00 ", pi);
test("%-12.3e", "-3.142e+00 ", negate(pi));
test("%.3e", "3.142e+00", pi);
test("%.3e", "-3.142e+00", negate(pi));
test("%.3e", "3.142e+06", mult(pi, 1000000.0));
test("%.3e", "-3.142e+06", mult(pi, -1000000.0));
test(Locale.FRANCE, "%e", "3,141593e+00", pi);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
//---------------------------------------------------------------------
// %e - BigDecimal
//---------------------------------------------------------------------
test("%.3e", "1.396e+149", piToThe300);
test("%.3e", "-1.396e+149", piToThe300.negate());
test("%.3e", "1.000e-100", recip(ten.pow(100)));
test("%.3e", "-1.000e-100", negate(recip(ten.pow(100))));
test("%3.0e", "1e-06", new BigDecimal("0.000001"));
test("%3.0e", "1e-05", new BigDecimal("0.00001"));
test("%3.0e", "1e-04", new BigDecimal("0.0001"));
test("%3.0e", "1e-03", new BigDecimal("0.001"));
test("%3.0e", "1e-02", new BigDecimal("0.01"));
test("%3.0e", "1e-01", new BigDecimal("0.1"));
test("%3.0e", "9e-01", new BigDecimal("0.9"));
test("%3.1e", "9.0e-01", new BigDecimal("0.9"));
test("%3.0e", "1e+00", new BigDecimal("1.00"));
test("%3.0e", "1e+01", new BigDecimal("10.00"));
test("%3.0e", "1e+02", new BigDecimal("99.19"));
test("%3.1e", "9.9e+01", new BigDecimal("99.19"));
test("%3.0e", "1e+02", new BigDecimal("99.99"));
test("%3.0e", "1e+02", new BigDecimal("100.00"));
test("%#3.0e", "1.e+03", new BigDecimal("1000.00"));
test("%3.0e", "1e+04", new BigDecimal("10000.00"));
test("%3.0e", "1e+05", new BigDecimal("100000.00"));
test("%3.0e", "1e+06", new BigDecimal("1000000.00"));
test("%3.0e", "1e+07", new BigDecimal("10000000.00"));
test("%3.0e", "1e+08", new BigDecimal("100000000.00"));
test("%10.3e", " 1.000e+00", one);
test("%+.3e", "+3.142e+00", pi);
test("%+.3e", "-3.142e+00", negate(pi));
test("% .3e", " 3.142e+00", pi);
test("% .3e", "-3.142e+00", negate(pi));
test("%#.0e", "3.e+00", create(3.0));
test("%#.0e", "-3.e+00", create(-3.0));
test("%.0e", "3e+00", create(3.0));
test("%.0e", "-3e+00", create(-3.0));
test("%(.4e", "3.1416e+06", mult(pi, 1000000.0));
test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %e - boundary problems
//---------------------------------------------------------------------
test("%3.0e", "1e-06", 0.000001);
test("%3.0e", "1e-05", 0.00001);
test("%3.0e", "1e-04", 0.0001);
test("%3.0e", "1e-03", 0.001);
test("%3.0e", "1e-02", 0.01);
test("%3.0e", "1e-01", 0.1);
test("%3.0e", "9e-01", 0.9);
test("%3.1e", "9.0e-01", 0.9);
test("%3.0e", "1e+00", 1.00);
test("%3.0e", "1e+01", 10.00);
test("%3.0e", "1e+02", 99.19);
test("%3.1e", "9.9e+01", 99.19);
test("%3.0e", "1e+02", 99.99);
test("%3.0e", "1e+02", 100.00);
test("%#3.0e", "1.e+03", 1000.00);
test("%3.0e", "1e+04", 10000.00);
test("%3.0e", "1e+05", 100000.00);
test("%3.0e", "1e+06", 1000000.00);
test("%3.0e", "1e+07", 10000000.00);
test("%3.0e", "1e+08", 100000000.00);
//---------------------------------------------------------------------
// %e - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.1e", "9.9e-324", 1e-323);
test("%.1e", "9.9e-323", 1e-322);
test("%.15e", "7.387900000000000e+20", 7.3879e20);
test("%.15e", "1.000000000000000e+23", 1e23);
test("%.16e", "2.0000000000000000e+23", 2e23);
test("%.16e", "1.9400994884341945e+25", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%f", "null", (Object)null);
test("%f", "3.141593", pi);
test(Locale.FRANCE, "%f", "3,141593", pi);
test("%10.3f", " 3.142", pi);
test("%10.3f", " -3.142", negate(pi));
test("%010.3f", "000003.142", pi);
test("%010.3f", "-00003.142", negate(pi));
test("%-10.3f", "3.142 ", pi);
test("%-10.3f", "-3.142 ", negate(pi));
test("%.3f", "3.142", pi);
test("%.3f", "-3.142", negate(pi));
test("%+.3f", "+3.142", pi);
test("%+.3f", "-3.142", negate(pi));
test("% .3f", " 3.142", pi);
test("% .3f", "-3.142", negate(pi));
test("%#.0f", "3.", create(3.0));
test("%#.0f", "-3.", create(-3.0));
test("%.0f", "3", create(3.0));
test("%.0f", "-3", create(-3.0));
test("%.3f", "10.000", ten);
test("%.3f", "1.000", one);
test("%10.3f", " 1.000", one);
//---------------------------------------------------------------------
// %f - boundary problems
//---------------------------------------------------------------------
test("%3.0f", " 0", 0.000001);
test("%3.0f", " 0", 0.00001);
test("%3.0f", " 0", 0.0001);
test("%3.0f", " 0", 0.001);
test("%3.0f", " 0", 0.01);
test("%3.0f", " 0", 0.1);
test("%3.0f", " 1", 0.9);
test("%3.1f", "0.9", 0.9);
test("%3.0f", " 1", 1.00);
test("%3.0f", " 10", 10.00);
test("%3.0f", " 99", 99.19);
test("%3.1f", "99.2", 99.19);
test("%3.0f", "100", 99.99);
test("%3.0f", "100", 100.00);
test("%#3.0f", "1000.", 1000.00);
test("%3.0f", "10000", 10000.00);
test("%3.0f", "100000", 100000.00);
test("%3.0f", "1000000", 1000000.00);
test("%3.0f", "10000000", 10000000.00);
test("%3.0f", "100000000", 100000000.00);
test("%10.0f", " 1000000", 1000000.00);
test("%,10.0f", " 1,000,000", 1000000.00);
test("%,10.1f", "1,000,000.0", 1000000.00);
test("%,3.0f", "1,000,000", 1000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
//---------------------------------------------------------------------
// %f - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.0f", "738790000000000000000", 7.3879e20);
test("%.0f", "100000000000000000000000", 1e23);
test("%.0f", "200000000000000000000000", 2e23);
test("%.0f", "19400994884341945000000000", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f - BigDecimal
//---------------------------------------------------------------------
test("%4.0f", " 99", new BigDecimal("99.19"));
test("%4.1f", "99.2", new BigDecimal("99.19"));
BigDecimal val = new BigDecimal("99.95");
test("%4.0f", " 100", val);
test("%#4.0f", "100.", val);
test("%4.1f", "100.0", val);
test("%4.2f", "99.95", val);
test("%4.3f", "99.950", val);
val = new BigDecimal(".99");
test("%4.1f", " 1.0", val);
test("%4.2f", "0.99", val);
test("%4.3f", "0.990", val);
// #6476425
val = new BigDecimal("0.00001");
test("%.0f", "0", val);
test("%.1f", "0.0", val);
test("%.2f", "0.00", val);
test("%.3f", "0.000", val);
test("%.4f", "0.0000", val);
test("%.5f", "0.00001", val);
val = new BigDecimal("1.00001");
test("%.0f", "1", val);
test("%.1f", "1.0", val);
test("%.2f", "1.00", val);
test("%.3f", "1.000", val);
test("%.4f", "1.0000", val);
test("%.5f", "1.00001", val);
val = new BigDecimal("1.23456");
test("%.0f", "1", val);
test("%.1f", "1.2", val);
test("%.2f", "1.23", val);
test("%.3f", "1.235", val);
test("%.4f", "1.2346", val);
test("%.5f", "1.23456", val);
test("%.6f", "1.234560", val);
val = new BigDecimal("9.99999");
test("%.0f", "10", val);
test("%.1f", "10.0", val);
test("%.2f", "10.00", val);
test("%.3f", "10.000", val);
test("%.4f", "10.0000", val);
test("%.5f", "9.99999", val);
test("%.6f", "9.999990", val);
val = new BigDecimal("1.99999");
test("%.0f", "2", val);
test("%.1f", "2.0", val);
test("%.2f", "2.00", val);
test("%.3f", "2.000", val);
test("%.4f", "2.0000", val);
test("%.5f", "1.99999", val);
test("%.6f", "1.999990", val);
val = new BigDecimal(0.9996);
test("%.0f", "1", val);
test("%.1f", "1.0", val);
test("%.2f", "1.00", val);
test("%.3f", "1.000", val);
test("%.4f", "0.9996", val);
test("%.5f", "0.99960", val);
test("%.6f", "0.999600", val);
val = new BigDecimal(BigInteger.ZERO, 6);
test("%.4f", "0.0000", val);
val = new BigDecimal(BigInteger.ZERO, -6);
test("%.4f", "0.0000", val);
//---------------------------------------------------------------------
// %f - float, double, Double, BigDecimal
//---------------------------------------------------------------------
test("%.3f", "3141592.654", mult(pi, 1000000.0));
test("%.3f", "-3141592.654", mult(pi, -1000000.0));
test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0));
test(Locale.FRANCE, "%,.4f", "3\u202f141\u202f592,6536", mult(pi, 1000000.0));
test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0));
test("%(.4f", "3141592.6536", mult(pi, 1000000.0));
test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0));
test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0));
test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %g
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%g", "null", (Object)null);
test("%g", "3.14159", pi);
test(Locale.FRANCE, "%g", "3,14159", pi);
test("%.0g", "1e+01", ten);
test("%G", "3.14159", pi);
test("%10.3g", " 3.14", pi);
test("%10.3g", " -3.14", negate(pi));
test("%010.3g", "0000003.14", pi);
test("%010.3g", "-000003.14", negate(pi));
test("%-12.3g", "3.14 ", pi);
test("%-12.3g", "-3.14 ", negate(pi));
test("%.3g", "3.14", pi);
test("%.3g", "-3.14", negate(pi));
test("%.3g", "3.14e+08", mult(pi, 100000000.0));
test("%.3g", "-3.14e+08", mult(pi, -100000000.0));
test("%.3g", "1.00e-05", recip(create(100000.0)));
test("%.3g", "-1.00e-05", recip(create(-100000.0)));
test("%.0g", "-1e-05", recip(create(-100000.0)));
test("%.0g", "1e+05", create(100000.0));
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);
test("%3.1g", "1e-05", 0.0000099);
test("%3.2g", "9.9e-06", 0.0000099);
test("%3.0g", "0.0001", 0.0001);
test("%3.0g", "9e-05", 0.00009);
test("%3.0g", "0.0001", 0.000099);
test("%3.1g", "0.0001", 0.000099);
test("%3.2g", "9.9e-05", 0.000099);
test("%3.0g", "0.001", 0.001);
test("%3.0g", "0.001", 0.00099);
test("%3.1g", "0.001", 0.00099);
test("%3.2g", "0.00099", 0.00099);
test("%3.3g", "0.00100", 0.001);
test("%3.4g", "0.001000", 0.001);
test("%3.0g", "0.01", 0.01);
test("%3.0g", "0.1", 0.1);
test("%3.0g", "0.9", 0.9);
test("%3.1g", "0.9", 0.9);
test("%3.0g", " 1", 1.00);
test("%3.2g", " 10", 10.00);
test("%3.0g", "1e+01", 10.00);
test("%3.0g", "1e+02", 99.19);
test("%3.1g", "1e+02", 99.19);
test("%3.2g", " 99", 99.19);
test("%3.0g", "1e+02", 99.9);
test("%3.1g", "1e+02", 99.9);
test("%3.2g", "1.0e+02", 99.9);
test("%3.0g", "1e+02", 99.99);
test("%3.0g", "1e+02", 100.00);
test("%3.0g", "1e+03", 999.9);
test("%3.1g", "1e+03", 999.9);
test("%3.2g", "1.0e+03", 999.9);
test("%3.3g", "1.00e+03", 999.9);
test("%3.4g", "999.9", 999.9);
test("%3.4g", "1000", 999.99);
test("%3.0g", "1e+03", 1000.00);
test("%3.0g", "1e+04", 10000.00);
test("%3.0g", "1e+05", 100000.00);
test("%3.0g", "1e+06", 1000000.00);
test("%3.0g", "1e+07", 10000000.00);
test("%3.9g", "100000000", 100000000.00);
test("%3.10g", "100000000.0", 100000000.00);
tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00);
//---------------------------------------------------------------------
// %g - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.2g", "9.9e-324", 1e-323);
test("%.2g", "9.9e-323", 1e-322);
test("%.16g", "7.387900000000000e+20", 7.3879e20);
test("%.16g", "1.000000000000000e+23", 1e23);
test("%.17g", "2.0000000000000000e+23", 2e23);
test("%.17g", "1.9400994884341945e+25", 1.9400994884341945E25);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
//---------------------------------------------------------------------
// %g - BigDecimal
//---------------------------------------------------------------------
test("%.3g", "1.40e+149", piToThe300);
test("%.3g", "-1.40e+149", piToThe300.negate());
test(Locale.FRANCE, "%.3g", "-1,40e+149", piToThe300.negate());
test("%.3g", "1.00e-100", recip(ten.pow(100)));
test("%.3g", "-1.00e-100", negate(recip(ten.pow(100))));
test("%3.0g", "1e-06", new BigDecimal("0.000001"));
test("%3.0g", "1e-05", new BigDecimal("0.00001"));
test("%3.0g", "0.0001", new BigDecimal("0.0001"));
test("%3.0g", "0.001", new BigDecimal("0.001"));
test("%3.3g", "0.00100", new BigDecimal("0.001"));
test("%3.4g", "0.001000", new BigDecimal("0.001"));
test("%3.0g", "0.01", new BigDecimal("0.01"));
test("%3.0g", "0.1", new BigDecimal("0.1"));
test("%3.0g", "0.9", new BigDecimal("0.9"));
test("%3.1g", "0.9", new BigDecimal("0.9"));
test("%3.0g", " 1", new BigDecimal("1.00"));
test("%3.2g", " 10", new BigDecimal("10.00"));
test("%3.0g", "1e+01", new BigDecimal("10.00"));
test("%3.0g", "1e+02", new BigDecimal("99.19"));
test("%3.1g", "1e+02", new BigDecimal("99.19"));
test("%3.2g", " 99", new BigDecimal("99.19"));
test("%3.0g", "1e+02", new BigDecimal("99.99"));
test("%3.0g", "1e+02", new BigDecimal("100.00"));
test("%3.0g", "1e+03", new BigDecimal("1000.00"));
test("%3.0g", "1e+04", new BigDecimal("10000.00"));
test("%3.0g", "1e+05", new BigDecimal("100000.00"));
test("%3.0g", "1e+06", new BigDecimal("1000000.00"));
test("%3.0g", "1e+07", new BigDecimal("10000000.00"));
test("%3.9g", "100000000", new BigDecimal("100000000.00"));
test("%3.10g", "100000000.0", new BigDecimal("100000000.00"));
test("%.3g", "10.0", ten);
test("%.3g", "1.00", one);
test("%10.3g", " 1.00", one);
test("%+10.3g", " +3.14", pi);
test("%+10.3g", " -3.14", negate(pi));
test("% .3g", " 3.14", pi);
test("% .3g", "-3.14", negate(pi));
test("%.0g", "3", create(3.0));
test("%.0g", "-3", create(-3.0));
test("%(.4g", "3.142e+08", mult(pi, 100000000.0));
test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0));
test("%,.11g", "3,141,592.6536", mult(pi, 1000000.0));
test("%(,.11g", "(3,141,592.6536)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %f, %e, %g, %a - Boundaries
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,365 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicBigInteger extends Basic {
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// BigInteger - errors
//---------------------------------------------------------------------
tryCatch("%f", IllegalFormatConversionException.class,
new BigInteger("1"));
//---------------------------------------------------------------------
// %d - BigInteger
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
test("%d", "1234567", new BigInteger("1234567", 10));
test("%,d", "1,234,567", new BigInteger("1234567", 10));
test(Locale.FRANCE, "%,d", "1\u202f234\u202f567", new BigInteger("1234567", 10));
test("%,d", "-1,234,567", new BigInteger("-1234567", 10));
test("%(d", "1234567", new BigInteger("1234567", 10));
test("%(d", "(1234567)", new BigInteger("-1234567", 10));
test("% d", " 1234567", new BigInteger("1234567", 10));
test("% d", "-1234567", new BigInteger("-1234567", 10));
test("%+d", "+1234567", new BigInteger("1234567", 10));
test("%+d", "-1234567", new BigInteger("-1234567", 10));
test("%010d", "0001234567", new BigInteger("1234567", 10));
test("%010d", "-001234567", new BigInteger("-1234567", 10));
test("%(10d", " (1234567)", new BigInteger("-1234567", 10));
test("%+d", "+1234567", new BigInteger("1234567", 10));
test("%+d", "-1234567", new BigInteger("-1234567", 10));
test("%-10d", "1234567 ", new BigInteger("1234567", 10));
test("%-10d", "-1234567 ", new BigInteger("-1234567", 10));
// , variations:
test("%0,10d", "01,234,567", new BigInteger("1234567", 10));
test("%0,10d", "-1,234,567", new BigInteger("-1234567", 10));
test("%(,10d", "(1,234,567)", new BigInteger("-1234567", 10));
test("%+,d", "+1,234,567", new BigInteger("1234567", 10));
test("%+,d", "-1,234,567", new BigInteger("-1234567", 10));
test("%-,10d", "1,234,567 ", new BigInteger("1234567", 10));
test("%-,10d", "-1,234,567", new BigInteger("-1234567", 10));
//---------------------------------------------------------------------
// %o - BigInteger
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
test("%o", "1234567", new BigInteger("1234567", 8));
test("%(o", "1234567", new BigInteger("1234567", 8));
test("%(o", "(1234567)", new BigInteger("-1234567", 8));
test("% o", " 1234567", new BigInteger("1234567", 8));
test("% o", "-1234567", new BigInteger("-1234567", 8));
test("%+o", "+1234567", new BigInteger("1234567", 8));
test("%+o", "-1234567", new BigInteger("-1234567", 8));
test("%010o", "0001234567", new BigInteger("1234567", 8));
test("%010o", "-001234567", new BigInteger("-1234567", 8));
test("%(10o", " (1234567)", new BigInteger("-1234567", 8));
test("%+o", "+1234567", new BigInteger("1234567", 8));
test("%+o", "-1234567", new BigInteger("-1234567", 8));
test("%-10o", "1234567 ", new BigInteger("1234567", 8));
test("%-10o", "-1234567 ", new BigInteger("-1234567", 8));
test("%#10o", " 01234567", new BigInteger("1234567", 8));
test("%#10o", " -01234567", new BigInteger("-1234567", 8));
//---------------------------------------------------------------------
// %x - BigInteger
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
test("%x", "1234567", new BigInteger("1234567", 16));
test("%(x", "1234567", new BigInteger("1234567", 16));
test("%(x", "(1234567)", new BigInteger("-1234567", 16));
test("% x", " 1234567", new BigInteger("1234567", 16));
test("% x", "-1234567", new BigInteger("-1234567", 16));
test("%+x", "+1234567", new BigInteger("1234567", 16));
test("%+x", "-1234567", new BigInteger("-1234567", 16));
test("%010x", "0001234567", new BigInteger("1234567", 16));
test("%010x", "-001234567", new BigInteger("-1234567", 16));
test("%(10x", " (1234567)", new BigInteger("-1234567", 16));
test("%+x", "+1234567", new BigInteger("1234567", 16));
test("%+x", "-1234567", new BigInteger("-1234567", 16));
test("%-10x", "1234567 ", new BigInteger("1234567", 16));
test("%-10x", "-1234567 ", new BigInteger("-1234567", 16));
test("%#10x", " 0x1234567", new BigInteger("1234567", 16));
test("%#10x", "-0x1234567", new BigInteger("-1234567", 16));
test("%#10X", " 0X1234567", new BigInteger("1234567", 16));
test("%#10X", "-0X1234567", new BigInteger("-1234567", 16));
test("%X", "1234567A", new BigInteger("1234567a", 16));
test("%X", "-1234567A", new BigInteger("-1234567a", 16));
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,284 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicBoolean extends Basic {
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,284 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicBooleanObject extends Basic {
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,377 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicByte extends Basic {
private static byte negate(byte v) {
return (byte) -v;
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
byte minByte = Byte.MIN_VALUE; // -128
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - byte
//---------------------------------------------------------------------
byte seventeen = (byte) 17;
test("%d", "17", seventeen);
test("%,d", "17", seventeen);
test("%,d", "-17", negate(seventeen));
test("%(d", "17", seventeen);
test("%(d", "(17)", negate(seventeen));
test("% d", " 17", seventeen);
test("% d", "-17", negate(seventeen));
test("%+d", "+17", seventeen);
test("%+d", "-17", negate(seventeen));
test("%010d", "0000000017", seventeen);
test("%010d", "-000000017", negate(seventeen));
test("%(10d", " (17)", negate(seventeen));
test("%-10d", "17 ", seventeen);
test("%-10d", "-17 ", negate(seventeen));
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - byte
//---------------------------------------------------------------------
test("%010o", "0000000200", minByte);
test("%-10o", "200 ", minByte);
test("%#10o", " 0200", minByte);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - byte
//---------------------------------------------------------------------
test("%010x", "0000000080", minByte);
test("%-10x", "80 ", minByte);
test("%#10x", " 0x80", minByte);
test("%0#10x","0x00000080", minByte);
test("%#10X", " 0X80", minByte);
test("%X", "80", minByte);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,344 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicByteObject extends Basic {
private static Byte negate(Byte v) {
return (byte) -v.byteValue();
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
Byte minByte = Byte.MIN_VALUE;
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,284 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicChar extends Basic {
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,284 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicCharObject extends Basic {
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,516 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.text.DecimalFormatSymbols;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;
import java.time.chrono.*;
import java.time.temporal.ChronoField;
import java.util.*;
import static java.util.Calendar.*;
import static java.util.SimpleTimeZone.*;
import java.util.regex.Pattern;
public class BasicDateTime extends Basic {
private static void testDateTime(String fs, String exp, Calendar c) {
testDateTime(fs, exp, c, true);
}
private static void testDateTime(String fs, String exp, Calendar c, boolean upper) {
//---------------------------------------------------------------------
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
// Calendar
test(fs, exp, c);
test((Locale)null, fs, exp, c);
test(Locale.US, fs, exp, c);
// Date/long do not have timezone information so they will always use
// the default timezone.
String nexp = (fs.equals("%tZ") || fs.equals("%TZ")
|| fs.equals("%tc") || fs.equals("%Tc")
? exp.replace("PST", "GMT-08:00")
: exp);
// Date (implemented via conversion to Calendar)
Date d = c.getTime();
test(fs, nexp, d);
test((Locale)null, fs, nexp, d);
test(Locale.US, fs, nexp, d);
// long (implemented via conversion to Calendar)
long l = c.getTimeInMillis();
test(fs, nexp, l);
test((Locale)null, fs, nexp, l);
test(Locale.US, fs, nexp, l);
if (upper)
// repeat all tests for upper case variant (%T)
testDateTime(Pattern.compile("t").matcher(fs).replaceFirst("T"),
exp.toUpperCase(), c, false);
}
private static void testHours() {
for (int i = 0; i < 24; i++) {
// GregorianCalendar(int year, int month, int dayOfMonth,
// int hourOfDay, int minute, int second);
Calendar c = new GregorianCalendar(1995, MAY, 23, i, 48, 34);
//-----------------------------------------------------------------
// DateTime.HOUR_OF_DAY - 'k' (0 - 23) -- like H
//-----------------------------------------------------------------
String exp = Integer.toString(i);
testDateTime("%tk", exp, c);
//-----------------------------------------------------------------
// DateTime.HOUR - 'l' (1 - 12) -- like I
//-----------------------------------------------------------------
int v = i % 12;
v = (v == 0 ? 12 : v);
String exp2 = Integer.toString(v);
testDateTime("%tl", exp2, c);
//-----------------------------------------------------------------
// DateTime.HOUR_OF_DAY_0 - 'H' (00 - 23) [zero padded]
//-----------------------------------------------------------------
if (exp.length() < 2) exp = "0" + exp;
testDateTime("%tH", exp, c);
//-----------------------------------------------------------------
// DateTime.HOUR_0 - 'I' (01 - 12)
//-----------------------------------------------------------------
if (exp2.length() < 2) exp2 = "0" + exp2;
testDateTime("%tI", exp2, c);
//-----------------------------------------------------------------
// DateTime.AM_PM - (am or pm)
//-----------------------------------------------------------------
testDateTime("%tp", (i <12 ? "am" : "pm"), c);
}
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t - create test Calendar
//---------------------------------------------------------------------
// Get the supported ids for GMT-08:00 (Pacific Standard Time)
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
// Create a Pacific Standard Time time zone
SimpleTimeZone tz = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
// public GregorianCalendar(TimeZone zone, Locale aLocale);
Calendar c0 = new GregorianCalendar(tz, Locale.US);
// public final void set(int year, int month, int date,
// int hourOfDay, int minute, int second);
c0.set(1995, MAY, 23, 19, 48, 34);
c0.set(Calendar.MILLISECOND, 584);
//---------------------------------------------------------------------
// %t - Minutes, {nano,milli}*seconds
//
// testDateTime() verifies the expected output for all applicable types
// (Calendar, Date, and long). It also verifies output for "%t" and
// "%T". Thus it is sufficient to invoke that method once per
// conversion/expected output.
//---------------------------------------------------------------------
testDateTime("%tM", "48", c0);
testDateTime("%tN", "584000000", c0);
testDateTime("%tL", "584", c0);
// testDateTime("%tQ", "801283714584", c0);
testDateTime("%ts", String.valueOf(c0.getTimeInMillis() / 1000), c0);
testDateTime("%tS", "34", c0);
testDateTime("%tT", "19:48:34", c0);
//---------------------------------------------------------------------
// %t - Hours, morning/afternoon markers
//
// testHours() iterates through all twenty-four hours to verify
// numeric return value and morning/afternoon markers.
//---------------------------------------------------------------------
testHours();
//---------------------------------------------------------------------
// %t - Portions of date [ day, month, dates, weeks ]
//---------------------------------------------------------------------
testDateTime("%ta", "Tue", c0);
testDateTime("%tA", "Tuesday", c0);
testDateTime("%tb", "May", c0);
testDateTime("%tB", "May", c0);
testDateTime("%tC", "19", c0);
testDateTime("%td", "23", c0);
testDateTime("%te", "23", c0);
testDateTime("%th", "May", c0);
testDateTime("%tj", "143", c0);
testDateTime("%tm", "05", c0);
testDateTime("%ty", "95", c0);
testDateTime("%tY", "1995", c0);
//---------------------------------------------------------------------
// %t - TimeZone
//---------------------------------------------------------------------
testDateTime("%tz", "-0800", c0);
testDateTime("%tZ", "PST", c0);
//---------------------------------------------------------------------
// %tz should always adjust for DST
//---------------------------------------------------------------------
TimeZone dtz = TimeZone.getDefault();
// Artificial TimeZone based on PST with 3:15 DST always in effect
TimeZone atz = new SimpleTimeZone(-8 * 60 * 60 * 1000, "AlwaysDST",
JANUARY, 1, 0, 0, STANDARD_TIME,
// 24hrs - 1m = 60 * 60 * 1000 * 24 - 1
DECEMBER, 31, 0, 60 * 60 * 1000 * 24 - 1, STANDARD_TIME,
(int)(60 * 60 * 1000 * 3.25));
TimeZone.setDefault(atz);
testDateTime("%tz", "-0445", Calendar.getInstance(atz));
// Restore the TimeZone and verify
TimeZone.setDefault(dtz);
if (atz.hasSameRules(TimeZone.getDefault()))
throw new RuntimeException("Default TimeZone not restored");
//---------------------------------------------------------------------
// %t - Composites
//---------------------------------------------------------------------
testDateTime("%tr", "07:48:34 PM", c0);
testDateTime("%tR", "19:48", c0);
testDateTime("%tc", "Tue May 23 19:48:34 PST 1995", c0);
testDateTime("%tD", "05/23/95", c0);
testDateTime("%tF", "1995-05-23", c0);
testDateTime("%-12tF", "1995-05-23 ", c0);
testDateTime("%12tF", " 1995-05-23", c0);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
// %tF LocalDate
test("%tF", "2023-01-13", LocalDate.of(2023, 1, 13));
test("%tF", "2023-10-03", LocalDate.of(2023, 10, 3));
test("%tF", "0001-10-03", LocalDate.of(1, 10, 3));
test("%tF", "0012-10-03", LocalDate.of(12, 10, 3));
test("%tF", "0123-10-03", LocalDate.of(123, 10, 3));
test("%tF", "+12345-10-03", LocalDate.of(12345, 10, 3));
test("%tF", "+12345-10-03", LocalDateTime.of(12345, 10, 3, 0, 0, 0));
test("%tF", "+12345-10-03", OffsetDateTime.of(LocalDateTime.of(12345, 10, 3, 0, 0, 0), ZoneOffset.UTC));
test("%tF", "+12345-10-03", ZonedDateTime.of(LocalDateTime.of(12345, 10, 3, 0, 0, 0), ZoneOffset.UTC));
test("%tF", "-0001-10-03", LocalDate.of(-1, 10, 3));
test("%tF", "-0012-10-03", LocalDate.of(-12, 10, 3));
test("%tF", "-0123-10-03", LocalDate.of(-123, 10, 3));
test("%tF", "-1234-10-03", LocalDate.of(-1234, 10, 3));
test("%tF", "-12345-10-03", LocalDate.of(-12345, 10, 3));
test("%tF", "-12345-10-03", LocalDate.of(-12345, 10, 3));
test("%tF", "-12345-10-03", LocalDateTime.of(-12345, 10, 3, 0, 0, 0));
test("%tF", "-12345-10-03", OffsetDateTime.of(LocalDateTime.of(-12345, 10, 3, 0, 0, 0), ZoneOffset.UTC));
test("%tF", "-12345-10-03", ZonedDateTime.of(LocalDateTime.of(-12345, 10, 3, 0, 0, 0), ZoneOffset.UTC));
// check minusSign
int year = 2023, month = 1, dayOfMonth = 13;
String specifier = "%tF";
for (Locale locale : Locale.getAvailableLocales()) {
char minusSign = DecimalFormatSymbols.getInstance(locale).getMinusSign();
String str = new Formatter(new StringBuilder(), locale)
.format(specifier, LocalDate.of(year, month, dayOfMonth))
.toString();
test(locale, specifier, minusSign + str, LocalDate.of(-year, month, dayOfMonth));
}
// ja-JP-u-ca-japanese
ChronoLocalDate jpDate = Chronology
.ofLocale(Locale.forLanguageTag("ja-JP-u-ca-japanese"))
.dateNow();
test(Locale.JAPANESE,
"%tF",
String.format(
"%04d-%02d-%02d",
jpDate.get(ChronoField.YEAR_OF_ERA),
jpDate.get(ChronoField.MONTH_OF_YEAR),
jpDate.get(ChronoField.DAY_OF_MONTH)),
jpDate);
ChronoLocalDate jpDate1 = JapaneseChronology.INSTANCE.dateNow();
test(Locale.JAPANESE,
"%tF",
String.format(
"%04d-%02d-%02d",
jpDate1.get(ChronoField.YEAR_OF_ERA),
jpDate1.get(ChronoField.MONTH_OF_YEAR),
jpDate1.get(ChronoField.DAY_OF_MONTH)),
jpDate1);
}
}

View file

@ -0,0 +1,740 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicDouble extends Basic {
private static double create(double v) {
return (double) v;
}
private static double negate(double v) {
return -v;
}
private static double mult(double v, double mul) {
return v * mul;
}
private static double recip(double v) {
return 1.0 / v;
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %s - double
//---------------------------------------------------------------------
double one = 1.0;
double ten = 10.0;
double pi = Math.PI;
test("%s", "3.141592653589793", pi);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%d", IllegalFormatConversionException.class, one);
tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one);
//---------------------------------------------------------------------
// %e
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%e", "null", (Object)null);
//---------------------------------------------------------------------
// %e - float and double
//---------------------------------------------------------------------
// double PI = 3.141 592 653 589 793 238 46;
test("%e", "3.141593e+00", pi);
test("%.0e", "1e+01", ten);
test("%#.0e", "1.e+01", ten);
test("%E", "3.141593E+00", pi);
test("%10.3e", " 3.142e+00", pi);
test("%10.3e", "-3.142e+00", negate(pi));
test("%010.3e", "03.142e+00", pi);
test("%010.3e", "-3.142e+00", negate(pi));
test("%-12.3e", "3.142e+00 ", pi);
test("%-12.3e", "-3.142e+00 ", negate(pi));
test("%.3e", "3.142e+00", pi);
test("%.3e", "-3.142e+00", negate(pi));
test("%.3e", "3.142e+06", mult(pi, 1000000.0));
test("%.3e", "-3.142e+06", mult(pi, -1000000.0));
test(Locale.FRANCE, "%e", "3,141593e+00", pi);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%10.3e", " 1.000e+00", one);
test("%+.3e", "+3.142e+00", pi);
test("%+.3e", "-3.142e+00", negate(pi));
test("% .3e", " 3.142e+00", pi);
test("% .3e", "-3.142e+00", negate(pi));
test("%#.0e", "3.e+00", create(3.0));
test("%#.0e", "-3.e+00", create(-3.0));
test("%.0e", "3e+00", create(3.0));
test("%.0e", "-3e+00", create(-3.0));
test("%(.4e", "3.1416e+06", mult(pi, 1000000.0));
test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %e - boundary problems
//---------------------------------------------------------------------
test("%3.0e", "1e-06", 0.000001);
test("%3.0e", "1e-05", 0.00001);
test("%3.0e", "1e-04", 0.0001);
test("%3.0e", "1e-03", 0.001);
test("%3.0e", "1e-02", 0.01);
test("%3.0e", "1e-01", 0.1);
test("%3.0e", "9e-01", 0.9);
test("%3.1e", "9.0e-01", 0.9);
test("%3.0e", "1e+00", 1.00);
test("%3.0e", "1e+01", 10.00);
test("%3.0e", "1e+02", 99.19);
test("%3.1e", "9.9e+01", 99.19);
test("%3.0e", "1e+02", 99.99);
test("%3.0e", "1e+02", 100.00);
test("%#3.0e", "1.e+03", 1000.00);
test("%3.0e", "1e+04", 10000.00);
test("%3.0e", "1e+05", 100000.00);
test("%3.0e", "1e+06", 1000000.00);
test("%3.0e", "1e+07", 10000000.00);
test("%3.0e", "1e+08", 100000000.00);
//---------------------------------------------------------------------
// %e - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.1e", "9.9e-324", 1e-323);
test("%.1e", "9.9e-323", 1e-322);
test("%.15e", "7.387900000000000e+20", 7.3879e20);
test("%.15e", "1.000000000000000e+23", 1e23);
test("%.16e", "2.0000000000000000e+23", 2e23);
test("%.16e", "1.9400994884341945e+25", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%f", "null", (Object)null);
test("%f", "3.141593", pi);
test(Locale.FRANCE, "%f", "3,141593", pi);
test("%10.3f", " 3.142", pi);
test("%10.3f", " -3.142", negate(pi));
test("%010.3f", "000003.142", pi);
test("%010.3f", "-00003.142", negate(pi));
test("%-10.3f", "3.142 ", pi);
test("%-10.3f", "-3.142 ", negate(pi));
test("%.3f", "3.142", pi);
test("%.3f", "-3.142", negate(pi));
test("%+.3f", "+3.142", pi);
test("%+.3f", "-3.142", negate(pi));
test("% .3f", " 3.142", pi);
test("% .3f", "-3.142", negate(pi));
test("%#.0f", "3.", create(3.0));
test("%#.0f", "-3.", create(-3.0));
test("%.0f", "3", create(3.0));
test("%.0f", "-3", create(-3.0));
test("%.3f", "10.000", ten);
test("%.3f", "1.000", one);
test("%10.3f", " 1.000", one);
//---------------------------------------------------------------------
// %f - boundary problems
//---------------------------------------------------------------------
test("%3.0f", " 0", 0.000001);
test("%3.0f", " 0", 0.00001);
test("%3.0f", " 0", 0.0001);
test("%3.0f", " 0", 0.001);
test("%3.0f", " 0", 0.01);
test("%3.0f", " 0", 0.1);
test("%3.0f", " 1", 0.9);
test("%3.1f", "0.9", 0.9);
test("%3.0f", " 1", 1.00);
test("%3.0f", " 10", 10.00);
test("%3.0f", " 99", 99.19);
test("%3.1f", "99.2", 99.19);
test("%3.0f", "100", 99.99);
test("%3.0f", "100", 100.00);
test("%#3.0f", "1000.", 1000.00);
test("%3.0f", "10000", 10000.00);
test("%3.0f", "100000", 100000.00);
test("%3.0f", "1000000", 1000000.00);
test("%3.0f", "10000000", 10000000.00);
test("%3.0f", "100000000", 100000000.00);
test("%10.0f", " 1000000", 1000000.00);
test("%,10.0f", " 1,000,000", 1000000.00);
test("%,10.1f", "1,000,000.0", 1000000.00);
test("%,3.0f", "1,000,000", 1000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
//---------------------------------------------------------------------
// %f - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.0f", "738790000000000000000", 7.3879e20);
test("%.0f", "100000000000000000000000", 1e23);
test("%.0f", "200000000000000000000000", 2e23);
test("%.0f", "19400994884341945000000000", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f - float, double, Double, BigDecimal
//---------------------------------------------------------------------
test("%.3f", "3141592.654", mult(pi, 1000000.0));
test("%.3f", "-3141592.654", mult(pi, -1000000.0));
test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0));
test(Locale.FRANCE, "%,.4f", "3\u202f141\u202f592,6536", mult(pi, 1000000.0));
test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0));
test("%(.4f", "3141592.6536", mult(pi, 1000000.0));
test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0));
test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0));
test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %g
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%g", "null", (Object)null);
test("%g", "3.14159", pi);
test(Locale.FRANCE, "%g", "3,14159", pi);
test("%.0g", "1e+01", ten);
test("%G", "3.14159", pi);
test("%10.3g", " 3.14", pi);
test("%10.3g", " -3.14", negate(pi));
test("%010.3g", "0000003.14", pi);
test("%010.3g", "-000003.14", negate(pi));
test("%-12.3g", "3.14 ", pi);
test("%-12.3g", "-3.14 ", negate(pi));
test("%.3g", "3.14", pi);
test("%.3g", "-3.14", negate(pi));
test("%.3g", "3.14e+08", mult(pi, 100000000.0));
test("%.3g", "-3.14e+08", mult(pi, -100000000.0));
test("%.3g", "1.00e-05", recip(create(100000.0)));
test("%.3g", "-1.00e-05", recip(create(-100000.0)));
test("%.0g", "-1e-05", recip(create(-100000.0)));
test("%.0g", "1e+05", create(100000.0));
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);
test("%3.1g", "1e-05", 0.0000099);
test("%3.2g", "9.9e-06", 0.0000099);
test("%3.0g", "0.0001", 0.0001);
test("%3.0g", "9e-05", 0.00009);
test("%3.0g", "0.0001", 0.000099);
test("%3.1g", "0.0001", 0.000099);
test("%3.2g", "9.9e-05", 0.000099);
test("%3.0g", "0.001", 0.001);
test("%3.0g", "0.001", 0.00099);
test("%3.1g", "0.001", 0.00099);
test("%3.2g", "0.00099", 0.00099);
test("%3.3g", "0.00100", 0.001);
test("%3.4g", "0.001000", 0.001);
test("%3.0g", "0.01", 0.01);
test("%3.0g", "0.1", 0.1);
test("%3.0g", "0.9", 0.9);
test("%3.1g", "0.9", 0.9);
test("%3.0g", " 1", 1.00);
test("%3.2g", " 10", 10.00);
test("%3.0g", "1e+01", 10.00);
test("%3.0g", "1e+02", 99.19);
test("%3.1g", "1e+02", 99.19);
test("%3.2g", " 99", 99.19);
test("%3.0g", "1e+02", 99.9);
test("%3.1g", "1e+02", 99.9);
test("%3.2g", "1.0e+02", 99.9);
test("%3.0g", "1e+02", 99.99);
test("%3.0g", "1e+02", 100.00);
test("%3.0g", "1e+03", 999.9);
test("%3.1g", "1e+03", 999.9);
test("%3.2g", "1.0e+03", 999.9);
test("%3.3g", "1.00e+03", 999.9);
test("%3.4g", "999.9", 999.9);
test("%3.4g", "1000", 999.99);
test("%3.0g", "1e+03", 1000.00);
test("%3.0g", "1e+04", 10000.00);
test("%3.0g", "1e+05", 100000.00);
test("%3.0g", "1e+06", 1000000.00);
test("%3.0g", "1e+07", 10000000.00);
test("%3.9g", "100000000", 100000000.00);
test("%3.10g", "100000000.0", 100000000.00);
tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00);
//---------------------------------------------------------------------
// %g - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.2g", "9.9e-324", 1e-323);
test("%.2g", "9.9e-323", 1e-322);
test("%.16g", "7.387900000000000e+20", 7.3879e20);
test("%.16g", "1.000000000000000e+23", 1e23);
test("%.17g", "2.0000000000000000e+23", 2e23);
test("%.17g", "1.9400994884341945e+25", 1.9400994884341945E25);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%.3g", "10.0", ten);
test("%.3g", "1.00", one);
test("%10.3g", " 1.00", one);
test("%+10.3g", " +3.14", pi);
test("%+10.3g", " -3.14", negate(pi));
test("% .3g", " 3.14", pi);
test("% .3g", "-3.14", negate(pi));
test("%.0g", "3", create(3.0));
test("%.0g", "-3", create(-3.0));
test("%(.4g", "3.142e+08", mult(pi, 100000000.0));
test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0));
test("%,.11g", "3,141,592.6536", mult(pi, 1000000.0));
test("%(,.11g", "(3,141,592.6536)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %a
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%a", "null", (Object)null);
test("%.11a", "0x0.00000000000p0", 0.0);
test(Locale.FRANCE, "%.11a", "0x0.00000000000p0", 0.0); // no localization
test("%.1a", "0x0.0p0", 0.0);
test("%.11a", "-0x0.00000000000p0", -0.0);
test("%.1a", "-0x0.0p0", -0.0);
test("%.11a", "0x1.00000000000p0", 1.0);
test("%.1a", "0x1.0p0", 1.0);
test("%.11a", "-0x1.00000000000p0", -1.0);
test("%.1a", "-0x1.0p0", -1.0);
test("%.11a", "0x1.80000000000p1", 3.0);
test("%.1a", "0x1.8p1", 3.0);
test("%.11a", "0x1.00000000000p-1022", Double.MIN_NORMAL);
test("%.1a", "0x1.0p-1022", Double.MIN_NORMAL);
test("%.11a", "0x1.00000000000p-1022",
Math.nextDown(Double.MIN_NORMAL));
test("%.1a", "0x1.0p-1022",
Math.nextDown(Double.MIN_NORMAL));
test("%.11a", "0x1.ffffffffffep-1023", 0x0.fffffffffffp-1022);
test("%.1a", "0x1.0p-1022", 0x0.fffffffffffp-1022);
test("%.30a", "0x0.000000000000100000000000000000p-1022", Double.MIN_VALUE);
test("%.13a", "0x0.0000000000001p-1022", Double.MIN_VALUE);
test("%.11a", "0x1.00000000000p-1074", Double.MIN_VALUE);
test("%.1a", "0x1.0p-1074", Double.MIN_VALUE);
test("%.11a", "0x1.08000000000p-1069",
Double.MIN_VALUE + Double.MIN_VALUE*32);
test("%.1a", "0x1.0p-1069",
Double.MIN_VALUE + Double.MIN_VALUE*32);
test("%.30a", "0x1.fffffffffffff00000000000000000p1023", Double.MAX_VALUE);
test("%.13a", "0x1.fffffffffffffp1023", Double.MAX_VALUE);
test("%.11a", "0x1.00000000000p1024", Double.MAX_VALUE);
test("%.1a", "0x1.0p1024", Double.MAX_VALUE);
test("%.11a", "0x1.18000000000p0", 0x1.18p0);
test("%.1a", "0x1.2p0", 0x1.18p0);
test("%.11a", "0x1.18000000000p0", 0x1.180000000001p0);
test("%.1a", "0x1.2p0", 0x1.180000000001p0);
test("%.11a", "0x1.28000000000p0", 0x1.28p0);
test("%.1a", "0x1.2p0", 0x1.28p0);
test("%.11a", "0x1.28000000000p0", 0x1.280000000001p0);
test("%.1a", "0x1.3p0", 0x1.280000000001p0);
test("%a", "0x0.123p-1022", 0x0.123p-1022);
test("%1.3a", "0x1.230p-1026", 0x0.123p-1022);
test("%1.12a", "0x1.230000000000p-1026", 0x0.123p-1022);
test("%1.15a", "0x0.123000000000000p-1022", 0x0.123p-1022);
test("%1.5a", "0x1.00000p-1074", 0x0.0000000000001p-1022);
test("%1.7a", "0x1.0000000p-1022", 0x0.fffffffffffffp-1022);
test("%1.6a", "0x1.230000p-1026", 0x0.123000057p-1022);
test("%1.7a", "0x1.2300005p-1026", 0x0.123000057p-1022);
test("%1.8a", "0x1.23000057p-1026", 0x0.123000057p-1022);
test("%1.9a", "0x1.230000570p-1026", 0x0.123000057p-1022);
test("%1.6a", "0x1.230000p-1026", 0x0.123000058p-1022);
test("%1.7a", "0x1.2300006p-1026", 0x0.123000058p-1022);
test("%1.8a", "0x1.23000058p-1026", 0x0.123000058p-1022);
test("%1.9a", "0x1.230000580p-1026", 0x0.123000058p-1022);
test("%1.6a", "0x1.230000p-1026", 0x0.123000059p-1022);
test("%1.7a", "0x1.2300006p-1026", 0x0.123000059p-1022);
test("%1.8a", "0x1.23000059p-1026", 0x0.123000059p-1022);
test("%1.9a", "0x1.230000590p-1026", 0x0.123000059p-1022);
test("%1.4a", "0x1.0000p-1022", Math.nextDown(Double.MIN_NORMAL));
test("%a", "0x1.fffffffffffffp1023", Double.MAX_VALUE);
test("%1.1a", "0x1.0p1024", Double.MAX_VALUE);
test("%1.2a", "0x1.00p1024", Double.MAX_VALUE);
test("%1.6a", "0x1.000000p1024", Double.MAX_VALUE);
test("%1.9a", "0x1.000000000p1024", Double.MAX_VALUE);
test("%1.11a", "0x1.00000000000p1024", Double.MAX_VALUE);
test("%1.12a", "0x1.000000000000p1024", Double.MAX_VALUE);
test("%1.13a", "0x1.fffffffffffffp1023", Double.MAX_VALUE);
//---------------------------------------------------------------------
// %f, %e, %g, %a - Boundaries
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// %f, %e, %g, %a - Double.MIN_VALUE
//---------------------------------------------------------------------
test("%f", "0.000000", Double.MIN_VALUE);
test("%,f", "0.000000", Double.MIN_VALUE);
test("%(f", "(0.000000)", -Double.MIN_VALUE);
test("%30.0f", " 0", Double.MIN_VALUE);
test("%30.5f", " 0.00000", Double.MIN_VALUE);
test("%30.13f", " 0.0000000000000", Double.MIN_VALUE);
test("%30.20f", " 0.00000000000000000000", Double.MIN_VALUE);
test("%30.350f","0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000490000000000000000000000000",
Double.MIN_VALUE);
test("%e", "4.900000e-324", Double.MIN_VALUE);
test("%E", "4.900000E-324", Double.MIN_VALUE);
test("%(.1e", "4.9e-324", Double.MIN_VALUE);
test("%(E", "(4.900000E-324)", -Double.MIN_VALUE);
test("%30.5e", " 4.90000e-324", Double.MIN_VALUE);
test("%30.13e", " 4.9000000000000e-324", Double.MIN_VALUE);
test("%30.20e", " 4.90000000000000000000e-324", Double.MIN_VALUE);
test("%g", "4.90000e-324", Double.MIN_VALUE);
test("%G", "4.90000E-324", Double.MIN_VALUE);
test("%(g", "4.90000e-324", Double.MIN_VALUE);
test("%,g", "4.90000e-324", Double.MIN_VALUE);
test("%30.5g", " 4.9000e-324", Double.MIN_VALUE);
test("%30.13g", " 4.900000000000e-324", Double.MIN_VALUE);
test("%30.20g", " 4.9000000000000000000e-324", Double.MIN_VALUE);
test("%a", "0x0.0000000000001p-1022", Double.MIN_VALUE);
test("%A", "0X0.0000000000001P-1022", Double.MIN_VALUE);
test("%30a", " 0x0.0000000000001p-1022", Double.MIN_VALUE);
//---------------------------------------------------------------------
// %f, %e, %g, %a - Double.MAX_VALUE
//---------------------------------------------------------------------
test("%f", "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000",
Double.MAX_VALUE);
test("%,f", "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000000",
Double.MAX_VALUE);
test("%,(f", "(179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000000)",
-Double.MAX_VALUE);
test("%,30.5f", "179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.00000",
Double.MAX_VALUE);
test("%30.13f", "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000000000000",
Double.MAX_VALUE);
test("%30.20f", "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000000000000000000",
Double.MAX_VALUE);
test("%e", "1.797693e+308", Double.MAX_VALUE);
test("%E", "1.797693E+308", Double.MAX_VALUE);
test("%(e", "1.797693e+308", Double.MAX_VALUE);
test("%(e", "(1.797693e+308)", -Double.MAX_VALUE);
test("%30.5e", " 1.79769e+308", Double.MAX_VALUE);
test("%30.13e", " 1.7976931348623e+308", Double.MAX_VALUE);
test("%30.20e", " 1.79769313486231570000e+308", Double.MAX_VALUE);
test("%g", "1.79769e+308", Double.MAX_VALUE);
test("%G", "1.79769E+308", Double.MAX_VALUE);
test("%,g", "1.79769e+308", Double.MAX_VALUE);
test("%(g", "(1.79769e+308)", -Double.MAX_VALUE);
test("%30.5g", " 1.7977e+308", Double.MAX_VALUE);
test("%30.13g", " 1.797693134862e+308", Double.MAX_VALUE);
test("%30.20g", " 1.7976931348623157000e+308", Double.MAX_VALUE);
test("%a", "0x1.fffffffffffffp1023", Double.MAX_VALUE);
test("%A", "0X1.FFFFFFFFFFFFFP1023", Double.MAX_VALUE);
test("%30a", " 0x1.fffffffffffffp1023", Double.MAX_VALUE);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,594 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicDoubleObject extends Basic {
private static Double create(double v) {
return v;
}
private static Double negate(Double v) {
return -v.doubleValue();
}
private static Double mult(Double v, double mul) {
return v.doubleValue() * mul;
}
private static Double recip(Double v) {
return 1.0 / v.doubleValue();
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %s - Double
//---------------------------------------------------------------------
Double one = 1.0d;
Double ten = 10.0d;
Double pi = (double) Math.PI;
test("%s", "3.141592653589793", pi);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%d", IllegalFormatConversionException.class, one);
tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one);
//---------------------------------------------------------------------
// %e
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%e", "null", (Object)null);
//---------------------------------------------------------------------
// %e - float and double
//---------------------------------------------------------------------
// double PI = 3.141 592 653 589 793 238 46;
test("%e", "3.141593e+00", pi);
test("%.0e", "1e+01", ten);
test("%#.0e", "1.e+01", ten);
test("%E", "3.141593E+00", pi);
test("%10.3e", " 3.142e+00", pi);
test("%10.3e", "-3.142e+00", negate(pi));
test("%010.3e", "03.142e+00", pi);
test("%010.3e", "-3.142e+00", negate(pi));
test("%-12.3e", "3.142e+00 ", pi);
test("%-12.3e", "-3.142e+00 ", negate(pi));
test("%.3e", "3.142e+00", pi);
test("%.3e", "-3.142e+00", negate(pi));
test("%.3e", "3.142e+06", mult(pi, 1000000.0));
test("%.3e", "-3.142e+06", mult(pi, -1000000.0));
test(Locale.FRANCE, "%e", "3,141593e+00", pi);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%10.3e", " 1.000e+00", one);
test("%+.3e", "+3.142e+00", pi);
test("%+.3e", "-3.142e+00", negate(pi));
test("% .3e", " 3.142e+00", pi);
test("% .3e", "-3.142e+00", negate(pi));
test("%#.0e", "3.e+00", create(3.0));
test("%#.0e", "-3.e+00", create(-3.0));
test("%.0e", "3e+00", create(3.0));
test("%.0e", "-3e+00", create(-3.0));
test("%(.4e", "3.1416e+06", mult(pi, 1000000.0));
test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %e - boundary problems
//---------------------------------------------------------------------
test("%3.0e", "1e-06", 0.000001);
test("%3.0e", "1e-05", 0.00001);
test("%3.0e", "1e-04", 0.0001);
test("%3.0e", "1e-03", 0.001);
test("%3.0e", "1e-02", 0.01);
test("%3.0e", "1e-01", 0.1);
test("%3.0e", "9e-01", 0.9);
test("%3.1e", "9.0e-01", 0.9);
test("%3.0e", "1e+00", 1.00);
test("%3.0e", "1e+01", 10.00);
test("%3.0e", "1e+02", 99.19);
test("%3.1e", "9.9e+01", 99.19);
test("%3.0e", "1e+02", 99.99);
test("%3.0e", "1e+02", 100.00);
test("%#3.0e", "1.e+03", 1000.00);
test("%3.0e", "1e+04", 10000.00);
test("%3.0e", "1e+05", 100000.00);
test("%3.0e", "1e+06", 1000000.00);
test("%3.0e", "1e+07", 10000000.00);
test("%3.0e", "1e+08", 100000000.00);
//---------------------------------------------------------------------
// %e - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.1e", "9.9e-324", 1e-323);
test("%.1e", "9.9e-323", 1e-322);
test("%.15e", "7.387900000000000e+20", 7.3879e20);
test("%.15e", "1.000000000000000e+23", 1e23);
test("%.16e", "2.0000000000000000e+23", 2e23);
test("%.16e", "1.9400994884341945e+25", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%f", "null", (Object)null);
test("%f", "3.141593", pi);
test(Locale.FRANCE, "%f", "3,141593", pi);
test("%10.3f", " 3.142", pi);
test("%10.3f", " -3.142", negate(pi));
test("%010.3f", "000003.142", pi);
test("%010.3f", "-00003.142", negate(pi));
test("%-10.3f", "3.142 ", pi);
test("%-10.3f", "-3.142 ", negate(pi));
test("%.3f", "3.142", pi);
test("%.3f", "-3.142", negate(pi));
test("%+.3f", "+3.142", pi);
test("%+.3f", "-3.142", negate(pi));
test("% .3f", " 3.142", pi);
test("% .3f", "-3.142", negate(pi));
test("%#.0f", "3.", create(3.0));
test("%#.0f", "-3.", create(-3.0));
test("%.0f", "3", create(3.0));
test("%.0f", "-3", create(-3.0));
test("%.3f", "10.000", ten);
test("%.3f", "1.000", one);
test("%10.3f", " 1.000", one);
//---------------------------------------------------------------------
// %f - boundary problems
//---------------------------------------------------------------------
test("%3.0f", " 0", 0.000001);
test("%3.0f", " 0", 0.00001);
test("%3.0f", " 0", 0.0001);
test("%3.0f", " 0", 0.001);
test("%3.0f", " 0", 0.01);
test("%3.0f", " 0", 0.1);
test("%3.0f", " 1", 0.9);
test("%3.1f", "0.9", 0.9);
test("%3.0f", " 1", 1.00);
test("%3.0f", " 10", 10.00);
test("%3.0f", " 99", 99.19);
test("%3.1f", "99.2", 99.19);
test("%3.0f", "100", 99.99);
test("%3.0f", "100", 100.00);
test("%#3.0f", "1000.", 1000.00);
test("%3.0f", "10000", 10000.00);
test("%3.0f", "100000", 100000.00);
test("%3.0f", "1000000", 1000000.00);
test("%3.0f", "10000000", 10000000.00);
test("%3.0f", "100000000", 100000000.00);
test("%10.0f", " 1000000", 1000000.00);
test("%,10.0f", " 1,000,000", 1000000.00);
test("%,10.1f", "1,000,000.0", 1000000.00);
test("%,3.0f", "1,000,000", 1000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
//---------------------------------------------------------------------
// %f - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.0f", "738790000000000000000", 7.3879e20);
test("%.0f", "100000000000000000000000", 1e23);
test("%.0f", "200000000000000000000000", 2e23);
test("%.0f", "19400994884341945000000000", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f - float, double, Double, BigDecimal
//---------------------------------------------------------------------
test("%.3f", "3141592.654", mult(pi, 1000000.0));
test("%.3f", "-3141592.654", mult(pi, -1000000.0));
test("%,.4f", "3,141,592.6536", mult(pi, 1000000.0));
test(Locale.FRANCE, "%,.4f", "3\u202f141\u202f592,6536", mult(pi, 1000000.0));
test("%,.4f", "-3,141,592.6536", mult(pi, -1000000.0));
test("%(.4f", "3141592.6536", mult(pi, 1000000.0));
test("%(.4f", "(3141592.6536)", mult(pi, -1000000.0));
test("%(,.4f", "3,141,592.6536", mult(pi, 1000000.0));
test("%(,.4f", "(3,141,592.6536)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %g
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%g", "null", (Object)null);
test("%g", "3.14159", pi);
test(Locale.FRANCE, "%g", "3,14159", pi);
test("%.0g", "1e+01", ten);
test("%G", "3.14159", pi);
test("%10.3g", " 3.14", pi);
test("%10.3g", " -3.14", negate(pi));
test("%010.3g", "0000003.14", pi);
test("%010.3g", "-000003.14", negate(pi));
test("%-12.3g", "3.14 ", pi);
test("%-12.3g", "-3.14 ", negate(pi));
test("%.3g", "3.14", pi);
test("%.3g", "-3.14", negate(pi));
test("%.3g", "3.14e+08", mult(pi, 100000000.0));
test("%.3g", "-3.14e+08", mult(pi, -100000000.0));
test("%.3g", "1.00e-05", recip(create(100000.0)));
test("%.3g", "-1.00e-05", recip(create(-100000.0)));
test("%.0g", "-1e-05", recip(create(-100000.0)));
test("%.0g", "1e+05", create(100000.0));
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);
test("%3.1g", "1e-05", 0.0000099);
test("%3.2g", "9.9e-06", 0.0000099);
test("%3.0g", "0.0001", 0.0001);
test("%3.0g", "9e-05", 0.00009);
test("%3.0g", "0.0001", 0.000099);
test("%3.1g", "0.0001", 0.000099);
test("%3.2g", "9.9e-05", 0.000099);
test("%3.0g", "0.001", 0.001);
test("%3.0g", "0.001", 0.00099);
test("%3.1g", "0.001", 0.00099);
test("%3.2g", "0.00099", 0.00099);
test("%3.3g", "0.00100", 0.001);
test("%3.4g", "0.001000", 0.001);
test("%3.0g", "0.01", 0.01);
test("%3.0g", "0.1", 0.1);
test("%3.0g", "0.9", 0.9);
test("%3.1g", "0.9", 0.9);
test("%3.0g", " 1", 1.00);
test("%3.2g", " 10", 10.00);
test("%3.0g", "1e+01", 10.00);
test("%3.0g", "1e+02", 99.19);
test("%3.1g", "1e+02", 99.19);
test("%3.2g", " 99", 99.19);
test("%3.0g", "1e+02", 99.9);
test("%3.1g", "1e+02", 99.9);
test("%3.2g", "1.0e+02", 99.9);
test("%3.0g", "1e+02", 99.99);
test("%3.0g", "1e+02", 100.00);
test("%3.0g", "1e+03", 999.9);
test("%3.1g", "1e+03", 999.9);
test("%3.2g", "1.0e+03", 999.9);
test("%3.3g", "1.00e+03", 999.9);
test("%3.4g", "999.9", 999.9);
test("%3.4g", "1000", 999.99);
test("%3.0g", "1e+03", 1000.00);
test("%3.0g", "1e+04", 10000.00);
test("%3.0g", "1e+05", 100000.00);
test("%3.0g", "1e+06", 1000000.00);
test("%3.0g", "1e+07", 10000000.00);
test("%3.9g", "100000000", 100000000.00);
test("%3.10g", "100000000.0", 100000000.00);
tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00);
//---------------------------------------------------------------------
// %g - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.2g", "9.9e-324", 1e-323);
test("%.2g", "9.9e-323", 1e-322);
test("%.16g", "7.387900000000000e+20", 7.3879e20);
test("%.16g", "1.000000000000000e+23", 1e23);
test("%.17g", "2.0000000000000000e+23", 2e23);
test("%.17g", "1.9400994884341945e+25", 1.9400994884341945E25);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%.3g", "10.0", ten);
test("%.3g", "1.00", one);
test("%10.3g", " 1.00", one);
test("%+10.3g", " +3.14", pi);
test("%+10.3g", " -3.14", negate(pi));
test("% .3g", " 3.14", pi);
test("% .3g", "-3.14", negate(pi));
test("%.0g", "3", create(3.0));
test("%.0g", "-3", create(-3.0));
test("%(.4g", "3.142e+08", mult(pi, 100000000.0));
test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0));
test("%,.11g", "3,141,592.6536", mult(pi, 1000000.0));
test("%(,.11g", "(3,141,592.6536)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %f, %e, %g, %a - Boundaries
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,763 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicFloat extends Basic {
private static float create(double v) {
return (float) v;
}
private static float negate(double v) {
return (float) -v;
}
private static float mult(float v, double mul) {
return v * (float) mul;
}
private static float recip(float v) {
return 1.0f / v;
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %s - float
//---------------------------------------------------------------------
float one = 1.0f;
float ten = 10.0f;
float pi = (float) Math.PI;
test("%s", "3.1415927", pi);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%d", IllegalFormatConversionException.class, one);
tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one);
//---------------------------------------------------------------------
// %e
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%e", "null", (Object)null);
//---------------------------------------------------------------------
// %e - float and double
//---------------------------------------------------------------------
// double PI = 3.141 592 653 589 793 238 46;
test("%e", "3.141593e+00", pi);
test("%.0e", "1e+01", ten);
test("%#.0e", "1.e+01", ten);
test("%E", "3.141593E+00", pi);
test("%10.3e", " 3.142e+00", pi);
test("%10.3e", "-3.142e+00", negate(pi));
test("%010.3e", "03.142e+00", pi);
test("%010.3e", "-3.142e+00", negate(pi));
test("%-12.3e", "3.142e+00 ", pi);
test("%-12.3e", "-3.142e+00 ", negate(pi));
test("%.3e", "3.142e+00", pi);
test("%.3e", "-3.142e+00", negate(pi));
test("%.3e", "3.142e+06", mult(pi, 1000000.0));
test("%.3e", "-3.142e+06", mult(pi, -1000000.0));
test(Locale.FRANCE, "%e", "3,141593e+00", pi);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%10.3e", " 1.000e+00", one);
test("%+.3e", "+3.142e+00", pi);
test("%+.3e", "-3.142e+00", negate(pi));
test("% .3e", " 3.142e+00", pi);
test("% .3e", "-3.142e+00", negate(pi));
test("%#.0e", "3.e+00", create(3.0));
test("%#.0e", "-3.e+00", create(-3.0));
test("%.0e", "3e+00", create(3.0));
test("%.0e", "-3e+00", create(-3.0));
test("%(.4e", "3.1416e+06", mult(pi, 1000000.0));
test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %e - boundary problems
//---------------------------------------------------------------------
test("%3.0e", "1e-06", 0.000001);
test("%3.0e", "1e-05", 0.00001);
test("%3.0e", "1e-04", 0.0001);
test("%3.0e", "1e-03", 0.001);
test("%3.0e", "1e-02", 0.01);
test("%3.0e", "1e-01", 0.1);
test("%3.0e", "9e-01", 0.9);
test("%3.1e", "9.0e-01", 0.9);
test("%3.0e", "1e+00", 1.00);
test("%3.0e", "1e+01", 10.00);
test("%3.0e", "1e+02", 99.19);
test("%3.1e", "9.9e+01", 99.19);
test("%3.0e", "1e+02", 99.99);
test("%3.0e", "1e+02", 100.00);
test("%#3.0e", "1.e+03", 1000.00);
test("%3.0e", "1e+04", 10000.00);
test("%3.0e", "1e+05", 100000.00);
test("%3.0e", "1e+06", 1000000.00);
test("%3.0e", "1e+07", 10000000.00);
test("%3.0e", "1e+08", 100000000.00);
//---------------------------------------------------------------------
// %e - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.1e", "9.9e-324", 1e-323);
test("%.1e", "9.9e-323", 1e-322);
test("%.15e", "7.387900000000000e+20", 7.3879e20);
test("%.15e", "1.000000000000000e+23", 1e23);
test("%.16e", "2.0000000000000000e+23", 2e23);
test("%.16e", "1.9400994884341945e+25", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%f", "null", (Object)null);
test("%f", "3.141593", pi);
test(Locale.FRANCE, "%f", "3,141593", pi);
test("%10.3f", " 3.142", pi);
test("%10.3f", " -3.142", negate(pi));
test("%010.3f", "000003.142", pi);
test("%010.3f", "-00003.142", negate(pi));
test("%-10.3f", "3.142 ", pi);
test("%-10.3f", "-3.142 ", negate(pi));
test("%.3f", "3.142", pi);
test("%.3f", "-3.142", negate(pi));
test("%+.3f", "+3.142", pi);
test("%+.3f", "-3.142", negate(pi));
test("% .3f", " 3.142", pi);
test("% .3f", "-3.142", negate(pi));
test("%#.0f", "3.", create(3.0));
test("%#.0f", "-3.", create(-3.0));
test("%.0f", "3", create(3.0));
test("%.0f", "-3", create(-3.0));
test("%.3f", "10.000", ten);
test("%.3f", "1.000", one);
test("%10.3f", " 1.000", one);
//---------------------------------------------------------------------
// %f - boundary problems
//---------------------------------------------------------------------
test("%3.0f", " 0", 0.000001);
test("%3.0f", " 0", 0.00001);
test("%3.0f", " 0", 0.0001);
test("%3.0f", " 0", 0.001);
test("%3.0f", " 0", 0.01);
test("%3.0f", " 0", 0.1);
test("%3.0f", " 1", 0.9);
test("%3.1f", "0.9", 0.9);
test("%3.0f", " 1", 1.00);
test("%3.0f", " 10", 10.00);
test("%3.0f", " 99", 99.19);
test("%3.1f", "99.2", 99.19);
test("%3.0f", "100", 99.99);
test("%3.0f", "100", 100.00);
test("%#3.0f", "1000.", 1000.00);
test("%3.0f", "10000", 10000.00);
test("%3.0f", "100000", 100000.00);
test("%3.0f", "1000000", 1000000.00);
test("%3.0f", "10000000", 10000000.00);
test("%3.0f", "100000000", 100000000.00);
test("%10.0f", " 1000000", 1000000.00);
test("%,10.0f", " 1,000,000", 1000000.00);
test("%,10.1f", "1,000,000.0", 1000000.00);
test("%,3.0f", "1,000,000", 1000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
//---------------------------------------------------------------------
// %f - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.0f", "738790000000000000000", 7.3879e20);
test("%.0f", "100000000000000000000000", 1e23);
test("%.0f", "200000000000000000000000", 2e23);
test("%.0f", "19400994884341945000000000", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f - float
//---------------------------------------------------------------------
// Float can not accurately store 1e6 * PI.
test("%.3f", "3141.593", mult(pi, 1000.0));
test("%.3f", "-3141.593", mult(pi, -1000.0));
test("%,.2f", "3,141.59", mult(pi, 1000.0));
test(Locale.FRANCE, "%,.2f", "3\u202f141,59", mult(pi, 1000.0));
test("%,.2f", "-3,141.59", mult(pi, -1000.0));
test("%(.2f", "3141.59", mult(pi, 1000.0));
test("%(.2f", "(3141.59)", mult(pi, -1000.0));
test("%(,.2f", "3,141.59", mult(pi, 1000.0));
test("%(,.2f", "(3,141.59)", mult(pi, -1000.0));
//---------------------------------------------------------------------
// %g
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%g", "null", (Object)null);
test("%g", "3.14159", pi);
test(Locale.FRANCE, "%g", "3,14159", pi);
test("%.0g", "1e+01", ten);
test("%G", "3.14159", pi);
test("%10.3g", " 3.14", pi);
test("%10.3g", " -3.14", negate(pi));
test("%010.3g", "0000003.14", pi);
test("%010.3g", "-000003.14", negate(pi));
test("%-12.3g", "3.14 ", pi);
test("%-12.3g", "-3.14 ", negate(pi));
test("%.3g", "3.14", pi);
test("%.3g", "-3.14", negate(pi));
test("%.3g", "3.14e+08", mult(pi, 100000000.0));
test("%.3g", "-3.14e+08", mult(pi, -100000000.0));
test("%.3g", "1.00e-05", recip(create(100000.0)));
test("%.3g", "-1.00e-05", recip(create(-100000.0)));
test("%.0g", "-1e-05", recip(create(-100000.0)));
test("%.0g", "1e+05", create(100000.0));
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);
test("%3.1g", "1e-05", 0.0000099);
test("%3.2g", "9.9e-06", 0.0000099);
test("%3.0g", "0.0001", 0.0001);
test("%3.0g", "9e-05", 0.00009);
test("%3.0g", "0.0001", 0.000099);
test("%3.1g", "0.0001", 0.000099);
test("%3.2g", "9.9e-05", 0.000099);
test("%3.0g", "0.001", 0.001);
test("%3.0g", "0.001", 0.00099);
test("%3.1g", "0.001", 0.00099);
test("%3.2g", "0.00099", 0.00099);
test("%3.3g", "0.00100", 0.001);
test("%3.4g", "0.001000", 0.001);
test("%3.0g", "0.01", 0.01);
test("%3.0g", "0.1", 0.1);
test("%3.0g", "0.9", 0.9);
test("%3.1g", "0.9", 0.9);
test("%3.0g", " 1", 1.00);
test("%3.2g", " 10", 10.00);
test("%3.0g", "1e+01", 10.00);
test("%3.0g", "1e+02", 99.19);
test("%3.1g", "1e+02", 99.19);
test("%3.2g", " 99", 99.19);
test("%3.0g", "1e+02", 99.9);
test("%3.1g", "1e+02", 99.9);
test("%3.2g", "1.0e+02", 99.9);
test("%3.0g", "1e+02", 99.99);
test("%3.0g", "1e+02", 100.00);
test("%3.0g", "1e+03", 999.9);
test("%3.1g", "1e+03", 999.9);
test("%3.2g", "1.0e+03", 999.9);
test("%3.3g", "1.00e+03", 999.9);
test("%3.4g", "999.9", 999.9);
test("%3.4g", "1000", 999.99);
test("%3.0g", "1e+03", 1000.00);
test("%3.0g", "1e+04", 10000.00);
test("%3.0g", "1e+05", 100000.00);
test("%3.0g", "1e+06", 1000000.00);
test("%3.0g", "1e+07", 10000000.00);
test("%3.9g", "100000000", 100000000.00);
test("%3.10g", "100000000.0", 100000000.00);
tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00);
//---------------------------------------------------------------------
// %g - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.2g", "9.9e-324", 1e-323);
test("%.2g", "9.9e-323", 1e-322);
test("%.16g", "7.387900000000000e+20", 7.3879e20);
test("%.16g", "1.000000000000000e+23", 1e23);
test("%.17g", "2.0000000000000000e+23", 2e23);
test("%.17g", "1.9400994884341945e+25", 1.9400994884341945E25);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%.3g", "10.0", ten);
test("%.3g", "1.00", one);
test("%10.3g", " 1.00", one);
test("%+10.3g", " +3.14", pi);
test("%+10.3g", " -3.14", negate(pi));
test("% .3g", " 3.14", pi);
test("% .3g", "-3.14", negate(pi));
test("%.0g", "3", create(3.0));
test("%.0g", "-3", create(-3.0));
test("%(.4g", "3.142e+08", mult(pi, 100000000.0));
test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0));
// Float can not accurately store 1e6 * PI.
test("%,.6g", "3,141.59", mult(pi, 1000.0));
test("%(,.6g", "(3,141.59)", mult(pi, -1000.0));
//---------------------------------------------------------------------
// %f, %e, %g, %a - Boundaries
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// %f, %e, %g, %a - NaN
//---------------------------------------------------------------------
test("%f", "NaN", Float.NaN);
// s
test("%+f", "NaN", Float.NaN);
// test("%F", "NAN", Float.NaN);
test("%e", "NaN", Float.NaN);
test("%+e", "NaN", Float.NaN);
test("%E", "NAN", Float.NaN);
test("%g", "NaN", Float.NaN);
test("%+g", "NaN", Float.NaN);
test("%G", "NAN", Float.NaN);
test("%a", "NaN", Float.NaN);
test("%+a", "NaN", Float.NaN);
test("%A", "NAN", Float.NaN);
//---------------------------------------------------------------------
// %f, %e, %g, %a - +0.0
//---------------------------------------------------------------------
test("%f", "0.000000", +0.0);
test("%+f", "+0.000000", +0.0);
test("% f", " 0.000000", +0.0);
// test("%F", "0.000000", +0.0);
test("%e", "0.000000e+00", 0e0);
test("%e", "0.000000e+00", +0.0);
test("%+e", "+0.000000e+00", +0.0);
test("% e", " 0.000000e+00", +0.0);
test("%E", "0.000000E+00", 0e0);
test("%E", "0.000000E+00", +0.0);
test("%+E", "+0.000000E+00", +0.0);
test("% E", " 0.000000E+00", +0.0);
test("%g", "0.00000", +0.0);
test("%+g", "+0.00000", +0.0);
test("% g", " 0.00000", +0.0);
test("%G", "0.00000", +0.0);
test("% G", " 0.00000", +0.0);
test("%a", "0x0.0p0", +0.0);
test("%+a", "+0x0.0p0", +0.0);
test("% a", " 0x0.0p0", +0.0);
test("%A", "0X0.0P0", +0.0);
test("% A", " 0X0.0P0", +0.0);
//---------------------------------------------------------------------
// %f, %e, %g, %a - -0.0
//---------------------------------------------------------------------
test("%f", "-0.000000", -0.0);
test("%+f", "-0.000000", -0.0);
// test("%F", "-0.000000", -0.0);
test("%e", "-0.000000e+00", -0.0);
test("%+e", "-0.000000e+00", -0.0);
test("%E", "-0.000000E+00", -0.0);
test("%+E", "-0.000000E+00", -0.0);
test("%g", "-0.00000", -0.0);
test("%+g", "-0.00000", -0.0);
test("%G", "-0.00000", -0.0);
test("%a", "-0x0.0p0", -0.0);
test("%+a", "-0x0.0p0", -0.0);
test("%+A", "-0X0.0P0", -0.0);
//---------------------------------------------------------------------
// %f, %e, %g, %a - +Infinity
//---------------------------------------------------------------------
test("%f", "Infinity", Float.POSITIVE_INFINITY);
test("%+f", "+Infinity", Float.POSITIVE_INFINITY);
test("% f", " Infinity", Float.POSITIVE_INFINITY);
// test("%F", "INFINITY", Float.POSITIVE_INFINITY);
test("%e", "Infinity", Float.POSITIVE_INFINITY);
test("%+e", "+Infinity", Float.POSITIVE_INFINITY);
test("% e", " Infinity", Float.POSITIVE_INFINITY);
test("%E", "INFINITY", Float.POSITIVE_INFINITY);
test("%+E", "+INFINITY", Float.POSITIVE_INFINITY);
test("% E", " INFINITY", Float.POSITIVE_INFINITY);
test("%g", "Infinity", Float.POSITIVE_INFINITY);
test("%+g", "+Infinity", Float.POSITIVE_INFINITY);
test("%G", "INFINITY", Float.POSITIVE_INFINITY);
test("% G", " INFINITY", Float.POSITIVE_INFINITY);
test("%+G", "+INFINITY", Float.POSITIVE_INFINITY);
test("%a", "Infinity", Float.POSITIVE_INFINITY);
test("%+a", "+Infinity", Float.POSITIVE_INFINITY);
test("% a", " Infinity", Float.POSITIVE_INFINITY);
test("%A", "INFINITY", Float.POSITIVE_INFINITY);
test("%+A", "+INFINITY", Float.POSITIVE_INFINITY);
test("% A", " INFINITY", Float.POSITIVE_INFINITY);
//---------------------------------------------------------------------
// %f, %e, %g, %a - -Infinity
//---------------------------------------------------------------------
test("%f", "-Infinity", Float.NEGATIVE_INFINITY);
test("%+f", "-Infinity", Float.NEGATIVE_INFINITY);
test("%(f", "(Infinity)", Float.NEGATIVE_INFINITY);
// test("%F", "-INFINITY", Float.NEGATIVE_INFINITY);
test("%e", "-Infinity", Float.NEGATIVE_INFINITY);
test("%+e", "-Infinity", Float.NEGATIVE_INFINITY);
test("%E", "-INFINITY", Float.NEGATIVE_INFINITY);
test("%+E", "-INFINITY", Float.NEGATIVE_INFINITY);
test("%g", "-Infinity", Float.NEGATIVE_INFINITY);
test("%+g", "-Infinity", Float.NEGATIVE_INFINITY);
test("%G", "-INFINITY", Float.NEGATIVE_INFINITY);
test("%+G", "-INFINITY", Float.NEGATIVE_INFINITY);
test("%a", "-Infinity", Float.NEGATIVE_INFINITY);
test("%+a", "-Infinity", Float.NEGATIVE_INFINITY);
test("%A", "-INFINITY", Float.NEGATIVE_INFINITY);
test("%+A", "-INFINITY", Float.NEGATIVE_INFINITY);
//---------------------------------------------------------------------
// %f, %e, %g, %a - Float.MIN_VALUE
//---------------------------------------------------------------------
test("%f", "0.000000", Float.MIN_VALUE);
test("%,f", "0.000000", Float.MIN_VALUE);
test("%(f", "(0.000000)", -Float.MIN_VALUE);
test("%30.0f", " 0", Float.MIN_VALUE);
test("%30.5f", " 0.00000", Float.MIN_VALUE);
test("%30.13f", " 0.0000000000000", Float.MIN_VALUE);
test("%30.20f", " 0.00000000000000000000", Float.MIN_VALUE);
test("%e", "1.401298e-45", Float.MIN_VALUE);
test("%E", "1.401298E-45", Float.MIN_VALUE);
test("%(.1e", "1.4e-45", Float.MIN_VALUE);
test("%(E", "(1.401298E-45)", -Float.MIN_VALUE);
test("%30.5e", " 1.40130e-45", Float.MIN_VALUE);
test("%30.13e", " 1.4012984643248e-45", Float.MIN_VALUE);
test("%30.20e", " 1.40129846432481700000e-45", Float.MIN_VALUE);
test("%g", "1.40130e-45", Float.MIN_VALUE);
test("%G", "1.40130E-45", Float.MIN_VALUE);
test("%(g", "1.40130e-45", Float.MIN_VALUE);
test("%,g", "1.40130e-45", Float.MIN_VALUE);
test("%(G", "(1.40130E-45)", -Float.MIN_VALUE);
test("%30.5g", " 1.4013e-45", Float.MIN_VALUE);
test("%30.13g", " 1.401298464325e-45", Float.MIN_VALUE);
test("%30.20g", " 1.4012984643248170000e-45", Float.MIN_VALUE);
test("%a", "0x1.0p-149", Float.MIN_VALUE);
test("%A", "0X1.0P-149", Float.MIN_VALUE);
test("%20a", " 0x1.0p-149", Float.MIN_VALUE);
//---------------------------------------------------------------------
// %f, %e, %g, %a - Float.MAX_VALUE
//---------------------------------------------------------------------
test("%f", "340282346638528860000000000000000000000.000000", Float.MAX_VALUE);
test("%,f","340,282,346,638,528,860,000,000,000,000,000,000,000.000000",
Float.MAX_VALUE);
test("%(f", "(340282346638528860000000000000000000000.000000)", -Float.MAX_VALUE);
test("%60.5f", " 340282346638528860000000000000000000000.00000",
Float.MAX_VALUE);
test("%60.13f", " 340282346638528860000000000000000000000.0000000000000",
Float.MAX_VALUE);
test("%61.20f", " 340282346638528860000000000000000000000.00000000000000000000",
Float.MAX_VALUE);
test("%e", "3.402823e+38", Float.MAX_VALUE);
test("%E", "3.402823E+38", Float.MAX_VALUE);
test("%(e", "3.402823e+38", Float.MAX_VALUE);
test("%(e", "(3.402823e+38)", -Float.MAX_VALUE);
test("%30.5e", " 3.40282e+38", Float.MAX_VALUE);
test("%30.13e", " 3.4028234663853e+38", Float.MAX_VALUE);
test("%30.20e", " 3.40282346638528860000e+38", Float.MAX_VALUE);
test("%g", "3.40282e+38", Float.MAX_VALUE);
test("%G", "3.40282E+38", Float.MAX_VALUE);
test("%,g", "3.40282e+38", Float.MAX_VALUE);
test("%(g", "(3.40282e+38)", -Float.MAX_VALUE);
test("%30.5g", " 3.4028e+38", Float.MAX_VALUE);
test("%30.13g", " 3.402823466385e+38", Float.MAX_VALUE);
test("%30.20G", " 3.4028234663852886000E+38", Float.MAX_VALUE);
test("%a", "0x1.fffffep127", Float.MAX_VALUE);
test("%A", "0X1.FFFFFEP127", Float.MAX_VALUE);
test("%20a"," 0x1.fffffep127", Float.MAX_VALUE);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,580 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicFloatObject extends Basic {
private static Float create(double v) {
return (float) v;
}
private static Float negate(Float v) {
return -v.floatValue();
}
private static Float mult(Float v, double mul) {
return v.floatValue() * (float) mul;
}
private static Float recip(Float v) {
return 1.0f / v.floatValue();
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %s - Float
//---------------------------------------------------------------------
Float one = 1.0f;
Float ten = 10.0f;
Float pi = (float) Math.PI;
test("%s", "3.1415927", pi);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%d", IllegalFormatConversionException.class, one);
tryCatch("%,.4e", FormatFlagsConversionMismatchException.class, one);
//---------------------------------------------------------------------
// %e
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%e", "null", (Object)null);
//---------------------------------------------------------------------
// %e - float and double
//---------------------------------------------------------------------
// double PI = 3.141 592 653 589 793 238 46;
test("%e", "3.141593e+00", pi);
test("%.0e", "1e+01", ten);
test("%#.0e", "1.e+01", ten);
test("%E", "3.141593E+00", pi);
test("%10.3e", " 3.142e+00", pi);
test("%10.3e", "-3.142e+00", negate(pi));
test("%010.3e", "03.142e+00", pi);
test("%010.3e", "-3.142e+00", negate(pi));
test("%-12.3e", "3.142e+00 ", pi);
test("%-12.3e", "-3.142e+00 ", negate(pi));
test("%.3e", "3.142e+00", pi);
test("%.3e", "-3.142e+00", negate(pi));
test("%.3e", "3.142e+06", mult(pi, 1000000.0));
test("%.3e", "-3.142e+06", mult(pi, -1000000.0));
test(Locale.FRANCE, "%e", "3,141593e+00", pi);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%10.3e", " 1.000e+00", one);
test("%+.3e", "+3.142e+00", pi);
test("%+.3e", "-3.142e+00", negate(pi));
test("% .3e", " 3.142e+00", pi);
test("% .3e", "-3.142e+00", negate(pi));
test("%#.0e", "3.e+00", create(3.0));
test("%#.0e", "-3.e+00", create(-3.0));
test("%.0e", "3e+00", create(3.0));
test("%.0e", "-3e+00", create(-3.0));
test("%(.4e", "3.1416e+06", mult(pi, 1000000.0));
test("%(.4e", "(3.1416e+06)", mult(pi, -1000000.0));
//---------------------------------------------------------------------
// %e - boundary problems
//---------------------------------------------------------------------
test("%3.0e", "1e-06", 0.000001);
test("%3.0e", "1e-05", 0.00001);
test("%3.0e", "1e-04", 0.0001);
test("%3.0e", "1e-03", 0.001);
test("%3.0e", "1e-02", 0.01);
test("%3.0e", "1e-01", 0.1);
test("%3.0e", "9e-01", 0.9);
test("%3.1e", "9.0e-01", 0.9);
test("%3.0e", "1e+00", 1.00);
test("%3.0e", "1e+01", 10.00);
test("%3.0e", "1e+02", 99.19);
test("%3.1e", "9.9e+01", 99.19);
test("%3.0e", "1e+02", 99.99);
test("%3.0e", "1e+02", 100.00);
test("%#3.0e", "1.e+03", 1000.00);
test("%3.0e", "1e+04", 10000.00);
test("%3.0e", "1e+05", 100000.00);
test("%3.0e", "1e+06", 1000000.00);
test("%3.0e", "1e+07", 10000000.00);
test("%3.0e", "1e+08", 100000000.00);
//---------------------------------------------------------------------
// %e - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.1e", "9.9e-324", 1e-323);
test("%.1e", "9.9e-323", 1e-322);
test("%.15e", "7.387900000000000e+20", 7.3879e20);
test("%.15e", "1.000000000000000e+23", 1e23);
test("%.16e", "2.0000000000000000e+23", 2e23);
test("%.16e", "1.9400994884341945e+25", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %f
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%f", "null", (Object)null);
test("%f", "3.141593", pi);
test(Locale.FRANCE, "%f", "3,141593", pi);
test("%10.3f", " 3.142", pi);
test("%10.3f", " -3.142", negate(pi));
test("%010.3f", "000003.142", pi);
test("%010.3f", "-00003.142", negate(pi));
test("%-10.3f", "3.142 ", pi);
test("%-10.3f", "-3.142 ", negate(pi));
test("%.3f", "3.142", pi);
test("%.3f", "-3.142", negate(pi));
test("%+.3f", "+3.142", pi);
test("%+.3f", "-3.142", negate(pi));
test("% .3f", " 3.142", pi);
test("% .3f", "-3.142", negate(pi));
test("%#.0f", "3.", create(3.0));
test("%#.0f", "-3.", create(-3.0));
test("%.0f", "3", create(3.0));
test("%.0f", "-3", create(-3.0));
test("%.3f", "10.000", ten);
test("%.3f", "1.000", one);
test("%10.3f", " 1.000", one);
//---------------------------------------------------------------------
// %f - boundary problems
//---------------------------------------------------------------------
test("%3.0f", " 0", 0.000001);
test("%3.0f", " 0", 0.00001);
test("%3.0f", " 0", 0.0001);
test("%3.0f", " 0", 0.001);
test("%3.0f", " 0", 0.01);
test("%3.0f", " 0", 0.1);
test("%3.0f", " 1", 0.9);
test("%3.1f", "0.9", 0.9);
test("%3.0f", " 1", 1.00);
test("%3.0f", " 10", 10.00);
test("%3.0f", " 99", 99.19);
test("%3.1f", "99.2", 99.19);
test("%3.0f", "100", 99.99);
test("%3.0f", "100", 100.00);
test("%#3.0f", "1000.", 1000.00);
test("%3.0f", "10000", 10000.00);
test("%3.0f", "100000", 100000.00);
test("%3.0f", "1000000", 1000000.00);
test("%3.0f", "10000000", 10000000.00);
test("%3.0f", "100000000", 100000000.00);
test("%10.0f", " 1000000", 1000000.00);
test("%,10.0f", " 1,000,000", 1000000.00);
test("%,10.1f", "1,000,000.0", 1000000.00);
test("%,3.0f", "1,000,000", 1000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
test("%,3.0f", "10,000,000", 10000000.00);
test("%,3.0f", "100,000,000", 100000000.00);
//---------------------------------------------------------------------
// %f - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.0f", "738790000000000000000", 7.3879e20);
test("%.0f", "100000000000000000000000", 1e23);
test("%.0f", "200000000000000000000000", 2e23);
test("%.0f", "19400994884341945000000000", 1.9400994884341945E25);
//---------------------------------------------------------------------
// %g
//
// Floating-point conversions applicable to float, double, and
// BigDecimal.
//---------------------------------------------------------------------
test("%g", "null", (Object)null);
test("%g", "3.14159", pi);
test(Locale.FRANCE, "%g", "3,14159", pi);
test("%.0g", "1e+01", ten);
test("%G", "3.14159", pi);
test("%10.3g", " 3.14", pi);
test("%10.3g", " -3.14", negate(pi));
test("%010.3g", "0000003.14", pi);
test("%010.3g", "-000003.14", negate(pi));
test("%-12.3g", "3.14 ", pi);
test("%-12.3g", "-3.14 ", negate(pi));
test("%.3g", "3.14", pi);
test("%.3g", "-3.14", negate(pi));
test("%.3g", "3.14e+08", mult(pi, 100000000.0));
test("%.3g", "-3.14e+08", mult(pi, -100000000.0));
test("%.3g", "1.00e-05", recip(create(100000.0)));
test("%.3g", "-1.00e-05", recip(create(-100000.0)));
test("%.0g", "-1e-05", recip(create(-100000.0)));
test("%.0g", "1e+05", create(100000.0));
test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099);
test("%3.1g", "1e-05", 0.0000099);
test("%3.2g", "9.9e-06", 0.0000099);
test("%3.0g", "0.0001", 0.0001);
test("%3.0g", "9e-05", 0.00009);
test("%3.0g", "0.0001", 0.000099);
test("%3.1g", "0.0001", 0.000099);
test("%3.2g", "9.9e-05", 0.000099);
test("%3.0g", "0.001", 0.001);
test("%3.0g", "0.001", 0.00099);
test("%3.1g", "0.001", 0.00099);
test("%3.2g", "0.00099", 0.00099);
test("%3.3g", "0.00100", 0.001);
test("%3.4g", "0.001000", 0.001);
test("%3.0g", "0.01", 0.01);
test("%3.0g", "0.1", 0.1);
test("%3.0g", "0.9", 0.9);
test("%3.1g", "0.9", 0.9);
test("%3.0g", " 1", 1.00);
test("%3.2g", " 10", 10.00);
test("%3.0g", "1e+01", 10.00);
test("%3.0g", "1e+02", 99.19);
test("%3.1g", "1e+02", 99.19);
test("%3.2g", " 99", 99.19);
test("%3.0g", "1e+02", 99.9);
test("%3.1g", "1e+02", 99.9);
test("%3.2g", "1.0e+02", 99.9);
test("%3.0g", "1e+02", 99.99);
test("%3.0g", "1e+02", 100.00);
test("%3.0g", "1e+03", 999.9);
test("%3.1g", "1e+03", 999.9);
test("%3.2g", "1.0e+03", 999.9);
test("%3.3g", "1.00e+03", 999.9);
test("%3.4g", "999.9", 999.9);
test("%3.4g", "1000", 999.99);
test("%3.0g", "1e+03", 1000.00);
test("%3.0g", "1e+04", 10000.00);
test("%3.0g", "1e+05", 100000.00);
test("%3.0g", "1e+06", 1000000.00);
test("%3.0g", "1e+07", 10000000.00);
test("%3.9g", "100000000", 100000000.00);
test("%3.10g", "100000000.0", 100000000.00);
tryCatch("%#3.0g", FormatFlagsConversionMismatchException.class, 1000.00);
//---------------------------------------------------------------------
// %g - adoption of Double.toString(double) algorithm
//---------------------------------------------------------------------
test("%.2g", "9.9e-324", 1e-323);
test("%.2g", "9.9e-323", 1e-322);
test("%.16g", "7.387900000000000e+20", 7.3879e20);
test("%.16g", "1.000000000000000e+23", 1e23);
test("%.17g", "2.0000000000000000e+23", 2e23);
test("%.17g", "1.9400994884341945e+25", 1.9400994884341945E25);
// double PI^300
// = 13962455701329742638131355433930076081862072808 ... e+149
test("%.3g", "10.0", ten);
test("%.3g", "1.00", one);
test("%10.3g", " 1.00", one);
test("%+10.3g", " +3.14", pi);
test("%+10.3g", " -3.14", negate(pi));
test("% .3g", " 3.14", pi);
test("% .3g", "-3.14", negate(pi));
test("%.0g", "3", create(3.0));
test("%.0g", "-3", create(-3.0));
test("%(.4g", "3.142e+08", mult(pi, 100000000.0));
test("%(.4g", "(3.142e+08)", mult(pi, -100000000.0));
//---------------------------------------------------------------------
// %f, %e, %g, %a - Boundaries
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,403 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicInt extends Basic {
private static int negate(int v) {
return (int) -v;
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
int minByte = Byte.MIN_VALUE; // -128
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - int and long
//---------------------------------------------------------------------
int oneToSeven = (int) 1234567;
test("%d", "1234567", oneToSeven);
test("%,d", "1,234,567", oneToSeven);
test(Locale.FRANCE, "%,d", "1\u202f234\u202f567", oneToSeven);
test("%,d", "-1,234,567", negate(oneToSeven));
test("%(d", "1234567", oneToSeven);
test("%(d", "(1234567)", negate(oneToSeven));
test("% d", " 1234567", oneToSeven);
test("% d", "-1234567", negate(oneToSeven));
test("%+d", "+1234567", oneToSeven);
test("%+d", "-1234567", negate(oneToSeven));
test("%010d", "0001234567", oneToSeven);
test("%010d", "-001234567", negate(oneToSeven));
test("%(10d", " (1234567)", negate(oneToSeven));
test("%-10d", "1234567 ", oneToSeven);
test("%-10d", "-1234567 ", negate(oneToSeven));
// , variations:
test("% ,d", " 1,234,567", oneToSeven);
test("% ,d", "-1,234,567", negate(oneToSeven));
test("%+,10d", "+1,234,567", oneToSeven);
test("%0,10d", "01,234,567", oneToSeven);
test("%0,10d", "-1,234,567", negate(oneToSeven));
test("%(,10d", "(1,234,567)", negate(oneToSeven));
test("%-,10d", "1,234,567 ", oneToSeven);
test("%-,10d", "-1,234,567", negate(oneToSeven));
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - int
//---------------------------------------------------------------------
test("%014o", "00037777777600", minByte);
test("%-14o", "37777777600 ", minByte);
test("%#14o", " 037777777600", minByte);
int oneToSevenOct = (int) 1234567;
test("%o", "4553207", oneToSevenOct);
test("%010o", "0004553207", oneToSevenOct);
test("%-10o", "4553207 ", oneToSevenOct);
test("%#10o", " 04553207", oneToSevenOct);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - int
//---------------------------------------------------------------------
int oneToSevenHex = (int)1234567;
test("%x", "null", (Object)null);
test("%x", "12d687", oneToSevenHex);
test("%010x", "000012d687", oneToSevenHex);
test("%-10x", "12d687 ", oneToSevenHex);
test("%#10x", " 0x12d687", oneToSevenHex);
test("%#10X", " 0X12D687",oneToSevenHex);
test("%X", "12D687", oneToSevenHex);
test("%010x", "00ffffff80", minByte);
test("%-10x", "ffffff80 ", minByte);
test("%#10x", "0xffffff80", minByte);
test("%0#12x","0x00ffffff80", minByte);
test("%#12X", " 0XFFFFFF80", minByte);
test("%X", "FFFFFF80", minByte);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,344 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicIntObject extends Basic {
private static Integer negate(Integer v) {
return -v.intValue();
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
Integer minByte = (int) Byte.MIN_VALUE;
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,403 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicLong extends Basic {
private static long negate(long v) {
return (long) -v;
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
long minByte = Byte.MIN_VALUE; // -128
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - int and long
//---------------------------------------------------------------------
long oneToSeven = (long) 1234567;
test("%d", "1234567", oneToSeven);
test("%,d", "1,234,567", oneToSeven);
test(Locale.FRANCE, "%,d", "1\u202f234\u202f567", oneToSeven);
test("%,d", "-1,234,567", negate(oneToSeven));
test("%(d", "1234567", oneToSeven);
test("%(d", "(1234567)", negate(oneToSeven));
test("% d", " 1234567", oneToSeven);
test("% d", "-1234567", negate(oneToSeven));
test("%+d", "+1234567", oneToSeven);
test("%+d", "-1234567", negate(oneToSeven));
test("%010d", "0001234567", oneToSeven);
test("%010d", "-001234567", negate(oneToSeven));
test("%(10d", " (1234567)", negate(oneToSeven));
test("%-10d", "1234567 ", oneToSeven);
test("%-10d", "-1234567 ", negate(oneToSeven));
// , variations:
test("% ,d", " 1,234,567", oneToSeven);
test("% ,d", "-1,234,567", negate(oneToSeven));
test("%+,10d", "+1,234,567", oneToSeven);
test("%0,10d", "01,234,567", oneToSeven);
test("%0,10d", "-1,234,567", negate(oneToSeven));
test("%(,10d", "(1,234,567)", negate(oneToSeven));
test("%-,10d", "1,234,567 ", oneToSeven);
test("%-,10d", "-1,234,567", negate(oneToSeven));
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - long
//---------------------------------------------------------------------
test("%024o", "001777777777777777777600", minByte);
test("%-24o", "1777777777777777777600 ", minByte);
test("%#24o", " 01777777777777777777600", minByte);
long oneToSevenOct = (long) 1234567;
test("%o", "4553207", oneToSevenOct);
test("%010o", "0004553207", oneToSevenOct);
test("%-10o", "4553207 ", oneToSevenOct);
test("%#10o", " 04553207", oneToSevenOct);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - long
//---------------------------------------------------------------------
long oneToSevenHex = (long)1234567;
test("%x", "null", (Object)null);
test("%x", "12d687", oneToSevenHex);
test("%010x", "000012d687", oneToSevenHex);
test("%-10x", "12d687 ", oneToSevenHex);
test("%#10x", " 0x12d687", oneToSevenHex);
test("%#10X", " 0X12D687",oneToSevenHex);
test("%X", "12D687", oneToSevenHex);
test("%018x", "00ffffffffffffff80", minByte);
test("%-18x", "ffffffffffffff80 ", minByte);
test("%#20x", " 0xffffffffffffff80", minByte);
test("%0#20x", "0x00ffffffffffffff80", minByte);
test("%#20X", " 0XFFFFFFFFFFFFFF80", minByte);
test("%X", "FFFFFFFFFFFFFF80", minByte);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,344 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicLongObject extends Basic {
private static Long negate(Long v) {
return -v.longValue();
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
Long minByte = (long) Byte.MIN_VALUE;
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,387 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicShort extends Basic {
private static short negate(short v) {
return (short) -v;
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
short minByte = Byte.MIN_VALUE; // -128
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - short
//---------------------------------------------------------------------
short oneToFive = (short) 12345;
test("%d", "12345", oneToFive);
test("%,d", "12,345", oneToFive);
test("%,d", "-12,345", negate(oneToFive));
test("%(d", "12345", oneToFive);
test("%(d", "(12345)", negate(oneToFive));
test("% d", " 12345", oneToFive);
test("% d", "-12345", negate(oneToFive));
test("%+d", "+12345", oneToFive);
test("%+d", "-12345", negate(oneToFive));
test("%010d", "0000012345", oneToFive);
test("%010d", "-000012345", negate(oneToFive));
test("%(10d", " (12345)", negate(oneToFive));
test("%-10d", "12345 ", oneToFive);
test("%-10d", "-12345 ", negate(oneToFive));
// , variations:
test("% ,d", " 12,345", oneToFive);
test("% ,d", "-12,345", negate(oneToFive));
test("%0,10d", "000012,345", oneToFive);
test("%0,10d", "-00012,345", negate(oneToFive));
test("%(,10d", " (12,345)", negate(oneToFive));
test("%-,10d", "12,345 ", oneToFive);
test("%-,10d", "-12,345 ", negate(oneToFive));
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - short
//---------------------------------------------------------------------
test("%010o", "0000177600", minByte);
test("%-10o", "177600 ", minByte);
test("%#10o", " 0177600", minByte);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - short
//---------------------------------------------------------------------
test("%010x", "000000ff80", minByte);
test("%-10x", "ff80 ", minByte);
test("%#10x", " 0xff80", minByte);
test("%0#10x","0x0000ff80", minByte);
test("%#10X", " 0XFF80", minByte);
test("%X", "FF80", minByte);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,344 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* Type-specific source code for unit test
*
* Regenerate the BasicX classes via genBasic.sh whenever this file changes.
* We check in the generated source files so that the test tree can be used
* independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.*;
import static java.util.Calendar.*;
public class BasicShortObject extends Basic {
private static Short negate(Short v) {
return (short) -v.shortValue();
}
public static void test() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT-0800"));
// Any characters not explicitly defined as conversions, date/time
// conversion suffixes, or flags are illegal and are reserved for
// future extensions. Use of such a character in a format string will
// cause an UnknownFormatConversionException or
// UnknownFormatFlagsException to be thrown.
tryCatch("%q", UnknownFormatConversionException.class);
tryCatch("%t&", UnknownFormatConversionException.class);
tryCatch("%&d", UnknownFormatConversionException.class);
tryCatch("%^b", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// Formatter.java class javadoc examples
//---------------------------------------------------------------------
test(Locale.FRANCE, "e = %+10.4f", "e = +2,7183", Math.E);
test("%4$2s %3$2s %2$2s %1$2s", " d c b a", "a", "b", "c", "d");
test("Amount gained or lost since last statement: $ %,(.2f",
"Amount gained or lost since last statement: $ (6,217.58)",
(new BigDecimal("-6217.58")));
Calendar c = new GregorianCalendar(1969, JULY, 20, 16, 17, 0);
testSysOut("Local time: %tT", "Local time: 16:17:00", c);
test("Unable to open file '%1$s': %2$s",
"Unable to open file 'food': No such file or directory",
"food", "No such file or directory");
Calendar duke = new GregorianCalendar(1995, MAY, 23, 19, 48, 34);
duke.set(Calendar.MILLISECOND, 584);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke);
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTime());
test("Duke's Birthday: %1$tB %1$te, %1$tY",
"Duke's Birthday: May 23, 1995",
duke.getTimeInMillis());
test("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s",
"d c b a d c b a", "a", "b", "c", "d");
test("%s %s %<s %<s", "a b b b", "a", "b", "c", "d");
test("%s %s %s %s", "a b c d", "a", "b", "c", "d");
test("%2$s %s %<s %s", "b a a b", "a", "b", "c", "d");
//---------------------------------------------------------------------
// %b
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%b", "true", true);
test("%b", "false", false);
test("%B", "TRUE", true);
test("%B", "FALSE", false);
test("%b", "true", Boolean.TRUE);
test("%b", "false", Boolean.FALSE);
test("%B", "TRUE", Boolean.TRUE);
test("%B", "FALSE", Boolean.FALSE);
test("%14b", " true", true);
test("%-14b", "true ", true);
test("%5.1b", " f", false);
test("%-5.1b", "f ", false);
test("%b", "true", "foo");
test("%b", "false", (Object)null);
// Boolean.java hardcodes the Strings for "true" and "false", so no
// localization is possible.
test(Locale.FRANCE, "%b", "true", true);
test(Locale.FRANCE, "%b", "false", false);
// If you pass in a single array to a varargs method, the compiler
// uses it as the array of arguments rather than treating it as a
// single array-type argument.
test("%b", "false", (Object[])new String[2]);
test("%b", "true", new String[2], new String[2]);
int [] ia = { 1, 2, 3 };
test("%b", "true", ia);
//---------------------------------------------------------------------
// %b - errors
//---------------------------------------------------------------------
tryCatch("%#b", FormatFlagsConversionMismatchException.class);
tryCatch("%-b", MissingFormatWidthException.class);
// correct or side-effect of implementation?
tryCatch("%.b", UnknownFormatConversionException.class);
tryCatch("%,b", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// %c
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%c", "i", 'i');
test("%C", "I", 'i');
test("%4c", " i", 'i');
test("%-4c", "i ", 'i');
test("%4C", " I", 'i');
test("%-4C", "I ", 'i');
test("%c", "i", Character.valueOf('i'));
test("%c", "H", (byte) 72);
test("%c", "i", (short) 105);
test("%c", "!", (int) 33);
test("%c", "\u007F", Byte.MAX_VALUE);
test("%c", new String(Character.toChars(Short.MAX_VALUE)),
Short.MAX_VALUE);
test("%c", "null", (Object) null);
//---------------------------------------------------------------------
// %c - errors
//---------------------------------------------------------------------
tryCatch("%c", IllegalFormatConversionException.class,
Boolean.TRUE);
tryCatch("%c", IllegalFormatConversionException.class,
(float) 0.1);
tryCatch("%c", IllegalFormatConversionException.class,
new Object());
tryCatch("%c", IllegalFormatCodePointException.class,
Byte.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Short.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MIN_VALUE);
tryCatch("%c", IllegalFormatCodePointException.class,
Integer.MAX_VALUE);
tryCatch("%#c", FormatFlagsConversionMismatchException.class);
tryCatch("%,c", FormatFlagsConversionMismatchException.class);
tryCatch("%(c", FormatFlagsConversionMismatchException.class);
tryCatch("%$c", UnknownFormatConversionException.class);
tryCatch("%.2c", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %s
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%s", "Hello, Duke", "Hello, Duke");
test("%S", "HELLO, DUKE", "Hello, Duke");
test("%20S", " HELLO, DUKE", "Hello, Duke");
test("%20s", " Hello, Duke", "Hello, Duke");
test("%-20s", "Hello, Duke ", "Hello, Duke");
test("%-20.5s", "Hello ", "Hello, Duke");
test("%s", "null", (Object)null);
StringBuffer sb = new StringBuffer("foo bar");
test("%s", sb.toString(), sb);
test("%S", sb.toString().toUpperCase(), sb);
//---------------------------------------------------------------------
// %s - errors
//---------------------------------------------------------------------
tryCatch("%-s", MissingFormatWidthException.class);
tryCatch("%--s", DuplicateFormatFlagsException.class);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, 0.5f);
tryCatch("%#s", FormatFlagsConversionMismatchException.class, "hello");
tryCatch("%#s", FormatFlagsConversionMismatchException.class, (Object)null);
//---------------------------------------------------------------------
// %h
//
// General conversion applicable to any argument.
//---------------------------------------------------------------------
test("%h", Integer.toHexString("Hello, Duke".hashCode()),
"Hello, Duke");
test("%10h", " ddf63471", "Hello, Duke");
test("%-10h", "ddf63471 ", "Hello, Duke");
test("%-10H", "DDF63471 ", "Hello, Duke");
test("%10h", " 402e0000", 15.0);
test("%10H", " 402E0000", 15.0);
//---------------------------------------------------------------------
// %h - errors
//---------------------------------------------------------------------
tryCatch("%#h", FormatFlagsConversionMismatchException.class);
//---------------------------------------------------------------------
// flag/conversion errors
//---------------------------------------------------------------------
tryCatch("%F", UnknownFormatConversionException.class);
tryCatch("%#g", FormatFlagsConversionMismatchException.class);
Short minByte = (short) Byte.MIN_VALUE;
//---------------------------------------------------------------------
// %d
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%d", "null", (Object)null);
//---------------------------------------------------------------------
// %d - errors
//---------------------------------------------------------------------
tryCatch("%#d", FormatFlagsConversionMismatchException.class);
tryCatch("%D", UnknownFormatConversionException.class);
tryCatch("%0d", MissingFormatWidthException.class);
tryCatch("%-d", MissingFormatWidthException.class);
tryCatch("%7.3d", IllegalFormatPrecisionException.class);
//---------------------------------------------------------------------
// %o
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%o", "null", (Object)null);
//---------------------------------------------------------------------
// %o - errors
//---------------------------------------------------------------------
tryCatch("%(o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%+o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("% o", FormatFlagsConversionMismatchException.class,
minByte);
tryCatch("%0o", MissingFormatWidthException.class);
tryCatch("%-o", MissingFormatWidthException.class);
tryCatch("%,o", FormatFlagsConversionMismatchException.class);
tryCatch("%O", UnknownFormatConversionException.class);
//---------------------------------------------------------------------
// %x
//
// Numeric conversion applicable to byte, short, int, long, and
// BigInteger.
//---------------------------------------------------------------------
test("%x", "null", (Object)null);
//---------------------------------------------------------------------
// %x - errors
//---------------------------------------------------------------------
tryCatch("%,x", FormatFlagsConversionMismatchException.class);
tryCatch("%0x", MissingFormatWidthException.class);
tryCatch("%-x", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %t
//
// Date/Time conversions applicable to Calendar, Date, and long.
//---------------------------------------------------------------------
test("%tA", "null", (Object)null);
test("%TA", "NULL", (Object)null);
//---------------------------------------------------------------------
// %t - errors
//---------------------------------------------------------------------
tryCatch("%t", UnknownFormatConversionException.class);
tryCatch("%T", UnknownFormatConversionException.class);
tryCatch("%tP", UnknownFormatConversionException.class);
tryCatch("%TP", UnknownFormatConversionException.class);
tryCatch("%.5tB", IllegalFormatPrecisionException.class);
tryCatch("%#tB", FormatFlagsConversionMismatchException.class);
tryCatch("%-tB", MissingFormatWidthException.class);
//---------------------------------------------------------------------
// %n
//---------------------------------------------------------------------
test("%n", System.getProperty("line.separator"), (Object)null);
test("%n", System.getProperty("line.separator"), "");
tryCatch("%,n", IllegalFormatFlagsException.class);
tryCatch("%.n", UnknownFormatConversionException.class);
tryCatch("%5.n", UnknownFormatConversionException.class);
tryCatch("%5n", IllegalFormatWidthException.class);
tryCatch("%.7n", IllegalFormatPrecisionException.class);
tryCatch("%<n", IllegalFormatFlagsException.class);
//---------------------------------------------------------------------
// %%
//---------------------------------------------------------------------
test("%%", "%", (Object)null);
test("%%", "%", "");
test("%5%", " %", (Object)null);
test("%5%", " %", "");
test("%-5%", "% ", (Object)null);
test("%-5%", "% ", "");
tryCatch("%.5%", IllegalFormatPrecisionException.class);
tryCatch("%5.5%", IllegalFormatPrecisionException.class);
tryCatch("%%%", UnknownFormatConversionException.class);
// perhaps an IllegalFormatArgumentIndexException should be defined?
tryCatch("%<%", IllegalFormatFlagsException.class);
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import java.io.IOException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
/* @test
* @summary Unit tests for formatter
* @library /test/lib
* @compile Basic.java
* @compile BasicBoolean.java
* @compile BasicBooleanObject.java
* @compile BasicByte.java
* @compile BasicByteObject.java
* @compile BasicChar.java
* @compile BasicCharObject.java
* @compile BasicShort.java
* @compile BasicShortObject.java
* @compile BasicInt.java
* @compile BasicIntObject.java
* @compile BasicLong.java
* @compile BasicLongObject.java
* @compile BasicBigInteger.java
* @compile BasicFloat.java
* @compile BasicFloatObject.java
* @compile BasicDouble.java
* @compile BasicDoubleObject.java
* @compile BasicBigDecimal.java
* @compile BasicDateTime.java
* @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937
* 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122
* 6344623 6369500 6534606 6282094 6286592 6476425 5063507 6469160 6476168
* 8059175 8204229 8300869
*
* @run junit BasicTestLauncher
*/
public class BasicTestLauncher {
// Test class
private static final String TEST_CLASS = "Basic";
/**
* Executes Formatter Basic tests
* @param timeZone the time zone to run tests against
*/
@ParameterizedTest
@ValueSource(strings = { "US/Pacific", "Asia/Novosibirsk" })
void testTimeZone(String timeZone) throws IOException{
System.out.printf("$$$ Testing against %s!%n", timeZone);
OutputAnalyzer output = RunTest(timeZone);
CheckTest(output);
System.out.printf("$$$ %s passed as expected!%n", timeZone);
}
/**
* Creates and runs the testJVM process using Basic class
* @param timeZone the time zone to be set in the testJVM environment
*/
private static OutputAnalyzer RunTest(String timeZone) throws IOException{
// Build and run Basic class with correct configuration
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(TEST_CLASS);
pb.environment().put("TZ", timeZone);
Process process = pb.start();
return new OutputAnalyzer(process);
}
/**
* Validates if the testJVM process passed all tests
* @param output is an Output Analyzer for the testJVM
* @throws RuntimeException for all testJVM failures
*/
private static void CheckTest(OutputAnalyzer output){
output.shouldHaveExitValue(0)
.reportDiagnosticSummary();
}
}

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8262744
* @summary BigDecimal does not always display formatting correctly because
* rounding is done after formatting check. Fix moves rounding to before the
* range-based formatting check for %g formatting flag.
* @run testng BigDecimalRounding
*/
import org.testng.annotations.Test;
import java.math.BigDecimal;
import static org.testng.Assert.*;
@Test
public class BigDecimalRounding {
public static void testBigDecimalRounding() {
var res1 = String.format("%g", 0.00009999999999999995);
var res2 = String.format("%g", 0.00009999999f);
var res3 = String.format("%g", new BigDecimal(0.0001));
var res4 = String.format("%g", new BigDecimal("0.00009999999999999999995"));
assertEquals(res1, res2);
assertEquals(res2, res3);
assertEquals(res3, res4);
var res5 = String.format("%.9g", 999999.999999432168754e+3);
var res6 = String.format("%.9g", 999999999.999432168754f);
var res7 = String.format("%.9g", new BigDecimal("999999.999999432168754e+3")); // !!
var res8 = String.format("%.9g", new BigDecimal("1000000000")); // !!
assertEquals(res5, res6);
assertEquals(res6, res7);
assertEquals(res7, res8);
}
}

View file

@ -0,0 +1,97 @@
/*
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 5030064
* @summary Basic tests for close().
*/
import java.io.Closeable;
import java.util.Formatter;
import java.util.FormatterClosedException;
public class Close {
private static class ExpectedException extends RuntimeException {}
private static class C implements Appendable, Closeable {
public Appendable append(CharSequence csq) { return null; }
public Appendable append(char c) { return null; }
public Appendable append(CharSequence csq, int s, int e) {
return null;
}
public void close() {
throw new ExpectedException();
}
}
private static class NC implements Appendable {
public Appendable append(CharSequence csq) { return null; }
public Appendable append(char c) { return null; }
public Appendable append(CharSequence csq, int s, int e) {
return null;
}
// method coincidentally called close()
public void close() {
throw new RuntimeException("NC.close should not be called");
}
}
private static void test(Formatter f) {
if (f.out() instanceof C) {
// C.close() called since C implements Closeable
try {
f.close();
throw new RuntimeException("C.close not called");
} catch (ExpectedException x) {
System.out.println(" C.close called");
}
} else {
// NC.close() not called since NC does not implement Closeable
f.close();
}
// Formatter is a Closeable
if (!(f instanceof Closeable))
throw new RuntimeException("Formatter is not a Closeable");
// multiple close() does not throw a FormatterClosedException
f.close();
try {
f.close();
System.out.println(" FormatterClosedException not thrown");
} catch (FormatterClosedException x) {
throw new RuntimeException("FormatterClosedException thrown");
}
}
public static void main(String [] args) {
System.out.println("testing Closeable");
test(new Formatter(new C()));
System.out.println("testing non-Closeable");
test(new Formatter(new NC()));
}
}

View file

@ -0,0 +1,557 @@
/*
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4981811 4984465 5064492 6240171 7000511
* @summary Unit test for all constructors introduced by the formatter feature
*/
import java.io.*;
import java.util.*;
import java.nio.charset.Charset;
public class Constructors {
private static int fail = 0;
private static int pass = 0;
private static Throwable first;
static void pass() {
pass++;
}
static void fail(String fs) {
String s = "'" + fs + "': exception not thrown";
if (first == null)
first = new RuntimeException(s);
System.err.println("FAILED: " + s);
fail++;
}
static void fail(String fs, Throwable ex) {
String s = "'" + fs + "': " + ex.getClass().getName() + " thrown";
if (first == null)
first = ex;
System.err.println("FAILED: " + s);
fail++;
}
static void locale(Formatter f) {
locale(f, Locale.getDefault(Locale.Category.FORMAT));
}
static void locale(Formatter f, Locale l) {
try {
if ((l != null && !l.equals(f.locale()))
|| (l == null && f.locale() != null))
throw new RuntimeException(f.locale() + " != " + l);
pass();
} catch (RuntimeException x) {
fail(x.getMessage());
}
}
static void out(Formatter f, Class c) {
try {
Appendable a = f.out();
if (!c.isInstance(a))
throw new RuntimeException(a.getClass().getName()
+ " != " + c.getName());
pass();
} catch (RuntimeException x) {
fail(x.getMessage());
}
}
public static void main(String [] args) {
// Formatter()
try (Formatter f = new Formatter()) {
pass();
out(f, StringBuilder.class);
locale(f);
} catch (Exception x) {
fail("new Formatter()", x);
}
// Formatter(Appendable a)
try (Formatter f = new Formatter((Appendable) null)) {
pass();
out(f, StringBuilder.class);
locale(f);
} catch (Exception x) {
fail("new Formatter((Appendable)null)", x);
}
// Formatter(Locale l)
try (Formatter f = new Formatter((Locale) null)) {
pass();
out(f, StringBuilder.class);
locale(f, null);
} catch (Exception x) {
fail("new Formatter((Locale)null)", x);
}
// Formatter(Appendable a, Locale l)
try (Formatter f = new Formatter((Appendable) null, (Locale) null)) {
pass();
out(f, StringBuilder.class);
locale(f, null);
} catch (Exception x) {
fail("new Formatter((Appendable) null, (Locale) null)", x);
}
// Formatter(String fileName)
try (Formatter f = new Formatter("foo")) {
pass();
out(f, BufferedWriter.class);
locale(f);
} catch (Exception x) {
fail("new Formatter(\"foo\")", x);
}
try {
new Formatter((String)null);
fail("new Formatter((String)null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter((String)null)", x);
}
// Formatter(String fileName, String csn)
try (Formatter f = new Formatter("foo", "UTF-8")) {
pass();
out(f, BufferedWriter.class);
locale(f);
} catch (Exception x) {
fail("new Formatter(\"foo\", \"UTF-8\")", x);
}
try {
new Formatter("foo", "bar");
fail("new Formatter(\"foo\", \"bar\")");
} catch (UnsupportedEncodingException x) {
pass();
} catch (Exception x) {
fail("new Formatter(\"foo\", \"bar\")", x);
}
try {
new Formatter(".", "bar");
fail("new Formatter(\".\", \"bar\")");
} catch (FileNotFoundException|UnsupportedEncodingException x) {
pass();
} catch (Exception x) {
fail("new Formatter(\".\", \"bar\")", x);
}
// Formatter(String fileName, String csn, Locale l)
try (Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY)) {
pass();
out(f, BufferedWriter.class);
locale(f, Locale.GERMANY);
} catch (Exception x) {
fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x);
}
try (Formatter f = new Formatter("foo", "ISO-8859-1", null)) {
pass();
locale(f, null);
out(f, BufferedWriter.class);
} catch (Exception x) {
fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x);
}
// Formatter(File)
try (Formatter f = new Formatter(new File("foo"))) {
pass();
locale(f);
out(f, BufferedWriter.class);
} catch (Exception x) {
fail("new Formatter(new File(\"foo\")", x);
}
try {
new Formatter((File)null);
fail("new Formatter((File)null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter((File)null)", x);
}
// Formatter(PrintStream ps)
try {
// ambiguity detected at compile-time
Formatter f = new Formatter(System.out);
pass();
out(f, PrintStream.class);
locale(f);
} catch (Exception x) {
fail("new Formatter(System.out)", x);
}
try {
new Formatter((PrintStream) null);
fail("new Formatter((PrintStream) null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter((PrintStream) null)", x);
}
try (Formatter f = new Formatter(new PrintStream("foo"))) {
pass();
locale(f);
out(f, PrintStream.class);
} catch (FileNotFoundException x) {
fail("new Formatter(new PrintStream(\"foo\")", x);
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\")", x);
}
try (Formatter f = new Formatter(new PrintStream("foo"),
Locale.JAPANESE)) {
pass();
locale(f, Locale.JAPANESE);
out(f, PrintStream.class);
} catch (FileNotFoundException x) {
fail("new Formatter(new PrintStream(\"foo\")", x);
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\")", x);
}
try (PrintStream ps = new PrintStream("foo")) {
// The cast here is necessary to avoid an ambiguity error
// between Formatter(Appendable a, Locale l)
// and Formatter(OutputStream os, String csn)
new Formatter(ps, (String)null);
fail("new Formatter(new PrintStream(\"foo\"), (String)null)");
} catch (FileNotFoundException x) {
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
} catch (NullPointerException x) {
pass();
} catch (UnsupportedEncodingException x) {
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
}
// The cast here is necessary to avoid an ambiguity error
// between Formatter(Appendable a, Locale l)
// and Formatter(OutputStream os, String csn)
try (Formatter f = new Formatter(new PrintStream("foo"),
(Locale)null)) {
pass();
locale(f, null);
out(f, PrintStream.class);
} catch (FileNotFoundException x) {
fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
}
try (Formatter f = new Formatter(new PrintStream("foo"),
Locale.KOREAN)) {
pass();
locale(f, Locale.KOREAN);
out(f, PrintStream.class);
} catch (FileNotFoundException x) {
fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
}
try (Formatter f = new Formatter(new PrintStream("foo"),
"UTF-16BE", null)) {
pass();
locale(f, null);
out(f, BufferedWriter.class);
} catch (FileNotFoundException x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
} catch (UnsupportedEncodingException x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
}
try (Formatter f = new Formatter(new PrintStream("foo"),
"UTF-16BE", Locale.ITALIAN)) {
pass();
locale(f, Locale.ITALIAN);
out(f, BufferedWriter.class);
} catch (FileNotFoundException x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
} catch (UnsupportedEncodingException x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
}
String csn = Charset.defaultCharset().newEncoder().canEncode('\u00a3') ?
"ASCII" : "ISO-8859-1";
try {
ByteArrayOutputStream bs[] = { new ByteArrayOutputStream(),
new ByteArrayOutputStream(),
new ByteArrayOutputStream()
};
new Formatter((Appendable) new PrintStream(bs[0], true, csn)).format("\u00a3");
new Formatter((OutputStream)new PrintStream(bs[1], true, csn)).format("\u00a3");
new Formatter( new PrintStream(bs[2], true, csn)).format("\u00a3");
if (Arrays.equals(bs[0].toByteArray(), bs[1].toByteArray())) {
fail("arrays shouldn't match: " + bs[0].toByteArray());
} else {
pass();
}
if (! Arrays.equals(bs[0].toByteArray(), bs[2].toByteArray())) {
fail("arrays should match: " + bs[0].toByteArray() + " != "
+ bs[2].toByteArray());
} else {
pass();
}
} catch (UnsupportedEncodingException x) {
fail("new PrintStream(newByteArrayOutputStream, true, " + csn + ")", x);
}
// Formatter(OutputStream os)
try {
new Formatter((OutputStream) null);
fail("new Formatter((OutputStream) null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter((OutputStream) null)", x);
}
try (Formatter f = new Formatter((OutputStream) new PrintStream("foo"))) {
pass();
locale(f);
out(f, BufferedWriter.class);
} catch (Exception x) {
fail("new Formatter((OutputStream) new PrintStream(\"foo\")", x);
}
// Formatter(OutputStream os, String csn)
try {
new Formatter((OutputStream) null, "ISO-8859-1");
fail("new Formatter((OutputStream) null, \"ISO-8859-1\")");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter((OutputStream) null, \"ISO-8859-1\")", x);
}
try (PrintStream ps = new PrintStream("foo")) {
new Formatter((OutputStream) ps, null);
fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null",
x);
}
try (PrintStream ps = new PrintStream("foo")) {
new Formatter(ps, "bar");
fail("new Formatter(new PrintStream(\"foo\"), \"bar\")");
} catch (UnsupportedEncodingException x) {
pass();
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), \"bar\")", x);
}
try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8")) {
pass();
locale(f);
out(f, BufferedWriter.class);
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\")", x);
}
// Formatter(OutputStream os, String csn, Locale l)
try {
new Formatter((OutputStream) null, "ISO-8859-1", Locale.UK);
fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)",
x);
}
try (PrintStream ps = new PrintStream("foo")) {
new Formatter(ps, (String)null, Locale.UK);
fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)",
x);
}
// Formatter(OutputStream os, Charset charset, Locale l)
try (PrintStream ps = new PrintStream("foo")) {
new Formatter(ps, (Charset)null, Locale.UK);
fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)",
x);
}
try (PrintStream ps = new PrintStream("foo")) {
new Formatter(ps, "bar", Locale.UK);
fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)");
} catch (UnsupportedEncodingException x) {
pass();
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)",
x);
}
try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK)) {
pass();
out(f, BufferedWriter.class);
locale(f, Locale.UK);
} catch (Exception x) {
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK",
x);
}
// PrintStream(String fileName)
try (PrintStream ps = new PrintStream("foo")) {
pass();
} catch (Exception x) {
fail("new PrintStream(\"foo\")", x);
}
// PrintStream(String fileName, String csn)
try {
new PrintStream("foo", (String)null);
fail("new PrintStream(\"foo\", (String)null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintStream(\"foo\", (String)null)", x);
}
// PrintStream(String fileName, Charset charset)
try {
new PrintStream("foo", (Charset)null);
fail("new PrintStream(\"foo\", (Charset)null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintStream(\"foo\", (Charset)null)", x);
}
// PrintStream(File file)
try (PrintStream ps = new PrintStream(new File("foo"))) {
pass();
} catch (Exception x) {
fail("new PrintStream(new File(\"foo\"))", x);
}
// PrintStream(File file, String csn)
try {
new PrintStream(new File("foo"), (String)null);
fail("new PrintStream(new File(\"foo\"), (String)null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintStream(new File(\"foo\"), (String)null)", x);
}
// PrintStream(File file, Charset charset)
try {
new PrintStream(new File("foo"), (Charset)null);
fail("new PrintStream(new File(\"foo\"), (Charset)null)");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintStream(new File(\"foo\"), (Charset)null)", x);
}
// PrintWriter(String fileName)
try (PrintWriter pw = new PrintWriter("foo")) {
pass();
} catch (Exception x) {
fail("new PrintWriter(\"foo\")", x);
}
// PrintWriter(String fileName, String csn)
try {
new PrintWriter("foo", (String)null);
fail("new PrintWriter(\"foo\"), (String)null");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintWriter(\"foo\"), (String)null", x);
}
// PrintWriter(String fileName, Charset charset)
try {
new PrintWriter("foo", (Charset)null);
fail("new PrintWriter(\"foo\"), (Charset)null");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintWriter(\"foo\"), (Charset)null", x);
}
// PrintWriter(File file)
try (PrintWriter pw = new PrintWriter(new File("foo"))) {
pass();
} catch (Exception x) {
fail("new PrintWriter(new File(\"foo\"))", x);
}
// PrintWriter(File file, String csn)
try {
new PrintWriter(new File("foo"), (String)null);
fail("new PrintWriter(new File(\"foo\")), (String)null");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintWriter(new File(\"foo\")), (String)null", x);
}
// PrintWriter(File file, Charset charset)
try {
new PrintWriter(new File("foo"), (Charset)null);
fail("new PrintWriter(new File(\"foo\")), (Charset)null");
} catch (NullPointerException x) {
pass();
} catch (Exception x) {
fail("new PrintWriter(new File(\"foo\")), (Charset)null", x);
}
if (fail != 0)
throw new RuntimeException((fail + pass) + " tests: "
+ fail + " failure(s), first", first);
else
System.out.println("all " + (fail + pass) + " tests passed");
}
}

View file

@ -0,0 +1,133 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Formatter;
import java.util.Locale;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* @test
* @bug 8183743
* @summary Test to verify the new overload method with Charset functions the same
* as the existing method that takes a charset name.
* @run testng EncodingTest
*/
public class EncodingTest {
static String USER_DIR = System.getProperty("user.dir", ".");
// Charset added only for the 3-parameter constructors
public static enum ConstructorType {
STRING3,
FILE3,
OUTPUTSTREAM3
}
@DataProvider(name = "parameters")
public Object[][] getParameters() throws IOException {
Locale l = Locale.getDefault();
String csn1 = StandardCharsets.ISO_8859_1.name();
Charset charset1 = StandardCharsets.ISO_8859_1;
String csn2 = StandardCharsets.UTF_8.name();
Charset charset2 = StandardCharsets.UTF_8;
File file1 = new File(USER_DIR, "FormatterCharsetTest1.txt");
File file2 = new File(USER_DIR, "FormatterCharsetTest2.txt");
return new Object[][]{
{ConstructorType.STRING3, file1, file2, csn1, charset1},
{ConstructorType.FILE3, file1, file2, csn1, charset1},
{ConstructorType.OUTPUTSTREAM3, file1, file2, csn1, charset1},
{ConstructorType.STRING3, file1, file2, csn2, charset2},
{ConstructorType.FILE3, file1, file2, csn2, charset2},
{ConstructorType.OUTPUTSTREAM3, file1, file2, csn2, charset2},
};
}
/**
* Verifies that the overloading constructor behaves the same as the existing
* one.
* @param type the type of the constructor
* @param file1 file1 written with the name of a charset
* @param file2 file2 written with a charset
* @param csn the charset name
* @param charset the charset
* @throws IOException
*/
@Test(dataProvider = "parameters")
public void testConstructor(ConstructorType type, File file1, File file2,
String csn, Charset charset) throws Exception {
format(getFormatter(type, file1.getPath(), csn, null));
format(getFormatter(type, file2.getPath(), null, charset));
Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
Files.readAllLines(Paths.get(file2.getPath()), charset));
}
void format(Formatter formatter)
throws IOException {
formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");
formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
formatter.flush();
formatter.close();
}
Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)
throws IOException {
Formatter formatter = null;
if (csn != null) {
switch (type) {
case STRING3:
formatter = new Formatter(path, csn, Locale.getDefault());
break;
case FILE3:
formatter = new Formatter(new File(path), csn, Locale.getDefault());
break;
case OUTPUTSTREAM3:
formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());
break;
}
} else {
switch (type) {
case STRING3:
formatter = new Formatter(path, charset, Locale.getDefault());
break;
case FILE3:
formatter = new Formatter(new File(path), charset, Locale.getDefault());
break;
case OUTPUTSTREAM3:
formatter = new Formatter(new FileOutputStream(path), charset, Locale.getDefault());
break;
}
}
return formatter;
}
}

View file

@ -0,0 +1,132 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 7000511
* @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
* exception thrown
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.util.Formatter;
public class FailingConstructors {
static final String fileName = "FailingConstructorsTest";
static final String UNSUPPORTED_CHARSET = "unknownCharset";
static final String FILE_CONTENTS = "This is a small file!";
private static void realMain(String[] args) throws Throwable {
test(false, new File(fileName));
/* create the file and write its contents */
File file = File.createTempFile(fileName, null);
file.deleteOnExit();
Files.write(file.toPath(), FILE_CONTENTS.getBytes());
test(true, file);
file.delete();
}
private static void test(boolean exists, File file) throws Throwable {
/* Formatter(File file, String csn) */
try {
new Formatter(file, UNSUPPORTED_CHARSET);
fail();
} catch(FileNotFoundException|UnsupportedEncodingException e) {
pass();
}
check(exists, file);
try {
new Formatter(file, null);
fail();
} catch(FileNotFoundException|NullPointerException e) {
pass();
}
check(exists, file);
/* Formatter(String fileName, String csn) */
try {
new Formatter(file.getName(), UNSUPPORTED_CHARSET);
fail();
} catch(FileNotFoundException|UnsupportedEncodingException e) {
pass();
}
check(exists, file);
try {
new Formatter(file.getName(), null);
fail();
} catch(FileNotFoundException|NullPointerException e) {
pass();
}
check(exists, file);
}
private static void check(boolean exists, File file) {
if (exists) {
/* the file should be unchanged */
verifyContents(file);
} else {
/* the file should not have been created */
if (file.exists()) { fail(file + " should not have been created"); }
}
}
private static void verifyContents(File file) {
try (FileInputStream fis = new FileInputStream(file)) {
byte[] contents = FILE_CONTENTS.getBytes();
int read, count = 0;
while ((read = fis.read()) != -1) {
if (read != contents[count++]) {
fail("file contents have been altered");
return;
}
}
} catch (IOException ioe) {
unexpected(ioe);
}
}
//--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0;
static void pass() {passed++;}
static void fail() {failed++; Thread.dumpStack();}
static void fail(String message) {System.out.println(message); fail(); }
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
public static void main(String[] args) throws Throwable {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}

View file

@ -0,0 +1,100 @@
/*
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 5030063
* @summary Basic tests for flush().
*/
import java.io.Closeable;
import java.io.Flushable;
import java.util.Formatter;
import java.util.FormatterClosedException;
public class Flush {
private static class ExpectedException extends RuntimeException {}
private static class F implements Appendable, Closeable, Flushable {
public Appendable append(CharSequence csq) { return null; }
public Appendable append(char c) { return null; }
public Appendable append(CharSequence csq, int s, int e) {
return null;
}
public void close() {}
public void flush() {
throw new ExpectedException();
}
}
private static class NF implements Appendable, Closeable {
public Appendable append(CharSequence csq) { return null; }
public Appendable append(char c) { return null; }
public Appendable append(CharSequence csq, int s, int e) {
return null;
}
public void close() {}
// method coincidentally called flush()
public void flush() {
throw new RuntimeException("NF.flush should not be called");
}
}
private static void test(Formatter f) {
if (f.out() instanceof F) {
// F.flush() called since F implements Flushable
try {
f.flush();
throw new RuntimeException("F.flush not called");
} catch (ExpectedException x) {
System.out.println(" F.flush called");
}
} else {
// NF.flush() not called since NF does not implement Flushable
f.flush();
}
// Formatter is a Flushable
if (!(f instanceof Flushable))
throw new RuntimeException("Formatter is not a Flushable");
// flush() after close() throws a FormatterClosedException
f.close();
try {
f.flush();
throw new RuntimeException("FormatterClosedException not thrown");
} catch (FormatterClosedException x) {
System.out.println(" FormatterClosedException thrown");
}
}
public static void main(String [] args) {
System.out.println("testing Flushable");
test(new Formatter(new F()));
System.out.println("testing non-Flushable");
test(new Formatter(new NF()));
}
}

View file

@ -0,0 +1,171 @@
/*
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8146156 8159548 8060094
* @modules jdk.localedata
* @summary test whether uppercasing follows Locale.Category.FORMAT locale.
* Also test whether the uppercasing uses the locale specified to the
* Formatter API.
*
* @run main/othervm FormatLocale
*/
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.Month;
import java.util.Calendar;
import java.util.Formatter;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.stream.IntStream;
public class FormatLocale {
static final Locale TURKISH = Locale.of("tr");
static final List<String> conversions = List.of(
"%S",
"%S",
"%TB",
"%G",
"%C");
static final List<Object> src = List.of(
"Turkish",
"Turkish",
LocalDate.of(2016, Month.APRIL, 1),
Float.valueOf(100_000_000),
'i');
static final List<Locale> defaultLocale = List.of(
Locale.ENGLISH,
TURKISH,
TURKISH,
Locale.FRANCE,
TURKISH);
static final List<Locale> formatLocale = List.of(
TURKISH,
Locale.ENGLISH,
Locale.FRANCE,
Locale.ENGLISH,
Locale.ENGLISH);
static final List<String> expectedWithDefaultLocale = List.of(
"TURKISH",
"TURK\u0130SH",
"N\u0130SAN",
"1,00000E+08",
"\u0130");
static final List<String> expectedWithFormatLocale = List.of(
"TURK\u0130SH",
"TURKISH",
"AVRIL",
"1.00000E+08",
"I");
static void formatLocaleTest() {
StringBuilder sb = new StringBuilder();
// checks whether upper casing follows Locale.Category.FORMAT locale
IntStream.range(0, src.size()).forEach(i -> {
sb.setLength(0);
Locale.setDefault(Locale.Category.FORMAT, defaultLocale.get(i));
new Formatter(sb).format(conversions.get(i), src.get(i));
if (!sb.toString().equals(expectedWithDefaultLocale.get(i))) {
throw new RuntimeException(
"Wrong uppercasing while using Formatter.format(" +
"\"" + conversions.get(i) + "\"" +
") with the default locale: '"
+ defaultLocale.get(i) +
"'. Expected: " + expectedWithDefaultLocale.get(i) +
" Returned: " + sb.toString());
}
});
// checks whether upper casing uses the locale set during creation of
// Formatter instance, instead of the default locale
IntStream.range(0, src.size()).forEach(i -> {
sb.setLength(0);
Locale.setDefault(Locale.Category.FORMAT, defaultLocale.get(i));
new Formatter(sb, formatLocale.get(i)).format(conversions.get(i),
src.get(i));
if (!sb.toString().equals(expectedWithFormatLocale.get(i))) {
throw new RuntimeException(
"Wrong uppercasing while using Formatter.format(" +
"\"" + conversions.get(i) + "\"" +
") with the locale specified during instance" +
" creation: '" + formatLocale.get(i) +
"'. Expected: " + expectedWithFormatLocale.get(i) +
" Returned: " + sb.toString());
}
});
}
static void nullLocaleTest() {
String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ";
String expected = "Fri Friday Jan January PST";
StringBuilder sb = new StringBuilder();
Locale orig = Locale.getDefault();
try {
Locale.setDefault(Locale.JAPAN);
Formatter f = new Formatter(sb, (Locale)null);
ZoneId zid = ZoneId.of("America/Los_Angeles");
Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US);
c.set(2016, 0, 1, 0, 0, 0);
f.format(fmt, c);
if (!sb.toString().equals(expected)) {
throw new RuntimeException(
"Localized text returned with null locale.\n" +
" expected: " + expected + "\n" +
" returned: " + sb.toString());
}
sb.setLength(0);
ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid);
f.format(fmt, zdt);
if (!sb.toString().equals(expected)) {
throw new RuntimeException(
"Localized text returned with null locale.\n" +
" expected: " + expected + "\n" +
" returned: " + sb.toString());
}
} finally {
Locale.setDefault(orig);
}
}
public static void main(String [] args) {
formatLocaleTest();
nullLocaleTest();
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8262351
* @summary Incorrect zero padding occurs in the presence of some leading sign
* flags for hexadecimal floating point conversions.
* @run testng HexFloatZeroPadding
*/
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@Test
public class HexFloatZeroPadding {
public void testCorrectWidthHexFloatZeroPadding()
{
final int EXPECTEDLENGTH = 12;
assertEquals(String.format("|%010a|", 0.0).length(), EXPECTEDLENGTH);
assertEquals(String.format("|% 010a|", 0.0).length(), EXPECTEDLENGTH);
assertEquals(String.format("|%+010a|", 0.0).length(), EXPECTEDLENGTH);
assertEquals(String.format("|%010a|", -0.0).length(), EXPECTEDLENGTH);
assertEquals(String.format("|% 010a|", -0.0).length(), EXPECTEDLENGTH);
assertEquals(String.format("|%+010a|", -0.0).length(), EXPECTEDLENGTH);
}
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8039390
* @summary Basic test for null argument
*/
import java.util.Formatter;
import java.util.Locale;
public class NullArg {
public static void main(String [] args) {
char[] cs = new char[] {
'b', 'B', 'h', 'H', 's', 'S', 'c', 'C', 'd', 'o', 'x', 'X',
'e', 'E', 'f', 'g', 'G', 'a', 'A', 't', 'T',
};
char[] tcs = new char[] {
'H', 'I', 'k', 'l', 'l', 'M', 'S', 'L', 'N', 'p', 'z', 'Z', 's',
'Q', 'B', 'b', 'h', 'A', 'a', 'C', 'Y', 'y', 'j', 'm', 'd', 'e',
'R', 'T', 'r', 'D', 'F', 'c'
};
for (char c : cs) {
String expected = (c == 'b' || c == 'B') ? "false" : "null";
if (Character.isUpperCase(c)) {
expected = expected.toUpperCase(Locale.ROOT);
}
if (c == 't' || c == 'T') {
for (char ct : tcs) {
if (!String.format("%" + c + ct, null).equals(expected)) {
throw new RuntimeException("%t" + ct + "null check failed.");
}
}
} else {
if (!String.format("%" + c , null).equals(expected)) {
throw new RuntimeException("%" + c + "null check failed.");
}
}
}
}
}

View file

@ -0,0 +1,428 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4906370 8299677 8326718 8328037
* @summary Tests to exercise padding on int and double values,
* with various flag combinations.
* @run junit Padding
*/
import java.util.Locale;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Padding {
/* blank padding, right adjusted, optional sign */
static Stream<? extends Arguments> blankPaddingRightAdjustedOptionalSign() {
var tenMillionBlanks = " ".repeat(10_000_000);
return Stream.of(
Arguments.of("12", "%1d", 12),
Arguments.of("12", "%2d", 12),
Arguments.of(" 12", "%3d", 12),
Arguments.of(" 12", "%4d", 12),
Arguments.of(" 12", "%5d", 12),
Arguments.of(" 12", "%10d", 12),
Arguments.of(tenMillionBlanks + "12", "%10000002d", 12),
Arguments.of("-12", "%1d", -12),
Arguments.of("-12", "%2d", -12),
Arguments.of("-12", "%3d", -12),
Arguments.of(" -12", "%4d", -12),
Arguments.of(" -12", "%5d", -12),
Arguments.of(" -12", "%10d", -12),
Arguments.of(tenMillionBlanks + "-12", "%10000003d", -12),
Arguments.of("1.2", "%1.1f", 1.2),
Arguments.of("1.2", "%2.1f", 1.2),
Arguments.of("1.2", "%3.1f", 1.2),
Arguments.of(" 1.2", "%4.1f", 1.2),
Arguments.of(" 1.2", "%5.1f", 1.2),
Arguments.of(" 1.2", "%10.1f", 1.2),
Arguments.of(tenMillionBlanks + "1.2", "%10000003.1f", 1.2),
Arguments.of("-1.2", "%1.1f", -1.2),
Arguments.of("-1.2", "%2.1f", -1.2),
Arguments.of("-1.2", "%3.1f", -1.2),
Arguments.of("-1.2", "%4.1f", -1.2),
Arguments.of(" -1.2", "%5.1f", -1.2),
Arguments.of(" -1.2", "%10.1f", -1.2),
Arguments.of(tenMillionBlanks + "-1.2", "%10000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void blankPaddingRightAdjustedOptionalSign(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* blank padding, right adjusted, mandatory sign */
static Stream<? extends Arguments> blankPaddingRightAdjustedMandatorySign() {
var tenMillionBlanks = " ".repeat(10_000_000);
return Stream.of(
Arguments.of("+12", "%+1d", 12),
Arguments.of("+12", "%+2d", 12),
Arguments.of("+12", "%+3d", 12),
Arguments.of(" +12", "%+4d", 12),
Arguments.of(" +12", "%+5d", 12),
Arguments.of(" +12", "%+10d", 12),
Arguments.of(tenMillionBlanks + "+12", "%+10000003d", 12),
Arguments.of("-12", "%+1d", -12),
Arguments.of("-12", "%+2d", -12),
Arguments.of("-12", "%+3d", -12),
Arguments.of(" -12", "%+4d", -12),
Arguments.of(" -12", "%+5d", -12),
Arguments.of(" -12", "%+10d", -12),
Arguments.of(tenMillionBlanks + "-12", "%+10000003d", -12),
Arguments.of("+1.2", "%+1.1f", 1.2),
Arguments.of("+1.2", "%+2.1f", 1.2),
Arguments.of("+1.2", "%+3.1f", 1.2),
Arguments.of("+1.2", "%+4.1f", 1.2),
Arguments.of(" +1.2", "%+5.1f", 1.2),
Arguments.of(" +1.2", "%+10.1f", 1.2),
Arguments.of(tenMillionBlanks + "+1.2", "%+10000004.1f", 1.2),
Arguments.of("-1.2", "%+1.1f", -1.2),
Arguments.of("-1.2", "%+2.1f", -1.2),
Arguments.of("-1.2", "%+3.1f", -1.2),
Arguments.of("-1.2", "%+4.1f", -1.2),
Arguments.of(" -1.2", "%+5.1f", -1.2),
Arguments.of(" -1.2", "%+10.1f", -1.2),
Arguments.of(tenMillionBlanks + "-1.2", "%+10000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void blankPaddingRightAdjustedMandatorySign(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* blank padding, right adjusted, mandatory blank sign */
static Stream<? extends Arguments> blankPaddingRightAdjustedMandatoryBlank() {
var tenMillionBlanks = " ".repeat(10_000_000);
return Stream.of(
Arguments.of(" 12", "% 1d", 12),
Arguments.of(" 12", "% 2d", 12),
Arguments.of(" 12", "% 3d", 12),
Arguments.of(" 12", "% 4d", 12),
Arguments.of(" 12", "% 5d", 12),
Arguments.of(" 12", "% 10d", 12),
Arguments.of(tenMillionBlanks + "12", "% 10000002d", 12),
Arguments.of("-12", "% 1d", -12),
Arguments.of("-12", "% 2d", -12),
Arguments.of("-12", "% 3d", -12),
Arguments.of(" -12", "% 4d", -12),
Arguments.of(" -12", "% 5d", -12),
Arguments.of(" -12", "% 10d", -12),
Arguments.of(tenMillionBlanks + "-12", "% 10000003d", -12),
Arguments.of(" 1.2", "% 1.1f", 1.2),
Arguments.of(" 1.2", "% 2.1f", 1.2),
Arguments.of(" 1.2", "% 3.1f", 1.2),
Arguments.of(" 1.2", "% 4.1f", 1.2),
Arguments.of(" 1.2", "% 5.1f", 1.2),
Arguments.of(" 1.2", "% 10.1f", 1.2),
Arguments.of(tenMillionBlanks + "1.2", "% 10000003.1f", 1.2),
Arguments.of("-1.2", "% 1.1f", -1.2),
Arguments.of("-1.2", "% 2.1f", -1.2),
Arguments.of("-1.2", "% 3.1f", -1.2),
Arguments.of("-1.2", "% 4.1f", -1.2),
Arguments.of(" -1.2", "% 5.1f", -1.2),
Arguments.of(" -1.2", "% 10.1f", -1.2),
Arguments.of(tenMillionBlanks + "-1.2", "% 10000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void blankPaddingRightAdjustedMandatoryBlank(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* blank padding, left adjusted, optional sign */
static Stream<? extends Arguments> blankPaddingLeftAdjustedOptionalSign() {
var tenMillionBlanks = " ".repeat(10_000_000);
return Stream.of(
Arguments.of("12", "%-1d", 12),
Arguments.of("12", "%-2d", 12),
Arguments.of("12 ", "%-3d", 12),
Arguments.of("12 ", "%-4d", 12),
Arguments.of("12 ", "%-5d", 12),
Arguments.of("12 ", "%-10d", 12),
Arguments.of("12" + tenMillionBlanks, "%-10000002d", 12),
Arguments.of("-12", "%-1d", -12),
Arguments.of("-12", "%-2d", -12),
Arguments.of("-12", "%-3d", -12),
Arguments.of("-12 ", "%-4d", -12),
Arguments.of("-12 ", "%-5d", -12),
Arguments.of("-12 ", "%-10d", -12),
Arguments.of("-12" + tenMillionBlanks, "%-10000003d", -12),
Arguments.of("1.2", "%-1.1f", 1.2),
Arguments.of("1.2", "%-2.1f", 1.2),
Arguments.of("1.2", "%-3.1f", 1.2),
Arguments.of("1.2 ", "%-4.1f", 1.2),
Arguments.of("1.2 ", "%-5.1f", 1.2),
Arguments.of("1.2 ", "%-10.1f", 1.2),
Arguments.of("1.2" + tenMillionBlanks, "%-10000003.1f", 1.2),
Arguments.of("-1.2", "%-1.1f", -1.2),
Arguments.of("-1.2", "%-2.1f", -1.2),
Arguments.of("-1.2", "%-3.1f", -1.2),
Arguments.of("-1.2", "%-4.1f", -1.2),
Arguments.of("-1.2 ", "%-5.1f", -1.2),
Arguments.of("-1.2 ", "%-10.1f", -1.2),
Arguments.of("-1.2" + tenMillionBlanks, "%-10000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void blankPaddingLeftAdjustedOptionalSign(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* blank padding, left adjusted, mandatory sign */
static Stream<? extends Arguments> blankPaddingLeftAdjustedMandatorySign() {
var tenMillionBlanks = " ".repeat(10_000_000);
return Stream.of(
Arguments.of("+12", "%-+1d", 12),
Arguments.of("+12", "%-+2d", 12),
Arguments.of("+12", "%-+3d", 12),
Arguments.of("+12 ", "%-+4d", 12),
Arguments.of("+12 ", "%-+5d", 12),
Arguments.of("+12 ", "%-+10d", 12),
Arguments.of("+12" + tenMillionBlanks, "%-+10000003d", 12),
Arguments.of("-12", "%-+1d", -12),
Arguments.of("-12", "%-+2d", -12),
Arguments.of("-12", "%-+3d", -12),
Arguments.of("-12 ", "%-+4d", -12),
Arguments.of("-12 ", "%-+5d", -12),
Arguments.of("-12 ", "%-+10d", -12),
Arguments.of("-12" + tenMillionBlanks, "%-+10000003d", -12),
Arguments.of("+1.2", "%-+1.1f", 1.2),
Arguments.of("+1.2", "%-+2.1f", 1.2),
Arguments.of("+1.2", "%-+3.1f", 1.2),
Arguments.of("+1.2", "%-+4.1f", 1.2),
Arguments.of("+1.2 ", "%-+5.1f", 1.2),
Arguments.of("+1.2 ", "%-+10.1f", 1.2),
Arguments.of("+1.2" + tenMillionBlanks, "%-+10000004.1f", 1.2),
Arguments.of("-1.2", "%-+1.1f", -1.2),
Arguments.of("-1.2", "%-+2.1f", -1.2),
Arguments.of("-1.2", "%-+3.1f", -1.2),
Arguments.of("-1.2", "%-+4.1f", -1.2),
Arguments.of("-1.2 ", "%-+5.1f", -1.2),
Arguments.of("-1.2 ", "%-+10.1f", -1.2),
Arguments.of("-1.2" + tenMillionBlanks, "%-+10000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void blankPaddingLeftAdjustedMandatorySign(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* blank padding, left adjusted, mandatory blank sign */
static Stream<? extends Arguments> blankPaddingLeftAdjustedMandatoryBlank() {
var tenMillionBlanks = " ".repeat(10_000_000);
return Stream.of(
Arguments.of(" 12", "%- 1d", 12),
Arguments.of(" 12", "%- 2d", 12),
Arguments.of(" 12", "%- 3d", 12),
Arguments.of(" 12 ", "%- 4d", 12),
Arguments.of(" 12 ", "%- 5d", 12),
Arguments.of(" 12 ", "%- 10d", 12),
Arguments.of(" 12" + tenMillionBlanks, "%- 10000003d", 12),
Arguments.of("-12", "%- 1d", -12),
Arguments.of("-12", "%- 2d", -12),
Arguments.of("-12", "%- 3d", -12),
Arguments.of("-12 ", "%- 4d", -12),
Arguments.of("-12 ", "%- 5d", -12),
Arguments.of("-12 ", "%- 10d", -12),
Arguments.of("-12" + tenMillionBlanks, "%- 10000003d", -12),
Arguments.of(" 1.2", "%- 1.1f", 1.2),
Arguments.of(" 1.2", "%- 2.1f", 1.2),
Arguments.of(" 1.2", "%- 3.1f", 1.2),
Arguments.of(" 1.2", "%- 4.1f", 1.2),
Arguments.of(" 1.2 ", "%- 5.1f", 1.2),
Arguments.of(" 1.2 ", "%- 10.1f", 1.2),
Arguments.of(" 1.2" + tenMillionBlanks, "%- 10000004.1f", 1.2),
Arguments.of("-1.2", "%- 1.1f", -1.2),
Arguments.of("-1.2", "%- 2.1f", -1.2),
Arguments.of("-1.2", "%- 3.1f", -1.2),
Arguments.of("-1.2", "%- 4.1f", -1.2),
Arguments.of("-1.2 ", "%- 5.1f", -1.2),
Arguments.of("-1.2 ", "%- 10.1f", -1.2),
Arguments.of("-1.2" + tenMillionBlanks, "%- 10000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void blankPaddingLeftAdjustedMandatoryBlank(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* zero padding, right adjusted, optional sign */
static Stream<? extends Arguments> zeroPaddingRightAdjustedOptionalSign() {
var tenMillionZeros = "0".repeat(10_000_000);
return Stream.of(
Arguments.of("12", "%01d", 12),
Arguments.of("12", "%02d", 12),
Arguments.of("012", "%03d", 12),
Arguments.of("0012", "%04d", 12),
Arguments.of("00012", "%05d", 12),
Arguments.of("0000000012", "%010d", 12),
Arguments.of(tenMillionZeros + "12", "%010000002d", 12),
Arguments.of("-12", "%01d", -12),
Arguments.of("-12", "%02d", -12),
Arguments.of("-12", "%03d", -12),
Arguments.of("-012", "%04d", -12),
Arguments.of("-0012", "%05d", -12),
Arguments.of("-000000012", "%010d", -12),
Arguments.of("-" + tenMillionZeros + "12", "%010000003d", -12),
Arguments.of("1.2", "%01.1f", 1.2),
Arguments.of("1.2", "%02.1f", 1.2),
Arguments.of("1.2", "%03.1f", 1.2),
Arguments.of("01.2", "%04.1f", 1.2),
Arguments.of("001.2", "%05.1f", 1.2),
Arguments.of("00000001.2", "%010.1f", 1.2),
Arguments.of(tenMillionZeros + "1.2", "%010000003.1f", 1.2),
Arguments.of("-1.2", "%01.1f", -1.2),
Arguments.of("-1.2", "%02.1f", -1.2),
Arguments.of("-1.2", "%03.1f", -1.2),
Arguments.of("-1.2", "%04.1f", -1.2),
Arguments.of("-01.2", "%05.1f", -1.2),
Arguments.of("-0000001.2", "%010.1f", -1.2),
Arguments.of("-" + tenMillionZeros + "1.2", "%010000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void zeroPaddingRightAdjustedOptionalSign(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* zero padding, right adjusted, mandatory sign */
static Stream<? extends Arguments> zeroPaddingRightAdjustedMandatorySign() {
var tenMillionZeros = "0".repeat(10_000_000);
return Stream.of(
Arguments.of("+12", "%+01d", 12),
Arguments.of("+12", "%+02d", 12),
Arguments.of("+12", "%+03d", 12),
Arguments.of("+012", "%+04d", 12),
Arguments.of("+0012", "%+05d", 12),
Arguments.of("+000000012", "%+010d", 12),
Arguments.of("+" + tenMillionZeros + "12", "%+010000003d", 12),
Arguments.of("-12", "%+01d", -12),
Arguments.of("-12", "%+02d", -12),
Arguments.of("-12", "%+03d", -12),
Arguments.of("-012", "%+04d", -12),
Arguments.of("-0012", "%+05d", -12),
Arguments.of("-000000012", "%+010d", -12),
Arguments.of("-" + tenMillionZeros + "12", "%+010000003d", -12),
Arguments.of("+1.2", "%+01.1f", 1.2),
Arguments.of("+1.2", "%+02.1f", 1.2),
Arguments.of("+1.2", "%+03.1f", 1.2),
Arguments.of("+1.2", "%+04.1f", 1.2),
Arguments.of("+01.2", "%+05.1f", 1.2),
Arguments.of("+0000001.2", "%+010.1f", 1.2),
Arguments.of("+" + tenMillionZeros + "1.2", "%+010000004.1f", 1.2),
Arguments.of("-1.2", "%+01.1f", -1.2),
Arguments.of("-1.2", "%+02.1f", -1.2),
Arguments.of("-1.2", "%+03.1f", -1.2),
Arguments.of("-1.2", "%+04.1f", -1.2),
Arguments.of("-01.2", "%+05.1f", -1.2),
Arguments.of("-0000001.2", "%+010.1f", -1.2),
Arguments.of("-" + tenMillionZeros + "1.2", "%+010000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void zeroPaddingRightAdjustedMandatorySign(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
/* zero padding, right adjusted, mandatory blank sign */
static Stream<? extends Arguments> zeroPaddingRightAdjustedMandatoryBlank() {
var tenMillionZeros = "0".repeat(10_000_000);
return Stream.of(
Arguments.of(" 12", "% 01d", 12),
Arguments.of(" 12", "% 02d", 12),
Arguments.of(" 12", "% 03d", 12),
Arguments.of(" 012", "% 04d", 12),
Arguments.of(" 0012", "% 05d", 12),
Arguments.of(" 000000012", "% 010d", 12),
Arguments.of(" " + tenMillionZeros + "12", "% 010000003d", 12),
Arguments.of("-12", "% 01d", -12),
Arguments.of("-12", "% 02d", -12),
Arguments.of("-12", "% 03d", -12),
Arguments.of("-012", "% 04d", -12),
Arguments.of("-0012", "% 05d", -12),
Arguments.of("-000000012", "% 010d", -12),
Arguments.of("-" + tenMillionZeros + "12", "% 010000003d", -12),
Arguments.of(" 1.2", "% 01.1f", 1.2),
Arguments.of(" 1.2", "% 02.1f", 1.2),
Arguments.of(" 1.2", "% 03.1f", 1.2),
Arguments.of(" 1.2", "% 04.1f", 1.2),
Arguments.of(" 01.2", "% 05.1f", 1.2),
Arguments.of(" 0000001.2", "% 010.1f", 1.2),
Arguments.of(" " + tenMillionZeros + "1.2", "% 010000004.1f", 1.2),
Arguments.of("-1.2", "% 01.1f", -1.2),
Arguments.of("-1.2", "% 02.1f", -1.2),
Arguments.of("-1.2", "% 03.1f", -1.2),
Arguments.of("-1.2", "% 04.1f", -1.2),
Arguments.of("-01.2", "% 05.1f", -1.2),
Arguments.of("-0000001.2", "% 010.1f", -1.2),
Arguments.of("-" + tenMillionZeros + "1.2", "% 010000004.1f", -1.2));
}
@ParameterizedTest
@MethodSource
void zeroPaddingRightAdjustedMandatoryBlank(String expected, String format, Object value) {
assertEquals(expected, String.format(Locale.US, format, value));
}
}

View file

@ -0,0 +1,60 @@
#! /bin/sh
#
# Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
javac -d . ../../../../../make/jdk/src/classes/build/tools/spp/Spp.java
gen() {
# if [ $3 = "true" ]
# then $SPP -K$1 -Dtype=$1 -DType=$2 -Kprim<Basic-X.java.template >Basic$2.java
# else $SPP -K$1 -Dtype=$1 -DType=$2 -K$3 <Basic-X.java.template >Basic$2.java
# fi
out=Basic$2.java
rm -f $out
java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -K$3 -K$4 -K$5 -K$6 -iBasic-X.java.template -nel -o$out
}
gen boolean Boolean prim "" "" ""
gen Boolean BooleanObject "" "" "" ""
gen byte Byte prim "" dec ""
gen Byte ByteObject "" "" dec ""
gen char Char prim "" "" ""
gen Character CharObject "" "" "" ""
gen short Short prim "" dec ""
gen Short ShortObject "" "" dec ""
gen int Int prim "" dec ""
gen Integer IntObject "" "" dec ""
gen long Long prim "" dec ""
gen Long LongObject "" "" dec ""
gen BigInteger BigInteger "" "" "" ""
gen float Float prim fp "" ""
gen Float FloatObject "" fp "" ""
gen double Double prim fp "" ""
gen Double DoubleObject "" fp "" ""
gen BigDecimal BigDecimal "" fp "" ""
gen Calendar DateTime "" "" "" datetime
rm -rf build

View file

@ -0,0 +1,69 @@
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8199672 8174269
* @summary test the Formatter.format() method with java.locale.providers=SPI,
* CLDR. It should not throw ClassCastException if an SPI is
* used and NumberFormat.getInstance() does not return a
* DecimalFormat object.
* @modules jdk.localedata
* @library provider
* @build provider/module-info provider/test.NumberFormatProviderImpl
* @run main/othervm -Djava.locale.providers=SPI,CLDR FormatterWithProvider
*/
import java.util.Formatter;
import java.util.Locale;
public class FormatterWithProvider {
public static void main(String[] args) {
Integer number = 1234567;
String formatString = "%,d";
try {
testFormatter(Locale.JAPANESE, formatString, number);
testFormatter(Locale.FRENCH, formatString, number);
testFormatter(Locale.of("hi", "IN"), formatString, number);
} catch (ClassCastException ex) {
throw new RuntimeException("[FAILED: A ClassCastException is" +
" thrown while using Formatter.format() with VM" +
" argument java.locale.providers=SPI,CLDR]", ex);
}
}
private static void testFormatter(Locale locale, String formatString,
Integer number) {
// test using String.format
String.format(locale, formatString, number);
// test using Formatter's format
Formatter formatter = new Formatter(locale);
formatter.format(formatString, number);
}
}

View file

@ -0,0 +1,75 @@
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8196399 8202537 8174269
* @summary test Formatter if any ArithmeticException is thrown while
* formatting a number in the locale which does not use any
* grouping, but specifies a grouping separator.
* @library provider
* @build provider/module-info provider/test.NumberFormatProviderImpl
* @run main/othervm -Djava.locale.providers=SPI,CLDR NoGroupingUsed
*/
import java.util.Formatter;
import java.util.Locale;
public class NoGroupingUsed {
public static void main(String[] args) {
Locale locale = Locale.of("xx", "YY");
String number = "1234567";
String formatString = "%,d";
try {
testGrouping(locale, formatString, number);
} catch (ArithmeticException ex) {
throw new RuntimeException("[FAILED: ArithmeticException occurred"
+ " while formatting the number: " + number + ", with"
+ " format string: " + formatString + ", in locale: "
+ locale, ex);
}
}
private static void testGrouping(Locale locale, String formatString, String number) {
// test using String.format
String result = String.format(locale, formatString, Integer.parseInt(number));
if (!number.equals(result)) {
throw new RuntimeException("[FAILED: Incorrect formatting"
+ " of number: " + number + " using String.format with format"
+ " string: " + formatString + " in locale: " + locale
+ ". Actual: " + result + ", Expected: " + number + "]");
}
// test using Formatter's format
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, locale);
formatter.format(formatString, Integer.parseInt(number));
if (!number.equals(sb.toString())) {
throw new RuntimeException("[FAILED: Incorrect formatting"
+ " of number: " + number + "using Formatter.format with"
+ " format string: " + formatString + " in locale: " + locale
+ ". Actual: " + sb.toString() + ", Expected: " + number + "]");
}
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
module provider {
exports test;
provides java.text.spi.NumberFormatProvider with test.NumberFormatProviderImpl;
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package test;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.spi.NumberFormatProvider;
import java.util.Locale;
public class NumberFormatProviderImpl extends NumberFormatProvider {
private static final Locale[] locales = {Locale.FRENCH, Locale.JAPANESE,
Locale.of("hi", "IN"), Locale.of("xx", "YY")};
@Override
public NumberFormat getCurrencyInstance(Locale locale) {
return null;
}
@Override
public NumberFormat getIntegerInstance(Locale locale) {
return null;
}
@Override
public NumberFormat getNumberInstance(Locale locale) {
if (locale.getLanguage().equals("xx")) {
return new DecimalFormat("#0.###", DecimalFormatSymbols.getInstance(Locale.US));
} else {
return null;
}
}
@Override
public NumberFormat getPercentInstance(Locale locale) {
return null;
}
@Override
public Locale[] getAvailableLocales() {
return locales;
}
}