6860965: Project Coin: binary literals
6860973: Project Coin: Underscores in literals
Summary: [Portions contributed by Bruce Chapman]
Reviewed-by: darcy
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Source.java Tue Sep 15 12:20:55 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Source.java Tue Sep 15 18:36:21 2009 -0700
@@ -159,6 +159,12 @@
public boolean allowTypeAnnotations() {
return compareTo(JDK1_7) >= 0;
}
+ public boolean allowBinaryLiterals() {
+ return compareTo(JDK1_7) >= 0;
+ }
+ public boolean allowUnderscoresInLiterals() {
+ return compareTo(JDK1_7) >= 0;
+ }
public static SourceVersion toSourceVersion(Source source) {
switch(source) {
case JDK1_2:
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/Scanner.java Tue Sep 15 12:20:55 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/Scanner.java Tue Sep 15 18:36:21 2009 -0700
@@ -100,6 +100,18 @@
*/
private boolean allowHexFloats;
+ /** Allow binary literals.
+ */
+ private boolean allowBinaryLiterals;
+
+ /** Allow underscores in literals.
+ */
+ private boolean allowUnderscoresInLiterals;
+
+ /** The source language setting.
+ */
+ private Source source;
+
/** The token's position, 0-based offset from beginning of text.
*/
private int pos;
@@ -162,10 +174,13 @@
/** Common code for constructors. */
private Scanner(Factory fac) {
- this.log = fac.log;
- this.names = fac.names;
- this.keywords = fac.keywords;
- this.allowHexFloats = fac.source.allowHexFloats();
+ log = fac.log;
+ names = fac.names;
+ keywords = fac.keywords;
+ source = fac.source;
+ allowBinaryLiterals = source.allowBinaryLiterals();
+ allowHexFloats = source.allowHexFloats();
+ allowUnderscoresInLiterals = source.allowBinaryLiterals();
}
private static final boolean hexFloatsWork = hexFloatsWork();
@@ -396,23 +411,42 @@
scanLitChar(true);
}
+ private void scanDigits(int digitRadix) {
+ char saveCh;
+ int savePos;
+ do {
+ if (ch != '_') {
+ putChar(ch);
+ } else {
+ if (!allowUnderscoresInLiterals) {
+ lexError("unsupported.underscore", source.name);
+ allowUnderscoresInLiterals = true;
+ }
+ }
+ saveCh = ch;
+ savePos = bp;
+ scanChar();
+ } while (digit(digitRadix) >= 0 || ch == '_');
+ if (saveCh == '_')
+ lexError(savePos, "illegal.underscore");
+ }
+
/** Read fractional part of hexadecimal floating point number.
*/
private void scanHexExponentAndSuffix() {
if (ch == 'p' || ch == 'P') {
putChar(ch);
scanChar();
+ skipIllegalUnderscores();
if (ch == '+' || ch == '-') {
putChar(ch);
scanChar();
}
+ skipIllegalUnderscores();
if ('0' <= ch && ch <= '9') {
- do {
- putChar(ch);
- scanChar();
- } while ('0' <= ch && ch <= '9');
+ scanDigits(10);
if (!allowHexFloats) {
- lexError("unsupported.fp.lit");
+ lexError("unsupported.fp.lit", source.name);
allowHexFloats = true;
}
else if (!hexFloatsWork)
@@ -438,23 +472,22 @@
/** Read fractional part of floating point number.
*/
private void scanFraction() {
- while (digit(10) >= 0) {
- putChar(ch);
- scanChar();
+ skipIllegalUnderscores();
+ if ('0' <= ch && ch <= '9') {
+ scanDigits(10);
}
int sp1 = sp;
if (ch == 'e' || ch == 'E') {
putChar(ch);
scanChar();
+ skipIllegalUnderscores();
if (ch == '+' || ch == '-') {
putChar(ch);
scanChar();
}
+ skipIllegalUnderscores();
if ('0' <= ch && ch <= '9') {
- do {
- putChar(ch);
- scanChar();
- } while ('0' <= ch && ch <= '9');
+ scanDigits(10);
return;
}
lexError("malformed.fp.lit");
@@ -487,10 +520,10 @@
assert ch == '.';
putChar(ch);
scanChar();
- while (digit(16) >= 0) {
+ skipIllegalUnderscores();
+ if (digit(16) >= 0) {
seendigit = true;
- putChar(ch);
- scanChar();
+ scanDigits(16);
}
if (!seendigit)
lexError("invalid.hex.number");
@@ -498,28 +531,35 @@
scanHexExponentAndSuffix();
}
+ private void skipIllegalUnderscores() {
+ if (ch == '_') {
+ lexError(bp, "illegal.underscore");
+ while (ch == '_')
+ scanChar();
+ }
+ }
+
/** Read a number.
- * @param radix The radix of the number; one of 8, 10, 16.
+ * @param radix The radix of the number; one of 2, j8, 10, 16.
*/
private void scanNumber(int radix) {
this.radix = radix;
// for octal, allow base-10 digit in case it's a float literal
- int digitRadix = (radix <= 10) ? 10 : 16;
+ int digitRadix = (radix == 8 ? 10 : radix);
boolean seendigit = false;
- while (digit(digitRadix) >= 0) {
+ if (digit(digitRadix) >= 0) {
seendigit = true;
- putChar(ch);
- scanChar();
+ scanDigits(digitRadix);
}
if (radix == 16 && ch == '.') {
scanHexFractionAndSuffix(seendigit);
} else if (seendigit && radix == 16 && (ch == 'p' || ch == 'P')) {
scanHexExponentAndSuffix();
- } else if (radix <= 10 && ch == '.') {
+ } else if (digitRadix == 10 && ch == '.') {
putChar(ch);
scanChar();
scanFractionAndSuffix();
- } else if (radix <= 10 &&
+ } else if (digitRadix == 10 &&
(ch == 'e' || ch == 'E' ||
ch == 'f' || ch == 'F' ||
ch == 'd' || ch == 'D')) {
@@ -821,6 +861,7 @@
scanChar();
if (ch == 'x' || ch == 'X') {
scanChar();
+ skipIllegalUnderscores();
if (ch == '.') {
scanHexFractionAndSuffix(false);
} else if (digit(16) < 0) {
@@ -828,8 +869,25 @@
} else {
scanNumber(16);
}
+ } else if (ch == 'b' || ch == 'B') {
+ if (!allowBinaryLiterals) {
+ lexError("unsupported.binary.lit", source.name);
+ allowBinaryLiterals = true;
+ }
+ scanChar();
+ skipIllegalUnderscores();
+ scanNumber(2);
} else {
putChar('0');
+ if (ch == '_') {
+ int savePos = bp;
+ do {
+ scanChar();
+ } while (ch == '_');
+ if (digit(10) < 0) {
+ lexError(savePos, "illegal.underscore");
+ }
+ }
scanNumber(8);
}
return;
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Sep 15 12:20:55 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Sep 15 18:36:21 2009 -0700
@@ -216,6 +216,8 @@
illegal line end in character literal
compiler.err.illegal.nonascii.digit=\
illegal non-ASCII digit
+compiler.err.illegal.underscore=\
+ illegal underscore
compiler.err.illegal.qual.not.icls=\
illegal qualifier; {0} is not an inner class
compiler.err.illegal.start.of.expr=\
@@ -1163,7 +1165,16 @@
# Diagnostics for language feature changes
########################################
compiler.err.unsupported.fp.lit=\
- hexadecimal floating-point literals are not supported before -source 5
+ hexadecimal floating point literals are not supported in -source {0}\n\
+(use -source 5 or higher to enable hexadecimal floating point literals)
+
+compiler.err.unsupported.binary.lit=\
+ binary literals are not supported in -source {0}\n\
+(use -source 7 or higher to enable binary literals)
+
+compiler.err.unsupported.underscore.lit=\
+ underscores in literals are not supported in -source {0}\n\
+(use -source 7 or higher to enable underscores in literals)
compiler.warn.enum.as.identifier=\
as of release 5, ''enum'' is a keyword, and may not be used as an identifier\n\
--- a/langtools/test/tools/javac/enum/6384542/T6384542.out Tue Sep 15 12:20:55 2009 -0700
+++ b/langtools/test/tools/javac/enum/6384542/T6384542.out Tue Sep 15 18:36:21 2009 -0700
@@ -1,6 +1,6 @@
T6384542.java:10:8: compiler.err.static.import.not.supported.in.source: 1.4
T6384542.java:12:8: compiler.err.enums.not.supported.in.source: 1.4
-T6384542.java:14:13: compiler.err.unsupported.fp.lit
+T6384542.java:14:13: compiler.err.unsupported.fp.lit: 1.4
T6384542.java:15:9: compiler.err.generics.not.supported.in.source: 1.4
T6384542.java:16:35: compiler.err.varargs.not.supported.in.source: 1.4
T6384542.java:17:25: compiler.err.foreach.not.supported.in.source: 1.4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/BadBinaryLiterals.6.out Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,8 @@
+BadBinaryLiterals.java:10:17: compiler.err.unsupported.binary.lit: 1.6
+BadBinaryLiterals.java:11:24: compiler.err.expected: ';'
+BadBinaryLiterals.java:13:21: compiler.err.int.number.too.large: 111111111111111111111111111111111
+BadBinaryLiterals.java:15:21: compiler.err.int.number.too.large: 11111111111111111111111111111111111111111111111111111111111111111
+BadBinaryLiterals.java:16:27: compiler.err.expected: ';'
+BadBinaryLiterals.java:17:27: compiler.err.expected: ';'
+BadBinaryLiterals.java:17:30: compiler.err.expected: token.identifier
+7 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/BadBinaryLiterals.7.out Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,7 @@
+BadBinaryLiterals.java:11:24: compiler.err.expected: ';'
+BadBinaryLiterals.java:13:21: compiler.err.int.number.too.large: 111111111111111111111111111111111
+BadBinaryLiterals.java:15:21: compiler.err.int.number.too.large: 11111111111111111111111111111111111111111111111111111111111111111
+BadBinaryLiterals.java:16:27: compiler.err.expected: ';'
+BadBinaryLiterals.java:17:27: compiler.err.expected: ';'
+BadBinaryLiterals.java:17:30: compiler.err.expected: token.identifier
+6 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/BadBinaryLiterals.java Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,18 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 6860965
+ * @summary Project Coin: binary literals
+ * @compile/fail/ref=BadBinaryLiterals.6.out -XDrawDiagnostics -source 6 BadBinaryLiterals.java
+ * @compile/fail/ref=BadBinaryLiterals.7.out -XDrawDiagnostics BadBinaryLiterals.java
+ */
+
+public class BadBinaryLiterals {
+ int valid = 0b0; // valid literal, illegal in source 6
+ int baddigit = 0b012; // bad digit
+ //aaaabbbbccccddddeeeeffffgggghhhh
+ int overflow1 = 0b111111111111111111111111111111111; // too long for int
+ //aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooopppp
+ int overflow2 = 0b11111111111111111111111111111111111111111111111111111111111111111L; // too long for long
+ float badfloat1 = 0b01.01; // no binary floats
+ float badfloat2 = 0b01e01; // no binary floats
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/BadUnderscoreLiterals.6.out Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,21 @@
+BadUnderscoreLiterals.java:14:17: compiler.err.unsupported.underscore: 1.6
+BadUnderscoreLiterals.java:18:15: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:22:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:25:14: compiler.err.unsupported.binary.lit: 1.6
+BadUnderscoreLiterals.java:25:16: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:26:17: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:29:16: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:30:17: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:33:17: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:34:18: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:35:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:36:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:37:18: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:38:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:41:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:42:20: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:43:21: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:44:22: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:45:21: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:46:22: compiler.err.illegal.underscore
+20 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/BadUnderscoreLiterals.7.out Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,19 @@
+BadUnderscoreLiterals.java:18:15: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:22:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:25:16: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:26:17: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:29:16: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:30:17: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:33:17: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:34:18: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:35:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:36:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:37:18: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:38:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:41:19: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:42:20: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:43:21: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:44:22: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:45:21: compiler.err.illegal.underscore
+BadUnderscoreLiterals.java:46:22: compiler.err.illegal.underscore
+18 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/BadUnderscoreLiterals.java Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,48 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 6860973
+ * @summary Project Coin: underscores in literals
+ *
+ * @compile/fail BadUnderscoreLiterals.java
+ * @compile/fail/ref=BadUnderscoreLiterals.7.out -XDrawDiagnostics BadUnderscoreLiterals.java
+ *
+ * @compile/fail -source 6 BadUnderscoreLiterals.java
+ * @compile/fail/ref=BadUnderscoreLiterals.6.out -XDrawDiagnostics -source 6 BadUnderscoreLiterals.java
+ */
+
+public class BadUnderscoreLiterals {
+ int valid = 1_1; // valid literal; illegal in -source 6
+
+ // test zero
+ int z1 = _0; // valid (but undefined) variable
+ int z2 = 0_; // trailing underscore
+
+ // test simple (decimal) integers
+ int i1 = _1_2_3; // valid (but undefined) variable
+ int i2 = 1_2_3_; // trailing underscore
+
+ // test binary integers
+ int b1 = 0b_0; // leading underscore after radix
+ int b2 = 0b0_; // trailing underscore
+
+ // test hexadecimal integers
+ int x1 = 0x_0; // leading underscore after radix
+ int x2 = 0x0_; // trailing underscore
+
+ // test floating point numbers
+ float f1 = 0_.1; // trailing underscore before decimal point
+ float f2 = 0._1; // leading underscore after decimal point
+ float f3 = 0.1_; // trailing underscore
+ float f4 = 0.1_e0; // trailing underscore before exponent
+ float f5 = 0e_1; // leading underscore in exponent
+ float f6 = 0e1_; // trailing underscore in exponent
+
+ // hexadecimal floating point
+ float xf1 = 0x_0.1p0; // leading underscore after radix
+ float xf2 = 0x0_.1p0; // trailing underscore before decimal point
+ float xf3 = 0x0._1p0; // leading underscore after decimal point
+ float xf4 = 0x0.1_p0; // trailing underscore before exponent
+ float xf5 = 0x0p_1; // leading underscore after exponent
+ float xf6 = 0x0p1_; // trailing underscore
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/BinaryLiterals.java Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6860965
+ * @summary Project Coin: binary literals
+ */
+
+public class BinaryLiterals {
+ public static void main(String... args) throws Exception {
+ new BinaryLiterals().run();
+ }
+
+ public void run() throws Exception {
+ test(0, 0B0);
+ test(1, 0B1);
+ test(2, 0B10);
+ test(3, 0B11);
+
+ test(0, 0b0);
+ test(1, 0b1);
+ test(2, 0b10);
+ test(3, 0b11);
+
+ test(-0, -0b0);
+ test(-1, -0b1);
+ test(-2, -0b10);
+ test(-3, -0b11);
+
+ test(-1, 0b11111111111111111111111111111111);
+ test(-2, 0b11111111111111111111111111111110);
+ test(-3, 0b11111111111111111111111111111101);
+
+ test( 1, -0b11111111111111111111111111111111);
+ test( 2, -0b11111111111111111111111111111110);
+ test( 3, -0b11111111111111111111111111111101);
+
+ test(0, 0b00);
+ test(1, 0b001);
+ test(2, 0b00010);
+ test(3, 0b000011);
+
+ // aaaabbbbccccddddeeeeffffgggghhhh
+ test( 0x10, 0b10000);
+ test( 0x100, 0b100000000);
+ test( 0x10000, 0b10000000000000000);
+ test(0x80000000, 0b10000000000000000000000000000000);
+ test(0xffffffff, 0b11111111111111111111111111111111);
+
+ test(0L, 0b0L);
+ test(1L, 0b1L);
+ test(2L, 0b10L);
+ test(3L, 0b11L);
+
+ test(0, 0b00L);
+ test(1, 0b001L);
+ test(2, 0b00010L);
+ test(3, 0b000011L);
+
+ // aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooopppp
+ test( 0x10L, 0b10000L);
+ test( 0x100L, 0b100000000L);
+ test( 0x10000L, 0b10000000000000000L);
+ test( 0x80000000L, 0b10000000000000000000000000000000L);
+ test( 0xffffffffL, 0b11111111111111111111111111111111L);
+ test(0x8000000000000000L, 0b1000000000000000000000000000000000000000000000000000000000000000L);
+ test(0xffffffffffffffffL, 0b1111111111111111111111111111111111111111111111111111111111111111L);
+
+ test(0l, 0b0l);
+ test(1l, 0b1l);
+ test(2l, 0b10l);
+ test(3l, 0b11l);
+
+ test(0, 0b00l);
+ test(1, 0b001l);
+ test(2, 0b00010l);
+ test(3, 0b000011l);
+
+ // aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooopppp
+ test( 0x10l, 0b10000l);
+ test( 0x100l, 0b100000000l);
+ test( 0x10000l, 0b10000000000000000l);
+ test( 0x80000000l, 0b10000000000000000000000000000000l);
+ test( 0xffffffffl, 0b11111111111111111111111111111111l);
+ test(0x8000000000000000l, 0b1000000000000000000000000000000000000000000000000000000000000000l);
+ test(0xffffffffffffffffl, 0b1111111111111111111111111111111111111111111111111111111111111111l);
+
+ if (errors > 0)
+ throw new Exception(errors + " errors found");
+ }
+
+ void test(int expect, int found) {
+ count++;
+ if (found != expect)
+ error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n found: 0x" + Integer.toHexString(found));
+ }
+
+ void test(long expect, long found) {
+ count++;
+ if (found != expect)
+ error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n found: 0x" + Long.toHexString(found));
+ }
+
+ void error(String message) {
+ System.out.println(message);
+ errors++;
+ }
+
+ int count;
+ int errors;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/literals/UnderscoreLiterals.java Tue Sep 15 18:36:21 2009 -0700
@@ -0,0 +1,195 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6860973
+ * @summary Project Coin: Underscores in literals
+ */
+
+
+public class UnderscoreLiterals {
+ public static void main(String... args) throws Exception {
+ new UnderscoreLiterals().run();
+ }
+
+ public void run() throws Exception {
+ // decimal
+ test(1, 1);
+ test(10, 10);
+ test(1_0, 10);
+ test(1__0, 10);
+ test(1_0_0, 100);
+ test(1__0__0, 100);
+ test(123_456_789, 123456789);
+
+ // long
+ test(1l, 1l);
+ test(10l, 10l);
+ test(1_0l, 10l);
+ test(1__0l, 10l);
+ test(1_0_0l, 100l);
+ test(1__0__0l, 100l);
+ test(123_456_789l, 123456789l);
+
+ // float
+ test(.1f, .1f);
+ test(.10f, .10f);
+ test(.1_0f, .10f);
+ test(.1__0f, .10f);
+ test(.1_0_0f, .100f);
+ test(.1__0__0f, .100f);
+ test(1e1, 1e1);
+ test(1e10, 1e10);
+ test(1e1_0, 1e10);
+ test(1e1__0, 1e10);
+ test(1e1_0_0, 1e100);
+ test(1e1__0__0, 1e100);
+ test(.123_456_789f, .123456789f);
+ test(0.1f, 0.1f);
+ test(0.10f, 0.10f);
+ test(0.1_0f, 0.10f);
+ test(0.1__0f, 0.10f);
+ test(0.1_0_0f, 0.100f);
+ test(0.1__0__0f, 0.100f);
+ test(0.123_456_789f, 0.123456789f);
+ test(1_1.1f, 1_1.1f);
+ test(1_1.10f, 1_1.10f);
+ test(1_1.1_0f, 1_1.10f);
+ test(1_1.1__0f, 1_1.10f);
+ test(1_1.1_0_0f, 1_1.100f);
+ test(1_1.1__0__0f, 1_1.100f);
+ test(1_1.123_456_789f, 1_1.123456789f);
+
+ // double
+ test(.1d, .1d);
+ test(.10d, .10d);
+ test(.1_0d, .10d);
+ test(.1__0d, .10d);
+ test(.1_0_0d, .100d);
+ test(.1__0__0d, .100d);
+ test(1e1, 1e1);
+ test(1e10, 1e10);
+ test(1e1_0, 1e10);
+ test(1e1__0, 1e10);
+ test(1e1_0_0, 1e100);
+ test(1e1__0__0, 1e100);
+ test(.123_456_789d, .123456789d);
+ test(0.1d, 0.1d);
+ test(0.10d, 0.10d);
+ test(0.1_0d, 0.10d);
+ test(0.1__0d, 0.10d);
+ test(0.1_0_0d, 0.100d);
+ test(0.1__0__0d, 0.100d);
+ test(0.123_456_789d, 0.123456789d);
+ test(1_1.1d, 1_1.1d);
+ test(1_1.10d, 1_1.10d);
+ test(1_1.1_0d, 1_1.10d);
+ test(1_1.1__0d, 1_1.10d);
+ test(1_1.1_0_0d, 1_1.100d);
+ test(1_1.1__0__0d, 1_1.100d);
+ test(1_1.123_456_789d, 1_1.123456789d);
+
+ // binary
+ test(0b1, 1);
+ test(0b10, 2);
+ test(0b1_0, 2);
+ test(0b1__0, 2);
+ test(0b1_0_0, 4);
+ test(0b1__0__0, 4);
+ test(0b0001_0010_0011, 0x123);
+
+ // octal
+ test(01, 1);
+ test(010, 8);
+ test(01_0, 8);
+ test(01__0, 8);
+ test(01_0_0, 64);
+ test(01__0__0, 64);
+ test(0_1, 1);
+ test(0_10, 8);
+ test(0_1_0, 8);
+ test(0_1__0, 8);
+ test(0_1_0_0, 64);
+ test(0_1__0__0, 64);
+ test(0_001_002_003, 01002003);
+
+ // hexadecimal
+ test(0x1, 1);
+ test(0x10, 16);
+ test(0x1_0, 16);
+ test(0x1__0, 16);
+ test(0x1_0_0, 256);
+ test(0x1__0__0, 256);
+ test(0x01_02_03_04, 0x1020304);
+
+ // misc
+ long creditCardNumber = 1234_5678_9012_3456L;
+ test(creditCardNumber, 1234567890123456L);
+ long socialSecurityNumbers = 999_99_9999L;
+ test(socialSecurityNumbers, 999999999L);
+ double monetaryAmount = 12_345_132.12d;
+ test(monetaryAmount, 12345132.12d);
+ long hexBytes = 0xFF_EC_DE_5E;
+ test(hexBytes, 0xffecde5e);
+ long hexWords = 0xFFEC_DE5E;
+ test(hexWords, 0xffecde5e);
+ long maxLong = 0x7fff_ffff_ffff_ffffL;
+ test(maxLong, 0x7fffffffffffffffL);
+ long maxLongDecimal = 9223372036854775807L;
+ long alsoMaxLong = 9_223_372_036_854_775_807L;
+ test(alsoMaxLong, maxLongDecimal);
+ double whyWouldYouEverDoThis = 0x1.ffff_ffff_ffff_fp10_23;
+ double whyWouldYouEverDoEvenThis = 0x1.fffffffffffffp1023;
+ test(whyWouldYouEverDoThis, whyWouldYouEverDoEvenThis);
+
+ if (errors > 0)
+ throw new Exception(errors + " errors found");
+ }
+
+ void test(int value, int expect) {
+ count++;
+ if (value != expect)
+ error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n found: 0x" + Integer.toHexString(value));
+ }
+
+ void test(double value, double expect) {
+ count++;
+ if (value != expect)
+ error("test " + count + "\nexpected: 0x" + expect + "\n found: 0x" + value);
+ }
+
+ void test(long value, long expect) {
+ count++;
+ if (value != expect)
+ error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n found: 0x" + Long.toHexString(value));
+ }
+
+ void error(String message) {
+ System.out.println(message);
+ errors++;
+ }
+
+ int count;
+ int errors;
+}