How do you get HPC to admit that you have 100% coverage of the guards in Haskell?

I am trying to get (and prove) 100% testing coverage for some code that I write 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 is always True. What is the easiest way to overcome this in the general case?

edit: Just for clarification. This code:

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

Works great with HPC, (running it provides 100% code coverage).

I mostly suffer from this problem: http://hackage.haskell.org/trac/ghc/ticket/3175

+4
source share
2 answers

No problems. If the expression is marked as always true, this does not mean that you have less than 100% coverage. As an example, I wrote a small fac-based executable, then ran hpc on it and an hpc report in the resulting tix file.

Here is 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 is 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 value is 100% used expressions, as well as 100% alternative, used 100% top-level ads. The fact that you have 66 percent logical coverage doesn't matter. Therefore, if you run the hpc markup and look at the resulting hpc_index file, it reports top level, alternative, and expressions, but not Boolean coverage.

+7
source

You can replace the security syntax with many if if else else expressions. I do not know any better ways to do this.

0
source

Source: https://habr.com/ru/post/1335108/


All Articles