vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 28 Sep 2012 14:58:21 +0200
changeset 48 4fca8ddf46de
parent 46 b07c7c256771
child 93 a236a9f137ac
permissions -rw-r--r--
Supporting iconst_m1
     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     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   147         StringBuilder sb = new StringBuilder();
   148         Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   149         
   150         Object ret = null;
   151         try {
   152             ret = i.invokeFunction(methodName, args);
   153         } catch (ScriptException ex) {
   154             fail("Execution failed in " + sb, ex);
   155         } catch (NoSuchMethodException ex) {
   156             fail("Cannot find method in " + sb, ex);
   157         }
   158         if (ret == null && expRes == null) {
   159             return;
   160         }
   161         if (expRes != null && expRes.equals(ret)) {
   162             return;
   163         }
   164         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   165         
   166     }
   167 
   168     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   169         if (sb == null) {
   170             sb = new StringBuilder();
   171         }
   172         GenJS.compile(sb, names);
   173         ScriptEngineManager sem = new ScriptEngineManager();
   174         ScriptEngine js = sem.getEngineByExtension("js");
   175         try {
   176             Object res = js.eval(sb.toString());
   177             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   178             return (Invocable)js;
   179         } catch (ScriptException ex) {
   180             fail("Could not compile:\n" + sb, ex);
   181             return null;
   182         }
   183     }
   184 }