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.
19 Java 4 Browser Bytecode Translator
20 Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
22 This program is free software: you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation, version 2 of the License.
26 This program is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program. Look for COPYING file in the top folder.
33 If not, see http://opensource.org/licenses/GPL-2.0.
35 package org.apidesign.vm4brwsr;
37 import java.io.IOException;
38 import javax.script.Invocable;
39 import javax.script.ScriptEngine;
40 import javax.script.ScriptEngineManager;
41 import javax.script.ScriptException;
42 import static org.testng.Assert.*;
43 import org.testng.annotations.BeforeClass;
44 import org.testng.annotations.Test;
46 /** Checks the basic behavior of the translator.
48 * @author Jaroslav Tulach <jtulach@netbeans.org>
50 public class StaticMethodTest {
51 @Test public void threePlusFour() throws Exception {
54 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
60 @Test public void powerOfThree() throws Exception {
63 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
69 @Test public void minusOne() throws Exception {
71 "Should be minus one",
72 "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
77 @Test public void doubleWithoutLong() throws Exception {
80 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
86 @Test public void divAndRound() throws Exception {
88 "Should be rounded to one",
89 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
94 @Test public void mixedMethodFourParams() throws Exception {
97 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
102 @Test public void factRec() throws Exception {
104 "Factorial of 5 is 120",
105 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
110 @Test public void factIter() throws Exception {
112 "Factorial of 5 is 120",
113 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
119 @Test public void xor() throws Exception {
122 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
129 @Test public void or() throws Exception {
132 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
139 @Test public void nullCheck() throws Exception {
142 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
146 @Test public void and() throws Exception {
149 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
156 @Test public void inc4() throws Exception {
159 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
164 @Test public void shiftLeftInJava() throws Exception {
165 int res = StaticMethod.shiftLeft(1, 8);
166 assertEquals(res, 256);
169 @Test public void shiftLeftInJS() throws Exception {
172 "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
178 @Test public void shiftRightInJava() throws Exception {
179 int res = StaticMethod.shiftArithmRight(-8, 3, true);
180 assertEquals(res, -1);
183 @Test public void shiftRightInJS() throws Exception {
186 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
191 @Test public void unsignedShiftRightInJava() throws Exception {
192 int res = StaticMethod.shiftArithmRight(8, 3, false);
193 assertEquals(res, 1);
196 @Test public void unsignedShiftRightInJS() throws Exception {
199 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
205 @Test public void javaScriptBody() throws Exception {
208 "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
214 private static CharSequence codeSeq;
215 private static Invocable code;
218 public void compileTheCode() throws Exception {
219 StringBuilder sb = new StringBuilder();
220 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
225 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
226 StringBuilder sb = new StringBuilder();
230 ret = code.invokeFunction(methodName, args);
231 } catch (ScriptException ex) {
232 fail("Execution failed in\n" + codeSeq, ex);
233 } catch (NoSuchMethodException ex) {
234 fail("Cannot find method in\n" + codeSeq, ex);
236 if (ret == null && expRes == null) {
239 if (expRes != null && expRes.equals(ret)) {
242 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
246 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
248 sb = new StringBuilder();
250 GenJS.compile(sb, names);
251 ScriptEngineManager sem = new ScriptEngineManager();
252 ScriptEngine js = sem.getEngineByExtension("js");
254 Object res = js.eval(sb.toString());
255 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
256 return (Invocable)js;
257 } catch (ScriptException ex) {
258 fail("Could not compile:\n" + sb, ex);