test/tools/javac/literals/UnderscoreLiterals.java

changeset 409
69eaccd3ea85
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/literals/UnderscoreLiterals.java	Tue Sep 15 18:36:21 2009 -0700
     1.3 @@ -0,0 +1,195 @@
     1.4 +/*
     1.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6860973
    1.30 + * @summary Project Coin: Underscores in literals
    1.31 + */
    1.32 +
    1.33 +
    1.34 +public class UnderscoreLiterals {
    1.35 +    public static void main(String... args) throws Exception {
    1.36 +        new UnderscoreLiterals().run();
    1.37 +    }
    1.38 +
    1.39 +    public void run() throws Exception {
    1.40 +        // decimal
    1.41 +        test(1, 1);
    1.42 +        test(10, 10);
    1.43 +        test(1_0, 10);
    1.44 +        test(1__0, 10);
    1.45 +        test(1_0_0, 100);
    1.46 +        test(1__0__0, 100);
    1.47 +        test(123_456_789, 123456789);
    1.48 +
    1.49 +        // long
    1.50 +        test(1l, 1l);
    1.51 +        test(10l, 10l);
    1.52 +        test(1_0l, 10l);
    1.53 +        test(1__0l, 10l);
    1.54 +        test(1_0_0l, 100l);
    1.55 +        test(1__0__0l, 100l);
    1.56 +        test(123_456_789l, 123456789l);
    1.57 +
    1.58 +        // float
    1.59 +        test(.1f, .1f);
    1.60 +        test(.10f, .10f);
    1.61 +        test(.1_0f, .10f);
    1.62 +        test(.1__0f, .10f);
    1.63 +        test(.1_0_0f, .100f);
    1.64 +        test(.1__0__0f, .100f);
    1.65 +        test(1e1, 1e1);
    1.66 +        test(1e10, 1e10);
    1.67 +        test(1e1_0, 1e10);
    1.68 +        test(1e1__0, 1e10);
    1.69 +        test(1e1_0_0, 1e100);
    1.70 +        test(1e1__0__0, 1e100);
    1.71 +        test(.123_456_789f, .123456789f);
    1.72 +        test(0.1f, 0.1f);
    1.73 +        test(0.10f, 0.10f);
    1.74 +        test(0.1_0f, 0.10f);
    1.75 +        test(0.1__0f, 0.10f);
    1.76 +        test(0.1_0_0f, 0.100f);
    1.77 +        test(0.1__0__0f, 0.100f);
    1.78 +        test(0.123_456_789f, 0.123456789f);
    1.79 +        test(1_1.1f, 1_1.1f);
    1.80 +        test(1_1.10f, 1_1.10f);
    1.81 +        test(1_1.1_0f, 1_1.10f);
    1.82 +        test(1_1.1__0f, 1_1.10f);
    1.83 +        test(1_1.1_0_0f, 1_1.100f);
    1.84 +        test(1_1.1__0__0f, 1_1.100f);
    1.85 +        test(1_1.123_456_789f, 1_1.123456789f);
    1.86 +
    1.87 +        // double
    1.88 +        test(.1d, .1d);
    1.89 +        test(.10d, .10d);
    1.90 +        test(.1_0d, .10d);
    1.91 +        test(.1__0d, .10d);
    1.92 +        test(.1_0_0d, .100d);
    1.93 +        test(.1__0__0d, .100d);
    1.94 +        test(1e1, 1e1);
    1.95 +        test(1e10, 1e10);
    1.96 +        test(1e1_0, 1e10);
    1.97 +        test(1e1__0, 1e10);
    1.98 +        test(1e1_0_0, 1e100);
    1.99 +        test(1e1__0__0, 1e100);
   1.100 +        test(.123_456_789d, .123456789d);
   1.101 +        test(0.1d, 0.1d);
   1.102 +        test(0.10d, 0.10d);
   1.103 +        test(0.1_0d, 0.10d);
   1.104 +        test(0.1__0d, 0.10d);
   1.105 +        test(0.1_0_0d, 0.100d);
   1.106 +        test(0.1__0__0d, 0.100d);
   1.107 +        test(0.123_456_789d, 0.123456789d);
   1.108 +        test(1_1.1d, 1_1.1d);
   1.109 +        test(1_1.10d, 1_1.10d);
   1.110 +        test(1_1.1_0d, 1_1.10d);
   1.111 +        test(1_1.1__0d, 1_1.10d);
   1.112 +        test(1_1.1_0_0d, 1_1.100d);
   1.113 +        test(1_1.1__0__0d, 1_1.100d);
   1.114 +        test(1_1.123_456_789d, 1_1.123456789d);
   1.115 +
   1.116 +        // binary
   1.117 +        test(0b1, 1);
   1.118 +        test(0b10, 2);
   1.119 +        test(0b1_0, 2);
   1.120 +        test(0b1__0, 2);
   1.121 +        test(0b1_0_0, 4);
   1.122 +        test(0b1__0__0, 4);
   1.123 +        test(0b0001_0010_0011, 0x123);
   1.124 +
   1.125 +        // octal
   1.126 +        test(01, 1);
   1.127 +        test(010, 8);
   1.128 +        test(01_0, 8);
   1.129 +        test(01__0, 8);
   1.130 +        test(01_0_0, 64);
   1.131 +        test(01__0__0, 64);
   1.132 +        test(0_1, 1);
   1.133 +        test(0_10, 8);
   1.134 +        test(0_1_0, 8);
   1.135 +        test(0_1__0, 8);
   1.136 +        test(0_1_0_0, 64);
   1.137 +        test(0_1__0__0, 64);
   1.138 +        test(0_001_002_003, 01002003);
   1.139 +
   1.140 +        // hexadecimal
   1.141 +        test(0x1, 1);
   1.142 +        test(0x10, 16);
   1.143 +        test(0x1_0, 16);
   1.144 +        test(0x1__0, 16);
   1.145 +        test(0x1_0_0, 256);
   1.146 +        test(0x1__0__0, 256);
   1.147 +        test(0x01_02_03_04, 0x1020304);
   1.148 +
   1.149 +        // misc
   1.150 +        long creditCardNumber = 1234_5678_9012_3456L;
   1.151 +        test(creditCardNumber, 1234567890123456L);
   1.152 +        long socialSecurityNumbers = 999_99_9999L;
   1.153 +        test(socialSecurityNumbers, 999999999L);
   1.154 +        double monetaryAmount = 12_345_132.12d;
   1.155 +        test(monetaryAmount, 12345132.12d);
   1.156 +        long hexBytes = 0xFF_EC_DE_5E;
   1.157 +        test(hexBytes, 0xffecde5e);
   1.158 +        long hexWords = 0xFFEC_DE5E;
   1.159 +        test(hexWords, 0xffecde5e);
   1.160 +        long maxLong = 0x7fff_ffff_ffff_ffffL;
   1.161 +        test(maxLong, 0x7fffffffffffffffL);
   1.162 +        long maxLongDecimal = 9223372036854775807L;
   1.163 +        long alsoMaxLong = 9_223_372_036_854_775_807L;
   1.164 +        test(alsoMaxLong, maxLongDecimal);
   1.165 +        double whyWouldYouEverDoThis = 0x1.ffff_ffff_ffff_fp10_23;
   1.166 +        double whyWouldYouEverDoEvenThis = 0x1.fffffffffffffp1023;
   1.167 +        test(whyWouldYouEverDoThis, whyWouldYouEverDoEvenThis);
   1.168 +
   1.169 +        if (errors > 0)
   1.170 +             throw new Exception(errors + " errors found");
   1.171 +    }
   1.172 +
   1.173 +    void test(int value, int expect) {
   1.174 +        count++;
   1.175 +        if (value != expect)
   1.176 +            error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n   found: 0x" + Integer.toHexString(value));
   1.177 +    }
   1.178 +
   1.179 +    void test(double value, double expect) {
   1.180 +        count++;
   1.181 +        if (value != expect)
   1.182 +            error("test " + count + "\nexpected: 0x" + expect + "\n   found: 0x" + value);
   1.183 +    }
   1.184 +
   1.185 +    void test(long value, long expect) {
   1.186 +        count++;
   1.187 +        if (value != expect)
   1.188 +            error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n   found: 0x" + Long.toHexString(value));
   1.189 +    }
   1.190 +
   1.191 +    void error(String message) {
   1.192 +        System.out.println(message);
   1.193 +        errors++;
   1.194 +    }
   1.195 +
   1.196 +    int count;
   1.197 +    int errors;
   1.198 +}

mercurial