001/*
002 * Copyright (c) 2011, 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.compiler.test;
024
025import com.oracle.graal.debug.*;
026
027import org.junit.*;
028
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
031import com.oracle.graal.phases.common.*;
032import com.oracle.graal.phases.tiers.*;
033
034/**
035 * In the following tests, the scalar type system of the compiler should be complete enough to see
036 * the relation between the different conditions.
037 */
038public class ScalarTypeSystemTest extends GraalCompilerTest {
039
040    public static int referenceSnippet1(int a) {
041        if (a > 0) {
042            return 1;
043        } else {
044            return 2;
045        }
046    }
047
048    @Test(expected = AssertionError.class)
049    public void test1() {
050        test("test1Snippet", "referenceSnippet1");
051    }
052
053    public static int test1Snippet(int a) {
054        if (a > 0) {
055            if (a > -1) {
056                return 1;
057            } else {
058                return 3;
059            }
060        } else {
061            return 2;
062        }
063    }
064
065    @Test(expected = AssertionError.class)
066    public void test2() {
067        test("test2Snippet", "referenceSnippet1");
068    }
069
070    public static int test2Snippet(int a) {
071        if (a > 0) {
072            if (a == -15) {
073                return 3;
074            } else {
075                return 1;
076            }
077        } else {
078            return 2;
079        }
080    }
081
082    @Test(expected = AssertionError.class)
083    public void test3() {
084        test("test3Snippet", "referenceSnippet2");
085    }
086
087    public static int referenceSnippet2(int a, int b) {
088        if (a > b) {
089            return 1;
090        } else {
091            return 2;
092        }
093    }
094
095    public static int test3Snippet(int a, int b) {
096        if (a > b) {
097            if (a == b) {
098                return 3;
099            } 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        Debug.dump(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}