comparison graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/BuiltinTest.java @ 8596:6ef9fc7375c7

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