comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InterfaceChildFieldTest.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InterfaceChildFieldTest.java@65a160d9d259
children 5bc7f7b867ab
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2014, 2014, 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;
24
25 import java.util.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.*;
30 import com.oracle.truffle.api.frame.*;
31 import com.oracle.truffle.api.nodes.*;
32
33 /**
34 * Test child fields declared with interface types instead of {@link Node} subclasses.
35 */
36 public class InterfaceChildFieldTest {
37
38 @Test
39 public void testChild() {
40 TruffleRuntime runtime = Truffle.getRuntime();
41 TestChildInterface leftChild = new TestLeafNode();
42 TestChildInterface rightChild = new TestLeafNode();
43 TestChildNode parent = new TestChildNode(leftChild, rightChild);
44 TestRootNode rootNode = new TestRootNode(parent);
45 CallTarget target = runtime.createCallTarget(rootNode);
46 Iterator<Node> iterator = parent.getChildren().iterator();
47 Assert.assertEquals(leftChild, iterator.next());
48 Assert.assertEquals(rightChild, iterator.next());
49 Assert.assertFalse(iterator.hasNext());
50 Object result = target.call();
51 Assert.assertEquals(42, result);
52
53 Assert.assertEquals(4, NodeUtil.countNodes(rootNode));
54 Assert.assertEquals(4, NodeUtil.countNodes(NodeUtil.cloneNode(rootNode)));
55 }
56
57 @Test
58 public void testChildren() {
59 TruffleRuntime runtime = Truffle.getRuntime();
60 TestChildInterface[] children = new TestChildInterface[5];
61 for (int i = 0; i < children.length; i++) {
62 children[i] = new TestLeafNode();
63 }
64 TestChildrenNode parent = new TestChildrenNode(children);
65 TestRootNode rootNode = new TestRootNode(parent);
66 CallTarget target = runtime.createCallTarget(rootNode);
67 Iterator<Node> iterator = parent.getChildren().iterator();
68 for (int i = 0; i < children.length; i++) {
69 Assert.assertEquals(children[i], iterator.next());
70 }
71 Assert.assertFalse(iterator.hasNext());
72 Object result = target.call();
73 Assert.assertEquals(105, result);
74
75 Assert.assertEquals(2 + children.length, NodeUtil.countNodes(rootNode));
76 Assert.assertEquals(2 + children.length, NodeUtil.countNodes(NodeUtil.cloneNode(rootNode)));
77 }
78
79 class TestRootNode extends RootNode {
80
81 @Child private TestChildInterface child;
82
83 public TestRootNode(TestChildInterface child) {
84 super(null);
85 this.child = child;
86 }
87
88 @Override
89 public Object execute(VirtualFrame frame) {
90 return child.executeIntf();
91 }
92 }
93
94 interface TestChildInterface extends NodeInterface {
95 int executeIntf();
96 }
97
98 class TestLeafNode extends Node implements TestChildInterface {
99 public TestLeafNode() {
100 super(null);
101 }
102
103 public int executeIntf() {
104 return this.replace(new TestLeaf2Node()).executeIntf();
105 }
106 }
107
108 class TestLeaf2Node extends Node implements TestChildInterface {
109 public TestLeaf2Node() {
110 super(null);
111 }
112
113 public int executeIntf() {
114 return 21;
115 }
116 }
117
118 class TestChildNode extends Node implements TestChildInterface {
119
120 @Child private TestChildInterface left;
121 @Child private TestChildInterface right;
122
123 public TestChildNode(TestChildInterface left, TestChildInterface right) {
124 super(null);
125 this.left = left;
126 this.right = right;
127 }
128
129 @Override
130 public int executeIntf() {
131 return left.executeIntf() + right.executeIntf();
132 }
133 }
134
135 class TestChildrenNode extends Node implements TestChildInterface {
136
137 @Children private final TestChildInterface[] children;
138
139 public TestChildrenNode(TestChildInterface[] children) {
140 super(null);
141 this.children = children;
142 }
143
144 @Override
145 public int executeIntf() {
146 int sum = 0;
147 for (int i = 0; i < children.length; ++i) {
148 sum += children[i].executeIntf();
149 }
150 return sum;
151 }
152 }
153 }