Using quickCheck

I wrote an implementation for foldl and wanted to check if it worked, I tried some cases and it seems to work fine, but I want to make sure.

I read about quickCheck and tried it, but I can't get it to work, this is the code

foldl'' :: (b -> a -> b) -> b -> [a] -> b

test :: Eq b => (b -> a -> b) -> b -> [a] -> Bool
test f e ls = foldl'' f e ls == foldl f e ls

when I run quickCheck test, it throws the following error:

No instance for (Show (b0 -> a0 -> b0))
  arising from a use of `quickCheck'
Possible fix:
  add an instance declaration for (Show (b0 -> a0 -> b0))
In the expression: quickCheck prueba
In an equation for `it': it = quickCheck prueba
+5
source share
3 answers

Your property requires three inputs: function, item, and list. The problem is that QuickCheck does not know how to deal with functions in general.

, QuickCheck , - . , String - Show. Show, . .

, . QuickCheck .

+7

Show Blind, QuickCheck .

-- Using Int instead of a, b which would be defaulted to () in GHCi
prueba :: Blind (Int -> Int -> Int) -> Int -> [Int] -> Bool
prueba (Blind f) e ls = foldl'' f e ls == foldl f e ls

, , , (*) . ( foldl'' = foldr . flip)

> quickCheck prueba 
*** Failed! Falsifiable (after 4 tests and 2 shrinks):    
(*)
0
[1,0]
+6

From what I understand, there is a mechanism for creating random functions in QuickCheck (see Test.QuickCheck.Function ), but I cannot say that I knew this stuff well to tell you how to use it.

At the same time, testing your property most likely makes more sense with the functions that you choose, so you can write something like quickCheck $ prueba (+)that that will work fine.

+2
source

All Articles