Haskell の QuickCheck パッケージを repl 環境で使う
Haskell の QuickCheck
を repl
環境で使ってみる。
org mode の source block を使うので、 chatty
を False
にセットしている。
まずは ghci でのおまじない。
:set -package QuickCheck
:set +m
下記を実行
import Test.QuickCheck
import Data.List (delete)
prop_delete :: Int -> [Int] -> Property
prop_delete x xs =
classify (count x xs == 0) "count x xs == 0" $
classify (count x xs == 1) "count x xs == 1" $
classify (count x xs >= 2) "count x xs >= 2" $
counterexample (show (delete x xs)) $
count x (delete x xs) == max 0 (count x xs-1)
where count x xs = length (filter (== x) xs)
myArgs = stdArgs {chatty = False}
output <$> quickCheckWithResult myArgs prop_delete >>= putStrLn
実行結果
+++ OK, passed 100 tests:
76% count x xs == 0
19% count x xs == 1
5% count x xs >= 2
思いどおりの結果が得られた。