コード例 #1
0
def test_containment():
    u, v = find(
        st.tuples(intlist, st.n_byte_unsigned(8)),
        lambda uv: uv[1] in uv[0] and uv[1] >= 100
    )
    assert u == [100]
    assert v == 100
コード例 #2
0
def test_a_block_of_bytes_simplifies_to_lexicographically_smallest(n):
    K = 1000
    d = find(
        lambda d: d.draw_bytes(K),
        lambda x: len([c for c in x if c > 0]) >= n
    )
    assert d == b'\0' * (K - n) + b'\1' * n
コード例 #3
0
def test_constant_lists_through_flatmap():
    x = find(
        st.integer_range(0, 99).flatmap(lambda x: st.lists(st.just(x))),
        lambda x: sum(x) >= 100
    )
    assert sum(x[:-1]) < 100
    assert len(x) * (x[0] - 1) < 100
コード例 #4
0
def test_find_broad_tree():
    xs = find(
        tree(),
        lambda x: len(tree_values(x)) >= 20,
        settings=Settings(mutations=100, generations=250, buffer_size=16 * 1024, max_shrinks=10000),
    )
    assert tree_values(xs) == [0] * 20
コード例 #5
0
def test_reverse_sorted_list_subsequences():
    k = 6
    xs = find(
        st.lists(intlist), lambda t: (
            length_of_longest_ordered_sequence(reversed(t)) >= k),
    )
    assert xs == [[t] for t in range(k - 2, -1, -1)] + [[]]
コード例 #6
0
def test_reverse_sorted_list_subsequences():
    k = 6
    xs = find(
        st.lists(intlist),
        lambda t: (length_of_longest_ordered_sequence(reversed(t)) >= k),
    )
    assert xs == [[t] for t in range(k - 2, -1, -1)] + [[]]
コード例 #7
0
def test_minimize_sets_of_sets():
    sos = st.lists(st.lists(st.n_byte_unsigned(8)).map(frozenset)).map(set)

    def union(ls):
        return reduce(operator.or_, ls, frozenset())

    x = find(sos, lambda ls: len(union(ls)) >= 30)
    assert x == {frozenset(range(30))}
コード例 #8
0
def test_sorted_integer_subsequences():
    k = 6
    xs = find(
        intlist, lambda t: (
            len(t) <= 30 and length_of_longest_ordered_sequence(t) >= k),
    )
    start = xs[0]
    assert xs == list(range(start, start + k))
コード例 #9
0
def test_find_broad_tree():
    xs = find(tree(),
              lambda x: len(tree_values(x)) >= 20,
              settings=Settings(mutations=100,
                                generations=250,
                                buffer_size=16 * 1024,
                                max_shrinks=10000))
    assert tree_values(xs) == [0] * 20
コード例 #10
0
def test_minimize_sets_of_sets():
    sos = st.lists(st.lists(st.n_byte_unsigned(8)).map(frozenset)).map(set)

    def union(ls):
        return reduce(operator.or_, ls, frozenset())

    x = find(sos, lambda ls: len(union(ls)) >= 30)
    assert x == {frozenset(range(30))}
コード例 #11
0
def test_sorted_list_subsequences():
    k = 6
    xs = find(st.lists(intlist), lambda t: (len(t) <= 30 and length_of_longest_ordered_sequence(t) >= k))
    assert len(xs) == k
    for i in range(k - 1):
        u = xs[i]
        v = xs[i + 1]
        if len(v) > len(u):
            assert v == u + [0]
コード例 #12
0
def test_find_deep_tree():
    d = 15
    xs = find(
        tree(),
        lambda x: tree_height(x) >= d,
        settings=Settings(mutations=100, generations=250, buffer_size=16 * 1024, max_shrinks=10000),
    )
    assert tree_height(xs) == d
    assert set(tree_values(xs)) == {0}
コード例 #13
0
def test_reverse_sorted_integer_subsequences():
    k = 6
    xs = find(
        intlist,
        lambda t: (len(t) <= 30 and length_of_longest_ordered_sequence(
            reversed(t)) >= k),
    )
    start = xs[0]
    assert xs == list(range(start, -1, -1))
コード例 #14
0
def test_find_deep_tree():
    d = 15
    xs = find(tree(),
              lambda x: tree_height(x) >= d,
              settings=Settings(mutations=100,
                                generations=250,
                                buffer_size=16 * 1024,
                                max_shrinks=10000))
    assert tree_height(xs) == d
    assert set(tree_values(xs)) == {0}
コード例 #15
0
def test_between_one_and_two():
    t = find(st.floats(),
             lambda x: 1 < x < 2,
             settings=Settings(
                 max_shrinks=10**6,
                 generations=10000,
             ))
    for k in range(dtoi(1.0), dtoi(t)):
        assert itod(k) <= t
        assert not (1 < itod(k))
