Haskell / SmallCheck: how to control the `Depth` parameter?

I have a simple data structure to validate in smallcheck.

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}

import Test.Tasty
import Test.Tasty.SmallCheck
import Test.SmallCheck.Series
import GHC.Generics

data T1 = T1 { p1 :: Int,
               p2 :: Char,
               p3 :: [Int]
             } deriving (Eq, Show, Generic)

instance Monad m => Serial m T1

main :: IO ()
main = defaultMain tests

tests :: TestTree
tests = testGroup "Tests" [scProps]

scProps = testGroup "(checked by SmallCheck)"
  [ testProperty "Test1" prop_test1
  ]

prop_test1 x = x == x
             where types = (x :: T1)

When running tests, is there any general solution for setting a parameter Depthfor (individual) tests, or, even better, a fine-grained solution for setting a parameter Depthfor individual fields, for example. limit the depth p3to 2 to avoid the combinatorial explosion of the search space?

Thank you very much in advance!

Jules

Information about: A few related questions here .

EDIT:
I made the decisions outlined in Roman Cheplyaka's accepted answer and implemented them in a minimal working example (thanks, Roman):

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}

import Test.Tasty
import Test.Tasty.Options
import Test.Tasty.SmallCheck
import Test.SmallCheck.Series
import Data.Functor
-- =============================================================================
main :: IO ()
main = defaultMain tests
-- =============================================================================
-- the data structure to be tested
data T1 = T1 { p1 :: Int,
               p2 :: Char,
               p3 :: Int,
               p5 :: [Int]
             } deriving (Show, Eq)
-- =============================================================================
-- some test-properties
prop_test1 x y = x == y
               where types = (x :: T1, y :: T1)
prop_test2 x = x == x
             where types = (x :: T1)
-- =============================================================================
-- how to change the depth of the search spaces?
{-| I Possibility: Command Line |-}
-- foo@bar$ runhaskell Test.hs --smallcheck-depth 2

-- -----------------------------------------------------------------------------
{-| II Possibility: |-}
-- normal:
-- tests :: TestTree
-- tests = testGroup "Tests" [scProps]

-- custom:
tests :: TestTree
tests = localOption d $ testGroup "Tests" [scProps]
      where d = 3 :: SmallCheckDepth

scProps = testGroup "(checked by SmallCheck)"
  [ testProperty "Test1" prop_test1, 
    testProperty "Test2" prop_test2 
  ]

-- -----------------------------------------------------------------------------
{-| III Possibility: Adjust Depth when making the type instance of Serial |-}
-- normal:
-- instance (Monad m) => Serial m T1 where
--   series = T1 <$> series <~> series <~> series <~> series 

-- custom:
instance (Monad m) => Serial m T1 where
    series = localDepth (const 4) $ T1 <$> (localDepth (const 2) series) <~> series <~> series <~> (decDepth series) 

-- (a few more examples):
-- instance (Monad m) => Serial m T1 where
--    series = decDepth $ T1 <$> series <~> series <~> series <~> (decDepth series ) 
-- instance (Monad m) => Serial m T1 where
--   series = localDepth (const 3) $ T1 <$> series <~> series <~> series <~> series 
-- instance (Monad m) => Serial m T1 where
--    series = localDepth (const 4) $ T1 <$> series <~> series <~> series <~> (decDepth series) 
+4
2

, :

  • --smallcheck-depth ""
  • adjustOption , ( ).
  • , localDepth , , . - 1 ( decDepth ), .

, , . , - . .

... , , , . ( , , !) .

, , github , . ( , . , .)

+3

, , .

( ) :

./test --smallcheck-depth 2

cabal:

cabal test --test-options="--smallcheck-depth 2"

/TestTree:

-- Global depth + 1  (usually supplied by command line)
props = adjustOption (+ SmallCheckDepth 1) $
        testGroup "some props"
          [ SC.testProperty myProp1
          , SC.testProperty myProp2
          ]

-- Local depth = 2
prps2 = localOption (SmallCheckDepth 2) $
        testGroup "fixed depth"
        [ ... ]

adjustOption localOption . , configureOption , , , TestTree.

+1

All Articles