comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/NodeUtilTest.java @ 17399:5787218bad91

Truffle: implemented recursive node iterator and node streams for the graal runtime.
author Christian Humer <christian.humer@gmail.com>
date Thu, 09 Oct 2014 17:25:18 +0200
parents
children e3c95cbbb50c
comparison
equal deleted inserted replaced
17398:9e1ec84d2899 17399:5787218bad91
1 /*
2 * Copyright (c) 2012, 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 com.oracle.truffle.api.test.nodes;
24
25 import java.util.*;
26
27 import org.junit.*;
28 import static org.junit.Assert.*;
29 import static org.hamcrest.CoreMatchers.*;
30
31 import com.oracle.truffle.api.frame.*;
32 import com.oracle.truffle.api.nodes.*;
33
34 public class NodeUtilTest {
35
36 @Test
37 public void testRecursiveIterator1() {
38 TestRootNode root = new TestRootNode();
39 root.child0 = new TestNode();
40 root.adoptChildren();
41
42 int count = iterate(NodeUtil.makeRecursiveIterator(root));
43
44 assertThat(count, is(2));
45 assertThat(root.visited, is(0));
46 assertThat(root.child0.visited, is(1));
47 }
48
49 private static int iterate(Iterator<Node> iterator) {
50 int iterationCount = 0;
51 while (iterator.hasNext()) {
52 Node node = iterator.next();
53 if (node == null) {
54 continue;
55 }
56 if (node instanceof TestNode) {
57 ((TestNode) node).visited = iterationCount;
58 } else if (node instanceof TestRootNode) {
59 ((TestRootNode) node).visited = iterationCount;
60 } else {
61 throw new AssertionError();
62 }
63 iterationCount++;
64 }
65 return iterationCount;
66 }
67
68 private static class TestNode extends Node {
69
70 @Child TestNode child0;
71 @Child TestNode child1;
72
73 private int visited;
74
75 public TestNode() {
76 }
77
78 }
79
80 private static class TestRootNode extends RootNode {
81
82 @Child TestNode child0;
83
84 private int visited;
85
86 @Override
87 public Object execute(VirtualFrame frame) {
88 return null;
89 }
90
91 }
92
93 }