jaroslav@1812: /** jaroslav@1812: * Back 2 Browser Bytecode Translator jaroslav@1812: * Copyright (C) 2012-2015 Jaroslav Tulach jaroslav@1812: * jaroslav@1812: * This program is free software: you can redistribute it and/or modify jaroslav@1812: * it under the terms of the GNU General Public License as published by jaroslav@1812: * the Free Software Foundation, version 2 of the License. jaroslav@1812: * jaroslav@1812: * This program is distributed in the hope that it will be useful, jaroslav@1812: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1812: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1812: * GNU General Public License for more details. jaroslav@1812: * jaroslav@1812: * You should have received a copy of the GNU General Public License jaroslav@1812: * along with this program. Look for COPYING file in the top folder. jaroslav@1812: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1812: */ jaroslav@1812: package org.apidesign.bck2brwsr.flow; jaroslav@1812: jaroslav@1842: import java.io.IOException; jaroslav@1818: import javax.script.ScriptEngine; jaroslav@1818: import javax.script.ScriptEngineManager; jaroslav@1818: import javax.script.ScriptException; jaroslav@1812: import org.apidesign.vm4brwsr.Bck2Brwsr.Flow; jaroslav@1812: import static org.testng.Assert.*; jaroslav@1812: import org.testng.annotations.AfterClass; jaroslav@1812: import org.testng.annotations.AfterMethod; jaroslav@1812: import org.testng.annotations.BeforeClass; jaroslav@1812: import org.testng.annotations.BeforeMethod; jaroslav@1812: import org.testng.annotations.Test; jaroslav@1812: jaroslav@1812: /** jaroslav@1812: * jaroslav@1812: * @author Jaroslav Tulach jaroslav@1812: */ jaroslav@1812: public class LoopControlTest { jaroslav@1812: private static TestVM vm; jaroslav@1812: jaroslav@1812: public LoopControlTest() { jaroslav@1812: } jaroslav@1812: jaroslav@1812: @BeforeClass jaroslav@1812: public static void setUpClass() throws Exception { jaroslav@1812: class MyFlow implements Flow.Analyzer { jaroslav@1812: boolean called; jaroslav@1812: @Override jaroslav@1842: public boolean analyze(Flow request) throws IOException { jaroslav@1818: if ( jaroslav@1818: request.getMethodName().equals("simpleLoopTest") || jaroslav@1818: request.getMethodName().equals("simpleLoopTestWithExit") || jaroslav@1818: false jaroslav@1818: ) { jaroslav@1815: called = true; jaroslav@1815: return GraalFlowAnalyzer.getDefault().analyze(request); jaroslav@1815: } else { jaroslav@1815: return false; jaroslav@1815: } jaroslav@1812: } jaroslav@1812: } jaroslav@1812: MyFlow flow = new MyFlow(); jaroslav@1812: vm = TestVM.compileClass(null, flow, LoopControl.class.getName().replace('.', '/')); jaroslav@1812: assertTrue(flow.called, "We have been consuted about at least one method"); jaroslav@1812: } jaroslav@1812: jaroslav@1812: @AfterClass jaroslav@1812: public static void tearDownClass() throws Exception { jaroslav@1812: vm = null; jaroslav@1812: } jaroslav@1812: jaroslav@1812: @BeforeMethod jaroslav@1812: public void setUpMethod() throws Exception { jaroslav@1812: } jaroslav@1812: jaroslav@1812: @AfterMethod jaroslav@1812: public void tearDownMethod() throws Exception { jaroslav@1812: } jaroslav@1812: jaroslav@1812: @Test jaroslav@1818: public void testSimpleLoopTest() throws Exception { jaroslav@1818: assertFlowControl("simpleLoopTest__II", LoopControl.simpleLoopTest(123)); jaroslav@1818: } jaroslav@1818: jaroslav@1818: // David, this test is broken due to wrong offsets of instructions jaroslav@1818: // @Test jaroslav@1818: // public void testSimpleLoopWithExitTest() throws Exception { jaroslav@1818: // assertFlowControl("simpleLoopTestWithExit__II", LoopControl.simpleLoopTestWithExit(123)); jaroslav@1818: // } jaroslav@1818: jaroslav@1818: private void assertFlowControl(final String mangledSig, int exp) throws Exception { jaroslav@1812: String code = vm.codeSeq().toString(); jaroslav@1818: int begin = code.indexOf(mangledSig + " = function"); jaroslav@1818: assertNotEquals(begin, -1, "Control loop defined for " + mangledSig + " in:\n" + code); jaroslav@1812: int end = code.indexOf("m.access = ", begin); jaroslav@1812: assertNotEquals(end, -1, "Control loop end defined" + code); jaroslav@1812: final String body = code.substring(begin, end); jaroslav@1812: assertFalse(body.contains("gt"), "No gt control flow used: " + body); jaroslav@1815: jaroslav@1818: ScriptEngine eng = new ScriptEngineManager().getEngineByMimeType("text/javascript"); jaroslav@1818: try { jaroslav@1818: eng.eval(body); jaroslav@1818: } catch (ScriptException ex) { jaroslav@1818: fail("Cannot parse:\n" + body, ex); jaroslav@1818: } jaroslav@1818: jaroslav@1818: vm.assertExec("Is the code compilable?", LoopControl.class, mangledSig, exp, 123); jaroslav@1812: } jaroslav@1812: jaroslav@1812: }