rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1861 433856f897dd
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.bck2brwsr.vmtest.impl;
    19 
    20 import java.lang.annotation.Annotation;
    21 import org.apidesign.bck2brwsr.vmtest.*;
    22 import java.lang.reflect.Method;
    23 import java.util.ArrayList;
    24 import java.util.HashSet;
    25 import java.util.List;
    26 import java.util.Set;
    27 import org.apidesign.bck2brwsr.launcher.Launcher;
    28 import org.testng.Assert;
    29 import org.testng.ITest;
    30 import org.testng.annotations.Factory;
    31 import org.testng.annotations.Test;
    32 
    33 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    34  * in provided class and builds set of tests that compare the computations
    35  * in real as well as JavaScript virtual machines. Use as:<pre>
    36  * {@code @}{@link Factory} public static create() {
    37  *   return @{link VMTest}.{@link #create(YourClass.class);
    38  * }</pre>
    39  *
    40  * @author Jaroslav Tulach <jtulach@netbeans.org>
    41  */
    42 public final class CompareCase implements ITest {
    43     private final Bck2BrwsrCase first, second;
    44     private final Method m;
    45     private final double slowdown;
    46     
    47     private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second, double slowdown) {
    48         this.first = first;
    49         this.second = second;
    50         this.m = m;
    51         this.slowdown = slowdown;
    52     }
    53 
    54     /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
    55      * instances of tests. Each instance runs the test in different virtual
    56      * machine and at the end they compare the results.
    57      * 
    58      * @param clazz the class to inspect
    59      * @return the set of created tests
    60      */
    61     public static Object[] create(String[] brwsr, Class[] classes, Class<? extends Annotation> brwsrTest) {
    62         List<Object> ret = new ArrayList<>();
    63         
    64         final LaunchSetup l = LaunchSetup.INSTANCE;
    65         ret.add(l);
    66         
    67         {
    68             String p = System.getProperty("vmtest.brwsrs");
    69             if (p != null) {
    70                 brwsr = p.split(",");
    71             }
    72         }
    73         
    74         for (Class clazz : classes) {
    75             Method[] arr = clazz.getMethods();
    76             for (Method m : arr) {
    77                 registerCompareCases(m, l, ret, brwsr);
    78                 registerBrwsrCases(brwsrTest, m, l, ret, brwsr);
    79             }
    80         }
    81         return ret.toArray();
    82     }
    83 
    84     /** Test that compares the previous results.
    85      * @throws Throwable 
    86      */
    87     @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
    88         Object v1 = first.value;
    89         Object v2 = second.value;
    90         if (v1 instanceof Integer || v1 instanceof Long || v1 instanceof Byte || v1 instanceof Short) {
    91             try {
    92                 v1 = Long.parseLong(v1.toString());
    93             } catch (NumberFormatException nfe) {
    94                 v1 = "Can't parse " + v1.toString();
    95             }
    96             try {
    97                 v2 = Long.parseLong(v2.toString());
    98             } catch (NullPointerException | NumberFormatException nfe) {
    99                 v2 = "Can't parse " + v2.toString();
   100             }
   101         } else if (v1 instanceof Number) {
   102             try {
   103                 v1 = Double.parseDouble(v1.toString());
   104             } catch (NullPointerException | NumberFormatException nfe) {
   105                 v1 = "Can't parse " + v1.toString();
   106             }
   107             try {
   108                 v2 = Double.parseDouble(v2.toString());
   109             } catch (NullPointerException | NumberFormatException nfe) {
   110                 v2 = "Can't parse " + v2.toString();
   111             }
   112         } else {
   113             if (v1 != null) {
   114                 v1 = v1.toString();
   115             } else {
   116                 v1 = "null";
   117             }
   118         }
   119         try {
   120             Assert.assertEquals(v2, v1, "Comparing results");
   121         } catch (AssertionError e) {
   122             StringBuilder sb = new StringBuilder();
   123             sb.append(e.getMessage());
   124             Bck2BrwsrCase.dumpJS(sb, second);
   125             throw new AssertionError(sb.toString());
   126         }
   127         if (slowdown > 0.0) {
   128             Bck2BrwsrCase slow;
   129             Bck2BrwsrCase fast;
   130             if (first.time >= second.time) {
   131                 slow = second;
   132                 fast = first;
   133             } else {
   134                 fast = second;
   135                 slow = first;
   136             }
   137             if (slow.time * slowdown < fast.time) {
   138                 Assert.fail("Too slow " + slow.getTestName() + " took " + slow.time + " ms vs. " + fast.time + " ms of " + fast.getTestName());
   139             }
   140         }
   141     }
   142     
   143     /** Test name.
   144      * @return name of the tested method followed by a suffix
   145      */
   146     @Override
   147     public String getTestName() {
   148         return m.getName() + "[Compare " + second.typeName() + "]";
   149     }
   150     
   151     private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   152         Compare c = m.getAnnotation(Compare.class);
   153         if (c == null) {
   154             return;
   155         }
   156         String slowdownOverride = System.getProperty("vmtest.slowdown");
   157         final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false, null, null);
   158         ret.add(real);
   159         double slowdown = c.slowdown();
   160         if (slowdown > 0.0 && slowdownOverride != null) {
   161             slowdown = Double.parseDouble(slowdownOverride);
   162         }
   163         Set<Launcher> unique = new HashSet<>();
   164         for (String b : brwsr) {
   165             final Launcher s = l.brwsr(b);
   166             ret.add(s);
   167             unique.add(s);
   168             final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false, null, null);
   169             ret.add(cse);
   170             ret.add(new CompareCase(m, real, cse, slowdown));
   171         }
   172         if (c.scripting()) {
   173             final Launcher jsLauncher = l.javaScript();
   174             if (unique.add(jsLauncher)) {
   175                 final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", jsLauncher, false, null, null);
   176                 ret.add(js);
   177                 ret.add(new CompareCase(m, real, js, slowdown));
   178             }
   179         }
   180     }
   181     private static void registerBrwsrCases(Class<? extends Annotation> brwsrTest, Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   182         Object c = m.getAnnotation(brwsrTest);
   183         if (c == null) {
   184             return;
   185         }
   186         HtmlFragment f = m.getAnnotation(HtmlFragment.class);
   187         if (f == null) {
   188             f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
   189         }
   190         Http.Resource[] r = {};
   191         Http h = m.getAnnotation(Http.class);
   192         if (h == null) {
   193             h = m.getDeclaringClass().getAnnotation(Http.class);
   194             if (h != null) {
   195                 r = h.value();
   196             }
   197         } else {
   198             r = h.value();
   199         }
   200         if (brwsr.length == 0) {
   201             final Launcher s = l.brwsr(null);
   202             ret.add(s);
   203             ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true, f, r));
   204         } else {
   205             for (String b : brwsr) {
   206                 final Launcher s = l.brwsr(b);
   207                 ret.add(s);
   208                 ret.add(new Bck2BrwsrCase(m, b, s, true, f, r));
   209             }
   210         }
   211     }
   212 }