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 checkReallyInitializedValues() throws Exception {
48 "org_apidesign_vm4brwsr_StaticMethod_isNullZ",
53 @Test public void powerOfThree() throws Exception {
56 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
62 @Test public void minusOne() throws Exception {
64 "Should be minus one",
65 "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
70 @Test public void doubleWithoutLong() throws Exception {
73 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
79 @Test public void divAndRound() throws Exception {
81 "Should be rounded to one",
82 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
87 @Test public void mixedMethodFourParams() throws Exception {
90 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
95 @Test public void factRec() throws Exception {
97 "Factorial of 5 is 120",
98 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
103 @Test public void factIter() throws Exception {
105 "Factorial of 5 is 120",
106 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
112 @Test public void xor() throws Exception {
115 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
122 @Test public void or() throws Exception {
125 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
132 @Test public void nullCheck() throws Exception {
135 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
139 @Test public void and() throws Exception {
142 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
149 @Test public void inc4() throws Exception {
152 "org_apidesign_vm4brwsr_StaticMethod_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 "org_apidesign_vm4brwsr_StaticMethod_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 "org_apidesign_vm4brwsr_StaticMethod_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 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
198 @Test public void javaScriptBody() throws Exception {
201 "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
207 @Test public void switchJarda() throws Exception {
209 "The expected value",
210 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
216 @Test public void switchDarda() throws Exception {
218 "The expected value",
219 "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
224 @Test public void switchParda() throws Exception {
226 "The expected value",
227 "org_apidesign_vm4brwsr_StaticMethod_swtch2Ljava_lang_StringI",
232 @Test public void switchMarda() throws Exception {
234 "The expected value",
235 "org_apidesign_vm4brwsr_StaticMethod_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(String msg, String methodName, Object expRes, Object... args) throws Exception {
253 StringBuilder sb = new StringBuilder();
257 ret = code.invokeFunction(methodName, args);
258 } catch (ScriptException ex) {
259 fail("Execution failed in\n" + codeSeq, ex);
260 } catch (NoSuchMethodException ex) {
261 fail("Cannot find method in\n" + codeSeq, ex);
263 if (ret == null && expRes == null) {
266 if (expRes != null && expRes.equals(ret)) {
269 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
273 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
274 return compileClass(sb, null, names);
276 static Invocable compileClass(
277 StringBuilder sb, ScriptEngine[] eng, String... names
278 ) throws ScriptException, IOException {
280 sb = new StringBuilder();
282 GenJS.compile(sb, names);
283 ScriptEngineManager sem = new ScriptEngineManager();
284 ScriptEngine js = sem.getEngineByExtension("js");
289 Object res = js.eval(sb.toString());
290 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
291 return (Invocable)js;
292 } catch (Exception ex) {
293 if (sb.length() > 2000) {
296 fail("Could not compile:\n" + sb, ex);
300 static StringBuilder dumpJS(CharSequence sb) throws IOException {
301 File f = File.createTempFile("execution", ".js");
302 FileWriter w = new FileWriter(f);
305 return new StringBuilder(f.getPath());