jaroslav@106
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@106
|
4 |
*
|
jaroslav@106
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@106
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@106
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@106
|
8 |
*
|
jaroslav@106
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@106
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@106
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@106
|
12 |
* GNU General Public License for more details.
|
jaroslav@106
|
13 |
*
|
jaroslav@106
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@106
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@106
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@106
|
17 |
*/
|
jaroslav@22
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@0
|
19 |
|
jaroslav@137
|
20 |
import java.io.File;
|
jaroslav@137
|
21 |
import java.io.FileWriter;
|
jaroslav@0
|
22 |
import java.io.IOException;
|
jaroslav@0
|
23 |
import javax.script.Invocable;
|
jaroslav@0
|
24 |
import javax.script.ScriptEngine;
|
jaroslav@0
|
25 |
import javax.script.ScriptEngineManager;
|
jaroslav@0
|
26 |
import javax.script.ScriptException;
|
jaroslav@0
|
27 |
import static org.testng.Assert.*;
|
jaroslav@103
|
28 |
import org.testng.annotations.BeforeClass;
|
jaroslav@0
|
29 |
import org.testng.annotations.Test;
|
jaroslav@0
|
30 |
|
jaroslav@0
|
31 |
/** Checks the basic behavior of the translator.
|
jaroslav@0
|
32 |
*
|
jaroslav@0
|
33 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@0
|
34 |
*/
|
jaroslav@0
|
35 |
public class StaticMethodTest {
|
jaroslav@0
|
36 |
@Test public void threePlusFour() throws Exception {
|
jaroslav@2
|
37 |
assertExec(
|
jaroslav@2
|
38 |
"Should be seven",
|
jaroslav@248
|
39 |
StaticMethod.class, "sum__III",
|
jaroslav@2
|
40 |
Double.valueOf(7),
|
jaroslav@2
|
41 |
3, 4
|
jaroslav@2
|
42 |
);
|
jaroslav@0
|
43 |
}
|
jaroslav@0
|
44 |
|
jaroslav@173
|
45 |
@Test public void checkReallyInitializedValues() throws Exception {
|
jaroslav@173
|
46 |
assertExec(
|
jaroslav@173
|
47 |
"Return true",
|
jaroslav@248
|
48 |
StaticMethod.class, "isNull__Z",
|
jaroslav@173
|
49 |
Double.valueOf(1)
|
jaroslav@173
|
50 |
);
|
jaroslav@173
|
51 |
}
|
jaroslav@173
|
52 |
|
jaroslav@1
|
53 |
@Test public void powerOfThree() throws Exception {
|
jaroslav@2
|
54 |
assertExec(
|
jaroslav@2
|
55 |
"Should be nine",
|
jaroslav@248
|
56 |
StaticMethod.class, "power__FF",
|
jaroslav@2
|
57 |
Double.valueOf(9),
|
jaroslav@2
|
58 |
3.0f
|
jaroslav@2
|
59 |
);
|
jaroslav@1
|
60 |
}
|
jaroslav@1
|
61 |
|
jaroslav@48
|
62 |
@Test public void minusOne() throws Exception {
|
jaroslav@48
|
63 |
assertExec(
|
jaroslav@48
|
64 |
"Should be minus one",
|
jaroslav@248
|
65 |
StaticMethod.class, "minusOne__I",
|
jaroslav@48
|
66 |
Double.valueOf(-1)
|
jaroslav@48
|
67 |
);
|
jaroslav@48
|
68 |
}
|
jaroslav@48
|
69 |
|
jaroslav@2
|
70 |
@Test public void doubleWithoutLong() throws Exception {
|
jaroslav@2
|
71 |
assertExec(
|
jaroslav@2
|
72 |
"Should be two",
|
jaroslav@248
|
73 |
StaticMethod.class, "minus__DDJ",
|
jaroslav@2
|
74 |
Double.valueOf(2),
|
jaroslav@2
|
75 |
3.0d, 1l
|
jaroslav@2
|
76 |
);
|
jaroslav@2
|
77 |
}
|
jaroslav@3
|
78 |
|
jaroslav@3
|
79 |
@Test public void divAndRound() throws Exception {
|
jaroslav@3
|
80 |
assertExec(
|
jaroslav@3
|
81 |
"Should be rounded to one",
|
jaroslav@248
|
82 |
StaticMethod.class, "div__IBD",
|
jaroslav@3
|
83 |
Double.valueOf(1),
|
jaroslav@3
|
84 |
3, 3.75
|
jaroslav@3
|
85 |
);
|
jaroslav@3
|
86 |
}
|
jaroslav@3
|
87 |
@Test public void mixedMethodFourParams() throws Exception {
|
jaroslav@3
|
88 |
assertExec(
|
jaroslav@3
|
89 |
"Should be two",
|
jaroslav@248
|
90 |
StaticMethod.class, "mix__IIJBD",
|
jaroslav@3
|
91 |
Double.valueOf(20),
|
jaroslav@3
|
92 |
2, 10l, 5, 2.0
|
jaroslav@3
|
93 |
);
|
jaroslav@3
|
94 |
}
|
jaroslav@4
|
95 |
@Test public void factRec() throws Exception {
|
jaroslav@4
|
96 |
assertExec(
|
jaroslav@4
|
97 |
"Factorial of 5 is 120",
|
jaroslav@248
|
98 |
StaticMethod.class, "factRec__JI",
|
jaroslav@4
|
99 |
Double.valueOf(120),
|
jaroslav@4
|
100 |
5
|
jaroslav@4
|
101 |
);
|
jaroslav@4
|
102 |
}
|
jaroslav@5
|
103 |
@Test public void factIter() throws Exception {
|
jaroslav@5
|
104 |
assertExec(
|
jaroslav@5
|
105 |
"Factorial of 5 is 120",
|
jaroslav@248
|
106 |
StaticMethod.class, "factIter__JI",
|
jaroslav@5
|
107 |
Double.valueOf(120),
|
jaroslav@5
|
108 |
5
|
jaroslav@5
|
109 |
);
|
jaroslav@5
|
110 |
}
|
jaroslav@2
|
111 |
|
jaroslav@6
|
112 |
@Test public void xor() throws Exception {
|
jaroslav@6
|
113 |
assertExec(
|
jaroslav@6
|
114 |
"Xor is 4",
|
jaroslav@248
|
115 |
StaticMethod.class, "xor__JIJ",
|
jaroslav@6
|
116 |
Double.valueOf(4),
|
jaroslav@6
|
117 |
7,
|
jaroslav@6
|
118 |
3
|
jaroslav@6
|
119 |
);
|
jaroslav@6
|
120 |
}
|
jaroslav@6
|
121 |
|
jaroslav@7
|
122 |
@Test public void or() throws Exception {
|
jaroslav@7
|
123 |
assertExec(
|
jaroslav@7
|
124 |
"Or will be 7",
|
jaroslav@248
|
125 |
StaticMethod.class, "orOrAnd__JZII",
|
jaroslav@7
|
126 |
Double.valueOf(7),
|
jaroslav@7
|
127 |
true,
|
jaroslav@7
|
128 |
4,
|
jaroslav@7
|
129 |
3
|
jaroslav@7
|
130 |
);
|
jaroslav@7
|
131 |
}
|
jaroslav@46
|
132 |
@Test public void nullCheck() throws Exception {
|
jaroslav@46
|
133 |
assertExec(
|
jaroslav@46
|
134 |
"Returns nothing",
|
jaroslav@248
|
135 |
StaticMethod.class, "none__Ljava_lang_Object_2II",
|
jaroslav@46
|
136 |
null, 1, 3
|
jaroslav@46
|
137 |
);
|
jaroslav@46
|
138 |
}
|
jaroslav@7
|
139 |
@Test public void and() throws Exception {
|
jaroslav@7
|
140 |
assertExec(
|
jaroslav@7
|
141 |
"And will be 3",
|
jaroslav@248
|
142 |
StaticMethod.class, "orOrAnd__JZII",
|
jaroslav@7
|
143 |
Double.valueOf(3),
|
jaroslav@7
|
144 |
false,
|
jaroslav@7
|
145 |
7,
|
jaroslav@7
|
146 |
3
|
jaroslav@7
|
147 |
);
|
jaroslav@7
|
148 |
}
|
jaroslav@9
|
149 |
@Test public void inc4() throws Exception {
|
jaroslav@9
|
150 |
assertExec(
|
jaroslav@9
|
151 |
"It will be 4",
|
jaroslav@248
|
152 |
StaticMethod.class, "inc4__I",
|
jaroslav@9
|
153 |
Double.valueOf(4)
|
jaroslav@9
|
154 |
);
|
jaroslav@9
|
155 |
}
|
jaroslav@7
|
156 |
|
jaroslav@93
|
157 |
@Test public void shiftLeftInJava() throws Exception {
|
jaroslav@93
|
158 |
int res = StaticMethod.shiftLeft(1, 8);
|
jaroslav@93
|
159 |
assertEquals(res, 256);
|
jaroslav@93
|
160 |
}
|
jaroslav@93
|
161 |
|
jaroslav@93
|
162 |
@Test public void shiftLeftInJS() throws Exception {
|
jaroslav@93
|
163 |
assertExec(
|
jaroslav@93
|
164 |
"Setting 9th bit",
|
jaroslav@248
|
165 |
StaticMethod.class, "shiftLeft__III",
|
jaroslav@93
|
166 |
Double.valueOf(256),
|
jaroslav@93
|
167 |
1, 8
|
jaroslav@93
|
168 |
);
|
jaroslav@93
|
169 |
}
|
jaroslav@93
|
170 |
|
jaroslav@93
|
171 |
@Test public void shiftRightInJava() throws Exception {
|
jaroslav@93
|
172 |
int res = StaticMethod.shiftArithmRight(-8, 3, true);
|
jaroslav@93
|
173 |
assertEquals(res, -1);
|
jaroslav@93
|
174 |
}
|
jaroslav@93
|
175 |
|
jaroslav@93
|
176 |
@Test public void shiftRightInJS() throws Exception {
|
jaroslav@93
|
177 |
assertExec(
|
jaroslav@93
|
178 |
"Get -1",
|
jaroslav@248
|
179 |
StaticMethod.class, "shiftArithmRight__IIIZ",
|
jaroslav@93
|
180 |
Double.valueOf(-1),
|
jaroslav@93
|
181 |
-8, 3, true
|
jaroslav@93
|
182 |
);
|
jaroslav@93
|
183 |
}
|
jaroslav@93
|
184 |
@Test public void unsignedShiftRightInJava() throws Exception {
|
jaroslav@93
|
185 |
int res = StaticMethod.shiftArithmRight(8, 3, false);
|
jaroslav@93
|
186 |
assertEquals(res, 1);
|
jaroslav@93
|
187 |
}
|
jaroslav@93
|
188 |
|
jaroslav@93
|
189 |
@Test public void unsignedShiftRightInJS() throws Exception {
|
jaroslav@93
|
190 |
assertExec(
|
jaroslav@93
|
191 |
"Get -1",
|
jaroslav@248
|
192 |
StaticMethod.class, "shiftArithmRight__IIIZ",
|
jaroslav@93
|
193 |
Double.valueOf(1),
|
jaroslav@93
|
194 |
8, 3, false
|
jaroslav@93
|
195 |
);
|
jaroslav@93
|
196 |
}
|
jaroslav@93
|
197 |
|
jaroslav@94
|
198 |
@Test public void javaScriptBody() throws Exception {
|
jaroslav@94
|
199 |
assertExec(
|
jaroslav@94
|
200 |
"JavaScript string",
|
jaroslav@248
|
201 |
StaticMethod.class, "i2s__Ljava_lang_String_2II",
|
jaroslav@94
|
202 |
"333",
|
jaroslav@99
|
203 |
330, 3
|
jaroslav@94
|
204 |
);
|
jaroslav@94
|
205 |
}
|
jaroslav@94
|
206 |
|
jaroslav@115
|
207 |
@Test public void switchJarda() throws Exception {
|
jaroslav@115
|
208 |
assertExec(
|
jaroslav@115
|
209 |
"The expected value",
|
jaroslav@248
|
210 |
StaticMethod.class, "swtch__Ljava_lang_String_2I",
|
jaroslav@115
|
211 |
"Jarda",
|
jaroslav@115
|
212 |
0
|
jaroslav@115
|
213 |
);
|
jaroslav@115
|
214 |
}
|
jaroslav@115
|
215 |
|
jaroslav@115
|
216 |
@Test public void switchDarda() throws Exception {
|
jaroslav@115
|
217 |
assertExec(
|
jaroslav@115
|
218 |
"The expected value",
|
jaroslav@248
|
219 |
StaticMethod.class, "swtch__Ljava_lang_String_2I",
|
jaroslav@115
|
220 |
"Darda",
|
jaroslav@115
|
221 |
1
|
jaroslav@115
|
222 |
);
|
jaroslav@115
|
223 |
}
|
jaroslav@115
|
224 |
@Test public void switchParda() throws Exception {
|
jaroslav@115
|
225 |
assertExec(
|
jaroslav@115
|
226 |
"The expected value",
|
jaroslav@248
|
227 |
StaticMethod.class, "swtch2__Ljava_lang_String_2I",
|
jaroslav@115
|
228 |
"Parda",
|
jaroslav@115
|
229 |
22
|
jaroslav@115
|
230 |
);
|
jaroslav@115
|
231 |
}
|
jaroslav@115
|
232 |
@Test public void switchMarda() throws Exception {
|
jaroslav@115
|
233 |
assertExec(
|
jaroslav@115
|
234 |
"The expected value",
|
jaroslav@248
|
235 |
StaticMethod.class, "swtch__Ljava_lang_String_2I",
|
jaroslav@115
|
236 |
"Marda",
|
jaroslav@115
|
237 |
-433
|
jaroslav@115
|
238 |
);
|
jaroslav@115
|
239 |
}
|
jaroslav@115
|
240 |
|
jaroslav@103
|
241 |
private static CharSequence codeSeq;
|
jaroslav@103
|
242 |
private static Invocable code;
|
jaroslav@103
|
243 |
|
jaroslav@103
|
244 |
@BeforeClass
|
jaroslav@103
|
245 |
public void compileTheCode() throws Exception {
|
jaroslav@103
|
246 |
StringBuilder sb = new StringBuilder();
|
jaroslav@103
|
247 |
code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
|
jaroslav@103
|
248 |
codeSeq = sb;
|
jaroslav@103
|
249 |
}
|
jaroslav@103
|
250 |
|
jaroslav@103
|
251 |
|
jaroslav@203
|
252 |
private static void assertExec(
|
jaroslav@203
|
253 |
String msg, Class clazz, String method,
|
jaroslav@203
|
254 |
Object expRes, Object... args
|
jaroslav@203
|
255 |
) throws Exception {
|
jaroslav@203
|
256 |
assertExec(code, codeSeq, msg, clazz, method, expRes, args);
|
jaroslav@203
|
257 |
}
|
jaroslav@203
|
258 |
static void assertExec(
|
jaroslav@203
|
259 |
Invocable toRun, CharSequence theCode,
|
jaroslav@203
|
260 |
String msg, Class clazz, String method,
|
jaroslav@203
|
261 |
Object expRes, Object... args
|
jaroslav@203
|
262 |
) throws Exception {
|
jaroslav@2
|
263 |
Object ret = null;
|
jaroslav@2
|
264 |
try {
|
jaroslav@274
|
265 |
ret = toRun.invokeFunction("bck2brwsr");
|
jaroslav@274
|
266 |
ret = toRun.invokeMethod(ret, "loadClass", clazz.getName().replace('.', '_'));
|
jaroslav@203
|
267 |
ret = toRun.invokeMethod(ret, method, args);
|
jaroslav@5
|
268 |
} catch (ScriptException ex) {
|
jaroslav@203
|
269 |
fail("Execution failed in\n" + dumpJS(theCode), ex);
|
jaroslav@2
|
270 |
} catch (NoSuchMethodException ex) {
|
jaroslav@203
|
271 |
fail("Cannot find method in\n" + dumpJS(theCode), ex);
|
jaroslav@2
|
272 |
}
|
jaroslav@2
|
273 |
if (ret == null && expRes == null) {
|
jaroslav@2
|
274 |
return;
|
jaroslav@2
|
275 |
}
|
jaroslav@46
|
276 |
if (expRes != null && expRes.equals(ret)) {
|
jaroslav@2
|
277 |
return;
|
jaroslav@2
|
278 |
}
|
jaroslav@203
|
279 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
|
jaroslav@2
|
280 |
|
jaroslav@2
|
281 |
}
|
jaroslav@2
|
282 |
|
jaroslav@13
|
283 |
static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
|
jaroslav@201
|
284 |
return compileClass(sb, null, names);
|
jaroslav@201
|
285 |
}
|
jaroslav@201
|
286 |
static Invocable compileClass(
|
jaroslav@201
|
287 |
StringBuilder sb, ScriptEngine[] eng, String... names
|
jaroslav@201
|
288 |
) throws ScriptException, IOException {
|
jaroslav@19
|
289 |
if (sb == null) {
|
jaroslav@19
|
290 |
sb = new StringBuilder();
|
jaroslav@19
|
291 |
}
|
jaroslav@29
|
292 |
GenJS.compile(sb, names);
|
jaroslav@0
|
293 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@0
|
294 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@201
|
295 |
if (eng != null) {
|
jaroslav@201
|
296 |
eng[0] = js;
|
jaroslav@201
|
297 |
}
|
jaroslav@0
|
298 |
try {
|
jaroslav@0
|
299 |
Object res = js.eval(sb.toString());
|
jaroslav@0
|
300 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@0
|
301 |
return (Invocable)js;
|
jaroslav@137
|
302 |
} catch (Exception ex) {
|
jaroslav@137
|
303 |
if (sb.length() > 2000) {
|
jtulach@190
|
304 |
sb = dumpJS(sb);
|
jaroslav@137
|
305 |
}
|
jaroslav@203
|
306 |
fail("Could not evaluate:\n" + sb, ex);
|
jaroslav@0
|
307 |
return null;
|
jaroslav@0
|
308 |
}
|
jaroslav@0
|
309 |
}
|
jtulach@190
|
310 |
static StringBuilder dumpJS(CharSequence sb) throws IOException {
|
jaroslav@172
|
311 |
File f = File.createTempFile("execution", ".js");
|
jaroslav@172
|
312 |
FileWriter w = new FileWriter(f);
|
jaroslav@172
|
313 |
w.append(sb);
|
jaroslav@172
|
314 |
w.close();
|
jtulach@190
|
315 |
return new StringBuilder(f.getPath());
|
jaroslav@172
|
316 |
}
|
jaroslav@0
|
317 |
}
|