rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 708 vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java@59d5596a9c6c
child 789 bb7506513353
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
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;
jtulach@169
    23
import static org.testng.Assert.*;
jtulach@169
    24
import org.testng.annotations.BeforeClass;
jtulach@169
    25
import org.testng.annotations.Test;
jtulach@169
    26
jtulach@169
    27
/**
jtulach@169
    28
 *
jtulach@169
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@169
    30
 */
jtulach@169
    31
public class VMinVMTest {
jaroslav@708
    32
    private static TestVM code;
jtulach@169
    33
    
jaroslav@302
    34
    @Test public void compareGeneratedCodeForArrayClass() throws Exception {
jaroslav@334
    35
        compareCode("org/apidesign/vm4brwsr/Array.class");
jaroslav@302
    36
    }
jaroslav@303
    37
jaroslav@303
    38
    @Test public void compareGeneratedCodeForClassesClass() throws Exception {
jaroslav@334
    39
        compareCode("org/apidesign/vm4brwsr/Classes.class");
jaroslav@303
    40
    }
jaroslav@334
    41
jaroslav@302
    42
    @BeforeClass
jaroslav@302
    43
    public void compileTheCode() throws Exception {
jaroslav@708
    44
        code = TestVM.compileClass("org/apidesign/vm4brwsr/VMinVM");
jaroslav@302
    45
    }
jaroslav@302
    46
    
jaroslav@302
    47
    private void compareCode(final String nm) throws Exception, IOException {
jaroslav@334
    48
        byte[] arr = BytesLoader.readClass(nm);
jaroslav@170
    49
        String ret1 = VMinVM.toJavaScript(arr);
jtulach@169
    50
        
jaroslav@170
    51
        Object ret;
jaroslav@170
    52
        try {
jaroslav@279
    53
            ret = code.invokeFunction("bck2brwsr");
jaroslav@279
    54
            ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
jaroslav@248
    55
            ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
jaroslav@170
    56
        } catch (Exception ex) {
jaroslav@170
    57
            File f = File.createTempFile("execution", ".js");
jaroslav@170
    58
            FileWriter w = new FileWriter(f);
jaroslav@173
    59
            w.append("var byteCode = [\n  ");
jaroslav@173
    60
            String sep = "";
jaroslav@173
    61
            for (int i = 0; i < arr.length; i++) {
jaroslav@173
    62
                w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
jaroslav@173
    63
                sep = ", ";
jaroslav@173
    64
                if (i % 20 == 0) {
jaroslav@173
    65
                    w.append("\n  ");
jaroslav@173
    66
                }
jaroslav@173
    67
            }
jaroslav@173
    68
            w.append("\n];\n");
jaroslav@708
    69
            w.append(code.toString());
jaroslav@170
    70
            w.close();
jaroslav@170
    71
            throw new Exception(ex.getMessage() + " file: " + f, ex);
jaroslav@170
    72
        }
jaroslav@170
    73
jaroslav@170
    74
        
jtulach@169
    75
        assertTrue(ret instanceof String, "It is string: " + ret);
jtulach@169
    76
        
jaroslav@302
    77
        if (!ret1.toString().equals(ret)) {
jaroslav@303
    78
            StringBuilder msg = new StringBuilder("Difference found between ");
jaroslav@708
    79
            msg.append(TestVM.dumpJS(ret1));
jaroslav@303
    80
            msg.append(" ");
jaroslav@708
    81
            msg.append(TestVM.dumpJS((CharSequence) ret));
jaroslav@303
    82
            msg.append(" compiled by ");
jaroslav@708
    83
            msg.append(code.toString());
jaroslav@302
    84
            fail(msg.toString());
jaroslav@170
    85
        }
jaroslav@170
    86
    }
jtulach@169
    87
}