vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Martin Soch <Martin.Soch@oracle.com>
Fri, 25 Jan 2013 11:00:52 +0100
brancharithmetic
changeset 582 8e546d108658
parent 306 f36b3c273de6
child 677 1ff540c1650f
permissions -rw-r--r--
Long arithmetic prototype, Long currently represented by separate JavaScript object with two JS-Numbers.
Just few operation implemented to pass tests. Tests under vmtest directory are still failing.
     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__Ljava_lang_Object_2II",
   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__Ljava_lang_String_2II",
   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__Ljava_lang_String_2I",
   214             "Jarda",
   215             0
   216         );
   217     }
   218     
   219     @Test public void switchDarda() throws Exception {
   220         assertExec(
   221             "The expected value",
   222             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   223             "Darda",
   224             1
   225         );
   226     }
   227     @Test public void switchParda() throws Exception {
   228         assertExec(
   229             "The expected value",
   230             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   231             "Parda",
   232             22
   233         );
   234     }
   235     @Test public void switchMarda() throws Exception {
   236         assertExec(
   237             "The expected value",
   238             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   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__Ljava_lang_String_2Z", 
   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 = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args);
   274         if (ret == null) {
   275             return;
   276         }
   277         if (expRes != null && expRes.equals(ret)) {
   278             return;
   279         }
   280         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
   281         
   282     }
   283 
   284     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   285         return compileClass(sb, null, names);
   286     }
   287     static Invocable compileClass(
   288         StringBuilder sb, ScriptEngine[] eng, String... names
   289     ) throws ScriptException, IOException {
   290         if (sb == null) {
   291             sb = new StringBuilder();
   292         }
   293         Bck2Brwsr.generate(sb, new EmulationResources(), names);
   294         ScriptEngineManager sem = new ScriptEngineManager();
   295         ScriptEngine js = sem.getEngineByExtension("js");
   296         if (eng != null) {
   297             eng[0] = js;
   298         }
   299         try {
   300             Object res = js.eval(sb.toString());
   301             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   302             return (Invocable)js;
   303         } catch (Exception ex) {
   304             if (sb.length() > 2000) {
   305                 sb = dumpJS(sb);
   306             }
   307             fail("Could not evaluate:\n" + sb, ex);
   308             return null;
   309         }
   310     }
   311     static StringBuilder dumpJS(CharSequence sb) throws IOException {
   312         File f = File.createTempFile("execution", ".js");
   313         FileWriter w = new FileWriter(f);
   314         w.append(sb);
   315         w.close();
   316         return new StringBuilder(f.getPath());
   317     }
   318     private static class EmulationResources implements Bck2Brwsr.Resources {
   319         @Override
   320         public InputStream get(String name) throws IOException {
   321             Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
   322             URL u = null;
   323             while (en.hasMoreElements()) {
   324                 u = en.nextElement();
   325             }
   326             if (u == null) {
   327                 throw new IOException("Can't find " + name);
   328             }
   329             if (u.toExternalForm().contains("rt.jar!")) {
   330                 throw new IOException("No emulation for " + u);
   331             }
   332             return u.openStream();
   333         }
   334     }
   335 }