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 rintNegativeUp() throws Exception {
83 final double cnts = -453904.634;
85 "Should round up to end with 5",
86 Math.class, "rint__DD",
91 @Test public void rintNegativeDown() throws Exception {
92 final double cnts = -453904.434;
94 "Should round up to end with 4",
95 Math.class, "rint__DD",
100 @Test public void rintPositiveUp() throws Exception {
101 final double cnts = 453904.634;
103 "Should round up to end with 5",
104 Math.class, "rint__DD",
108 @Test public void rintPositiveDown() throws Exception {
109 final double cnts = 453904.434;
111 "Should round up to end with 4",
112 Math.class, "rint__DD",
116 @Test public void rintOneHalf() throws Exception {
117 final double cnts = 1.5;
119 "Should round up to end with 2",
120 Math.class, "rint__DD",
124 @Test public void rintNegativeOneHalf() throws Exception {
125 final double cnts = -1.5;
127 "Should round up to end with 2",
128 Math.class, "rint__DD",
132 @Test public void rintTwoAndHalf() throws Exception {
133 final double cnts = 2.5;
135 "Should round up to end with 2",
136 Math.class, "rint__DD",
140 @Test public void rintNegativeTwoOneHalf() throws Exception {
141 final double cnts = -2.5;
143 "Should round up to end with 2",
144 Math.class, "rint__DD",
149 @Test public void ieeeReminder1() throws Exception {
152 Math.class, "IEEEremainder__DDD",
153 Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
157 @Test public void ieeeReminder2() throws Exception {
160 Math.class, "IEEEremainder__DDD",
161 Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
165 @Test public void divAndRound() throws Exception {
167 "Should be rounded to one",
168 StaticMethod.class, "div__IBD",
173 @Test public void mixedMethodFourParams() throws Exception {
176 StaticMethod.class, "mix__IIJBD",
181 @Test public void factRec() throws Exception {
183 "Factorial of 5 is 120",
184 StaticMethod.class, "factRec__JI",
189 @Test public void factIter() throws Exception {
191 "Factorial of 5 is 120",
192 StaticMethod.class, "factIter__JI",
198 @Test public void xor() throws Exception {
201 StaticMethod.class, "xor__JIJ",
208 @Test public void or() throws Exception {
211 StaticMethod.class, "orOrAnd__JZII",
218 @Test public void nullCheck() throws Exception {
221 StaticMethod.class, "none__Ljava_lang_Object_2II",
225 @Test public void and() throws Exception {
228 StaticMethod.class, "orOrAnd__JZII",
235 @Test public void inc4() throws Exception {
238 StaticMethod.class, "inc4__I",
243 @Test public void shiftLeftInJava() throws Exception {
244 int res = StaticMethod.shiftLeft(1, 8);
245 assertEquals(res, 256);
248 @Test public void shiftLeftInJS() throws Exception {
251 StaticMethod.class, "shiftLeft__III",
257 @Test public void shiftRightInJava() throws Exception {
258 int res = StaticMethod.shiftArithmRight(-8, 3, true);
259 assertEquals(res, -1);
262 @Test public void shiftRightInJS() throws Exception {
265 StaticMethod.class, "shiftArithmRight__IIIZ",
270 @Test public void unsignedShiftRightInJava() throws Exception {
271 int res = StaticMethod.shiftArithmRight(8, 3, false);
272 assertEquals(res, 1);
275 @Test public void unsignedShiftRightInJS() throws Exception {
278 StaticMethod.class, "shiftArithmRight__IIIZ",
284 @Test public void javaScriptBody() throws Exception {
287 StaticMethod.class, "i2s__Ljava_lang_String_2II",
293 @Test public void switchJarda() throws Exception {
295 "The expected value",
296 StaticMethod.class, "swtch__Ljava_lang_String_2I",
302 @Test public void switchDarda() throws Exception {
304 "The expected value",
305 StaticMethod.class, "swtch__Ljava_lang_String_2I",
310 @Test public void switchParda() throws Exception {
312 "The expected value",
313 StaticMethod.class, "swtch2__Ljava_lang_String_2I",
318 @Test public void switchMarda() throws Exception {
320 "The expected value",
321 StaticMethod.class, "swtch__Ljava_lang_String_2I",
327 @Test public void checkNullCast() throws Exception {
328 assertExec("Null can be cast to any type",
329 StaticMethod.class, "castNull__Ljava_lang_String_2Z",
334 private static CharSequence codeSeq;
335 private static Invocable code;
338 public void compileTheCode() throws Exception {
339 StringBuilder sb = new StringBuilder();
340 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
345 private static void assertExec(
346 String msg, Class clazz, String method,
347 Object expRes, Object... args
349 assertExec(code, codeSeq, msg, clazz, method, expRes, args);
351 static void assertExec(
352 Invocable toRun, CharSequence theCode,
353 String msg, Class clazz, String method,
354 Object expRes, Object... args
356 Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args);
360 if (expRes != null && expRes.equals(ret)) {
363 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
367 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
368 return compileClass(sb, null, names);
370 static Invocable compileClass(
371 StringBuilder sb, ScriptEngine[] eng, String... names
372 ) throws ScriptException, IOException {
374 sb = new StringBuilder();
376 Bck2Brwsr.generate(sb, new EmulationResources(), names);
377 ScriptEngineManager sem = new ScriptEngineManager();
378 ScriptEngine js = sem.getEngineByExtension("js");
383 Object res = js.eval(sb.toString());
384 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
385 return (Invocable)js;
386 } catch (Exception ex) {
387 if (sb.length() > 2000) {
390 fail("Could not evaluate:\n" + sb, ex);
394 static StringBuilder dumpJS(CharSequence sb) throws IOException {
395 File f = File.createTempFile("execution", ".js");
396 FileWriter w = new FileWriter(f);
399 return new StringBuilder(f.getPath());
401 private static class EmulationResources implements Bck2Brwsr.Resources {
403 public InputStream get(String name) throws IOException {
404 Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
406 while (en.hasMoreElements()) {
407 u = en.nextElement();
410 throw new IOException("Can't find " + name);
412 if (u.toExternalForm().contains("rt.jar!")) {
413 throw new IOException("No emulation for " + u);
415 return u.openStream();