rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/LoopControlTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 11 Sep 2015 14:51:09 +0200
branchflow
changeset 1842 dd4dabfead82
parent 1818 21089a85f02b
permissions -rw-r--r--
Giving the flow analyzer chance to generate the whole function body
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 package org.apidesign.bck2brwsr.flow;
    19 
    20 import java.io.IOException;
    21 import javax.script.ScriptEngine;
    22 import javax.script.ScriptEngineManager;
    23 import javax.script.ScriptException;
    24 import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
    25 import static org.testng.Assert.*;
    26 import org.testng.annotations.AfterClass;
    27 import org.testng.annotations.AfterMethod;
    28 import org.testng.annotations.BeforeClass;
    29 import org.testng.annotations.BeforeMethod;
    30 import org.testng.annotations.Test;
    31 
    32 /**
    33  *
    34  * @author Jaroslav Tulach
    35  */
    36 public class LoopControlTest {
    37     private static TestVM vm;
    38     
    39     public LoopControlTest() {
    40     }
    41 
    42     @BeforeClass
    43     public static void setUpClass() throws Exception {
    44         class MyFlow implements Flow.Analyzer {
    45             boolean called;
    46             @Override
    47             public boolean analyze(Flow request) throws IOException {
    48                 if (
    49                     request.getMethodName().equals("simpleLoopTest") ||
    50                     request.getMethodName().equals("simpleLoopTestWithExit") ||
    51                     false
    52                 ) {
    53                     called = true;
    54                     return GraalFlowAnalyzer.getDefault().analyze(request);
    55                 } else {
    56                     return false;
    57                 }
    58             }
    59         }
    60         MyFlow flow = new MyFlow();
    61         vm = TestVM.compileClass(null, flow, LoopControl.class.getName().replace('.', '/'));
    62         assertTrue(flow.called, "We have been consuted about at least one method");
    63     }
    64 
    65     @AfterClass
    66     public static void tearDownClass() throws Exception {
    67         vm = null;
    68     }
    69 
    70     @BeforeMethod
    71     public void setUpMethod() throws Exception {
    72     }
    73 
    74     @AfterMethod
    75     public void tearDownMethod() throws Exception {
    76     }
    77 
    78     @Test
    79     public void testSimpleLoopTest() throws Exception {
    80         assertFlowControl("simpleLoopTest__II", LoopControl.simpleLoopTest(123));
    81     }
    82 
    83 //   David, this test is broken due to wrong offsets of instructions
    84 //    @Test
    85 //    public void testSimpleLoopWithExitTest() throws Exception {
    86 //        assertFlowControl("simpleLoopTestWithExit__II", LoopControl.simpleLoopTestWithExit(123));
    87 //    }
    88     
    89     private void assertFlowControl(final String mangledSig, int exp) throws Exception {
    90         String code = vm.codeSeq().toString();
    91         int begin = code.indexOf(mangledSig + " = function");
    92         assertNotEquals(begin, -1, "Control loop defined for " + mangledSig + " in:\n" + code);
    93         int end = code.indexOf("m.access = ", begin);
    94         assertNotEquals(end, -1, "Control loop end defined" + code);
    95         final String body = code.substring(begin, end);
    96         assertFalse(body.contains("gt"), "No gt control flow used: " + body);
    97         
    98         ScriptEngine eng = new ScriptEngineManager().getEngineByMimeType("text/javascript");
    99         try {
   100             eng.eval(body);
   101         } catch (ScriptException ex) {
   102             fail("Cannot parse:\n" + body, ex);
   103         }
   104         
   105         vm.assertExec("Is the code compilable?", LoopControl.class, mangledSig, exp, 123);
   106     }
   107     
   108 }