1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.lang;
18
19import junit.framework.TestCase;
20
21public class OldAndroidInstanceofTest extends TestCase {
22
23    protected A mA;
24    protected ChildOfAOne mOne;
25    protected ChildOfAOne mTwo;
26    protected ChildOfAOne mThree;
27    protected ChildOfAOne mFour;
28    protected ChildOfAFive mFive;
29
30    @Override
31    protected void setUp() throws Exception {
32        super.setUp();
33
34        mA = new A();
35        mOne = new ChildOfAOne();
36        mTwo = new ChildOfATwo();
37        mThree = new ChildOfAThree();
38        mFour = new ChildOfAFour();
39        mFive = new ChildOfAFive();
40    }
41
42
43    public void testNoInterface() throws Exception {
44        A a = mA;
45        for (int i = 0; i < 100000; i++) {
46            assertFalse("m_a should not be a ChildOfAFive", a instanceof ChildOfAFive);
47        }
48    }
49
50    public void testDerivedOne() throws Exception {
51        InterfaceOne one = mOne;
52        for (int i = 0; i < 100000; i++) {
53            assertFalse("m_one should not be a ChildOfAFive", one instanceof ChildOfAFive);
54        }
55    }
56
57    public void testDerivedTwo() throws Exception {
58        InterfaceTwo two = mTwo;
59        for (int i = 0; i < 100000; i++) {
60            assertFalse("m_two should not be a ChildOfAFive", two instanceof ChildOfAFive);
61        }
62    }
63
64    public void testDerivedThree() throws Exception {
65        InterfaceThree three = mThree;
66        for (int i = 0; i < 100000; i++) {
67            assertFalse("m_three should not be a ChildOfAFive", three instanceof ChildOfAFive);
68        }
69    }
70
71    public void testDerivedFour() throws Exception {
72        InterfaceFour four = mFour;
73        for (int i = 0; i < 100000; i++) {
74            assertFalse("m_four should not be a ChildOfAFive", four instanceof ChildOfAFive);
75        }
76    }
77
78    public void testSuccessClass() throws Exception {
79        ChildOfAOne five = mFive;
80        for (int i = 0; i < 100000; i++) {
81            assertTrue("m_five is suppose to be a ChildOfAFive", five instanceof ChildOfAFive);
82        }
83    }
84
85    public void testSuccessInterface() throws Exception {
86        ChildOfAFive five = mFive;
87        for (int i = 0; i < 100000; i++) {
88            assertTrue("m_five is suppose to be a InterfaceFour", five instanceof InterfaceFour);
89        }
90    }
91
92    public void testFailInterface() throws Exception {
93        InterfaceOne one = mFive;
94        for (int i = 0; i < 100000; i++) {
95            assertFalse("m_five does not implement InterfaceFive", one instanceof InterfaceFive);
96        }
97    }
98
99    private interface InterfaceOne {
100    }
101
102    private interface InterfaceTwo {
103    }
104
105    private interface InterfaceThree {
106    }
107
108    private interface InterfaceFour {
109    }
110
111    private interface InterfaceFive {
112    }
113
114    private static class A {
115    }
116
117    private static class ChildOfAOne extends A implements InterfaceOne, InterfaceTwo, InterfaceThree, InterfaceFour {
118    }
119
120    private static class ChildOfATwo extends ChildOfAOne {
121    }
122
123    private static class ChildOfAThree extends ChildOfATwo {
124    }
125
126    private static class ChildOfAFour extends ChildOfAThree {
127    }
128
129    private static class ChildOfAFive extends ChildOfAFour {
130    }
131}
132