コード例 #1
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))}
コード例 #2
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))}
コード例 #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_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)] + [[]]
コード例 #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_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]
コード例 #7
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]
コード例 #8
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)))
コード例 #9
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
コード例 #10
0
def test_minimal_bool_lists():
    t = find(st.lists(st.booleans()), lambda x: any(x) and not all(x))
    assert t == [False, True]
コード例 #11
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))
コード例 #12
0
def test_minimal_bool_lists():
    t = find(
        st.lists(st.booleans()), lambda x: any(x) and not all(x)
    )
    assert t == [False, True]
コード例 #13
0
def test_no_overflow_mean():
    find(
        st.lists(st.floats()).filter(lambda x: x and math.isfinite(sum(x))),
        lambda xs: not (min(xs) <= mean(xs) <= max(xs)))
コード例 #14
0
def test_sum_float():
    x = find(st.lists(st.floats()), lambda x: sum(x) >= 2 ** 32)
    assert sum(x) == 2 ** 32
コード例 #15
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]
コード例 #16
0
from conjecture import find, Settings, NoSuchExample
import conjecture.strategies as st

import math
import pytest
from functools import reduce
import operator


intlist = st.lists(st.n_byte_unsigned(8))


def draw_following(data):
    n = data.draw_bytes(1)[0]
    return data.draw_bytes(n)


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


@pytest.mark.parametrize('n', [0, 10, 100, 200, 501])
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
コード例 #17
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]
コード例 #18
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
コード例 #19
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))
    )
コード例 #20
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))
コード例 #21
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
コード例 #22
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]
コード例 #23
0
def test_sum_float():
    x = find(st.lists(st.floats()), lambda x: sum(x) >= 2**32)
    assert sum(x) == 2**32
コード例 #24
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)))
コード例 #25
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]
コード例 #26
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)))
コード例 #27
0
from conjecture import find, Settings, NoSuchExample
import conjecture.strategies as st
import struct
import math
import pytest
from functools import reduce
import operator

intlist = st.lists(st.n_byte_unsigned(8))


def draw_following(data):
    n = data.draw_bytes(1)[0]
    return data.draw_bytes(n)


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


@pytest.mark.parametrize('n', [0, 10, 100, 200, 501])
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


def test_variable_shrink():
    assert find(draw_following, any) == bytes([1])
コード例 #28
0
def test_no_overflow_mean():
    find(
        st.lists(st.floats()).filter(lambda x: x and math.isfinite(sum(x))),
        lambda xs: not (min(xs) <= mean(xs) <= max(xs)),
    )