use of java.util.Formatter in project j2objc by google.
the class FormatterTest method test_Constructor.
/**
* java.util.Formatter#Formatter()
*/
public void test_Constructor() {
Formatter f = new Formatter();
assertNotNull(f);
assertTrue(f.out() instanceof StringBuilder);
assertEquals(f.locale(), Locale.getDefault());
assertNotNull(f.toString());
}
use of java.util.Formatter in project j2objc by google.
the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_FloatConversionE.
/**
* java.util.Formatter#format(String, Object...) for Float/Double
* conversion type 'e' and 'E'
*/
public void test_formatLjava_lang_String$Ljava_lang_Object_FloatConversionE() {
Formatter f = null;
final Object[][] tripleE = { { 0f, "%e", "0.000000e+00" }, { 0f, "%#.0e", "0.e+00" }, { 0f, "%#- (9.8e", " 0.00000000e+00" }, { 0f, "%#+0(8.4e", "+0.0000e+00" }, { 0f, "%-+(1.6e", "+0.000000e+00" }, { 0f, "% 0(12e", " 0.000000e+00" }, { 101f, "%e", "1.010000e+02" }, { 101f, "%#.0e", "1.e+02" }, { 101f, "%#- (9.8e", " 1.01000000e+02" }, { 101f, "%#+0(8.4e", "+1.0100e+02" }, { 101f, "%-+(1.6e", "+1.010000e+02" }, { 101f, "% 0(12e", " 1.010000e+02" }, { 1.f, "%e", "1.000000e+00" }, { 1.f, "%#.0e", "1.e+00" }, { 1.f, "%#- (9.8e", " 1.00000000e+00" }, { 1.f, "%#+0(8.4e", "+1.0000e+00" }, { 1.f, "%-+(1.6e", "+1.000000e+00" }, { 1.f, "% 0(12e", " 1.000000e+00" }, { -98f, "%e", "-9.800000e+01" }, { -98f, "%#.0e", "-1.e+02" }, { -98f, "%#- (9.8e", "(9.80000000e+01)" }, { -98f, "%#+0(8.4e", "(9.8000e+01)" }, { -98f, "%-+(1.6e", "(9.800000e+01)" }, { -98f, "% 0(12e", "(9.800000e+01)" }, { 1.23f, "%e", "1.230000e+00" }, { 1.23f, "%#.0e", "1.e+00" }, { 1.23f, "%#- (9.8e", " 1.23000002e+00" }, { 1.23f, "%#+0(8.4e", "+1.2300e+00" }, { 1.23f, "%-+(1.6e", "+1.230000e+00" }, { 1.23f, "% 0(12e", " 1.230000e+00" }, { 34.1234567f, "%e", "3.412346e+01" }, { 34.1234567f, "%#.0e", "3.e+01" }, { 34.1234567f, "%#- (9.8e", " 3.41234550e+01" }, { 34.1234567f, "%#+0(8.4e", "+3.4123e+01" }, { 34.1234567f, "%-+(1.6e", "+3.412346e+01" }, { 34.1234567f, "% 0(12e", " 3.412346e+01" }, { -.12345f, "%e", "-1.234500e-01" }, { -.12345f, "%#.0e", "-1.e-01" }, { -.12345f, "%#- (9.8e", "(1.23450004e-01)" }, { -.12345f, "%#+0(8.4e", "(1.2345e-01)" }, { -.12345f, "%-+(1.6e", "(1.234500e-01)" }, { -.12345f, "% 0(12e", "(1.234500e-01)" }, { -9876.1234567f, "%e", "-9.876123e+03" }, { -9876.1234567f, "%#.0e", "-1.e+04" }, { -9876.1234567f, "%#- (9.8e", "(9.87612305e+03)" }, { -9876.1234567f, "%#+0(8.4e", "(9.8761e+03)" }, { -9876.1234567f, "%-+(1.6e", "(9.876123e+03)" }, { -9876.1234567f, "% 0(12e", "(9.876123e+03)" }, { Float.MAX_VALUE, "%e", "3.402823e+38" }, { Float.MAX_VALUE, "%#.0e", "3.e+38" }, { Float.MAX_VALUE, "%#- (9.8e", " 3.40282347e+38" }, { Float.MAX_VALUE, "%#+0(8.4e", "+3.4028e+38" }, { Float.MAX_VALUE, "%-+(1.6e", "+3.402823e+38" }, { Float.MAX_VALUE, "% 0(12e", " 3.402823e+38" }, { Float.MIN_VALUE, "%e", "1.401298e-45" }, { Float.MIN_VALUE, "%#.0e", "1.e-45" }, { Float.MIN_VALUE, "%#- (9.8e", " 1.40129846e-45" }, { Float.MIN_VALUE, "%#+0(8.4e", "+1.4013e-45" }, { Float.MIN_VALUE, "%-+(1.6e", "+1.401298e-45" }, { Float.MIN_VALUE, "% 0(12e", " 1.401298e-45" }, { Float.NaN, "%e", "NaN" }, { Float.NaN, "%#.0e", "NaN" }, { Float.NaN, "%#- (9.8e", "NaN " }, { Float.NaN, "%#+0(8.4e", " NaN" }, { Float.NaN, "%-+(1.6e", "NaN" }, { Float.NaN, "% 0(12e", " NaN" }, { Float.NEGATIVE_INFINITY, "%e", "-Infinity" }, { Float.NEGATIVE_INFINITY, "%#.0e", "-Infinity" }, { Float.NEGATIVE_INFINITY, "%#- (9.8e", "(Infinity)" }, { Float.NEGATIVE_INFINITY, "%#+0(8.4e", "(Infinity)" }, { Float.NEGATIVE_INFINITY, "%-+(1.6e", "(Infinity)" }, { Float.NEGATIVE_INFINITY, "% 0(12e", " (Infinity)" }, { Float.NEGATIVE_INFINITY, "%e", "-Infinity" }, { Float.NEGATIVE_INFINITY, "%#.0e", "-Infinity" }, { Float.NEGATIVE_INFINITY, "%#- (9.8e", "(Infinity)" }, { Float.NEGATIVE_INFINITY, "%#+0(8.4e", "(Infinity)" }, { Float.NEGATIVE_INFINITY, "%-+(1.6e", "(Infinity)" }, { Float.NEGATIVE_INFINITY, "% 0(12e", " (Infinity)" }, { 0d, "%e", "0.000000e+00" }, { 0d, "%#.0e", "0.e+00" }, { 0d, "%#- (9.8e", " 0.00000000e+00" }, { 0d, "%#+0(8.4e", "+0.0000e+00" }, { 0d, "%-+(1.6e", "+0.000000e+00" }, { 0d, "% 0(12e", " 0.000000e+00" }, { 1d, "%e", "1.000000e+00" }, { 1d, "%#.0e", "1.e+00" }, { 1d, "%#- (9.8e", " 1.00000000e+00" }, { 1d, "%#+0(8.4e", "+1.0000e+00" }, { 1d, "%-+(1.6e", "+1.000000e+00" }, { 1d, "% 0(12e", " 1.000000e+00" }, { -1d, "%e", "-1.000000e+00" }, { -1d, "%#.0e", "-1.e+00" }, { -1d, "%#- (9.8e", "(1.00000000e+00)" }, { -1d, "%#+0(8.4e", "(1.0000e+00)" }, { -1d, "%-+(1.6e", "(1.000000e+00)" }, { -1d, "% 0(12e", "(1.000000e+00)" }, { .00000001d, "%e", "1.000000e-08" }, { .00000001d, "%#.0e", "1.e-08" }, { .00000001d, "%#- (9.8e", " 1.00000000e-08" }, { .00000001d, "%#+0(8.4e", "+1.0000e-08" }, { .00000001d, "%-+(1.6e", "+1.000000e-08" }, { .00000001d, "% 0(12e", " 1.000000e-08" }, { 9122.10d, "%e", "9.122100e+03" }, { 9122.10d, "%#.0e", "9.e+03" }, { 9122.10d, "%#- (9.8e", " 9.12210000e+03" }, { 9122.10d, "%#+0(8.4e", "+9.1221e+03" }, { 9122.10d, "%-+(1.6e", "+9.122100e+03" }, { 9122.10d, "% 0(12e", " 9.122100e+03" }, { 0.1d, "%e", "1.000000e-01" }, { 0.1d, "%#.0e", "1.e-01" }, { 0.1d, "%#- (9.8e", " 1.00000000e-01" }, { 0.1d, "%#+0(8.4e", "+1.0000e-01" }, { 0.1d, "%-+(1.6e", "+1.000000e-01" }, { 0.1d, "% 0(12e", " 1.000000e-01" }, { -2.d, "%e", "-2.000000e+00" }, { -2.d, "%#.0e", "-2.e+00" }, { -2.d, "%#- (9.8e", "(2.00000000e+00)" }, { -2.d, "%#+0(8.4e", "(2.0000e+00)" }, { -2.d, "%-+(1.6e", "(2.000000e+00)" }, { -2.d, "% 0(12e", "(2.000000e+00)" }, { -.39d, "%e", "-3.900000e-01" }, { -.39d, "%#.0e", "-4.e-01" }, { -.39d, "%#- (9.8e", "(3.90000000e-01)" }, { -.39d, "%#+0(8.4e", "(3.9000e-01)" }, { -.39d, "%-+(1.6e", "(3.900000e-01)" }, { -.39d, "% 0(12e", "(3.900000e-01)" }, { -1234567890.012345678d, "%e", "-1.234568e+09" }, { -1234567890.012345678d, "%#.0e", "-1.e+09" }, { -1234567890.012345678d, "%#- (9.8e", "(1.23456789e+09)" }, { -1234567890.012345678d, "%#+0(8.4e", "(1.2346e+09)" }, { -1234567890.012345678d, "%-+(1.6e", "(1.234568e+09)" }, { -1234567890.012345678d, "% 0(12e", "(1.234568e+09)" }, { Double.MAX_VALUE, "%e", "1.797693e+308" }, { Double.MAX_VALUE, "%#.0e", "2.e+308" }, { Double.MAX_VALUE, "%#- (9.8e", " 1.79769313e+308" }, { Double.MAX_VALUE, "%#+0(8.4e", "+1.7977e+308" }, { Double.MAX_VALUE, "%-+(1.6e", "+1.797693e+308" }, { Double.MAX_VALUE, "% 0(12e", " 1.797693e+308" }, { Double.MIN_VALUE, "%e", "4.900000e-324" }, { Double.MIN_VALUE, "%#.0e", "5.e-324" }, { Double.MIN_VALUE, "%#- (9.8e", " 4.90000000e-324" }, { Double.MIN_VALUE, "%#+0(8.4e", "+4.9000e-324" }, { Double.MIN_VALUE, "%-+(1.6e", "+4.900000e-324" }, { Double.MIN_VALUE, "% 0(12e", " 4.900000e-324" }, { Double.NaN, "%e", "NaN" }, { Double.NaN, "%#.0e", "NaN" }, { Double.NaN, "%#- (9.8e", "NaN " }, { Double.NaN, "%#+0(8.4e", " NaN" }, { Double.NaN, "%-+(1.6e", "NaN" }, { Double.NaN, "% 0(12e", " NaN" }, { Double.NEGATIVE_INFINITY, "%e", "-Infinity" }, { Double.NEGATIVE_INFINITY, "%#.0e", "-Infinity" }, { Double.NEGATIVE_INFINITY, "%#- (9.8e", "(Infinity)" }, { Double.NEGATIVE_INFINITY, "%#+0(8.4e", "(Infinity)" }, { Double.NEGATIVE_INFINITY, "%-+(1.6e", "(Infinity)" }, { Double.NEGATIVE_INFINITY, "% 0(12e", " (Infinity)" }, { Double.POSITIVE_INFINITY, "%e", "Infinity" }, { Double.POSITIVE_INFINITY, "%#.0e", "Infinity" }, { Double.POSITIVE_INFINITY, "%#- (9.8e", " Infinity" }, { Double.POSITIVE_INFINITY, "%#+0(8.4e", "+Infinity" }, { Double.POSITIVE_INFINITY, "%-+(1.6e", "+Infinity" }, { Double.POSITIVE_INFINITY, "% 0(12e", " Infinity" } };
final int input = 0;
final int pattern = 1;
final int output = 2;
for (int i = 0; i < tripleE.length; i++) {
f = new Formatter(Locale.US);
f.format((String) tripleE[i][pattern], tripleE[i][input]);
assertEquals("triple[" + i + "]:" + tripleE[i][input] + ",pattern[" + i + "]:" + tripleE[i][pattern], tripleE[i][output], f.toString());
// test for conversion type 'E'
f = new Formatter(Locale.US);
f.format(((String) tripleE[i][pattern]).toUpperCase(), tripleE[i][input]);
assertEquals("triple[" + i + "]:" + tripleE[i][input] + ",pattern[" + i + "]:" + tripleE[i][pattern], ((String) tripleE[i][output]).toUpperCase(Locale.UK), f.toString());
}
f = new Formatter(Locale.GERMAN);
f.format("%e", 1001f);
/*
* fail on RI, spec says 'e' requires the output to be formatted in
* general scientific notation and the localization algorithm is
* applied. But RI format this case to 1.001000e+03, which does not
* conform to the German Locale
*/
assertEquals("1,001000e+03", f.toString());
}
use of java.util.Formatter in project j2objc by google.
the class FormatterTest method test_formatLjava_lang_String$LBigInteger.
/**
* java.util.Formatter#format(String, Object...) for legal
* BigInteger conversion type 'd'
*/
public void test_formatLjava_lang_String$LBigInteger() {
final Object[][] tripleD = { //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%d", "123456789012345678901234567890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%10d", "123456789012345678901234567890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%-1d", "123456789012345678901234567890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%+d", "+123456789012345678901234567890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "% d", " 123456789012345678901234567890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%,d", "123.456.789.012.345.678.901.234.567.890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%(d", "123456789012345678901234567890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%08d", "123456789012345678901234567890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%-+,(11d", "+123.456.789.012.345.678.901.234.567.890" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%0 ,(11d", " 123.456.789.012.345.678.901.234.567.890" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%d", "-9876543210987654321098765432100000" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%10d", "-9876543210987654321098765432100000" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%-1d", "-9876543210987654321098765432100000" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%+d", "-9876543210987654321098765432100000" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "% d", "-9876543210987654321098765432100000" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%,d", "-9.876.543.210.987.654.321.098.765.432.100.000" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%(d", "(9876543210987654321098765432100000)" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%08d", "-9876543210987654321098765432100000" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%-+,(11d", "(9.876.543.210.987.654.321.098.765.432.100.000)" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%0 ,(11d", "(9.876.543.210.987.654.321.098.765.432.100.000)" } };
final int input = 0;
final int pattern = 1;
final int output = 2;
Formatter f;
for (int i = 0; i < tripleD.length; i++) {
f = new Formatter(Locale.GERMAN);
f.format((String) tripleD[i][pattern], tripleD[i][input]);
assertEquals("triple[" + i + "]:" + tripleD[i][input] + ",pattern[" + i + "]:" + tripleD[i][pattern], tripleD[i][output], f.toString());
}
final Object[][] tripleO = { //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%o", "143564417755415637016711617605322" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%-6o", "143564417755415637016711617605322" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%08o", "143564417755415637016711617605322" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%#o", "0143564417755415637016711617605322" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%0#11o", "0143564417755415637016711617605322" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%-#9o", "0143564417755415637016711617605322" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%o", "-36336340043453651353467270113157312240" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%-6o", "-36336340043453651353467270113157312240" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%08o", "-36336340043453651353467270113157312240" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%#o", "-036336340043453651353467270113157312240" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%0#11o", "-036336340043453651353467270113157312240" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%-#9o", "-036336340043453651353467270113157312240" } };
for (int i = 0; i < tripleO.length; i++) {
f = new Formatter(Locale.ITALY);
f.format((String) tripleO[i][pattern], tripleO[i][input]);
assertEquals("triple[" + i + "]:" + tripleO[i][input] + ",pattern[" + i + "]:" + tripleO[i][pattern], tripleO[i][output], f.toString());
}
final Object[][] tripleX = { //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%x", "18ee90ff6c373e0ee4e3f0ad2" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%-8x", "18ee90ff6c373e0ee4e3f0ad2" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%06x", "18ee90ff6c373e0ee4e3f0ad2" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%#x", "0x18ee90ff6c373e0ee4e3f0ad2" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%0#12x", "0x18ee90ff6c373e0ee4e3f0ad2" }, //$NON-NLS-2$
{ new BigInteger("123456789012345678901234567890"), "%-#9x", "0x18ee90ff6c373e0ee4e3f0ad2" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%x", "-1e6f380472bd4bae6eb8259bd94a0" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%-8x", "-1e6f380472bd4bae6eb8259bd94a0" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%06x", "-1e6f380472bd4bae6eb8259bd94a0" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%#x", "-0x1e6f380472bd4bae6eb8259bd94a0" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%0#12x", "-0x1e6f380472bd4bae6eb8259bd94a0" }, //$NON-NLS-2$
{ new BigInteger("-9876543210987654321098765432100000"), "%-#9x", "-0x1e6f380472bd4bae6eb8259bd94a0" } };
for (int i = 0; i < tripleX.length; i++) {
f = new Formatter(Locale.FRANCE);
f.format((String) tripleX[i][pattern], tripleX[i][input]);
assertEquals("triple[" + i + "]:" + tripleX[i][input] + ",pattern[" + i + "]:" + tripleX[i][pattern], tripleX[i][output], f.toString());
}
f = new Formatter(Locale.GERMAN);
f.format("%(+,-7d%<( o%<+(x %<( 06X", (BigInteger) null);
assertEquals("null nullnull NULL", f.toString());
}
use of java.util.Formatter in project j2objc by google.
the class FormatterTest method test_ioException.
/**
* java.util.Formatter#ioException()
*/
public void test_ioException() throws IOException {
Formatter f = null;
f = new Formatter(new MockDestination());
assertNull(f.ioException());
f.flush();
assertNotNull(f.ioException());
f.close();
MockDestination md = new MockDestination();
f = new Formatter(md);
f.format("%s%s", "1", "2");
// format stop working after IOException
assertNotNull(f.ioException());
assertEquals("", f.toString());
}
use of java.util.Formatter in project j2objc by google.
the class FormatterTest method test_ConstructorLjava_io_FileLjava_lang_StringLjava_util_Locale.
/**
* java.util.Formatter#Formatter(File, String, Locale)
*/
public void test_ConstructorLjava_io_FileLjava_lang_StringLjava_util_Locale() throws IOException {
Formatter f = null;
try {
f = new Formatter((File) null, Charset.defaultCharset().name(), Locale.KOREA);
fail("should throw NullPointerException");
} catch (NullPointerException e1) {
// expected
}
try {
f = new Formatter(notExist, null, Locale.KOREA);
fail("should throw NullPointerException");
} catch (NullPointerException e2) {
// expected
}
f = new Formatter(notExist, Charset.defaultCharset().name(), null);
assertNotNull(f);
f.close();
f = new Formatter(notExist, Charset.defaultCharset().name(), Locale.KOREA);
assertEquals(f.locale(), Locale.KOREA);
f.close();
try {
f = new Formatter(notExist, "ISO 1111-1", Locale.CHINA);
fail("should throw UnsupportedEncodingException");
} catch (UnsupportedEncodingException e1) {
// expected
}
f = new Formatter(fileWithContent.getPath(), "UTF-16BE", Locale.CANADA_FRENCH);
assertEquals(0, fileWithContent.length());
f.close();
if (!root) {
try {
f = new Formatter(readOnly.getPath(), Charset.defaultCharset().name(), Locale.ITALY);
fail("should throw FileNotFoundException");
} catch (FileNotFoundException e) {
// expected
}
}
}
Aggregations