Ejemplo n.º 1
0
def test_union():
    u = Union({1, 2, 3}, {3, 4, 5})
    assert u._children == [SetWrapper({1, 2, 3}), SetWrapper({3, 4, 5})]
    assert set(u.enumerate()) == {1, 2, 3, 4, 5}
    assert set(u.enumerate(lazy=True)) == {1, 2, 3, 4, 5}

    assert u.size() == 5
    assert u.size_limits() == (3, 6)
    assert feq(u.expected_size(), 4.5)

    assert u.hasmember(1)
    assert u.hasmember(5)
    assert not u.hasmember(6)

    for k in range(SAMPLES):
        item = u.sample()
        with on_error(elog, u.hasmember, (u, item)):
            assert u.hasmember(item)

    item = u.sample(lazy=True)
    assert u.hasmember(item)

    assert u.to_set() == {1, 2, 3, 4, 5}

    u2 = Union(Range(1, 3), Range(5, 7), Range(6, 9), NULL, {10}, {13})
    assert u2.to_set() == {1, 2, 3, 5, 6, 7, 8, 9, 10, 13}
    assert sorted(u2.enumerate()) == [1, 2, 3, 5, 6, 7, 8, 9, 10, 13]
    assert len(list(u2.enumerate(max_enumerate = 2))) == 2

    for k in range(SAMPLES):
        item = u2.sample()
        with on_error(elog, u2.hasmember, (u2, item)):
            assert u2.hasmember(item)

    assert u2.display() == '([1, 3] | [5, 7] | [6, 9] | {} | {10} | {13})'

    # Sanity Check
    class Foo(object): pass
    class F1(Foo): pass
    u = Union(range(10), Range(10, 20), ClassWrapper(Foo))
    assert u.size() == 23
    assert feq(u.expected_size(), 17)
    us = u.to_set()
    assert len(us) == 23
    assert us == set(range(10)) | set(range(10, 21)) | set([Foo, F1])

    # Edge Cases
    assert list(Union().enumerate()) == []
    assert Union().to_set() == set()
    assert Union(Range(1, 2)).to_set() == {1, 2}
    assert Union({2, 4}).to_set() == {2, 4}
Ejemplo n.º 2
0
def test_range():
    r1 = Range(1, 5)
    r2 = Range(2, 5)

    assert r1.display() == '[1, 5]'
    assert r1.size() == 5

    assert r2.issubset(r1)
    assert r1.issuperset(r2)
    assert not r1.issubset(r2)
    assert not r2.issuperset(r1)

    assert r1.hasmember(3)
    assert r2.hasmember(3)
    assert r1.hasmember(4)
    assert r1.hasmember(5)
    assert not r1.hasmember(0)
    assert not r2.hasmember(1)

    for k in range(SAMPLES):
        samp = r1.sample()
        with on_error(elog, r1.hasmember, (r1, samp)):
            assert r1.hasmember(samp)

    assert sorted(list(r1.enumerate())) == [1, 2, 3, 4, 5]
    assert r1.to_set() == {1, 2, 3, 4, 5}
    assert len(list(r1.enumerate(max_enumerate=3))) == 3

    r1.validate()
    r2.validate()
    assert_raises(ValueError, Range(2, 1).validate)
Ejemplo n.º 3
0
def test_sequence():
    int_seq = Sequence(int)
    assert int_seq == Sequence(int)
    assert int_seq != Sequence(float)

    assert int_seq.query([1, 2, 3])
    assert not int_seq.query([1.2, 2, 3])
    assert int_seq.query((1, 2, 3))
    assert not int_seq.query(1)

    int_list = List(int)

    assert int_list.query([1, 2, 3])
    assert not int_list.query([1.2, 2, 3])
    assert not int_list.query((1, 2, 3))

    for k in xrange(SAMPLES):
        l = int_list.generate()
        with on_error(elog, int_list.check, (l, )):
            int_list.check(l)

    bad_list = (1.2, '3', 4)
    good_list = [1, 2, 3]
    assert int_list.coerce(bad_list) == [1, 3, 4]
    assert int_list.coerce(good_list) is good_list

    assert int_list.display() == 'list(int)'
    assert int_list.rst() == '*list* (*int*)'
