问题描述
我想用SmallCheck来测试我的代码。我设法生成了任意的int对列表,但这不是我的类型应该包含的。该列表表示一组范围,其中 [1,3],[4,6)
将被编码/存储为 [(1,3 ),(4,6)]
。
这些是我范围规格化形式的不变量:
fst a< snd a
snd a< fst b其中a在列表中的b之前
我想与SmallCheck进行沟通,以便它不会产生我丢弃的大量值,因为它们不能满足我的不变量,但也许这是不可能的。
如何生成满足我的不变量的列表?
内置int类型的应用程序特定类型(Int,List)。这个建议不仅适用于SmallCheck,也适用于任何语言的任何软件。 data Interval = Interval(Int, Int)
data Domain = Domain [Interval]
编写执行不变量的智能构造函数。
interval :: Int - > Int - >间隔
间隔x y =间隔(min x y,max x y) - 如果您想要这个
域:: [间隔] - > Domain
domain ints = Domain ...(可以将间隔排序,也许将它们合并)
I want to use SmallCheck to test my code. I managed to generate arbitrary lists of pairs of ints, but that's not what my type should contain. The list represents a set of ranges, where [1,3),[4,6)
would be encoded/stored as [(1,3),(4,6)]
.
These are the invariants for the normalized form of my ranges:
fst a < snd a
snd a < fst b where a is before b in the list
I would like to communicate this to SmallCheck so that it doesn't generate loads of values that I throw away, because they aren't satisfying my invariants, but maybe that's not possible.
How can I generate lists satisfying my invariants?
Favour application-specific types over built-int types (Int, List). This is advice not just for SmallCheck, but for any piece of software, in any language.
data Interval = Interval (Int,Int)
data Domain = Domain [Interval]
Write smart constructors that enforce your invariants.
interval :: Int -> Int -> Interval
interval x y = Interval (min x y, max x y) -- if you want this
domain :: [Interval] -> Domain
domain ints = Domain ... (something that sorts intervals, and perhaps merges them)
Then use these to create Serial instances.
这篇关于SmallCheck生成满足不变量的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!