Fixed error in neg64 found bu Lubo.
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 java.io.InputStream;
25 import java.util.Enumeration;
26 import javax.script.Invocable;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29 import javax.script.ScriptException;
30 import static org.testng.Assert.*;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
34 /** Checks the basic behavior of the translator.
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 public class StaticMethodTest {
39 @Test public void threePlusFour() throws Exception {
42 StaticMethod.class, "sum__III",
48 @Test public void checkReallyInitializedValues() throws Exception {
51 StaticMethod.class, "isNull__Z",
56 @Test public void powerOfThree() throws Exception {
59 StaticMethod.class, "power__FF",
65 @Test public void minusOne() throws Exception {
67 "Should be minus one",
68 StaticMethod.class, "minusOne__I",
73 @Test public void doubleWithoutLong() throws Exception {
76 StaticMethod.class, "minus__DDJ",
82 @Test public void divAndRound() throws Exception {
84 "Should be rounded to one",
85 StaticMethod.class, "div__IBD",
90 @Test public void mixedMethodFourParams() throws Exception {
93 StaticMethod.class, "mix__IIJBD",
98 @Test public void factRec() throws Exception {
100 "Factorial of 5 is 120",
101 StaticMethod.class, "factRec__JI",
106 @Test public void factIter() throws Exception {
108 "Factorial of 5 is 120",
109 StaticMethod.class, "factIter__JI",
115 @Test public void xor() throws Exception {
118 StaticMethod.class, "xor__JIJ",
125 @Test public void or() throws Exception {
128 StaticMethod.class, "orOrAnd__JZII",
135 @Test public void nullCheck() throws Exception {
138 StaticMethod.class, "none__Ljava_lang_Object_2II",
142 @Test public void and() throws Exception {
145 StaticMethod.class, "orOrAnd__JZII",
152 @Test public void inc4() throws Exception {
155 StaticMethod.class, "inc4__I",
160 @Test public void shiftLeftInJava() throws Exception {
161 int res = StaticMethod.shiftLeft(1, 8);
162 assertEquals(res, 256);
165 @Test public void shiftLeftInJS() throws Exception {
168 StaticMethod.class, "shiftLeft__III",
174 @Test public void shiftRightInJava() throws Exception {
175 int res = StaticMethod.shiftArithmRight(-8, 3, true);
176 assertEquals(res, -1);
179 @Test public void shiftRightInJS() throws Exception {
182 StaticMethod.class, "shiftArithmRight__IIIZ",
187 @Test public void unsignedShiftRightInJava() throws Exception {
188 int res = StaticMethod.shiftArithmRight(8, 3, false);
189 assertEquals(res, 1);
192 @Test public void unsignedShiftRightInJS() throws Exception {
195 StaticMethod.class, "shiftArithmRight__IIIZ",
201 @Test public void javaScriptBody() throws Exception {
204 StaticMethod.class, "i2s__Ljava_lang_String_2II",
210 @Test public void switchJarda() throws Exception {
212 "The expected value",
213 StaticMethod.class, "swtch__Ljava_lang_String_2I",
219 @Test public void switchDarda() throws Exception {
221 "The expected value",
222 StaticMethod.class, "swtch__Ljava_lang_String_2I",
227 @Test public void switchParda() throws Exception {
229 "The expected value",
230 StaticMethod.class, "swtch2__Ljava_lang_String_2I",
235 @Test public void switchMarda() throws Exception {
237 "The expected value",
238 StaticMethod.class, "swtch__Ljava_lang_String_2I",
244 @Test public void checkNullCast() throws Exception {
245 assertExec("Null can be cast to any type",
246 StaticMethod.class, "castNull__Ljava_lang_String_2Z",
251 private static CharSequence codeSeq;
252 private static Invocable code;
255 public void compileTheCode() throws Exception {
256 StringBuilder sb = new StringBuilder();
257 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
262 private static void assertExec(
263 String msg, Class clazz, String method,
264 Object expRes, Object... args
266 assertExec(code, codeSeq, msg, clazz, method, expRes, args);
268 static void assertExec(
269 Invocable toRun, CharSequence theCode,
270 String msg, Class clazz, String method,
271 Object expRes, Object... args
273 Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args);
277 if (expRes != null && expRes.equals(ret)) {
280 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
284 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
285 return compileClass(sb, null, names);
287 static Invocable compileClass(
288 StringBuilder sb, ScriptEngine[] eng, String... names
289 ) throws ScriptException, IOException {
291 sb = new StringBuilder();
293 Bck2Brwsr.generate(sb, new EmulationResources(), names);
294 ScriptEngineManager sem = new ScriptEngineManager();
295 ScriptEngine js = sem.getEngineByExtension("js");
300 Object res = js.eval(sb.toString());
301 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
302 return (Invocable)js;
303 } catch (Exception ex) {
304 if (sb.length() > 2000) {
307 fail("Could not evaluate:\n" + sb, ex);
311 static StringBuilder dumpJS(CharSequence sb) throws IOException {
312 File f = File.createTempFile("execution", ".js");
313 FileWriter w = new FileWriter(f);
316 return new StringBuilder(f.getPath());
318 private static class EmulationResources implements Bck2Brwsr.Resources {
320 public InputStream get(String name) throws IOException {
321 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
323 while (en.hasMoreElements()) {
324 u = en.nextElement();
327 throw new IOException("Can't find " + name);
329 if (u.toExternalForm().contains("rt.jar!")) {
330 throw new IOException("No emulation for " + u);
332 return u.openStream();