コード例 #16
0
def test_minimum_sum_tree(n):
    xs = find(
        tree(),
        lambda x: sum(tree_values(x)) >= n,
        settings=Settings(mutations=100, generations=250, buffer_size=16 * 1024, max_shrinks=10000),
    )
    ts = tree_values(xs)
    assert sum(ts) == n
    if n > 0:
        assert 0 not in ts
コード例 #17
0
def test_minimum_sum_tree(n):
    xs = find(tree(),
              lambda x: sum(tree_values(x)) >= n,
              settings=Settings(mutations=100,
                                generations=250,
                                buffer_size=16 * 1024,
                                max_shrinks=10000))
    ts = tree_values(xs)
    assert sum(ts) == n
    if n > 0:
        assert 0 not in ts
コード例 #18
0
def test_sorted_list_subsequences():
    k = 6
    xs = find(
        st.lists(intlist),
        lambda t:
        (len(t) <= 30 and length_of_longest_ordered_sequence(t) >= k),
    )
    assert len(xs) == k
    for i in range(k - 1):
        u = xs[i]
        v = xs[i + 1]
        if len(v) > len(u):
            assert v == u + [0]
コード例 #19
0
def test_integral_float():
    find(st.floats(), lambda x: math.isfinite(x) and int(x) == x)
コード例 #20
0
def test_find_infinity():
    assert find(st.floats(), lambda x: not math.isfinite(x)) == float('inf')
コード例 #21
0
def test_imprecise_pow():
    find(st.floats(), lambda x: math.isfinite(x) and (x * 3.0) / 3.0 != x)
コード例 #22
0
def test_small_integer():
    t = find(st.n_byte_unsigned(8), lambda x: abs(x) <= 100 and abs(x) > 0)
    assert t == 1
コード例 #23
0
def test_find_middling_integer():
    t = find(st.n_byte_unsigned(8),
             lambda x: x >= 2**62 and x <= 2**64 - 2**62)
    assert t == 2**62
コード例 #24
0
def test_minimal_bigger_than_one_is_two():
    assert find(st.floats(), lambda x: x > 1) == 2.0
コード例 #25
0
def test_can_sort_lists():
    x = find(
        st.lists(st.n_byte_unsigned(8)), lambda x: len(set(x)) >= 10
    )
    assert x == list(range(10))
コード例 #26
0
def test_variable_shrink():
    assert find(draw_following, any) == bytes([1])
コード例 #27
0
def test_minimum_sum_lists(n):
    x = find(intlist, lambda xs: sum(xs) >= n)
    assert sum(x) == n
    assert 0 not in x
コード例 #28
0
def test_between_one_and_two():
    t = find(st.floats(), lambda x: 1 < x < 2, settings=Settings(max_shrinks=10 ** 6, generations=10000))
    for k in range(dtoi(1.0), dtoi(t)):
        assert itod(k) <= t
        assert not (1 < itod(k))
コード例 #29
0
def test_minimal_bigger_than_one_is_two():
    assert find(st.floats(), lambda x: x > 1) == 2.0
コード例 #30
0
def test_minimal_hashing():
    import hashlib
    find(
        lambda d: d.draw_bytes(100),
        lambda s: hashlib.sha1(s).hexdigest()[0] == '0'
    )
コード例 #31
0
def test_minimal_mixed():
    xs = find(
        st.lists(st.booleans() | st.tuples(st.booleans(), st.booleans())),
        lambda x: len(x) >= 10
    )
    assert xs == [False] * 10
コード例 #32
0
def test_random_walk():
    find(
        st.lists(st.n_byte_signed(8)).filter(lambda x: len(x) >= 10),
        lambda x: abs(sum(x)) >= 2 ** 64 * math.sqrt(len(x))
    )
コード例 #33
0
def test_can_find_negative_zero():
    find(st.floats(), lambda x: x == 0 and math.copysign(1, x) < 0)
コード例 #34
0
def test_can_find_duplicates():
    x = find(intlist, lambda xs: len(set(xs)) < len(xs))
    assert len(x) == 2
    assert sorted(x) == [0, 0]
コード例 #35
0
def test_minimum_sum_lists(n):
    x = find(intlist, lambda xs: sum(xs) >= n)
    assert sum(x) == n
    assert 0 not in x
コード例 #36
0
def test_low_variance():
    t = find(st.lists(st.floats()).filter(lambda x: len(x) >= 10), lambda x: variance(x) >= 1)
    assert t == [0.0] * 9 + [4.0]
コード例 #37
0
def test_can_find_duplicates():
    x = find(intlist, lambda xs: len(set(xs)) < len(xs))
    assert len(x) == 2
    assert sorted(x) == [0, 0]
