test/tools/javac/6979683/TestCast6979683_GOOD.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/6979683/TestCast6979683_GOOD.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6979683
    1.30 + * @summary Verify that casts can narrow and unbox at the same time
    1.31 + * @author jrose
    1.32 + *
    1.33 + * @compile TestCast6979683_GOOD.java
    1.34 + * @run main TestCast6979683_GOOD
    1.35 + */
    1.36 +
    1.37 +public class TestCast6979683_GOOD {
    1.38 +    public static void main(String... av) {
    1.39 +        bugReportExample();
    1.40 +        for (int x = -1; x <= 2; x++) {
    1.41 +            zconvTests(x != 0);
    1.42 +            iconvTests(x);
    1.43 +            bconvTests((byte)x);
    1.44 +            cconvTests((char)x);
    1.45 +        }
    1.46 +        System.out.println("Successfully ran "+tests+" tests.");
    1.47 +    }
    1.48 +
    1.49 +    static int tests;
    1.50 +    static void assertEquals(Object x, Object y) {
    1.51 +        if (!x.equals(y)) {
    1.52 +            throw new RuntimeException("assertEquals: "+x+" != "+y);
    1.53 +        }
    1.54 +        ++tests;
    1.55 +    }
    1.56 +
    1.57 +    static void bugReportExample() {
    1.58 +  {} // example in bug report:
    1.59 +  Object x = (Object)1;
    1.60 +  int y = (int)x;
    1.61 +  {} // end example
    1.62 +    }
    1.63 +
    1.64 +    static boolean zconv1(Boolean o) { return o; }
    1.65 +    static boolean zconv2(Object o) { return (boolean)o; }
    1.66 +    static boolean zconv3(Comparable<Boolean> o) { return (boolean)o; }
    1.67 +
    1.68 +    static void zconvTests(boolean x) {
    1.69 +        assertEquals(x, zconv1(x));
    1.70 +        assertEquals(x, zconv2(x));
    1.71 +        assertEquals(x, zconv3(x));
    1.72 +    }
    1.73 +
    1.74 +    static int iconv1(Integer o) { return o; }
    1.75 +    static int iconv2(Object o) { return (int)o; }
    1.76 +    static int iconv3(java.io.Serializable o) { return (int)o; }
    1.77 +    static int iconv4(Number o) { return (int)o; }
    1.78 +    static int iconv5(Comparable<Integer> o) { return (int)o; }
    1.79 +
    1.80 +    static void iconvTests(int x) {
    1.81 +        assertEquals(x, iconv1(x));
    1.82 +        assertEquals(x, iconv2(x));
    1.83 +        assertEquals(x, iconv3(x));
    1.84 +        assertEquals(x, iconv4(x));
    1.85 +        assertEquals(x, iconv5(x));
    1.86 +    }
    1.87 +
    1.88 +    static float bconv1(Byte o) { return o; }  // note type "float"
    1.89 +    static float bconv2(Object o) { return (byte)o; }
    1.90 +    static float bconv3(java.io.Serializable o) { return (byte)o; }
    1.91 +    static float bconv4(Number o) { return (byte)o; }
    1.92 +
    1.93 +    static void bconvTests(byte x) {
    1.94 +        float xf = x;
    1.95 +        assertEquals(xf, bconv1(x));
    1.96 +        assertEquals(xf, bconv2(x));
    1.97 +        assertEquals(xf, bconv3(x));
    1.98 +        assertEquals(xf, bconv4(x));
    1.99 +    }
   1.100 +
   1.101 +    static float cconv1(Character o) { return o; }  // note type "float"
   1.102 +    static float cconv2(Object o) { return (char)o; }
   1.103 +    static float cconv3(java.io.Serializable o) { return (char)o; }
   1.104 +    static float cconv4(Comparable<Character> o) { return (char)o; }
   1.105 +
   1.106 +    static void cconvTests(char x) {
   1.107 +        float xf = x;
   1.108 +        assertEquals(xf, cconv1(x));
   1.109 +        assertEquals(xf, cconv2(x));
   1.110 +        assertEquals(xf, cconv3(x));
   1.111 +        assertEquals(xf, cconv4(x));
   1.112 +    }
   1.113 +
   1.114 +}

mercurial