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 package org.graalvm.compiler.graph.test;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
  27 import static org.junit.Assert.assertEquals;
  28 import static org.junit.Assert.assertNotNull;
  29 
  30 import org.junit.Assert;
  31 import org.junit.Test;
  32 
  33 import org.graalvm.compiler.graph.Graph;
  34 import org.graalvm.compiler.graph.IterableNodeType;
  35 import org.graalvm.compiler.graph.Node;
  36 import org.graalvm.compiler.graph.NodeClass;
  37 import org.graalvm.compiler.nodeinfo.NodeInfo;
  38 
  39 public class TypedNodeIteratorTest2 {
  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();
 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();
 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 }