Block coverage
Code Coverage for Ada
When analyzing Ada source code, Code Coverage can provide the following block coverage types:
-
Statement blocks
-
Statement and decision blocks
-
Statement, decision, and loop blocks
-
Asynchronous transfer of control (ATC) blocks
Statement blocks (or simple blocks)
Simple blocks are the main blocks within units as well as blocks introduced by decisions, such as:
-
then and else (elsif) of an if
-
loop...end loop blocks of a for...while
-
exit when...end loop or exit when blocks at the end of an instruction sequence
-
when blocks of a case
-
when blocks of exception processing blocks
-
do...end block of the accept instruction
-
or and else blocks of the select instruction
-
begin...exception blocks of the declare block that contain an exceptions processing block.
-
select...then abort blocks of an ATC statement
-
sequence blocks: instructions found after a potentially terminal statement.
A simple block constitutes one branch. Each unit contains at least one simple block corresponding to its body, except packages that do not contain an initialization block.
Decision coverage (implicit blocks)
An if statement without an else statement introduces an implicit block.
-- Function power_10
-- -block=decision or -block=implicit
function power_10 ( value, max : in integer) return integer is
ret, i : integer ;
begin
if ( value == 0 ) then
return 0;
-- implicit else block
end if ;
for i in 0..9
loop
if ( (max /10) < ret ) then
ret := ret *10 ;
else
ret := max ;
end if ;
end loop ;
return ret;
end ;
An implicit block constitutes one branch.
Implicit blocks refer to simple blocks to describe possible decisions. The Code Coverage report presents the sum of these decisions as an absolute value and a ratio.
Loop coverage (logical blocks)
A for or while loop constitutes three branches:
-
The simple block contained in the loop is never executed: the exit condition is true immediately
-
The simple block is run only once: the exit condition is false, and then true on the next iteration
-
The simple block run at least twice: the exit condition is false at least twice, then finally true)
A loop...end loop block requires only two branches because the exit condition, if it exists, is tested within the loop:
-
The simple block is played only once: the exit condition is true on the first iteration, if the condition exists
-
The simple block is played at least twice: the exit condition false at least once and then finally true, if the condition exists
In the following example, you need to execute the function try_five_times() several times for 100 % coverage of the three logical blocks induced by this while loop.
-- Function try_five_times
function try_five_times return integer is
result, i : integer := 0 ;
begin
-- try is any function
while ( i < 5 ) and then ( result <= 0 ) loop
result := try ;
i := integer'succ(i);
end loop ;
return result;
end ; -- 3 logical blocks
Logical blocks are attached to the loop introduction keyword.
Asynchronous transfer of control (ATC) blocks
This coverage type is specific to the Ada 95 asynchronous transfer of control (ATC) block statement (see your Ada documentation).
The ATC block contains tree branches:
-
Control immediately transferred: The sequence of control never passes through the block then abort /end select, but is immediately transferred to the block select/then abort.
-
Control transferred: The sequence of control starts at the block then abort/end select, but never reaches the end of this block. Because of trigger event appearance, the sequence is transferred to the block select/then abort.
-
Control never transferred: Because the trigger event never appears, the sequence of control starts and reaches the end of the block then abort/end select, and was never transferred to the block select/then abort.
In the following example, you need to execute the compute_done function several times to obtain full coverage of the three ATC blocks induced by the select statement:
function compute_done return boolean is
result : boolean := true ;
begin
-- if computing is not done before 10s ...
select
delay 10.0;
result := false ;
then abort
compute;
end select;
return result;
end ; -- 3 logical blocks
Code Coverage blocks are attached to the Select keyword of the ATC statement.
Related Topics