Exemplo n.º 1
0
def test_wraps_around_to_beginning():
    def f(chooser):
        chooser.choose(range(3))

    tree = ChoiceTree()
    assert tree.step((), f) == (1, )
    assert tree.step((1, ), f) == (0, )
    assert tree.step((0, ), f) == ()
Exemplo n.º 2
0
def test_exhausts_randomly_when_filtering():
    def f(chooser):
        chooser.choose(range(10), lambda x: False)

    tree = ChoiceTree()

    random = Random()

    tree.step(random_selection_order(random), f)

    assert tree.exhausted
Exemplo n.º 3
0
def test_extends_prefix_from_right():
    def f(chooser):
        chooser.choose(range(4))

    tree = ChoiceTree()
    result = tree.step(select(), f)
    assert result == (3,)
Exemplo n.º 4
0
def test_skips_over_exhausted_subtree():
    def f(chooser):
        chooser.choose(range(10))

    tree = ChoiceTree()
    assert tree.step(select(8), f) == (8,)
    assert tree.step(select(8), f) == (7,)
def test_skips_over_exhausted_children():

    results = []

    def f(chooser):
        results.append((
            chooser.choose(range(3), condition=lambda x: x > 0),
            chooser.choose(range(2)),
        ))

    tree = ChoiceTree()

    tree.step(select(1, 0), f)
    tree.step(select(1, 1), f)
    tree.step(select(0, 0), f)

    assert results == [(1, 0), (1, 1), (2, 0)]
Exemplo n.º 6
0
def exhaust(f):
    tree = ChoiceTree()

    results = []

    prefix = ()

    while not tree.exhausted:
        prefix = tree.step(prefix, lambda chooser: results.append(f(chooser)))
    return results
Exemplo n.º 7
0
def test_exhausts_randomly():
    def f(chooser):
        chooser.choose(range(10))

    tree = ChoiceTree()

    random = Random()

    seen = set()

    for _ in range(10):
        seen.add(tree.step(random_selection_order(random), f))

    assert len(seen) == 10
    assert tree.exhausted
Exemplo n.º 8
0
def test_starts_from_the_end():
    def f(chooser):
        chooser.choose(range(3))

    tree = ChoiceTree()
    assert tree.step(select(), f) == (2,)