jaroslav@1453: /** jaroslav@1453: * Back 2 Browser Bytecode Translator jaroslav@1453: * Copyright (C) 2012 Jaroslav Tulach jaroslav@1453: * jaroslav@1453: * This program is free software: you can redistribute it and/or modify jaroslav@1453: * it under the terms of the GNU General Public License as published by jaroslav@1453: * the Free Software Foundation, version 2 of the License. jaroslav@1453: * jaroslav@1453: * This program is distributed in the hope that it will be useful, jaroslav@1453: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1453: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1453: * GNU General Public License for more details. jaroslav@1453: * jaroslav@1453: * You should have received a copy of the GNU General Public License jaroslav@1453: * along with this program. Look for COPYING file in the top folder. jaroslav@1453: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1453: */ jaroslav@1453: /* jaroslav@1453: * To change this license header, choose License Headers in Project Properties. jaroslav@1453: * To change this template file, choose Tools | Templates jaroslav@1453: * and open the template in the editor. jaroslav@1453: */ jaroslav@1453: jaroslav@1453: package org.apidesign.vm4brwsr; jaroslav@1453: jaroslav@1457: import java.io.IOException; jaroslav@1457: import java.io.InputStream; jaroslav@1544: import static org.testng.Assert.*; jaroslav@1453: import org.testng.annotations.AfterClass; jaroslav@1453: import org.testng.annotations.BeforeClass; jaroslav@1453: import org.testng.annotations.Test; jaroslav@1453: jaroslav@1453: /** jaroslav@1453: * jaroslav@1453: * @author Jaroslav Tulach jaroslav@1453: */ jaroslav@1453: public class SizeOfAMethodTest { jaroslav@1457: private static String code; jaroslav@1453: jaroslav@1453: @Test public void sumXYShouldBeSmall() { jaroslav@1457: String s = code; jaroslav@1453: int beg = s.indexOf("c.sum__III"); jaroslav@1704: int end = s.indexOf(".access", beg); jaroslav@1453: jaroslav@1457: assertTrue(beg > 0, "Found sum method in " + code); jaroslav@1457: assertTrue(beg < end, "Found end of sum method in " + code); jaroslav@1453: jaroslav@1453: String method = s.substring(beg, end); jaroslav@1453: jaroslav@1457: jaroslav@1457: assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method); jaroslav@1457: } jaroslav@1457: jaroslav@1544: @Test public void betterConstructor() { jaroslav@1544: String s = code; jaroslav@1544: int beg = s.indexOf("c.initInflater__IIZ"); jaroslav@1704: int end = s.indexOf(".access", beg); jaroslav@1544: jaroslav@1544: assertTrue(beg > 0, "Found initInflater method in " + code); jaroslav@1544: assertTrue(beg < end, "Found end of initInflater method in " + code); jaroslav@1544: jaroslav@1544: String method = s.substring(beg, end); jaroslav@1544: jaroslav@1544: assertEquals(method.indexOf("stA1"), -1, "No need for stA1 register:\n" + method); jaroslav@1544: } jaroslav@1544: jaroslav@1545: @Test public void deepConstructor() { jaroslav@1545: String s = code; jaroslav@1545: int beg = s.indexOf("c.intHolder__I"); jaroslav@1704: int end = s.indexOf(".access", beg); jaroslav@1545: jaroslav@1545: assertTrue(beg > 0, "Found intHolder method in " + code); jaroslav@1545: assertTrue(beg < end, "Found end of intHolder method in " + code); jaroslav@1545: jaroslav@1545: String method = s.substring(beg, end); jaroslav@1545: jaroslav@1545: assertEquals(method.indexOf("stA3"), -1, "No need for stA3 register on second constructor:\n" + method); jaroslav@1545: } jaroslav@1545: jaroslav@1457: @Test public void emptyConstructorRequiresNoStack() { jaroslav@1457: String s = code; jaroslav@1457: int beg = s.indexOf("CLS.cons__V"); jaroslav@1704: int end = s.indexOf(".access", beg); jaroslav@1457: jaroslav@1457: assertTrue(beg > 0, "Found constructor in " + code); jaroslav@1457: assertTrue(beg < end, "Found end of constructor in " + code); jaroslav@1457: jaroslav@1457: String method = s.substring(beg, end); jaroslav@1457: method = method.replace("constructor", "CNSTR"); jaroslav@1457: jaroslav@1453: assertEquals(method.indexOf("st"), -1, "There should be no stack operations:\n" + method); jaroslav@1469: assertEquals(method.indexOf("for"), -1, "There should be no for blocks:\n" + method); jaroslav@1453: } jaroslav@1453: jaroslav@1472: @Test public void dontGeneratePrimitiveFinalConstants() { jaroslav@1472: assertEquals(code.indexOf("MISSING_CONSTANT"), -1, "MISSING_CONSTANT field should not be generated"); jaroslav@1472: } jaroslav@1453: jaroslav@1453: @BeforeClass jaroslav@1453: public static void compileTheCode() throws Exception { jaroslav@1457: final String res = "org/apidesign/vm4brwsr/StaticMethod"; jaroslav@1453: StringBuilder sb = new StringBuilder(); jaroslav@1457: class JustStaticMethod implements Bck2Brwsr.Resources { jaroslav@1457: @Override jaroslav@1457: public InputStream get(String resource) throws IOException { jaroslav@1457: final String cn = res + ".class"; jaroslav@1457: if (resource.equals(cn)) { jaroslav@1457: return getClass().getClassLoader().getResourceAsStream(cn); jaroslav@1457: } jaroslav@1457: return null; jaroslav@1457: } jaroslav@1457: } jaroslav@1457: Bck2Brwsr.generate(sb, new JustStaticMethod(), res); jaroslav@1457: code = sb.toString(); jaroslav@1453: } jaroslav@1453: @AfterClass jaroslav@1453: public static void releaseTheCode() { jaroslav@1453: code = null; jaroslav@1453: } jaroslav@1453: jaroslav@1453: }