Skip to content

Commit 88acfee

Browse files
authored
Merge pull request #2049 from Momo-Not-Emo/test/emptySwitchEliminator
Add test case for EmptySwitchEliminator
2 parents a62f684 + b43213a commit 88acfee

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package soot.jimple.toolkits.scalar;
2+
3+
/*-
4+
* #%L
5+
* Soot - a J*va Optimization Framework
6+
* %%
7+
* Copyright (C) 1997 - 2020 Raja Vallee-Rai and others
8+
* %%
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as
11+
* published by the Free Software Foundation, either version 2.1 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Lesser Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Lesser Public
20+
* License along with this program. If not, see
21+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
22+
* #L%
23+
*/
24+
25+
import org.junit.Test;
26+
import soot.*;
27+
import soot.jimple.IntConstant;
28+
import soot.jimple.Jimple;
29+
import soot.jimple.JimpleBody;
30+
import soot.jimple.LookupSwitchStmt;
31+
import soot.jimple.internal.JGotoStmt;
32+
import soot.util.Chain;
33+
34+
import java.util.ArrayList;
35+
import java.util.Collections;
36+
import java.util.Iterator;
37+
38+
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertTrue;
40+
41+
public class EmptySwitchEliminatorTest {
42+
43+
/**
44+
* The test is ported from https://github.com/soot-oss/SootUp/blob/develop/sootup.java.bytecode/src/test/java/sootup/java/bytecode/interceptors/EmptySwitchEliminatorTest.java
45+
*/
46+
@Test
47+
public void testEmptySwitch() {
48+
// build test method and body
49+
SootClass cl = new SootClass("TestClass", Modifier.PUBLIC);
50+
SootMethod method = new SootMethod("test", Collections.emptyList(), VoidType.v(), Modifier.PUBLIC);
51+
cl.addMethod(method);
52+
JimpleBody body = Jimple.v().newBody(method);
53+
method.setActiveBody(body);
54+
55+
// build locals
56+
Local l0 = Jimple.v().newLocal("l0", IntType.v());
57+
Local l1 = Jimple.v().newLocal("l1", IntType.v());
58+
Local l2 = Jimple.v().newLocal("l2", IntType.v());
59+
Chain<Local> locals = body.getLocals();
60+
locals.add(l0);
61+
locals.add(l1);
62+
locals.add(l2);
63+
64+
// build statements
65+
Unit startingStmt = Jimple.v().newIdentityStmt(l0, Jimple.v().newThisRef(method.getDeclaringClass().getType()));
66+
Unit stmt1 = Jimple.v().newAssignStmt(l1, IntConstant.v(3));
67+
Unit defaultStmt = Jimple.v().newAssignStmt(l2, IntConstant.v(0));
68+
LookupSwitchStmt sw = Jimple.v().newLookupSwitchStmt(l1, new ArrayList<>(), new ArrayList<>(), defaultStmt);
69+
Unit ret = Jimple.v().newReturnVoidStmt();
70+
71+
/*
72+
l0 := @this: TestClass
73+
l1 = 3
74+
lookupswitch(l1) {
75+
default:
76+
goto l2 = 0;
77+
}
78+
return
79+
*/
80+
UnitPatchingChain units = body.getUnits();
81+
units.add(startingStmt);
82+
units.add(stmt1);
83+
units.add(sw);
84+
units.add(ret);
85+
86+
// execute transform
87+
EmptySwitchEliminator.v().internalTransform(body, "testPhase", Collections.emptyMap());
88+
89+
// check resulting code (switch should be removed)
90+
/*
91+
Expected:
92+
l0 := @this: TestClass
93+
l1 = 3
94+
goto [?= l2 = 0]
95+
return
96+
*/
97+
Iterator<Unit> it = units.iterator();
98+
assertEquals(startingStmt, it.next());
99+
assertEquals(stmt1, it.next());
100+
Unit stmt3 = it.next();
101+
assertTrue(stmt3 instanceof JGotoStmt);
102+
assertEquals(defaultStmt, ((JGotoStmt) stmt3).getTargetBox().getUnit());
103+
assertEquals(ret, it.next());
104+
}
105+
}

0 commit comments

Comments
 (0)