Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get HPC to acknowledge that you have 100% code coverage with guards in Haskell?

I'm trying to get (and prove) 100% test coverage for some code I'm writing in Haskell using HPC. However if I write something like this:

fac n | n > 0 = n * (fac (n - 1))
      | otherwise = 1

Then the second expression of the guard statement has always True tagged to it. What is the easiest way to overcome this in the general case?

edit: Just to clarify. This code:

fac n = if n > 0 then n * (fac (n - 1))
        else 1

Works fine with HPC, (running it gives 100% code coverage).

I'm basically suffering from this problem: http://hackage.haskell.org/trac/ghc/ticket/3175

like image 293
dan_waterworth Avatar asked Jan 11 '11 15:01

dan_waterworth


1 Answers

There's no issue. If an expression is marked as always true, that does not mean that you have less than 100% coverage. As an example, I just wrote a small executable based on fac, then ran hpc on it and hpc report on the resulting tix file.

Here's the source:

fac n | n > 0 = n * (fac (n - 1))
      | n == 0 = 1
      | otherwise = 125 -- An arbitrary value. This of couse is demo code, and not actually a factorial.

main = print (fac 12) >> print (fac (negate 100))

and here's the result:

100% expressions used (23/23)
 66% boolean coverage (2/3)
      66% guards (2/3), 1 always True
     100% 'if' conditions (0/0)
     100% qualifiers (0/0)
100% alternatives used (3/3)
100% local declarations used (0/0)
100% top-level declarations used (2/2)

The key thing is 100% expressions used, and 100% alternative used, 100% top-level declarations used. The fact that you have 66% boolean coverage is irrelevant. That's why if you run hpc markup and look at the resulting hpc_index file, it reports top level, alternative, and expressions, but not boolean coverage.

like image 88
sclv Avatar answered Sep 28 '22 05:09

sclv