JaCoCo 的指令覆盖率:指令数不正确
Instruction Coverage with JaCoCo: Number of instructions is not correct
这是一个简单的测试方法:
public static boolean isPowerOfTwo(final int i) {
return (i > 0) && (i & (i - 1)) == 0;
}
获取.class文件中的字节码我运行javap -c
:
public static boolean isPowerOfTwo(int);
Code:
0: iload_0
1: ifle 14
4: iload_0
5: iload_0
6: iconst_1
7: isub
8: iand
9: ifne 14
12: iconst_1
13: ireturn
14: iconst_0
15: ireturn
可以看到,有16条字节码指令。
现在我运行一个简单的单元测试
@Test
public void testPowerOfTwoInt() {
assertFalse(Util.isPowerOfTwo(Integer.MAX_VALUE));
assertFalse(Util.isPowerOfTwo(0));
assertTrue(Util.isPowerOfTwo(64));
assertTrue(Util.isPowerOfTwo(128));
assertFalse(Util.isPowerOfTwo(-128));
}
但是JaCoCo告诉我,isPowerOfTwo
只包含12字节码指令:
只有 12 个而不是 16 个字节代码指令的原因是什么? class 文件由 Eclipse 编译器生成。 JaCoCo 运行 是否有另一个 class 文件?
再次查看您的输出 - javap
:
只报告了 12 条指令
public static boolean isPowerOfTwo(int);
Code:
0: iload_0
1: ifle 14
//There's no 2 or 3 here...
4: iload_0
5: iload_0
6: iconst_1
7: isub
8: iand
9: ifne 14
//and no 10 or 11 here.
12: iconst_1
13: ireturn
14: iconst_0
15: ireturn
这是一个简单的测试方法:
public static boolean isPowerOfTwo(final int i) {
return (i > 0) && (i & (i - 1)) == 0;
}
获取.class文件中的字节码我运行javap -c
:
public static boolean isPowerOfTwo(int);
Code:
0: iload_0
1: ifle 14
4: iload_0
5: iload_0
6: iconst_1
7: isub
8: iand
9: ifne 14
12: iconst_1
13: ireturn
14: iconst_0
15: ireturn
可以看到,有16条字节码指令。
现在我运行一个简单的单元测试
@Test
public void testPowerOfTwoInt() {
assertFalse(Util.isPowerOfTwo(Integer.MAX_VALUE));
assertFalse(Util.isPowerOfTwo(0));
assertTrue(Util.isPowerOfTwo(64));
assertTrue(Util.isPowerOfTwo(128));
assertFalse(Util.isPowerOfTwo(-128));
}
但是JaCoCo告诉我,isPowerOfTwo
只包含12字节码指令:
只有 12 个而不是 16 个字节代码指令的原因是什么? class 文件由 Eclipse 编译器生成。 JaCoCo 运行 是否有另一个 class 文件?
再次查看您的输出 - javap
:
public static boolean isPowerOfTwo(int);
Code:
0: iload_0
1: ifle 14
//There's no 2 or 3 here...
4: iload_0
5: iload_0
6: iconst_1
7: isub
8: iand
9: ifne 14
//and no 10 or 11 here.
12: iconst_1
13: ireturn
14: iconst_0
15: ireturn