Vertex
Vertex

Reputation: 2712

Instruction Coverage with JaCoCo: Number of instructions is not correct

This is a simple method to test:

public static boolean isPowerOfTwo(final int i) {
    return (i > 0) && (i & (i - 1)) == 0;
}

To get the byte code I run javap -c on the .class file:

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

As you can see, there are 16 byte code instructions.

Now I run a simple Unit-Test

@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));
}

But JaCoCo tells me, that isPowerOfTwo contains only 12 byte code instructions: Instruction Coverage

What is the reason for only 12 instead of 16 byte code instructions? The class file is generated by the Eclipse compiler. Does JaCoCo runs another class file?

Upvotes: 2

Views: 4554

Answers (1)

Sbodd
Sbodd

Reputation: 11454

Look at your output again - there are only 12 instructions reported by 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

Upvotes: 8

Related Questions