vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Martin Soch <Martin.Soch@oracle.com>
Tue, 05 Feb 2013 17:04:22 +0100
brancharithmetic
changeset 677 1ff540c1650f
parent 582 8e546d108658
parent 605 3223b1897b71
child 708 59d5596a9c6c
permissions -rw-r--r--
merge with trunk
     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 rintNegativeUp() throws Exception {
    83         final double cnts = -453904.634;
    84         assertExec(
    85             "Should round up to end with 5",
    86             Math.class, "rint__DD", 
    87             -453905.0, cnts
    88         );
    89     }
    90 
    91     @Test public void rintNegativeDown() throws Exception {
    92         final double cnts = -453904.434;
    93         assertExec(
    94             "Should round up to end with 4",
    95             Math.class, "rint__DD", 
    96             -453904.0, cnts
    97         );
    98     }
    99 
   100     @Test public void rintPositiveUp() throws Exception {
   101         final double cnts = 453904.634;
   102         assertExec(
   103             "Should round up to end with 5",
   104             Math.class, "rint__DD", 
   105             453905.0, cnts
   106         );
   107     }
   108     @Test public void rintPositiveDown() throws Exception {
   109         final double cnts = 453904.434;
   110         assertExec(
   111             "Should round up to end with 4",
   112             Math.class, "rint__DD", 
   113             453904.0, cnts
   114         );
   115     }
   116     @Test public void rintOneHalf() throws Exception {
   117         final double cnts = 1.5;
   118         assertExec(
   119             "Should round up to end with 2",
   120             Math.class, "rint__DD", 
   121             2.0, cnts
   122         );
   123     }
   124     @Test public void rintNegativeOneHalf() throws Exception {
   125         final double cnts = -1.5;
   126         assertExec(
   127             "Should round up to end with 2",
   128             Math.class, "rint__DD", 
   129             -2.0, cnts
   130         );
   131     }
   132     @Test public void rintTwoAndHalf() throws Exception {
   133         final double cnts = 2.5;
   134         assertExec(
   135             "Should round up to end with 2",
   136             Math.class, "rint__DD", 
   137             2.0, cnts
   138         );
   139     }
   140     @Test public void rintNegativeTwoOneHalf() throws Exception {
   141         final double cnts = -2.5;
   142         assertExec(
   143             "Should round up to end with 2",
   144             Math.class, "rint__DD", 
   145             -2.0, cnts
   146         );
   147     }
   148 
   149     @Test public void ieeeReminder1() throws Exception {
   150         assertExec(
   151             "Same result 1",
   152             Math.class, "IEEEremainder__DDD", 
   153             Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
   154         );
   155     }
   156 
   157     @Test public void ieeeReminder2() throws Exception {
   158         assertExec(
   159             "Same result 1",
   160             Math.class, "IEEEremainder__DDD", 
   161             Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
   162         );
   163     }
   164 
   165     @Test public void divAndRound() throws Exception {
   166         assertExec(
   167             "Should be rounded to one",
   168             StaticMethod.class, "div__IBD", 
   169             Double.valueOf(1),
   170             3, 3.75
   171         );
   172     }
   173     @Test public void mixedMethodFourParams() throws Exception {
   174         assertExec(
   175             "Should be two",
   176             StaticMethod.class, "mix__IIJBD", 
   177             Double.valueOf(20),
   178             2, 10l, 5, 2.0
   179         );
   180     }
   181     @Test public void factRec() throws Exception {
   182         assertExec(
   183             "Factorial of 5 is 120",
   184             StaticMethod.class, "factRec__JI", 
   185             Double.valueOf(120),
   186             5
   187         );
   188     }
   189     @Test public void factIter() throws Exception {
   190         assertExec(
   191             "Factorial of 5 is 120",
   192             StaticMethod.class, "factIter__JI", 
   193             Double.valueOf(120),
   194             5
   195         );
   196     }
   197     
   198     @Test public void xor() throws Exception {
   199         assertExec(
   200             "Xor is 4",
   201             StaticMethod.class, "xor__JIJ",
   202             Double.valueOf(4),
   203             7,
   204             3
   205         );
   206     }
   207     
   208     @Test public void or() throws Exception {
   209         assertExec(
   210             "Or will be 7",
   211             StaticMethod.class, "orOrAnd__JZII",
   212             Double.valueOf(7),
   213             true,
   214             4,
   215             3
   216         );
   217     }
   218     @Test public void nullCheck() throws Exception {
   219         assertExec(
   220             "Returns nothing",
   221             StaticMethod.class, "none__Ljava_lang_Object_2II",
   222             null, 1, 3
   223         );
   224     }
   225     @Test public void and() throws Exception {
   226         assertExec(
   227             "And will be 3",
   228             StaticMethod.class, "orOrAnd__JZII",
   229             Double.valueOf(3),
   230             false,
   231             7,
   232             3
   233         );
   234     }
   235     @Test public void inc4() throws Exception {
   236         assertExec(
   237             "It will be 4",
   238             StaticMethod.class, "inc4__I",
   239             Double.valueOf(4)
   240         );
   241     }
   242     
   243     @Test public void shiftLeftInJava() throws Exception {
   244         int res = StaticMethod.shiftLeft(1, 8);
   245         assertEquals(res, 256);
   246     }
   247 
   248     @Test public void shiftLeftInJS() throws Exception {
   249         assertExec(
   250             "Setting 9th bit",
   251             StaticMethod.class, "shiftLeft__III",
   252             Double.valueOf(256),
   253             1, 8
   254         );
   255     }
   256 
   257     @Test public void shiftRightInJava() throws Exception {
   258         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   259         assertEquals(res, -1);
   260     }
   261 
   262     @Test public void shiftRightInJS() throws Exception {
   263         assertExec(
   264             "Get -1",
   265             StaticMethod.class, "shiftArithmRight__IIIZ",
   266             Double.valueOf(-1),
   267             -8, 3, true
   268         );
   269     }
   270     @Test public void unsignedShiftRightInJava() throws Exception {
   271         int res = StaticMethod.shiftArithmRight(8, 3, false);
   272         assertEquals(res, 1);
   273     }
   274 
   275     @Test public void unsignedShiftRightInJS() throws Exception {
   276         assertExec(
   277             "Get -1",
   278             StaticMethod.class, "shiftArithmRight__IIIZ",
   279             Double.valueOf(1),
   280             8, 3, false
   281         );
   282     }
   283     
   284     @Test public void javaScriptBody() throws Exception {
   285         assertExec(
   286             "JavaScript string",
   287             StaticMethod.class, "i2s__Ljava_lang_String_2II",
   288             "333",
   289             330, 3
   290         );
   291     }
   292     
   293     @Test public void switchJarda() throws Exception {
   294         assertExec(
   295             "The expected value",
   296             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   297             "Jarda",
   298             0
   299         );
   300     }
   301     
   302     @Test public void switchDarda() throws Exception {
   303         assertExec(
   304             "The expected value",
   305             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   306             "Darda",
   307             1
   308         );
   309     }
   310     @Test public void switchParda() throws Exception {
   311         assertExec(
   312             "The expected value",
   313             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   314             "Parda",
   315             22
   316         );
   317     }
   318     @Test public void switchMarda() throws Exception {
   319         assertExec(
   320             "The expected value",
   321             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   322             "Marda",
   323             -433
   324         );
   325     }
   326     
   327     @Test public void checkNullCast() throws Exception {
   328         assertExec("Null can be cast to any type",
   329             StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
   330             null, true
   331         );
   332     }
   333     
   334     private static CharSequence codeSeq;
   335     private static Invocable code;
   336     
   337     @BeforeClass 
   338     public void compileTheCode() throws Exception {
   339         StringBuilder sb = new StringBuilder();
   340         code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   341         codeSeq = sb;
   342     }
   343     
   344     
   345     private static void assertExec(
   346         String msg, Class clazz, String method, 
   347         Object expRes, Object... args
   348     ) throws Exception {
   349         assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   350     }
   351     static void assertExec(
   352         Invocable toRun, CharSequence theCode,
   353         String msg, Class clazz, String method, 
   354         Object expRes, Object... args
   355     ) throws Exception {
   356         Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args);
   357         if (ret == null) {
   358             return;
   359         }
   360         if (expRes != null && expRes.equals(ret)) {
   361             return;
   362         }
   363         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
   364         
   365     }
   366 
   367     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   368         return compileClass(sb, null, names);
   369     }
   370     static Invocable compileClass(
   371         StringBuilder sb, ScriptEngine[] eng, String... names
   372     ) throws ScriptException, IOException {
   373         if (sb == null) {
   374             sb = new StringBuilder();
   375         }
   376         Bck2Brwsr.generate(sb, new EmulationResources(), names);
   377         ScriptEngineManager sem = new ScriptEngineManager();
   378         ScriptEngine js = sem.getEngineByExtension("js");
   379         if (eng != null) {
   380             eng[0] = js;
   381         }
   382         try {
   383             Object res = js.eval(sb.toString());
   384             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   385             return (Invocable)js;
   386         } catch (Exception ex) {
   387             if (sb.length() > 2000) {
   388                 sb = dumpJS(sb);
   389             }
   390             fail("Could not evaluate:\n" + sb, ex);
   391             return null;
   392         }
   393     }
   394     static StringBuilder dumpJS(CharSequence sb) throws IOException {
   395         File f = File.createTempFile("execution", ".js");
   396         FileWriter w = new FileWriter(f);
   397         w.append(sb);
   398         w.close();
   399         return new StringBuilder(f.getPath());
   400     }
   401     private static class EmulationResources implements Bck2Brwsr.Resources {
   402         @Override
   403         public InputStream get(String name) throws IOException {
   404             Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
   405             URL u = null;
   406             while (en.hasMoreElements()) {
   407                 u = en.nextElement();
   408             }
   409             if (u == null) {
   410                 throw new IOException("Can't find " + name);
   411             }
   412             if (u.toExternalForm().contains("rt.jar!")) {
   413                 throw new IOException("No emulation for " + u);
   414             }
   415             return u.openStream();
   416         }
   417     }
   418 }