Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/NodeContainerTest.java @ 10597:79041ab43660
Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 01 Jul 2013 20:58:32 +0200 |
parents | |
children | c7d9ff67beed |
comparison
equal
deleted
inserted
replaced
10596:f43eb2f1bbbc | 10597:79041ab43660 |
---|---|
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.NodeContainerTestFactory.StrFactory.StrAccessContextFactory; | |
32 import com.oracle.truffle.api.dsl.test.NodeContainerTestFactory.StrFactory.StrConcatFactory; | |
33 import com.oracle.truffle.api.dsl.test.NodeContainerTestFactory.StrFactory.StrLengthFactory; | |
34 import com.oracle.truffle.api.dsl.test.NodeContainerTestFactory.StrFactory.StrSubstrFactory; | |
35 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
36 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
37 | |
38 public class NodeContainerTest { | |
39 | |
40 @Test | |
41 public void testConcat() { | |
42 TestRootNode<BuiltinNode> node = createRoot(StrConcatFactory.getInstance(), new Context()); | |
43 Str str1 = new Str("42"); | |
44 Str str2 = new Str(" is the number."); | |
45 assertEquals(str1.concat(str2), executeWith(node, str1, str2)); | |
46 } | |
47 | |
48 @Test(expected = UnsupportedOperationException.class) | |
49 public void testConcatUnsupported() { | |
50 TestRootNode<BuiltinNode> node = createRoot(StrConcatFactory.getInstance(), new Context()); | |
51 executeWith(node, 42, new Str(" is the number.")); | |
52 } | |
53 | |
54 @Test | |
55 public void testSubstrSpecialized() { | |
56 TestRootNode<BuiltinNode> node = createRoot(StrSubstrFactory.getInstance(), new Context()); | |
57 Str str = new Str("test 42"); | |
58 | |
59 assertEquals(str.substr(5, 7), executeWith(node, str, 5, 7)); | |
60 } | |
61 | |
62 @Test | |
63 public void testSubstrGeneric() { | |
64 TestRootNode<BuiltinNode> node = createRoot(StrSubstrFactory.getInstance(), new Context()); | |
65 Str str = new Str("test 42"); | |
66 | |
67 assertEquals(Str.substr(str, "5", "7"), executeWith(node, str, "5", "7")); | |
68 } | |
69 | |
70 @Test(expected = UnsupportedOperationException.class) | |
71 public void testSubstrUnsupported() { | |
72 TestRootNode<BuiltinNode> node = createRoot(StrSubstrFactory.getInstance(), new Context()); | |
73 executeWith(node, new Object(), "5", "7"); | |
74 } | |
75 | |
76 @Test | |
77 public void testLength() { | |
78 TestRootNode<BuiltinNode> node = createRoot(StrLengthFactory.getInstance(), new Context()); | |
79 Str testStr = new Str("test 42"); | |
80 assertEquals(testStr.length(), executeWith(node, testStr)); | |
81 } | |
82 | |
83 @Test(expected = UnsupportedOperationException.class) | |
84 public void testLengthUnsupported() { | |
85 TestRootNode<BuiltinNode> node = createRoot(StrLengthFactory.getInstance(), new Context()); | |
86 executeWith(node, new Object()); | |
87 } | |
88 | |
89 @Test | |
90 public void testAccessContext() { | |
91 Context context = new Context(); | |
92 TestRootNode<BuiltinNode> node = createRoot(StrAccessContextFactory.getInstance(), context); | |
93 // accessible by node | |
94 assertSame(context, node.getNode().getContext()); | |
95 // accessible by execution | |
96 assertSame(context, executeWith(node)); | |
97 } | |
98 | |
99 @NodeContainer(BuiltinNode.class) | |
100 static class Str { | |
101 | |
102 private final String internal; | |
103 | |
104 public Str(String internal) { | |
105 this.internal = internal; | |
106 } | |
107 | |
108 @Specialization | |
109 Str concat(Str s1) { | |
110 return new Str(internal + s1.internal); | |
111 } | |
112 | |
113 @Specialization | |
114 Str substr(int beginIndex, int endIndex) { | |
115 return new Str(internal.substring(beginIndex, endIndex)); | |
116 } | |
117 | |
118 @Generic | |
119 static Str substr(Object thisValue, Object beginIndex, Object endIndex) { | |
120 if (!(thisValue instanceof Str)) { | |
121 throw new UnsupportedOperationException(); | |
122 } | |
123 return ((Str) thisValue).substr(convertInt(beginIndex), convertInt(endIndex)); | |
124 } | |
125 | |
126 @Specialization | |
127 int length() { | |
128 return internal.length(); | |
129 } | |
130 | |
131 @Specialization | |
132 static Object accessContext(Context context) { | |
133 return context; | |
134 } | |
135 | |
136 static int convertInt(Object value) { | |
137 if (value instanceof Number) { | |
138 return ((Number) value).intValue(); | |
139 } else if (value instanceof String) { | |
140 return Integer.parseInt((String) value); | |
141 } | |
142 throw new RuntimeException("Invalid datatype"); | |
143 } | |
144 | |
145 @Override | |
146 public boolean equals(Object obj) { | |
147 if (obj instanceof Str) { | |
148 return internal.equals(((Str) obj).internal); | |
149 } | |
150 return super.equals(obj); | |
151 } | |
152 | |
153 @Override | |
154 public String toString() { | |
155 return internal; | |
156 } | |
157 | |
158 @Override | |
159 public int hashCode() { | |
160 return internal.hashCode(); | |
161 } | |
162 } | |
163 | |
164 @NodeChild(value = "children", type = ValueNode[].class) | |
165 abstract static class BuiltinNode extends ValueNode { | |
166 | |
167 protected final Context context; | |
168 | |
169 public BuiltinNode(BuiltinNode node) { | |
170 this(node.context); | |
171 } | |
172 | |
173 public BuiltinNode(Context context) { | |
174 this.context = context; | |
175 } | |
176 | |
177 public Context getContext() { | |
178 return context; | |
179 } | |
180 } | |
181 | |
182 static class Context { | |
183 | |
184 } | |
185 | |
186 } |