The base unit contains all instructions that have a property that, if one of them is executed, all the others in one base unit. Calling the first base unit instruction a leader, we get the following definition of a base unit: a set of all instructions following the leader that are executed if the leader is executed. The leader is the beginning of the base unit.
To determine the leader, find all the instructions for moving to your code. Each goal of the transition and each team after the jump is a leader. The first instruction in the method is also the leader.
To find the base blocks, simply follow all the instructions from the leader to the next.
Your first example:
int c = 12; // Leader if (a != 0 && b > 10) { // Jump c += (a + b); // Leader } else { c += 1; // Leader } printf("%d", c); // Leader -- target of jump from the end of the true branch
You have 4 base blocks: one for each if branch, one for printf after if and one for initializing c . If a == 1 && b == 12 only three base blocks are executed, then the coverage is 75%.
In your second example, there is no jump instruction => there is only one base block.
source share