Esempio n. 1
0
def test_streams_copy_as_self():
    x = strategy(streaming(bool)).example()
    assert copy(x) is x
    assert deepcopy(x) is x

    y = x.map(lambda x: not x)
    assert copy(y) is y
    assert deepcopy(y) is y
Esempio n. 2
0
def test_can_save_minimized_in_database():
    spec = streaming(bool)
    t = some_template(spec)
    assert isinstance(t.stream[10], bool)
    s = t.with_value(10, not t.stream[10])
    assert s != t
    sd = via_database(spec, s)
    assert s == sd
Esempio n. 3
0
def test_template_equality():
    t = some_template(streaming(bool))
    t2 = StreamTemplate(t.seed, t.parameter_seed, Stream(t.stream))

    while True:
        t3 = some_template(streaming(bool))
        if t3.seed != t.seed:
            break
    assert t == t2
    assert t != t3
    assert t != 1

    v = t.stream[11]
    t4 = t2.with_value(11, not v)
    assert t4 != t

    t5 = StreamTemplate(t.seed, t.parameter_seed + 1, Stream(t.stream))
    assert t2 != t5

    assert len({t, t2, t3, t4, t5}) == 4
Esempio n. 4
0
def test_check_serialization_preserves_changed_marker():
    strat = strategy(
        streaming(floats_in_range(0.0, 2.2250738585072014e-308)))
    template = strat.draw_template(
        BuildContext(Random(0)), strat.draw_parameter(Random(0)))
    strat.reify(template)[0]
    simpler = next(strat.full_simplify(
        Random(0), template
    ))

    as_basic = strat.to_basic(simpler)
    assert as_basic == strat.to_basic(strat.from_basic(as_basic))
Esempio n. 5
0
def test_streaming():
    assert isinstance(strategy(s.streaming(bool)).example()[100], bool)
Esempio n. 6
0
    one_of((int, (bool, ))),
    sampled_from(range(10)),
    one_of((just('a'), just('b'), just('c'))),
    sampled_from(('a', 'b', 'c')),
    int,
    integers_from(3),
    integers_in_range(-2**32, 2**64),
    float,
    floats_in_range(-2.0, 3.0),
    text_type,
    binary_type,
    bool,
    (bool, bool),
    frozenset({int}),
    complex,
    Fraction,
    Decimal,
    [[bool]],
    OrderedPair,
    ConstantList(int),
    strategy(streaming(int)).map(lambda x: list(x[:2]) and x),
    strategy(int).filter(lambda x: abs(x) > 100),
    floats_in_range(-sys.float_info.max, sys.float_info.max),
    None,
    Random,
]


def parametrize(args, values):
    return pytest.mark.parametrize(args, values, ids=list(map(show, values)))
Esempio n. 7
0
class TestCatalogBuilder(unittest.TestCase):

    @given(dictionary(int, int))
    def test_arbitrary_mapping(self, mapping):
        builder = CatalogBuilder(mapping)
        catalog = builder.create()
        shared_items = set(mapping.items()) & set(catalog.items())
        self.assertEqual(len(shared_items), len(mapping))

    @given(dictionary(int, just(42)))
    def test_constant_mapping(self, mapping):
        builder = CatalogBuilder(mapping)
        catalog = builder.create()
        shared_items = set(mapping.items()) & set(catalog.items())
        self.assertEqual(len(shared_items), len(mapping))

    @given(start=int,
           num=integers_in_range(0, 10000),
           step=integers_in_range(-10000, 10000),
           value=int)
    def test_regular_constant_mapping(self, start, num, step, value):
        assume(step != 0)
        mapping = {key: value for key in range(start, start + num*step, step)}
        builder = CatalogBuilder(mapping)
        catalog = builder.create()
        shared_items = set(mapping.items()) & set(catalog.items())
        self.assertEqual(len(shared_items), len(mapping))

    @given(start=int,
           num=integers_in_range(0, 10000),
           step=integers_in_range(-10000, 10000),
           values=streaming(int))
    def test_regular_mapping(self, start, num, step, values):
        assume(step != 0)
        mapping = {key: value for key, value in zip(range(start, start + num*step, step), values)}
        builder = CatalogBuilder(mapping)
        catalog = builder.create()
        shared_items = set(mapping.items()) & set(catalog.items())
        self.assertEqual(len(shared_items), len(mapping))

    @given(num=integers_in_range(0, 10000),
           key_start=int,
           key_step=integers_in_range(-10000, 10000),
           value_start=int,
           value_step=integers_in_range(-10000, 10000))
    def test_linear_regular_mapping(self, num, key_start, key_step, value_start, value_step):
        assume(key_step != 0)
        assume(value_step != 0)
        mapping = {key: value for key, value in zip(range(key_start, key_start + num*key_step, key_step),
                                                    range(value_start, value_start + num*value_step, value_step))}
        builder = CatalogBuilder(mapping)
        catalog = builder.create()
        shared_items = set(mapping.items()) & set(catalog.items())
        self.assertEqual(len(shared_items), len(mapping))

    @given(dictionary((int, int), int))
    def test_arbitrary_mapping(self, mapping):
        builder = CatalogBuilder(mapping)
        catalog = builder.create()
        shared_items = set(mapping.items()) & set(catalog.items())
        self.assertEqual(len(shared_items), len(mapping))

    @given(i_start=integers_in_range(0, 10),
           i_num=integers_in_range(1, 10),
           i_step=just(1),
           j_start=integers_in_range(0, 10),
           j_num=integers_in_range(1, 10),
           j_step=just(1),
           c=integers_in_range(1, 10))
    def test_linear_regular_mapping_2d(self, i_start, i_num, i_step, j_start, j_num, j_step, c):
        assume(i_step != 0)
        assume(j_step != 0)

        def v(i, j):
            return (i - i_start) * ((j_start + j_num*j_step) - j_start) + (j - j_start) + c

        mapping = {(i, j): v(i, j)
                   for i in range(i_start, i_start + i_num*i_step, i_step)
                   for j in range(j_start, j_start + j_num*j_step, j_step)}

        builder = CatalogBuilder(mapping)
        catalog = builder.create()
        shared_items = set(mapping.items()) & set(catalog.items())
        self.assertEqual(len(shared_items), len(mapping))
