Source

intspan / test / test_intspan.py

Full commit

from intspan import intspan

def test_basic():
    s = intspan()
    tests = ['', '1','1-2', '1-3,9-10', '1-3,14,29,92-97']
    for t in tests:
        s = intspan(t)
        assert str(s) == t
        
def test_contains():
    s = intspan()
    assert 1 not in s
    assert 100 not in s
    assert 0 not in s
    
    t = intspan('1,10')
    assert 1 in t
    assert 10 in t
    assert 0 not in t
    assert 2 not in t
    
def test_equals():
    s = intspan('1,3,5,7,9')
    assert s == set([1,3,5,7,9])
    
def test_copy():
    t = intspan('1,10')
    tt = t.copy()
    assert type(tt) == type(t)
    assert t == tt
    assert t is not tt
    
def test_merge():
    assert str(intspan('1-4,5')) ==  '1-5'
        
def test_out_of_order():
    assert str(intspan('1,0,99,4,7,9,98')) == '0-1,4,7,9,98-99'

def test_discard():
    s = intspan('1-3,14,29,92-97')
    s.discard('2,13,92')
    assert str(s) == '1,3,14,29,93-97'

def test_add():
    s = intspan('1-2')
    s.add('3,29')
    assert str(s) == '1-3,29'
    s.add('92,97,96,95,94')
    assert str(s) == '1-3,29,92,94-97'
    s.add(93)
    assert str(s) == '1-3,29,92-97'
    s.add('14')
    assert str(s) == '1-3,14,29,92-97'
    
def test_iteration():
    s = intspan('92,97,96,95,0,94')
    assert [ item for item in s ] == [0, 92, 94, 95, 96, 97]
    assert list(s) == [0, 92, 94, 95, 96, 97]
    assert set(s) == set([0, 92, 94, 95, 96, 97])
  
def test_issubset():
    s = intspan('92,97,96,95,0,94')
    assert s.issubset('0-100')
    assert s.issubset(range(98))
    assert s.issubset(range(101))
    assert s.issubset('0, 92-100')
    assert s.issubset( [0] + list(range(92,101)) )
    assert s.issubset(intspan('92,97,96,95,0,94'))
    assert s.issubset([0, 92, 94, 95, 96, 97])
    assert not s.issubset('0-10')
    assert not s.issubset(range(20))
    assert not s.issubset(range(95))

def test_issuperset():
    s = intspan('0-3,7')
    assert s.issuperset('0-2')
    assert s.issuperset([0,1,3])
    assert not s.issuperset(range(6))
    assert not s.issuperset('0-6')
    
    assert s >= intspan('0-2')
    assert s >= intspan([0,1,3])
    assert not s >= range(6)
    assert not s >= intspan('0-6')

def test_union():
    s = intspan('0-3,7')
    assert s.union('0-2') == s
    assert list(s.union('0-2')) == [0, 1, 2, 3, 7]
    assert list(s.union([99,101])) ==  [0, 1, 2, 3, 7, 99, 101]
    assert s.union([99,101]) == intspan('0-3,7,99,101')
    
    assert s | intspan('0-2') == s.union('0-2')
    assert s | [99,101] == s.union('99,101')

def test_intersection():
    s = intspan('1-8')
    t = intspan('2-5')
    u = intspan('8,100')
    assert s.intersection(t) == intspan('2-5')
    assert t.intersection(u) == intspan()
    assert s.intersection(u) == intspan('8')
    
    assert s & t == s.intersection(t)
    assert t & u == t.intersection(u) 
    assert s & u == s.intersection(u) 

def test_difference():
    s = intspan('1-8')
    t = intspan('2-5')
    assert s.difference(t) == intspan('1,6-8')
    assert t.difference(s) == intspan()
    
    assert s - t == s.difference(t)
    assert t - s == t.difference(s)

def test_symmetric_difference():
    s = intspan('1-8')
    t = intspan('2-5')
    assert s.symmetric_difference(t) == intspan('1,6-8')
    assert t.symmetric_difference(s) == intspan('1,6-8')
    assert t.symmetric_difference(t) == intspan()
    
    assert s ^ t == s.symmetric_difference(t)
    assert t ^ s == t.symmetric_difference(s)
    assert t ^ t == t.symmetric_difference(t)
    
def test_increments():
    s = intspan('50-60')
    s |= intspan('10-20')
    assert s == intspan('10-20,50-60')
    s &= intspan('0-55')
    assert s == intspan('10-20,50-55')
    s -= intspan('16-20')
    assert s == intspan('10-15,50-55')
    s ^= intspan('10,99')
    assert s == intspan('11-15,50-55,99')
    
def test_pop():
    s = intspan('100-110')
    assert s.pop() == 100
    assert s.pop() == 101
    assert s.pop() == 102
    assert s.pop() == 103
    assert s.pop() == 104
    assert s.pop() == 105
    assert s == intspan('106-110')