6.2.4.8. Test module

Comienzo python section to interscript/tests/test_mxTools.py[1 /1 ]
     1: #line 3484 "mxTools.pak"
     2: import sys
     3: sys.path = ['']+ sys.path
     4: from interscript.core.mxTools import *
     5: import time,whrandom,sys
     6: 
     7: # forall
     8: t = (3,) * 10; assert forall(lambda x: x==3,t) == 1
     9: t = t + (4,); assert forall(lambda x: x==3,t) == 0
    10: 
    11: # exists
    12: t = (3,) * 10; assert exists(lambda x: x==4,t) == 0
    13: t = t + (4,); assert exists(lambda x: x==4,t) == 1
    14: 
    15: # count
    16: t = (3,) * 10; assert count(lambda x: x==3,t) == 10
    17: t = t + (4,); assert count(lambda x: x==4,t) == 1
    18: 
    19: # index
    20: t = (3,) * 10
    21: try:
    22:     index(lambda x: x!=3,t)
    23: except ValueError:
    24:     ok = 1
    25: else:
    26:     ok = 0
    27: assert ok == 1
    28: t = t + (4,); assert index(lambda x: x==4,t) == 10
    29: 
    30: def testkw(x,a=4):
    31:       return x,a
    32: 
    33: # napply
    34: t = napply(10,time.time)
    35: t = napply(10,len,(t,))
    36: t = napply(10,whrandom.randint,(0,10))
    37: t = napply(10,testkw,(2,),{'a':3})
    38: assert t == ((2, 3), (2, 3), (2, 3), (2, 3), (2, 3), (2, 3),
    39:              (2, 3), (2, 3), (2, 3), (2, 3))
    40: 
    41: # trange
    42: t = trange(10); assert t == tuple(range(10))
    43: t = trange(1,10); assert t == tuple(range(1,10))
    44: t = trange(1,10,2); assert t == tuple(range(1,10,2))
    45: t = trange(1,10,3); assert t == tuple(range(1,10,3))
    46: t = trange(-10); assert t == tuple(range(-10))
    47: t = trange(-1,-10); assert t == tuple(range(-1,-10))
    48: t = trange(-10,-1); assert t == tuple(range(-10,-1))
    49: t = trange(-10,-1,2); assert t == tuple(range(-10,-1,2))
    50: t = trange(-10,-1,3); assert t == tuple(range(-10,-1,3))
    51: t = trange(-1,-10,-1); assert t == tuple(range(-1,-10,-1))
    52: t = trange(-1,-10,-2); assert t == tuple(range(-1,-10,-2))
    53: t = trange(-1,-10,-3); assert t == tuple(range(-1,-10,-3))
    54: 
    55: # indices
    56: l = range(10); assert indices(l) == trange(10)
    57: t = trange(10); assert indices(t) == trange(10)
    58: s = '0123456789'; assert indices(s) == trange(10)
    59: 
    60: # range_len
    61: l = range(10); assert range_len(l) == range(10)
    62: t = trange(10); assert range_len(t) == range(10)
    63: s = '0123456789'; assert range_len(s) == range(10)
    64: 
    65: # irange
    66: l = range(1,10,2); assert irange(l) == ((0, 1), (1, 3), (2, 5), (3, 7), (4, 9))
    67: t = range(1,10,2); assert irange(t) == ((0, 1), (1, 3), (2, 5), (3, 7), (4, 9))
    68: d = {0:2,1:5,2:7}; assert irange(d) == ((0, 2), (1, 5), (2, 7))
    69: d = {'a':1,'m':2,'r':3,'c':4}; assert irange(d,'marc') == (('m', 2), ('a', 1), ('r', 3), ('c', 4))
    70: l = range(10); assert irange(l,(1,3,5,6,7)) == ((1, 1), (3, 3), (5, 5), (6, 6), (7, 7))
    71: t = range(10); assert irange(t,(4,1,5,2,3)) == ((4, 4), (1, 1), (5, 5), (2, 2), (3, 3))
    72: 
    73: # ifilter
    74: c = lambda x: x>5
    75: l = range(10); assert ifilter(c,l) == [(6, 6), (7, 7), (8, 8), (9, 9)]
    76: t = trange(10); assert ifilter(c,t) == [(6, 6), (7, 7), (8, 8), (9, 9)]
    77: c = lambda x: x>='f'
    78: s = 'abcdefghijk'; assert ifilter(c,s) == [(5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j'), (10, 'k')]
    79: c = lambda x: x>5
    80: l = range(10); assert ifilter(c,l,(2,6,7)) == [(6, 6), (7, 7)]
    81: t = trange(10); assert ifilter(c,t,(7,6,2)) == [(7, 7), (6, 6)]
    82: c = lambda x: x>='f'
    83: s = 'abcdefghijk'; assert ifilter(c,s,(1,3,5,7)) == [(5, 'f'), (7, 'h')]
    84: 
    85: # mapply
    86: class C:
    87:     def test(self,x,y):
    88:         return (x,y)
    89: o = napply(10,C,()) # create 10 objects
    90: l = map(getattr,o,('test',)*len(o)) # get test methods
    91: r = mapply(l,(1,2)) # call each of them with (1,2)
    92: assert r == ((1,2),)*10
    93: 
    94: # method_mapply
    95: l = [None] * 100000
    96: for i in indices(l):
    97:     l[i] = []
    98: print 'for-loop:',
    99: start = time.clock()
   100: for x in l:
   101:     x.append('hi')
   102: print time.clock() - start,'seconds'
   103: print 'map:',
   104: start = time.clock()
   105: map(lambda x: x.append('hi'),l)
   106: print time.clock() - start,'seconds'
   107: print 'method_mapply:',
   108: start = time.clock()
   109: method_mapply(l,'append',('hi',))
   110: print time.clock() - start,'seconds'
   111: 
   112: print 'checking...'
   113: for x,y,z in l:
   114:     assert x == y == z
   115: 
   116: # get
   117: l = range(10)
   118: assert get(l,2) == 2
   119: assert get(l,20,2) == 2
   120: 
   121: # extract
   122: l = range(10)
   123: assert extract(l,(1,2,3)) == [1,2,3]
   124: assert extract(l,(1,20,30),(1,20,30)) == [1,20,30]
   125: 
   126: # findattr
   127: l = []
   128: d = {}
   129: assert findattr((l,d),'count')
   130: assert findattr((l,d),'items')
   131: 
   132: # tuples
   133: a = range(1,10)
   134: b = range(2,12)
   135: c = range(3,14)
   136: assert tuples(a,b,c) == [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6),
   137:                          (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10),
   138:                          (9, 10, 11)]
   139: assert tuples(c,b,a,b,c) == \
   140:        [(3, 2, 1, 2, 3), (4, 3, 2, 3, 4), (5, 4, 3, 4, 5), (6, 5, 4, 5, 6),
   141:         (7, 6, 5, 6, 7), (8, 7, 6, 7, 8), (9, 8, 7, 8, 9), (10, 9, 8, 9, 10),
   142:         (11, 10, 9, 10, 11), (12, 11, None, 11, 12),
   143:         (13, None, None, None, 13)]
   144: 
   145: # lists
   146: a = range(1,10)
   147: b = range(2,11)
   148: c = range(3,12)
   149: assert (a,b,c) == lists(tuples(a,b,c))
   150: assert lists(b,c,a) == ([2, 3, 1], [3, 4, 2], [4, 5, 3], [5, 6, 4],
   151:                         [6, 7, 5], [7, 8, 6], [8, 9, 7], [9, 10, 8],
   152:                         [10, 11, 9])
   153: assert lists(b[:3],a,c) == ([2, 1, 3], [3, 2, 4], [4, 3, 5])
   154: 
   155: # dict
   156: items = tuples(a,b)
   157: d = dict(items)
   158: assert d == {9: 10, 8: 9, 7: 8, 6: 7, 5: 6, 4: 5, 3: 4, 2: 3, 1: 2}
   159: 
   160: # invdict
   161: assert invdict(d) == {10: 9, 9: 8, 8: 7, 7: 6, 6: 5, 5: 4, 4: 3, 3: 2, 2: 1}
   162: 
   163: # acquire
   164: class C:
   165:         baseobj = None
   166:         def __init__(self,baseobj=None):
   167:                 self.baseobj = baseobj
   168:         __getattr__ = acquire
   169: 
   170: class B:
   171:         a = 1
   172: 
   173: b = B()
   174: c = C(baseobj=b)
   175: assert c.a == 1
   176: 
   177: # xmap
   178: m = xmap(lambda x: 2*x, xrange(sys.maxint))
   179: assert list(m[0:10]) == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
   180: assert list(m[10000:10010]) == [20000, 20002, 20004, 20006, 20008, 20010,
   181:                                 20012, 20014, 20016, 20018]
   182: try:
   183:         m[sys.maxint-1]
   184: except OverflowError:
   185:         pass
   186: else:
   187:         raise AssertionError,'should have received an OverflowError'
   188: 
   189: print 'Works.'
   190: 
End python section to interscript/tests/test_mxTools.py[1]