rt/vm/src/test/java/org/apidesign/vm4brwsr/SizeOfAMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 15:39:34 +0100
branchReducedStack
changeset 1469 f57fa856ffc4
parent 1457 b9386cc3ff7b
child 1472 0a1b4f1bf4d0
permissions -rw-r--r--
15KB less from 1.4MB by eliminating empty for operations
     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.assertEquals;
    29 import static org.testng.Assert.assertTrue;
    30 import org.testng.annotations.AfterClass;
    31 import org.testng.annotations.BeforeClass;
    32 import org.testng.annotations.Test;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 public class SizeOfAMethodTest {
    39     private static String code;
    40 
    41     @Test public void sumXYShouldBeSmall() {
    42         String s = code;
    43         int beg = s.indexOf("c.sum__III");
    44         int end = s.indexOf("c.sum__III.access");
    45         
    46         assertTrue(beg > 0, "Found sum method in " + code);
    47         assertTrue(beg < end, "Found end of sum method in " + code);
    48         
    49         String method = s.substring(beg, end);
    50         
    51         
    52         assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
    53     }
    54 
    55     @Test public void emptyConstructorRequiresNoStack() {
    56         String s = code;
    57         int beg = s.indexOf("CLS.cons__V");
    58         int end = s.indexOf("CLS.cons__V.access");
    59         
    60         assertTrue(beg > 0, "Found constructor in " + code);
    61         assertTrue(beg < end, "Found end of constructor in " + code);
    62         
    63         String method = s.substring(beg, end);
    64         method = method.replace("constructor", "CNSTR");
    65         
    66         assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method);
    67         assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method);
    68     }
    69     
    70     
    71     @BeforeClass 
    72     public static void compileTheCode() throws Exception {
    73         final String res = "org/apidesign/vm4brwsr/StaticMethod";
    74         StringBuilder sb = new StringBuilder();
    75         class JustStaticMethod implements Bck2Brwsr.Resources {
    76             @Override
    77             public InputStream get(String resource) throws IOException {
    78                 final String cn = res + ".class";
    79                 if (resource.equals(cn)) {
    80                     return getClass().getClassLoader().getResourceAsStream(cn);
    81                 }
    82                 return null;
    83             }
    84         }
    85         Bck2Brwsr.generate(sb, new JustStaticMethod(), res);
    86         code = sb.toString();
    87     }
    88     @AfterClass
    89     public static void releaseTheCode() {
    90         code = null;
    91     }
    92     
    93 }