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