如何让QuickCheck只为参数生成有效的列表索引? [英] How to tell QuickCheck to generate only valid list indices for a parameter?
本文介绍了如何让QuickCheck只为参数生成有效的列表索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(!!)
函数编写一些单元测试。 my_prop xs n = ...
我想限制n只能有效索引,我知道我可以做一些像
my_prop xs n =(not.null)(drop n xs)==> ; ...
但是,这使得绝大多数生成的案例无效并且被抛出远。有没有一种方法可以让QuickCheck首先生成 xs
列表,并使用它的值仅生成 n $的有效情况c $ c>?
解决方案
my_prop(NonEmpty xs)= forAll(choose( 0,length xs - 1))$ \\\
- > ...
Say I want to write some unit tests for the (!!)
function.
my_prop xs n = ...
I want to restrict n to only valid indexes and I know I could do something like
my_prop xs n = (not.null) (drop n xs) ==> ...
But this makes it so that the vast majority of the generated cases are invalid and get thrown away. Is there a way I can set things up so that QuickCheck generates the xs
list first and uses its value to generate only valid cases of n
?
解决方案
Using forAll
, you can specify a generator for n
which depends on the earlier arguments, e.g.
my_prop (NonEmpty xs) = forAll (choose (0, length xs - 1)) $ \n -> ...
这篇关于如何让QuickCheck只为参数生成有效的列表索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文