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