module Test.Feat.Driver(
test
, testOptions
, Options(..)
, defOptions
, testFlex
, Result(..)
, FlexibleOptions(..)
, FlexOptions(..)
, defFlex
, toFlex
, toFlexWith
) where
import Control.Enumerable
import Test.Feat.Access
import Test.Feat.Finite
import Test.Feat.Enumerate
import System.Timeout
import Data.IORef
data Options = Options
{ oTimeoutSec :: Maybe Int
, oSizeFromTo :: Maybe (Int,Int)
, oMaxCounter :: Maybe Int
, oSilent :: Bool
, oSkipping :: Maybe (Index, Integer)
, oBounded :: Maybe Integer
} deriving (Show,Read)
type FlexibleOptions a = IO (FlexOptions a)
data FlexOptions a = FlexOptions
{ fIO :: IO Bool -> IO (Result,[a])
, fReport :: a -> IO Bool
, fOutput :: String -> IO ()
, fProcess :: Enumerate a -> Enumerate a
, fEnum :: Enumerate a
}
data Result = Exhausted
| Quota
| TimedOut
| Other
deriving Show
defOptions :: Options
defOptions = Options
{ oTimeoutSec = Just 60
, oSizeFromTo = Just (0,100)
, oSilent = False
, oSkipping = Nothing
, oBounded = Just 100000
, oMaxCounter = Just 1
}
defFlex :: Enumerable a => FlexibleOptions a
defFlex = defFlexWith optimal
defFlexWith :: Enumerate a -> FlexibleOptions a
defFlexWith e = toFlexWith e defOptions
toFlex :: Enumerable a => Options -> FlexibleOptions a
toFlex = toFlexWith optimal
toFlexWith :: Enumerate a -> Options -> FlexibleOptions a
toFlexWith e o = do
res <- newIORef []
count <- newIORef 0
let doReport x = do
modifyIORef res (x:)
modifyIORef count (+1)
maybe (return True) (checkCount) (oMaxCounter o)
checkCount mx = do
n <- readIORef count
return (n < mx)
doIO io = do
mb <- maybe (fmap Just io) (\t -> timeout (t*1000000) io) (oTimeoutSec o)
res <- readIORef res
return $ maybe (TimedOut,res) (\b -> if b then (Exhausted,res) else (Quota,res)) mb
skip = maybe id (\(i,n) e -> skipping e i n) (oSkipping o)
bound = maybe id (\n e -> bounded e n) (oBounded o)
sizes = maybe id (\bs e -> sizeRange e bs) (oSizeFromTo o)
return $ FlexOptions
{ fIO = doIO
, fOutput = if oSilent o then const (return ()) else putStr
, fReport = doReport
, fProcess = bound . skip . sizes
, fEnum = e
}
test :: Enumerable a => (a -> Bool) -> IO [a]
test = testOptions defOptions
testOptions :: Enumerable a => Options -> (a -> Bool) -> IO [a]
testOptions o p = fmap snd $ testFlex fo p
where
fo = toFlex o
testFlex :: FlexibleOptions a -> (a -> Bool) -> IO (Result, [a])
testFlex ioOp p = do
op <- ioOp
let e = fProcess op (fEnum op)
lazyResult = [(n,filter (not . p) xs) | (n,xs) <- valuesWith e]
runSize k (n,cs) = do
fOutput op $ "*** Searching in " ++ show n ++ " values of size " ++ show k ++ "\n"
doWhile (map (\x -> fOutput op "Counterexample found!\n" >> fReport op x) cs)
rxs@(r,_) <- fIO op ((doWhile $ zipWith runSize [0..] lazyResult))
case r of
Exhausted -> fOutput op "Search space exhausted\n"
TimedOut -> fOutput op "Timed out\n"
_ -> return ()
return rxs
doWhile :: [IO Bool] -> IO Bool
doWhile [] = return True
doWhile (iob:iobs) = iob >>= \b -> if b then doWhile iobs else return False