我正在使用QuickCheck来测试我的代码以进行一些数值计算.基本上我有一个确切的功能和几个近似的效率更高.
我目前正在实现我想要测试的属性:
prop_blah input = (abs $ (exact input)-(approx input)) < threshold
Run Code Online (Sandbox Code Playgroud)
但是,确切地知道每种近似算法的准确度并将它们相互比较真的很不错.一种简单的方法是获得不等式左侧的均值和标准差的报告.这有点可能吗?
这是一个简单的函数。它接受一个输入Int并返回一个(可能为空)对列表(Int, Int),其中输入Int是任何对的立方元素的总和。
cubeDecomposition :: Int -> [(Int, Int)]
cubeDecomposition n = [(x, y) | x <- [1..m], y <- [x..m], x^3 + y^3 == n]
where m = truncate $ fromIntegral n ** (1/3)
-- cubeDecomposition 1729
-- [(1,12),(9,10)]
Run Code Online (Sandbox Code Playgroud)
我想测试上述属性是否正确;如果我对每个元素进行立方并对任何返回元组求和,那么我会得到我的输入:
import Control.Arrow
cubedElementsSumToN :: Int -> Bool
cubedElementsSumToN n = all (== n) d
where d = map (uncurry (+) . ((^3) *** (^3))) (cubeDecomposition n)
Run Code Online (Sandbox Code Playgroud)
出于运行时考虑,我想Int在使用 QuickCheck 进行测试时将输入限制为一定大小。我可以定义适当的类型和Arbitrary实例:
{-# …Run Code Online (Sandbox Code Playgroud) 我正在尝试一个简单的玫瑰树代码.
data RoseT a = Leaf a | Node a [RoseT a] deriving (Show)
instance Eq (RoseT a) where
(==) (Leaf a) (Leaf b) = a == b
(==) (Node a rs1) (Node b rs2) = and ((a==b): (zipWith (==) rs1 rs2))
(==) _ _ = False
Run Code Online (Sandbox Code Playgroud)
我可以使用quickcheck来测试Eq实例的实现吗?如果有,怎么样?如果不是,最好的选择是什么?
我还有一个功能,可以执行以下操作:
appendPath :: (RoseT a) -> (RoseT (a,[a]))
appendPath rst = appendPath' [] rst
appendPath :: [a] -> (RoseT a) -> (RoseT (a,[a]))
appendPath' p (Leaf a) = Leaf (a,p)
appendPath' p …Run Code Online (Sandbox Code Playgroud) 我正在编写Vector和Matrix依赖类型的数据类型.
data Vector n e where
EmptyVector :: Vector Zero e
(:>) :: e -> Vector n e -> Vector (Succ n) e
deriving instance Eq e => Eq (Vector n e)
infixr :>
data Matrix r c e where
EmptyMatrix :: Matrix Zero c e
(:/) :: Vector c e -> Matrix r c e -> Matrix (Succ r) c e
deriving instance Eq e => Eq (Matrix r c e)
infixr :/
Run Code Online (Sandbox Code Playgroud)
它们取决于自然数,也取决于类型.
data …Run Code Online (Sandbox Code Playgroud) 我正在玩FsCheck所以我有这个实现:
let add a b =
if a > 100
then failwith "nasty bug"
else a + b
Run Code Online (Sandbox Code Playgroud)
......以及基于FsCheck的测试:
fun (a:int) -> (add a 0) = a
|> Check.QuickThrowOnFailure
Run Code Online (Sandbox Code Playgroud)
而测试永远不会失败.我的猜测是随机生成器产生的100个值绝不会大于100.
价值不应该更"随机"吗?
如何使用检查器库来测试简单解析器的Functor定律?
import Test.QuickCheck
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes
import qualified Data.ByteString as BS
type Index = Int
newtype Parser a = Parser (BS.ByteString -> Index -> (a, Index))
runParser :: Parser a -> BS.ByteString -> Index -> (a, Index)
runParser (Parser p) = p
instance Functor Parser where
f `fmap` p = Parser $ \bs i ->
let (a, ix) = runParser p bs i
in (f a, ix)
Run Code Online (Sandbox Code Playgroud)
我想我必须使用Test.QuickCheck.Classes中的仿函数函数
类型是:
functor :: forall m a …Run Code Online (Sandbox Code Playgroud) 我正在使用 QuickCheck 生成任意函数,并且我想生成任意单射函数(即f a == f b当且仅当a == b)。
我以为我已经弄清楚了:
newtype Injective = Injective (Fun Word Char) deriving Show
instance Arbitrary Injective where
arbitrary = fmap Injective fun
where
fun :: Gen (Fun Word Char)
fun = do
a <- arbitrary
b <- arbitrary
arbitrary `suchThat` \(Fn f) ->
(f a /= f b) || (a == b)
Run Code Online (Sandbox Code Playgroud)
但是我看到生成的函数将不同的输入映射到相同的输出的情况。
我想要的是:
我正在尝试编写一个改变数独的道具,然后检查它是否仍然有效.
但是,我不确定如何正确使用"oneof"功能.你能给我一些提示吗?
prop_candidates :: Sudoku -> Bool
prop_candidates su = isSudoku newSu && isOkay newSu
where
newSu = update su aBlank aCandidate
aCandidate = oneof [return x | x <- candidates su aBlank]
aBlank = oneof [return x | x <- (blanks su)]
Run Code Online (Sandbox Code Playgroud)
这里有一些更多信息......
type Pos = (Int, Int)
update :: Sudoku -> Pos -> Maybe Int -> Sudoku
blanks :: Sudoku -> [Pos]
candidates :: Sudoku -> Pos -> [Int]
[return x | x <- (blanks example)] :: (Monad …Run Code Online (Sandbox Code Playgroud) 在QuickCheck使用Xcode 6.1(6A1052d)的Mac Pro 2013上运行的OS X Yosemite 10.10(14A389)系统上安装for Haskell GHC 7.8.3时,我遇到了以下clang错误:
$ cabal install QuickCheck
Resolving dependencies...
Configuring primitive-0.5.4.0...
Building primitive-0.5.4.0...
Preprocessing library primitive-0.5.4.0...
[ 1 of 10] Compiling Data.Primitive.Internal.Compat ( Data/Primitive/Internal/Compat.hs, dist/build/Data/Primitive/Internal/Compat.o )
[ 2 of 10] Compiling Data.Primitive.MachDeps ( Data/Primitive/MachDeps.hs, dist/build/Data/Primitive/MachDeps.o )
[ 3 of 10] Compiling Data.Primitive.Internal.Operations ( Data/Primitive/Internal/Operations.hs, dist/build/Data/Primitive/Internal/Operations.o )
[ 4 of 10] Compiling Control.Monad.Primitive ( Control/Monad/Primitive.hs, dist/build/Control/Monad/Primitive.o )
[ 5 of 10] Compiling Data.Primitive.Types ( Data/Primitive/Types.hs, dist/build/Data/Primitive/Types.o )
[ 6 of 10] Compiling …Run Code Online (Sandbox Code Playgroud) 我需要测试许多访问数据库的函数(通过Persistent).虽然我可以使用它monadicIO,withSqlitePool但会导致测试效率低下.每个测试,而不是属性,但测试,将创建和销毁数据库池.我该如何防止这种情况?
重要提示:忘记效率或优雅.我甚至无法制作QuickCheck和Persistent类型甚至构成.
instance (Monad a) => MonadThrow (PropertyM a)
instance (MonadThrow a) => MonadCatch (PropertyM a)
type NwApp = SqlPersistT IO
prop_childCreation :: PropertyM NwApp Bool
prop_childCreation = do
uid <- pick $ UserKey <$> arbitrary
lid <- pick $ LogKey <$> arbitrary
gid <- pick $ Aria2Gid <$> arbitrary
let createDownload_ = createDownload gid lid uid []
(Entity pid _) <- run $ createDownload_ Nothing
dstatus <- pick arbitrary
parent …Run Code Online (Sandbox Code Playgroud) quickcheck ×10
haskell ×9
cabal ×1
clang ×1
f# ×1
fscheck ×1
ghci ×1
osx-yosemite ×1
persistent ×1
testing ×1
tree ×1