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