langtools/test/tools/javac/6979683/TestCast6979683_GOOD.java
author hannesw
Tue, 22 Mar 2016 14:23:16 +0100
changeset 36690 06b714373aa4
parent 6583 6410e8c9c848
permissions -rw-r--r--
8151810: for-in iteration does not provide per-iteration scope Reviewed-by: attila, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6583
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     1
/*
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     4
 *
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     8
 *
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    13
 * accompanied this code).
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    14
 *
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    18
 *
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    21
 * questions.
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    22
 */
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    23
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    24
/*
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    25
 * @test
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    26
 * @bug 6979683
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    27
 * @summary Verify that casts can narrow and unbox at the same time
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    28
 * @author jrose
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    29
 *
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    30
 * @compile TestCast6979683_GOOD.java
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    31
 * @run main TestCast6979683_GOOD
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    32
 */
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    33
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    34
public class TestCast6979683_GOOD {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    35
    public static void main(String... av) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    36
        bugReportExample();
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    37
        for (int x = -1; x <= 2; x++) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    38
            zconvTests(x != 0);
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    39
            iconvTests(x);
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    40
            bconvTests((byte)x);
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    41
            cconvTests((char)x);
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    42
        }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    43
        System.out.println("Successfully ran "+tests+" tests.");
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    44
    }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    45
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    46
    static int tests;
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    47
    static void assertEquals(Object x, Object y) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    48
        if (!x.equals(y)) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    49
            throw new RuntimeException("assertEquals: "+x+" != "+y);
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    50
        }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    51
        ++tests;
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    52
    }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    53
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    54
    static void bugReportExample() {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    55
  {} // example in bug report:
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    56
  Object x = (Object)1;
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    57
  int y = (int)x;
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    58
  {} // end example
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    59
    }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    60
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    61
    static boolean zconv1(Boolean o) { return o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    62
    static boolean zconv2(Object o) { return (boolean)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    63
    static boolean zconv3(Comparable<Boolean> o) { return (boolean)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    64
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    65
    static void zconvTests(boolean x) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    66
        assertEquals(x, zconv1(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    67
        assertEquals(x, zconv2(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    68
        assertEquals(x, zconv3(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    69
    }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    70
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    71
    static int iconv1(Integer o) { return o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    72
    static int iconv2(Object o) { return (int)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    73
    static int iconv3(java.io.Serializable o) { return (int)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    74
    static int iconv4(Number o) { return (int)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    75
    static int iconv5(Comparable<Integer> o) { return (int)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    76
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    77
    static void iconvTests(int x) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    78
        assertEquals(x, iconv1(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    79
        assertEquals(x, iconv2(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    80
        assertEquals(x, iconv3(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    81
        assertEquals(x, iconv4(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    82
        assertEquals(x, iconv5(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    83
    }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    84
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    85
    static float bconv1(Byte o) { return o; }  // note type "float"
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    86
    static float bconv2(Object o) { return (byte)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    87
    static float bconv3(java.io.Serializable o) { return (byte)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    88
    static float bconv4(Number o) { return (byte)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    89
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    90
    static void bconvTests(byte x) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    91
        float xf = x;
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    92
        assertEquals(xf, bconv1(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    93
        assertEquals(xf, bconv2(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    94
        assertEquals(xf, bconv3(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    95
        assertEquals(xf, bconv4(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    96
    }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    97
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    98
    static float cconv1(Character o) { return o; }  // note type "float"
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
    99
    static float cconv2(Object o) { return (char)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   100
    static float cconv3(java.io.Serializable o) { return (char)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   101
    static float cconv4(Comparable<Character> o) { return (char)o; }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   102
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   103
    static void cconvTests(char x) {
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   104
        float xf = x;
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   105
        assertEquals(xf, cconv1(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   106
        assertEquals(xf, cconv2(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   107
        assertEquals(xf, cconv3(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   108
        assertEquals(xf, cconv4(x));
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   109
    }
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   110
6410e8c9c848 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case
jrose
parents:
diff changeset
   111
}