Any*_*ser 3 haskell quickcheck hspec
我有以下代码用于在以下网站上创建挑战:codewars
describe "Random cases" $ do
it "It should handle random test cases" $
property $ prop_check where
prop_check (Positive x) = solution x == ref_sol x
--- ref_sol function
Run Code Online (Sandbox Code Playgroud)
我想将xprop_check 中的值设置为大于 4 的正整数,并且最多为五位数字(不超过五位数字,即:最大值 = 99999)。
我该如何接近它呢?
您可以使用 QuickCheck 的choose功能来选择包含范围内的值。最简单的方法可能是prop_check用do符号来写:
prop_check :: Gen Bool
prop_check = do
x <- choose (5, 99999) :: Gen Integer
return $ solution x == ref_sol x
Run Code Online (Sandbox Code Playgroud)
这里,x是和Integer之间的值。599999
根据solution和的类型ref_sol,您可能不需要Gen Integer第一行的类型注释。但由于我不知道这些函数的类型,所以我必须添加注释。