001/*
002 * Copyright (c) 2007, 2012, 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.jtt.optimize;
024
025import org.junit.*;
026
027import com.oracle.graal.jtt.*;
028
029/*
030 */
031public class TypeCastElem extends JTTTest {
032
033    interface Int1 {
034
035        int do1();
036    }
037
038    interface Int2 {
039
040        int do2();
041    }
042
043    interface Int3 extends Int1 {
044
045        int do3();
046    }
047
048    public static class ClassA implements Int1 {
049
050        private int a;
051
052        public ClassA(int a) {
053            this.a = a;
054        }
055
056        public int do1() {
057            return a;
058        }
059    }
060
061    public static class ClassB extends ClassA implements Int2 {
062
063        int b;
064
065        public ClassB(int a, int b) {
066            super(a);
067            this.b = b;
068        }
069
070        public int do2() {
071            return b;
072        }
073    }
074
075    public static class ClassC implements Int3 {
076
077        private int a;
078        private int b;
079
080        public ClassC(int a, int b) {
081            this.a = a;
082            this.b = b;
083        }
084
085        public int do3() {
086            return b;
087        }
088
089        public int do1() {
090            return a;
091        }
092
093    }
094
095    public static int test1(Object o) {
096        if (o instanceof ClassB) {
097            ClassB b = (ClassB) o;
098            if (o instanceof Int1) {
099                return b.b - b.b + 1;
100            }
101            return 7;
102        }
103        return 3;
104    }
105
106    public static int test2(Object o) {
107        Object b = o;
108        if (o instanceof ClassB) {
109            ClassA a = (ClassA) o;
110            if (b instanceof Int1) {
111                return ((Int1) a).do1();
112            }
113            return 7;
114        }
115        return 3;
116    }
117
118    public static int test3(Object o) {
119        Object b = o;
120        boolean t = o instanceof Int3;
121        if (t) {
122            Int1 a = (Int1) b;
123            return a.do1();
124        }
125        return 3;
126    }
127
128    public static int test(int a, int b, int c) {
129        ClassA ca = new ClassA(a);
130        ClassB cb = new ClassB(a, b);
131        ClassC cc = new ClassC(c, c);
132        int sum1 = test1(ca) + test1(cb) * 10 + test1(cc) * 100;
133        int sum2 = test2(ca) + test2(cb) * 10 + test2(cc) * 100;
134        int sum3 = test3(ca) + test3(cb) * 10 + test3(cc) * 100;
135        int result = sum1 * 5 + sum2 * 7 + sum3 * 9;
136        return result;
137    }
138
139    @Test
140    public void run0() throws Throwable {
141        runTest("test", 10, 13, 25);
142    }
143
144}