rt/vm/src/test/java/org/apidesign/vm4brwsr/SizeOfAMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 25 Sep 2014 23:48:41 +0200
changeset 1704 2e69145b9feb
parent 1545 50991eae72d4
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Store just defined member in variable m and use it to assign access, cls and export the member
     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(".access", beg);
    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(".access", beg);
    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 deepConstructor() {
    68         String s = code;
    69         int beg = s.indexOf("c.intHolder__I");
    70         int end = s.indexOf(".access", beg);
    71         
    72         assertTrue(beg > 0, "Found intHolder method in " + code);
    73         assertTrue(beg < end, "Found end of intHolder method in " + code);
    74         
    75         String method = s.substring(beg, end);
    76         
    77         assertEquals(method.indexOf("stA3"), -1, "No need for stA3 register on second constructor:\n" + method);
    78     }
    79 
    80     @Test public void emptyConstructorRequiresNoStack() {
    81         String s = code;
    82         int beg = s.indexOf("CLS.cons__V");
    83         int end = s.indexOf(".access", beg);
    84         
    85         assertTrue(beg > 0, "Found constructor in " + code);
    86         assertTrue(beg < end, "Found end of constructor in " + code);
    87         
    88         String method = s.substring(beg, end);
    89         method = method.replace("constructor", "CNSTR");
    90         
    91         assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
    92         assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method);
    93     }
    94     
    95     @Test public void dontGeneratePrimitiveFinalConstants() {
    96         assertEquals(code.indexOf("MISSING_CONSTANT"), -1, "MISSING_CONSTANT field should not be generated");
    97     }
    98     
    99     @BeforeClass 
   100     public static void compileTheCode() throws Exception {
   101         final String res = "org/apidesign/vm4brwsr/StaticMethod";
   102         StringBuilder sb = new StringBuilder();
   103         class JustStaticMethod implements Bck2Brwsr.Resources {
   104             @Override
   105             public InputStream get(String resource) throws IOException {
   106                 final String cn = res + ".class";
   107                 if (resource.equals(cn)) {
   108                     return getClass().getClassLoader().getResourceAsStream(cn);
   109                 }
   110                 return null;
   111             }
   112         }
   113         Bck2Brwsr.generate(sb, new JustStaticMethod(), res);
   114         code = sb.toString();
   115     }
   116     @AfterClass
   117     public static void releaseTheCode() {
   118         code = null;
   119     }
   120     
   121 }