1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Mon Sep 24 11:07:38 2012 +0200
1.3 @@ -0,0 +1,202 @@
1.4 +/*
1.5 +Java 4 Browser Bytecode Translator
1.6 +Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 +
1.8 +This program is free software: you can redistribute it and/or modify
1.9 +it under the terms of the GNU General Public License as published by
1.10 +the Free Software Foundation, version 2 of the License.
1.11 +
1.12 +This program is distributed in the hope that it will be useful,
1.13 +but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 +GNU General Public License for more details.
1.16 +
1.17 +You should have received a copy of the GNU General Public License
1.18 +along with this program. Look for COPYING file in the top folder.
1.19 +If not, see http://opensource.org/licenses/GPL-2.0.
1.20 +*/
1.21 +package org.apidesign.vm4brwsr;
1.22 +
1.23 +import org.apidesign.vm4brwsr.ByteCodeToJavaScript;
1.24 +import java.io.IOException;
1.25 +import java.io.InputStream;
1.26 +import java.util.Arrays;
1.27 +import java.util.HashSet;
1.28 +import java.util.Iterator;
1.29 +import java.util.LinkedList;
1.30 +import java.util.Set;
1.31 +import java.util.TreeSet;
1.32 +import javax.script.Invocable;
1.33 +import javax.script.ScriptEngine;
1.34 +import javax.script.ScriptEngineManager;
1.35 +import javax.script.ScriptException;
1.36 +import static org.testng.Assert.*;
1.37 +import org.testng.annotations.Test;
1.38 +
1.39 +/** Checks the basic behavior of the translator.
1.40 + *
1.41 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.42 + */
1.43 +public class StaticMethodTest {
1.44 + @Test public void threePlusFour() throws Exception {
1.45 + assertExec(
1.46 + "Should be seven",
1.47 + "org_apidesign_vm4brwsr_StaticMethod_sumIII",
1.48 + Double.valueOf(7),
1.49 + 3, 4
1.50 + );
1.51 + }
1.52 +
1.53 + @Test public void powerOfThree() throws Exception {
1.54 + assertExec(
1.55 + "Should be nine",
1.56 + "org_apidesign_vm4brwsr_StaticMethod_powerFF",
1.57 + Double.valueOf(9),
1.58 + 3.0f
1.59 + );
1.60 + }
1.61 +
1.62 + @Test public void doubleWithoutLong() throws Exception {
1.63 + assertExec(
1.64 + "Should be two",
1.65 + "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
1.66 + Double.valueOf(2),
1.67 + 3.0d, 1l
1.68 + );
1.69 + }
1.70 +
1.71 + @Test public void divAndRound() throws Exception {
1.72 + assertExec(
1.73 + "Should be rounded to one",
1.74 + "org_apidesign_vm4brwsr_StaticMethod_divIBD",
1.75 + Double.valueOf(1),
1.76 + 3, 3.75
1.77 + );
1.78 + }
1.79 + @Test public void mixedMethodFourParams() throws Exception {
1.80 + assertExec(
1.81 + "Should be two",
1.82 + "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
1.83 + Double.valueOf(20),
1.84 + 2, 10l, 5, 2.0
1.85 + );
1.86 + }
1.87 + @Test public void factRec() throws Exception {
1.88 + assertExec(
1.89 + "Factorial of 5 is 120",
1.90 + "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
1.91 + Double.valueOf(120),
1.92 + 5
1.93 + );
1.94 + }
1.95 + @Test public void factIter() throws Exception {
1.96 + assertExec(
1.97 + "Factorial of 5 is 120",
1.98 + "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
1.99 + Double.valueOf(120),
1.100 + 5
1.101 + );
1.102 + }
1.103 +
1.104 + @Test public void xor() throws Exception {
1.105 + assertExec(
1.106 + "Xor is 4",
1.107 + "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
1.108 + Double.valueOf(4),
1.109 + 7,
1.110 + 3
1.111 + );
1.112 + }
1.113 +
1.114 + @Test public void or() throws Exception {
1.115 + assertExec(
1.116 + "Or will be 7",
1.117 + "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
1.118 + Double.valueOf(7),
1.119 + true,
1.120 + 4,
1.121 + 3
1.122 + );
1.123 + }
1.124 + @Test public void and() throws Exception {
1.125 + assertExec(
1.126 + "And will be 3",
1.127 + "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
1.128 + Double.valueOf(3),
1.129 + false,
1.130 + 7,
1.131 + 3
1.132 + );
1.133 + }
1.134 + @Test public void inc4() throws Exception {
1.135 + assertExec(
1.136 + "It will be 4",
1.137 + "org_apidesign_vm4brwsr_StaticMethod_inc4I",
1.138 + Double.valueOf(4)
1.139 + );
1.140 + }
1.141 +
1.142 + private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
1.143 + StringBuilder sb = new StringBuilder();
1.144 + Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
1.145 +
1.146 + Object ret = null;
1.147 + try {
1.148 + ret = i.invokeFunction(methodName, args);
1.149 + } catch (ScriptException ex) {
1.150 + fail("Execution failed in " + sb, ex);
1.151 + } catch (NoSuchMethodException ex) {
1.152 + fail("Cannot find method in " + sb, ex);
1.153 + }
1.154 + if (ret == null && expRes == null) {
1.155 + return;
1.156 + }
1.157 + if (expRes.equals(ret)) {
1.158 + return;
1.159 + }
1.160 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
1.161 +
1.162 + }
1.163 +
1.164 + static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
1.165 + if (sb == null) {
1.166 + sb = new StringBuilder();
1.167 + }
1.168 + Set<String> processed = new HashSet<String>();
1.169 +
1.170 + LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
1.171 + for (;;) {
1.172 + toProcess.removeAll(processed);
1.173 + if (toProcess.isEmpty()) {
1.174 + break;
1.175 + }
1.176 + String name = toProcess.getFirst();
1.177 + processed.add(name);
1.178 + if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
1.179 + continue;
1.180 + }
1.181 + InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
1.182 + assertNotNull(is, "Class file found");
1.183 + try {
1.184 + ByteCodeToJavaScript.compile(is, sb, toProcess);
1.185 + } catch (RuntimeException ex) {
1.186 + int lastBlock = sb.lastIndexOf("{");
1.187 + throw new IllegalStateException(
1.188 + "Error while compiling " + name + "\n" +
1.189 + sb.substring(0, sb.length()),
1.190 + ex
1.191 + );
1.192 + }
1.193 + }
1.194 + ScriptEngineManager sem = new ScriptEngineManager();
1.195 + ScriptEngine js = sem.getEngineByExtension("js");
1.196 + try {
1.197 + Object res = js.eval(sb.toString());
1.198 + assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.199 + return (Invocable)js;
1.200 + } catch (ScriptException ex) {
1.201 + fail("Could not compile:\n" + sb, ex);
1.202 + return null;
1.203 + }
1.204 + }
1.205 +}