Ejemplo n.º 4
0
def test_iteration_length():
    from syn.base_utils import iteration_length

    assert iteration_length(5) == 5
    assert iteration_length(5, 0) == 5
    assert iteration_length(5, 1) == 4
    assert iteration_length(5, 0, 2) == 3

    for k in range(SAMPLES):
        N = rand_int(0, 20)
        start = rand_int(0, 20)
        step = rand_int(1, 5)

        with on_error(elog, iteration_length, (N, start, step)):
            assert iteration_length(N, start, step) == \
                len(list(range(start, N, step)))

    assert iteration_length(0) == 0
    assert iteration_length(10, 10) == 0
    assert iteration_length(10, 20) == 0
    assert_raises(ValueError, iteration_length, -1)
    assert_raises(ValueError, iteration_length, 10, 20, -1)

    assert iteration_length(10, -1, 1) == 1
    assert iteration_length(10, -1, 10) == 1

    assert iteration_length(10, -1, -1) == 10
    assert iteration_length(10, -1, -3) == 4
    assert iteration_length(10, 1, 3) == 3
    assert iteration_length(10, 5, -1) == 6

    assert_raises(ValueError, iteration_length, 10, -11)
Ejemplo n.º 5
0
def test_sequence():
    int_seq = Sequence(int)
    assert int_seq == Sequence(int)
    assert int_seq != Sequence(float)

    assert int_seq.query([1, 2, 3])
    assert not int_seq.query([1.2, 2, 3])
    assert int_seq.query((1, 2, 3))
    assert not int_seq.query(1)

    int_list = List(int)

    assert int_list.query([1, 2, 3])
    assert not int_list.query([1.2, 2, 3])
    assert not int_list.query((1, 2, 3))

    for k in xrange(SAMPLES):
        l = int_list.generate()
        with on_error(elog, int_list.check, (l,)):
            int_list.check(l)

    bad_list = (1.2, '3', 4)
    good_list = [1, 2, 3]
    assert int_list.coerce(bad_list) == [1, 3, 4]
    assert int_list.coerce(good_list) is good_list

    assert int_list.display() == 'list(int)'
    assert int_list.rst() == '*list* (*int*)'
Ejemplo n.º 6
0
def test_classwrapper():
    class Foo(object): pass
    class F1(Foo): pass
    class F2(F1): pass

    cw = ClassWrapper(Foo)
    
    assert cw.display() == 'ClassWrapper(Foo)'
    assert cw.size() == 3

    for k in range(SAMPLES):
        item = cw.sample()
        with on_error(elog, cw.hasmember, (cw, item)):
            assert cw.hasmember(item)

    cwset = cw.to_set()
    assert len(cwset) >= 3
    assert Foo in cwset
    assert F1 in cwset
    assert F2 in cwset

    cwlist = list(cw.enumerate(max_enumerate = 2))
    assert len(cwlist) == 2
    assert all(issubclass(x, Foo) for x in cwlist)
    assert all(issubclass(x, Foo) for x in cw.to_set())
Ejemplo n.º 7
0
def test_range():
    r1 = Range(1, 5)
    r2 = Range(2, 5)
    
    assert r1.display() == '[1, 5]'
    assert r1.size() == 5

    assert r2.issubset(r1)
    assert r1.issuperset(r2)
    assert not r1.issubset(r2)
    assert not r2.issuperset(r1)

    assert r1.hasmember(3)
    assert r2.hasmember(3)
    assert r1.hasmember(4)
    assert r1.hasmember(5)
    assert not r1.hasmember(0)
    assert not r2.hasmember(1)

    for k in range(SAMPLES):
        samp = r1.sample()
        with on_error(elog, r1.hasmember, (r1, samp)):
            assert r1.hasmember(samp)

    assert sorted(list(r1.enumerate())) == [1, 2, 3, 4, 5]
    assert r1.to_set() == {1, 2, 3, 4, 5}
    assert len(list(r1.enumerate(max_enumerate = 3))) == 3

    r1.validate()
    r2.validate()
    assert_raises(ValueError, Range(2, 1).validate)
