故事:
目前,我有一个正在测试的功能,需要整数列表的列表遵循以下规则:
- 子列表的数量(我们称之为
N
) 可以是 1 到 50
- 子列表内的值的数量对于所有子列表(矩形形式)都是相同的,并且应该 >= 0 且
- 子列表内的值不能大于或等于子列表的总数。换句话说,子列表中的每个值都是一个整数 >= 0 且 N
有效输入示例:
[[0]]
[[2, 1], [2, 0], [3, 1], [1, 0]]
[[1], [0]]
无效输入示例:
[[2]] # 2 is more than N=1 (total number of sublists)
[[0, 1], [2, 0]] # 2 is equal to N=2 (total number of sublists)
我正在尝试接近它基于属性的测试并生成不同的有效输入hypothesis library http://hypothesis.readthedocs.io/en/latest/index.html并试图把我的头绕过去lists()
and integers()
,但无法使其工作:
- 条件 #1 很容易实现
lists()
and min_size
and max_size
论点
- 条件 #2 包含在Chaining strategies together http://hypothesis.readthedocs.io/en/latest/data.html#chaining-strategies-together
- 条件 #3 是我正在努力解决的问题 - 因为,如果我们使用
rectangle_lists
从上面的示例中,我们没有引用内部“父”列表的长度integers()
问题:
如何限制子列表内的整数值小于子列表的总数?
我的一些尝试:
from hypothesis import given
from hypothesis.strategies import lists, integers
@given(lists(lists(integers(min_value=0, max_value=5), min_size=1, max_size=5), min_size=1, max_size=50))
def test(l):
# ...
这远远不能满足要求 - 列表并不是严格的矩形形式,生成的整数值可以超过列表的生成大小。
from hypothesis import given
from hypothesis.strategies import lists, integers
@given(integers(min_value=0, max_value=5).flatmap(lambda n: lists(lists(integers(min_value=1, max_value=5), min_size=n, max_size=n), min_size=1, max_size=50)))
def test(l):
# ...
在这里,#1 和 #2 是满足要求的,但整数值可以大于列表的大小 - 不满足要求 #3。