Lighting the main unit - what is the exact definition?

Say I have this piece of C / C ++ code:

int c = 12; // Should I count this line as basic block? if (a != 0 && b > 10) { c += (a + b); } else { c += 1; } printf("%d", c); // Should I count this line as basic block? 

What is the main block coverage for the test case a = 1, b = 12 ?

Is it 75% or 50%?

Should I consider the 1st and last lines to be base? What is the exact definition of the base unit?

Another confusion:

 int c = 16; d += c; 

Is it one base unit or two main blocks? Should each row be considered a base unit?

+6
source share
1 answer

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.

+5
source

Source: https://habr.com/ru/post/905963/


All Articles