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