rt/vm/src/test/java/org/apidesign/vm4brwsr/SizeOfAMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 07 May 2014 10:45:42 +0200
branchclosure
changeset 1544 f75ee66cfc2f
parent 1472 0a1b4f1bf4d0
child 1545 50991eae72d4
permissions -rw-r--r--
Test to verify constructors don't need special register
     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 /*
    19  * To change this license header, choose License Headers in Project Properties.
    20  * To change this template file, choose Tools | Templates
    21  * and open the template in the editor.
    22  */
    23 
    24 package org.apidesign.vm4brwsr;
    25 
    26 import java.io.IOException;
    27 import java.io.InputStream;
    28 import static org.testng.Assert.*;
    29 import org.testng.annotations.AfterClass;
    30 import org.testng.annotations.BeforeClass;
    31 import org.testng.annotations.Test;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public class SizeOfAMethodTest {
    38     private static String code;
    39 
    40     @Test public void sumXYShouldBeSmall() {
    41         String s = code;
    42         int beg = s.indexOf("c.sum__III");
    43         int end = s.indexOf("c.sum__III.access");
    44         
    45         assertTrue(beg > 0, "Found sum method in " + code);
    46         assertTrue(beg < end, "Found end of sum method in " + code);
    47         
    48         String method = s.substring(beg, end);
    49         
    50         
    51         assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
    52     }
    53 
    54     @Test public void betterConstructor() {
    55         String s = code;
    56         int beg = s.indexOf("c.initInflater__IIZ");
    57         int end = s.indexOf("c.initInflater__IIZ.access");
    58         
    59         assertTrue(beg > 0, "Found initInflater method in " + code);
    60         assertTrue(beg < end, "Found end of initInflater method in " + code);
    61         
    62         String method = s.substring(beg, end);
    63         
    64         assertEquals(method.indexOf("stA1"), -1, "No need for stA1 register:\n" + method);
    65     }
    66 
    67     @Test public void emptyConstructorRequiresNoStack() {
    68         String s = code;
    69         int beg = s.indexOf("CLS.cons__V");
    70         int end = s.indexOf("CLS.cons__V.access");
    71         
    72         assertTrue(beg > 0, "Found constructor in " + code);
    73         assertTrue(beg < end, "Found end of constructor in " + code);
    74         
    75         String method = s.substring(beg, end);
    76         method = method.replace("constructor", "CNSTR");
    77         
    78         assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
    79         assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method);
    80     }
    81     
    82     @Test public void dontGeneratePrimitiveFinalConstants() {
    83         assertEquals(code.indexOf("MISSING_CONSTANT"), -1, "MISSING_CONSTANT field should not be generated");
    84     }
    85     
    86     @BeforeClass 
    87     public static void compileTheCode() throws Exception {
    88         final String res = "org/apidesign/vm4brwsr/StaticMethod";
    89         StringBuilder sb = new StringBuilder();
    90         class JustStaticMethod implements Bck2Brwsr.Resources {
    91             @Override
    92             public InputStream get(String resource) throws IOException {
    93                 final String cn = res + ".class";
    94                 if (resource.equals(cn)) {
    95                     return getClass().getClassLoader().getResourceAsStream(cn);
    96                 }
    97                 return null;
    98             }
    99         }
   100         Bck2Brwsr.generate(sb, new JustStaticMethod(), res);
   101         code = sb.toString();
   102     }
   103     @AfterClass
   104     public static void releaseTheCode() {
   105         code = null;
   106     }
   107     
   108 }