3 ms·
> 100% branch coverage says that every line of code has been executed, but not how many times Actually, 100% branch coverage says every line has been executed
by veltas 4y ago
> 100% branch coverage says that every line of code has been executed, but not how many times
Actually, 100% branch coverage says every line has been executed but also that every branch has been both taken and not taken. So e.g. every non-constant if statement is both taken and not taken.
- XCSme 4y agoSo also all combination of branches should be tested, right? Doesn't that lead to exponential test-case growth?
- SCHiM 4y agoNo so that's exactly it. Not all combinations are tested, all branches are both taken and not taken, but not all combinations of taken/not taken are tested.
- IshKebab 4y agoIt's only possible to execute all lines if you take/don't take every if branch. They're the same.
- mort96 4y agoNo? Take this code: void foo(int arg) { int *x = malloc(sizeof(int)); if (arg) { free(x); } } If your test only ever calls `foo` with a non-zero `arg`, you have covered 100% of the lines in the function, but you never discover the memory leak bug in the case where `if` body isn't executed.
- rhdunn 4y agoAn if statement without an else can have the case where line coverage does not cover the "don't take the branch" part of branch coverage. Therefore, if not taking that branch results in a bug line coverage only will not pick up that bug. -- For example, if you initialize a value in an if statement's then branch but don't have a default value for it or an else branch to provide a default. Or even if you have a ternary operator (which has an implicit if-then-else take/don't take branch) that is usually only one line.