test/tools/javac/literals/UnderscoreLiterals.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 6860973
aoqi@0 27 * @summary Project Coin: Underscores in literals
aoqi@0 28 */
aoqi@0 29
aoqi@0 30
aoqi@0 31 public class UnderscoreLiterals {
aoqi@0 32 public static void main(String... args) throws Exception {
aoqi@0 33 new UnderscoreLiterals().run();
aoqi@0 34 }
aoqi@0 35
aoqi@0 36 public void run() throws Exception {
aoqi@0 37 // decimal
aoqi@0 38 test(1, 1);
aoqi@0 39 test(10, 10);
aoqi@0 40 test(1_0, 10);
aoqi@0 41 test(1__0, 10);
aoqi@0 42 test(1_0_0, 100);
aoqi@0 43 test(1__0__0, 100);
aoqi@0 44 test(123_456_789, 123456789);
aoqi@0 45 test(2_147_483_647, Integer.MAX_VALUE);
aoqi@0 46 test(-2_147_483_648, Integer.MIN_VALUE);
aoqi@0 47 test(32_767, Short.MAX_VALUE);
aoqi@0 48 test(-32_768, Short.MIN_VALUE);
aoqi@0 49 test(1_2_7, Byte.MAX_VALUE);
aoqi@0 50 test(-1_2_8, Byte.MIN_VALUE);
aoqi@0 51
aoqi@0 52 // long
aoqi@0 53 test(1l, 1l);
aoqi@0 54 test(10l, 10l);
aoqi@0 55 test(1_0l, 10l);
aoqi@0 56 test(1__0l, 10l);
aoqi@0 57 test(1_0_0l, 100l);
aoqi@0 58 test(1__0__0l, 100l);
aoqi@0 59 test(123_456_789l, 123456789l);
aoqi@0 60 test(9_223_372_036_854_775_807l, Long.MAX_VALUE);
aoqi@0 61 test(-9_223_372_036_854_775_808l, Long.MIN_VALUE);
aoqi@0 62
aoqi@0 63 // float
aoqi@0 64 test(.1f, .1f);
aoqi@0 65 test(.10f, .10f);
aoqi@0 66 test(.1_0f, .10f);
aoqi@0 67 test(.1__0f, .10f);
aoqi@0 68 test(.1_0_0f, .100f);
aoqi@0 69 test(.1__0__0f, .100f);
aoqi@0 70 test(1e1, 1e1);
aoqi@0 71 test(1e10, 1e10);
aoqi@0 72 test(1e1_0, 1e10);
aoqi@0 73 test(1e1__0, 1e10);
aoqi@0 74 test(1e1_0_0, 1e100);
aoqi@0 75 test(1e1__0__0, 1e100);
aoqi@0 76 test(.123_456_789f, .123456789f);
aoqi@0 77 test(0.1f, 0.1f);
aoqi@0 78 test(0.10f, 0.10f);
aoqi@0 79 test(0.1_0f, 0.10f);
aoqi@0 80 test(0.1__0f, 0.10f);
aoqi@0 81 test(0.1_0_0f, 0.100f);
aoqi@0 82 test(0.1__0__0f, 0.100f);
aoqi@0 83 test(0.123_456_789f, 0.123456789f);
aoqi@0 84 test(1_1.1f, 1_1.1f);
aoqi@0 85 test(1_1.10f, 1_1.10f);
aoqi@0 86 test(1_1.1_0f, 1_1.10f);
aoqi@0 87 test(1_1.1__0f, 1_1.10f);
aoqi@0 88 test(1_1.1_0_0f, 1_1.100f);
aoqi@0 89 test(1_1.1__0__0f, 1_1.100f);
aoqi@0 90 test(1_1.123_456_789f, 1_1.123456789f);
aoqi@0 91 test(3.4_028_235E38f, Float.MAX_VALUE);
aoqi@0 92 test(1.4E-4_5f, Float.MIN_VALUE);
aoqi@0 93
aoqi@0 94 // double
aoqi@0 95 test(.1d, .1d);
aoqi@0 96 test(.10d, .10d);
aoqi@0 97 test(.1_0d, .10d);
aoqi@0 98 test(.1__0d, .10d);
aoqi@0 99 test(.1_0_0d, .100d);
aoqi@0 100 test(.1__0__0d, .100d);
aoqi@0 101 test(1e1, 1e1);
aoqi@0 102 test(1e10, 1e10);
aoqi@0 103 test(1e1_0, 1e10);
aoqi@0 104 test(1e1__0, 1e10);
aoqi@0 105 test(1e1_0_0, 1e100);
aoqi@0 106 test(1e1__0__0, 1e100);
aoqi@0 107 test(.123_456_789d, .123456789d);
aoqi@0 108 test(0.1d, 0.1d);
aoqi@0 109 test(0.10d, 0.10d);
aoqi@0 110 test(0.1_0d, 0.10d);
aoqi@0 111 test(0.1__0d, 0.10d);
aoqi@0 112 test(0.1_0_0d, 0.100d);
aoqi@0 113 test(0.1__0__0d, 0.100d);
aoqi@0 114 test(0.123_456_789d, 0.123456789d);
aoqi@0 115 test(1_1.1d, 1_1.1d);
aoqi@0 116 test(1_1.10d, 1_1.10d);
aoqi@0 117 test(1_1.1_0d, 1_1.10d);
aoqi@0 118 test(1_1.1__0d, 1_1.10d);
aoqi@0 119 test(1_1.1_0_0d, 1_1.100d);
aoqi@0 120 test(1_1.1__0__0d, 1_1.100d);
aoqi@0 121 test(1_1.123_456_789d, 1_1.123456789d);
aoqi@0 122 test(1.797_6_9_3_1_348_623_157E3_08, Double.MAX_VALUE);
aoqi@0 123 test(4.9E-3_24, Double.MIN_VALUE);
aoqi@0 124
aoqi@0 125 // binary
aoqi@0 126 test(0b1, 1);
aoqi@0 127 test(0b10, 2);
aoqi@0 128 test(0b1_0, 2);
aoqi@0 129 test(0b1__0, 2);
aoqi@0 130 test(0b1_0_0, 4);
aoqi@0 131 test(0b1__0__0, 4);
aoqi@0 132 test(0b0001_0010_0011, 0x123);
aoqi@0 133 test(0b111_1111_1111_1111_1111_1111_1111_1111, Integer.MAX_VALUE);
aoqi@0 134 test(0b1000_0000_0000_0000_0000_0000_0000_0000, Integer.MIN_VALUE);
aoqi@0 135 test(0b111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111l, Long.MAX_VALUE);
aoqi@0 136 test(0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000l, Long.MIN_VALUE);
aoqi@0 137 test(0b111_1111_1111_1111, Short.MAX_VALUE);
aoqi@0 138 test((short)-0b1000_0000_0000_0000, Short.MIN_VALUE);
aoqi@0 139 test(0b111_1111, Byte.MAX_VALUE);
aoqi@0 140 test((byte)-0b1000_0000, Byte.MIN_VALUE);
aoqi@0 141
aoqi@0 142 // octal
aoqi@0 143 test(01, 1);
aoqi@0 144 test(010, 8);
aoqi@0 145 test(01_0, 8);
aoqi@0 146 test(01__0, 8);
aoqi@0 147 test(01_0_0, 64);
aoqi@0 148 test(01__0__0, 64);
aoqi@0 149 test(0_1, 1);
aoqi@0 150 test(0_10, 8);
aoqi@0 151 test(0_1_0, 8);
aoqi@0 152 test(0_1__0, 8);
aoqi@0 153 test(0_1_0_0, 64);
aoqi@0 154 test(0_1__0__0, 64);
aoqi@0 155 test(0_001_002_003, 01002003);
aoqi@0 156 test(0177_7777_7777, Integer.MAX_VALUE);
aoqi@0 157 test(-0200_0000_0000, Integer.MIN_VALUE);
aoqi@0 158 test(077_77_77_77_77_7_77_77_77_77_77l, Long.MAX_VALUE);
aoqi@0 159 test(-010_00_00_00_00_00_00_00_00_00_00l, Long.MIN_VALUE);
aoqi@0 160 test((short)07_77_77, Short.MAX_VALUE);
aoqi@0 161 test((short)-010_00_00, Short.MIN_VALUE);
aoqi@0 162 test(01_77, Byte.MAX_VALUE);
aoqi@0 163 test((byte)-02_00, Byte.MIN_VALUE);
aoqi@0 164
aoqi@0 165 // hexadecimal
aoqi@0 166 test(0x1, 1);
aoqi@0 167 test(0x10, 16);
aoqi@0 168 test(0x1_0, 16);
aoqi@0 169 test(0x1__0, 16);
aoqi@0 170 test(0x1_0_0, 256);
aoqi@0 171 test(0x1__0__0, 256);
aoqi@0 172 test(0x01_02_03_04, 0x1020304);
aoqi@0 173 test(0x7f_ff_ff_ff, Integer.MAX_VALUE);
aoqi@0 174 test(0x80_00_00_00, Integer.MIN_VALUE);
aoqi@0 175 test(0x1.f_ff_ffep127f, Float.MAX_VALUE);
aoqi@0 176 test(0x0.00_00_02p-126f, Float.MIN_VALUE);
aoqi@0 177 test(0x1.f__ff_ff_ff_ff_ff_ffp1_023, Double.MAX_VALUE);
aoqi@0 178 test(0x0.000_000_000_000_1p-1_022, Double.MIN_VALUE);
aoqi@0 179 test(0x7f_ff_ff_ff_ff_ff_ff_ffl, Long.MAX_VALUE);
aoqi@0 180 test(0x80_00_00_00_00_00_00_00l, Long.MIN_VALUE);
aoqi@0 181 test(0x7f_ff, Short.MAX_VALUE);
aoqi@0 182 test((short)0x80_00, Short.MIN_VALUE);
aoqi@0 183 test(0x7_f, Byte.MAX_VALUE);
aoqi@0 184 test((byte)0x8_0, Byte.MIN_VALUE);
aoqi@0 185
aoqi@0 186 // misc
aoqi@0 187 long creditCardNumber = 1234_5678_9012_3456L;
aoqi@0 188 test(creditCardNumber, 1234567890123456L);
aoqi@0 189 long socialSecurityNumbers = 999_99_9999L;
aoqi@0 190 test(socialSecurityNumbers, 999999999L);
aoqi@0 191 double monetaryAmount = 12_345_132.12d;
aoqi@0 192 test(monetaryAmount, 12345132.12d);
aoqi@0 193 long hexBytes = 0xFF_EC_DE_5E;
aoqi@0 194 test(hexBytes, 0xffecde5e);
aoqi@0 195 long hexWords = 0xFFEC_DE5E;
aoqi@0 196 test(hexWords, 0xffecde5e);
aoqi@0 197 long maxLong = 0x7fff_ffff_ffff_ffffL;
aoqi@0 198 test(maxLong, 0x7fffffffffffffffL);
aoqi@0 199 long maxLongDecimal = 9223372036854775807L;
aoqi@0 200 long alsoMaxLong = 9_223_372_036_854_775_807L;
aoqi@0 201 test(alsoMaxLong, maxLongDecimal);
aoqi@0 202 double whyWouldYouEverDoThis = 0x1.ffff_ffff_ffff_fp10_23;
aoqi@0 203 double whyWouldYouEverDoEvenThis = 0x1.fffffffffffffp1023;
aoqi@0 204 test(whyWouldYouEverDoThis, whyWouldYouEverDoEvenThis);
aoqi@0 205
aoqi@0 206 if (errors > 0)
aoqi@0 207 throw new Exception(errors + " errors found");
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 void test(int value, int expect) {
aoqi@0 211 count++;
aoqi@0 212 if (value != expect)
aoqi@0 213 error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n found: 0x" + Integer.toHexString(value));
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 void test(double value, double expect) {
aoqi@0 217 count++;
aoqi@0 218 if (value != expect)
aoqi@0 219 error("test " + count + "\nexpected: 0x" + expect + "\n found: 0x" + value);
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 void test(long value, long expect) {
aoqi@0 223 count++;
aoqi@0 224 if (value != expect)
aoqi@0 225 error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n found: 0x" + Long.toHexString(value));
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 void error(String message) {
aoqi@0 229 System.out.println(message);
aoqi@0 230 errors++;
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 int count;
aoqi@0 234 int errors;
aoqi@0 235 }

mercurial