Ejemplo n.º 8
0
def test_classwrapper():
    class Foo(object):
        pass

    class F1(Foo):
        pass

    class F2(F1):
        pass

    cw = ClassWrapper(Foo)

    assert cw.display() == 'ClassWrapper(Foo)'
    assert cw.size() == 3

    for k in range(SAMPLES):
        item = cw.sample()
        with on_error(elog, cw.hasmember, (cw, item)):
            assert cw.hasmember(item)

    cwset = cw.to_set()
    assert len(cwset) >= 3
    assert Foo in cwset
    assert F1 in cwset
    assert F2 in cwset

    cwlist = list(cw.enumerate(max_enumerate=2))
    assert len(cwlist) == 2
    assert all(issubclass(x, Foo) for x in cwlist)
    assert all(issubclass(x, Foo) for x in cw.to_set())
Ejemplo n.º 9
0
def test_iteration_length():
    from syn.base_utils import iteration_length

    assert iteration_length(5) == 5
    assert iteration_length(5, 0) == 5
    assert iteration_length(5, 1) == 4
    assert iteration_length(5, 0, 2) == 3

    for k in range(SAMPLES):
        N = rand_int(0, 20)
        start = rand_int(0, 20)
        step = rand_int(1, 5)
        
        with on_error(elog, iteration_length, (N, start, step)):
            assert iteration_length(N, start, step) == \
                len(list(range(start, N, step)))

    assert iteration_length(0) == 0
    assert iteration_length(10, 10) == 0
    assert iteration_length(10, 20) == 0
    assert_raises(ValueError, iteration_length, -1)
    assert_raises(ValueError, iteration_length, 10, 20, -1)

    assert iteration_length(10, -1, 1) == 1
    assert iteration_length(10, -1, 10) == 1

    assert iteration_length(10, -1, -1) == 10
    assert iteration_length(10, -1, -3) == 4
    assert iteration_length(10, 1, 3) == 3
    assert iteration_length(10, 5, -1) == 6

    assert_raises(ValueError, iteration_length, 10, -11)
Ejemplo n.º 10
0
def test_rand_hashable():
    from syn.base_utils import rand_hashable

    for k in xrange(SAMPLES):
        x = rand_hashable()

        with on_error(elog, is_hashable, (x,)):
            assert is_hashable(x)
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
def test_rand_list():
    from syn.base_utils import rand_list, is_flat

    lists = [rand_list() for k in xrange(SAMPLESIII)]
    assert all(isinstance(l, list) for l in lists)

    for k in xrange(SAMPLESII):
        l = rand_list(max_depth=0)

        with on_error(elog, is_flat, (l,)):
            assert is_flat(l)
