1 /*
   2  * Copyright (c) 2011, 2018, 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.core.test;
  26 
  27 import org.graalvm.compiler.debug.DebugContext;
  28 import org.graalvm.compiler.nodes.StructuredGraph;
  29 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  30 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  31 import org.graalvm.compiler.phases.tiers.PhaseContext;
  32 import org.junit.Test;
  33 
  34 /**
  35  * In the following tests, the scalar type system of the compiler should be complete enough to see
  36  * the relation between the different conditions.
  37  */
  38 public class ScalarTypeSystemTest extends GraalCompilerTest {
  39 
  40     public static int referenceSnippet1(int a) {
  41         if (a > 0) {
  42             return 1;
  43         } else {
  44             return 2;
  45         }
  46     }
  47 
  48     @Test(expected = AssertionError.class)
  49     public void test1() {
  50         test("test1Snippet", "referenceSnippet1");
  51     }
  52 
  53     public static int test1Snippet(int a) {
  54         if (a > 0) {
  55             if (a > -1) {
  56                 return 1;
  57             } else {
  58                 return 3;
  59             }
  60         } else {
  61             return 2;
  62         }
  63     }
  64 
  65     @Test(expected = AssertionError.class)
  66     public void test2() {
  67         test("test2Snippet", "referenceSnippet1");
  68     }
  69 
  70     public static int test2Snippet(int a) {
  71         if (a > 0) {
  72             if (a == -15) {
  73                 return 3;
  74             } else {
  75                 return 1;
  76             }
  77         } else {
  78             return 2;
  79         }
  80     }
  81 
  82     @Test(expected = AssertionError.class)
  83     public void test3() {
  84         test("test3Snippet", "referenceSnippet2");
  85     }
  86 
  87     public static int referenceSnippet2(int a, int b) {
  88         if (a > b) {
  89             return 1;
  90         } else {
  91             return 2;
  92         }
  93     }
  94 
  95     public static int test3Snippet(int a, int b) {
  96         if (a > b) {
  97             if (a == b) {
  98                 return 3;
  99             } else {
 100                 return 1;
 101             }
 102         } else {
 103             return 2;
 104         }
 105     }
 106 
 107     public static int referenceSnippet3(int a, int b) {
 108         if (a == b) {
 109             return 1;
 110         } else {
 111             return 2;
 112         }
 113     }
 114 
 115     @Test(expected = AssertionError.class)
 116     public void test6() {
 117         test("test6Snippet", "referenceSnippet3");
 118     }
 119 
 120     public static int test6Snippet(int a, int b) {
 121         if (a == b) {
 122             if (a == b + 1) {
 123                 return 3;
 124             } else {
 125                 return 1;
 126             }
 127         } else {
 128             return 2;
 129         }
 130     }
 131 
 132     private void test(final String snippet, final String referenceSnippet) {
 133         // No debug scope to reduce console noise for @Test(expected = ...) tests
 134         StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
 135         graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "Graph");
 136         PhaseContext context = new PhaseContext(getProviders());
 137         new CanonicalizerPhase().apply(graph, context);
 138         StructuredGraph referenceGraph = parseEager(referenceSnippet, AllowAssumptions.NO);
 139         assertEquals(referenceGraph, graph);
 140     }
 141 }