Intersecting Rectangles and Coverage Test Requirements

This example shows the application of coverage analysis to a simple design problem and compares the coverage requirements for different metrics.

Example Model

In this example, use a MATLAB® Function within a Stateflow® model as a framework to execute a simple algorithm for determining when two rectangles intersect.

The algorithm uses coordinates for the rectangles in the form of an origin, a height, and width combined together in a four element vector. The algorithm takes two rectangle vectors as inputs and returns a single Boolean output to indicate if the rectangles intersect.

The logic of this algorithm is implemented in the function rct_intersect. The first if statement checks for Y axis overlap, that is, if the first rectangle is completely below the second or the second is completely below the first. The second if statement checks for X axis overlap, that is, if the first rectangle is completely to the right of the second or the second is completely to the right of the first.

Both Y and X axis overlap must be present for the rectangles to intersect. If Y axis overlap is not present X axis overlap is not checked. This logic can be structured using only a single if statement or broken into the following if statements.

Statement Coverage Requirements

The test requirements to achieve a particular level of coverage are strongly dependent on the structure of the coded implementation. Complete statement coverage requires at least two tests. One test should not overlap in the Y axis resulting in execution of the first out = 0; statement. The other test should overlap in the Y axis but not in the X axis resulting in execution of the second out = 0; statement.

This model includes a simple visualization function to show the relative positions of both of the rectangles used in each test case.

Scroll through the HTML report to the coverage information displayed for the MATLAB function.

Decision Coverage Requirements

Full decision coverage requires a true and a false outcome for each if statement. Full decision coverage executes the else statement for each if statement (explicit or implied), which improves the likelihood of finding errors.

This example requires three test cases for full decision coverage. A single test case tests the true outcome for the top level if statement. Two test cases test the true and false outcomes for the nested if statement when the top level if statement is false.

Notice that both if keywords are colored green because they have both been tested true and false.

Condition and MCDC Coverage Requirements

Full condition and MCDC coverage require 5 test cases. This level of coverage is less sensitive to the modifications that result when if statements are replaced with && and operators in the conditions. The coverage requirements to fully satisfy this metric would be the same even if there were only a single if statement.

These test cases have a clear symmetry with the two test cases for statement coverage. Each is an X or Y axis mirror image of a statement coverage test case. This ensures independent verification of both conditions in each of the if statements.

Notice that the entire expression for each if statement is colored green.

Close the example model.