Ejemplo n.º 14
0
def test_setwrapper():
    s1 = SetWrapper([1, 2, 3])
    s2 = {3, 4, 5}
    s3 = SetWrapper(s2)
    s4 = SetWrapper(set([1]))

    assert s4.display() == '{1}'
    assert s1.size() == s3.size() == 3
    assert s1.size_limits() == (3, 3)

    assert s1.union(s3) == SetWrapper([1, 2, 3, 4, 5])
    assert s1.union(s2) == SetWrapper([1, 2, 3, 4, 5])
    assert s1.union(s2, NULL) == SetWrapper([1, 2, 3, 4, 5])
    assert_raises(TypeError, s1.union, [3, 4])

    assert s1.intersection(s3) == SetWrapper([3])
    assert s1.intersection(s2) == SetWrapper([3])
    assert_raises(TypeError, s1.intersection, [3, 4])

    assert s1.difference(s3) == SetWrapper([1, 2])
    assert s1.difference(s2) == SetWrapper([1, 2])
    assert_raises(TypeError, s1.difference, [3, 4])

    universe = {1, 2, 3, 4, 5}
    assert s1.complement(universe) == SetWrapper([4, 5])
    assert s1.complement(SetWrapper(universe)) == SetWrapper([4, 5])
    assert_raises(TypeError, s1.complement, [3, 4])

    assert s1.issubset(universe)
    assert not s1.issubset(s3)
    assert_raises(TypeError, s1.issubset, [3, 4])

    assert SetWrapper(universe).issuperset(s1)
    assert not s1.issuperset(s3)
    assert_raises(TypeError, s1.issuperset, [3, 4])

    assert s1.hasmember(3)
    assert not s1.hasmember(4)

    for k in range(SAMPLES):
        samp = s1.sample()
        with on_error(elog, s1.hasmember, (s1, samp)):
            assert s1.hasmember(samp)

    item = s1.get_one()
    assert s1.hasmember(item)

    assert sorted(list(s1.enumerate())) == [1, 2, 3]
    assert s1.to_set() == {1, 2, 3}
    assert len(list(s1.enumerate(max_enumerate=2))) == 2
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
def test_setwrapper():
    s1 = SetWrapper([1, 2, 3])
    s2 = {3, 4, 5}
    s3 = SetWrapper(s2)
    s4 = SetWrapper(set([1]))

    assert s4.display() == '{1}'
    assert s1.size() == s3.size() == 3
    assert s1.size_limits() == (3, 3)

    assert s1.union(s3) == SetWrapper([1,2,3,4,5])
    assert s1.union(s2) == SetWrapper([1,2,3,4,5])
    assert s1.union(s2, NULL) == SetWrapper([1,2,3,4,5])
    assert_raises(TypeError, s1.union, [3, 4])

    assert s1.intersection(s3) == SetWrapper([3])
    assert s1.intersection(s2) == SetWrapper([3])
    assert_raises(TypeError, s1.intersection, [3, 4])

    assert s1.difference(s3) == SetWrapper([1, 2])
    assert s1.difference(s2) == SetWrapper([1, 2])
    assert_raises(TypeError, s1.difference, [3, 4])

    universe = {1, 2, 3, 4, 5}
    assert s1.complement(universe) == SetWrapper([4, 5])
    assert s1.complement(SetWrapper(universe)) == SetWrapper([4,5])
    assert_raises(TypeError, s1.complement, [3, 4])

    assert s1.issubset(universe)
    assert not s1.issubset(s3)
    assert_raises(TypeError, s1.issubset, [3, 4])

    assert SetWrapper(universe).issuperset(s1)
    assert not s1.issuperset(s3)
    assert_raises(TypeError, s1.issuperset, [3, 4])

    assert s1.hasmember(3)
    assert not s1.hasmember(4)

    for k in range(SAMPLES):
        samp = s1.sample()
        with on_error(elog, s1.hasmember, (s1, samp)):
            assert s1.hasmember(samp)

    item = s1.get_one()
    assert s1.hasmember(item)

    assert sorted(list(s1.enumerate())) == [1, 2, 3]
    assert s1.to_set() == {1, 2, 3}    
    assert len(list(s1.enumerate(max_enumerate = 2))) == 2
Ejemplo n.º 17
0
def test_typewrapper():
    tw = TypeWrapper(int)

    assert tw.display() == 'TypeWrapper(int)'
    assert tw.size() == float('inf')
    
    for k in range(SAMPLES):
        item = tw.sample()
        with on_error(elog, tw.hasmember, (tw, item)):
            assert tw.hasmember(item)

    twlist = list(tw.enumerate(max_enumerate = 30))
    assert len(twlist) == 30
    assert all(isinstance(x, int) for x in twlist)
    assert all(isinstance(x, int) for x in tw.to_set())
Ejemplo n.º 18
0
def test_typewrapper():
    tw = TypeWrapper(int)

    assert tw.display() == 'TypeWrapper(int)'
    assert tw.size() == float('inf')

    for k in range(SAMPLES):
        item = tw.sample()
        with on_error(elog, tw.hasmember, (tw, item)):
            assert tw.hasmember(item)

    twlist = list(tw.enumerate(max_enumerate=30))
    assert len(twlist) == 30
    assert all(isinstance(x, int) for x in twlist)
    assert all(isinstance(x, int) for x in tw.to_set())
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
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)
Ejemplo n.º 21
0
def test_strrange():
    s = StrRange('a', u'c')
    assert s.lb == 97
    assert s.ub == 99

    assert s.display() == '[a, c]'
    assert s.hasmember('a')
    assert s.hasmember(u'a')
    assert not s.hasmember('d')
    assert_raises(TypeError, s.hasmember, 97)

    for k in range(SAMPLES):
        item = s.sample()
        with on_error(elog, s.hasmember, (s, item)):
            assert s.hasmember(item)

    assert sorted(s.enumerate()) == ['a', 'b', 'c']
    assert s.to_set() == {'a', 'b', 'c'}
