diff options
author | Walter Dörwald <walter@livinglogic.de> | 2003-12-08 11:38:45 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2003-12-08 11:38:45 (GMT) |
commit | 1dde95dffa44c7ddc4eee7b21cc44274d3e3effe (patch) | |
tree | d0dd6303c14c3c86f91cc93d3b1c028ecc34c334 /Lib/test/test_userlist.py | |
parent | 6b70f8b91d48b1d2ebc8c77a10dcf9e1a7817a19 (diff) | |
download | cpython-1dde95dffa44c7ddc4eee7b21cc44274d3e3effe.zip cpython-1dde95dffa44c7ddc4eee7b21cc44274d3e3effe.tar.gz cpython-1dde95dffa44c7ddc4eee7b21cc44274d3e3effe.tar.bz2 |
Move list and tuple tests from test_types.py to their own scripts:
test_tuple.py and test_list.py. Common tests for tuple, list and UserList
are shared (in seq_tests.py and list_tests.py). Port tests to PyUnit.
(From SF patch #736962)
Diffstat (limited to 'Lib/test/test_userlist.py')
-rw-r--r-- | Lib/test/test_userlist.py | 263 |
1 files changed, 28 insertions, 235 deletions
diff --git a/Lib/test/test_userlist.py b/Lib/test/test_userlist.py index 2d11a24..8c7ef2e 100644 --- a/Lib/test/test_userlist.py +++ b/Lib/test/test_userlist.py @@ -1,165 +1,22 @@ # Check every path through every method of UserList from UserList import UserList -import unittest, test.test_support +import unittest +from test import test_support, list_tests -class UserListTest(unittest.TestCase): - - def test_constructors(self): - l0 = [] - l1 = [0] - l2 = [0, 1] - - u = UserList() - u0 = UserList(l0) - u1 = UserList(l1) - u2 = UserList(l2) - - uu = UserList(u) - uu0 = UserList(u0) - uu1 = UserList(u1) - uu2 = UserList(u2) - - v = UserList(tuple(u)) - class OtherList: - def __init__(self, initlist): - self.__data = initlist - def __len__(self): - return len(self.__data) - def __getitem__(self, i): - return self.__data[i] - v0 = UserList(OtherList(u0)) - vv = UserList("this is also a sequence") - - def test_repr(self): - l0 = [] - l2 = [0, 1, 2] - u0 = UserList(l0) - u2 = UserList(l2) - - self.assertEqual(str(u0), str(l0)) - self.assertEqual(repr(u0), repr(l0)) - self.assertEqual(`u2`, `l2`) - - def test_cmplen(self): - l0 = [] - l1 = [0] - l2 = [0, 1] - - # Test constructors - - u = UserList() - u0 = UserList(l0) - u1 = UserList(l1) - u2 = UserList(l2) - - uu = UserList(u) - uu0 = UserList(u0) - uu1 = UserList(u1) - uu2 = UserList(u2) - - def mycmp(x, y): - r = cmp(x, y) - if r < 0: return -1 - if r > 0: return 1 - return r - - all = [l0, l1, l2, u, u0, u1, u2, uu, uu0, uu1, uu2] - for a in all: - for b in all: - self.assertEqual(mycmp(a, b), mycmp(len(a), len(b))) - - self.assert_(u0 <= u2) - self.assert_(u2 >= u0) - - def test_getitem(self): - u = UserList([0, 1, 2]) - for i in xrange(len(u)): - self.assertEqual(u[i], i) - - def test_setitem(self): - u = UserList([0, 1]) - u[0] = 0 - u[1] = 100 - self.assertEqual(u, [0, 100]) - self.assertRaises(IndexError, u.__setitem__, 2, 200) - - def test_delitem(self): - u = UserList([0, 1]) - del u[1] - del u[0] - self.assertRaises(IndexError, u.__delitem__, 0) +class UserListTest(list_tests.CommonTest): + type2test = UserList def test_getslice(self): - l = [0, 1] - u = UserList(l) - for i in xrange(-3, 4): + super(UserListTest, self).test_getslice() + l = [0, 1, 2, 3, 4] + u = self.type2test(l) + for i in range(-3, 6): self.assertEqual(u[:i], l[:i]) self.assertEqual(u[i:], l[i:]) - for j in xrange(-3, 4): + for j in xrange(-3, 6): self.assertEqual(u[i:j], l[i:j]) - def test_setslice(self): - l = [0, 1] - u = UserList(l) - - # Test __setslice__ - for i in range(-3, 4): - u[:i] = l[:i] - self.assertEqual(u, l) - u2 = u[:] - u2[:i] = u[:i] - self.assertEqual(u2, u) - u[i:] = l[i:] - self.assertEqual(u, l) - u2 = u[:] - u2[i:] = u[i:] - self.assertEqual(u2, u) - for j in range(-3, 4): - u[i:j] = l[i:j] - self.assertEqual(u, l) - u2 = u[:] - u2[i:j] = u[i:j] - self.assertEqual(u2, u) - - uu2 = u2[:] - uu2[:0] = [-2, -1] - self.assertEqual(uu2, [-2, -1, 0, 1]) - uu2[0:] = [] - self.assertEqual(uu2, []) - - def test_contains(self): - u = UserList([0, 1, 2]) - for i in u: - self.assert_(i in u) - for i in min(u)-1, max(u)+1: - self.assert_(i not in u) - - def test_delslice(self): - u = UserList([0, 1]) - del u[1:2] - del u[0:1] - self.assertEqual(u, []) - - u = UserList([0, 1]) - del u[1:] - del u[:1] - self.assertEqual(u, []) - - def test_addmul(self): - u1 = UserList([0]) - u2 = UserList([0, 1]) - self.assertEqual(u1, u1 + []) - self.assertEqual(u1, [] + u1) - self.assertEqual(u1 + [1], u2) - self.assertEqual([-1] + u1, [-1, 0]) - self.assertEqual(u2, u2*1) - self.assertEqual(u2, 1*u2) - self.assertEqual(u2+u2, u2*2) - self.assertEqual(u2+u2, 2*u2) - self.assertEqual(u2+u2+u2, u2*3) - self.assertEqual(u2+u2+u2, 3*u2) - def test_add_specials(self): u = UserList("spam") u2 = u + "eggs" @@ -172,96 +29,32 @@ class UserListTest(unittest.TestCase): u2 = u.__radd__(UserList("spam")) self.assertEqual(u2, list("spameggs")) - def test_append(self): - u = UserList((0, )) - u.append(1) - self.assertEqual(u, [0, 1]) - - def test_insert(self): - u = UserList((0, 1)) - u.insert(0, -1) - self.assertEqual(u, [-1, 0, 1]) - - def test_pop(self): - u = UserList((-1, 0, 1)) - u.pop() - self.assertEqual(u, [-1, 0]) - u.pop(0) - self.assertEqual(u, [0]) - - def test_remove(self): - u = UserList((0, 1)) - u.remove(1) - self.assertEqual(u, [0]) - - def test_count(self): - u = UserList((0, 1))*3 - self.assertEqual(u.count(0), 3) - self.assertEqual(u.count(1), 3) - self.assertEqual(u.count(2), 0) - - def test_index(self): - u = UserList((0, 1)) - self.assertEqual(u.index(0), 0) - self.assertEqual(u.index(1), 1) - self.assertRaises(ValueError, u.index, 2) - - u = UserList([-2,-1,0,0,1,2]) - self.assertEqual(u.count(0), 2) - self.assertEqual(u.index(0), 2) - self.assertEqual(u.index(0,2), 2) - self.assertEqual(u.index(-2,-10), 0) - self.assertEqual(u.index(0,3), 3) - self.assertEqual(u.index(0,3,4), 3) - self.assertRaises(ValueError, u.index, 2,0,-10) - - def test_reverse(self): - u = UserList((0, 1)) - u2 = u[:] - u.reverse() - self.assertEqual(u, [1, 0]) - u.reverse() - self.assertEqual(u, u2) - - def test_sort(self): - u = UserList([1, 0]) - u.sort() - self.assertEqual(u, [0, 1]) - - def test_slice(self): - u = UserList("spam") - u[:2] = "h" - self.assertEqual(u, list("ham")) - def test_iadd(self): - u = UserList((0, 1)) - u += [0, 1] - self.assertEqual(u, [0, 1, 0, 1]) + super(UserListTest, self).test_iadd() + u = [0, 1] u += UserList([0, 1]) - self.assertEqual(u, [0, 1, 0, 1, 0, 1]) - - u = UserList("spam") - u += "eggs" - self.assertEqual(u, list("spameggs")) + self.assertEqual(u, [0, 1, 0, 1]) - def test_extend(self): - u1 = UserList((0, )) - u2 = UserList((0, 1)) - u = u1[:] - u.extend(u2) - self.assertEqual(u, u1 + u2) + def test_mixedcmp(self): + u = self.type2test([0, 1]) + self.assertEqual(u, [0, 1]) + self.assertNotEqual(u, [0]) + self.assertNotEqual(u, [0, 2]) - u = UserList("spam") - u.extend("eggs") - self.assertEqual(u, list("spameggs")) + def test_mixedadd(self): + u = self.type2test([0, 1]) + self.assertEqual(u + [], u) + self.assertEqual(u + [2], [0, 1, 2]) - def test_imul(self): - u = UserList((0, 1)) - u *= 3 - self.assertEqual(u, [0, 1, 0, 1, 0, 1]) + def test_getitemoverwriteiter(self): + # Verify that __getitem__ overrides *are* recognized by __iter__ + class T(self.type2test): + def __getitem__(self, key): + return str(key) + '!!!' + self.assertEqual(iter(T((1,2))).next(), "0!!!") def test_main(): - test.test_support.run_unittest(UserListTest) + test_support.run_unittest(UserListTest) if __name__ == "__main__": test_main() |