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