core: restore simple indexed loops

This commit is contained in:
Skylot
2014-08-30 23:04:33 +04:00
parent ec8309af49
commit 195eeceb62
34 changed files with 403 additions and 62 deletions
@@ -5,15 +5,15 @@ import jadx.core.dex.instructions.IfOp;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.LiteralArg;
import jadx.core.dex.regions.Compare;
import jadx.core.dex.regions.IfCondition;
import jadx.core.dex.regions.conditions.Compare;
import jadx.core.dex.regions.conditions.IfCondition;
import org.junit.Test;
import static jadx.core.dex.regions.IfCondition.Mode;
import static jadx.core.dex.regions.IfCondition.merge;
import static jadx.core.dex.regions.IfCondition.not;
import static jadx.core.dex.regions.IfCondition.simplify;
import static jadx.core.dex.regions.conditions.IfCondition.Mode;
import static jadx.core.dex.regions.conditions.IfCondition.merge;
import static jadx.core.dex.regions.conditions.IfCondition.not;
import static jadx.core.dex.regions.conditions.IfCondition.simplify;
import static org.junit.Assert.assertEquals;
public class TestIfCondition {
@@ -5,7 +5,7 @@ import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static jadx.tests.utils.JadxMatchers.containsOne;
import static org.junit.Assert.assertThat;
public class TestInline2 extends InternalJadxTest {
@@ -30,9 +30,7 @@ public class TestInline2 extends InternalJadxTest {
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsString("i < a.length"));
assertThat(code, containsString("long i2 ="));
assertThat(code, containsString("+ i2"));
assertThat(code, containsString("i2--;"));
assertThat(code, containsOne("for (int i = 0; i < a.length; i++) {"));
assertThat(code, containsOne("for (long i2 = (long) b; i2 > 0; i2--) {"));
}
}
@@ -6,6 +6,7 @@ import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static jadx.tests.utils.JadxMatchers.containsOne;
import static jadx.tests.utils.JadxMatchers.countString;
import static org.junit.Assert.assertThat;
public class TestBreakInLoop extends InternalJadxTest {
@@ -14,13 +15,11 @@ public class TestBreakInLoop extends InternalJadxTest {
private int f;
private void test(int[] a, int b) {
int i = 0;
while (i < a.length) {
for (int i = 0; i < a.length; i++) {
a[i]++;
if (i < b) {
break;
}
i++;
}
this.f++;
}
@@ -32,12 +31,12 @@ public class TestBreakInLoop extends InternalJadxTest {
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsOne("this.f++;"));
assertThat(code, containsOne("for (int i = 0; i < a.length; i++) {"));
// assertThat(code, containsOne("a[i]++;"));
assertThat(code, containsOne("if (i < b) {"));
assertThat(code, containsOne("break;"));
assertThat(code, containsOne("i++;"));
assertThat(code, containsOne("this.f++;"));
// assertThat(code, countString(0, "else"));
assertThat(code, countString(0, "else"));
}
}
@@ -0,0 +1,38 @@
package jadx.tests.internal.loops;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static jadx.tests.utils.JadxMatchers.containsLines;
import static org.junit.Assert.assertThat;
public class TestIndexForLoop extends InternalJadxTest {
public static class TestCls {
private int test(int[] a, int b) {
int sum = 0;
for (int i = 0; i < b; i++) {
sum += a[i];
}
return sum;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsLines(2,
"int sum = 0;",
"for (int i = 0; i < b; i++) {",
indent(1) + "sum += a[i];",
"}",
"return sum;"
));
}
}
@@ -5,6 +5,7 @@ import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static jadx.tests.utils.JadxMatchers.containsOne;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
@@ -33,9 +34,8 @@ public class TestLoopDetection2 extends InternalJadxTest {
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsString("while (i < b) {"));
assertThat(code, containsString("int c = a + b;"));
assertThat(code, containsOne("int c = a + b;"));
assertThat(code, containsOne("for (int i = a; i < b; i++) {"));
assertThat(code, not(containsString("c_2")));
assertThat(code, containsString("i++"));
}
}
@@ -0,0 +1,38 @@
package jadx.tests.internal.loops;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import java.util.List;
import org.junit.Test;
import static jadx.tests.utils.JadxMatchers.containsOne;
import static org.junit.Assert.assertThat;
public class TestNestedLoops2 extends InternalJadxTest {
public static class TestCls {
private boolean test(List<String> list) {
int j = 0;
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
while (j < s.length()) {
j++;
}
}
return j > 10;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsOne("for (int i = 0; i < list.size(); i++) {"));
assertThat(code, containsOne("while (j < ((String) list.get(i)).length()) {"));
}
}