1 /*
2  * Copyright (c) 2013, 2016, 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 package org.graalvm.compiler.nodes.test;
26 
27 import jdk.vm.ci.meta.JavaKind;
28 
29 import org.junit.Assert;
30 import org.junit.Test;
31 
32 import org.graalvm.compiler.core.common.type.ObjectStamp;
33 import org.graalvm.compiler.core.common.type.Stamp;
34 import org.graalvm.compiler.core.common.type.StampFactory;
35 import org.graalvm.compiler.core.common.type.TypeReference;
36 
37 public class ObjectStampMeetTest extends AbstractObjectStampTest {
38 
39     @Test
testMeet0()40     public void testMeet0() {
41         check(A.class, B.class, A.class);
42     }
43 
44     @Test
testMeet1()45     public void testMeet1() {
46         Stamp a = StampFactory.object(getType(A.class));
47         Stamp aNonNull = StampFactory.objectNonNull(getType(A.class));
48         Stamp b = StampFactory.object(getType(B.class));
49         Stamp bNonNull = StampFactory.objectNonNull(getType(B.class));
50         Assert.assertEquals(a, meet(aNonNull, b));
51         Assert.assertEquals(aNonNull, meet(aNonNull, bNonNull));
52     }
53 
54     @Test
testMeet2()55     public void testMeet2() {
56         Stamp a = StampFactory.object(getType(A.class));
57         Stamp aExact = StampFactory.objectNonNull(getType(A.class).asExactReference());
58         Stamp b = StampFactory.object(getType(B.class));
59         Assert.assertEquals(a, meet(aExact, b));
60     }
61 
62     @Test
testMeet3()63     public void testMeet3() {
64         check(C.class, D.class, A.class);
65     }
66 
67     @Test
testMeet4()68     public void testMeet4() {
69         Stamp dExactNonNull = StampFactory.objectNonNull(getType(D.class).asExactReference());
70         Stamp cExactNonNull = StampFactory.objectNonNull(getType(C.class).asExactReference());
71         Stamp aNonNull = StampFactory.objectNonNull(getType(A.class));
72         Assert.assertEquals(aNonNull, meet(cExactNonNull, dExactNonNull));
73     }
74 
75     @Test
testMeet5()76     public void testMeet5() {
77         Stamp dExact = StampFactory.object(getType(D.class).asExactReference());
78         Stamp c = StampFactory.object(getType(C.class));
79         Stamp a = StampFactory.object(getType(A.class));
80         Assert.assertEquals(a, meet(dExact, c));
81     }
82 
83     @Test
testMeet6()84     public void testMeet6() {
85         Stamp dExactNonNull = StampFactory.objectNonNull(getType(D.class).asExactReference());
86         Stamp alwaysNull = StampFactory.alwaysNull();
87         Stamp dExact = StampFactory.object(getType(D.class).asExactReference());
88         Assert.assertEquals(dExact, meet(dExactNonNull, alwaysNull));
89     }
90 
91     @Test
testMeet7()92     public void testMeet7() {
93         Stamp aExact = StampFactory.object(getType(A.class).asExactReference());
94         Stamp e = StampFactory.object(getType(E.class));
95         Stamp a = StampFactory.object(getType(A.class));
96         Assert.assertEquals(a, meet(aExact, e));
97     }
98 
99     @Test
testMeet8()100     public void testMeet8() {
101         check(A.class, A.class, A.class);
102     }
103 
104     @Test
testMeet9()105     public void testMeet9() {
106         Stamp base1 = StampFactory.object(getType(Base1.class));
107         Stamp ord1 = StampFactory.object(getType(ImplOrder1.class));
108         Stamp ord2 = StampFactory.object(getType(ImplOrder2.class));
109         Assert.assertEquals(base1, meet(ord1, ord2));
110     }
111 
112     @Test
testMeet10()113     public void testMeet10() {
114         Stamp base1 = StampFactory.object(getType(Object.class));
115         Stamp ord1 = StampFactory.object(getType(Deep1.class));
116         Stamp ord2 = StampFactory.object(getType(Deep2.class));
117         Assert.assertEquals(base1, meet(ord1, ord2));
118     }
119 
120     @Test
testMeetInterface0()121     public void testMeetInterface0() {
122         check(C.class, I.class, I.class);
123     }
124 
125     @Test
testMeetInterface1()126     public void testMeetInterface1() {
127         check(I.class, SubI1.class, I.class);
128     }
129 
130     @Test
testMeetInterface2()131     public void testMeetInterface2() {
132         check(SubI1.class, SubI2.class, I.class);
133     }
134 
135     @Test
testMeetInterface3()136     public void testMeetInterface3() {
137         check(SubI4.class, SubI5.class, SubI3.class);
138     }
139 
140     @Test
testMeetInterface4()141     public void testMeetInterface4() {
142         check(SubI4.class, SubI6.class, Object.class);
143     }
144 
check(Class<?> a, Class<?> b, Class<?> result)145     private void check(Class<?> a, Class<?> b, Class<?> result) {
146         Stamp aStamp = StampFactory.object(getType(a));
147         Stamp bStamp = StampFactory.object(getType(b));
148         ObjectStamp resultStamp = StampFactory.object(getType(result));
149         Assert.assertEquals(resultStamp, meet(aStamp, bStamp));
150     }
151 
152     @Test
testMeetIllegal1()153     public void testMeetIllegal1() {
154         for (Class<?> clazz : new Class<?>[]{A.class, B.class, C.class, D.class, E.class, I.class, Object.class}) {
155             TypeReference type = getType(clazz);
156             for (Stamp test : new Stamp[]{StampFactory.object(type), StampFactory.objectNonNull(type), StampFactory.object(type.asExactReference()),
157                             StampFactory.objectNonNull(type.asExactReference())}) {
158                 if (type.getType().isConcrete() || !((ObjectStamp) test).isExactType()) {
159                     Assert.assertEquals("meeting empty and " + test, test, meet(StampFactory.empty(JavaKind.Object), test));
160                 }
161             }
162         }
163     }
164 }
165