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;
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.BeforeClass;
27 import org.testng.annotations.Test;
29 /** Checks the basic behavior of the translator.
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 public class StaticMethodTest {
34 @Test public void threePlusFour() throws Exception {
37 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
43 @Test public void powerOfThree() throws Exception {
46 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
52 @Test public void minusOne() throws Exception {
54 "Should be minus one",
55 "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
60 @Test public void doubleWithoutLong() throws Exception {
63 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
69 @Test public void divAndRound() throws Exception {
71 "Should be rounded to one",
72 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
77 @Test public void mixedMethodFourParams() throws Exception {
80 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
85 @Test public void factRec() throws Exception {
87 "Factorial of 5 is 120",
88 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
93 @Test public void factIter() throws Exception {
95 "Factorial of 5 is 120",
96 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
102 @Test public void xor() throws Exception {
105 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
112 @Test public void or() throws Exception {
115 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
122 @Test public void nullCheck() throws Exception {
125 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
129 @Test public void and() throws Exception {
132 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
139 @Test public void inc4() throws Exception {
142 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
147 @Test public void shiftLeftInJava() throws Exception {
148 int res = StaticMethod.shiftLeft(1, 8);
149 assertEquals(res, 256);
152 @Test public void shiftLeftInJS() throws Exception {
155 "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
161 @Test public void shiftRightInJava() throws Exception {
162 int res = StaticMethod.shiftArithmRight(-8, 3, true);
163 assertEquals(res, -1);
166 @Test public void shiftRightInJS() throws Exception {
169 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
174 @Test public void unsignedShiftRightInJava() throws Exception {
175 int res = StaticMethod.shiftArithmRight(8, 3, false);
176 assertEquals(res, 1);
179 @Test public void unsignedShiftRightInJS() throws Exception {
182 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
188 @Test public void javaScriptBody() throws Exception {
191 "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
197 @Test public void switchJarda() throws Exception {
199 "The expected value",
200 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
206 @Test public void switchDarda() throws Exception {
208 "The expected value",
209 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
214 @Test public void switchParda() throws Exception {
216 "The expected value",
217 "org_apidesign_vm4brwsr_StaticMethod_swtch2Ljava_lang_StringI",
222 @Test public void switchMarda() throws Exception {
224 "The expected value",
225 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
231 private static CharSequence codeSeq;
232 private static Invocable code;
235 public void compileTheCode() throws Exception {
236 StringBuilder sb = new StringBuilder();
237 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
242 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
243 StringBuilder sb = new StringBuilder();
247 ret = code.invokeFunction(methodName, args);
248 } catch (ScriptException ex) {
249 fail("Execution failed in\n" + codeSeq, ex);
250 } catch (NoSuchMethodException ex) {
251 fail("Cannot find method in\n" + codeSeq, ex);
253 if (ret == null && expRes == null) {
256 if (expRes != null && expRes.equals(ret)) {
259 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
263 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
265 sb = new StringBuilder();
267 GenJS.compile(sb, names);
268 ScriptEngineManager sem = new ScriptEngineManager();
269 ScriptEngine js = sem.getEngineByExtension("js");
271 Object res = js.eval(sb.toString());
272 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
273 return (Invocable)js;
274 } catch (ScriptException ex) {
275 fail("Could not compile:\n" + sb, ex);