comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/NodeFieldTest.java @ 11187:7a8835ec5e7d

Truffle-DSL: Added new @NodeField and @NodeFields annotation. Which can be used to avoid the cumbersome definition of copy constructors.
author Christian Humer <christian.humer@gmail.com>
date Tue, 30 Jul 2013 17:42:50 +0200
parents
children dcaf879d4a7e
comparison
equal deleted inserted replaced
11186:4a9936bb03a4 11187:7a8835ec5e7d
1 /*
2 * Copyright (c) 2012, 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.dsl.test;
24
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*;
26 import static org.junit.Assert.*;
27
28 import org.junit.*;
29
30 import com.oracle.truffle.api.dsl.*;
31 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.*;
32 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.TestContainerFactory.TestContainerContainerFieldFactory;
33 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
34
35 public class NodeFieldTest {
36
37 @Test
38 public void testIntField() {
39 assertEquals(42, createCallTarget(IntFieldTestNodeFactory.create(42)).call());
40 }
41
42 @NodeField(name = "field", type = int.class)
43 abstract static class IntFieldTestNode extends ValueNode {
44
45 public abstract int getField();
46
47 @Specialization
48 int intField() {
49 return getField();
50 }
51
52 }
53
54 @Test
55 public void testIntFieldNoGetter() {
56 assertEquals(42, createCallTarget(IntFieldNoGetterTestNodeFactory.create(42)).call());
57 }
58
59 @NodeField(name = "field", type = int.class)
60 abstract static class IntFieldNoGetterTestNode extends ValueNode {
61
62 @Specialization
63 int intField(int field) {
64 return field;
65 }
66
67 }
68
69 @Test
70 public void testMultipleFields() {
71 assertEquals(42, createCallTarget(MultipleFieldsTestNodeFactory.create(21, 21)).call());
72 }
73
74 @NodeFields({@NodeField(name = "field0", type = int.class), @NodeField(name = "field1", type = int.class)})
75 abstract static class MultipleFieldsTestNode extends ValueNode {
76
77 public abstract int getField0();
78
79 public abstract int getField1();
80
81 @Specialization
82 int intField() {
83 return getField0() + getField1();
84 }
85
86 }
87
88 @Test
89 public void testStringField() {
90 assertEquals("42", createCallTarget(StringFieldTestNodeFactory.create("42")).call());
91 }
92
93 @NodeField(name = "field", type = String.class)
94 abstract static class StringFieldTestNode extends ValueNode {
95
96 public abstract String getField();
97
98 @Specialization
99 String stringField() {
100 return getField();
101 }
102
103 }
104
105 @Test
106 public void testRewrite() {
107 assertEquals("42", createCallTarget(RewriteTestNodeFactory.create("42")).call());
108 }
109
110 @NodeField(name = "field", type = String.class)
111 abstract static class RewriteTestNode extends ValueNode {
112
113 public abstract String getField();
114
115 @Specialization(order = 1, rewriteOn = RuntimeException.class)
116 String alwaysRewrite() {
117 throw new RuntimeException();
118 }
119
120 @Specialization(order = 2)
121 String returnField() {
122 return getField();
123 }
124 }
125
126 @Test
127 public void testStringContainer() {
128 assertEquals(42, createCallTarget(TestContainerContainerFieldFactory.create(42, "42")).call());
129 }
130
131 @NodeField(name = "field", type = int.class)
132 abstract static class IntContainerNode extends ValueNode {
133
134 public abstract int getField();
135
136 }
137
138 @NodeContainer(IntContainerNode.class)
139 @NodeField(name = "anotherField", type = String.class)
140 abstract static class TestContainer {
141
142 @Specialization
143 static int containerField(int field, String anotherField) {
144 return anotherField.equals("42") ? field : -1;
145 }
146
147 }
148
149 }