jjg@409: /* jjg@409: * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. jjg@409: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@409: * jjg@409: * This code is free software; you can redistribute it and/or modify it jjg@409: * under the terms of the GNU General Public License version 2 only, as jjg@409: * published by the Free Software Foundation. jjg@409: * jjg@409: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@409: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@409: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@409: * version 2 for more details (a copy is included in the LICENSE file that jjg@409: * accompanied this code). jjg@409: * jjg@409: * You should have received a copy of the GNU General Public License version jjg@409: * 2 along with this work; if not, write to the Free Software Foundation, jjg@409: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@409: * jjg@409: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@409: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@409: * have any questions. jjg@409: */ jjg@409: jjg@409: /* jjg@409: * @test jjg@409: * @bug 6860973 jjg@409: * @summary Project Coin: Underscores in literals jjg@409: */ jjg@409: jjg@409: jjg@409: public class UnderscoreLiterals { jjg@409: public static void main(String... args) throws Exception { jjg@409: new UnderscoreLiterals().run(); jjg@409: } jjg@409: jjg@409: public void run() throws Exception { jjg@409: // decimal jjg@409: test(1, 1); jjg@409: test(10, 10); jjg@409: test(1_0, 10); jjg@409: test(1__0, 10); jjg@409: test(1_0_0, 100); jjg@409: test(1__0__0, 100); jjg@409: test(123_456_789, 123456789); jjg@409: jjg@409: // long jjg@409: test(1l, 1l); jjg@409: test(10l, 10l); jjg@409: test(1_0l, 10l); jjg@409: test(1__0l, 10l); jjg@409: test(1_0_0l, 100l); jjg@409: test(1__0__0l, 100l); jjg@409: test(123_456_789l, 123456789l); jjg@409: jjg@409: // float jjg@409: test(.1f, .1f); jjg@409: test(.10f, .10f); jjg@409: test(.1_0f, .10f); jjg@409: test(.1__0f, .10f); jjg@409: test(.1_0_0f, .100f); jjg@409: test(.1__0__0f, .100f); jjg@409: test(1e1, 1e1); jjg@409: test(1e10, 1e10); jjg@409: test(1e1_0, 1e10); jjg@409: test(1e1__0, 1e10); jjg@409: test(1e1_0_0, 1e100); jjg@409: test(1e1__0__0, 1e100); jjg@409: test(.123_456_789f, .123456789f); jjg@409: test(0.1f, 0.1f); jjg@409: test(0.10f, 0.10f); jjg@409: test(0.1_0f, 0.10f); jjg@409: test(0.1__0f, 0.10f); jjg@409: test(0.1_0_0f, 0.100f); jjg@409: test(0.1__0__0f, 0.100f); jjg@409: test(0.123_456_789f, 0.123456789f); jjg@409: test(1_1.1f, 1_1.1f); jjg@409: test(1_1.10f, 1_1.10f); jjg@409: test(1_1.1_0f, 1_1.10f); jjg@409: test(1_1.1__0f, 1_1.10f); jjg@409: test(1_1.1_0_0f, 1_1.100f); jjg@409: test(1_1.1__0__0f, 1_1.100f); jjg@409: test(1_1.123_456_789f, 1_1.123456789f); jjg@409: jjg@409: // double jjg@409: test(.1d, .1d); jjg@409: test(.10d, .10d); jjg@409: test(.1_0d, .10d); jjg@409: test(.1__0d, .10d); jjg@409: test(.1_0_0d, .100d); jjg@409: test(.1__0__0d, .100d); jjg@409: test(1e1, 1e1); jjg@409: test(1e10, 1e10); jjg@409: test(1e1_0, 1e10); jjg@409: test(1e1__0, 1e10); jjg@409: test(1e1_0_0, 1e100); jjg@409: test(1e1__0__0, 1e100); jjg@409: test(.123_456_789d, .123456789d); jjg@409: test(0.1d, 0.1d); jjg@409: test(0.10d, 0.10d); jjg@409: test(0.1_0d, 0.10d); jjg@409: test(0.1__0d, 0.10d); jjg@409: test(0.1_0_0d, 0.100d); jjg@409: test(0.1__0__0d, 0.100d); jjg@409: test(0.123_456_789d, 0.123456789d); jjg@409: test(1_1.1d, 1_1.1d); jjg@409: test(1_1.10d, 1_1.10d); jjg@409: test(1_1.1_0d, 1_1.10d); jjg@409: test(1_1.1__0d, 1_1.10d); jjg@409: test(1_1.1_0_0d, 1_1.100d); jjg@409: test(1_1.1__0__0d, 1_1.100d); jjg@409: test(1_1.123_456_789d, 1_1.123456789d); jjg@409: jjg@409: // binary jjg@409: test(0b1, 1); jjg@409: test(0b10, 2); jjg@409: test(0b1_0, 2); jjg@409: test(0b1__0, 2); jjg@409: test(0b1_0_0, 4); jjg@409: test(0b1__0__0, 4); jjg@409: test(0b0001_0010_0011, 0x123); jjg@409: jjg@409: // octal jjg@409: test(01, 1); jjg@409: test(010, 8); jjg@409: test(01_0, 8); jjg@409: test(01__0, 8); jjg@409: test(01_0_0, 64); jjg@409: test(01__0__0, 64); jjg@409: test(0_1, 1); jjg@409: test(0_10, 8); jjg@409: test(0_1_0, 8); jjg@409: test(0_1__0, 8); jjg@409: test(0_1_0_0, 64); jjg@409: test(0_1__0__0, 64); jjg@409: test(0_001_002_003, 01002003); jjg@409: jjg@409: // hexadecimal jjg@409: test(0x1, 1); jjg@409: test(0x10, 16); jjg@409: test(0x1_0, 16); jjg@409: test(0x1__0, 16); jjg@409: test(0x1_0_0, 256); jjg@409: test(0x1__0__0, 256); jjg@409: test(0x01_02_03_04, 0x1020304); jjg@409: jjg@409: // misc jjg@409: long creditCardNumber = 1234_5678_9012_3456L; jjg@409: test(creditCardNumber, 1234567890123456L); jjg@409: long socialSecurityNumbers = 999_99_9999L; jjg@409: test(socialSecurityNumbers, 999999999L); jjg@409: double monetaryAmount = 12_345_132.12d; jjg@409: test(monetaryAmount, 12345132.12d); jjg@409: long hexBytes = 0xFF_EC_DE_5E; jjg@409: test(hexBytes, 0xffecde5e); jjg@409: long hexWords = 0xFFEC_DE5E; jjg@409: test(hexWords, 0xffecde5e); jjg@409: long maxLong = 0x7fff_ffff_ffff_ffffL; jjg@409: test(maxLong, 0x7fffffffffffffffL); jjg@409: long maxLongDecimal = 9223372036854775807L; jjg@409: long alsoMaxLong = 9_223_372_036_854_775_807L; jjg@409: test(alsoMaxLong, maxLongDecimal); jjg@409: double whyWouldYouEverDoThis = 0x1.ffff_ffff_ffff_fp10_23; jjg@409: double whyWouldYouEverDoEvenThis = 0x1.fffffffffffffp1023; jjg@409: test(whyWouldYouEverDoThis, whyWouldYouEverDoEvenThis); jjg@409: jjg@409: if (errors > 0) jjg@409: throw new Exception(errors + " errors found"); jjg@409: } jjg@409: jjg@409: void test(int value, int expect) { jjg@409: count++; jjg@409: if (value != expect) jjg@409: error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n found: 0x" + Integer.toHexString(value)); jjg@409: } jjg@409: jjg@409: void test(double value, double expect) { jjg@409: count++; jjg@409: if (value != expect) jjg@409: error("test " + count + "\nexpected: 0x" + expect + "\n found: 0x" + value); jjg@409: } jjg@409: jjg@409: void test(long value, long expect) { jjg@409: count++; jjg@409: if (value != expect) jjg@409: error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n found: 0x" + Long.toHexString(value)); jjg@409: } jjg@409: jjg@409: void error(String message) { jjg@409: System.out.println(message); jjg@409: errors++; jjg@409: } jjg@409: jjg@409: int count; jjg@409: int errors; jjg@409: }