Example 1
if ( a1 || a2 || ... || an ) {
...
}
Sonar shows that 0/2*n
branches are covered by tests. (E.g.: 0/6
for n=3
)
Let's take a function:
f:{a1,...,an}->{true,false}
The number of functions (=all possible combinations) is equal to |{true,false}|^|{a1,...,an}| = 2^n
which is obviously different than 2*n
( 8 != 6
for n=3
). It could be demonstrated using a sum of combinations, too.
If we want to write that if in the following way:
if ( a1 ) {
...
} else if ( a2 ) {
...
} else if ( a3 ) {
...
}
...
we can cover all branches using the following n
sequences - example for n=3
:
- T**
- FT*
- FFT
where T=True, F=False, *=don't care
Q: So, why is there 2*n
?
Example 2
if ( a != null && a.length > 0 ) {
...
}
Sonar shows that 0/4
branches are covered by tests.
Q: How can I achieve 4/4
? I see only 3 possible cases (inputs):
- a != null, a.length > 0
- a != null, a.length <= 0
- a == null (the second condition cannot be adjusted)
I know that SonarQube uses another tool like Cobertura or JaCoCo for coverage and this tool is responsible for the above issues, but I didn't specify it because I don't know it. According to these features it seems that probably the coverage tool is Cobertura or JaCoCo because they have line and branch metrics.
I'd like to know the principle (how they decided to use those metrics).