001/*
002 * Copyright (c) 2012, 2012, 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.compiler.test;
024
025import org.junit.*;
026
027import com.oracle.graal.graph.*;
028import com.oracle.graal.nodeinfo.*;
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.nodes.calc.*;
031
032public class NodePosIteratorTest extends GraalCompilerTest {
033
034    @NodeInfo
035    static final class TestNode extends Node {
036        public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class);
037        @Successor Node s1;
038        @Successor Node s2;
039        @Successor NodeSuccessorList<Node> stail;
040
041        @Input NodeInputList<ValueNode> itail;
042        @Input ConstantNode i1;
043        @Input FloatingNode i2;
044
045        public TestNode() {
046            super(TYPE);
047        }
048
049    }
050
051    @Test
052    public void testInputs() {
053        TestNode n = new TestNode();
054
055        ConstantNode i1 = ConstantNode.forInt(1);
056        ConstantNode i2 = ConstantNode.forDouble(1.0d);
057        ConstantNode i3 = ConstantNode.forInt(4);
058        ConstantNode i4 = ConstantNode.forInt(14);
059        n.itail = new NodeInputList<>(n, new ValueNode[]{i3, i4});
060        n.i1 = i1;
061        n.i2 = i2;
062
063        NodeClassIterable inputs = n.inputs();
064
065        NodePosIterator iterator = inputs.iterator();
066        Assert.assertTrue(iterator.hasNext());
067        Assert.assertTrue(iterator.hasNext());
068        Assert.assertEquals(iterator.next(), i1);
069        Assert.assertTrue(iterator.hasNext());
070        Assert.assertTrue(iterator.hasNext());
071        Assert.assertEquals(iterator.next(), i2);
072        Assert.assertTrue(iterator.hasNext());
073        Assert.assertTrue(iterator.hasNext());
074        Assert.assertEquals(iterator.next(), i3);
075        Assert.assertTrue(iterator.hasNext());
076        Assert.assertTrue(iterator.hasNext());
077        Assert.assertEquals(iterator.next(), i4);
078        Assert.assertFalse(iterator.hasNext());
079        Assert.assertFalse(iterator.hasNext());
080
081        iterator = inputs.iterator();
082        Assert.assertTrue(iterator.hasNext());
083        Assert.assertTrue(iterator.hasNext());
084        Assert.assertEquals("ConstantNode:i1", iterator.nextPosition().toString());
085        Assert.assertTrue(iterator.hasNext());
086        Assert.assertTrue(iterator.hasNext());
087        Assert.assertEquals("FloatingNode:i2", iterator.nextPosition().toString());
088        Assert.assertTrue(iterator.hasNext());
089        Assert.assertTrue(iterator.hasNext());
090        Assert.assertEquals("NodeInputList:itail[0]", iterator.nextPosition().toString());
091        Assert.assertTrue(iterator.hasNext());
092        Assert.assertTrue(iterator.hasNext());
093        Assert.assertEquals("NodeInputList:itail[1]", iterator.nextPosition().toString());
094        Assert.assertFalse(iterator.hasNext());
095        Assert.assertFalse(iterator.hasNext());
096
097        iterator = inputs.iterator();
098        n.i1 = i4;
099        Assert.assertTrue(iterator.hasNext());
100        Assert.assertEquals(iterator.next(), i4);
101        n.i2 = i1;
102        Assert.assertTrue(iterator.hasNext());
103        Assert.assertEquals(iterator.next(), i1);
104        Assert.assertTrue(iterator.hasNext());
105        Assert.assertEquals(iterator.next(), i3);
106        n.itail.initialize(1, i4);
107        Assert.assertTrue(iterator.hasNext());
108        Assert.assertEquals(iterator.next(), i4);
109        Assert.assertFalse(iterator.hasNext());
110
111        iterator = inputs.iterator();
112        n.i1 = null;
113        n.i2 = i2;
114        n.itail.initialize(0, null);
115        n.itail.initialize(1, i4);
116        Assert.assertTrue(iterator.hasNext());
117        Assert.assertEquals(iterator.next(), i2);
118        Assert.assertTrue(iterator.hasNext());
119        Assert.assertEquals(iterator.next(), i4);
120        Assert.assertFalse(iterator.hasNext());
121
122        iterator = inputs.withNullIterator();
123        n.i1 = null;
124        n.i2 = null;
125        n.itail.initialize(0, i3);
126        n.itail.initialize(1, null);
127        Assert.assertTrue(iterator.hasNext());
128        Assert.assertNull(iterator.next());
129        Assert.assertTrue(iterator.hasNext());
130        Assert.assertNull(iterator.next());
131        Assert.assertTrue(iterator.hasNext());
132        Assert.assertEquals(iterator.next(), i3);
133        Assert.assertTrue(iterator.hasNext());
134        Assert.assertNull(iterator.next());
135        Assert.assertFalse(iterator.hasNext());
136    }
137
138    @Test
139    public void testSuccessors() {
140        TestNode n = new TestNode();
141        EndNode s1 = new EndNode();
142        EndNode s2 = new EndNode();
143        EndNode s3 = new EndNode();
144        EndNode s4 = new EndNode();
145        n.s1 = s1;
146        n.s2 = s2;
147        n.stail = new NodeSuccessorList<>(n, new Node[]{s3, s4});
148
149        NodeClassIterable successors = n.successors();
150        NodePosIterator iterator = successors.iterator();
151        Assert.assertTrue(iterator.hasNext());
152        Assert.assertTrue(iterator.hasNext());
153        Assert.assertEquals(iterator.next(), s1);
154        Assert.assertTrue(iterator.hasNext());
155        Assert.assertTrue(iterator.hasNext());
156        Assert.assertEquals(iterator.next(), s2);
157        Assert.assertTrue(iterator.hasNext());
158        Assert.assertTrue(iterator.hasNext());
159        Assert.assertEquals(iterator.next(), s3);
160        Assert.assertTrue(iterator.hasNext());
161        Assert.assertTrue(iterator.hasNext());
162        Assert.assertEquals(iterator.next(), s4);
163        Assert.assertFalse(iterator.hasNext());
164        Assert.assertFalse(iterator.hasNext());
165
166        iterator = successors.iterator();
167        Assert.assertTrue(iterator.hasNext());
168        Assert.assertTrue(iterator.hasNext());
169        Assert.assertEquals(Node.class.getSimpleName() + ":s1", iterator.nextPosition().toString());
170        Assert.assertTrue(iterator.hasNext());
171        Assert.assertTrue(iterator.hasNext());
172        Assert.assertEquals(Node.class.getSimpleName() + ":s2", iterator.nextPosition().toString());
173        Assert.assertTrue(iterator.hasNext());
174        Assert.assertTrue(iterator.hasNext());
175        Assert.assertEquals(NodeSuccessorList.class.getSimpleName() + ":stail[0]", iterator.nextPosition().toString());
176        Assert.assertTrue(iterator.hasNext());
177        Assert.assertTrue(iterator.hasNext());
178        Assert.assertEquals(NodeSuccessorList.class.getSimpleName() + ":stail[1]", iterator.nextPosition().toString());
179        Assert.assertFalse(iterator.hasNext());
180        Assert.assertFalse(iterator.hasNext());
181
182        iterator = successors.iterator();
183        n.s1 = s4;
184        Assert.assertTrue(iterator.hasNext());
185        Assert.assertEquals(iterator.next(), s4);
186        n.s2 = s1;
187        Assert.assertTrue(iterator.hasNext());
188        Assert.assertEquals(iterator.next(), s1);
189        Assert.assertTrue(iterator.hasNext());
190        Assert.assertEquals(iterator.next(), s3);
191        n.stail.initialize(1, s4);
192        Assert.assertTrue(iterator.hasNext());
193        Assert.assertEquals(iterator.next(), s4);
194        Assert.assertFalse(iterator.hasNext());
195
196        iterator = successors.iterator();
197        n.s1 = null;
198        n.s2 = s2;
199        n.stail.initialize(0, null);
200        n.stail.initialize(1, s4);
201        Assert.assertTrue(iterator.hasNext());
202        Assert.assertEquals(iterator.next(), s2);
203        Assert.assertTrue(iterator.hasNext());
204        Assert.assertEquals(iterator.next(), s4);
205        Assert.assertFalse(iterator.hasNext());
206
207        iterator = successors.withNullIterator();
208        n.s1 = null;
209        n.s2 = null;
210        n.stail.initialize(0, s3);
211        n.stail.initialize(1, null);
212        Assert.assertTrue(iterator.hasNext());
213        Assert.assertNull(iterator.next());
214        Assert.assertTrue(iterator.hasNext());
215        Assert.assertNull(iterator.next());
216        Assert.assertTrue(iterator.hasNext());
217        Assert.assertEquals(iterator.next(), s3);
218        Assert.assertTrue(iterator.hasNext());
219        Assert.assertNull(iterator.next());
220        Assert.assertFalse(iterator.hasNext());
221    }
222}