Too much branches

Too much branches

I have a problem to recognize the working of NCover. We need a coverage tool for use in an software project with iec 61508. Therefor i have to evalute your product.

But I have a comprehension problem to get astute of the following: http://ploader.net/files/1eb0e974fa1cd5d73c79fe941fd0cd25.jpg

The code shows a simple if else branch (compiled as 'Release'). Therefor the number of branches (branch coverage, release, optimized code) has to be 2. But it is shown as 3.

Original C# code:
public void DoWorkIf(bool check)
{
Console.WriteLine("execute 'DoWorkIf(bool)'");
if(check) Console.WriteLine("check state");
else Console.WriteLine("no check state");
}

Optimized (Release build) IL code:
.method public hidebysig instance void DoWorkIf(bool check) cil managed
{
// Code size 35 (0x23)
.maxstack 8
IL_0000: ldstr "execute 'DoWorkIf(bool)'"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ldarg.1
IL_000b: brfalse.s IL_0018
IL_000d: ldstr "check state"
IL_0012: call void [mscorlib]System.Console::WriteLine(string)
IL_0017: ret
IL_0018: ldstr "no check state"
IL_001d: call void [mscorlib]System.Console::WriteLine(string)
IL_0022: ret
} // end of method Worker::DoWorkIf

Not optimized (Release build) IL code:
.method public hidebysig instance void DoWorkIf(bool check) cil managed
{
// Code size 49 (0x31)
.maxstack 2
.locals init ([0] bool CS$4$0000)
IL_0000: nop
IL_0001: ldstr "execute 'DoWorkIf(bool)'"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ldarg.1
IL_000d: ldc.i4.0
IL_000e: ceq
IL_0010: stloc.0
IL_0011: ldloc.0
IL_0012: brtrue.s IL_0023
IL_0014: nop
IL_0015: ldstr "check state"
IL_001a: call void [mscorlib]System.Console::WriteLine(string)
IL_001f: nop
IL_0020: nop
IL_0021: br.s IL_0030
IL_0023: nop
IL_0024: ldstr "no check state"
IL_0029: call void [mscorlib]System.Console::WriteLine(string)
IL_002e: nop
IL_002f: nop
IL_0030: ret
} // end of method Worker::DoWorkIf

Can you explain me how the algorithm is working at branch coverage.

Nearby it would be really helpful if the code is also highlited at branch coverage not only at symbol coverage.


RE: Too much branches

We use any branch statements in IL (the statements usually start with br) and any return statements (ret) to determine where branches occur.

So in the first example above we count brfalse.s and the two ret statements as branches. In the second example we count brtrue.s, br.s, and ret.

We're working on ways to display branch coverage with source, and are hoping that we'll be able to include that as a feature in the near future.