rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 18 Mar 2013 09:23:45 +0100
changeset 857 43084920090b
parent 789 bb7506513353
child 862 467ae5eb67f0
permissions -rw-r--r--
Is the gt = value correct?
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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.vm4brwsr;
    19 
    20 import java.io.File;
    21 import java.io.FileWriter;
    22 import java.io.IOException;
    23 import static org.testng.Assert.*;
    24 import org.testng.annotations.AfterClass;
    25 import org.testng.annotations.BeforeClass;
    26 import org.testng.annotations.Test;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class VMinVMTest {
    33     private static TestVM code;
    34     
    35     @Test public void compareGeneratedCodeForArrayClass() throws Exception {
    36         compareCode("org/apidesign/vm4brwsr/Array.class");
    37     }
    38 
    39     @Test public void compareGeneratedCodeForClassesClass() throws Exception {
    40         compareCode("org/apidesign/vm4brwsr/Classes.class");
    41     }
    42 
    43     @Test public void compareGeneratedCodeForToolkitClass() throws Exception {
    44         String genCode = compareCode("org/apidesign/vm4brwsr/Bck2BrwsrToolkit.class");
    45         int indx = genCode.indexOf("gt = 65604");
    46         if (indx >= 0) {
    47             fail("Goto to an invalid label:\n...." + genCode.substring(indx - 30, indx + 30) + "....");
    48         }
    49         fail(genCode);
    50     }
    51 
    52     @BeforeClass
    53     public static void compileTheCode() throws Exception {
    54         code = TestVM.compileClass("org/apidesign/vm4brwsr/VMinVM");
    55     }
    56     @AfterClass
    57     public static void releaseTheCode() {
    58         code = null;
    59     }
    60     
    61     private String compareCode(final String nm) throws Exception, IOException {
    62         byte[] arr = BytesLoader.readClass(nm);
    63         String ret1 = VMinVM.toJavaScript(arr);
    64         
    65         Object ret;
    66         try {
    67             ret = code.invokeFunction("bck2brwsr");
    68             ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
    69             ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
    70         } catch (Exception ex) {
    71             File f = File.createTempFile("execution", ".js");
    72             FileWriter w = new FileWriter(f);
    73             w.append("var byteCode = [\n  ");
    74             String sep = "";
    75             for (int i = 0; i < arr.length; i++) {
    76                 w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
    77                 sep = ", ";
    78                 if (i % 20 == 0) {
    79                     w.append("\n  ");
    80                 }
    81             }
    82             w.append("\n];\n");
    83             w.append(code.toString());
    84             w.close();
    85             throw new Exception(ex.getMessage() + " file: " + f, ex);
    86         }
    87 
    88         
    89         assertTrue(ret instanceof String, "It is string: " + ret);
    90         
    91         if (!ret1.toString().equals(ret)) {
    92             StringBuilder msg = new StringBuilder("Difference found between ");
    93             msg.append(TestVM.dumpJS(ret1));
    94             msg.append(" ");
    95             msg.append(TestVM.dumpJS((CharSequence) ret));
    96             msg.append(" compiled by ");
    97             msg.append(code.toString());
    98             fail(msg.toString());
    99         }
   100         
   101         return ret1;
   102     }
   103 }