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