jaroslav@1812
|
1 |
/**
|
jaroslav@1812
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1812
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1812
|
4 |
*
|
jaroslav@1812
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1812
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1812
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1812
|
8 |
*
|
jaroslav@1812
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1812
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1812
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1812
|
12 |
* GNU General Public License for more details.
|
jaroslav@1812
|
13 |
*
|
jaroslav@1812
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1812
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1812
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1812
|
17 |
*/
|
jaroslav@1812
|
18 |
package org.apidesign.bck2brwsr.flow;
|
jaroslav@1812
|
19 |
|
jaroslav@1842
|
20 |
import java.io.IOException;
|
jaroslav@1818
|
21 |
import javax.script.ScriptEngine;
|
jaroslav@1818
|
22 |
import javax.script.ScriptEngineManager;
|
jaroslav@1818
|
23 |
import javax.script.ScriptException;
|
jaroslav@1812
|
24 |
import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
|
jaroslav@1812
|
25 |
import static org.testng.Assert.*;
|
jaroslav@1812
|
26 |
import org.testng.annotations.AfterClass;
|
jaroslav@1812
|
27 |
import org.testng.annotations.AfterMethod;
|
jaroslav@1812
|
28 |
import org.testng.annotations.BeforeClass;
|
jaroslav@1812
|
29 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@1812
|
30 |
import org.testng.annotations.Test;
|
jaroslav@1812
|
31 |
|
jaroslav@1812
|
32 |
/**
|
jaroslav@1812
|
33 |
*
|
jaroslav@1812
|
34 |
* @author Jaroslav Tulach
|
jaroslav@1812
|
35 |
*/
|
jaroslav@1812
|
36 |
public class LoopControlTest {
|
jaroslav@1812
|
37 |
private static TestVM vm;
|
jaroslav@1812
|
38 |
|
jaroslav@1812
|
39 |
public LoopControlTest() {
|
jaroslav@1812
|
40 |
}
|
jaroslav@1812
|
41 |
|
jaroslav@1812
|
42 |
@BeforeClass
|
jaroslav@1812
|
43 |
public static void setUpClass() throws Exception {
|
jaroslav@1812
|
44 |
class MyFlow implements Flow.Analyzer {
|
jaroslav@1812
|
45 |
boolean called;
|
jaroslav@1812
|
46 |
@Override
|
jaroslav@1842
|
47 |
public boolean analyze(Flow request) throws IOException {
|
jaroslav@1818
|
48 |
if (
|
jaroslav@1818
|
49 |
request.getMethodName().equals("simpleLoopTest") ||
|
jaroslav@1818
|
50 |
request.getMethodName().equals("simpleLoopTestWithExit") ||
|
jaroslav@1818
|
51 |
false
|
jaroslav@1818
|
52 |
) {
|
jaroslav@1815
|
53 |
called = true;
|
jaroslav@1815
|
54 |
return GraalFlowAnalyzer.getDefault().analyze(request);
|
jaroslav@1815
|
55 |
} else {
|
jaroslav@1815
|
56 |
return false;
|
jaroslav@1815
|
57 |
}
|
jaroslav@1812
|
58 |
}
|
jaroslav@1812
|
59 |
}
|
jaroslav@1812
|
60 |
MyFlow flow = new MyFlow();
|
jaroslav@1812
|
61 |
vm = TestVM.compileClass(null, flow, LoopControl.class.getName().replace('.', '/'));
|
jaroslav@1812
|
62 |
assertTrue(flow.called, "We have been consuted about at least one method");
|
jaroslav@1812
|
63 |
}
|
jaroslav@1812
|
64 |
|
jaroslav@1812
|
65 |
@AfterClass
|
jaroslav@1812
|
66 |
public static void tearDownClass() throws Exception {
|
jaroslav@1812
|
67 |
vm = null;
|
jaroslav@1812
|
68 |
}
|
jaroslav@1812
|
69 |
|
jaroslav@1812
|
70 |
@BeforeMethod
|
jaroslav@1812
|
71 |
public void setUpMethod() throws Exception {
|
jaroslav@1812
|
72 |
}
|
jaroslav@1812
|
73 |
|
jaroslav@1812
|
74 |
@AfterMethod
|
jaroslav@1812
|
75 |
public void tearDownMethod() throws Exception {
|
jaroslav@1812
|
76 |
}
|
jaroslav@1812
|
77 |
|
jaroslav@1812
|
78 |
@Test
|
jaroslav@1818
|
79 |
public void testSimpleLoopTest() throws Exception {
|
jaroslav@1818
|
80 |
assertFlowControl("simpleLoopTest__II", LoopControl.simpleLoopTest(123));
|
jaroslav@1818
|
81 |
}
|
jaroslav@1818
|
82 |
|
jaroslav@1818
|
83 |
// David, this test is broken due to wrong offsets of instructions
|
jaroslav@1818
|
84 |
// @Test
|
jaroslav@1818
|
85 |
// public void testSimpleLoopWithExitTest() throws Exception {
|
jaroslav@1818
|
86 |
// assertFlowControl("simpleLoopTestWithExit__II", LoopControl.simpleLoopTestWithExit(123));
|
jaroslav@1818
|
87 |
// }
|
jaroslav@1818
|
88 |
|
jaroslav@1818
|
89 |
private void assertFlowControl(final String mangledSig, int exp) throws Exception {
|
jaroslav@1812
|
90 |
String code = vm.codeSeq().toString();
|
jaroslav@1818
|
91 |
int begin = code.indexOf(mangledSig + " = function");
|
jaroslav@1818
|
92 |
assertNotEquals(begin, -1, "Control loop defined for " + mangledSig + " in:\n" + code);
|
jaroslav@1812
|
93 |
int end = code.indexOf("m.access = ", begin);
|
jaroslav@1812
|
94 |
assertNotEquals(end, -1, "Control loop end defined" + code);
|
jaroslav@1812
|
95 |
final String body = code.substring(begin, end);
|
jaroslav@1812
|
96 |
assertFalse(body.contains("gt"), "No gt control flow used: " + body);
|
jaroslav@1815
|
97 |
|
jaroslav@1818
|
98 |
ScriptEngine eng = new ScriptEngineManager().getEngineByMimeType("text/javascript");
|
jaroslav@1818
|
99 |
try {
|
jaroslav@1818
|
100 |
eng.eval(body);
|
jaroslav@1818
|
101 |
} catch (ScriptException ex) {
|
jaroslav@1818
|
102 |
fail("Cannot parse:\n" + body, ex);
|
jaroslav@1818
|
103 |
}
|
jaroslav@1818
|
104 |
|
jaroslav@1818
|
105 |
vm.assertExec("Is the code compilable?", LoopControl.class, mangledSig, exp, 123);
|
jaroslav@1812
|
106 |
}
|
jaroslav@1812
|
107 |
|
jaroslav@1812
|
108 |
}
|