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