vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 11 Dec 2012 10:24:00 +0100
branchlazyvm
changeset 302 af9c8df8a660
parent 279 ee34358037b3
child 303 c12342170235
permissions -rw-r--r--
More flexible infrastructure for comparing generated codes
jtulach@169
     1
/**
jtulach@169
     2
 * Back 2 Browser Bytecode Translator
jtulach@169
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@169
     4
 *
jtulach@169
     5
 * This program is free software: you can redistribute it and/or modify
jtulach@169
     6
 * it under the terms of the GNU General Public License as published by
jtulach@169
     7
 * the Free Software Foundation, version 2 of the License.
jtulach@169
     8
 *
jtulach@169
     9
 * This program is distributed in the hope that it will be useful,
jtulach@169
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jtulach@169
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jtulach@169
    12
 * GNU General Public License for more details.
jtulach@169
    13
 *
jtulach@169
    14
 * You should have received a copy of the GNU General Public License
jtulach@169
    15
 * along with this program. Look for COPYING file in the top folder.
jtulach@169
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jtulach@169
    17
 */
jtulach@169
    18
package org.apidesign.vm4brwsr;
jtulach@169
    19
jaroslav@170
    20
import java.io.File;
jaroslav@170
    21
import java.io.FileWriter;
jaroslav@170
    22
import java.io.IOException;
jaroslav@170
    23
import java.io.InputStream;
jtulach@169
    24
import static org.testng.Assert.*;
jtulach@169
    25
import javax.script.Invocable;
jtulach@169
    26
import org.testng.annotations.BeforeClass;
jtulach@169
    27
import org.testng.annotations.Test;
jtulach@169
    28
jtulach@169
    29
/**
jtulach@169
    30
 *
jtulach@169
    31
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@169
    32
 */
jtulach@169
    33
public class VMinVMTest {
jtulach@169
    34
jtulach@169
    35
    private static CharSequence codeSeq;
jtulach@169
    36
    private static Invocable code;
jtulach@169
    37
    
jaroslav@302
    38
    @Test public void compareGeneratedCodeForArrayClass() throws Exception {
jaroslav@302
    39
        compareCode("/org/apidesign/vm4brwsr/Array.class");
jaroslav@302
    40
    }
jaroslav@302
    41
    
jaroslav@302
    42
    @BeforeClass
jaroslav@302
    43
    public void compileTheCode() throws Exception {
jaroslav@302
    44
        StringBuilder sb = new StringBuilder();
jaroslav@302
    45
        code = StaticMethodTest.compileClass(sb, 
jaroslav@302
    46
            "org/apidesign/vm4brwsr/VMinVM"
jaroslav@302
    47
        );
jaroslav@302
    48
        codeSeq = sb;
jaroslav@302
    49
    }
jaroslav@302
    50
    
jaroslav@302
    51
    private static byte[] readClass(String res) throws IOException {
jaroslav@302
    52
        InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
jaroslav@302
    53
        assertNotNull(is1, "Stream found");
jaroslav@302
    54
        byte[] arr = new byte[is1.available()];
jaroslav@302
    55
        int len = is1.read(arr);
jaroslav@302
    56
        is1.close();
jaroslav@302
    57
        if (len != arr.length) {
jaroslav@302
    58
            throw new IOException("Wrong len " + len + " for arr: " + arr.length);
jaroslav@302
    59
        }
jaroslav@302
    60
        return arr;
jaroslav@302
    61
    }
jaroslav@302
    62
jaroslav@302
    63
    private void compareCode(final String nm) throws Exception, IOException {
jaroslav@302
    64
        byte[] arr = readClass(nm);
jaroslav@170
    65
        String ret1 = VMinVM.toJavaScript(arr);
jtulach@169
    66
        
jaroslav@170
    67
        Object ret;
jaroslav@170
    68
        try {
jaroslav@279
    69
            ret = code.invokeFunction("bck2brwsr");
jaroslav@279
    70
            ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
jaroslav@248
    71
            ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
jaroslav@170
    72
        } catch (Exception ex) {
jaroslav@170
    73
            File f = File.createTempFile("execution", ".js");
jaroslav@170
    74
            FileWriter w = new FileWriter(f);
jaroslav@173
    75
            w.append("var byteCode = [\n  ");
jaroslav@173
    76
            String sep = "";
jaroslav@173
    77
            for (int i = 0; i < arr.length; i++) {
jaroslav@173
    78
                w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
jaroslav@173
    79
                sep = ", ";
jaroslav@173
    80
                if (i % 20 == 0) {
jaroslav@173
    81
                    w.append("\n  ");
jaroslav@173
    82
                }
jaroslav@173
    83
            }
jaroslav@173
    84
            w.append("\n];\n");
jaroslav@170
    85
            w.append(codeSeq);
jaroslav@170
    86
            w.close();
jaroslav@170
    87
            throw new Exception(ex.getMessage() + " file: " + f, ex);
jaroslav@170
    88
        }
jaroslav@170
    89
jaroslav@170
    90
        
jtulach@169
    91
        assertTrue(ret instanceof String, "It is string: " + ret);
jtulach@169
    92
        
jaroslav@302
    93
        if (!ret1.toString().equals(ret)) {
jaroslav@302
    94
            StringBuilder msg = StaticMethodTest.dumpJS(ret1);
jaroslav@302
    95
            msg.append(" is not the same as ");
jaroslav@302
    96
            msg.append(StaticMethodTest.dumpJS((CharSequence) ret));
jaroslav@302
    97
            fail(msg.toString());
jaroslav@170
    98
        }
jaroslav@170
    99
    }
jtulach@169
   100
}