def test_is_unique(): from syn.base_utils import is_unique assert is_unique([1, 2, 3, 4]) assert not is_unique([1, 2, 3, 3, 4]) assert is_unique([[], [1]]) assert not is_unique([[], [1], []])
def test_sequence(): l = [1, 2.3, 'abc'] t = Type.dispatch(l) assert isinstance(t, Sequence) assert type(t) is List if PY2: assert set(hashable(l)) == set(t.hashable()) == \ {'__builtin__.list', 1, 2.3, 'abc'} else: assert set(hashable(l)) == set(t.hashable()) == \ {'builtins.list', 1, 2.3, 'abc'} assert not is_hashable(l) assert is_hashable(hashable(l)) l1 = [1, 2, 3] l2 = [1, 2, 3, 4] l3 = [1, 2, 4] assert find_ne(l1, l2) == DifferentLength(l1, l2) assert find_ne(l2, l1) == DifferentLength(l2, l1) assert find_ne(l1, l3) == DiffersAtIndex(l1, l3, 2) e1 = eval(estr(l1)) assert_equivalent(e1, l1) tup = tuple(l) examine_sequence(List, l) examine_sequence(Tuple, tup) examine_sequence(Tuple, ([ -1839677305294322342, b'', b'\x05l\xbf', b'0\xcfXp\xaa', -8468204163727415930 ], True)) for cls in subclasses(Sequence): for k in xrange(SAMPLES): val = cls.generate() with on_error(elog, examine_sequence, (cls, val)): hangwatch(1, examine_sequence, cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100): assert type(item) is cls.type assert item != last buf.append(item) last = item assert is_unique(buf) assert list(visit(l, enumerate=True)) == [(0, 1), (1, 2.3), (2, 'abc')] assert list(visit([])) == [] l = [1, 2, (3, 4)] assert primitive_form(l) == [1, 2, [3, 4]] assert collect(l) == primitive_form(l)
def test_sequence(): l = [1, 2.3, 'abc'] t = Type.dispatch(l) assert isinstance(t, Sequence) assert type(t) is List if PY2: assert set(hashable(l)) == set(t.hashable()) == \ {'__builtin__.list', 1, 2.3, 'abc'} else: assert set(hashable(l)) == set(t.hashable()) == \ {'builtins.list', 1, 2.3, 'abc'} assert not is_hashable(l) assert is_hashable(hashable(l)) l1 = [1, 2, 3] l2 = [1, 2, 3, 4] l3 = [1, 2, 4] assert find_ne(l1, l2) == DifferentLength(l1, l2) assert find_ne(l2, l1) == DifferentLength(l2, l1) assert find_ne(l1, l3) == DiffersAtIndex(l1, l3, 2) e1 = eval(estr(l1)) assert_equivalent(e1, l1) tup = tuple(l) examine_sequence(List, l) examine_sequence(Tuple, tup) examine_sequence(Tuple, ([-1839677305294322342, b'', b'\x05l\xbf', b'0\xcfXp\xaa', -8468204163727415930], True)) for cls in subclasses(Sequence): for k in xrange(SAMPLES): val = cls.generate() with on_error(elog, examine_sequence, (cls, val)): hangwatch(1, examine_sequence, cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100): assert type(item) is cls.type assert item != last buf.append(item) last = item assert is_unique(buf) assert list(visit(l, enumerate=True)) == [(0, 1), (1, 2.3), (2, 'abc')] assert list(visit([])) == [] l = [1, 2, (3, 4)] assert primitive_form(l) == [1, 2, [3, 4]] assert collect(l) == primitive_form(l)
def test_numeric(): x = 1 t = Type.dispatch(x) assert isinstance(t, Numeric) assert type(t) is Int assert hashable(x) == x examine_numeric(Int, x) for cls in subclasses(Numeric): if cls.type is None: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_numeric, (cls, val)): examine_numeric(cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10): assert type(item) is cls.type assert item != last buf.append(item) if last is None: # These need to be here under enumerate_ b/c of float equality issues eitem = eval(estr(item)) assert eitem == item assert type(eitem) is cls.type last = item if cls.type not in { bool, }: assert is_unique(buf) else: assert not is_unique(buf)
def test_syn_types_functionality(): s = SynTypesTest(a=1, b=1.2, c='abc') s2 = SynTypesTest(a=1, b=1.2, c='abcd') assert not is_hashable(s) assert is_hashable(hashable(s)) assert find_ne(s, s) is None assert find_ne(s, s2) == DiffersAtAttribute(s, s2, 'c') e1 = eval(estr(s)) assert_equivalent(e1, s) assert list(visit(s)) == [('a', 1), ('b', 1.2), ('c', 'abc')] assert rstr(s) == "SynTypesTest(a = 1, b = 1.2, c = 'abc')" assert attrs(s) == ['a', 'b', 'c'] assert pairs(s) == [('a', 1), ('b', 1.2), ('c', 'abc')] sval = deserialize(serialize(s)) assert_equivalent(sval, s) assert deep_feq(sval, s) assert primitive_form(s) == dict(a=1, b=1.2, c='abc') assert primitive_form(SynTypesTest) is SynTypesTest assert SynTypesTest is deserialize(serialize(SynTypesTest)) val = generate(SynTypesTest) assert type(val) is SynTypesTest buf = [] last = None for item in enum(SynTypesTest, max_enum=SAMPLES * 10, step=100): assert type(item) is SynTypesTest assert item != last buf.append(item) last = item assert enumeration_value(SynTypesTest, 0) == \ first(enum(SynTypesTest, max_enum=1)) assert is_unique(buf) val = generate(SynTypesTest2) assert type(val) is not SynTypesTest2 assert type(val) is SynTypesTest item = first(enum(SynTypesTest3, max_enum=1)) assert type(item) is SynTypesTest3 assert not hasattr(item, 'd')
def test_string(): s = u'abc' t = Type.dispatch(s) assert isinstance(t, String) if PY2: assert type(t) is Unicode examine_string(Unicode, s) else: assert type(t) is String examine_string(String, s) assert hashable(s) == t.hashable() == s assert is_hashable(s) assert is_hashable(hashable(s)) assert find_ne('abc', 'abcd') == DifferentLength('abc', 'abcd') assert find_ne('abcd', 'abc') == DifferentLength('abcd', 'abc') assert find_ne('abc', 'abd') == DiffersAtIndex('abc', 'abd', 2) for cls in subclasses(String, [String]): if cls.type is None or cls is Basestring: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_string, (cls, val)): examine_string(cls, val) x = 0 buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100): assert type(item) is cls.type assert item != last buf.append(item) last = item x += 100 assert is_unique(buf) # estr potential edge cases cases = [ "abc'de\r7fghi", "\x00", "\\", "\'", '\"', '\a', '\b', '\t', '\v', u'\u2013', '\\u2' ] for case in cases: assert eval(estr(case)) == case
def test_numeric(): x = 1 t = Type.dispatch(x) assert isinstance(t, Numeric) assert type(t) is Int assert hashable(x) == x examine_numeric(Int, x) for cls in subclasses(Numeric): if cls.type is None: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_numeric, (cls, val)): examine_numeric(cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10): assert type(item) is cls.type assert item != last buf.append(item) if last is None: # These need to be here under enumerate_ b/c of float equality issues eitem = eval(estr(item)) assert eitem == item assert type(eitem) is cls.type last = item if cls.type not in {bool,}: assert is_unique(buf) else: assert not is_unique(buf)
def test_string(): s = u'abc' t = Type.dispatch(s) assert isinstance(t, String) if PY2: assert type(t) is Unicode examine_string(Unicode, s) else: assert type(t) is String examine_string(String, s) assert hashable(s) == t.hashable() == s assert is_hashable(s) assert is_hashable(hashable(s)) assert find_ne('abc', 'abcd') == DifferentLength('abc', 'abcd') assert find_ne('abcd', 'abc') == DifferentLength('abcd', 'abc') assert find_ne('abc', 'abd') == DiffersAtIndex('abc', 'abd', 2) for cls in subclasses(String, [String]): if cls.type is None or cls is Basestring: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_string, (cls, val)): examine_string(cls, val) x = 0 buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100): assert type(item) is cls.type assert item != last buf.append(item) last = item x += 100 assert is_unique(buf) # estr potential edge cases cases = ["abc'de\r7fghi", "\x00", "\\", "\'", '\"', '\a', '\b', '\t', '\v', u'\u2013', '\\u2'] for case in cases: assert eval(estr(case)) == case
def test_custom_type(): b = Baz(1, 2.3) b2 = Baz(1, 2.4) b3 = Baz(2, 2.3) assert b != b2 assert_equivalent(Baz(1, 2.3), Baz(1, 2.3)) if PY3: assert not is_hashable(b) else: assert is_hashable(b) assert is_hashable(hashable(b)) assert find_ne(b, b) is None assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b') assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a') e1 = eval(estr(b)) assert_equivalent(e1, b) assert list(visit(b)) == [('a', 1), ('b', 2.3)] assert rstr(b) == 'Baz(1,2.3)' assert attrs(b) == ['a', 'b'] sval = deserialize(serialize(b)) assert_equivalent(sval, b) assert deep_feq(sval, b) assert Baz is deserialize(serialize(Baz)) assert primitive_form(Baz) is Baz assert primitive_form(b) == dict(a=1, b=2.3) val = generate(Baz) assert type(val) is Baz assert isinstance(val.a, int) assert isinstance(val.b, float) buf = [] last = None for item in enum(Baz, max_enum=SAMPLES * 10, step=100): assert type(item) is Baz assert item != last buf.append(item) last = item assert is_unique(buf)
def test_custom_object(): f = Foo(1, 1.2) f2 = Foo(1, 1.3) f3 = Foo(2, 1.2) assert f != f2 assert_equivalent(Foo(1, 2.3), Foo(1, 2.3)) assert not is_hashable(f) assert is_hashable(hashable(f)) assert find_ne(f, f) is None assert find_ne(f, f2) == DiffersAtAttribute(f, f2, 'b') assert find_ne(f, f3) == DiffersAtAttribute(f, f3, 'a') e1 = eval(estr(f)) assert_equivalent(e1, f) assert list(visit(f)) == [('a', 1), ('b', 1.2)] assert rstr(f) == 'Foo(1,1.2)' assert attrs(f) == ['a', 'b'] assert pairs(f) == [('a', 1), ('b', 1.2)] sval = deserialize(serialize(f)) assert_equivalent(sval, f) assert deep_feq(sval, f) assert Foo is deserialize(serialize(Foo)) assert primitive_form(Foo) is Foo assert primitive_form(f) == dict(a=1, b=1.2) assert collect(f) == primitive_form(f) val = generate(Foo) assert type(val) is Foo buf = [] last = None for item in enum(Foo, max_enum=SAMPLES * 10, step=100): assert type(item) is Foo assert item != last buf.append(item) last = item assert enumeration_value(Foo, 0) == first(enum(Foo, max_enum=1)) assert is_unique(buf)
def test_mapping(): d = dict(a = 1, b = 2.3) t = Type.dispatch(d) assert isinstance(t, Mapping) assert type(t) is Dict if PY2: assert set(hashable(d)) == set(t.hashable()) == \ {'__builtin__.dict', ('a', 1), ('b', 2.3)} else: assert set(hashable(d)) == set(t.hashable()) == \ {'builtins.dict', ('a', 1), ('b', 2.3)} d1 = dict(a=1, b=2) d2 = dict(a=1, b=2, c=3) d3 = dict(a=1, b=3) assert find_ne(d1, d2) == KeyDifferences(d1, d2) assert find_ne(d2, d1) == KeyDifferences(d2, d1) assert find_ne(d1, d3) == DiffersAtKey(d1, d3, 'b') e1 = eval(estr(d1)) assert_equivalent(e1, d1) assert not is_hashable(d) assert is_hashable(hashable(d)) examine_mapping(Dict, d) for cls in subclasses(Mapping): for k in xrange(SAMPLES): val = cls.generate() with on_error(elog, examine_mapping, (cls, val)): hangwatch(1, examine_mapping, cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100): assert type(item) is cls.type assert item != last buf.append(item) last = item assert is_unique(buf) d = dict(a=1, b=[1, 2, (3, 4)]) assert primitive_form(d) == dict(a=1, b=[1, 2, [3, 4]]) assert collect(d) == primitive_form(d)
def test_set(): s = frozenset([1, 2.3, 'abc']) t = Type.dispatch(s) assert isinstance(t, Set) assert type(t) is FrozenSet assert hashable(s) == t.hashable() == s assert is_hashable(s) assert is_hashable(hashable(s)) s1 = {1, 2, 3} s2 = {2, 3, 4} assert find_ne(s1, s2) == SetDifferences(s1, s2) e1 = eval(estr(s1)) assert_equivalent(e1, s1) examine_set(Set, set(s)) examine_set(FrozenSet, s) for cls in subclasses(Set, [Set]): for k in xrange(SAMPLES): val = cls.generate() with on_error(elog, examine_set, (cls, val)): hangwatch(1, examine_set, cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100): assert type(item) is cls.type assert item != last buf.append(item) last = item assert is_unique(buf) s = {1, 2, (3, 4)} assert primitive_form(s) == [1, 2, [3, 4]] assert collect(s) == primitive_form(s)