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.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.assertFalse; 29 import static org.junit.Assert.assertNotNull; 30 import static org.junit.Assert.assertTrue; 31 import static org.junit.Assert.fail; 32 33 import java.util.Iterator; 34 35 import org.junit.Test; 36 37 import org.graalvm.compiler.graph.Graph; 38 import org.graalvm.compiler.graph.IterableNodeType; 39 import org.graalvm.compiler.graph.Node; 40 import org.graalvm.compiler.graph.NodeClass; 41 import org.graalvm.compiler.nodeinfo.NodeInfo; 42 43 public class TypedNodeIteratorTest { 44 45 @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED) 46 static final class TestNode extends Node implements IterableNodeType, TestNodeInterface { 47 48 public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class); 49 protected final String name; 50 51 protected TestNode(String name) { 52 super(TYPE); 53 this.name = name; 54 } 55 56 @Override 57 public String getName() { 58 return name; 59 } 60 } 61 62 @Test 63 public void singleNodeTest() { 64 Graph graph = new Graph(); 65 graph.add(new TestNode("a")); 66 assertTrue(graph.hasNode(TestNode.TYPE)); 67 assertEquals("a", toString(graph.getNodes(TestNode.TYPE))); 68 } 69 70 @Test 71 public void deletingNodeTest() { 72 TestNode testNode = new TestNode("a"); 73 Graph graph = new Graph(); 74 graph.add(testNode); 75 testNode.safeDelete(); 76 assertEquals("", toString(graph.getNodes(TestNode.TYPE))); 77 } 78 79 @Test 80 public void deleteAndAddTest() { 81 TestNode testNode = new TestNode("b"); 82 Graph graph = new Graph(); 83 graph.add(new TestNode("a")); 84 graph.add(testNode); 85 testNode.safeDelete(); 86 assertEquals("a", toString(graph.getNodes(TestNode.TYPE))); 87 graph.add(new TestNode("c")); 88 assertEquals("ac", toString(graph.getNodes(TestNode.TYPE))); 89 } 90 91 @Test 92 public void iteratorBehaviorTest() { 93 Graph graph = new Graph(); 94 graph.add(new TestNode("a")); 95 Iterator<TestNode> iterator = graph.getNodes(TestNode.TYPE).iterator(); 96 assertTrue(iterator.hasNext()); 97 assertEquals("a", iterator.next().getName()); 98 assertFalse(iterator.hasNext()); 99 graph.add(new TestNode("b")); 100 assertTrue(iterator.hasNext()); 101 assertEquals("b", iterator.next().getName()); 102 assertFalse(iterator.hasNext()); 103 TestNode c = new TestNode("c"); 104 graph.add(c); 105 assertTrue(iterator.hasNext()); 106 c.safeDelete(); 107 assertFalse(iterator.hasNext()); 108 } 109 110 @Test 111 public void complicatedIterationTest() { 112 Graph graph = new Graph(); 113 graph.add(new TestNode("a")); 114 for (TestNode tn : graph.getNodes(TestNode.TYPE)) { 115 String name = tn.getName(); 116 for (int i = 0; i < name.length(); ++i) { 117 char c = name.charAt(i); 118 if (c == 'a') { 119 tn.safeDelete(); 120 graph.add(new TestNode("b")); 121 graph.add(new TestNode("c")); 122 } else if (c == 'b') { 123 tn.safeDelete(); 124 } else if (c == 'c') { 125 graph.add(new TestNode("d")); 126 graph.add(new TestNode("e")); 127 graph.add(new TestNode("d")); 128 graph.add(new TestNode("e")); 129 graph.add(new TestNode("e")); 130 graph.add(new TestNode("d")); 131 graph.add(new TestNode("e")); 132 graph.add(new TestNode("d")); 133 } else if (c == 'd') { 134 for (TestNode tn2 : graph.getNodes(TestNode.TYPE)) { 135 if (tn2.getName().equals("e")) { 136 tn2.safeDelete(); 137 } else if (tn2.getName().equals("c")) { 138 tn2.safeDelete(); 139 } 140 } 141 } else if (c == 'e') { 142 fail("All e nodes must have been deleted by visiting the d node"); 143 } 144 } 145 } 146 assertEquals("dddd", toString(graph.getNodes(TestNode.TYPE))); 147 } 148 149 @Test 150 public void addingNodeDuringIterationTest() { 151 Graph graph = new Graph(); 152 graph.add(new TestNode("a")); 153 StringBuilder sb = new StringBuilder(); 154 int z = 0; 155 for (TestNode tn : graph.getNodes(TestNode.TYPE)) { 156 if (z == 0) { 157 graph.add(new TestNode("b")); 158 } 159 sb.append(tn.getName()); 160 z++; 161 } 162 assertEquals(2, z); 163 assertEquals("ab", sb.toString()); 164 z = 0; 165 for (TestNode tn : graph.getNodes(TestNode.TYPE)) { 166 if (z == 0) { 167 graph.add(new TestNode("c")); 168 } 169 assertNotNull(tn); 170 z++; 171 } 172 assertEquals(3, z); 173 } 174 175 public static String toString(Iterable<? extends TestNodeInterface> nodes) { 176 StringBuilder sb = new StringBuilder(); 177 for (TestNodeInterface tn : nodes) { 178 sb.append(tn.getName()); 179 } 180 return sb.toString(); 181 } 182 }