vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 11 Oct 2012 04:11:42 -0700
branchemul
changeset 99 67e892757752
parent 94 19497b4312bb
child 103 e8438996d406
permissions -rw-r--r--
Multiparameter static methods with JavaScriptBody work now
     1 /*
     2 Java 4 Browser Bytecode Translator
     3 Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4 
     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.
     8 
     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.
    13 
    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.
    17 */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import javax.script.Invocable;
    22 import javax.script.ScriptEngine;
    23 import javax.script.ScriptEngineManager;
    24 import javax.script.ScriptException;
    25 import static org.testng.Assert.*;
    26 import org.testng.annotations.Test;
    27 
    28 /** Checks the basic behavior of the translator.
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class StaticMethodTest {
    33     @Test public void threePlusFour() throws Exception {
    34         assertExec(
    35             "Should be seven", 
    36             "org_apidesign_vm4brwsr_StaticMethod_sumIII", 
    37             Double.valueOf(7), 
    38             3, 4
    39         );
    40     }
    41 
    42     @Test public void powerOfThree() throws Exception {
    43         assertExec(
    44             "Should be nine", 
    45             "org_apidesign_vm4brwsr_StaticMethod_powerFF", 
    46             Double.valueOf(9),
    47             3.0f
    48         );
    49     }
    50 
    51     @Test public void minusOne() throws Exception {
    52         assertExec(
    53             "Should be minus one", 
    54             "org_apidesign_vm4brwsr_StaticMethod_minusOneI", 
    55             Double.valueOf(-1)
    56         );
    57     }
    58 
    59     @Test public void doubleWithoutLong() throws Exception {
    60         assertExec(
    61             "Should be two",
    62             "org_apidesign_vm4brwsr_StaticMethod_minusDDJ", 
    63             Double.valueOf(2),
    64             3.0d, 1l
    65         );
    66     }
    67 
    68     @Test public void divAndRound() throws Exception {
    69         assertExec(
    70             "Should be rounded to one",
    71             "org_apidesign_vm4brwsr_StaticMethod_divIBD", 
    72             Double.valueOf(1),
    73             3, 3.75
    74         );
    75     }
    76     @Test public void mixedMethodFourParams() throws Exception {
    77         assertExec(
    78             "Should be two",
    79             "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD", 
    80             Double.valueOf(20),
    81             2, 10l, 5, 2.0
    82         );
    83     }
    84     @Test public void factRec() throws Exception {
    85         assertExec(
    86             "Factorial of 5 is 120",
    87             "org_apidesign_vm4brwsr_StaticMethod_factRecJI", 
    88             Double.valueOf(120),
    89             5
    90         );
    91     }
    92     @Test public void factIter() throws Exception {
    93         assertExec(
    94             "Factorial of 5 is 120",
    95             "org_apidesign_vm4brwsr_StaticMethod_factIterJI", 
    96             Double.valueOf(120),
    97             5
    98         );
    99     }
   100     
   101     @Test public void xor() throws Exception {
   102         assertExec(
   103             "Xor is 4",
   104             "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
   105             Double.valueOf(4),
   106             7,
   107             3
   108         );
   109     }
   110     
   111     @Test public void or() throws Exception {
   112         assertExec(
   113             "Or will be 7",
   114             "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
   115             Double.valueOf(7),
   116             true,
   117             4,
   118             3
   119         );
   120     }
   121     @Test public void nullCheck() throws Exception {
   122         assertExec(
   123             "Returns nothing",
   124             "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
   125             null, 1, 3
   126         );
   127     }
   128     @Test public void and() throws Exception {
   129         assertExec(
   130             "And will be 3",
   131             "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
   132             Double.valueOf(3),
   133             false,
   134             7,
   135             3
   136         );
   137     }
   138     @Test public void inc4() throws Exception {
   139         assertExec(
   140             "It will be 4",
   141             "org_apidesign_vm4brwsr_StaticMethod_inc4I",
   142             Double.valueOf(4)
   143         );
   144     }
   145     
   146     @Test public void shiftLeftInJava() throws Exception {
   147         int res = StaticMethod.shiftLeft(1, 8);
   148         assertEquals(res, 256);
   149     }
   150 
   151     @Test public void shiftLeftInJS() throws Exception {
   152         assertExec(
   153             "Setting 9th bit",
   154             "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
   155             Double.valueOf(256),
   156             1, 8
   157         );
   158     }
   159 
   160     @Test public void shiftRightInJava() throws Exception {
   161         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   162         assertEquals(res, -1);
   163     }
   164 
   165     @Test public void shiftRightInJS() throws Exception {
   166         assertExec(
   167             "Get -1",
   168             "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
   169             Double.valueOf(-1),
   170             -8, 3, true
   171         );
   172     }
   173     @Test public void unsignedShiftRightInJava() throws Exception {
   174         int res = StaticMethod.shiftArithmRight(8, 3, false);
   175         assertEquals(res, 1);
   176     }
   177 
   178     @Test public void unsignedShiftRightInJS() throws Exception {
   179         assertExec(
   180             "Get -1",
   181             "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
   182             Double.valueOf(1),
   183             8, 3, false
   184         );
   185     }
   186     
   187     @Test public void javaScriptBody() throws Exception {
   188         assertExec(
   189             "JavaScript string",
   190             "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
   191             "333",
   192             330, 3
   193         );
   194     }
   195     
   196     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   197         StringBuilder sb = new StringBuilder();
   198         Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   199         
   200         Object ret = null;
   201         try {
   202             ret = i.invokeFunction(methodName, args);
   203         } catch (ScriptException ex) {
   204             fail("Execution failed in " + sb, ex);
   205         } catch (NoSuchMethodException ex) {
   206             fail("Cannot find method in " + sb, ex);
   207         }
   208         if (ret == null && expRes == null) {
   209             return;
   210         }
   211         if (expRes != null && expRes.equals(ret)) {
   212             return;
   213         }
   214         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   215         
   216     }
   217 
   218     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   219         if (sb == null) {
   220             sb = new StringBuilder();
   221         }
   222         GenJS.compile(sb, names);
   223         ScriptEngineManager sem = new ScriptEngineManager();
   224         ScriptEngine js = sem.getEngineByExtension("js");
   225         try {
   226             Object res = js.eval(sb.toString());
   227             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   228             return (Invocable)js;
   229         } catch (ScriptException ex) {
   230             fail("Could not compile:\n" + sb, ex);
   231             return null;
   232         }
   233     }
   234 }