2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import javax.script.Invocable;
24 import javax.script.ScriptEngine;
25 import javax.script.ScriptEngineManager;
26 import javax.script.ScriptException;
27 import static org.testng.Assert.*;
28 import org.testng.annotations.BeforeClass;
29 import org.testng.annotations.Test;
31 /** Checks the basic behavior of the translator.
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public class StaticMethodTest {
36 @Test public void threePlusFour() throws Exception {
39 StaticMethod.class, "sumIII",
45 @Test public void checkReallyInitializedValues() throws Exception {
48 StaticMethod.class, "isNullZ",
53 @Test public void powerOfThree() throws Exception {
56 StaticMethod.class, "powerFF",
62 @Test public void minusOne() throws Exception {
64 "Should be minus one",
65 StaticMethod.class, "minusOneI",
70 @Test public void doubleWithoutLong() throws Exception {
73 StaticMethod.class, "minusDDJ",
79 @Test public void divAndRound() throws Exception {
81 "Should be rounded to one",
82 StaticMethod.class, "divIBD",
87 @Test public void mixedMethodFourParams() throws Exception {
90 StaticMethod.class, "mixIIJBD",
95 @Test public void factRec() throws Exception {
97 "Factorial of 5 is 120",
98 StaticMethod.class, "factRecJI",
103 @Test public void factIter() throws Exception {
105 "Factorial of 5 is 120",
106 StaticMethod.class, "factIterJI",
112 @Test public void xor() throws Exception {
115 StaticMethod.class, "xorJIJ",
122 @Test public void or() throws Exception {
125 StaticMethod.class, "orOrAndJZII",
132 @Test public void nullCheck() throws Exception {
135 StaticMethod.class, "noneLjava_lang_ObjectII",
139 @Test public void and() throws Exception {
142 StaticMethod.class, "orOrAndJZII",
149 @Test public void inc4() throws Exception {
152 StaticMethod.class, "inc4I",
157 @Test public void shiftLeftInJava() throws Exception {
158 int res = StaticMethod.shiftLeft(1, 8);
159 assertEquals(res, 256);
162 @Test public void shiftLeftInJS() throws Exception {
165 StaticMethod.class, "shiftLeftIII",
171 @Test public void shiftRightInJava() throws Exception {
172 int res = StaticMethod.shiftArithmRight(-8, 3, true);
173 assertEquals(res, -1);
176 @Test public void shiftRightInJS() throws Exception {
179 StaticMethod.class, "shiftArithmRightIIIZ",
184 @Test public void unsignedShiftRightInJava() throws Exception {
185 int res = StaticMethod.shiftArithmRight(8, 3, false);
186 assertEquals(res, 1);
189 @Test public void unsignedShiftRightInJS() throws Exception {
192 StaticMethod.class, "shiftArithmRightIIIZ",
198 @Test public void javaScriptBody() throws Exception {
201 StaticMethod.class, "i2sLjava_lang_StringII",
207 @Test public void switchJarda() throws Exception {
209 "The expected value",
210 StaticMethod.class, "swtchLjava_lang_StringI",
216 @Test public void switchDarda() throws Exception {
218 "The expected value",
219 StaticMethod.class, "swtchLjava_lang_StringI",
224 @Test public void switchParda() throws Exception {
226 "The expected value",
227 StaticMethod.class, "swtch2Ljava_lang_StringI",
232 @Test public void switchMarda() throws Exception {
234 "The expected value",
235 StaticMethod.class, "swtchLjava_lang_StringI",
241 private static CharSequence codeSeq;
242 private static Invocable code;
245 public void compileTheCode() throws Exception {
246 StringBuilder sb = new StringBuilder();
247 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
252 private static void assertExec(
253 String msg, Class clazz, String method,
254 Object expRes, Object... args
256 assertExec(code, codeSeq, msg, clazz, method, expRes, args);
258 static void assertExec(
259 Invocable toRun, CharSequence theCode,
260 String msg, Class clazz, String method,
261 Object expRes, Object... args
265 ret = toRun.invokeFunction(clazz.getName().replace('.', '_') + "_proto");
266 ret = toRun.invokeMethod(ret, method, args);
267 } catch (ScriptException ex) {
268 fail("Execution failed in\n" + dumpJS(theCode), ex);
269 } catch (NoSuchMethodException ex) {
270 fail("Cannot find method in\n" + dumpJS(theCode), ex);
272 if (ret == null && expRes == null) {
275 if (expRes != null && expRes.equals(ret)) {
278 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
282 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
283 return compileClass(sb, null, names);
285 static Invocable compileClass(
286 StringBuilder sb, ScriptEngine[] eng, String... names
287 ) throws ScriptException, IOException {
289 sb = new StringBuilder();
291 GenJS.compile(sb, names);
292 ScriptEngineManager sem = new ScriptEngineManager();
293 ScriptEngine js = sem.getEngineByExtension("js");
298 Object res = js.eval(sb.toString());
299 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
300 return (Invocable)js;
301 } catch (Exception ex) {
302 if (sb.length() > 2000) {
305 fail("Could not evaluate:\n" + sb, ex);
309 static StringBuilder dumpJS(CharSequence sb) throws IOException {
310 File f = File.createTempFile("execution", ".js");
311 FileWriter w = new FileWriter(f);
314 return new StringBuilder(f.getPath());