Question

I have a simple function set of converse functions (simple shift codes):

  encode, decode :: Int -> String -> String

and they test fine by some simple manual tests, but a quickCheck test reports a failure:

*** Failed! Falsifiable (after 8 tests and 4 shrinks):
"\254"
0

But running this same test by hand works fine:

   *Main> decode 0 ( encode 0 "\254")
   "c"

I'm not asking why it fails (that is something I'll chase down), but why does quickCheck fail when the (seemingly) same test by hand works?

I suspect it has to do with the character encoding, one decoding it and the other treating it as a String, but am not sure why.


This question is about how the testing works, not (yet!) the function or why it fails, but here is the code:

import Data.Char
let2int c =  ord c - ord 'a'

int2let n =  chr (ord 'a' + n)

shift :: Int -> Char -> Char
shift n c | isLower c   =  int2let ((let2int c + n) `mod` 26)
          | otherwise   =  c

encode, decode :: Int -> String -> String
encode n xs  = [shift n x | x <- xs]
decode n     = encode (-n)

Ref: Hutton, 2007

quickCheck $ (\s n-> (decode n $ encode n s) == s)
Was it helpful?

Solution

You are providing insufficient information - next time please just give the full test and the full code. There is no magic involved and no good reason for the expression in your test when applied to the output from quickCheck to give anything other than a failing result.

From your code I figured your test is decode 0 . encode 0 ~ id. This is not true, just look at the output from ghci:

*Main Test.QuickCheck> quickCheck (\x -> x == decode 0 (encode 0 x))
*** Failed! Falsifiable (after 13 tests and 4 shrinks):    
"\244"
*Main Test.QuickCheck> decode 0 (encode 0 "\244")
"r"
*Main Test.QuickCheck> "\244" == "r"
False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top