Example #1
0
 def define_set_strategy(specifier, settings):
     if not specifier:
         return st.sets(max_size=0)
     else:
         with settings:
             return st.sets(st.one_of(
                 *[self(s, settings) for s in specifier]))
Example #2
0
def type_to_strat(x, opts): # type: (type) -> SearchStrategy
   '''
   Given a type, return a strategy which yields a value of that type. Types maybe complex: Union, NamedTuple, etc.
   For more information, see https://docs.python.org/3/library/typing.html
   Usage:
   >>> type_to_strat(Union[int,str]).exmample()
   . . . 3
   '''
   recur = lambda y: type_to_strat(y, opts)
   if x in primitives:
       prim =  primitives[x].filter(opts.get(x, lambda x: x))
       return prim
   elif hasattr(x, '_fields'):# NamedTuple isn't a type, it's a function
   #elif isinstance(x, Callable): #this catches List[T] for some reason
       name = x.__name__
       fts = OrderedDict(x._field_types)
       vals = map(recur, fts.values())
       # `NamedTuple` is actually a ... `namedtuple` itself
       toArgDict = lambda xs: dict(zip(fts.keys(), xs))
       return st.tuples(*vals).map(lambda ys: x(**toArgDict(ys)))
   elif issubclass(x, Dict):
       return st.dictionaries(*map(recur, x.__parameters__))
   elif issubclass(x, Tuple):
       if x.__tuple_use_ellipsis__: # variable lenth tuple
           element_type = x.__tuple_params__[0]
           return recur(List[element_type]).map(tuple)
       return st.tuples(*map(recur, x.__tuple_params__))
   elif issubclass(x, Union):
       return reduce(operator.ior, map(recur, x.__union_params__))
   elif issubclass(x, Optional):
       # Optional[X] is equivalent to Union[X, type(None)]. second param is always Nonetype.
       value = x.__union_params__[0]
       return (recur(value) | st.none()) # type: SearchStrategy
   else:
       element_type = recur(x.__parameters__[0])
       if issubclass(x, list):
           return st.lists(element_type)
       elif issubclass(x, set):
           return st.sets(element_type)
       elif issubclass(x, Sequence):
           anySizeTuple = recur(Tuple[element_type,...])
           return st.sets(element_type) | st.lists(element_type) | anySizeTuple
       elif issubclass(x, Generator):
           toGen = lambda xs: (x for x in xs) # type: Callable[[Iterable[T]], Generator[T]]
           return recur(List[element_type]).map(toGen)
       # not sure how to create an Iterable (it doesn't have an `__next__` method)
       elif issubclass(x, Iterator)  or issubclass(x, Iteratable):
           return recur(List[element_type]).map(iter)
       else:
           raise ValueError("Could not find strategy for type %s" % x)
Example #3
0
def test_can_form_sets_of_recursive_data():
    trees = st.sets(st.recursive(
        st.booleans(), lambda x: st.lists(x).map(tuple), max_leaves=4))
    xs = find(trees, lambda x: len(x) >= 10)
    assert len(xs) == 10
    assert False in xs
    assert True in xs
Example #4
0
def lists_pairs_nodup(draw, elements=IMMUTABLES, min_size=0, max_size=LISTS_MAX_SIZE):
    """Generate a list of pairs from the given elements with no duplication."""
    n = draw(st.integers(min_value=min_size, max_value=max_size))
    size_n_sets = st.sets(elements, min_size=n, max_size=n)
    keys = draw(size_n_sets)
    vals = draw(size_n_sets)
    return list(izip(keys, vals))
def list_of_sets():
    return st.lists(
        st.sets(
            st.integers(max_value=8),
            max_size=8
        ),
        max_size=16
    )
Example #6
0
def patches():
    """Returns a strategy which generates a Patch object.
    At present, planex.patchqueue.Patchqueue only supports a single
    guard on each patch.
    """
    # If support for multiple guards is added, the max_size constraint
    # can be increased.
    return st.builds(Patch, files(), st.sets(guards(), min_size=0, max_size=1))
def test_sets_of_fixed_length(n):
    x = find(
        sets(integers(), min_size=n, max_size=n), lambda x: True)

    if not n:
        assert x == set()
    else:
        assert x == set(range(min(x), min(x) + n))
def test_can_draw_sets_of_hard_to_find_elements(rnd):
    rarebool = floats(0, 1).map(lambda x: x <= 0.05)
    find_any(
        sets(rarebool, min_size=2),
        lambda x: True,
        random=rnd,
        settings=settings(database=None),
    )
def test_minimize_sets_of_sets():
    elements = integers(1, 100)
    size = 15
    set_of_sets = minimal(sets(frozensets(elements)), lambda s: len(s) >= size)
    assert frozenset() in set_of_sets
    assert len(set_of_sets) == size
    for s in set_of_sets:
        if len(s) > 1:
            assert any(s != t and t.issubset(s) for t in set_of_sets)
def test_can_form_sets_of_recursive_data():
    size = 3

    trees = st.sets(st.recursive(
        st.booleans(),
        lambda x: st.lists(x, min_size=size).map(tuple),
        max_leaves=20))
    xs = minimal(trees, lambda x: len(x) >= size, timeout_after=None)
    assert len(xs) == size
def test_can_form_sets_of_recursive_data():
    trees = st.sets(st.recursive(
        st.booleans(),
        lambda x: st.lists(x, min_size=5).map(tuple),
        max_leaves=20))
    xs = find(trees, lambda x: len(x) >= 10, settings=settings(
        database=None, timeout=20, max_shrinks=1000, max_examples=1000
    ))
    assert len(xs) == 10
Example #12
0
def test_reified_templates_are_simpler(seed):
    s = sets(integers())
    t1 = s.draw_and_produce(Random(seed))
    t2 = s.draw_and_produce(Random(seed))

    assert t1 == t2
    assert not s.strictly_simpler(t1, t2)
    assume(s.reify(t1))
    assert s.strictly_simpler(t1, t2)
    assert not s.strictly_simpler(t2, t1)
def add_extra_searchwords(draw, result):
    for lang in LANGUAGES:
        words = draw(sets(text(SAFE_LETTERS + 'åäöÅÄÖ ',
                               min_size=1, max_size=25)))
        if len(words) == 0:
            event('extra searchwords length {}'.format(len(words)))
            words = None
        else:
            event('extra searchwords length >0')
            words = ', '.join((word.strip() for word in words))
            result['extra_searchwords_{}'.format(lang)] = words
