def test_dictionary(dict_class): assert minimal(dictionary(int, text_type, dict_class)) == dict_class() x = minimal(dictionary(int, text_type, dict_class), lambda t: len(t) >= 3) assert isinstance(x, dict_class) assert set(x.values()) == {''} for k in x: if k < 0: assert k + 1 in x if k > 0: assert k - 1 in x
def test_variadic_dictionaries(): x = strategy(s.dictionary(bool, bool)).example() assert all( isinstance(k, bool) and isinstance(v, bool) for k, v in x.items())
TestGiantIntegerRange = strategy_test_suite( integers_in_range(-(2**129), 2**129)) TestFloatRange = strategy_test_suite(floats_in_range(0.5, 10)) TestSampled10 = strategy_test_suite(sampled_from(elements=list(range(10)))) TestSampled1 = strategy_test_suite(sampled_from(elements=(1, ))) TestSampled2 = strategy_test_suite(sampled_from(elements=(1, 2))) TestIntegersFrom = strategy_test_suite(integers_from(13)) TestOneOf = strategy_test_suite(one_of((int, int, bool))) TestOneOfSameType = strategy_test_suite( one_of((integers_in_range(1, 10), integers_in_range(8, 15)))) TestRandom = strategy_test_suite(Random) TestInts = strategy_test_suite(int) TestBoolLists = strategy_test_suite([bool]) TestDictionaries = strategy_test_suite(dictionary((int, int), bool)) TestOrderedDictionaries = strategy_test_suite(dictionary( int, int, OrderedDict)) TestString = strategy_test_suite(text_type) BinaryString = strategy_test_suite(binary_type) TestIntBool = strategy_test_suite((int, bool)) TestFloats = strategy_test_suite(float) TestComplex = strategy_test_suite(complex) TestJust = strategy_test_suite(just('hi')) TestTemplates = strategy_test_suite(TemplatesFor({int})) TestEmptyString = strategy_test_suite(strings(alphabet='')) TestSingleString = strategy_test_suite(strings(alphabet='a')) TestManyString = strategy_test_suite(strings(alphabet='abcdef☃')) Stuff = namedtuple('Stuff', ('a', 'b'))
) TestFloatRange = strategy_test_suite(floats_in_range(0.5, 10)) TestSampled10 = strategy_test_suite(sampled_from(elements=list(range(10)))) TestSampled1 = strategy_test_suite(sampled_from(elements=(1,))) TestSampled2 = strategy_test_suite(sampled_from(elements=(1, 2))) TestIntegersFrom = strategy_test_suite(integers_from(13)) TestOneOf = strategy_test_suite(one_of((int, int, bool))) TestOneOfSameType = strategy_test_suite( one_of((integers_in_range(1, 10), integers_in_range(8, 15))) ) TestRandom = strategy_test_suite(Random) TestInts = strategy_test_suite(int) TestBoolLists = strategy_test_suite([bool]) TestDictionaries = strategy_test_suite(dictionary((int, int), bool)) TestOrderedDictionaries = strategy_test_suite( dictionary(int, int, OrderedDict) ) TestString = strategy_test_suite(text_type) BinaryString = strategy_test_suite(binary_type) TestIntBool = strategy_test_suite((int, bool)) TestFloats = strategy_test_suite(float) TestComplex = strategy_test_suite(complex) TestJust = strategy_test_suite(just('hi')) TestTemplates = strategy_test_suite(TemplatesFor({int})) TestEmptyString = strategy_test_suite(strings(alphabet='')) TestSingleString = strategy_test_suite(strings(alphabet='a')) TestManyString = strategy_test_suite(strings(alphabet='abcdef☃'))
from hypothesis.specifiers import dictionary from bidict import bidict, frozenbidict def inv(d): return {v: k for (k, v) in d.items()} def prune_dup_vals(d): pruned = inv(inv(d)) assume(len(pruned) >= 0.5 * len(d)) return pruned immutable_types_ = (None, bool, int, float, str, bytes, tuple(), frozenset()) immutable_types = reduce(or_, map(strategy, immutable_types_)) d = strategy(dictionary(immutable_types, immutable_types)).map(prune_dup_vals) @given(d) def test_len(d): assert len(d) == len(inv(d)) == len(bidict(d)) def isnan_(x): return isnan(x) if isinstance(x, float) else False def both_nan(a, b): return isnan_(a) and isnan_(b) @given(d) def test_bidirectional_mappings(d): b = bidict(d) for k, v in b.items():
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))
def test_minimize_multi_key_dicts(): assert find(dictionary(bool, bool), bool) == {False: False}
def test_find_dictionary(): assert len(find( dictionary(int, int), lambda xs: any(kv[0] > kv[1] for kv in xs.items()))) == 1