001/*
002 * Copyright (c) 2013, 2015, 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.nodes.test;
024
025import jdk.internal.jvmci.meta.*;
026
027import org.junit.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.nodes.type.*;
031
032public class ObjectStampJoinTest extends AbstractObjectStampTest {
033
034    // class A
035    // class B extends A
036    // class C extends B implements I
037    // class D extends A
038    // abstract class E extends A
039    // interface I
040
041    @Test
042    public void testJoin0() {
043        Stamp a = StampFactory.declared(getType(A.class));
044        Stamp b = StampFactory.declared(getType(B.class));
045        Assert.assertEquals(b, join(a, b));
046    }
047
048    @Test
049    public void testJoin1() {
050        Stamp aNonNull = StampFactory.declaredNonNull(getType(A.class));
051        Stamp b = StampFactory.declared(getType(B.class));
052        Stamp bNonNull = StampFactory.declaredNonNull(getType(B.class));
053        Assert.assertEquals(bNonNull, join(aNonNull, b));
054    }
055
056    @Test
057    public void testJoin2() {
058        Stamp aExact = StampFactory.exactNonNull(getType(A.class));
059        Stamp b = StampFactory.declared(getType(B.class));
060        Assert.assertEquals(StampFactory.empty(Kind.Object), join(aExact, b));
061    }
062
063    @Test
064    public void testJoin3() {
065        Stamp d = StampFactory.declared(getType(D.class));
066        Stamp c = StampFactory.declared(getType(C.class));
067        Assert.assertTrue(StampTool.isPointerAlwaysNull(join(c, d)));
068    }
069
070    @Test
071    public void testJoin4() {
072        Stamp dExactNonNull = StampFactory.exactNonNull(getType(D.class));
073        Stamp c = StampFactory.declared(getType(C.class));
074        Assert.assertEquals(StampFactory.empty(Kind.Object), join(c, dExactNonNull));
075    }
076
077    @Test
078    public void testJoin5() {
079        Stamp dExact = StampFactory.exact(getType(D.class));
080        Stamp c = StampFactory.declared(getType(C.class));
081        Stamp join = join(c, dExact);
082        Assert.assertTrue(StampTool.isPointerAlwaysNull(join));
083        Assert.assertNull(StampTool.typeOrNull(join));
084        Assert.assertFalse(StampTool.isExactType(join));
085    }
086
087    @Test
088    public void testJoin6() {
089        Stamp dExactNonNull = StampFactory.exactNonNull(getType(D.class));
090        Stamp alwaysNull = StampFactory.alwaysNull();
091        Stamp join = join(alwaysNull, dExactNonNull);
092        Assert.assertFalse(join.hasValues());
093        Assert.assertFalse(StampTool.isPointerAlwaysNull(join));
094    }
095
096    @Test
097    public void testJoin7() {
098        Stamp aExact = StampFactory.exact(getType(A.class));
099        Stamp e = StampFactory.declared(getType(E.class));
100        Stamp join = join(aExact, e);
101        Assert.assertTrue(StampTool.isPointerAlwaysNull(join));
102        Assert.assertNull(StampTool.typeOrNull(join));
103        Assert.assertFalse(StampTool.isExactType(join));
104    }
105
106    @Test
107    public void testJoin8() {
108        Stamp bExact = StampFactory.exactNonNull(getType(B.class));
109        Stamp dExact = StampFactory.exact(getType(D.class));
110        Stamp join = join(bExact, dExact);
111        Assert.assertFalse(join.hasValues());
112    }
113
114    @Test
115    public void testJoin9() {
116        Stamp bExact = StampFactory.exact(getType(B.class));
117        Stamp dExact = StampFactory.exact(getType(D.class));
118        Stamp join = join(bExact, dExact);
119        Assert.assertTrue(StampTool.isPointerAlwaysNull(join));
120        Assert.assertNull(StampTool.typeOrNull(join));
121        Assert.assertNull(StampTool.typeOrNull(join));
122    }
123
124    @Test
125    public void testJoinInterfaceSimple() {
126        // Tests joining of interface
127        testJoinInterface(A.class, B.class, I.class);
128    }
129
130    @Test
131    public void testJoinInterfaceArray() {
132        // Tests joining of arrays interface
133        testJoinInterface(A[].class, B[].class, I[].class);
134    }
135
136    @Test
137    public void testJoinInterfaceMultiArray() {
138        // Tests joining of multidimensional arrays of interface
139        testJoinInterface(A[][].class, B[][].class, I[][].class);
140    }
141
142    private void testJoinInterface(Class<?> typeA, Class<?> typeB, Class<?> typeI) {
143        testJoinInterface0(typeA, typeI);
144        testJoinInterface1(typeA, typeI);
145        testJoinInterface2(typeB, typeI);
146        testJoinInterface3(typeB, typeI);
147    }
148
149    private void testJoinInterface0(Class<?> typeA, Class<?> typeI) {
150        Stamp a = StampFactory.declared(getType(typeA));
151        Stamp i = StampFactory.declaredTrusted(getType(typeI));
152        Assert.assertNotSame(StampFactory.empty(Kind.Object), join(a, i));
153    }
154
155    private void testJoinInterface1(Class<?> typeA, Class<?> typeI) {
156        Stamp aNonNull = StampFactory.declaredNonNull(getType(typeA));
157        Stamp i = StampFactory.declaredTrusted(getType(typeI));
158        Stamp join = join(aNonNull, i);
159        Assert.assertTrue(join instanceof ObjectStamp);
160        Assert.assertTrue(((ObjectStamp) join).nonNull());
161    }
162
163    private void testJoinInterface2(Class<?> typeB, Class<?> typeI) {
164        Stamp bExact = StampFactory.exactNonNull(getType(typeB));
165        Stamp i = StampFactory.declaredTrusted(getType(typeI));
166        Stamp join = join(i, bExact);
167        Assert.assertEquals(StampFactory.empty(Kind.Object), join);
168    }
169
170    private void testJoinInterface3(Class<?> typeB, Class<?> typeI) {
171        Stamp bExact = StampFactory.exactNonNull(getType(typeB));
172        Stamp i = StampFactory.declared(getType(typeI)); // not trusted
173        Stamp join = join(i, bExact);
174        Assert.assertEquals(bExact, join);
175    }
176
177}