001/*
002 * Copyright (c) 2013, 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.graph.*;
030import com.oracle.graal.nodeinfo.*;
031
032public class TypedNodeIteratorTest2 {
033
034    @NodeInfo
035    static class NodeA extends Node implements TestNodeInterface {
036
037        public static final NodeClass<NodeA> TYPE = NodeClass.create(NodeA.class);
038        protected final String name;
039
040        public NodeA(String name) {
041            this(TYPE, name);
042        }
043
044        protected NodeA(NodeClass<? extends NodeA> c, String name) {
045            super(c);
046            this.name = name;
047        }
048
049        public String getName() {
050            return name;
051        }
052    }
053
054    @NodeInfo
055    static class NodeB extends NodeA implements IterableNodeType {
056        public static final NodeClass<NodeB> TYPE = NodeClass.create(NodeB.class);
057
058        public NodeB(String name) {
059            this(TYPE, name);
060        }
061
062        protected NodeB(NodeClass<? extends NodeB> c, String name) {
063            super(c, name);
064        }
065
066    }
067
068    @NodeInfo
069    static class NodeC extends NodeB {
070        public static final NodeClass<NodeC> TYPE = NodeClass.create(NodeC.class);
071
072        public NodeC(String name) {
073            this(TYPE, name);
074        }
075
076        protected NodeC(NodeClass<? extends NodeC> c, String name) {
077            super(c, name);
078        }
079
080    }
081
082    @NodeInfo
083    static final class NodeD extends NodeC {
084        public static final NodeClass<NodeD> TYPE = NodeClass.create(NodeD.class);
085
086        public NodeD(String name) {
087            super(TYPE, name);
088        }
089
090    }
091
092    @Test
093    public void simpleSubclassTest() {
094        Graph graph = new Graph();
095        graph.add(new NodeB("b"));
096        graph.add(new NodeD("d"));
097
098        Assert.assertEquals("bd", TypedNodeIteratorTest.toString(graph.getNodes(NodeB.TYPE)));
099        Assert.assertEquals("d", TypedNodeIteratorTest.toString(graph.getNodes(NodeD.TYPE)));
100    }
101
102    @Test
103    public void addingNodeDuringIterationTest() {
104        Graph graph = new Graph();
105        graph.add(new NodeB("b1"));
106        NodeD d1 = graph.add(new NodeD("d1"));
107        StringBuilder sb = new StringBuilder();
108        for (NodeB tn : graph.getNodes(NodeB.TYPE)) {
109            if (tn == d1) {
110                graph.add(new NodeB("b2"));
111            }
112            sb.append(tn.getName());
113        }
114        assertEquals("b1d1b2", sb.toString());
115        for (NodeB tn : graph.getNodes(NodeB.TYPE)) {
116            if (tn == d1) {
117                graph.add(new NodeB("b3"));
118            }
119            assertNotNull(tn);
120        }
121        assertEquals(4, graph.getNodes(NodeB.TYPE).count());
122        assertEquals(1, graph.getNodes(NodeD.TYPE).count());
123    }
124
125}