vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 30 Oct 2012 22:59:31 +0100
changeset 131 dbfbcd718146
parent 115 3d5597011af0
child 137 45184b2f9697
permissions -rw-r--r--
One license is enough
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 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     @Test public void switchJarda() throws Exception {
   198         assertExec(
   199             "The expected value",
   200             "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
   201             "Jarda",
   202             0
   203         );
   204     }
   205     
   206     @Test public void switchDarda() throws Exception {
   207         assertExec(
   208             "The expected value",
   209             "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
   210             "Darda",
   211             1
   212         );
   213     }
   214     @Test public void switchParda() throws Exception {
   215         assertExec(
   216             "The expected value",
   217             "org_apidesign_vm4brwsr_StaticMethod_swtch2Ljava_lang_StringI",
   218             "Parda",
   219             22
   220         );
   221     }
   222     @Test public void switchMarda() throws Exception {
   223         assertExec(
   224             "The expected value",
   225             "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
   226             "Marda",
   227             -433
   228         );
   229     }
   230     
   231     private static CharSequence codeSeq;
   232     private static Invocable code;
   233     
   234     @BeforeClass 
   235     public void compileTheCode() throws Exception {
   236         StringBuilder sb = new StringBuilder();
   237         code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   238         codeSeq = sb;
   239     }
   240     
   241     
   242     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   243         StringBuilder sb = new StringBuilder();
   244         
   245         Object ret = null;
   246         try {
   247             ret = code.invokeFunction(methodName, args);
   248         } catch (ScriptException ex) {
   249             fail("Execution failed in\n" + codeSeq, ex);
   250         } catch (NoSuchMethodException ex) {
   251             fail("Cannot find method in\n" + codeSeq, ex);
   252         }
   253         if (ret == null && expRes == null) {
   254             return;
   255         }
   256         if (expRes != null && expRes.equals(ret)) {
   257             return;
   258         }
   259         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   260         
   261     }
   262 
   263     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   264         if (sb == null) {
   265             sb = new StringBuilder();
   266         }
   267         GenJS.compile(sb, names);
   268         ScriptEngineManager sem = new ScriptEngineManager();
   269         ScriptEngine js = sem.getEngineByExtension("js");
   270         try {
   271             Object res = js.eval(sb.toString());
   272             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   273             return (Invocable)js;
   274         } catch (ScriptException ex) {
   275             fail("Could not compile:\n" + sb, ex);
   276             return null;
   277         }
   278     }
   279 }