001/*
002 * Copyright (c) 2011, 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 java.util.*;
028
029import org.junit.*;
030
031import com.oracle.graal.graph.*;
032import com.oracle.graal.nodeinfo.*;
033
034public class TypedNodeIteratorTest {
035
036    @NodeInfo
037    static final class TestNode extends Node implements IterableNodeType, TestNodeInterface {
038
039        public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class);
040        protected final String name;
041
042        public TestNode(String name) {
043            super(TYPE);
044            this.name = name;
045        }
046
047        public String getName() {
048            return name;
049        }
050    }
051
052    @Test
053    public void singleNodeTest() {
054        Graph graph = new Graph();
055        graph.add(new TestNode("a"));
056        assertTrue(graph.hasNode(TestNode.TYPE));
057        assertEquals("a", toString(graph.getNodes(TestNode.TYPE)));
058    }
059
060    @Test
061    public void deletingNodeTest() {
062        TestNode testNode = new TestNode("a");
063        Graph graph = new Graph();
064        graph.add(testNode);
065        testNode.safeDelete();
066        assertEquals("", toString(graph.getNodes(TestNode.TYPE)));
067    }
068
069    @Test
070    public void deleteAndAddTest() {
071        TestNode testNode = new TestNode("b");
072        Graph graph = new Graph();
073        graph.add(new TestNode("a"));
074        graph.add(testNode);
075        testNode.safeDelete();
076        assertEquals("a", toString(graph.getNodes(TestNode.TYPE)));
077        graph.add(new TestNode("c"));
078        assertEquals("ac", toString(graph.getNodes(TestNode.TYPE)));
079    }
080
081    @Test
082    public void iteratorBehaviorTest() {
083        Graph graph = new Graph();
084        graph.add(new TestNode("a"));
085        Iterator<TestNode> iterator = graph.getNodes(TestNode.TYPE).iterator();
086        assertTrue(iterator.hasNext());
087        assertEquals("a", iterator.next().getName());
088        assertFalse(iterator.hasNext());
089        graph.add(new TestNode("b"));
090        assertTrue(iterator.hasNext());
091        assertEquals("b", iterator.next().getName());
092        assertFalse(iterator.hasNext());
093        TestNode c = new TestNode("c");
094        graph.add(c);
095        assertTrue(iterator.hasNext());
096        c.safeDelete();
097        assertFalse(iterator.hasNext());
098    }
099
100    @Test
101    public void complicatedIterationTest() {
102        Graph graph = new Graph();
103        graph.add(new TestNode("a"));
104        for (TestNode tn : graph.getNodes(TestNode.TYPE)) {
105            String name = tn.getName();
106            for (int i = 0; i < name.length(); ++i) {
107                char c = name.charAt(i);
108                if (c == 'a') {
109                    tn.safeDelete();
110                    graph.add(new TestNode("b"));
111                    graph.add(new TestNode("c"));
112                } else if (c == 'b') {
113                    tn.safeDelete();
114                } else if (c == 'c') {
115                    graph.add(new TestNode("d"));
116                    graph.add(new TestNode("e"));
117                    graph.add(new TestNode("d"));
118                    graph.add(new TestNode("e"));
119                    graph.add(new TestNode("e"));
120                    graph.add(new TestNode("d"));
121                    graph.add(new TestNode("e"));
122                    graph.add(new TestNode("d"));
123                } else if (c == 'd') {
124                    for (TestNode tn2 : graph.getNodes(TestNode.TYPE)) {
125                        if (tn2.getName().equals("e")) {
126                            tn2.safeDelete();
127                        } else if (tn2.getName().equals("c")) {
128                            tn2.safeDelete();
129                        }
130                    }
131                } else if (c == 'e') {
132                    fail("All e nodes must have been deleted by visiting the d node");
133                }
134            }
135        }
136        assertEquals("dddd", toString(graph.getNodes(TestNode.TYPE)));
137    }
138
139    @Test
140    public void addingNodeDuringIterationTest() {
141        Graph graph = new Graph();
142        graph.add(new TestNode("a"));
143        StringBuilder sb = new StringBuilder();
144        int z = 0;
145        for (TestNode tn : graph.getNodes(TestNode.TYPE)) {
146            if (z == 0) {
147                graph.add(new TestNode("b"));
148            }
149            sb.append(tn.getName());
150            z++;
151        }
152        assertEquals(2, z);
153        assertEquals("ab", sb.toString());
154        z = 0;
155        for (TestNode tn : graph.getNodes(TestNode.TYPE)) {
156            if (z == 0) {
157                graph.add(new TestNode("c"));
158            }
159            assertNotNull(tn);
160            z++;
161        }
162        assertEquals(3, z);
163    }
164
165    public static String toString(Iterable<? extends TestNodeInterface> nodes) {
166        StringBuilder sb = new StringBuilder();
167        for (TestNodeInterface tn : nodes) {
168            sb.append(tn.getName());
169        }
170        return sb.toString();
171    }
172}