= Coverage Analysis Theory = [[TOC(Developer/Coverage/CoverageAnalysisTheory, depth=2)]] The subject of Code Coverage Analysis is broad and has been written about many times over. This background material is not intented to summerize or rehash what can be read elsewhere. Instead, the focus here will be on the aspects of Code Coverage Analysis as they pertain to the [wiki:TBR/UserManual/RTEMS_Coverage_Analysis RTEMS Coverage Analysis] effort. The ultimate goal of Code Coverage Analysis is to ensure that a test suite adequately tests a particular body of code. In order to achieve this goal, several different coverage criteria may have to be examined. Let's consider the following criteria: * '''Statement Coverage''' - Has each line of the source code been executed? * '''Decision Coverage''' (also known as Branch coverage) - Has each control structure (such as an if statement) evaluated both to true and false? * '''Condition Coverage''' - Has each boolean sub-expression evaluated both to true and false (this does not necessarily imply decision coverage)? * '''Object Coverage''' - Has each line of generated assembly been executed? = Statement Coverage = Statement Coverage requires that each line of source code must be executed. This is often considered the simpliest criteria. The problem is that it only identifies the lines that were executed, and does not consider the logic flow of the code. It can be useful for indentifying "chunks" of code (i.e. new functionality) that are not covered by the test suite, but not much else. = Decision Coverage = Decision Coverage requires that each control structure evaluate to both TRUE and FALSE. This is a pretty good criteria because it generally ensures that both the TRUE and FALSE paths of an expression are covered. However, short-circuit operators will prevent some portions of a complex expression from being evaluated. = Condition Coverage = Condition Coverage requires that each boolean sub-expression evaluate to both TRUE and FALSE. This criteria goes a little further than Decision Coverage by ensuring that the component parts of a compound expression each evaluate to TRUE and FALSE. But it should be noted that this does not necessarily imply decision coverage. This issue usually requires Decision Coverage and Condition Coverage to be applied together. = Object Coverage = Object Coverage requires that each line of generated assembly be executed. This can be a very good general criteria because it ensures test cases for most of what the other criteria ensure. = Criteria Relationships = [[Image(CoverageCategories.png)]]]] Each of these criteria can be used independently to analyze the code in question. Application of any one criteria will likely improve the test suite to some degree albeit at some cost. Examination of the criteria collectively, shows that there are clear relationships between the different criteria as shown in the picture. The completness of the test suite increases as it satifies first Statement Coverage and then Decision Coverage and finally Condition/Decision Coverage. If the test suite satisfies Statement Coverage, it will partially satisfy Decision Coverage and Condition/Decision Coverage. If the test suite satisfies Decision Coverage, it will completely satisfy Statement Coverage and partially satisfy Condition/Decision Coverage. There is also a complexity relationship where Statement Coverage is the least complex to satisfy and Condition/Decision Coverage is the most complex to satisfy. The last thing to note in the picture is the fact that Object Coverage satisfies part of all of the other criteria.
= An Example = In order to illustrate what is covered by each of the different criteria, consider the following example showing the source code for a simple if statement along with its generated pseudo-code instructions. {| border="1" style="margin: 1em auto 1em auto;text-align: left;" |+ |- |'''Block''' || '''Source Code''' || '''Block''' || '''Object Pseudo-code''' |- | A || if (x OR y) || A1 || cmp x, 0 branch if FALSE to do something |- | || || A2 || cmp y, 0 branch if TRUE around do something |- | B || do something || B|| do something instructions |- |} = Statement Coverage = A single test case that allows the if statement to evaluate to TRUE will achieve 100% Statement Coverage. = Decision Coverage = Two test cases are required to achieve 100% Decision Coverage. One case must force the if statement to evaluate to TRUE and the other case must force the if statement to evaluate to FALSE. = Condition Coverage = Two test case are required to achieve 100% Condition Coverage. In one case x must be TRUE while y is FALSE and in the other case x must be FALSE while y is TRUE. = Object Coverage = Only a carefully chosen test case where x is FALSE and y is TRUE will achieve 100% Object Coverage.