Esempio n. 8
0
def test_can_minimize():
    x = minimal(streaming(int), lambda x: x[10] >= 1)
    ts = list(x[:11])
    assert ts == [0] * 10 + [1]
 def build_stream(self, strat):
     return strategy(streaming(strat))
Esempio n. 10
0
def test_streaming():
    assert isinstance(strategy(s.streaming(bool)).example()[100], bool)
Esempio n. 11
0
from __future__ import division, print_function, absolute_import, \
    unicode_literals

from itertools import islice

import pytest
from hypothesis import given, assume
from hypothesis.errors import InvalidArgument
from hypothesis.specifiers import streaming
from hypothesis.utils.show import show
from hypothesis.strategytests import strategy_test_suite
from hypothesis.internal.debug import minimal, via_database, some_template
from hypothesis.internal.compat import text_type, integer_types
from hypothesis.searchstrategy.streams import Stream, StreamTemplate

TestIntStreams = strategy_test_suite(streaming(int))
TestStreamLists = strategy_test_suite(streaming(int))
TestIntStreamStreams = strategy_test_suite(streaming(streaming(int)))


@given([bool])
def test_stream_give_lists(xs):
    s = Stream(iter(xs))
    assert list(s) == xs
    assert list(s) == xs


@given([bool])
def test_can_zip_streams_with_self(xs):
    s = Stream(iter(xs))
    assert list(zip(s, s)) == list(zip(xs, xs))
Esempio n. 12
0
    Bitfields,
    [], (), set(), frozenset(), {},
    NAryTree(bool, bool, bool),
    ABC(bool, bool, bool),
    ABC(bool, bool, int),
    {'a': int, 'b': bool},
    one_of((int, (bool,))),
    sampled_from(range(10)),
    one_of((just('a'), just('b'), just('c'))),
    sampled_from(('a', 'b', 'c')),
    int, integers_from(3), integers_in_range(-2 ** 32, 2 ** 64),
    float, floats_in_range(-2.0, 3.0),
    text_type, binary_type,
    bool,
    (bool, bool),
    frozenset({int}),
    complex,
    Fraction,
    Decimal,
    [[bool]],
    OrderedPair, ConstantList(int),
    strategy(streaming(int)).map(lambda x: list(x[:2]) and x),
    strategy(int).filter(lambda x: abs(x) > 100),
    floats_in_range(-sys.float_info.max, sys.float_info.max),
    None, Random,
]


def parametrize(args, values):
    return pytest.mark.parametrize(args, values, ids=list(map(show, values)))
Esempio n. 13
0
def test_find_streaming_int():
    n = 100
    r = find(streaming(int), lambda x: all(t >= 1 for t in x[:n]))
    assert list(r[:n]) == [1] * n
Esempio n. 14
0
def test_find_streaming_int():
    n = 100
    r = find(streaming(int), lambda x: all(t >= 1 for t in x[:n]))
    assert list(r[:n]) == [1] * n
Esempio n. 15
0
    return strategy(int, settings).flatmap(
        lambda right: strategy(integers_from(0), settings).map(
            lambda length: OrderedPair(right - length, right)))


