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 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
45 @Test public void powerOfThree() throws Exception {
48 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
54 @Test public void minusOne() throws Exception {
56 "Should be minus one",
57 "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
62 @Test public void doubleWithoutLong() throws Exception {
65 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
71 @Test public void divAndRound() throws Exception {
73 "Should be rounded to one",
74 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
79 @Test public void mixedMethodFourParams() throws Exception {
82 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
87 @Test public void factRec() throws Exception {
89 "Factorial of 5 is 120",
90 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
95 @Test public void factIter() throws Exception {
97 "Factorial of 5 is 120",
98 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
104 @Test public void xor() throws Exception {
107 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
114 @Test public void or() throws Exception {
117 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
124 @Test public void nullCheck() throws Exception {
127 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
131 @Test public void and() throws Exception {
134 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
141 @Test public void inc4() throws Exception {
144 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
149 @Test public void shiftLeftInJava() throws Exception {
150 int res = StaticMethod.shiftLeft(1, 8);
151 assertEquals(res, 256);
154 @Test public void shiftLeftInJS() throws Exception {
157 "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
163 @Test public void shiftRightInJava() throws Exception {
164 int res = StaticMethod.shiftArithmRight(-8, 3, true);
165 assertEquals(res, -1);
168 @Test public void shiftRightInJS() throws Exception {
171 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
176 @Test public void unsignedShiftRightInJava() throws Exception {
177 int res = StaticMethod.shiftArithmRight(8, 3, false);
178 assertEquals(res, 1);
181 @Test public void unsignedShiftRightInJS() throws Exception {
184 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
190 @Test public void javaScriptBody() throws Exception {
193 "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
199 @Test public void switchJarda() throws Exception {
201 "The expected value",
202 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
208 @Test public void switchDarda() throws Exception {
210 "The expected value",
211 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
216 @Test public void switchParda() throws Exception {
218 "The expected value",
219 "org_apidesign_vm4brwsr_StaticMethod_swtch2Ljava_lang_StringI",
224 @Test public void switchMarda() throws Exception {
226 "The expected value",
227 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
233 private static CharSequence codeSeq;
234 private static Invocable code;
237 public void compileTheCode() throws Exception {
238 StringBuilder sb = new StringBuilder();
239 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
244 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
245 StringBuilder sb = new StringBuilder();
249 ret = code.invokeFunction(methodName, args);
250 } catch (ScriptException ex) {
251 fail("Execution failed in\n" + codeSeq, ex);
252 } catch (NoSuchMethodException ex) {
253 fail("Cannot find method in\n" + codeSeq, ex);
255 if (ret == null && expRes == null) {
258 if (expRes != null && expRes.equals(ret)) {
261 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
265 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
267 sb = new StringBuilder();
269 GenJS.compile(sb, names);
270 ScriptEngineManager sem = new ScriptEngineManager();
271 ScriptEngine js = sem.getEngineByExtension("js");
273 Object res = js.eval(sb.toString());
274 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
275 return (Invocable)js;
276 } catch (Exception ex) {
277 if (sb.length() > 2000) {
280 fail("Could not compile:\n" + sb, ex);
284 static String dumpJS(CharSequence sb) throws IOException {
285 File f = File.createTempFile("execution", ".js");
286 FileWriter w = new FileWriter(f);