当前位置: 首页>>代码示例>>Python>>正文


Python ConjectureRunner.new_conjecture_data方法代码示例

本文整理汇总了Python中hypothesis.internal.conjecture.engine.ConjectureRunner.new_conjecture_data方法的典型用法代码示例。如果您正苦于以下问题:Python ConjectureRunner.new_conjecture_data方法的具体用法?Python ConjectureRunner.new_conjecture_data怎么用?Python ConjectureRunner.new_conjecture_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在hypothesis.internal.conjecture.engine.ConjectureRunner的用法示例。


在下文中一共展示了ConjectureRunner.new_conjecture_data方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_can_reduce_poison_from_any_subtree

# 需要导入模块: from hypothesis.internal.conjecture.engine import ConjectureRunner [as 别名]
# 或者: from hypothesis.internal.conjecture.engine.ConjectureRunner import new_conjecture_data [as 别名]
def test_can_reduce_poison_from_any_subtree(size, seed):
    """This test validates that we can minimize to any leaf node of a binary
    tree, regardless of where in the tree the leaf is."""
    random = Random(seed)

    # Initially we create the minimal tree of size n, regardless of whether it
    # is poisoned (which it won't be - the poison event essentially never
    # happens when drawing uniformly at random).

    # Choose p so that the expected size of the tree is equal to the desired
    # size.
    p = 1.0 / (2.0 - 1.0 / size)
    strat = PoisonedTree(p)

    def test_function(data):
        v = data.draw(strat)
        if len(v) >= size:
            data.mark_interesting()

    runner = ConjectureRunner(
        test_function, random=random, settings=settings(TEST_SETTINGS, buffer_size=LOTS)
    )

    while not runner.interesting_examples:
        runner.test_function(
            runner.new_conjecture_data(lambda data, n: uniform(random, n))
        )

    runner.shrink_interesting_examples()

    data, = runner.interesting_examples.values()

    assert len(ConjectureData.for_buffer(data.buffer).draw(strat)) == size

    starts = [b.start for b in data.blocks if b.length == 2]
    assert len(starts) % 2 == 0

    for i in hrange(0, len(starts), 2):
        # Now for each leaf position in the tree we try inserting a poison
        # value artificially. Additionally, we add a marker to the end that
        # must be preserved. The marker means that we are not allow to rely on
        # discarding the end of the buffer to get the desired shrink.
        u = starts[i]
        marker = hbytes([1, 2, 3, 4])

        def test_function_with_poison(data):
            v = data.draw(strat)
            m = data.draw_bytes(len(marker))
            if POISON in v and m == marker:
                data.mark_interesting()

        runner = ConjectureRunner(
            test_function_with_poison, random=random, settings=TEST_SETTINGS
        )

        runner.cached_test_function(
            data.buffer[:u] + hbytes([255]) * 4 + data.buffer[u + 4 :] + marker
        )

        assert runner.interesting_examples
        runner.shrink_interesting_examples()

        shrunk, = runner.interesting_examples.values()

        assert ConjectureData.for_buffer(shrunk.buffer).draw(strat) == (POISON,)
开发者ID:HypothesisWorks,项目名称:hypothesis-python,代码行数:67,代码来源:test_poisoned_trees.py


注:本文中的hypothesis.internal.conjecture.engine.ConjectureRunner.new_conjecture_data方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。