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