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