001/*
002 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.test;
024
025import jdk.internal.jvmci.meta.*;
026
027import org.junit.*;
028
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.nodes.java.*;
031
032/**
033 * Tests the implementation of checkcast, allowing profiling information to be manually specified.
034 */
035public class CheckCastTest extends TypeCheckTest {
036
037    @Override
038    protected void replaceProfile(StructuredGraph graph, JavaTypeProfile profile) {
039        CheckCastNode ccn = graph.getNodes().filter(CheckCastNode.class).first();
040        if (ccn != null) {
041            CheckCastNode ccnNew = graph.add(new CheckCastNode(ccn.type(), ccn.object(), profile, false));
042            graph.replaceFixedWithFixed(ccn, ccnNew);
043        }
044    }
045
046    @Test
047    public void test1() {
048        test("asNumber", profile(), 111);
049        test("asNumber", profile(Integer.class), 111);
050        test("asNumber", profile(Long.class, Short.class), 111);
051        test("asNumberExt", profile(), 111);
052        test("asNumberExt", profile(Integer.class), 111);
053        test("asNumberExt", profile(Long.class, Short.class), 111);
054    }
055
056    @Test
057    public void test2() {
058        test("asString", profile(), "111");
059        test("asString", profile(String.class), "111");
060        test("asString", profile(String.class), "111");
061
062        final String nullString = null;
063        test("asString", profile(), nullString);
064        test("asString", profile(String.class), nullString);
065        test("asString", profile(String.class), nullString);
066
067        test("asStringExt", profile(), "111");
068        test("asStringExt", profile(String.class), "111");
069        test("asStringExt", profile(String.class), "111");
070    }
071
072    @Test
073    public void test3() {
074        test("asNumber", profile(), "111");
075    }
076
077    @Test
078    public void test4() {
079        test("asString", profile(String.class), 111);
080    }
081
082    @Test
083    public void test5() {
084        test("asNumberExt", profile(), "111");
085    }
086
087    @Test
088    public void test6() {
089        test("asStringExt", profile(String.class), 111);
090    }
091
092    @Test
093    public void test7() {
094        Throwable throwable = new Exception();
095        test("asThrowable", profile(), throwable);
096        test("asThrowable", profile(Throwable.class), throwable);
097        test("asThrowable", profile(Exception.class, Error.class), throwable);
098    }
099
100    @Test
101    public void test8() {
102        test("arrayStore", new Object[100], "111");
103    }
104
105    @Test
106    public void test801() {
107        test("arrayFill", new Object[100], "111");
108    }
109
110    public static Number asNumber(Object o) {
111        return (Number) o;
112    }
113
114    public static String asString(Object o) {
115        return (String) o;
116    }
117
118    public static Throwable asThrowable(Object o) {
119        return (Throwable) o;
120    }
121
122    public static ValueNode asValueNode(Object o) {
123        return (ValueNode) o;
124    }
125
126    public static Number asNumberExt(Object o) {
127        Number n = (Number) o;
128        return n.intValue() + 10;
129    }
130
131    public static String asStringExt(Object o) {
132        String s = (String) o;
133        return "#" + s;
134    }
135
136    public static Object[] arrayStore(Object[] arr, Object value) {
137        arr[15] = value;
138        return arr;
139    }
140
141    public static Object[] arrayFill(Object[] arr, Object value) {
142        for (int i = 0; i < arr.length; i++) {
143            arr[i] = value;
144        }
145        return arr;
146    }
147
148    static class Depth1 implements Cloneable {
149    }
150
151    static class Depth2 extends Depth1 {
152    }
153
154    static class Depth3 extends Depth2 {
155    }
156
157    static class Depth4 extends Depth3 {
158    }
159
160    static class Depth5 extends Depth4 {
161    }
162
163    static class Depth6 extends Depth5 {
164    }
165
166    static class Depth7 extends Depth6 {
167    }
168
169    static class Depth8 extends Depth7 {
170    }
171
172    static class Depth9 extends Depth8 {
173    }
174
175    static class Depth10 extends Depth9 {
176    }
177
178    static class Depth11 extends Depth10 {
179    }
180
181    static class Depth12 extends Depth11 {
182    }
183
184    static class Depth13 extends Depth12 {
185    }
186
187    static class Depth14 extends Depth12 {
188    }
189
190    public static Depth12 asDepth12(Object o) {
191        return (Depth12) o;
192    }
193
194    public static Depth12[][] asDepth12Arr(Object o) {
195        return (Depth12[][]) o;
196    }
197
198    public static Cloneable asCloneable(Object o) {
199        return (Cloneable) o;
200    }
201
202    @Test
203    public void test9() {
204        Object o = new Depth13();
205        test("asDepth12", profile(), o);
206        test("asDepth12", profile(Depth13.class), o);
207        test("asDepth12", profile(Depth13.class, Depth14.class), o);
208    }
209
210    @Test
211    public void test10() {
212        Object o = new Depth13[3][];
213        test("asDepth12Arr", o);
214    }
215}