Path: blob/master/test/langtools/tools/javac/6979683/TestCast6979683_GOOD.java
41149 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 697968326* @summary Verify that casts can narrow and unbox at the same time27* @author jrose28*29* @compile TestCast6979683_GOOD.java30* @run main TestCast6979683_GOOD31*/3233public class TestCast6979683_GOOD {34public static void main(String... av) {35bugReportExample();36for (int x = -1; x <= 2; x++) {37zconvTests(x != 0);38iconvTests(x);39bconvTests((byte)x);40cconvTests((char)x);41}42System.out.println("Successfully ran "+tests+" tests.");43}4445static int tests;46static void assertEquals(Object x, Object y) {47if (!x.equals(y)) {48throw new RuntimeException("assertEquals: "+x+" != "+y);49}50++tests;51}5253static void bugReportExample() {54{} // example in bug report:55Object x = (Object)1;56int y = (int)x;57{} // end example58}5960static boolean zconv1(Boolean o) { return o; }61static boolean zconv2(Object o) { return (boolean)o; }62static boolean zconv3(Comparable<Boolean> o) { return (boolean)o; }6364static void zconvTests(boolean x) {65assertEquals(x, zconv1(x));66assertEquals(x, zconv2(x));67assertEquals(x, zconv3(x));68}6970static int iconv1(Integer o) { return o; }71static int iconv2(Object o) { return (int)o; }72static int iconv3(java.io.Serializable o) { return (int)o; }73static int iconv4(Number o) { return (int)o; }74static int iconv5(Comparable<Integer> o) { return (int)o; }7576static void iconvTests(int x) {77assertEquals(x, iconv1(x));78assertEquals(x, iconv2(x));79assertEquals(x, iconv3(x));80assertEquals(x, iconv4(x));81assertEquals(x, iconv5(x));82}8384static float bconv1(Byte o) { return o; } // note type "float"85static float bconv2(Object o) { return (byte)o; }86static float bconv3(java.io.Serializable o) { return (byte)o; }87static float bconv4(Number o) { return (byte)o; }8889static void bconvTests(byte x) {90float xf = x;91assertEquals(xf, bconv1(x));92assertEquals(xf, bconv2(x));93assertEquals(xf, bconv3(x));94assertEquals(xf, bconv4(x));95}9697static float cconv1(Character o) { return o; } // note type "float"98static float cconv2(Object o) { return (char)o; }99static float cconv3(java.io.Serializable o) { return (char)o; }100static float cconv4(Comparable<Character> o) { return (char)o; }101102static void cconvTests(char x) {103float xf = x;104assertEquals(xf, cconv1(x));105assertEquals(xf, cconv2(x));106assertEquals(xf, cconv3(x));107assertEquals(xf, cconv4(x));108}109110}111112113