コード例 #38
0
def test_sphere():
    t = find(st.lists(st.floats()), lambda x: len(x) >= 3 and sum(map(safesquare, x)) >= 1)
    assert t == [0.0, 0.0, 1.0]
コード例 #39
0
def test_out_of_bounds_overflow_mean():
    find(
        good_list, lambda xs: not (min(xs) <= mean(xs) <= max(xs)))
コード例 #40
0
def test_can_find_negative_zero():
    find(st.floats(), lambda x: x == 0 and math.copysign(1, x) < 0)
コード例 #41
0
def test_find_large_integer():
    t = find(st.n_byte_unsigned(8), lambda x: x >= 2**64 - 100)
    assert t == 2**64 - 100
コード例 #42
0
def test_can_sort_lists():
    x = find(st.lists(st.n_byte_unsigned(8)), lambda x: len(set(x)) >= 10)
    assert x == list(range(10))
コード例 #43
0
def test_variable_shrink():
    assert find(draw_following, any) == bytes([1])
コード例 #44
0
def test_random_walk():
    find(
        st.lists(st.n_byte_signed(8)).filter(lambda x: len(x) >= 10),
        lambda x: abs(sum(x)) >= 2**64 * math.sqrt(len(x)))
コード例 #45
0
def test_find_reasonable_range_float():
    assert find(st.floats(), lambda x: 1 <= x <= 1000) == 1.0
コード例 #46
0
def test_minimal_mixed():
    xs = find(
        st.lists(st.booleans() | st.tuples(st.booleans(), st.booleans())),
        lambda x: len(x) >= 10)
    assert xs == [False] * 10
コード例 #47
0
def test_sum_float():
    x = find(st.lists(st.floats()), lambda x: sum(x) >= 2**32)
    assert sum(x) == 2**32
コード例 #48
0
def test_minimal_hashing():
    import hashlib
    find(lambda d: d.draw_bytes(100),
         lambda s: hashlib.sha1(s).hexdigest()[0] == '0')
コード例 #49
0
def test_can_find_zero_and_it_is_positive():
    t = find(st.floats(), lambda x: x == 0)
    assert math.copysign(1, t) > 0
コード例 #50
0
def test_filtering_just_does_not_loop():
    with pytest.raises(NoSuchExample):
        find(
            st.just(False).filter(bool), lambda x: True
        )
コード例 #51
0
def test_sphere():
    t = find(st.lists(st.floats()),
             lambda x: len(x) >= 3 and sum(map(safesquare, x)) >= 1)
    assert t == [0.0, 0.0, 1.0]
コード例 #52
0
def test_non_reversible_floats():
    good_list = st.lists(st.floats().filter(math.isfinite))
    find(good_list, lambda xs:  sum(xs) != sum(reversed(xs)))
コード例 #53
0
def test_low_variance():
    t = find(
        st.lists(st.floats()).filter(lambda x: len(x) >= 10),
        lambda x: variance(x) >= 1)
    assert t == [0.0] * 9 + [4.0]
コード例 #54
0
def test_minimal_bool_lists():
    t = find(
        st.lists(st.booleans()), lambda x: any(x) and not all(x)
    )
    assert t == [False, True]
コード例 #55
0
def test_non_associative_floats():
    tup = st.tuples(
        st.floats(), st.floats(), st.floats()).filter(
        lambda t: all(math.isfinite(s) for s in t))
    find(tup, lambda x: (x[0] + x[1]) + x[2] != x[0] + (x[1] + x[2]))
コード例 #56
0
def test_can_find_zero_and_it_is_positive():
    t = find(st.floats(), lambda x: x == 0)
    assert math.copysign(1, t) > 0
コード例 #57
0
def test_constant_lists_through_flatmap():
    x = find(
        st.integer_range(0, 99).flatmap(lambda x: st.lists(st.just(x))),
        lambda x: sum(x) >= 100)
    assert sum(x[:-1]) < 100
    assert len(x) * (x[0] - 1) < 100
コード例 #58
0
def test_a_block_of_bytes_simplifies_to_zeros():
    d = find(lambda d: d.draw_bytes(100), lambda x: True)
    assert d == b'\0' * 100
コード例 #59
0
def test_a_block_of_bytes_simplifies_to_lexicographically_smallest(n):
    K = 1000
    d = find(lambda d: d.draw_bytes(K),
             lambda x: len([c for c in x if c > 0]) >= n)
    assert d == b'\0' * (K - n) + b'\1' * n
コード例 #60
0
def test_reverse_sorted_integer_subsequences():
    k = 6
    xs = find(intlist, lambda t: (len(t) <= 30 and length_of_longest_ordered_sequence(reversed(t)) >= k))
    start = xs[0]
    assert xs == list(range(start, -1, -1))