1 /* 2 * Copyright (c) 2014, 2015, 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.graph.test; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED; 26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED; 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.fail; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 33 import org.graalvm.compiler.api.test.Graal; 34 import org.graalvm.compiler.graph.Graph; 35 import org.graalvm.compiler.graph.Node; 36 import org.graalvm.compiler.graph.NodeClass; 37 import org.graalvm.compiler.graph.NodeMap; 38 import org.graalvm.compiler.nodeinfo.NodeInfo; 39 40 public class NodeMapTest { 41 42 @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED) 43 static final class TestNode extends Node { 44 public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class); 45 46 protected TestNode() { 47 super(TYPE); 48 } 49 } 50 51 private Graph graph; 52 private TestNode[] nodes = new TestNode[100]; 53 private NodeMap<Integer> map; 54 55 @Before 56 public void before() { 57 // Need to initialize HotSpotGraalRuntime before any Node class is initialized. 58 Graal.getRuntime(); 59 60 graph = new Graph(); 61 for (int i = 0; i < nodes.length; i++) { 62 nodes[i] = graph.add(new TestNode()); 63 } 64 map = new NodeMap<>(graph); 65 for (int i = 0; i < nodes.length; i += 2) { 66 map.set(nodes[i], i); 67 } 68 } 69 70 @Test 71 public void testEmpty() { 72 NodeMap<Integer> emptyMap = new NodeMap<>(graph); 73 for (TestNode node : nodes) { 74 assertEquals(null, emptyMap.get(node)); 75 } 76 } 77 78 @Test 79 public void testSimple() { 80 for (int i = 0; i < nodes.length; i++) { 81 if ((i & 1) == 0) { 82 assertEquals((Integer) i, map.get(nodes[i])); 83 } else { 84 assertEquals(null, map.get(nodes[i])); 85 } 86 } 87 } 88 89 @Test 90 public void testSimpleChanged() { 91 for (TestNode node : nodes) { 92 map.set(node, 1); 93 } 94 for (TestNode node : nodes) { 95 map.set(node, null); 96 } 97 for (int i = 0; i < nodes.length; i += 2) { 98 map.set(nodes[i], i); 99 } 100 101 for (int i = 0; i < nodes.length; i++) { 102 if ((i & 1) == 0) { 103 assertEquals((Integer) i, map.get(nodes[i])); 104 } else { 105 assertEquals(null, map.get(nodes[i])); 106 } 107 } 108 } 109 110 @SuppressWarnings("all") 111 private static boolean assertionsEnabled() { 112 boolean assertionsEnabled = false; 113 assert assertionsEnabled = true; 114 return assertionsEnabled; 115 } 116 117 @Test 118 public void testNewGet() { 119 /* 120 * Failing here is not required, but if this behavior changes, usages of get need to be 121 * checked for compatibility. 122 */ 123 TestNode newNode = graph.add(new TestNode()); 124 try { 125 map.get(newNode); 126 fail("expected " + (assertionsEnabled() ? AssertionError.class.getSimpleName() : ArrayIndexOutOfBoundsException.class.getSimpleName())); 127 } catch (AssertionError ae) { 128 // thrown when assertions are enabled 129 } catch (ArrayIndexOutOfBoundsException e) { 130 // thrown when assertions are disabled 131 } 132 } 133 134 @Test 135 public void testNewSet() { 136 /* 137 * Failing here is not required, but if this behavior changes, usages of set need to be 138 * checked for compatibility. 139 */ 140 TestNode newNode = graph.add(new TestNode()); 141 try { 142 map.set(newNode, 1); 143 fail("expected " + (assertionsEnabled() ? AssertionError.class.getSimpleName() : ArrayIndexOutOfBoundsException.class.getSimpleName())); 144 } catch (AssertionError ae) { 145 // thrown when assertions are enabled 146 } catch (ArrayIndexOutOfBoundsException e) { 147 // thrown when assertions are disabled 148 } 149 } 150 151 @Test 152 public void testNewGetAndGrow() { 153 TestNode newNode = graph.add(new TestNode()); 154 assertEquals(null, map.getAndGrow(newNode)); 155 } 156 157 @Test 158 public void testNewSetAndGrow() { 159 TestNode newNode = graph.add(new TestNode()); 160 map.setAndGrow(newNode, 1); 161 assertEquals((Integer) 1, map.get(newNode)); 162 } 163 }