Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/RuntimeStringTest.java @ 8249:aad7e9f4f71c
A few additions to codegen tests.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 12 Mar 2013 11:38:24 +0100 |
parents | graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/RuntimeString.java@3862508afe2f |
children | cb70ed101b5f |
comparison
equal
deleted
inserted
replaced
8248:c4c3f50fa9c2 | 8249:aad7e9f4f71c |
---|---|
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 org.junit.*; | |
26 | |
27 import com.oracle.truffle.api.*; | |
28 import com.oracle.truffle.api.codegen.*; | |
29 import com.oracle.truffle.api.codegen.test.TypeSystemTest.TestRootNode; | |
30 import com.oracle.truffle.api.codegen.test.TypeSystemTest.ValueNode; | |
31 | |
32 public class RuntimeStringTest { | |
33 | |
34 @Test | |
35 public void testSubstr() { | |
36 assertExecute(new RuntimeString("es"), "substr", new RuntimeString("test"), 1, 3); | |
37 } | |
38 | |
39 @Test | |
40 public void testConcat() { | |
41 assertExecute(new RuntimeString("concatconcat"), "concat", new RuntimeString("concat"), new RuntimeString("concat")); | |
42 } | |
43 | |
44 @Test(expected = ArrayIndexOutOfBoundsException.class) | |
45 public void testConcatFail() { | |
46 assertExecute(new RuntimeString("concatconcat"), "concat", new RuntimeString("concat")); | |
47 } | |
48 | |
49 @Test | |
50 public void testFindMethodByMethodName() { | |
51 // TODO | |
52 } | |
53 | |
54 private static void assertExecute(Object expectedResult, String name, Object... argumentsArray) { | |
55 ArgNode[] args = new ArgNode[argumentsArray.length]; | |
56 for (int i = 0; i < args.length; i++) { | |
57 args[i] = new ArgNode(argumentsArray, i); | |
58 } | |
59 | |
60 BuiltinNode node = null; | |
61 for (NodeFactory<BuiltinNode> nodeFactory : RuntimeStringTestFactory.getFactories()) { | |
62 GeneratedBy generated = nodeFactory.getClass().getAnnotation(GeneratedBy.class); | |
63 Assert.assertNotNull(generated); | |
64 Assert.assertNotSame("", generated.methodName()); | |
65 if (generated.methodName().equals(name)) { | |
66 node = nodeFactory.createNode((Object) args); | |
67 break; | |
68 } | |
69 } | |
70 Assert.assertNotNull("Node not found", node); | |
71 CallTarget target = Truffle.getRuntime().createCallTarget(new TestRootNode(node)); | |
72 Assert.assertEquals(expectedResult, target.call()); | |
73 } | |
74 | |
75 static class ArgNode extends ValueNode { | |
76 | |
77 final Object[] arguments; | |
78 final int index; | |
79 | |
80 ArgNode(Object[] args, int index) { | |
81 this.arguments = args; | |
82 this.index = index; | |
83 } | |
84 | |
85 @Override | |
86 Object execute() { | |
87 return arguments[index]; | |
88 } | |
89 } | |
90 | |
91 abstract static class BuiltinNode extends ValueNode { | |
92 | |
93 @Children ArgNode[] parameters; | |
94 | |
95 BuiltinNode(ArgNode[] parameters) { | |
96 this.parameters = adoptChildren(parameters); | |
97 } | |
98 | |
99 BuiltinNode(BuiltinNode prev) { | |
100 this(prev.parameters); | |
101 } | |
102 | |
103 } | |
104 | |
105 @NodeClass(BuiltinNode.class) | |
106 static class RuntimeString { | |
107 | |
108 private final String internal; | |
109 | |
110 public RuntimeString(String internal) { | |
111 this.internal = internal; | |
112 } | |
113 | |
114 @Specialization | |
115 static RuntimeString concat(RuntimeString s1, RuntimeString s2) { | |
116 return new RuntimeString(s1.internal + s2.internal); | |
117 } | |
118 | |
119 @Specialization | |
120 RuntimeString substr(int beginIndex, int endIndex) { | |
121 return new RuntimeString(internal.substring(beginIndex, endIndex)); | |
122 } | |
123 | |
124 @Generic | |
125 RuntimeString substr(Object beginIndex, Object endIndex) { | |
126 return substr(convertInt(beginIndex), convertInt(endIndex)); | |
127 } | |
128 | |
129 static int convertInt(Object value) { | |
130 if (value instanceof Number) { | |
131 return ((Number) value).intValue(); | |
132 } else if (value instanceof String) { | |
133 return Integer.parseInt((String) value); | |
134 } | |
135 throw new RuntimeException("Invalid datatype"); | |
136 } | |
137 | |
138 @Override | |
139 public boolean equals(Object obj) { | |
140 if (obj instanceof RuntimeString) { | |
141 return internal.equals(((RuntimeString) obj).internal); | |
142 } | |
143 return super.equals(obj); | |
144 } | |
145 | |
146 @Override | |
147 public int hashCode() { | |
148 return internal.hashCode(); | |
149 } | |
150 | |
151 @Override | |
152 public String toString() { | |
153 return internal; | |
154 } | |
155 | |
156 } | |
157 | |
158 } |