Ejemplo n.º 22
0
def test_strrange():
    s = StrRange('a', u'c')
    assert s.lb == 97
    assert s.ub == 99

    assert s.display() == '[a, c]'
    assert s.hasmember('a')
    assert s.hasmember(u'a')
    assert not s.hasmember('d')
    assert_raises(TypeError, s.hasmember, 97)

    for k in range(SAMPLES):
        item = s.sample()
        with on_error(elog, s.hasmember, (s, item)):
            assert s.hasmember(item)

    assert sorted(s.enumerate()) == ['a', 'b', 'c']
    assert s.to_set() == {'a', 'b', 'c'}
Ejemplo n.º 23
0
def test_difference():
    d = Difference({1, 2, 3}, {2, 3, 4})
    assert d._children == [SetWrapper({1, 2, 3}), SetWrapper({2, 3, 4})]
    assert set(d.enumerate()) == {1}
    assert set(d.lazy_enumerate()) == {1}
    
    assert d.size() == 1
    assert d.size_limits() == (0, 3)
    assert Difference({1, 2}, {1, 2, 3}).size_limits() == (0, 2)

    assert d.hasmember(1)
    assert not d.hasmember(2)
    assert not d.hasmember(4)

    for k in range(SAMPLES):
        item = d.sample()
        with on_error(elog, d.hasmember, (d, item)):
            assert d.hasmember(item)
    
    item = d.lazy_sample()
    assert d.hasmember(item)

    assert d.to_set() == {1}

    d = Difference(Range(1, 5), Range(2, 3))
    assert d.to_set() == {1, 4, 5}
    assert d.display() == '([1, 5] - [2, 3])'

    d = Difference(Range(1, 5), Range(3, 7))
    assert d.to_set() == {1, 2}

    d = Difference(Range(1, 5), {4, 5, 6})
    assert d.to_set() == {1, 2, 3}

    d = Difference({1, 2}, {1, 2, 3})
    assert d.to_set() == set()
    assert_raises(ValueError, d.lazy_sample)

    # Test lazy_enumerate
    d = Difference(Range(0, 100000), Range(100, 200))
    assert list(d.lazy_enumerate(max_enumerate=10)) == range(10)
Ejemplo n.º 24
0
def test_product():
    p = Product(Range(1, 5), Range(6, 10), Range(11, 15))
    ps = p.to_set()
    assert (1, 6, 11) in ps
    assert (5, 6, 11) in ps
    assert (5, 10, 11) in ps
    assert (5, 10, 15) in ps

    assert p.display() == '([1, 5] x [6, 10] x [11, 15])'

    assert feq(p.expected_size(), 125)

    assert not p.hasmember((0, 6, 11))
    for k in range(SAMPLES):
        item = p.sample()
        with on_error(elog, p.hasmember, (p, item)):
            assert p.hasmember(item)

    p = Product({1}, {2}, {3})
    assert p.to_set() == set([(1, 2, 3)])
    assert p.sample() == (1, 2, 3)
Ejemplo n.º 25
0
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)
Ejemplo n.º 26
0
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)
Ejemplo n.º 27
0
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)
Ejemplo n.º 28
0
def test_set():
    from syn.sets.b import Range

    t = Set(Range(1, 5))
    assert t == Set(Range(1, 5))
    assert t != Set(Range(0, 5))
    assert Type.dispatch(t) is t

    assert t.query(1)
    assert not t.query(0)
    t.validate(1)
    assert_raises(TypeError, t.validate, 0)

    assert t.coerce(1) == 1
    assert_raises(TypeError, t.coerce, 0)

    s = set(xrange(1, 6))
    for k in xrange(SAMPLES):
        val = t.generate()
        with on_error(elog, s.__contains__, (val, )):
            assert val in s

    assert t.display() == t.rst() == '<Set>'