ConstantList = namedtuple('ConstantList', ('spec',))


@strategy.extend(ConstantList)
def constant_list_strategy(spec, settings):
    return strategy(spec.spec, settings).flatmap(
        lambda v: [just(v)],
    )


EvalledIntStream = strategy(streaming(int)).map(lambda x: list(x[:10]) and x)

ABC = namedtuple('ABC', ('a', 'b', 'c'))

standard_types = [
    Bitfields,
    EvalledIntStream,
    [], (), set(), frozenset(), {},
    NAryTree(bool, bool, bool),
    ABC(bool, bool, bool),
    ABC(bool, bool, int),
    TemplatesFor(one_of(just(i) for i in hrange(10))),
    {'a': int, 'b': bool},
    one_of((int, (bool,))),
    sampled_from(range(10)),
    one_of((just('a'), just('b'), just('c'))),
Esempio n. 16
0
    )
)

TestMappedSampling = strategy_test_suite(
    strategy([int]).filter(bool).flatmap(sampled_from)
)

TestManyFlatmaps = strategy_test_suite(
    strategy(int)
    .flatmap(integers_from)
    .flatmap(integers_from)
    .flatmap(integers_from)
    .flatmap(integers_from)
)

TestIntStreams = strategy_test_suite(streaming(int))
TestStreamLists = strategy_test_suite(streaming(int))
TestIntStreamStreams = strategy_test_suite(streaming(streaming(int)))


TestBoringBitfieldsClass = strategy_test_suite(BoringBitfields)
TestBitfieldsClass = strategy_test_suite(Bitfields)
TestBitfieldsInstance = strategy_test_suite(Bitfields())


TestBitfields = strategy_test_suite([
    basic_strategy(
        generate=lambda r, p: r.getrandbits(128),
        simplify=simplify_bitfield,
        copy=lambda x: x,
    )
Esempio n. 17
0
TestConstantLists = strategy_test_suite(
    strategy(int).flatmap(lambda i: [just(i)]))

TestOrderedPairs = strategy_test_suite(
    strategy(integers_in_range(
        1, 200)).flatmap(lambda e: (integers_in_range(0, e - 1), just(e))))

TestMappedSampling = strategy_test_suite(
    strategy([int]).filter(bool).flatmap(sampled_from))

TestManyFlatmaps = strategy_test_suite(
    strategy(int).flatmap(integers_from).flatmap(integers_from).flatmap(
        integers_from).flatmap(integers_from))

TestIntStreams = strategy_test_suite(streaming(int))
TestStreamLists = strategy_test_suite(streaming(int))
TestIntStreamStreams = strategy_test_suite(streaming(streaming(int)))

TestBoringBitfieldsClass = strategy_test_suite(BoringBitfields)
TestBitfieldsClass = strategy_test_suite(Bitfields)
TestBitfieldsInstance = strategy_test_suite(Bitfields())

TestBitfields = strategy_test_suite([
    basic_strategy(
        generate=lambda r, p: r.getrandbits(128),
        simplify=simplify_bitfield,
        copy=lambda x: x,
    )
])
Esempio n. 18
0
def test_can_zip_streams_with_self(xs):
    s = Stream(iter(xs))
    assert list(zip(s, s)) == list(zip(xs, xs))


def loop(x):
    while True:
        yield x


def test_can_stream_infinite():
    s = Stream(loop(False))
    assert list(islice(s, 100)) == [False] * 100


@given(streaming(text_type))
def test_fetched_repr_is_in_stream_repr(s):
    assert repr(s) == 'Stream(...)'
    assert show(next(iter(s))) in show(s)


def test_cannot_thunk_past_end_of_list():
    with pytest.raises(IndexError):
        Stream([1])._thunk_to(5)


def test_thunking_evaluates_initial_list():
    x = Stream([1, 2, 3])
    x._thunk_to(1)
    assert len(x.fetched) == 1
Esempio n. 19
0
# Copyright (C) 2013-2015 David R. MacIver ([email protected])

# This file is part of Hypothesis (https://github.com/DRMacIver/hypothesis)

# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.

# END HEADER

from __future__ import division, print_function, absolute_import, \
    unicode_literals

from itertools import islice

from hypothesis import given, assume
from hypothesis.specifiers import streaming
from hypothesis.internal.compat import integer_types


@given(streaming(int))
def test_can_adaptively_assume_about_streams(xs):
    for i in islice(xs, 200):
        assume(i >= 0)


@given(streaming(int))
def test_streams_are_arbitrarily_long(ss):
    for i in islice(ss, 100):
        assert isinstance(i, integer_types)
 def evalled_stream(self, strat, i):
     return strategy(streaming(strat)).map(lambda x: list(x[:i]) and x)