Example #14
0
def test_can_form_sets_of_recursive_data():
    trees = st.sets(st.recursive(
        st.booleans(),
        lambda x: st.lists(x, min_size=5).map(tuple),
        max_leaves=10))
    xs = find(trees, lambda x: len(x) >= 10, settings=settings(
        database=None
    ))
    print(xs)
    assert len(xs) == 10
    assert False in xs
    assert True in xs
    def options_string(cls):
        """
        Strategy for generation of nfs options formatted for an exports line.
        """
        def format_options(opts):
            if not opts:
                return ''
            else:
                return '({0:s})'.format(','.join(opts))

        options = cls.options()
        return builds(format_options, sets(options))
Example #16
0
def _build_node(applications):
    # All the manifestations in `applications`.
    app_manifestations = set(app.volume.manifestation for app in applications if app.volume)
    # A set that contains all of those, plus an arbitrary set of
    # manifestations.
    dataset_ids = frozenset(app.volume.manifestation.dataset_id for app in applications if app.volume)
    manifestations = (
        st.sets(MANIFESTATIONS.filter(lambda m: m.dataset_id not in dataset_ids))
        .map(pset)
        .map(lambda ms: ms.union(app_manifestations))
        .map(lambda ms: dict((m.dataset.dataset_id, m) for m in ms))
    )
    return st.builds(Node, uuid=UUIDS, applications=st.just(applications), manifestations=manifestations)
Example #17
0
def test_simplify_does_not_error_on_unreified_data():
    s = sets(integers())
    for i in range(100):
        t1 = s.draw_and_produce(Random(i))
        t2 = s.draw_and_produce(Random(i))
        if t1.size > 1:
            break

    s.reify(t1)
    simplifiers = list(s.simplifiers(Random(1), t1))
    assert len(simplifiers) > 2
    for s in simplifiers:
        assert list(s(Random(1), t2)) == []
 def export(cls):
     """
     Strategy that generates :py:class:`scality_manila_utils.export.Export`.
     """
     export_point = cls.path()
     host = cls.host()
     options = sets(cls.options(), average_size=3)
     clients = dictionaries(
         keys=host,
         values=options,
         min_size=1,
         average_size=5
     )
     return builds(Export, export_point, clients)
Example #19
0
def test_find_large_union_list():
    def large_mostly_non_overlapping(xs):
        assume(xs)
        assume(all(xs))
        union = reduce(operator.or_, xs)
        return len(union) >= 30

    result = minimal(lists(sets(integers())), large_mostly_non_overlapping, timeout_after=30)
    union = reduce(operator.or_, result)
    assert len(union) == 30
    assert max(union) == min(union) + len(union) - 1
    for x in result:
        for y in result:
            if x is not y:
                assert not (x & y)
Example #20
0
def objects(elements=None, *,
            required_fields=None,
            optional_fields=None,
            min_size=None,
            average_size=None,
            max_size=None):
    """Returns a strategy that generates dicts of specified `elements`.

    The `elements` must be valid Hypothesis strategy. The keys are ensured to
    be string only as JSON requires.

    While choice of `elements` left here for user side, it's not recommended to
    use anything that produces non-serializable to JSON values.

    Also possible to ensure that some fields with values generated by a certain
    strategy are always exists (`required_fields`) or just optional
    (`optional_fields`).
    """
    def check_type(varname, var, expected):
        if not isinstance(var, expected):
            raise TypeError('{} must be {}, got {}'.format(
                varname, expected, type(var)))

    acc = []
    if required_fields:
        check_type('required_fields', required_fields, dict)
        for key in required_fields:
            check_type('required field name', key, str)
        acc.append(st.fixed_dictionaries(required_fields))

    if optional_fields:
        check_type('optional_fields', optional_fields, dict)
        for key in optional_fields:
            check_type('optional field name', key, str)
        acc.append(st.sets(st.sampled_from(optional_fields)).flatmap(
            lambda keys: st.fixed_dictionaries({key: optional_fields[key]
                                                for key in keys})))
    if elements:
        acc.append(st.dictionaries(strings(), elements,
                                   min_size=min_size,
                                   average_size=average_size,
                                   max_size=max_size))

    if not acc:
        raise RuntimeError('object must have any strategy for fields')

    return st.tuples(*reversed(acc)).map(
        lambda s: reduce(lambda a, d: dict(a, **d), s))
Example #21
0
 def test_change(self, C, data):
     """
     Changes work.
     """
     # Take the first attribute, and change it.
     assume(fields(C))  # Skip classes with no attributes.
     field_names = [a.name for a in fields(C)]
     original = C()
     chosen_names = data.draw(st.sets(st.sampled_from(field_names)))
     # We pay special attention to private attributes, they should behave
     # like in `__init__`.
     change_dict = {name.replace('_', ''): data.draw(st.integers())
                    for name in chosen_names}
     changed = evolve(original, **change_dict)
     for name in chosen_names:
         assert getattr(changed, name) == change_dict[name.replace('_', '')]
Example #22
0
def test_template_equality():
    s = sets(integers())
    t = s.draw_and_produce(Random(1))
    assert t != 1

    t2 = s.draw_and_produce(Random(1))
    assert t is not t2
    assert t == t2
    s.reify(t2)
    assert t == t2
    assert hash(t) == hash(t2)

    t3 = s.draw_and_produce(Random(1))
    s.reify(t3)
    for ts in s.full_simplify(Random(1), t3):
        assert t3 != ts
Example #23
0
    def test_change(self, C, data):
        """
        Changes work.
        """
        # Take the first attribute, and change it.
        assume(fields(C))  # Skip classes with no attributes.
        field_names = [a.name for a in fields(C)]
        original = C()
        chosen_names = data.draw(st.sets(st.sampled_from(field_names)))
        change_dict = {name: data.draw(st.integers())
                       for name in chosen_names}

        with pytest.deprecated_call():
            changed = assoc(original, **change_dict)

        for k, v in change_dict.items():
            assert getattr(changed, k) == v
Example #24
0
def test_struct(data):
    _elements = one_of(characters(), integers(), text())
    _sets = sets(elements=_elements, min_size=1, average_size=5)
    allowed_keys = data.draw(_sets)

    keys = list(allowed_keys)
    values = data.draw(lists(elements=_elements, min_size=len(keys), max_size=len(keys)))
    s = py_tools.Struct(allowed_keys)

    for key, value in zip(keys, values):
        s[key] = value
        assert s[key] == value

    assert s == {k: v for k, v in zip(keys, values)}

    prohibited_keys = {k for k in data.draw(_sets) if k not in allowed_keys}
    for key in prohibited_keys:
        with pytest.raises(KeyError):
            s[key] = data.draw(_elements)
Example #25
0
def test_template_equality(seed):
    s = sets(integers())
    t = s.draw_and_produce(Random(seed))
    assert t != 1

    t2 = s.draw_and_produce(Random(seed))
    assert t is not t2
    assert t == t2
    s.reify(t2)
    assert t == t2
    assert hash(t) == hash(t2)

    t3 = s.draw_and_produce(Random(seed))
    s.reify(t3)
    simplified = False
    for ts in s.full_simplify(Random(seed), t3):
        simplified = True
        assert t3 != ts
    assume(simplified)
