vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
branchcanvas
changeset 808 bd8b726902a3
parent 731 e8283f10a127
parent 807 e93506e603ad
child 809 5c61f5e4898e
     1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Thu Feb 14 12:06:16 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,418 +0,0 @@
     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.io.File;
    1.24 -import java.io.FileWriter;
    1.25 -import java.io.IOException;
    1.26 -import java.io.InputStream;
    1.27 -import java.net.URL;
    1.28 -import java.util.Enumeration;
    1.29 -import javax.script.Invocable;
    1.30 -import javax.script.ScriptEngine;
    1.31 -import javax.script.ScriptEngineManager;
    1.32 -import javax.script.ScriptException;
    1.33 -import static org.testng.Assert.*;
    1.34 -import org.testng.annotations.BeforeClass;
    1.35 -import org.testng.annotations.Test;
    1.36 -
    1.37 -/** Checks the basic behavior of the translator.
    1.38 - *
    1.39 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.40 - */
    1.41 -public class StaticMethodTest {
    1.42 -    @Test public void threePlusFour() throws Exception {
    1.43 -        assertExec(
    1.44 -            "Should be seven", 
    1.45 -            StaticMethod.class, "sum__III", 
    1.46 -            Double.valueOf(7), 
    1.47 -            3, 4
    1.48 -        );
    1.49 -    }
    1.50 -
    1.51 -    @Test public void checkReallyInitializedValues() throws Exception {
    1.52 -        assertExec(
    1.53 -            "Return true", 
    1.54 -            StaticMethod.class, "isNull__Z", 
    1.55 -            Double.valueOf(1)
    1.56 -        );
    1.57 -    }
    1.58 -
    1.59 -    @Test public void powerOfThree() throws Exception {
    1.60 -        assertExec(
    1.61 -            "Should be nine", 
    1.62 -            StaticMethod.class, "power__FF", 
    1.63 -            Double.valueOf(9),
    1.64 -            3.0f
    1.65 -        );
    1.66 -    }
    1.67 -
    1.68 -    @Test public void minusOne() throws Exception {
    1.69 -        assertExec(
    1.70 -            "Should be minus one", 
    1.71 -            StaticMethod.class, "minusOne__I", 
    1.72 -            Double.valueOf(-1)
    1.73 -        );
    1.74 -    }
    1.75 -
    1.76 -    @Test public void doubleWithoutLong() throws Exception {
    1.77 -        assertExec(
    1.78 -            "Should be two",
    1.79 -            StaticMethod.class, "minus__DDJ", 
    1.80 -            Double.valueOf(2),
    1.81 -            3.0d, 1l
    1.82 -        );
    1.83 -    }
    1.84 -    
    1.85 -    @Test public void rintNegativeUp() throws Exception {
    1.86 -        final double cnts = -453904.634;
    1.87 -        assertExec(
    1.88 -            "Should round up to end with 5",
    1.89 -            Math.class, "rint__DD", 
    1.90 -            -453905.0, cnts
    1.91 -        );
    1.92 -    }
    1.93 -
    1.94 -    @Test public void rintNegativeDown() throws Exception {
    1.95 -        final double cnts = -453904.434;
    1.96 -        assertExec(
    1.97 -            "Should round up to end with 4",
    1.98 -            Math.class, "rint__DD", 
    1.99 -            -453904.0, cnts
   1.100 -        );
   1.101 -    }
   1.102 -
   1.103 -    @Test public void rintPositiveUp() throws Exception {
   1.104 -        final double cnts = 453904.634;
   1.105 -        assertExec(
   1.106 -            "Should round up to end with 5",
   1.107 -            Math.class, "rint__DD", 
   1.108 -            453905.0, cnts
   1.109 -        );
   1.110 -    }
   1.111 -    @Test public void rintPositiveDown() throws Exception {
   1.112 -        final double cnts = 453904.434;
   1.113 -        assertExec(
   1.114 -            "Should round up to end with 4",
   1.115 -            Math.class, "rint__DD", 
   1.116 -            453904.0, cnts
   1.117 -        );
   1.118 -    }
   1.119 -    @Test public void rintOneHalf() throws Exception {
   1.120 -        final double cnts = 1.5;
   1.121 -        assertExec(
   1.122 -            "Should round up to end with 2",
   1.123 -            Math.class, "rint__DD", 
   1.124 -            2.0, cnts
   1.125 -        );
   1.126 -    }
   1.127 -    @Test public void rintNegativeOneHalf() throws Exception {
   1.128 -        final double cnts = -1.5;
   1.129 -        assertExec(
   1.130 -            "Should round up to end with 2",
   1.131 -            Math.class, "rint__DD", 
   1.132 -            -2.0, cnts
   1.133 -        );
   1.134 -    }
   1.135 -    @Test public void rintTwoAndHalf() throws Exception {
   1.136 -        final double cnts = 2.5;
   1.137 -        assertExec(
   1.138 -            "Should round up to end with 2",
   1.139 -            Math.class, "rint__DD", 
   1.140 -            2.0, cnts
   1.141 -        );
   1.142 -    }
   1.143 -    @Test public void rintNegativeTwoOneHalf() throws Exception {
   1.144 -        final double cnts = -2.5;
   1.145 -        assertExec(
   1.146 -            "Should round up to end with 2",
   1.147 -            Math.class, "rint__DD", 
   1.148 -            -2.0, cnts
   1.149 -        );
   1.150 -    }
   1.151 -
   1.152 -    @Test public void ieeeReminder1() throws Exception {
   1.153 -        assertExec(
   1.154 -            "Same result 1",
   1.155 -            Math.class, "IEEEremainder__DDD", 
   1.156 -            Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
   1.157 -        );
   1.158 -    }
   1.159 -
   1.160 -    @Test public void ieeeReminder2() throws Exception {
   1.161 -        assertExec(
   1.162 -            "Same result 1",
   1.163 -            Math.class, "IEEEremainder__DDD", 
   1.164 -            Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
   1.165 -        );
   1.166 -    }
   1.167 -
   1.168 -    @Test public void divAndRound() throws Exception {
   1.169 -        assertExec(
   1.170 -            "Should be rounded to one",
   1.171 -            StaticMethod.class, "div__IBD", 
   1.172 -            Double.valueOf(1),
   1.173 -            3, 3.75
   1.174 -        );
   1.175 -    }
   1.176 -    @Test public void mixedMethodFourParams() throws Exception {
   1.177 -        assertExec(
   1.178 -            "Should be two",
   1.179 -            StaticMethod.class, "mix__IIJBD", 
   1.180 -            Double.valueOf(20),
   1.181 -            2, 10l, 5, 2.0
   1.182 -        );
   1.183 -    }
   1.184 -    @Test public void factRec() throws Exception {
   1.185 -        assertExec(
   1.186 -            "Factorial of 5 is 120",
   1.187 -            StaticMethod.class, "factRec__JI", 
   1.188 -            Double.valueOf(120),
   1.189 -            5
   1.190 -        );
   1.191 -    }
   1.192 -    @Test public void factIter() throws Exception {
   1.193 -        assertExec(
   1.194 -            "Factorial of 5 is 120",
   1.195 -            StaticMethod.class, "factIter__JI", 
   1.196 -            Double.valueOf(120),
   1.197 -            5
   1.198 -        );
   1.199 -    }
   1.200 -    
   1.201 -    @Test public void xor() throws Exception {
   1.202 -        assertExec(
   1.203 -            "Xor is 4",
   1.204 -            StaticMethod.class, "xor__JIJ",
   1.205 -            Double.valueOf(4),
   1.206 -            7,
   1.207 -            3
   1.208 -        );
   1.209 -    }
   1.210 -    
   1.211 -    @Test public void or() throws Exception {
   1.212 -        assertExec(
   1.213 -            "Or will be 7",
   1.214 -            StaticMethod.class, "orOrAnd__JZII",
   1.215 -            Double.valueOf(7),
   1.216 -            true,
   1.217 -            4,
   1.218 -            3
   1.219 -        );
   1.220 -    }
   1.221 -    @Test public void nullCheck() throws Exception {
   1.222 -        assertExec(
   1.223 -            "Returns nothing",
   1.224 -            StaticMethod.class, "none__Ljava_lang_Object_2II",
   1.225 -            null, 1, 3
   1.226 -        );
   1.227 -    }
   1.228 -    @Test public void and() throws Exception {
   1.229 -        assertExec(
   1.230 -            "And will be 3",
   1.231 -            StaticMethod.class, "orOrAnd__JZII",
   1.232 -            Double.valueOf(3),
   1.233 -            false,
   1.234 -            7,
   1.235 -            3
   1.236 -        );
   1.237 -    }
   1.238 -    @Test public void inc4() throws Exception {
   1.239 -        assertExec(
   1.240 -            "It will be 4",
   1.241 -            StaticMethod.class, "inc4__I",
   1.242 -            Double.valueOf(4)
   1.243 -        );
   1.244 -    }
   1.245 -    
   1.246 -    @Test public void shiftLeftInJava() throws Exception {
   1.247 -        int res = StaticMethod.shiftLeft(1, 8);
   1.248 -        assertEquals(res, 256);
   1.249 -    }
   1.250 -
   1.251 -    @Test public void shiftLeftInJS() throws Exception {
   1.252 -        assertExec(
   1.253 -            "Setting 9th bit",
   1.254 -            StaticMethod.class, "shiftLeft__III",
   1.255 -            Double.valueOf(256),
   1.256 -            1, 8
   1.257 -        );
   1.258 -    }
   1.259 -
   1.260 -    @Test public void shiftRightInJava() throws Exception {
   1.261 -        int res = StaticMethod.shiftArithmRight(-8, 3, true);
   1.262 -        assertEquals(res, -1);
   1.263 -    }
   1.264 -
   1.265 -    @Test public void shiftRightInJS() throws Exception {
   1.266 -        assertExec(
   1.267 -            "Get -1",
   1.268 -            StaticMethod.class, "shiftArithmRight__IIIZ",
   1.269 -            Double.valueOf(-1),
   1.270 -            -8, 3, true
   1.271 -        );
   1.272 -    }
   1.273 -    @Test public void unsignedShiftRightInJava() throws Exception {
   1.274 -        int res = StaticMethod.shiftArithmRight(8, 3, false);
   1.275 -        assertEquals(res, 1);
   1.276 -    }
   1.277 -
   1.278 -    @Test public void unsignedShiftRightInJS() throws Exception {
   1.279 -        assertExec(
   1.280 -            "Get -1",
   1.281 -            StaticMethod.class, "shiftArithmRight__IIIZ",
   1.282 -            Double.valueOf(1),
   1.283 -            8, 3, false
   1.284 -        );
   1.285 -    }
   1.286 -    
   1.287 -    @Test public void javaScriptBody() throws Exception {
   1.288 -        assertExec(
   1.289 -            "JavaScript string",
   1.290 -            StaticMethod.class, "i2s__Ljava_lang_String_2II",
   1.291 -            "333",
   1.292 -            330, 3
   1.293 -        );
   1.294 -    }
   1.295 -    
   1.296 -    @Test public void switchJarda() throws Exception {
   1.297 -        assertExec(
   1.298 -            "The expected value",
   1.299 -            StaticMethod.class, "swtch__Ljava_lang_String_2I",
   1.300 -            "Jarda",
   1.301 -            0
   1.302 -        );
   1.303 -    }
   1.304 -    
   1.305 -    @Test public void switchDarda() throws Exception {
   1.306 -        assertExec(
   1.307 -            "The expected value",
   1.308 -            StaticMethod.class, "swtch__Ljava_lang_String_2I",
   1.309 -            "Darda",
   1.310 -            1
   1.311 -        );
   1.312 -    }
   1.313 -    @Test public void switchParda() throws Exception {
   1.314 -        assertExec(
   1.315 -            "The expected value",
   1.316 -            StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   1.317 -            "Parda",
   1.318 -            22
   1.319 -        );
   1.320 -    }
   1.321 -    @Test public void switchMarda() throws Exception {
   1.322 -        assertExec(
   1.323 -            "The expected value",
   1.324 -            StaticMethod.class, "swtch__Ljava_lang_String_2I",
   1.325 -            "Marda",
   1.326 -            -433
   1.327 -        );
   1.328 -    }
   1.329 -    
   1.330 -    @Test public void checkNullCast() throws Exception {
   1.331 -        assertExec("Null can be cast to any type",
   1.332 -            StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
   1.333 -            null, true
   1.334 -        );
   1.335 -    }
   1.336 -    
   1.337 -    private static CharSequence codeSeq;
   1.338 -    private static Invocable code;
   1.339 -    
   1.340 -    @BeforeClass 
   1.341 -    public void compileTheCode() throws Exception {
   1.342 -        StringBuilder sb = new StringBuilder();
   1.343 -        code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   1.344 -        codeSeq = sb;
   1.345 -    }
   1.346 -    
   1.347 -    
   1.348 -    private static void assertExec(
   1.349 -        String msg, Class clazz, String method, 
   1.350 -        Object expRes, Object... args
   1.351 -    ) throws Exception {
   1.352 -        assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   1.353 -    }
   1.354 -    static void assertExec(
   1.355 -        Invocable toRun, CharSequence theCode,
   1.356 -        String msg, Class clazz, String method, 
   1.357 -        Object expRes, Object... args
   1.358 -    ) throws Exception {
   1.359 -        Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args);
   1.360 -        if (ret == null) {
   1.361 -            return;
   1.362 -        }
   1.363 -        if (expRes != null && expRes.equals(ret)) {
   1.364 -            return;
   1.365 -        }
   1.366 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
   1.367 -        
   1.368 -    }
   1.369 -
   1.370 -    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   1.371 -        return compileClass(sb, null, names);
   1.372 -    }
   1.373 -    static Invocable compileClass(
   1.374 -        StringBuilder sb, ScriptEngine[] eng, String... names
   1.375 -    ) throws ScriptException, IOException {
   1.376 -        if (sb == null) {
   1.377 -            sb = new StringBuilder();
   1.378 -        }
   1.379 -        Bck2Brwsr.generate(sb, new EmulationResources(), names);
   1.380 -        ScriptEngineManager sem = new ScriptEngineManager();
   1.381 -        ScriptEngine js = sem.getEngineByExtension("js");
   1.382 -        if (eng != null) {
   1.383 -            eng[0] = js;
   1.384 -        }
   1.385 -        try {
   1.386 -            Object res = js.eval(sb.toString());
   1.387 -            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   1.388 -            return (Invocable)js;
   1.389 -        } catch (Exception ex) {
   1.390 -            if (sb.length() > 2000) {
   1.391 -                sb = dumpJS(sb);
   1.392 -            }
   1.393 -            fail("Could not evaluate:\n" + sb, ex);
   1.394 -            return null;
   1.395 -        }
   1.396 -    }
   1.397 -    static StringBuilder dumpJS(CharSequence sb) throws IOException {
   1.398 -        File f = File.createTempFile("execution", ".js");
   1.399 -        FileWriter w = new FileWriter(f);
   1.400 -        w.append(sb);
   1.401 -        w.close();
   1.402 -        return new StringBuilder(f.getPath());
   1.403 -    }
   1.404 -    private static class EmulationResources implements Bck2Brwsr.Resources {
   1.405 -        @Override
   1.406 -        public InputStream get(String name) throws IOException {
   1.407 -            Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
   1.408 -            URL u = null;
   1.409 -            while (en.hasMoreElements()) {
   1.410 -                u = en.nextElement();
   1.411 -            }
   1.412 -            if (u == null) {
   1.413 -                throw new IOException("Can't find " + name);
   1.414 -            }
   1.415 -            if (u.toExternalForm().contains("rt.jar!")) {
   1.416 -                throw new IOException("No emulation for " + u);
   1.417 -            }
   1.418 -            return u.openStream();
   1.419 -        }
   1.420 -    }
   1.421 -}