001/* 002 * Copyright (c) 2014, 2015, 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.graph.test; 024 025import static org.junit.Assert.*; 026 027import org.junit.*; 028 029import com.oracle.graal.api.runtime.*; 030import com.oracle.graal.graph.*; 031import com.oracle.graal.nodeinfo.*; 032 033public class NodeMapTest { 034 035 @NodeInfo 036 static final class TestNode extends Node { 037 public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class); 038 039 protected TestNode() { 040 super(TYPE); 041 } 042 } 043 044 private Graph graph; 045 private TestNode[] nodes = new TestNode[100]; 046 private NodeMap<Integer> map; 047 048 @Before 049 public void before() { 050 // Need to initialize HotSpotGraalRuntime before any Node class is initialized. 051 Graal.getRuntime(); 052 053 graph = new Graph(); 054 for (int i = 0; i < nodes.length; i++) { 055 nodes[i] = graph.add(new TestNode()); 056 } 057 map = new NodeMap<>(graph); 058 for (int i = 0; i < nodes.length; i += 2) { 059 map.set(nodes[i], i); 060 } 061 } 062 063 @Test 064 public void testEmpty() { 065 NodeMap<Integer> emptyMap = new NodeMap<>(graph); 066 for (TestNode node : nodes) { 067 assertEquals(null, emptyMap.get(node)); 068 } 069 } 070 071 @Test 072 public void testSimple() { 073 for (int i = 0; i < nodes.length; i++) { 074 if ((i & 1) == 0) { 075 assertEquals((Integer) i, map.get(nodes[i])); 076 } else { 077 assertEquals(null, map.get(nodes[i])); 078 } 079 } 080 } 081 082 @Test 083 public void testSimpleChanged() { 084 for (TestNode node : nodes) { 085 map.set(node, 1); 086 } 087 for (TestNode node : nodes) { 088 map.set(node, null); 089 } 090 for (int i = 0; i < nodes.length; i += 2) { 091 map.set(nodes[i], i); 092 } 093 094 for (int i = 0; i < nodes.length; i++) { 095 if ((i & 1) == 0) { 096 assertEquals((Integer) i, map.get(nodes[i])); 097 } else { 098 assertEquals(null, map.get(nodes[i])); 099 } 100 } 101 } 102 103 @SuppressWarnings("all") 104 private static boolean assertionsEnabled() { 105 boolean assertionsEnabled = false; 106 assert assertionsEnabled = true; 107 return assertionsEnabled; 108 } 109 110 @Test 111 public void testNewGet() { 112 /* 113 * Failing here is not required, but if this behavior changes, usages of get need to be 114 * checked for compatibility. 115 */ 116 TestNode newNode = graph.add(new TestNode()); 117 try { 118 map.get(newNode); 119 fail("expected " + (assertionsEnabled() ? AssertionError.class.getSimpleName() : ArrayIndexOutOfBoundsException.class.getSimpleName())); 120 } catch (AssertionError ae) { 121 // thrown when assertions are enabled 122 } catch (ArrayIndexOutOfBoundsException e) { 123 // thrown when assertions are disabled 124 } 125 } 126 127 @Test 128 public void testNewSet() { 129 /* 130 * Failing here is not required, but if this behavior changes, usages of set need to be 131 * checked for compatibility. 132 */ 133 TestNode newNode = graph.add(new TestNode()); 134 try { 135 map.set(newNode, 1); 136 fail("expected " + (assertionsEnabled() ? AssertionError.class.getSimpleName() : ArrayIndexOutOfBoundsException.class.getSimpleName())); 137 } catch (AssertionError ae) { 138 // thrown when assertions are enabled 139 } catch (ArrayIndexOutOfBoundsException e) { 140 // thrown when assertions are disabled 141 } 142 } 143 144 @Test 145 public void testNewGetAndGrow() { 146 TestNode newNode = graph.add(new TestNode()); 147 assertEquals(null, map.getAndGrow(newNode)); 148 } 149 150 @Test 151 public void testNewSetAndGrow() { 152 TestNode newNode = graph.add(new TestNode()); 153 map.setAndGrow(newNode, 1); 154 assertEquals((Integer) 1, map.get(newNode)); 155 } 156}