module Driver where
import System.Environment
import Control.Monad
import Test.QuickCheck hiding (promote)
import System.Random hiding (next)
import Text.Printf
import Data.List (sort,group,intersperse)
main :: (Read t, Num t, PrintfArg t1, Num b, PrintfArg b) =>
[(t1, t -> IO (Bool, b))] -> IO ()
main tests = do
args <- fmap (drop 1) getArgs
let n = if null args then 100 else read (head args)
(results, passed) <- fmap unzip $ mapM (\(s,a) -> printf "%-25s: " s >> a n) tests
printf "Passed %d tests!\n" (sum passed) :: IO ()
when (not . and $ results) $ fail "Not all tests passed!"
debug :: Bool
debug = False
mytest :: Testable a => a -> Int -> IO (Bool, Int)
mytest a n = mycheck (stdArgs {maxSuccess = n}) a
mycheck :: Testable a => Args -> a -> IO (Bool, Int)
mycheck config a = do
rnd <- newStdGen
results <- quickCheckWithResult config {replay = Just (rnd, 1)} a
print results
return $ case results of
Success {} ->(True, maxSuccess config)
GaveUp {numTests = n} ->(True, n)
Failure {} -> (False, 0)
NoExpectedFailure {} -> (True, 0)
done :: String -> Int -> [[String]] -> IO ()
done mesg ntest stamps = putStr ( mesg ++ " " ++ show ntest ++ " tests" ++ table )
where
table = display
. map entry
. reverse
. sort
. map pairLength
. group
. sort
. filter (not . null)
$ stamps
display [] = ".\n"
display [x] = " (" ++ x ++ ").\n"
display xs = ".\n" ++ unlines (map (++ ".") xs)
pairLength xss@(xs:_) = (length xss, xs)
pairLength [] = (0, [])
entry (n, xs) = percentage n ntest
++ " "
++ concat (intersperse ", " xs)
percentage n m = show ((100 * n) `div` m) ++ "%"
integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
fromIntegral b :: Integer) g of
(x,h) -> (fromIntegral x, h)