2 Java 4 Browser Bytecode Translator
3 Copyright (C) 2012-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;
20 import java.io.IOException;
21 import javax.script.Invocable;
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptEngineManager;
24 import javax.script.ScriptException;
25 import static org.testng.Assert.*;
26 import org.testng.annotations.Test;
28 /** Checks the basic behavior of the translator.
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 public class StaticMethodTest {
33 @Test public void threePlusFour() throws Exception {
36 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
42 @Test public void powerOfThree() throws Exception {
45 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
51 @Test public void minusOne() throws Exception {
53 "Should be minus one",
54 "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
59 @Test public void doubleWithoutLong() throws Exception {
62 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
68 @Test public void divAndRound() throws Exception {
70 "Should be rounded to one",
71 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
76 @Test public void mixedMethodFourParams() throws Exception {
79 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
84 @Test public void factRec() throws Exception {
86 "Factorial of 5 is 120",
87 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
92 @Test public void factIter() throws Exception {
94 "Factorial of 5 is 120",
95 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
101 @Test public void xor() throws Exception {
104 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
111 @Test public void or() throws Exception {
114 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
121 @Test public void nullCheck() throws Exception {
124 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
128 @Test public void and() throws Exception {
131 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
138 @Test public void inc4() throws Exception {
141 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
146 @Test public void shiftLeftInJava() throws Exception {
147 int res = StaticMethod.shiftLeft(1, 8);
148 assertEquals(res, 256);
151 @Test public void shiftLeftInJS() throws Exception {
154 "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
160 @Test public void shiftRightInJava() throws Exception {
161 int res = StaticMethod.shiftArithmRight(-8, 3, true);
162 assertEquals(res, -1);
165 @Test public void shiftRightInJS() throws Exception {
168 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
173 @Test public void unsignedShiftRightInJava() throws Exception {
174 int res = StaticMethod.shiftArithmRight(8, 3, false);
175 assertEquals(res, 1);
178 @Test public void unsignedShiftRightInJS() throws Exception {
181 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
187 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
188 StringBuilder sb = new StringBuilder();
189 Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
193 ret = i.invokeFunction(methodName, args);
194 } catch (ScriptException ex) {
195 fail("Execution failed in " + sb, ex);
196 } catch (NoSuchMethodException ex) {
197 fail("Cannot find method in " + sb, ex);
199 if (ret == null && expRes == null) {
202 if (expRes != null && expRes.equals(ret)) {
205 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
209 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
211 sb = new StringBuilder();
213 GenJS.compile(sb, names);
214 ScriptEngineManager sem = new ScriptEngineManager();
215 ScriptEngine js = sem.getEngineByExtension("js");
217 Object res = js.eval(sb.toString());
218 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
219 return (Invocable)js;
220 } catch (ScriptException ex) {
221 fail("Could not compile:\n" + sb, ex);