PyXR

c:\python24\lib \ test \ seq_tests.py



0001 """
0002 Tests common to tuple, list and UserList.UserList
0003 """
0004 
0005 import unittest
0006 from test import test_support
0007 
0008 class CommonTest(unittest.TestCase):
0009     # The type to be tested
0010     type2test = None
0011 
0012     def test_constructors(self):
0013         l0 = []
0014         l1 = [0]
0015         l2 = [0, 1]
0016 
0017         u = self.type2test()
0018         u0 = self.type2test(l0)
0019         u1 = self.type2test(l1)
0020         u2 = self.type2test(l2)
0021 
0022         uu = self.type2test(u)
0023         uu0 = self.type2test(u0)
0024         uu1 = self.type2test(u1)
0025         uu2 = self.type2test(u2)
0026 
0027         v = self.type2test(tuple(u))
0028         class OtherSeq:
0029             def __init__(self, initseq):
0030                 self.__data = initseq
0031             def __len__(self):
0032                 return len(self.__data)
0033             def __getitem__(self, i):
0034                 return self.__data[i]
0035         s = OtherSeq(u0)
0036         v0 = self.type2test(s)
0037         self.assertEqual(len(v0), len(s))
0038 
0039         s = "this is also a sequence"
0040         vv = self.type2test(s)
0041         self.assertEqual(len(vv), len(s))
0042 
0043     def test_truth(self):
0044         self.assert_(not self.type2test())
0045         self.assert_(self.type2test([42]))
0046 
0047     def test_getitem(self):
0048         u = self.type2test([0, 1, 2, 3, 4])
0049         for i in xrange(len(u)):
0050             self.assertEqual(u[i], i)
0051             self.assertEqual(u[long(i)], i)
0052         for i in xrange(-len(u), -1):
0053             self.assertEqual(u[i], len(u)+i)
0054             self.assertEqual(u[long(i)], len(u)+i)
0055         self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
0056         self.assertRaises(IndexError, u.__getitem__, len(u))
0057         self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
0058 
0059         u = self.type2test()
0060         self.assertRaises(IndexError, u.__getitem__, 0)
0061         self.assertRaises(IndexError, u.__getitem__, -1)
0062 
0063         self.assertRaises(TypeError, u.__getitem__)
0064 
0065         a = self.type2test([10, 11])
0066         self.assertEqual(a[0], 10)
0067         self.assertEqual(a[1], 11)
0068         self.assertEqual(a[-2], 10)
0069         self.assertEqual(a[-1], 11)
0070         self.assertRaises(IndexError, a.__getitem__, -3)
0071         self.assertRaises(IndexError, a.__getitem__, 3)
0072 
0073     def test_getslice(self):
0074         l = [0, 1, 2, 3, 4]
0075         u = self.type2test(l)
0076 
0077         self.assertEqual(u[0:0], self.type2test())
0078         self.assertEqual(u[1:2], self.type2test([1]))
0079         self.assertEqual(u[-2:-1], self.type2test([3]))
0080         self.assertEqual(u[-1000:1000], u)
0081         self.assertEqual(u[1000:-1000], self.type2test([]))
0082         self.assertEqual(u[:], u)
0083         self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4]))
0084         self.assertEqual(u[None:3], self.type2test([0, 1, 2]))
0085 
0086         # Extended slices
0087         self.assertEqual(u[::], u)
0088         self.assertEqual(u[::2], self.type2test([0, 2, 4]))
0089         self.assertEqual(u[1::2], self.type2test([1, 3]))
0090         self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0]))
0091         self.assertEqual(u[::-2], self.type2test([4, 2, 0]))
0092         self.assertEqual(u[3::-2], self.type2test([3, 1]))
0093         self.assertEqual(u[3:3:-2], self.type2test([]))
0094         self.assertEqual(u[3:2:-2], self.type2test([3]))
0095         self.assertEqual(u[3:1:-2], self.type2test([3]))
0096         self.assertEqual(u[3:0:-2], self.type2test([3, 1]))
0097         self.assertEqual(u[::-100], self.type2test([4]))
0098         self.assertEqual(u[100:-100:], self.type2test([]))
0099         self.assertEqual(u[-100:100:], u)
0100         self.assertEqual(u[100:-100:-1], u[::-1])
0101         self.assertEqual(u[-100:100:-1], self.type2test([]))
0102         self.assertEqual(u[-100L:100L:2L], self.type2test([0, 2, 4]))
0103 
0104         # Test extreme cases with long ints
0105         a = self.type2test([0,1,2,3,4])
0106         self.assertEqual(a[ -pow(2,128L): 3 ], self.type2test([0,1,2]))
0107         self.assertEqual(a[ 3: pow(2,145L) ], self.type2test([3,4]))
0108 
0109         self.assertRaises(TypeError, u.__getslice__)
0110 
0111     def test_contains(self):
0112         u = self.type2test([0, 1, 2])
0113         for i in u:
0114             self.assert_(i in u)
0115         for i in min(u)-1, max(u)+1:
0116             self.assert_(i not in u)
0117 
0118         self.assertRaises(TypeError, u.__contains__)
0119 
0120     def test_len(self):
0121         self.assertEqual(len(self.type2test()), 0)
0122         self.assertEqual(len(self.type2test([])), 0)
0123         self.assertEqual(len(self.type2test([0])), 1)
0124         self.assertEqual(len(self.type2test([0, 1, 2])), 3)
0125 
0126     def test_minmax(self):
0127         u = self.type2test([0, 1, 2])
0128         self.assertEqual(min(u), 0)
0129         self.assertEqual(max(u), 2)
0130 
0131     def test_addmul(self):
0132         u1 = self.type2test([0])
0133         u2 = self.type2test([0, 1])
0134         self.assertEqual(u1, u1 + self.type2test())
0135         self.assertEqual(u1, self.type2test() + u1)
0136         self.assertEqual(u1 + self.type2test([1]), u2)
0137         self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
0138         self.assertEqual(self.type2test(), u2*0)
0139         self.assertEqual(self.type2test(), 0*u2)
0140         self.assertEqual(self.type2test(), u2*0L)
0141         self.assertEqual(self.type2test(), 0L*u2)
0142         self.assertEqual(u2, u2*1)
0143         self.assertEqual(u2, 1*u2)
0144         self.assertEqual(u2, u2*1L)
0145         self.assertEqual(u2, 1L*u2)
0146         self.assertEqual(u2+u2, u2*2)
0147         self.assertEqual(u2+u2, 2*u2)
0148         self.assertEqual(u2+u2, u2*2L)
0149         self.assertEqual(u2+u2, 2L*u2)
0150         self.assertEqual(u2+u2+u2, u2*3)
0151         self.assertEqual(u2+u2+u2, 3*u2)
0152 
0153         class subclass(self.type2test):
0154             pass
0155         u3 = subclass([0, 1])
0156         self.assertEqual(u3, u3*1)
0157         self.assert_(u3 is not u3*1)
0158 
0159     def test_iadd(self):
0160         u = self.type2test([0, 1])
0161         u += self.type2test()
0162         self.assertEqual(u, self.type2test([0, 1]))
0163         u += self.type2test([2, 3])
0164         self.assertEqual(u, self.type2test([0, 1, 2, 3]))
0165         u += self.type2test([4, 5])
0166         self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
0167 
0168         u = self.type2test("spam")
0169         u += self.type2test("eggs")
0170         self.assertEqual(u, self.type2test("spameggs"))
0171 
0172     def test_imul(self):
0173         u = self.type2test([0, 1])
0174         u *= 3
0175         self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
0176 
0177     def test_getitemoverwriteiter(self):
0178         # Verify that __getitem__ overrides are not recognized by __iter__
0179         class T(self.type2test):
0180             def __getitem__(self, key):
0181                 return str(key) + '!!!'
0182         self.assertEqual(iter(T((1,2))).next(), 1)
0183 
0184     def test_repeat(self):
0185         for m in xrange(4):
0186             s = tuple(range(m))
0187             for n in xrange(-3, 5):
0188                 self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
0189             self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
0190             self.assertEqual(id(s), id(s*1))
0191 
0192     def test_subscript(self):
0193         a = self.type2test([10, 11])
0194         self.assertEqual(a.__getitem__(0L), 10)
0195         self.assertEqual(a.__getitem__(1L), 11)
0196         self.assertEqual(a.__getitem__(-2L), 10)
0197         self.assertEqual(a.__getitem__(-1L), 11)
0198         self.assertRaises(IndexError, a.__getitem__, -3)
0199         self.assertRaises(IndexError, a.__getitem__, 3)
0200         self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
0201         self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
0202         self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
0203         self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
0204         self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
0205         self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
0206         self.assertRaises(TypeError, a.__getitem__, 'x')
0207 

Generated by PyXR 0.9.4
SourceForge.net Logo