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