Infrastructure for comparing runs in Hotspot as well as bck2brwsr VMs. Just use @Compare and CompareVMs.create
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 06 Dec 2012 16:13:03 +0100
changeset 27359b9f9f5364f
parent 272 a6a23aa7a546
child 274 81f6e7778135
child 280 21c0cf272b05
Infrastructure for comparing runs in Hotspot as well as bck2brwsr VMs. Just use @Compare and CompareVMs.create
vm/src/test/java/org/apidesign/vm4brwsr/Compare.java
vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java
vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareHashTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Compare.java	Thu Dec 06 16:13:03 2012 +0100
     1.3 @@ -0,0 +1,36 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import java.lang.annotation.ElementType;
    1.24 +import java.lang.annotation.Retention;
    1.25 +import java.lang.annotation.RetentionPolicy;
    1.26 +import java.lang.annotation.Target;
    1.27 +
    1.28 +/** Can be applied on a method that yields a return value. 
    1.29 + * Together with {@link VMCompare#create} it can be used to write
    1.30 + * methods which are executed in real as well as JavaScript VMs and
    1.31 + * their results are compared.
    1.32 + *
    1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.34 + */
    1.35 +@Retention(RetentionPolicy.RUNTIME)
    1.36 +@Target(ElementType.METHOD)
    1.37 +public @interface Compare {
    1.38 +    
    1.39 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java	Thu Dec 06 16:13:03 2012 +0100
     2.3 @@ -0,0 +1,147 @@
     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 java.lang.reflect.Method;
    2.24 +import javax.script.Invocable;
    2.25 +import javax.script.ScriptEngine;
    2.26 +import javax.script.ScriptEngineManager;
    2.27 +import org.testng.Assert;
    2.28 +import org.testng.ITest;
    2.29 +import org.testng.annotations.Factory;
    2.30 +import org.testng.annotations.Test;
    2.31 +
    2.32 +/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    2.33 + * in provided class and builds set of tests that compare the computations
    2.34 + * in real as well as JavaScript virtual machines. Use as:<pre>
    2.35 + * {@code @}{@link Factory} public static create() {
    2.36 + *   return @{link CompareVMs}.{@link #create(YourClass.class);
    2.37 + * }</pre>
    2.38 + *
    2.39 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.40 + */
    2.41 +public final class CompareVMs implements ITest {
    2.42 +    private final Method m;
    2.43 +    private final boolean js;
    2.44 +    private final CompareVMs first, second;
    2.45 +    private Object value;
    2.46 +    private static Invocable code;
    2.47 +    private static CharSequence codeSeq;
    2.48 +    
    2.49 +    private CompareVMs(Method m, boolean js) {
    2.50 +        this.m = m;
    2.51 +        this.js = js;
    2.52 +        this.first = null;
    2.53 +        this.second = null;
    2.54 +    }
    2.55 +
    2.56 +    private CompareVMs(Method m, CompareVMs first, CompareVMs second) {
    2.57 +        this.first = first;
    2.58 +        this.second = second;
    2.59 +        this.m = m;
    2.60 +        this.js = false;
    2.61 +    }
    2.62 +    
    2.63 +    private static void compileTheCode(Class<?> clazz) throws Exception {
    2.64 +        if (code != null) {
    2.65 +            return;
    2.66 +        }
    2.67 +        StringBuilder sb = new StringBuilder();
    2.68 +        class SkipMe extends GenJS {
    2.69 +            public SkipMe(Appendable out) {
    2.70 +                super(out);
    2.71 +            }
    2.72 +
    2.73 +            @Override
    2.74 +            protected boolean requireReference(String cn) {
    2.75 +                if (cn.contains("CompareVMs")) {
    2.76 +                    return true;
    2.77 +                }
    2.78 +                return super.requireReference(cn);
    2.79 +            }
    2.80 +            
    2.81 +            
    2.82 +        }
    2.83 +        SkipMe sm = new SkipMe(sb);
    2.84 +        sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
    2.85 +            clazz.getName().replace('.', '/')
    2.86 +        ));
    2.87 +        
    2.88 +        ScriptEngineManager sem = new ScriptEngineManager();
    2.89 +        ScriptEngine js = sem.getEngineByExtension("js");
    2.90 +        
    2.91 +        Object res = js.eval(sb.toString());
    2.92 +        Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    2.93 +        code = (Invocable)js;
    2.94 +        codeSeq = sb;
    2.95 +    }
    2.96 +    
    2.97 +
    2.98 +    public static Object[] create(Class<?> clazz) {
    2.99 +        Method[] arr = clazz.getMethods();
   2.100 +        Object[] ret = new Object[3 * arr.length];
   2.101 +        int cnt = 0;
   2.102 +        for (Method m : arr) {
   2.103 +            Compare c = m.getAnnotation(Compare.class);
   2.104 +            if (c == null) {
   2.105 +                continue;
   2.106 +            }
   2.107 +            final CompareVMs real = new CompareVMs(m, false);
   2.108 +            final CompareVMs js = new CompareVMs(m, true);
   2.109 +            ret[cnt++] = real;
   2.110 +            ret[cnt++] = js;
   2.111 +            ret[cnt++] = new CompareVMs(m, real, js);
   2.112 +        }
   2.113 +        Object[] r = new Object[cnt];
   2.114 +        for (int i = 0; i < cnt; i++) {
   2.115 +            r[i] = ret[i];
   2.116 +        }
   2.117 +        return r;
   2.118 +    }
   2.119 +    
   2.120 +    @Test public void runTest() throws Throwable {
   2.121 +        if (first != null) {
   2.122 +            Object v1 = first.value;
   2.123 +            Object v2 = second.value;
   2.124 +            if (v1 instanceof Number) {
   2.125 +                v1 = ((Number)v1).doubleValue();
   2.126 +            }
   2.127 +            Assert.assertEquals(v1, v2, "Comparing results");
   2.128 +        } else {
   2.129 +            if (js) {
   2.130 +                try {
   2.131 +                    compileTheCode(m.getDeclaringClass());
   2.132 +                    Object inst = code.invokeFunction(m.getDeclaringClass().getName().replace('.', '_'), false);
   2.133 +                    value = code.invokeMethod(inst, m.getName() + "__I");
   2.134 +                } catch (Exception ex) {
   2.135 +                    throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
   2.136 +                }
   2.137 +            } else {
   2.138 +                value = m.invoke(m.getDeclaringClass().newInstance());
   2.139 +            }
   2.140 +        }
   2.141 +    }
   2.142 +    
   2.143 +    @Override
   2.144 +    public String getTestName() {
   2.145 +        if (first != null) {
   2.146 +            return m.getName() + "[Compare]";
   2.147 +        }
   2.148 +        return m.getName() + (js ? "[JavaScript]" : "[Java]");
   2.149 +    }
   2.150 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareHashTest.java	Thu Dec 06 16:13:03 2012 +0100
     3.3 @@ -0,0 +1,37 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.vm4brwsr.tck;
    3.22 +
    3.23 +import org.apidesign.vm4brwsr.Compare;
    3.24 +import org.apidesign.vm4brwsr.CompareVMs;
    3.25 +import org.testng.annotations.Factory;
    3.26 +
    3.27 +/**
    3.28 + *
    3.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.30 + */
    3.31 +public class CompareHashTest {
    3.32 +    @Compare public int hashOfString() {
    3.33 +        return "Ahoj".hashCode();
    3.34 +    }
    3.35 +    
    3.36 +    @Factory
    3.37 +    public static Object[] create() {
    3.38 +        return CompareVMs.create(CompareHashTest.class);
    3.39 +    }
    3.40 +}