UNB/ CS/ David Bremner/ teaching/ cs2613/ tests/ T3/ q3-tests.py
def square(n):
    return n*n
def test_apply_square():
    assert apply(square,[1,2,3])==[1,4,9]

def add1(n):
    return n+1
def test_apply_big():
    assert apply(add1,range(1000))==list(range(1,1001))

def test_apply_parity():
    def parity(num):
        return (-1)**num
    assert apply(parity,range(6))==[1,-1,1,-1,1,-1]
    
def test_apply_string1():
    assert apply(str,range(3,7))==['3','4','5','6']

def test_apply_string2():
    assert apply(int,['3','4','5','6'])==list(range(3,7))

def test_tuple():
    def pair(thing):
        return (thing,thing)
    
    assert apply(pair,['hello',3,7,'bob',10])== \
        [('hello','hello'), (3,3), (7,7), ('bob','bob'),(10,10)]
def test_when():
    def odd(num):
        return num%2 != 0
    assert apply(add1,[1,2,3,4,5,6],when=odd)==[2,2,4,4,6,6]

def test_nums_only():
    def isint(val):
        return isinstance(val,int)
    assert apply(square,[1,'2','3',4,5,'6'],when=isint)==[1,'2','3',16,25,'6']

def test_never():
    def no(val):
        return False
    assert apply(square,['1','2','3','4'],when=no)==['1','2','3','4']
def test_nest_add1():
    assert apply(add1,[1,[2,[3,4],4],5])==[2,[3,[4,5],5],6]

def test_nest_square():
    assert apply(square,[1,[2,[3,4,5]],6])==[1,[4,[9,16,25]],36]

def test_tree():
    def inv(n):
        return 9-n
    assert apply(inv,[[[1,2],[3,4]],[[5,6],[7,8]]])==\
        [[[8,7],[6,5]],[[4,3],[2,1]]]