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.*;
030
031public class ObjectStampMeetTest extends AbstractObjectStampTest {
032
033    // class A
034    // class B extends A
035    // class C extends B implements I
036    // class D extends A
037    // abstract class E extends A
038    // interface I
039
040    @Test
041    public void testMeet0() {
042        Stamp a = StampFactory.declared(getType(A.class));
043        Stamp b = StampFactory.declared(getType(B.class));
044        Assert.assertEquals(a, meet(a, b));
045    }
046
047    @Test
048    public void testMeet1() {
049        Stamp a = StampFactory.declared(getType(A.class));
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(a, meet(aNonNull, b));
054        Assert.assertEquals(aNonNull, meet(aNonNull, bNonNull));
055    }
056
057    @Test
058    public void testMeet2() {
059        Stamp a = StampFactory.declared(getType(A.class));
060        Stamp aExact = StampFactory.exactNonNull(getType(A.class));
061        Stamp b = StampFactory.declared(getType(B.class));
062        Assert.assertEquals(a, meet(aExact, b));
063    }
064
065    @Test
066    public void testMeet3() {
067        Stamp a = StampFactory.declared(getType(A.class));
068        Stamp d = StampFactory.declared(getType(D.class));
069        Stamp c = StampFactory.declared(getType(C.class));
070        Assert.assertEquals(a, meet(c, d));
071    }
072
073    @Test
074    public void testMeet4() {
075        Stamp dExactNonNull = StampFactory.exactNonNull(getType(D.class));
076        Stamp cExactNonNull = StampFactory.exactNonNull(getType(C.class));
077        Stamp aNonNull = StampFactory.declaredNonNull(getType(A.class));
078        Assert.assertEquals(aNonNull, meet(cExactNonNull, dExactNonNull));
079    }
080
081    @Test
082    public void testMeet() {
083        Stamp dExact = StampFactory.exact(getType(D.class));
084        Stamp c = StampFactory.declared(getType(C.class));
085        Stamp a = StampFactory.declared(getType(A.class));
086        Assert.assertEquals(a, meet(dExact, c));
087    }
088
089    @Test
090    public void testMeet6() {
091        Stamp dExactNonNull = StampFactory.exactNonNull(getType(D.class));
092        Stamp alwaysNull = StampFactory.alwaysNull();
093        Stamp dExact = StampFactory.exact(getType(D.class));
094        Assert.assertEquals(dExact, meet(dExactNonNull, alwaysNull));
095    }
096
097    @Test
098    public void testMeet7() {
099        Stamp aExact = StampFactory.exact(getType(A.class));
100        Stamp e = StampFactory.declared(getType(E.class));
101        Stamp a = StampFactory.declared(getType(A.class));
102        Assert.assertEquals(a, meet(aExact, e));
103    }
104
105    @Test
106    public void testMeetInterface0() {
107        Stamp a = StampFactory.declared(getType(A.class));
108        Stamp i = StampFactory.declaredTrusted(getType(I.class));
109        Assert.assertEquals(StampFactory.declared(getType(Object.class)), meet(a, i));
110    }
111
112    @Test
113    public void testMeetIllegal1() {
114        for (Class<?> clazz : new Class<?>[]{A.class, B.class, C.class, D.class, E.class, I.class, Object.class}) {
115            ResolvedJavaType type = getType(clazz);
116            for (Stamp test : new Stamp[]{StampFactory.declared(type), StampFactory.declaredNonNull(type), StampFactory.exact(type), StampFactory.exactNonNull(type)}) {
117                if (type.isConcrete() || !((ObjectStamp) test).isExactType()) {
118                    Assert.assertEquals("meeting empty and " + test, test, meet(StampFactory.empty(Kind.Object), test));
119                }
120            }
121        }
122    }
123}