我试图使用quickcheck生成给定函数的随机参数(假设它的所有类型都有Arbitrary实例和Show实例)以及对这些参数的函数的评估.我只需要打印参数的值并在之后评估答案.所以我期待一个具有以下类型的函数
randomEvaluate :: Testable a => a -> IO ( [String] -- arguments
, String ) -- Answer after evaluating
-- IO is just needed to get a new random number generator. If I pass a generator then I think probably I will not need IO here.
Run Code Online (Sandbox Code Playgroud)
我仍然不确定这里的类型,但我认为Testable a会这样做.我仍然无法真正得到我需要的东西.我在快速检查数据类型的乱七八糟的所有困惑Rose,Result等等.
UPDATE
假设我有一个功能
add :: Int -> Int -> Int
add a b = a+b
Run Code Online (Sandbox Code Playgroud)
然后我假设一个行为
> randomEvaluate add
(["1","3"],"4")
Run Code Online (Sandbox Code Playgroud)
其中1和3是生成的随机值Int,4是f 1 3 …
我需要一些关于如何为Foo数据类型创建随机生成器的建议,以便Boo列表永远不会为空?
data Boo = Boo Float Float Float
data Foo = Foo Float Float Float [Boo]
Run Code Online (Sandbox Code Playgroud) 我有以下数据和功能
data Foo = A | B deriving (Show)
foolist :: Maybe Foo -> [Foo]
foolist Nothing = [A]
foolist (Just x) = [x]
prop_foolist x = (length (foolist x)) == 1
Run Code Online (Sandbox Code Playgroud)
在运行时quickCheck prop_foolist,ghc告诉我Foo必须是任意实例。
No instance for (Arbitrary Foo) arising from a use of ‘quickCheck’
In the expression: quickCheck prop_foolist
In an equation for ‘it’: it = quickCheck prop_foolist
Run Code Online (Sandbox Code Playgroud)
我尝试过data Foo = A | B deriving (Show, Arbitrary),但这导致
Can't make a derived instance of ‘Arbitrary Foo’: …Run Code Online (Sandbox Code Playgroud) Rust quickcheck 文档指出,对于任何类型的实现Arbitrary
它们还必须是可发送的和静态的,因为每个测试都使用 thread::Builder::spawn 在自己的线程中运行,这需要 Send + 'static 边界。
如果我需要为包含引用的结构生成数据,我该怎么做?例如:
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(quickcheck_macros))]
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
use quickcheck::{Arbitrary,Gen};
#[allow(dead_code)]
#[derive(Debug,Clone)]
pub struct C<'a> {
s: &'a str,
b: bool
}
#[cfg(test)]
impl<'a> Arbitrary for C<'a> {
fn arbitrary<G: Gen>(g: &mut G) -> C<'a> {
let s = g.gen::<&str>();
C{s: s, b: (s.len() > 0)}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[quickcheck]
fn len_checks_out(c: C) -> bool {
(c.s.len() > 0) == c.b …Run Code Online (Sandbox Code Playgroud) 我想使用QuickCheck来测试一个函数,以确保它终止(没有无限递归,没有抛出异常等).这就是我现在所做的:
f :: Int -> Int -> Int
prop_fTerminates :: Int -> Int -> Bool -- say
prop_fTerminates x y = f x y `seq` True
Run Code Online (Sandbox Code Playgroud)
是否有更好的(更具表现力和惯用语)的方式?
我是 Scala 的新手,正在编写我的第一个 Scalacheck 套件。
我的程序中有一个数据结构,本质上看起来像 a,(List[Double], List[Double])只有当 的每个元素都_1严格大于 的相应元素时,它才是格式良好的_2。
由于它在实践中稍微复杂一些(尽管出于本 MWE 的目的,我们可以假装它已经包含了所有内容),所以我为其编写了一个自定义生成器。
然后我添加了两个简单的测试(包括最简单的1 == 1测试),在这两种情况下测试都失败,并显示消息Gave up after only XX passed tests. YYY tests were discarded.
为什么会这样,我该如何解决?
附件是我的测试套件和输出。
package com.foo.bar
import org.scalacheck._
import Prop._
import Arbitrary._
object FooSpecification extends Properties("FooIntervals") {
type FooIntervals = (List[Double], List[Double])
/* This is supposed to be a tuple of lists s.t. each element of _1
* is < the corresponding element of _2
*
* …Run Code Online (Sandbox Code Playgroud) 我该怎么做呢?我想要一些sevenList :: Gen [Integer]产生列表的结构,并且保证总是包含至少一个7.
我知道如果我想生成一个大小为n的列表,我可以这样做:
listOfLength n gen = sequence [ gen | i <- [1..n] ]
Run Code Online (Sandbox Code Playgroud)
但这还不够,因为如果我生成一个长度为10的列表并且没有生成7,我必须继续,但此刻它不会.
我是Haskell的新手,我遇到了这个错误的麻烦.我在Windows上使用ghci.这是代码:
data Direction = North | South | East | West
deriving (Eq, Show)
type Point = (Int, Int)
origin = (0,0)
type Road = [Direction]
movement :: Point -> Road -> Point
{- ... }
test :: Road -> Road -> Bool
test road1 road2 = movement origin road1 == movement origin road2
-- i check if two roads lead to the same destination starting from
-- the origin point in a grid
Run Code Online (Sandbox Code Playgroud)
当我尝试运行测试时会发生这种情况:
*Main> quickCheck test
<interactive>:8:1: error: …Run Code Online (Sandbox Code Playgroud) 我开始通过解决99个Haskell问题来学习Haskell. http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell_Problems 我想使用quickcheck为每个程序/函数编写测试.
我有以下代码:
import Test.QuickCheck
import Text.Printf
main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests
-- 1
myLast lst = last lst
prop_1a xs x = myLast (xs ++ [x]) == (x::String)
myLast' = head . reverse
prop_1b xs x = myLast' (xs ++ [x]) == (x::String)
tests = [("1a", quickCheck prop_1a)
,("1b", quickCheck prop_1b)
]
我可能会写myLast'',myLast'''等等.有没有办法可以测试所有这些方法,而无需重复代码和快速检查属性?
相关问题:现在,我正在快速检查使用字符串.有没有办法随机使用不同类型进行测试?
我是Haskell的新手.我正在玩QuickCheck测试,尝试测试一个简单的功能calculateStrengthSingle(参见下面的测试人员来源)
# Fighter.hs
module Fighter
( Quantity(Quantity)
, Fighter(TeamPlayer, LoneWolf, Polymorph)
, Strength(Strength)
, calculateStrengthSingle)
where
import System.Random
data Strength = Strength Double
instance Eq Strength where
Strength s1 == Strength s2 =
s1 == s2
instance Ord Strength where
Strength s1 < Strength s2 =
s1 < s2
data Quantity = Quantity Int deriving (Show)
instance Random Quantity where
randomR (Quantity lo, Quantity hi) g =
let rand = randomR (lo, hi) g
(r, g1) = rand …Run Code Online (Sandbox Code Playgroud)