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