test/tools/javac/literals/UnderscoreLiterals.java

Tue, 15 Sep 2009 18:36:21 -0700

author
jjg
date
Tue, 15 Sep 2009 18:36:21 -0700
changeset 409
69eaccd3ea85
child 554
9d9f26857129
permissions
-rw-r--r--

6860965: Project Coin: binary literals
6860973: Project Coin: Underscores in literals
Summary: [Portions contributed by Bruce Chapman]
Reviewed-by: darcy

jjg@409 1 /*
jjg@409 2 * Copyright 2009 Sun Microsystems, Inc. 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 *
jjg@409 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@409 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@409 21 * have any 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@409 45
jjg@409 46 // long
jjg@409 47 test(1l, 1l);
jjg@409 48 test(10l, 10l);
jjg@409 49 test(1_0l, 10l);
jjg@409 50 test(1__0l, 10l);
jjg@409 51 test(1_0_0l, 100l);
jjg@409 52 test(1__0__0l, 100l);
jjg@409 53 test(123_456_789l, 123456789l);
jjg@409 54
jjg@409 55 // float
jjg@409 56 test(.1f, .1f);
jjg@409 57 test(.10f, .10f);
jjg@409 58 test(.1_0f, .10f);
jjg@409 59 test(.1__0f, .10f);
jjg@409 60 test(.1_0_0f, .100f);
jjg@409 61 test(.1__0__0f, .100f);
jjg@409 62 test(1e1, 1e1);
jjg@409 63 test(1e10, 1e10);
jjg@409 64 test(1e1_0, 1e10);
jjg@409 65 test(1e1__0, 1e10);
jjg@409 66 test(1e1_0_0, 1e100);
jjg@409 67 test(1e1__0__0, 1e100);
jjg@409 68 test(.123_456_789f, .123456789f);
jjg@409 69 test(0.1f, 0.1f);
jjg@409 70 test(0.10f, 0.10f);
jjg@409 71 test(0.1_0f, 0.10f);
jjg@409 72 test(0.1__0f, 0.10f);
jjg@409 73 test(0.1_0_0f, 0.100f);
jjg@409 74 test(0.1__0__0f, 0.100f);
jjg@409 75 test(0.123_456_789f, 0.123456789f);
jjg@409 76 test(1_1.1f, 1_1.1f);
jjg@409 77 test(1_1.10f, 1_1.10f);
jjg@409 78 test(1_1.1_0f, 1_1.10f);
jjg@409 79 test(1_1.1__0f, 1_1.10f);
jjg@409 80 test(1_1.1_0_0f, 1_1.100f);
jjg@409 81 test(1_1.1__0__0f, 1_1.100f);
jjg@409 82 test(1_1.123_456_789f, 1_1.123456789f);
jjg@409 83
jjg@409 84 // double
jjg@409 85 test(.1d, .1d);
jjg@409 86 test(.10d, .10d);
jjg@409 87 test(.1_0d, .10d);
jjg@409 88 test(.1__0d, .10d);
jjg@409 89 test(.1_0_0d, .100d);
jjg@409 90 test(.1__0__0d, .100d);
jjg@409 91 test(1e1, 1e1);
jjg@409 92 test(1e10, 1e10);
jjg@409 93 test(1e1_0, 1e10);
jjg@409 94 test(1e1__0, 1e10);
jjg@409 95 test(1e1_0_0, 1e100);
jjg@409 96 test(1e1__0__0, 1e100);
jjg@409 97 test(.123_456_789d, .123456789d);
jjg@409 98 test(0.1d, 0.1d);
jjg@409 99 test(0.10d, 0.10d);
jjg@409 100 test(0.1_0d, 0.10d);
jjg@409 101 test(0.1__0d, 0.10d);
jjg@409 102 test(0.1_0_0d, 0.100d);
jjg@409 103 test(0.1__0__0d, 0.100d);
jjg@409 104 test(0.123_456_789d, 0.123456789d);
jjg@409 105 test(1_1.1d, 1_1.1d);
jjg@409 106 test(1_1.10d, 1_1.10d);
jjg@409 107 test(1_1.1_0d, 1_1.10d);
jjg@409 108 test(1_1.1__0d, 1_1.10d);
jjg@409 109 test(1_1.1_0_0d, 1_1.100d);
jjg@409 110 test(1_1.1__0__0d, 1_1.100d);
jjg@409 111 test(1_1.123_456_789d, 1_1.123456789d);
jjg@409 112
jjg@409 113 // binary
jjg@409 114 test(0b1, 1);
jjg@409 115 test(0b10, 2);
jjg@409 116 test(0b1_0, 2);
jjg@409 117 test(0b1__0, 2);
jjg@409 118 test(0b1_0_0, 4);
jjg@409 119 test(0b1__0__0, 4);
jjg@409 120 test(0b0001_0010_0011, 0x123);
jjg@409 121
jjg@409 122 // octal
jjg@409 123 test(01, 1);
jjg@409 124 test(010, 8);
jjg@409 125 test(01_0, 8);
jjg@409 126 test(01__0, 8);
jjg@409 127 test(01_0_0, 64);
jjg@409 128 test(01__0__0, 64);
jjg@409 129 test(0_1, 1);
jjg@409 130 test(0_10, 8);
jjg@409 131 test(0_1_0, 8);
jjg@409 132 test(0_1__0, 8);
jjg@409 133 test(0_1_0_0, 64);
jjg@409 134 test(0_1__0__0, 64);
jjg@409 135 test(0_001_002_003, 01002003);
jjg@409 136
jjg@409 137 // hexadecimal
jjg@409 138 test(0x1, 1);
jjg@409 139 test(0x10, 16);
jjg@409 140 test(0x1_0, 16);
jjg@409 141 test(0x1__0, 16);
jjg@409 142 test(0x1_0_0, 256);
jjg@409 143 test(0x1__0__0, 256);
jjg@409 144 test(0x01_02_03_04, 0x1020304);
jjg@409 145
jjg@409 146 // misc
jjg@409 147 long creditCardNumber = 1234_5678_9012_3456L;
jjg@409 148 test(creditCardNumber, 1234567890123456L);
jjg@409 149 long socialSecurityNumbers = 999_99_9999L;
jjg@409 150 test(socialSecurityNumbers, 999999999L);
jjg@409 151 double monetaryAmount = 12_345_132.12d;
jjg@409 152 test(monetaryAmount, 12345132.12d);
jjg@409 153 long hexBytes = 0xFF_EC_DE_5E;
jjg@409 154 test(hexBytes, 0xffecde5e);
jjg@409 155 long hexWords = 0xFFEC_DE5E;
jjg@409 156 test(hexWords, 0xffecde5e);
jjg@409 157 long maxLong = 0x7fff_ffff_ffff_ffffL;
jjg@409 158 test(maxLong, 0x7fffffffffffffffL);
jjg@409 159 long maxLongDecimal = 9223372036854775807L;
jjg@409 160 long alsoMaxLong = 9_223_372_036_854_775_807L;
jjg@409 161 test(alsoMaxLong, maxLongDecimal);
jjg@409 162 double whyWouldYouEverDoThis = 0x1.ffff_ffff_ffff_fp10_23;
jjg@409 163 double whyWouldYouEverDoEvenThis = 0x1.fffffffffffffp1023;
jjg@409 164 test(whyWouldYouEverDoThis, whyWouldYouEverDoEvenThis);
jjg@409 165
jjg@409 166 if (errors > 0)
jjg@409 167 throw new Exception(errors + " errors found");
jjg@409 168 }
jjg@409 169
jjg@409 170 void test(int value, int expect) {
jjg@409 171 count++;
jjg@409 172 if (value != expect)
jjg@409 173 error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n found: 0x" + Integer.toHexString(value));
jjg@409 174 }
jjg@409 175
jjg@409 176 void test(double value, double expect) {
jjg@409 177 count++;
jjg@409 178 if (value != expect)
jjg@409 179 error("test " + count + "\nexpected: 0x" + expect + "\n found: 0x" + value);
jjg@409 180 }
jjg@409 181
jjg@409 182 void test(long value, long expect) {
jjg@409 183 count++;
jjg@409 184 if (value != expect)
jjg@409 185 error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n found: 0x" + Long.toHexString(value));
jjg@409 186 }
jjg@409 187
jjg@409 188 void error(String message) {
jjg@409 189 System.out.println(message);
jjg@409 190 errors++;
jjg@409 191 }
jjg@409 192
jjg@409 193 int count;
jjg@409 194 int errors;
jjg@409 195 }

mercurial