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