comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/NodeFieldTest.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.dsl.test/src/com/oracle/truffle/api/dsl/test/NodeFieldTest.java@476374f3fe9a
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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.IntFieldNoGetterTestNodeFactory;
32 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.IntFieldTestNodeFactory;
33 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.MultipleFieldsTestNodeFactory;
34 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.ObjectContainerNodeFactory;
35 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.RewriteTestNodeFactory;
36 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.StringFieldTestNodeFactory;
37 import com.oracle.truffle.api.dsl.test.NodeFieldTestFactory.TestContainerFactory;
38 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
39
40 public class NodeFieldTest {
41
42 @Test
43 public void testIntField() {
44 assertEquals(42, createCallTarget(IntFieldTestNodeFactory.create(42)).call());
45 }
46
47 @NodeField(name = "field", type = int.class)
48 abstract static class IntFieldTestNode extends ValueNode {
49
50 public abstract int getField();
51
52 @Specialization
53 int intField() {
54 return getField();
55 }
56
57 }
58
59 @Test
60 public void testIntFieldNoGetter() {
61 assertEquals(42, createCallTarget(IntFieldNoGetterTestNodeFactory.create(42)).call());
62 }
63
64 @NodeField(name = "field", type = int.class)
65 abstract static class IntFieldNoGetterTestNode extends ValueNode {
66
67 @Specialization
68 int intField(int field) {
69 return field;
70 }
71
72 }
73
74 @Test
75 public void testMultipleFields() {
76 assertEquals(42, createCallTarget(MultipleFieldsTestNodeFactory.create(21, 21)).call());
77 }
78
79 @NodeFields({@NodeField(name = "field0", type = int.class), @NodeField(name = "field1", type = int.class)})
80 abstract static class MultipleFieldsTestNode extends ValueNode {
81
82 public abstract int getField0();
83
84 public abstract int getField1();
85
86 @Specialization
87 int intField() {
88 return getField0() + getField1();
89 }
90
91 }
92
93 @Test
94 public void testStringField() {
95 assertEquals("42", createCallTarget(StringFieldTestNodeFactory.create("42")).call());
96 }
97
98 @NodeField(name = "field", type = String.class)
99 abstract static class StringFieldTestNode extends ValueNode {
100
101 public abstract String getField();
102
103 @Specialization
104 String stringField() {
105 return getField();
106 }
107
108 }
109
110 @Test
111 public void testRewrite() {
112 assertEquals("42", createCallTarget(RewriteTestNodeFactory.create("42")).call());
113 }
114
115 @NodeField(name = "field", type = String.class)
116 abstract static class RewriteTestNode extends ValueNode {
117
118 public abstract String getField();
119
120 @Specialization(rewriteOn = RuntimeException.class)
121 String alwaysRewrite() {
122 throw new RuntimeException();
123 }
124
125 @Specialization(contains = "alwaysRewrite")
126 Object returnField() {
127 return getField();
128 }
129 }
130
131 @Test
132 public void testStringContainer() {
133 assertEquals(42, createCallTarget(TestContainerFactory.create("42")).call());
134 }
135
136 @NodeField(name = "field", type = int.class)
137 abstract static class IntContainerNode extends ValueNode {
138
139 public abstract int getField();
140
141 }
142
143 @NodeField(name = "anotherField", type = String.class)
144 abstract static class TestContainer extends ValueNode {
145
146 @Specialization
147 int containerField(String field) {
148 return field.equals("42") ? 42 : -1;
149 }
150
151 }
152
153 @Test
154 public void testObjectContainer() {
155 assertEquals("42", createCallTarget(ObjectContainerNodeFactory.create("42")).call());
156 }
157
158 @NodeField(name = "object", type = Object.class)
159 abstract static class ObjectContainerNode extends ValueNode {
160
161 public abstract Object getObject();
162
163 @Specialization
164 Object containerField() {
165 return getObject();
166 }
167
168 }
169
170 }