vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 03 Jan 2013 11:29:22 +0100
branchTypeNickNames
changeset 406 2670f519a46d
parent 405 e41809be6106
permissions -rw-r--r--
Using 'o' instead of full name of java.lang.Object
     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 java.io.InputStream;
    24 import java.net.URL;
    25 import java.util.Enumeration;
    26 import javax.script.Invocable;
    27 import javax.script.ScriptEngine;
    28 import javax.script.ScriptEngineManager;
    29 import javax.script.ScriptException;
    30 import static org.testng.Assert.*;
    31 import org.testng.annotations.BeforeClass;
    32 import org.testng.annotations.Test;
    33 
    34 /** Checks the basic behavior of the translator.
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 public class StaticMethodTest {
    39     @Test public void threePlusFour() throws Exception {
    40         assertExec(
    41             "Should be seven", 
    42             StaticMethod.class, "sum__III", 
    43             Double.valueOf(7), 
    44             3, 4
    45         );
    46     }
    47 
    48     @Test public void checkReallyInitializedValues() throws Exception {
    49         assertExec(
    50             "Return true", 
    51             StaticMethod.class, "isNull__Z", 
    52             Double.valueOf(1)
    53         );
    54     }
    55 
    56     @Test public void powerOfThree() throws Exception {
    57         assertExec(
    58             "Should be nine", 
    59             StaticMethod.class, "power__FF", 
    60             Double.valueOf(9),
    61             3.0f
    62         );
    63     }
    64 
    65     @Test public void minusOne() throws Exception {
    66         assertExec(
    67             "Should be minus one", 
    68             StaticMethod.class, "minusOne__I", 
    69             Double.valueOf(-1)
    70         );
    71     }
    72 
    73     @Test public void doubleWithoutLong() throws Exception {
    74         assertExec(
    75             "Should be two",
    76             StaticMethod.class, "minus__DDJ", 
    77             Double.valueOf(2),
    78             3.0d, 1l
    79         );
    80     }
    81 
    82     @Test public void divAndRound() throws Exception {
    83         assertExec(
    84             "Should be rounded to one",
    85             StaticMethod.class, "div__IBD", 
    86             Double.valueOf(1),
    87             3, 3.75
    88         );
    89     }
    90     @Test public void mixedMethodFourParams() throws Exception {
    91         assertExec(
    92             "Should be two",
    93             StaticMethod.class, "mix__IIJBD", 
    94             Double.valueOf(20),
    95             2, 10l, 5, 2.0
    96         );
    97     }
    98     @Test public void factRec() throws Exception {
    99         assertExec(
   100             "Factorial of 5 is 120",
   101             StaticMethod.class, "factRec__JI", 
   102             Double.valueOf(120),
   103             5
   104         );
   105     }
   106     @Test public void factIter() throws Exception {
   107         assertExec(
   108             "Factorial of 5 is 120",
   109             StaticMethod.class, "factIter__JI", 
   110             Double.valueOf(120),
   111             5
   112         );
   113     }
   114     
   115     @Test public void xor() throws Exception {
   116         assertExec(
   117             "Xor is 4",
   118             StaticMethod.class, "xor__JIJ",
   119             Double.valueOf(4),
   120             7,
   121             3
   122         );
   123     }
   124     
   125     @Test public void or() throws Exception {
   126         assertExec(
   127             "Or will be 7",
   128             StaticMethod.class, "orOrAnd__JZII",
   129             Double.valueOf(7),
   130             true,
   131             4,
   132             3
   133         );
   134     }
   135     @Test public void nullCheck() throws Exception {
   136         assertExec(
   137             "Returns nothing",
   138             StaticMethod.class, "none__oII",
   139             null, 1, 3
   140         );
   141     }
   142     @Test public void and() throws Exception {
   143         assertExec(
   144             "And will be 3",
   145             StaticMethod.class, "orOrAnd__JZII",
   146             Double.valueOf(3),
   147             false,
   148             7,
   149             3
   150         );
   151     }
   152     @Test public void inc4() throws Exception {
   153         assertExec(
   154             "It will be 4",
   155             StaticMethod.class, "inc4__I",
   156             Double.valueOf(4)
   157         );
   158     }
   159     
   160     @Test public void shiftLeftInJava() throws Exception {
   161         int res = StaticMethod.shiftLeft(1, 8);
   162         assertEquals(res, 256);
   163     }
   164 
   165     @Test public void shiftLeftInJS() throws Exception {
   166         assertExec(
   167             "Setting 9th bit",
   168             StaticMethod.class, "shiftLeft__III",
   169             Double.valueOf(256),
   170             1, 8
   171         );
   172     }
   173 
   174     @Test public void shiftRightInJava() throws Exception {
   175         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   176         assertEquals(res, -1);
   177     }
   178 
   179     @Test public void shiftRightInJS() throws Exception {
   180         assertExec(
   181             "Get -1",
   182             StaticMethod.class, "shiftArithmRight__IIIZ",
   183             Double.valueOf(-1),
   184             -8, 3, true
   185         );
   186     }
   187     @Test public void unsignedShiftRightInJava() throws Exception {
   188         int res = StaticMethod.shiftArithmRight(8, 3, false);
   189         assertEquals(res, 1);
   190     }
   191 
   192     @Test public void unsignedShiftRightInJS() throws Exception {
   193         assertExec(
   194             "Get -1",
   195             StaticMethod.class, "shiftArithmRight__IIIZ",
   196             Double.valueOf(1),
   197             8, 3, false
   198         );
   199     }
   200     
   201     @Test public void javaScriptBody() throws Exception {
   202         assertExec(
   203             "JavaScript string",
   204             StaticMethod.class, "i2s__sII",
   205             "333",
   206             330, 3
   207         );
   208     }
   209     
   210     @Test public void switchJarda() throws Exception {
   211         assertExec(
   212             "The expected value",
   213             StaticMethod.class, "swtch__sI",
   214             "Jarda",
   215             0
   216         );
   217     }
   218     
   219     @Test public void switchDarda() throws Exception {
   220         assertExec(
   221             "The expected value",
   222             StaticMethod.class, "swtch__sI",
   223             "Darda",
   224             1
   225         );
   226     }
   227     @Test public void switchParda() throws Exception {
   228         assertExec(
   229             "The expected value",
   230             StaticMethod.class, "swtch2__sI",
   231             "Parda",
   232             22
   233         );
   234     }
   235     @Test public void switchMarda() throws Exception {
   236         assertExec(
   237             "The expected value",
   238             StaticMethod.class, "swtch__sI",
   239             "Marda",
   240             -433
   241         );
   242     }
   243     
   244     @Test public void checkNullCast() throws Exception {
   245         assertExec("Null can be cast to any type",
   246             StaticMethod.class, "castNull__sZ", 
   247             null, true
   248         );
   249     }
   250     
   251     private static CharSequence codeSeq;
   252     private static Invocable code;
   253     
   254     @BeforeClass 
   255     public void compileTheCode() throws Exception {
   256         StringBuilder sb = new StringBuilder();
   257         code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   258         codeSeq = sb;
   259     }
   260     
   261     
   262     private static void assertExec(
   263         String msg, Class clazz, String method, 
   264         Object expRes, Object... args
   265     ) throws Exception {
   266         assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   267     }
   268     static void assertExec(
   269         Invocable toRun, CharSequence theCode,
   270         String msg, Class clazz, String method, 
   271         Object expRes, Object... args
   272     ) throws Exception {
   273         Object ret = null;
   274         try {
   275             ret = toRun.invokeFunction("bck2brwsr");
   276             ret = toRun.invokeMethod(ret, "loadClass", clazz.getName());
   277             ret = toRun.invokeMethod(ret, method, args);
   278         } catch (ScriptException ex) {
   279             fail("Execution failed in\n" + dumpJS(theCode), ex);
   280         } catch (NoSuchMethodException ex) {
   281             fail("Cannot find method in\n" + dumpJS(theCode), ex);
   282         }
   283         if (ret == null && expRes == null) {
   284             return;
   285         }
   286         if (expRes != null && expRes.equals(ret)) {
   287             return;
   288         }
   289         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
   290         
   291     }
   292 
   293     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   294         return compileClass(sb, null, names);
   295     }
   296     static Invocable compileClass(
   297         StringBuilder sb, ScriptEngine[] eng, String... names
   298     ) throws ScriptException, IOException {
   299         if (sb == null) {
   300             sb = new StringBuilder();
   301         }
   302         Bck2Brwsr.generate(sb, new EmulationResources(), names);
   303         ScriptEngineManager sem = new ScriptEngineManager();
   304         ScriptEngine js = sem.getEngineByExtension("js");
   305         if (eng != null) {
   306             eng[0] = js;
   307         }
   308         try {
   309             Object res = js.eval(sb.toString());
   310             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   311             return (Invocable)js;
   312         } catch (Exception ex) {
   313             if (sb.length() > 2000) {
   314                 sb = dumpJS(sb);
   315             }
   316             fail("Could not evaluate:\n" + sb, ex);
   317             return null;
   318         }
   319     }
   320     static StringBuilder dumpJS(CharSequence sb) throws IOException {
   321         File f = File.createTempFile("execution", ".js");
   322         FileWriter w = new FileWriter(f);
   323         w.append(sb);
   324         w.close();
   325         return new StringBuilder(f.getPath());
   326     }
   327     private static class EmulationResources implements Bck2Brwsr.Resources {
   328         @Override
   329         public InputStream get(String name) throws IOException {
   330             Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
   331             URL u = null;
   332             while (en.hasMoreElements()) {
   333                 u = en.nextElement();
   334             }
   335             if (u == null) {
   336                 throw new IOException("Can't find " + name);
   337             }
   338             if (u.toExternalForm().contains("rt.jar!")) {
   339                 throw new IOException("No emulation for " + u);
   340             }
   341             return u.openStream();
   342         }
   343     }
   344 }