def test_regression_issue_1230():
    strategy = st.builds(
        lambda x, y: dict((list(x.items()) + list(y.items()))),
        st.fixed_dictionaries({'0': st.text()}),
        st.builds(
            lambda dictionary, keys: {key: dictionary[key] for key in keys},
            st.fixed_dictionaries({
                '1': st.lists(elements=st.sampled_from(['a']))
            }),
            st.sets(elements=st.sampled_from(['1']))
        )
    )

    @seed(81581571036124932593143257836081491416)
    @settings(database=None)
    @given(strategy)
    def test_false_is_false(params):
        assume(params.get('0') not in ('', '\x00'))
        raise ValueError()

    with pytest.raises(ValueError):
        test_false_is_false()
Example #27
0
def test_can_draw_sets_of_hard_to_find_elements(rnd):
    rarebool = floats(0, 1).map(lambda x: x <= 0.01)
    find(
        sets(rarebool, min_size=2), lambda x: True,
        random=rnd, settings=settings(database=None))
Example #28
0
    assert p.state == states.INITIAL
    assert p.state_history == [states.INITIAL]
    assert p._stage_count == 0
    assert p.current_stage == 0
    assert type(p.lock) == type(threading.Lock())
    assert type(p._completed_flag) == type(threading.Event())
    assert p.completed == False


# ------------------------------------------------------------------------------
#
@given(t=st.text(),
       l=st.lists(st.text()),
       i=st.integers().filter(lambda x: type(x) == int),
       b=st.booleans(),
       se=st.sets(st.text()))
def test_pipeline_assignment_exceptions(t, l, i, b, se):

    p = Pipeline()

    data_type = [t, l, i, b, se]

    for data in data_type:

        if not isinstance(data, str):
            with pytest.raises(TypeError):
                p.name = data

        with pytest.raises(TypeError):
            p.stages = data
Example #29
0
class DefinitionTests(TestCase):
    def test_roundtrip_examples(self):
        # tree has this structure:
        #
        #      ┌--0--┐
        #      |     |
        #   ┌--1--┌--2--┐
        #   |     |     |
        # ┌-3-┐ ┌-4-┐ ┌-5-┐
        # |   | |   | |   |
        # 6   7 8   9 10 11

        edges = [
            ("0", "1"),
            ("0", "2"),
            ("1", "3"),
            ("1", "4"),
            ("2", "4"),
            ("2", "5"),
            ("3", "6"),
            ("3", "7"),
            ("4", "8"),
            ("4", "9"),
            ("5", "10"),
            ("5", "11"),
        ]
        paths = edges_to_paths("0", edges)
        tree = paths_to_tree(paths)

        for codes, query in [
                # the root element
            ({"0"}, ["0"]),
                # an intermediate element
            ({"2"}, ["2"]),
                # a leaf element
            ({"8"}, ["8"]),
                # everything under intermediate element, inclusive
            ({"2", "4", "5", "8", "9", "10", "11"}, ["2<"]),
                # everything under intermediate element, exclusive
            ({"4", "5", "8", "9", "10", "11"}, ["4<", "5<"]),
                # everything under intermediate element, except another intermediate element
            ({"2", "5", "8", "9", "10", "11"}, ["2<", "~4"]),
                # everything under intermediate element, except leaf element
            ({"2", "4", "5", "9", "10", "11"}, ["2<", "~8"]),
                # everything under root element, except intermediate element and its children (i)
            ({"0", "1", "2", "3", "4", "6", "7", "8", "9"}, ["0", "1<", "2"]),
                # everything under root element, except intermediate element and its children (ii)
            (
                {"0", "1", "2", "3", "5", "6", "7", "10", "11"},
                ["0", "1", "2", "3<", "5<"],
            ),
        ]:
            with self.subTest(codes=codes, query=query):
                defn1 = Definition.from_codes(codes, tree)
                self.assertEqual(sorted(str(e) for e in defn1.elements),
                                 sorted(query))
                self.assertEqual(defn1.codes(tree), codes)

                defn2 = Definition.from_query(query)
                self.assertEqual(sorted(str(e) for e in defn2.elements),
                                 sorted(query))
                self.assertEqual(defn2.codes(tree), codes)

    @settings(deadline=None)
    @given(dags(24), st.sets(st.sampled_from(range(16))), st.floats(0.1, 0.5))
    def test_roundtrip(self, dag, codes, r):
        edges = [(parent, child) for parent, children in dag.items()
                 for child in children]
        paths = edges_to_paths(0, edges)
        tree = paths_to_tree(paths)
        definition = Definition.from_codes(codes, tree, r)
        self.assertEqual(definition.codes(tree), codes)
        fragments = [e.fragment for e in definition.elements]
        self.assertEqual(len(fragments), len(set(fragments)))
        definition_codes = [e.code for e in definition.elements]
        self.assertEqual(len(definition_codes), len(set(definition_codes)))
