jeudi 16 janvier 2020

How to iteratively test in Haskell using QuickCheck?

How do you generate a random instance in Haskell QuickCheck and then run further tests on this very instance?

Let me demonstrate by giving an example. Within the following code snippet I generate an Int and then want to run further tests on this very int:

{-# OPTIONS -Wall #-}

import           Test.Tasty
import           Test.Tasty.QuickCheck as QC
import qualified Debug.Trace as Trace

main :: IO()
main = defaultMain tests

test1 :: Int -> Property
test1 i = withMaxSuccess 2 (test2 i)

test2 :: Int -> (Int, Int) -> Property
test2 i (a, b) =
  Trace.trace ("(n,a,b) = " ++ show (i, a, b)) $
  property True

tests :: TestTree
tests =
  testGroup
  "Test suite"
  [
    QC.testProperty "Test" (withMaxSuccess 3 test1)
  ]

As output I would expect something like:

(n,a,b) = (0,0,0)
(n,a,b) = (0,1,2)
(n,a,b) = (-1,-1,-1)
(n,a,b) = (-1,-2,-3)
(n,a,b) = (2,0,-2)
(n,a,b) = (2,1,-1)

But instead I get:

(n,a,b) = (0,0,0)
(n,a,b) = (-1,-1,-1)
(n,a,b) = (2,0,-2)

I found this post (How can QuickCheck test all properties for each sample), but it didn't really help me.

Aucun commentaire:

Enregistrer un commentaire