vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 12:42:00 +0200
changeset 106 346633cd13d6
parent 103 e8438996d406
child 115 3d5597011af0
permissions -rw-r--r--
Fixing license in all files
     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 /*
    19 Java 4 Browser Bytecode Translator
    20 Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    21 
    22 This program is free software: you can redistribute it and/or modify
    23 it under the terms of the GNU General Public License as published by
    24 the Free Software Foundation, version 2 of the License.
    25 
    26 This program is distributed in the hope that it will be useful,
    27 but WITHOUT ANY WARRANTY; without even the implied warranty of
    28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    29 GNU General Public License for more details.
    30 
    31 You should have received a copy of the GNU General Public License
    32 along with this program. Look for COPYING file in the top folder.
    33 If not, see http://opensource.org/licenses/GPL-2.0.
    34 */
    35 package org.apidesign.vm4brwsr;
    36 
    37 import java.io.IOException;
    38 import javax.script.Invocable;
    39 import javax.script.ScriptEngine;
    40 import javax.script.ScriptEngineManager;
    41 import javax.script.ScriptException;
    42 import static org.testng.Assert.*;
    43 import org.testng.annotations.BeforeClass;
    44 import org.testng.annotations.Test;
    45 
    46 /** Checks the basic behavior of the translator.
    47  *
    48  * @author Jaroslav Tulach <jtulach@netbeans.org>
    49  */
    50 public class StaticMethodTest {
    51     @Test public void threePlusFour() throws Exception {
    52         assertExec(
    53             "Should be seven", 
    54             "org_apidesign_vm4brwsr_StaticMethod_sumIII", 
    55             Double.valueOf(7), 
    56             3, 4
    57         );
    58     }
    59 
    60     @Test public void powerOfThree() throws Exception {
    61         assertExec(
    62             "Should be nine", 
    63             "org_apidesign_vm4brwsr_StaticMethod_powerFF", 
    64             Double.valueOf(9),
    65             3.0f
    66         );
    67     }
    68 
    69     @Test public void minusOne() throws Exception {
    70         assertExec(
    71             "Should be minus one", 
    72             "org_apidesign_vm4brwsr_StaticMethod_minusOneI", 
    73             Double.valueOf(-1)
    74         );
    75     }
    76 
    77     @Test public void doubleWithoutLong() throws Exception {
    78         assertExec(
    79             "Should be two",
    80             "org_apidesign_vm4brwsr_StaticMethod_minusDDJ", 
    81             Double.valueOf(2),
    82             3.0d, 1l
    83         );
    84     }
    85 
    86     @Test public void divAndRound() throws Exception {
    87         assertExec(
    88             "Should be rounded to one",
    89             "org_apidesign_vm4brwsr_StaticMethod_divIBD", 
    90             Double.valueOf(1),
    91             3, 3.75
    92         );
    93     }
    94     @Test public void mixedMethodFourParams() throws Exception {
    95         assertExec(
    96             "Should be two",
    97             "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD", 
    98             Double.valueOf(20),
    99             2, 10l, 5, 2.0
   100         );
   101     }
   102     @Test public void factRec() throws Exception {
   103         assertExec(
   104             "Factorial of 5 is 120",
   105             "org_apidesign_vm4brwsr_StaticMethod_factRecJI", 
   106             Double.valueOf(120),
   107             5
   108         );
   109     }
   110     @Test public void factIter() throws Exception {
   111         assertExec(
   112             "Factorial of 5 is 120",
   113             "org_apidesign_vm4brwsr_StaticMethod_factIterJI", 
   114             Double.valueOf(120),
   115             5
   116         );
   117     }
   118     
   119     @Test public void xor() throws Exception {
   120         assertExec(
   121             "Xor is 4",
   122             "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
   123             Double.valueOf(4),
   124             7,
   125             3
   126         );
   127     }
   128     
   129     @Test public void or() throws Exception {
   130         assertExec(
   131             "Or will be 7",
   132             "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
   133             Double.valueOf(7),
   134             true,
   135             4,
   136             3
   137         );
   138     }
   139     @Test public void nullCheck() throws Exception {
   140         assertExec(
   141             "Returns nothing",
   142             "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
   143             null, 1, 3
   144         );
   145     }
   146     @Test public void and() throws Exception {
   147         assertExec(
   148             "And will be 3",
   149             "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
   150             Double.valueOf(3),
   151             false,
   152             7,
   153             3
   154         );
   155     }
   156     @Test public void inc4() throws Exception {
   157         assertExec(
   158             "It will be 4",
   159             "org_apidesign_vm4brwsr_StaticMethod_inc4I",
   160             Double.valueOf(4)
   161         );
   162     }
   163     
   164     @Test public void shiftLeftInJava() throws Exception {
   165         int res = StaticMethod.shiftLeft(1, 8);
   166         assertEquals(res, 256);
   167     }
   168 
   169     @Test public void shiftLeftInJS() throws Exception {
   170         assertExec(
   171             "Setting 9th bit",
   172             "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
   173             Double.valueOf(256),
   174             1, 8
   175         );
   176     }
   177 
   178     @Test public void shiftRightInJava() throws Exception {
   179         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   180         assertEquals(res, -1);
   181     }
   182 
   183     @Test public void shiftRightInJS() throws Exception {
   184         assertExec(
   185             "Get -1",
   186             "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
   187             Double.valueOf(-1),
   188             -8, 3, true
   189         );
   190     }
   191     @Test public void unsignedShiftRightInJava() throws Exception {
   192         int res = StaticMethod.shiftArithmRight(8, 3, false);
   193         assertEquals(res, 1);
   194     }
   195 
   196     @Test public void unsignedShiftRightInJS() throws Exception {
   197         assertExec(
   198             "Get -1",
   199             "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
   200             Double.valueOf(1),
   201             8, 3, false
   202         );
   203     }
   204     
   205     @Test public void javaScriptBody() throws Exception {
   206         assertExec(
   207             "JavaScript string",
   208             "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
   209             "333",
   210             330, 3
   211         );
   212     }
   213     
   214     private static CharSequence codeSeq;
   215     private static Invocable code;
   216     
   217     @BeforeClass 
   218     public void compileTheCode() throws Exception {
   219         StringBuilder sb = new StringBuilder();
   220         code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   221         codeSeq = sb;
   222     }
   223     
   224     
   225     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   226         StringBuilder sb = new StringBuilder();
   227         
   228         Object ret = null;
   229         try {
   230             ret = code.invokeFunction(methodName, args);
   231         } catch (ScriptException ex) {
   232             fail("Execution failed in\n" + codeSeq, ex);
   233         } catch (NoSuchMethodException ex) {
   234             fail("Cannot find method in\n" + codeSeq, ex);
   235         }
   236         if (ret == null && expRes == null) {
   237             return;
   238         }
   239         if (expRes != null && expRes.equals(ret)) {
   240             return;
   241         }
   242         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   243         
   244     }
   245 
   246     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   247         if (sb == null) {
   248             sb = new StringBuilder();
   249         }
   250         GenJS.compile(sb, names);
   251         ScriptEngineManager sem = new ScriptEngineManager();
   252         ScriptEngine js = sem.getEngineByExtension("js");
   253         try {
   254             Object res = js.eval(sb.toString());
   255             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   256             return (Invocable)js;
   257         } catch (ScriptException ex) {
   258             fail("Could not compile:\n" + sb, ex);
   259             return null;
   260         }
   261     }
   262 }