rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/LoopControlTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 12 Mar 2015 12:07:54 +0100
branchflow
changeset 1815 fbe883b5a793
parent 1812 4fef6b767f61
child 1818 21089a85f02b
permissions -rw-r--r--
Optimize only simpleLoopTest... method and first check the generated code before compiling it as JavaScript
     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 org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
    21 import static org.testng.Assert.*;
    22 import org.testng.annotations.AfterClass;
    23 import org.testng.annotations.AfterMethod;
    24 import org.testng.annotations.BeforeClass;
    25 import org.testng.annotations.BeforeMethod;
    26 import org.testng.annotations.Test;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach
    31  */
    32 public class LoopControlTest {
    33     private static TestVM vm;
    34     
    35     public LoopControlTest() {
    36     }
    37 
    38     @BeforeClass
    39     public static void setUpClass() throws Exception {
    40         class MyFlow implements Flow.Analyzer {
    41             boolean called;
    42             @Override
    43             public boolean analyze(Flow request) {
    44                 if (request.getMethodName().equals("simpleLoopTestWithExit")) {
    45                     called = true;
    46                     return GraalFlowAnalyzer.getDefault().analyze(request);
    47                 } else {
    48                     return false;
    49                 }
    50             }
    51         }
    52         MyFlow flow = new MyFlow();
    53         vm = TestVM.compileClass(null, flow, LoopControl.class.getName().replace('.', '/'));
    54         assertTrue(flow.called, "We have been consuted about at least one method");
    55     }
    56 
    57     @AfterClass
    58     public static void tearDownClass() throws Exception {
    59         vm = null;
    60     }
    61 
    62     @BeforeMethod
    63     public void setUpMethod() throws Exception {
    64     }
    65 
    66     @AfterMethod
    67     public void tearDownMethod() throws Exception {
    68     }
    69 
    70     @Test
    71     public void testExecute() throws Exception {
    72         String code = vm.codeSeq().toString();
    73         int begin = code.indexOf("simpleLoopTestWithExit__II = function");
    74         assertNotEquals(begin, -1, "Control loop defined" + code);
    75         int end = code.indexOf("m.access = ", begin);
    76         assertNotEquals(end, -1, "Control loop end defined" + code);
    77         final String body = code.substring(begin, end);
    78         assertFalse(body.contains("gt"), "No gt control flow used: " + body);
    79         
    80         int exp = LoopControl.simpleLoopTestWithExit(123);
    81         vm.assertExec("Is the code compilable?", LoopControl.class, "simpleLoopTestWithExit__II", exp, 123);
    82     }
    83     
    84 }