Ejemplo n.º 29
0
def test_set():
    from syn.sets.b import Range

    t = Set(Range(1, 5))
    assert t == Set(Range(1, 5))
    assert t != Set(Range(0, 5))
    assert Type.dispatch(t) is t

    assert t.query(1)
    assert not t.query(0)
    t.validate(1)
    assert_raises(TypeError, t.validate, 0)

    assert t.coerce(1) == 1
    assert_raises(TypeError, t.coerce, 0)

    s = set(xrange(1, 6))
    for k in xrange(SAMPLES):
        val = t.generate()
        with on_error(elog, s.__contains__, (val,)):
            assert val in s

    assert t.display() == t.rst() == '<Set>'
Ejemplo n.º 30
0
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)
Ejemplo n.º 31
0
def test_intersection():
    i = Intersection({1, 2, 3}, {2, 3, 4})
    assert i._children == [SetWrapper({1, 2, 3}), SetWrapper({2, 3, 4})]
    assert set(i.enumerate()) == {2, 3}
    assert set(i.lazy_enumerate()) == {2, 3}

    assert i.size() == 2
    assert i.size_limits() == (0, 3)

    assert not i.hasmember(1)
    assert i.hasmember(2)
    assert not i.hasmember(4)

    for k in range(SAMPLES):
        item = i.sample()
        with on_error(elog, i.hasmember, (i, item)):
            assert i.hasmember(item)
    
    item = i.lazy_sample()
    assert i.hasmember(item)
    item = i.get_one()
    assert i.hasmember(item)

    assert i.to_set() == {2, 3}

    assert Intersection({1, 2}, NULL).to_set() == set()

    i2 = Intersection(range(10), range(8), Range(2, 7), Range(3, 8))
    assert i2.to_set() == set(range(3,8))
    assert sorted(list(i2.enumerate())) == range(3, 8)
    assert sorted(list(i2.enumerate(lazy=True))) == range(3, 8)

    for k in range(SAMPLES):
        item = i2.sample()
        with on_error(elog, i2.hasmember, (i2, item)):
            assert i2.hasmember(item)

    assert Intersection(Range(1, 5), Range(2, 6)).to_set() == {2, 3, 4, 5}
    assert Intersection(Range(1, 5), {2, 3}).to_set() == {2, 3}
    assert Intersection(Range(1, 5), Union({2}, Range(4,5))).to_set() == {2, 4, 5}
    assert Intersection(NULL, Union({2}, Range(4,5))).to_set() == set()

    i3 = Intersection({1, 2, 3}, {1, 2, 3})
    assert len(list(i3.enumerate(max_enumerate = 2))) == 2

    i4 = Intersection({1, 2}, {3, 4})
    assert i4.to_set() == set()
    assert_raises(ValueError, i4.lazy_sample)

    # Advantages of lazy_enumerate()
    i5 = Intersection(Range(-10000000000, 10000000000), range(10))
    assert set(i5.lazy_enumerate()) == set(range(10))

    # Test max_enumerate
    i6 = Intersection(Range(0, 100), Range(10, 90))
    assert list(i6.lazy_enumerate(max_enumerate=10)) == range(10, 20)

    assert i6.display() == '([0, 100] & [10, 90])'

    # Sanity check
    i7 = Intersection(range(10), Range(0, 50))
    assert i7.to_set() == set(range(10))

    # Edge Cases
    assert Intersection().to_set() == set()
    assert list(Intersection().lazy_enumerate()) == []
    assert Intersection(Range(1, 5)).to_set() == set(range(1,6))
Ejemplo n.º 32
0
 def test():
     with on_error(add, 2, ___suppress_global=False):
         raise RuntimeError
Ejemplo n.º 33
0
 def test2():
     with on_error(add, 3, ___suppress_errors=True):
         raise RuntimeError