Example #30
0
class FeaturesTestCase(TestCase):
    def test_read(self):
        self.assertTrue(isinstance(extractor.pos_tags, tuple))
        self.assertIn('PROPN', extractor.pos_tags)
        self.assertIn('CONJ', extractor.pos_tags)

        self.assertTrue(isinstance(extractor.morph, dict))
        self.assertIn('Case', extractor.morph)
        self.assertIn('Definite', extractor.morph)
        self.assertIn('Number', extractor.morph)

        self.assertTrue(isinstance(extractor.lemmas, dict))
        self.assertIn('_', extractor.lemmas)
        self.assertIn('\xa0', extractor.lemmas)
        self.assertIn('Atenas', extractor.lemmas)
        self.assertIn('ordea', extractor.lemmas)

    @given(sets(text()))
    def test_featurise_lemma(self, lemmas):
        assume(all([lemma not in ['_', '\xa0'] for lemma in lemmas]))

        extractor = Extractor()
        for lemma in lemmas:
            extractor.lemmas[lemma]

        self.assertEqual(extractor.get_vocab_sizes()['lemmas'],
                         len(lemmas) + 2)

        self.assertEqual(extractor.featurise_lemma('_'), 0)
        self.assertEqual(extractor.featurise_lemma('\xa0'), 1)

        res = [extractor.featurise_lemma(lemma) for lemma in lemmas]
        self.assertEqual(len(res), len(lemmas))
        self.assertTrue(all([number not in [0, 1] for number in res]))

    @given(sampled_from(extractor.pos_tags))
    def test_featurise_pos_tag(self, pos_tag):
        res = extractor.featurise_pos_tag(pos_tag)
        self.assertTrue(isinstance(res, int))
        self.assertTrue(res > 0)
        self.assertTrue(res <= len(extractor.pos_tags))

    @given(subdicts(extractor.morph))
    def test_featurise_morph(self, subdict):
        res = extractor.featurise_morph(subdict)

        self.assertTrue(isinstance(res, np.ndarray))
        self.assertEqual(
            len(res), sum([len(value) for value in extractor.morph.values()]))

        self.assertTrue(all([i == 0 or i == 1 for i in res]))
        self.assertEqual(len([i for i in res if i == 1]),
                         sum([len(value) for value in subdict.values()]))

    def test_model_files(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            path = os.path.join(temp_dir, 'model')
            extractor.write_to_model_file(path)
            new_ext = Extractor.create_from_model_file(path)

        self.assertTrue(isinstance(new_ext, Extractor))

        self.assertEqual(new_ext.lemmas, extractor.lemmas)
        self.assertEqual(new_ext.morph, extractor.morph)
        self.assertEqual(new_ext.pos_tags, extractor.pos_tags)

        self.assertFalse(new_ext.ignore_lemmas)
        self.assertFalse(new_ext.ignore_morph)

    def test_model_files_error(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            path = os.path.join(temp_dir, 'model')
            with self.assertRaises(OSError):
                Extractor.create_from_model_file(path)

            with open(path, 'w') as f:
                f.write('hi')

            with self.assertRaises(OSError):
                Extractor.create_from_model_file(path)

        assert not os.path.exists(temp_dir)

        with self.assertRaises(OSError):
            extractor.write_to_model_file(path)
Example #31
0
# END HEADER

from __future__ import division, print_function, absolute_import, \
    unicode_literals

import pytest
from hypothesis.strategies import just, sets, text, lists, binary, \
    floats, one_of, tuples, randoms, booleans, integers, frozensets, \
    sampled_from, complex_numbers, fixed_dictionaries
from hypothesis.searchstrategy.narytree import n_ary_tree
from hypothesis.searchstrategy.strategies import BadData, strategy


@pytest.mark.parametrize(('specifier', 'data'), [
    (sets(text()), 0j),
    (complex_numbers(), set('hi')),
    (lists(sets(booleans())), 0),
    (just(1), 'hi'),
    (binary(), 0.0),
    (binary(), frozenset()),
    (fixed_dictionaries({True: sets(integers())}), []),
    (randoms(), []),
    (integers(), ''),
    (integers(), [0, '']),
    (text(), 'kittens'),
    (tuples(integers(), integers(), integers()), (1, 2)),
    (sampled_from((1, 2, 3)), 'fish'),
    (sampled_from((1, 2, 3)), 5),
    (sampled_from((1, 2, 3)), -2),
    (one_of(integers(), floats()), 1),
Example #32
0
 class Foo(ForkingTestCase):
     @given(sets(booleans()))
     def runs_normally(self, x):
         pass
def test_minimize_3_set():
    assert minimal(sets(integers()),
                   lambda x: len(x) >= 3) in ({0, 1, 2}, {-1, 0, 1})
Example #34
0
        test_reusable_strategies_are_all_reusable
    )
    test_reusable_strategies_are_all_reusable = example(st.tuples(s))(
        test_reusable_strategies_are_all_reusable
    )


def test_composing_breaks_reusability():
    s = st.integers()
    assert s.has_reusable_values
    assert not s.filter(lambda x: True).has_reusable_values
    assert not s.map(lambda x: x).has_reusable_values
    assert not s.flatmap(lambda x: st.just(x)).has_reusable_values


@pytest.mark.parametrize(
    "strat",
    [
        st.lists(st.booleans()),
        st.sets(st.booleans()),
        st.dictionaries(st.booleans(), st.booleans()),
    ],
)
def test_mutable_collections_do_not_have_reusable_values(strat):
    assert not strat.has_reusable_values


def test_recursion_does_not_break_reusability():
    x = st.deferred(lambda: st.none() | st.tuples(x))
    assert x.has_reusable_values
    nothing,
    sets,
    text,
    tuples,
)

from tests.common.debug import find_any, minimal
from tests.common.utils import flaky


@pytest.mark.parametrize(
    ("col", "strat"),
    [
        ((), tuples()),
        ([], lists(none(), max_size=0)),
        (set(), sets(none(), max_size=0)),
        (frozenset(), frozensets(none(), max_size=0)),
        ({}, fixed_dictionaries({})),
        ({}, fixed_dictionaries({}, optional={})),
        (OrderedDict(), fixed_dictionaries(OrderedDict(), optional=OrderedDict())),
        ({}, fixed_dictionaries({}, optional={1: booleans()})),
        ({0: False}, fixed_dictionaries({0: booleans()}, optional={1: booleans()})),
        ({}, fixed_dictionaries({}, optional={(): booleans(), 0: booleans()})),
        ([], lists(nothing())),
        ([], lists(nothing(), unique=True)),
    ],
)
def test_find_empty_collection_gives_empty(col, strat):
    assert minimal(strat, lambda x: True) == col


test_sampled_from_large_number_can_mix = define_test(
    lists(sampled_from(range(50))), 0.1, lambda x: len(set(x)) >= 25,
    condition=lambda t: len(t) >= 50,
)


test_sampled_from_often_distorted = define_test(
    lists(sampled_from(range(5))), 0.28, distorted_value,
    condition=lambda x: len(x) >= 3,
)


test_non_empty_subset_of_two_is_usually_large = define_test(
    sets(sampled_from((1, 2))), 0.15,
    lambda t: len(t) == 2
)

test_subset_of_ten_is_sometimes_empty = define_test(
    sets(integers(1, 10)), 0.05, lambda t: len(t) == 0
)

test_subset_of_ten_with_large_average_is_usually_full = define_test(
    sets(integers(1, 10), average_size=9.5), 0.6, lambda t: len(t) == 10
)


test_mostly_sensible_floats = define_test(
    floats(), 0.5,
    lambda t: t + 1 > t
  def testDistribution(self, dist_name, data):
    if tf.executing_eagerly() != (FLAGS.tf_mode == 'eager'):
      return
    tf1.set_random_seed(
        data.draw(
            hpnp.arrays(dtype=np.int64, shape=[]).filter(lambda x: x != 0)))
    dist, batch_shape = data.draw(
        distributions(dist_name=dist_name, enable_vars=True))
    batch_shape2 = data.draw(tfp_hps.broadcast_compatible_shape(batch_shape))
    dist2, _ = data.draw(
        distributions(
            dist_name=dist_name,
            batch_shape=batch_shape2,
            event_dim=get_event_dim(dist),
            enable_vars=True))
    del batch_shape
    logging.info(
        'distribution: %s; parameters used: %s', dist,
        [k for k, v in six.iteritems(dist.parameters) if v is not None])
    self.evaluate([var.initializer for var in dist.variables])
    for k, v in six.iteritems(dist.parameters):
      if not tensor_util.is_mutable(v):
        continue
      try:
        self.assertIs(getattr(dist, k), v)
      except AssertionError as e:
        raise AssertionError(
            'No attr found for parameter {} of distribution {}: \n{}'.format(
                k, dist_name, e))

    for stat in data.draw(
        hps.sets(
            hps.one_of(
                map(hps.just, [
                    'covariance', 'entropy', 'mean', 'mode', 'stddev',
                    'variance'
                ])),
            min_size=3,
            max_size=3)):
      logging.info('%s.%s', dist_name, stat)
      try:
        with tfp_hps.assert_no_excessive_var_usage(
            'statistic `{}` of `{}`'.format(stat, dist)):
          getattr(dist, stat)()

      except NotImplementedError:
        pass

    with tf.GradientTape() as tape:
      with tfp_hps.assert_no_excessive_var_usage(
          'method `sample` of `{}`'.format(dist)):
        sample = dist.sample()
    if dist.reparameterization_type == tfd.FULLY_REPARAMETERIZED:
      grads = tape.gradient(sample, dist.variables)
      for grad, var in zip(grads, dist.variables):
        var_name = var.name.rstrip('_0123456789:')
        if var_name in NO_SAMPLE_PARAM_GRADS.get(dist_name, ()):
          continue
        if grad is None:
          raise AssertionError(
              'Missing sample -> {} grad for distribution {}'.format(
                  var_name, dist_name))

    # Turn off validations, since log_prob can choke on dist's own samples.
    # Also, to relax conversion counts for KL (might do >2 w/ validate_args).
    dist = dist.copy(validate_args=False)
    dist2 = dist2.copy(validate_args=False)

    try:
      for d1, d2 in (dist, dist2), (dist2, dist):
        with tf.GradientTape() as tape:
          with tfp_hps.assert_no_excessive_var_usage(
              '`kl_divergence` of (`{}` (vars {}), `{}` (vars {}))'.format(
                  d1, d1.variables, d2, d2.variables),
              max_permissible=1):  # No validation => 1 convert per var.
            kl = d1.kl_divergence(d2)
        wrt_vars = list(d1.variables) + list(d2.variables)
        grads = tape.gradient(kl, wrt_vars)
        for grad, var in zip(grads, wrt_vars):
          if grad is None and dist_name not in NO_KL_PARAM_GRADS:
            raise AssertionError('Missing KL({} || {}) -> {} grad:\n'
                                 '{} vars: {}\n{} vars: {}'.format(
                                     d1, d2, var, d1, d1.variables, d2,
                                     d2.variables))
    except NotImplementedError:
      pass

    if dist_name not in NO_LOG_PROB_PARAM_GRADS:
      with tf.GradientTape() as tape:
        lp = dist.log_prob(tf.stop_gradient(sample))
      grads = tape.gradient(lp, dist.variables)
      for grad, var in zip(grads, dist.variables):
        if grad is None:
          raise AssertionError(
              'Missing log_prob -> {} grad for distribution {}'.format(
                  var, dist_name))

    for evaluative in data.draw(
        hps.sets(
            hps.one_of(
                map(hps.just, [
                    'log_prob', 'prob', 'log_cdf', 'cdf',
                    'log_survival_function', 'survival_function'
                ])),
            min_size=3,
            max_size=3)):
      logging.info('%s.%s', dist_name, evaluative)
      try:
        with tfp_hps.assert_no_excessive_var_usage(
            'evaluative `{}` of `{}`'.format(evaluative, dist),
            max_permissible=1):  # No validation => 1 convert
          getattr(dist, evaluative)(sample)
      except NotImplementedError:
        pass
Example #38
0
from hypothesis.errors import InvalidArgument
from hypothesis.strategies import sets, lists, floats, randoms, integers


def test_unique_lists_error_on_too_large_average_size():
    with pytest.raises(InvalidArgument):
        lists(integers(), unique=True, average_size=10, max_size=5).example()


@given(randoms())
@settings(max_examples=5)
def test_can_draw_sets_of_hard_to_find_elements(rnd):
    rarebool = floats(0, 1).map(lambda x: x <= 0.01)
    find(
        sets(rarebool, min_size=2), lambda x: True,
        random=rnd, settings=settings(database=None))


def test_sets_of_small_average_size():
    assert len(sets(integers(), average_size=1.0).example()) <= 10


@given(sets(max_size=0))
def test_empty_sets(x):
    assert x == set()


@given(sets(integers(), max_size=2))
def test_bounded_size_sets(x):
    assert len(x) <= 2
def test_minimize_3_set():
    assert minimal(sets(integers()), lambda x: len(x) >= 3) in (
        set((0, 1, 2)),
        set((-1, 0, 1)),
    )
def test_minimize_list_of_sets():
    assert find(
        lists(sets(booleans())),
        lambda x: len(list(filter(None, x))) >= 3) == (
        [set((False,))] * 3
    )
Example #41
0
from hypothesis import given, strategies as st
from openff.toolkit.topology import Molecule as OFFMolecule
import numpy as np

import polymetrizer as pet
from polymetrizer.oligomer import (Oligomer, Monomer,
                                   AtomWrapper, HYDROGEN,
                                   create_hydrogen_caps)


from .data import PEGMA_R_SMILES,  BMA_R_SMILES, SPLIT_MOL_R_SMILES, SPLIT_MOL_R_LINKAGES, FULL_MOL_SMARTS

CAPLIST = [(1, HYDROGEN), (2, HYDROGEN), (3, HYDROGEN)]
SUBSTITUENTS = {i: CAPLIST for i in range(1, 10)}

@given(r_numbers=st.sets(st.integers()))
def test_create_hydrogen_caps(r_numbers):
    r_group_numbers = list(r_numbers)
    caps = create_hydrogen_caps(r_group_numbers)
    assert list(caps) == r_group_numbers
    assert all(len(x) == 1 for x in caps.values())
    assert all(x[0][0] == 1 for x in caps.values())

class TestMonomer:
    @pytest.mark.parametrize(
        "smiles, r_group_indices, central_indices",
        [
            ("[1*:1][H]", {1: 0}, [1]),
            ("[R1][H]", {1: 0}, [1]),
            ("[*:3][H]", {3: 0}, [1]),
            ("[R3][H]", {3: 0}, [1]),
def test_can_draw_empty_set_from_unsatisfiable_strategy():
    assert find_any(sets(integers().filter(lambda s: False))) == set()
Example #43
0
class FormatMixin:
    """
    Tests for the file format.
    """
    def setUp(self):
        fd, self.temp_file = tempfile.mkstemp(suffix=".kas",
                                              prefix="kas_rt_test")
        os.close(fd)

    def tearDown(self):
        os.unlink(self.temp_file)

    def test_header_format(self):
        for n in range(10):
            kas.dump(
                {str(j): np.zeros(1)
                 for j in range(n)},
                self.temp_file,
                engine=self.engine,
            )
            with open(self.temp_file, "rb") as f:
                contents = f.read()
            self.assertEqual(contents[0:8], store.MAGIC)
            major, minor, num_items, size = struct.unpack(
                "<HHIQ", contents[8:24])
            self.assertEqual(major, store.VERSION_MAJOR)
            self.assertEqual(minor, store.VERSION_MINOR)
            self.assertEqual(num_items, n)
            self.assertEqual(size, len(contents))
            trailer = contents[24:store.HEADER_SIZE]
            # The remainder should be zeros.
            self.assertEqual(
                trailer, bytearray(0 for _ in range(store.HEADER_SIZE - 24)))

    def test_zero_items(self):
        kas.dump({}, self.temp_file, engine=self.engine)
        with open(self.temp_file, "rb") as f:
            contents = f.read()
        self.assertEqual(len(contents), 64)

    def test_item_descriptor_format(self):
        for n in range(10):
            kas.dump(
                {str(j): j * np.ones(j)
                 for j in range(n)},
                self.temp_file,
                engine=self.engine,
            )
            with open(self.temp_file, "rb") as f:
                contents = f.read()
            self.assertEqual(struct.unpack("<I", contents[12:16])[0], n)
            offset = store.HEADER_SIZE
            for _ in range(n):
                descriptor = contents[offset:offset +
                                      store.ITEM_DESCRIPTOR_SIZE]
                offset += store.ITEM_DESCRIPTOR_SIZE
                type_ = struct.unpack("<B", descriptor[0:1])[0]
                key_start, key_len, array_start, array_len = struct.unpack(
                    "<QQQQ", descriptor[8:40])
                trailer = descriptor[40:store.ITEM_DESCRIPTOR_SIZE]
                # The remainder should be zeros.
                self.assertEqual(
                    trailer,
                    bytearray(0
                              for _ in range(store.ITEM_DESCRIPTOR_SIZE - 40)),
                )
                self.assertEqual(descriptor[1:4], bytearray([0, 0, 0]))
                self.assertEqual(type_, store.FLOAT64)
                self.assertGreater(key_start, 0)
                self.assertGreater(key_len, 0)
                self.assertGreater(array_start, 0)
                self.assertGreaterEqual(array_len, 0)

    def validate_storage(self, data):
        kas.dump(data, self.temp_file, engine=self.engine)
        with open(self.temp_file, "rb") as f:
            contents = f.read()
        offset = store.HEADER_SIZE
        descriptors = []
        for _ in range(len(data)):
            descriptor = store.ItemDescriptor.unpack(
                contents[offset:offset + store.ItemDescriptor.size])
            descriptors.append(descriptor)
            offset += store.ItemDescriptor.size
        # Keys must be sorted lexicographically.
        sorted_keys = sorted(data.keys())
        # Keys should be packed sequentially immediately after the descriptors.
        offset = store.HEADER_SIZE + len(data) * store.ITEM_DESCRIPTOR_SIZE
        for d, key in zip(descriptors, sorted_keys):
            self.assertEqual(d.key_start, offset)
            unpacked_key = contents[d.key_start:d.key_start + d.key_len]
            self.assertEqual(key.encode("utf8"), unpacked_key)
            offset += d.key_len
        # Arrays should be packed sequentially immediately after the keys on
        # 8 byte boundaries
        for d, key in zip(descriptors, sorted_keys):
            remainder = offset % 8
            if remainder != 0:
                offset += 8 - remainder
            self.assertEqual(d.array_start, offset)
            nbytes = d.array_len * store.type_size(d.type)
            array = np.frombuffer(
                contents[d.array_start:d.array_start + nbytes],
                dtype=store.type_to_np_dtype_map[d.type],
            )
            np.testing.assert_equal(data[key], array)
            offset += nbytes

    def test_simple_key_storage(self):
        for n in range(10):
            self.validate_storage(
                {"a" * (j + 1): np.ones(1)
                 for j in range(n)})

    def test_simple_array_storage(self):
        for n in range(10):
            self.validate_storage({str(j): j * np.ones(j) for j in range(n)})

    # Note that hypothesis seems to be leaking memory, so when we're running tests
    # against the C API for memory leaks this must be commented out.
    @hypothesis.given(keys=hst.sets(hst.text(alphabet=key_alphabet,
                                             min_size=1),
                                    min_size=1))
    def test_many_keys(self, keys):
        data = {
            key: np.ones(j, dtype=np.int32) * j
            for j, key in enumerate(keys)
        }
        self.validate_storage(data)
Example #44
0
        xs.remove(y)
    except ValueError:
        pass

    assert y not in xs


@fails
@given(lists(integers(), average_size=25.0), integers())
def test_removing_an_element_from_a_non_unique_list(xs, y):
    assume(y in xs)
    xs.remove(y)
    assert y not in xs


@given(sets(sampled_from(list(range(10)))))
def test_can_test_sets_sampled_from(xs):
    assert all(isinstance(x, int) for x in xs)
    assert all(0 <= x < 10 for x in xs)


mix = one_of(sampled_from([1, 2, 3]), text())


@fails
@given(mix, mix)
def test_can_mix_sampling_with_generating(x, y):
    assert type(x) == type(y)


@fails
def test_minimize_list_of_sets():
    assert minimal(lists(sets(booleans())),
                   lambda x: len(list(filter(None, x))) >= 3) == ([{False}] *
                                                                  3)
test_sampled_from_large_number_can_mix = define_test(
    lists(sampled_from(range(50)), min_size=50),
    0.1,
    lambda x: len(set(x)) >= 25,
)

test_sampled_from_often_distorted = define_test(
    lists(sampled_from(range(5))),
    0.28,
    distorted_value,
    condition=lambda x: len(x) >= 3,
)

test_non_empty_subset_of_two_is_usually_large = define_test(
    sets(sampled_from((1, 2))), 0.1, lambda t: len(t) == 2)

test_subset_of_ten_is_sometimes_empty = define_test(sets(integers(1,
                                                                  10)), 0.05,
                                                    lambda t: len(t) == 0)

test_mostly_sensible_floats = define_test(floats(), 0.5, lambda t: t + 1 > t)

test_mostly_largish_floats = define_test(
    floats(),
    0.5,
    lambda t: t + 1 > 1,
    condition=lambda x: x > 0,
)

test_ints_can_occasionally_be_really_large = define_test(
def test_minimize_3_set_of_tuples():
    assert minimal(sets(tuples(integers())), lambda x: len(x) >= 2) == {(0, ),
                                                                        (1, )}
Example #48
0
    s = st.sampled_from(range(n))
    for i in range(n):
        if i != target:
            s = forbid(s, i)

    return s


@given(rare_value_strategy(n=128, target=80))
def test_chained_filters_find_rare_value(x):
    assert x == 80


@fails_with(InvalidArgument)
@given(st.sets(st.sampled_from(range(10)), min_size=11))
def test_unsat_sets_of_samples(x):
    assert False


@given(st.sets(st.sampled_from(range(50)), min_size=50))
def test_efficient_sets_of_samples(x):
    assert x == set(range(50))


class AnEnum(enum.Enum):
    a = enum.auto()
    b = enum.auto()


def test_enum_repr_uses_class_not_a_list():
Example #49
0
    result = runner.invoke(["sync"])
    assert not result.exception
    assert set(result.output.splitlines()) == {
        "Syncing bambaz",
        "Syncing foobar",
    }


collections_strategy = st.sets(
    st.text(
        st.characters(
            blacklist_characters=set(
                "./\x00"),  # Invalid chars on POSIX filesystems
            # Surrogates can't be encoded to utf-8 in Python
            blacklist_categories={"Cs"},
        ),
        min_size=1,
        max_size=50,
    ),
    min_size=1,
)


# XXX: https://github.com/pimutils/vdirsyncer/issues/617
@pytest.mark.skipif(sys.platform == "darwin",
                    reason="This test inexplicably fails")
@pytest.mark.parametrize(
    "collections",
    [
        ("persönlich", ),
 def resolve_Set(thing):
     return st.sets(st.from_type(thing.__args__[0]))
    def testDistribution(self, dist_name, data):
        seed = test_util.test_seed()
        # Explicitly draw event_dim here to avoid relying on _params_event_ndims
        # later, so this test can support distributions that do not implement the
        # slicing protocol.
        event_dim = data.draw(hps.integers(min_value=2, max_value=6))
        dist = data.draw(
            dhps.distributions(dist_name=dist_name,
                               event_dim=event_dim,
                               enable_vars=True))
        batch_shape = dist.batch_shape
        batch_shape2 = data.draw(
            tfp_hps.broadcast_compatible_shape(batch_shape))
        dist2 = data.draw(
            dhps.distributions(dist_name=dist_name,
                               batch_shape=batch_shape2,
                               event_dim=event_dim,
                               enable_vars=True))
        self.evaluate([var.initializer for var in dist.variables])

        # Check that the distribution passes Variables through to the accessor
        # properties (without converting them to Tensor or anything like that).
        for k, v in six.iteritems(dist.parameters):
            if not tensor_util.is_ref(v):
                continue
            self.assertIs(getattr(dist, k), v)

        # Check that standard statistics do not read distribution parameters more
        # than twice (once in the stat itself and up to once in any validation
        # assertions).
        max_permissible = 2 + extra_tensor_conversions_allowed(dist)
        for stat in sorted(
                data.draw(
                    hps.sets(hps.one_of(
                        map(hps.just, [
                            'covariance', 'entropy', 'mean', 'mode', 'stddev',
                            'variance'
                        ])),
                             min_size=3,
                             max_size=3))):
            hp.note('Testing excessive var usage in {}.{}'.format(
                dist_name, stat))
            try:
                with tfp_hps.assert_no_excessive_var_usage(
                        'statistic `{}` of `{}`'.format(stat, dist),
                        max_permissible=max_permissible):
                    getattr(dist, stat)()

            except NotImplementedError:
                pass

        # Check that `sample` doesn't read distribution parameters more than twice,
        # and that it produces non-None gradients (if the distribution is fully
        # reparameterized).
        with tf.GradientTape() as tape:
            # TDs do bijector assertions twice (once by distribution.sample, and once
            # by bijector.forward).
            max_permissible = 2 + extra_tensor_conversions_allowed(dist)
            with tfp_hps.assert_no_excessive_var_usage(
                    'method `sample` of `{}`'.format(dist),
                    max_permissible=max_permissible):
                sample = dist.sample(seed=seed)
        if dist.reparameterization_type == tfd.FULLY_REPARAMETERIZED:
            grads = tape.gradient(sample, dist.variables)
            for grad, var in zip(grads, dist.variables):
                var_name = var.name.rstrip('_0123456789:')
                if var_name in NO_SAMPLE_PARAM_GRADS.get(dist_name, ()):
                    continue
                if grad is None:
                    raise AssertionError(
                        'Missing sample -> {} grad for distribution {}'.format(
                            var_name, dist_name))

        # Turn off validations, since TODO(b/129271256) log_prob can choke on dist's
        # own samples.  Also, to relax conversion counts for KL (might do >2 w/
        # validate_args).
        dist = dist.copy(validate_args=False)
        dist2 = dist2.copy(validate_args=False)

        # Test that KL divergence reads distribution parameters at most once, and
        # that is produces non-None gradients.
        try:
            for d1, d2 in (dist, dist2), (dist2, dist):
                with tf.GradientTape() as tape:
                    with tfp_hps.assert_no_excessive_var_usage(
                            '`kl_divergence` of (`{}` (vars {}), `{}` (vars {}))'
                            .format(d1, d1.variables, d2, d2.variables),
                            max_permissible=1
                    ):  # No validation => 1 convert per var.
                        kl = d1.kl_divergence(d2)
                wrt_vars = list(d1.variables) + list(d2.variables)
                grads = tape.gradient(kl, wrt_vars)
                for grad, var in zip(grads, wrt_vars):
                    if grad is None and dist_name not in NO_KL_PARAM_GRADS:
                        raise AssertionError(
                            'Missing KL({} || {}) -> {} grad:\n'  # pylint: disable=duplicate-string-formatting-argument
                            '{} vars: {}\n{} vars: {}'.format(
                                d1, d2, var, d1, d1.variables, d2,
                                d2.variables))
        except NotImplementedError:
            pass

        # Test that log_prob produces non-None gradients, except for distributions
        # on the NO_LOG_PROB_PARAM_GRADS blacklist.
        if dist_name not in NO_LOG_PROB_PARAM_GRADS:
            with tf.GradientTape() as tape:
                lp = dist.log_prob(tf.stop_gradient(sample))
            grads = tape.gradient(lp, dist.variables)
            for grad, var in zip(grads, dist.variables):
                if grad is None:
                    raise AssertionError(
                        'Missing log_prob -> {} grad for distribution {}'.
                        format(var, dist_name))

        # Test that all forms of probability evaluation avoid reading distribution
        # parameters more than once.
        for evaluative in sorted(
                data.draw(
                    hps.sets(hps.one_of(
                        map(hps.just, [
                            'log_prob', 'prob', 'log_cdf', 'cdf',
                            'log_survival_function', 'survival_function'
                        ])),
                             min_size=3,
                             max_size=3))):
            hp.note('Testing excessive var usage in {}.{}'.format(
                dist_name, evaluative))
            try:
                # No validation => 1 convert. But for TD we allow 2:
                # dist.log_prob(bijector.inverse(samp)) + bijector.ildj(samp)
                max_permissible = 2 + extra_tensor_conversions_allowed(dist)
                with tfp_hps.assert_no_excessive_var_usage(
                        'evaluative `{}` of `{}`'.format(evaluative, dist),
                        max_permissible=max_permissible):
                    getattr(dist, evaluative)(sample)
            except NotImplementedError:
                pass
            if v != w:
                adjacencies[v].add(w)
                adjacencies[w].add(v)
    return adjacencies


def test_degeneracy_ordering_empty() -> None:
    g = UndirectedGraph(adjacencies=[])
    assert list(degeneracy_ordering(g)) == []
    assert list(degeneracy_ordering(g, drop=1)) == []


@given(
    builds(
        symmetric_adjacencies,
        integers(min_value=1,
                 max_value=99).flatmap(lambda order: lists(sets(
                     integers(min_value=0, max_value=order - 1)),
                                                           min_size=order,
                                                           max_size=order))))
def test_degeneracy_ordering_nonempty(adjacencies: List[Set[Vertex]]) -> None:
    g = UndirectedGraph(adjacencies=adjacencies)
    connected_vertices = g.connected_vertices()

    ordering = list(degeneracy_ordering(g))
    assert set(ordering) == connected_vertices
    assert all(g.degree(ordering[0]) <= g.degree(v) for v in ordering[1:])

    ordering_min1 = list(degeneracy_ordering(g, drop=1))
    assert ordering_min1 == ordering[:-1]
Example #53
0
            CONSERVATIVE_REGEX.map(lambda s: s + u"+"),
            CONSERVATIVE_REGEX.map(lambda s: s + u"?"),
            CONSERVATIVE_REGEX.map(lambda s: s + u"*"),
            st.lists(CONSERVATIVE_REGEX, min_size=1,
                     max_size=3).map(u"|".join),
            st.lists(CONSERVATIVE_REGEX, min_size=1, max_size=3).map(u"".join),
        ))
    assume(COMBINED_MATCHER.search(result) is None)
    control = sum(result.count(c) for c in "?+*")
    assume(control <= 3)
    return result


CONSERVATIVE_REGEX = conservative_regex()
FLAGS = st.sets(st.sampled_from(
    [getattr(re, "A", 0), re.I, re.M,
     re.S])).map(lambda flag_set: reduce(int.__or__, flag_set, 0))


@given(st.data())
def test_conservative_regex_are_correct_by_construction(data):
    pattern = re.compile(data.draw(CONSERVATIVE_REGEX), flags=data.draw(FLAGS))
    result = data.draw(base_regex_strategy(pattern))
    assert pattern.search(result) is not None


@given(st.data())
def test_fuzz_stuff(data):
    pattern = data.draw(
        st.text(min_size=1, max_size=5)
        | st.binary(min_size=1, max_size=5)
Example #54
0
    ])

    result = runner.invoke(['sync'])
    assert not result.exception
    assert set(result.output.splitlines()) == set([
        'Syncing bambaz',
        'Syncing foobar',
    ])


@given(collections=st.sets(
    st.text(
        st.characters(
            blacklist_characters=set(
                u'./\x00'  # Invalid chars on POSIX filesystems
            ),
            # Surrogates can't be encoded to utf-8 in Python
            blacklist_categories=set(['Cs'])),
        min_size=1,
        max_size=50),
    min_size=1))
@example(collections=[u'persönlich'])
@example(collections=set('aA'))
def test_create_collections(subtest, collections):
    @subtest
    def test_inner(tmpdir, runner):
        runner.write_with_general(
            dedent('''
        [pair foobar]
        a = "foo"
        b = "bar"
Example #55
0
def metric(draw):
    tags = draw(strategies.sets(tag))
    return draw(metric_without_value).with_value(
        value=draw(value), ).with_tags(*tags)
Example #56
0
def test_sets_of_small_average_size():
    assert len(sets(integers(), average_size=1.0).example()) <= 10
def test_can_draw_empty_set_from_unsatisfiable_strategy():
    assert sets(integers().filter(lambda s: False)).example() == set()
Example #58
0
from coverage.backward import byte_to_int
from coverage.numbits import (
    nums_to_numbits,
    numbits_to_nums,
    numbits_union,
    numbits_intersection,
    numbits_any_intersection,
    num_in_numbits,
    register_sqlite_functions,
)

from tests.coveragetest import CoverageTest

# Hypothesis-generated line number data
line_numbers = integers(min_value=1, max_value=9999)
line_number_sets = sets(line_numbers)

# When coverage-testing ourselves, hypothesis complains about a test being
# flaky because the first run exceeds the deadline (and fails), and the second
# run succeeds.  Disable the deadline if we are coverage-testing.
default_settings = settings()
if env.METACOV:
    default_settings = settings(default_settings, deadline=None)


def good_numbits(numbits):
    """Assert that numbits is good."""
    # It shouldn't end with a zero byte, that should have been trimmed off.
    assert (not numbits) or (byte_to_int(numbits[-1]) != 0)

def test_minimize_3_set_of_tuples():
    assert minimal(
        sets(tuples(integers())),
        lambda x: len(x) >= 2) == set(((0,), (1,)))
Example #60
0
@given(lists(integers(), min_size=2))
def test_inits_heads(elements: List[int]) -> None:
    """inits(elements)[:1] == [[], [elements[0]]."""
    assert sut.inits(elements)[0] == []
    assert sut.inits(elements)[1] == [elements[0]]


@given(lists(integers()))
def test_inits_monotonic_length(elements: List[int]) -> None:
    """[len(x) for xs in inits(elements)] == range(len(elements) + 1)."""
    lengths = [len(xs) for xs in sut.inits(elements)]

    assert lengths == list(range(len(elements) + 1))


@given(sets(integers()), sets(integers()))
def test_venn_subsets(lefts: Set[int], rights: Set[int]) -> None:
    """All combinations of venn with subsets."""
    r_lefts: List[int]
    r_middles: List[int]
    r_rights: List[int]

    r_lefts, r_middles, r_rights = sut.venn(list(lefts), list(lefts | rights))

    assert (set(r_lefts), set(r_middles), set(r_rights)) == (set(), lefts,
                                                             rights - lefts)

    r_lefts, r_middles, r_rights = sut.venn(list(lefts | rights), list(rights))

    assert (set(r_lefts), set(r_middles), set(r_rights)) == (lefts - rights,
                                                             rights, set())