1 /*
   2  * Copyright (c) 2013, 2015, 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 
  24 
  25 package org.graalvm.compiler.graph.test;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertNotNull;
  31 
  32 import org.junit.Assert;
  33 import org.junit.Test;
  34 
  35 import org.graalvm.compiler.graph.Graph;
  36 import org.graalvm.compiler.graph.IterableNodeType;
  37 import org.graalvm.compiler.graph.Node;
  38 import org.graalvm.compiler.graph.NodeClass;
  39 import org.graalvm.compiler.nodeinfo.NodeInfo;
  40 
  41 public class TypedNodeIteratorTest2 extends GraphTest {
  42 
  43     @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  44     static class NodeA extends Node implements TestNodeInterface {
  45 
  46         public static final NodeClass<NodeA> TYPE = NodeClass.create(NodeA.class);
  47         protected final String name;
  48 
  49         protected NodeA(String name) {
  50             this(TYPE, name);
  51         }
  52 
  53         protected NodeA(NodeClass<? extends NodeA> c, String name) {
  54             super(c);
  55             this.name = name;
  56         }
  57 
  58         @Override
  59         public String getName() {
  60             return name;
  61         }
  62     }
  63 
  64     @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  65     static class NodeB extends NodeA implements IterableNodeType {
  66         public static final NodeClass<NodeB> TYPE = NodeClass.create(NodeB.class);
  67 
  68         protected NodeB(String name) {
  69             this(TYPE, name);
  70         }
  71 
  72         protected NodeB(NodeClass<? extends NodeB> c, String name) {
  73             super(c, name);
  74         }
  75 
  76     }
  77 
  78     @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  79     static class NodeC extends NodeB {
  80         public static final NodeClass<NodeC> TYPE = NodeClass.create(NodeC.class);
  81 
  82         protected NodeC(String name) {
  83             this(TYPE, name);
  84         }
  85 
  86         protected NodeC(NodeClass<? extends NodeC> c, String name) {
  87             super(c, name);
  88         }
  89 
  90     }
  91 
  92     @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  93     static final class NodeD extends NodeC {
  94         public static final NodeClass<NodeD> TYPE = NodeClass.create(NodeD.class);
  95 
  96         protected NodeD(String name) {
  97             super(TYPE, name);
  98         }
  99 
 100     }
 101 
 102     @Test
 103     public void simpleSubclassTest() {
 104         Graph graph = new Graph(getOptions(), getDebug());
 105         graph.add(new NodeB("b"));
 106         graph.add(new NodeD("d"));
 107 
 108         Assert.assertEquals("bd", TypedNodeIteratorTest.toString(graph.getNodes(NodeB.TYPE)));
 109         Assert.assertEquals("d", TypedNodeIteratorTest.toString(graph.getNodes(NodeD.TYPE)));
 110     }
 111 
 112     @Test
 113     public void addingNodeDuringIterationTest() {
 114         Graph graph = new Graph(getOptions(), getDebug());
 115         graph.add(new NodeB("b1"));
 116         NodeD d1 = graph.add(new NodeD("d1"));
 117         StringBuilder sb = new StringBuilder();
 118         for (NodeB tn : graph.getNodes(NodeB.TYPE)) {
 119             if (tn == d1) {
 120                 graph.add(new NodeB("b2"));
 121             }
 122             sb.append(tn.getName());
 123         }
 124         assertEquals("b1d1b2", sb.toString());
 125         for (NodeB tn : graph.getNodes(NodeB.TYPE)) {
 126             if (tn == d1) {
 127                 graph.add(new NodeB("b3"));
 128             }
 129             assertNotNull(tn);
 130         }
 131         assertEquals(4, graph.getNodes(NodeB.TYPE).count());
 132         assertEquals(1, graph.getNodes(NodeD.TYPE).count());
 133     }
 134 
 135 }