TypedNodeIteratorTest2.java revision 12968:4d8a004e5c6d
1219820Sjeff/*
2219820Sjeff * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3219820Sjeff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219820Sjeff *
5219820Sjeff * This code is free software; you can redistribute it and/or modify it
6219820Sjeff * under the terms of the GNU General Public License version 2 only, as
7219820Sjeff * published by the Free Software Foundation.
8219820Sjeff *
9219820Sjeff * This code is distributed in the hope that it will be useful, but WITHOUT
10219820Sjeff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11219820Sjeff * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12219820Sjeff * version 2 for more details (a copy is included in the LICENSE file that
13219820Sjeff * accompanied this code).
14219820Sjeff *
15219820Sjeff * You should have received a copy of the GNU General Public License version
16219820Sjeff * 2 along with this work; if not, write to the Free Software Foundation,
17219820Sjeff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219820Sjeff *
19219820Sjeff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20219820Sjeff * or visit www.oracle.com if you need additional information or have any
21219820Sjeff * 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.assertNotNull;
29
30import org.junit.Assert;
31import org.junit.Test;
32
33import org.graalvm.compiler.graph.Graph;
34import org.graalvm.compiler.graph.IterableNodeType;
35import org.graalvm.compiler.graph.Node;
36import org.graalvm.compiler.graph.NodeClass;
37import org.graalvm.compiler.nodeinfo.NodeInfo;
38
39public class TypedNodeIteratorTest2 extends GraphTest {
40
41    @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
42    static class NodeA extends Node implements TestNodeInterface {
43
44        public static final NodeClass<NodeA> TYPE = NodeClass.create(NodeA.class);
45        protected final String name;
46
47        protected NodeA(String name) {
48            this(TYPE, name);
49        }
50
51        protected NodeA(NodeClass<? extends NodeA> c, String name) {
52            super(c);
53            this.name = name;
54        }
55
56        @Override
57        public String getName() {
58            return name;
59        }
60    }
61
62    @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
63    static class NodeB extends NodeA implements IterableNodeType {
64        public static final NodeClass<NodeB> TYPE = NodeClass.create(NodeB.class);
65
66        protected NodeB(String name) {
67            this(TYPE, name);
68        }
69
70        protected NodeB(NodeClass<? extends NodeB> c, String name) {
71            super(c, name);
72        }
73
74    }
75
76    @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
77    static class NodeC extends NodeB {
78        public static final NodeClass<NodeC> TYPE = NodeClass.create(NodeC.class);
79
80        protected NodeC(String name) {
81            this(TYPE, name);
82        }
83
84        protected NodeC(NodeClass<? extends NodeC> c, String name) {
85            super(c, name);
86        }
87
88    }
89
90    @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
91    static final class NodeD extends NodeC {
92        public static final NodeClass<NodeD> TYPE = NodeClass.create(NodeD.class);
93
94        protected NodeD(String name) {
95            super(TYPE, name);
96        }
97
98    }
99
100    @Test
101    public void simpleSubclassTest() {
102        Graph graph = new Graph(getOptions());
103        graph.add(new NodeB("b"));
104        graph.add(new NodeD("d"));
105
106        Assert.assertEquals("bd", TypedNodeIteratorTest.toString(graph.getNodes(NodeB.TYPE)));
107        Assert.assertEquals("d", TypedNodeIteratorTest.toString(graph.getNodes(NodeD.TYPE)));
108    }
109
110    @Test
111    public void addingNodeDuringIterationTest() {
112        Graph graph = new Graph(getOptions());
113        graph.add(new NodeB("b1"));
114        NodeD d1 = graph.add(new NodeD("d1"));
115        StringBuilder sb = new StringBuilder();
116        for (NodeB tn : graph.getNodes(NodeB.TYPE)) {
117            if (tn == d1) {
118                graph.add(new NodeB("b2"));
119            }
120            sb.append(tn.getName());
121        }
122        assertEquals("b1d1b2", sb.toString());
123        for (NodeB tn : graph.getNodes(NodeB.TYPE)) {
124            if (tn == d1) {
125                graph.add(new NodeB("b3"));
126            }
127            assertNotNull(tn);
128        }
129        assertEquals(4, graph.getNodes(NodeB.TYPE).count());
130        assertEquals(1, graph.getNodes(NodeD.TYPE).count());
131    }
132
133}
134