A test to verify the extension mode closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 25 Apr 2014 09:59:48 +0200
branchclosure
changeset 148784a744941c9f
parent 1486 b5c7970a39e6
child 1488 c2a6be99ffe2
A test to verify the extension mode
rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
     1.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Fri Apr 25 08:14:12 2014 +0200
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Fri Apr 25 09:59:48 2014 +0200
     1.3 @@ -26,6 +26,7 @@
     1.4   *
     1.5   * @author Jaroslav Tulach <jtulach@netbeans.org>
     1.6   */
     1.7 +@org.apidesign.bck2brwsr.core.Exported 
     1.8  public class Numbers {
     1.9      private static Double autoboxDbl() {
    1.10          return 3.3;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java	Fri Apr 25 09:59:48 2014 +0200
     2.3 @@ -0,0 +1,238 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.vm4brwsr;
    2.22 +
    2.23 +import static org.testng.Assert.*;
    2.24 +import org.testng.annotations.AfterClass;
    2.25 +import org.testng.annotations.BeforeClass;
    2.26 +import org.testng.annotations.Test;
    2.27 +
    2.28 +/**
    2.29 + *
    2.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.31 + */
    2.32 +public class NumbersAsExtensionTest {
    2.33 +    @Test public void integerFromString() throws Exception {
    2.34 +        assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
    2.35 +            Double.valueOf(333), "333"
    2.36 +        );
    2.37 +    }
    2.38 +
    2.39 +    @Test public void doubleFromString() throws Exception {
    2.40 +        assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
    2.41 +            Double.valueOf(33.3), "33.3"
    2.42 +        );
    2.43 +    }
    2.44 +
    2.45 +    @Test public void autoboxDouble() throws Exception {
    2.46 +        assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
    2.47 +            "3.3"
    2.48 +        );
    2.49 +    }
    2.50 +    
    2.51 +    @Test public void javalog1000() throws Exception {
    2.52 +        assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
    2.53 +    }
    2.54 +
    2.55 +    @Test public void jslog1000() throws Exception {
    2.56 +        assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
    2.57 +            Double.valueOf(3.0), 1000.0
    2.58 +        );
    2.59 +    }
    2.60 +    
    2.61 +    @Test public void javaRem() {
    2.62 +        assertEquals(3, Numbers.rem(303, 10));
    2.63 +    }
    2.64 +    @Test public void jsRem() throws Exception {
    2.65 +        assertExec("Should be three", Numbers.class, "rem__III", 
    2.66 +            Double.valueOf(3.0), 303, 10
    2.67 +        );
    2.68 +    }
    2.69 +    
    2.70 +    @Test public void deserializeInt() throws Exception {
    2.71 +        int exp = Numbers.deserInt();
    2.72 +        assertExec("Should be the same", Numbers.class, "deserInt__I", 
    2.73 +            Double.valueOf(exp)
    2.74 +        );
    2.75 +    }
    2.76 +
    2.77 +    @Test public void deserializeSimpleLong() throws Exception {
    2.78 +        assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
    2.79 +            Double.valueOf(3454), 
    2.80 +            new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    2.81 +        );
    2.82 +    }
    2.83 +    /* XXX: JavaScript cannot represent as big longs as Java. 
    2.84 +    @Test public void deserializeLargeLong() throws Exception {
    2.85 +        final byte[] arr = new byte[] {
    2.86 +            (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    2.87 +        };
    2.88 +        long exp = Numbers.deserLong(arr);
    2.89 +        assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB", 
    2.90 +            Double.valueOf(exp), arr);
    2.91 +    }
    2.92 +    */
    2.93 +    
    2.94 +    @Test public void deserializeFloatInJava() throws Exception {
    2.95 +        float f = 54324.32423f;
    2.96 +        float r = Numbers.deserFloat();
    2.97 +        assertEquals(r, f, "Floats are the same");
    2.98 +    }
    2.99 +    
   2.100 +    @Test public void deserializeFloatInJS() throws Exception {
   2.101 +        float f = 54324.32423f;
   2.102 +        assertExec("Should be the same", Numbers.class, "deserFloat__F", 
   2.103 +            Double.valueOf(f)
   2.104 +        );
   2.105 +    }
   2.106 +
   2.107 +    @Test public void deserializeDoubleInJava() throws Exception {
   2.108 +        double f = 3.0;
   2.109 +        double r = Numbers.deserDouble();
   2.110 +        assertEquals(r, f, 0.001, "Doubles are the same");
   2.111 +    }
   2.112 +    
   2.113 +    @Test public void deserializeDoubleInJS() throws Exception {
   2.114 +        double f = 3.0;
   2.115 +        assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
   2.116 +    }
   2.117 +    /*
   2.118 +    @Test public void serDouble() throws IOException {
   2.119 +        double f = 3.0;
   2.120 +        ByteArrayOutputStream os = new ByteArrayOutputStream();
   2.121 +        DataOutputStream d = new DataOutputStream(os);
   2.122 +        d.writeLong(3454);
   2.123 +        d.close();
   2.124 +        
   2.125 +        StringBuilder sb = new StringBuilder();
   2.126 +        byte[] arr = os.toByteArray();
   2.127 +        for (int i = 0; i < arr.length; i++) {
   2.128 +            sb.append("(byte)").append(arr[i]).append(", ");
   2.129 +        }
   2.130 +        fail("" + sb);
   2.131 +    }
   2.132 +*/    
   2.133 +    @Test public void fiveInStringJS() throws Exception {
   2.134 +        String s = Numbers.intToString();
   2.135 +        assertExec("Should be the same: " + s, 
   2.136 +            Numbers.class, "intToString__Ljava_lang_String_2", 
   2.137 +            s
   2.138 +        );
   2.139 +    }
   2.140 +
   2.141 +    @Test public void sevenInStringJS() throws Exception {
   2.142 +        String s = Numbers.floatToString();
   2.143 +        assertExec("Should be the same: " + s, 
   2.144 +            Numbers.class, "floatToString__Ljava_lang_String_2", 
   2.145 +            s
   2.146 +        );
   2.147 +    }
   2.148 +
   2.149 +    @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
   2.150 +        assertExec("Can we call doubleValue?", 
   2.151 +            Numbers.class, "seven__DI", 
   2.152 +            Double.valueOf(7.0), 0
   2.153 +        );
   2.154 +    }
   2.155 +    @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
   2.156 +        assertExec("Can we call doubleValue?", 
   2.157 +            Numbers.class, "seven__DI", 
   2.158 +            Double.valueOf(7.0), 1
   2.159 +        );
   2.160 +    }
   2.161 +    @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
   2.162 +        assertExec("Can we call doubleValue?", 
   2.163 +            Numbers.class, "seven__DI", 
   2.164 +            Double.valueOf(7.0), 2
   2.165 +        );
   2.166 +    }
   2.167 +    @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
   2.168 +        assertExec("Can we call doubleValue?", 
   2.169 +            Numbers.class, "seven__DI", 
   2.170 +            Double.valueOf(7.0), 3
   2.171 +        );
   2.172 +    }
   2.173 +    @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
   2.174 +        assertExec("Can we call doubleValue?", 
   2.175 +            Numbers.class, "seven__DI", 
   2.176 +            Double.valueOf(7.0), 4
   2.177 +        );
   2.178 +    }
   2.179 +    @Test public void valueOfNumber() throws Exception {
   2.180 +        assertExec("Can we call JavaScripts valueOf?", 
   2.181 +            Numbers.class, "seven__DI", 
   2.182 +            Double.valueOf(7.0), 8
   2.183 +        );
   2.184 +    }
   2.185 +    @Test public void valueOfLongNumber() throws Exception {
   2.186 +        assertExec("Can we call JavaScripts valueOf?", 
   2.187 +            Numbers.class, "seven__DI", 
   2.188 +            Double.valueOf(Long.MAX_VALUE / 5), 9
   2.189 +        );
   2.190 +    }
   2.191 +    @Test public void valueOfLongCharA() throws Exception {
   2.192 +        assertExec("Can we call JavaScripts valueOf on Character?", 
   2.193 +            Numbers.class, "seven__DI", 
   2.194 +            Double.valueOf('A'), 65
   2.195 +        );
   2.196 +    }
   2.197 +
   2.198 +    @Test public void valueOfLongBooleanTrue() throws Exception {
   2.199 +        assertExec("Can we call JavaScripts valueOf on Boolean?", 
   2.200 +            Numbers.class, "bseven__ZI", 
   2.201 +            true, 31
   2.202 +        );
   2.203 +    }
   2.204 +    @Test public void valueOfLongBooleanFalse() throws Exception {
   2.205 +        assertExec("Can we call JavaScripts valueOf on Boolean?", 
   2.206 +            Numbers.class, "bseven__ZI", 
   2.207 +            false, 30
   2.208 +        );
   2.209 +    }
   2.210 +
   2.211 +    private static TestVM code;
   2.212 +
   2.213 +    @BeforeClass
   2.214 +    public static void compileTheCode() throws Exception {
   2.215 +        code = TestVM.compileClassAsExtension(null, null, "org/apidesign/vm4brwsr/Numbers");
   2.216 +    }
   2.217 +    @AfterClass
   2.218 +    public static void releaseTheCode() {
   2.219 +        code = null;
   2.220 +    }
   2.221 +
   2.222 +    private static void assertExec(
   2.223 +        String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   2.224 +    {
   2.225 +        Object ret = code.execCode(msg, clazz, method, expRes, args);
   2.226 +        if (ret == null) {
   2.227 +            return;
   2.228 +        }
   2.229 +        if (expRes instanceof Double && ret instanceof Double) {
   2.230 +            double expD = ((Double)expRes).doubleValue();
   2.231 +            double retD = ((Double)ret).doubleValue();
   2.232 +            assertEquals(retD, expD, 0.000004, msg + " "
   2.233 +                    + code.toString());
   2.234 +            return;
   2.235 +        }
   2.236 +        assertEquals(ret, expRes, msg + " " + code.toString());
   2.237 +    }
   2.238 +
   2.239 +
   2.240 +    
   2.241 +}
     3.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Fri Apr 25 08:14:12 2014 +0200
     3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Fri Apr 25 09:59:48 2014 +0200
     3.3 @@ -125,6 +125,34 @@
     3.4              return null;
     3.5          }
     3.6      }
     3.7 +    
     3.8 +    static TestVM compileClassAsExtension(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
     3.9 +        if (sb == null) {
    3.10 +            sb = new StringBuilder();
    3.11 +        }
    3.12 +        Bck2Brwsr.generate(sb, new EmulationResources());
    3.13 +        Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
    3.14 +            resources(new EmulationResources()).
    3.15 +            addRootClasses(names).
    3.16 +            extension(true);
    3.17 +        b2b.generate(sb);
    3.18 +        ScriptEngineManager sem = new ScriptEngineManager();
    3.19 +        ScriptEngine js = sem.getEngineByExtension("js");
    3.20 +        if (eng != null) {
    3.21 +            eng[0] = js;
    3.22 +        }
    3.23 +        try {
    3.24 +            Object res = js.eval(sb.toString());
    3.25 +            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    3.26 +            return new TestVM((Invocable) js, sb);
    3.27 +        } catch (Exception ex) {
    3.28 +            if (sb.length() > 2000) {
    3.29 +                sb = dumpJS(sb);
    3.30 +            }
    3.31 +            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
    3.32 +            return null;
    3.33 +        }
    3.34 +    }
    3.35  
    3.36      Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
    3.37          return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());