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 | |
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')
-rw-r--r-- | Lib/test/list_tests.py | 418 | ||||
-rw-r--r-- | Lib/test/output/test_types | 5 | ||||
-rw-r--r-- | Lib/test/seq_tests.py | 171 | ||||
-rw-r--r-- | Lib/test/test_list.py | 25 | ||||
-rw-r--r-- | Lib/test/test_tuple.py | 49 | ||||
-rw-r--r-- | Lib/test/test_types.py | 279 | ||||
-rw-r--r-- | Lib/test/test_userlist.py | 263 |
7 files changed, 696 insertions, 514 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py new file mode 100644 index 0000000..84327a6 --- /dev/null +++ b/Lib/test/list_tests.py @@ -0,0 +1,418 @@ +""" +Tests common to list and UserList.UserList +""" + +import sys + +import unittest +from test import test_support, seq_tests + +class CommonTest(seq_tests.CommonTest): + + def test_repr(self): + l0 = [] + l2 = [0, 1, 2] + a0 = self.type2test(l0) + a2 = self.type2test(l2) + + self.assertEqual(str(a0), str(l0)) + self.assertEqual(repr(a0), repr(l0)) + self.assertEqual(`a2`, `l2`) + self.assertEqual(str(a2), "[0, 1, 2]") + self.assertEqual(repr(a2), "[0, 1, 2]") + + def test_setitem(self): + a = self.type2test([0, 1]) + a[0] = 0 + a[1] = 100 + self.assertEqual(a, self.type2test([0, 100])) + a[-1] = 200 + self.assertEqual(a, self.type2test([0, 200])) + a[-2] = 100 + self.assertEqual(a, self.type2test([100, 200])) + self.assertRaises(IndexError, a.__setitem__, -3, 200) + self.assertRaises(IndexError, a.__setitem__, 2, 200) + + a = self.type2test([]) + self.assertRaises(IndexError, a.__setitem__, 0, 200) + self.assertRaises(IndexError, a.__setitem__, -1, 200) + + self.assertRaises(TypeError, a.__setitem__) + + a = self.type2test([0,1,2,3,4]) + a[0L] = 1 + a[1L] = 2 + a[2L] = 3 + self.assertEqual(a, self.type2test([1,2,3,3,4])) + a[0] = 5 + a[1] = 6 + a[2] = 7 + self.assertEqual(a, self.type2test([5,6,7,3,4])) + a[-2L] = 88 + a[-1L] = 99 + self.assertEqual(a, self.type2test([5,6,7,88,99])) + a[-2] = 8 + a[-1] = 9 + self.assertEqual(a, self.type2test([5,6,7,8,9])) + + def test_delitem(self): + a = self.type2test([0, 1]) + del a[1] + self.assertEqual(a, [0]) + del a[0] + self.assertEqual(a, []) + + a = self.type2test([0, 1]) + del a[-2] + self.assertEqual(a, [1]) + del a[-1] + self.assertEqual(a, []) + + a = self.type2test([0, 1]) + self.assertRaises(IndexError, a.__delitem__, -3) + self.assertRaises(IndexError, a.__delitem__, 2) + + a = self.type2test([]) + self.assertRaises(IndexError, a.__delitem__, 0) + + self.assertRaises(TypeError, a.__delitem__) + + def test_setslice(self): + l = [0, 1] + a = self.type2test(l) + + for i in range(-3, 4): + a[:i] = l[:i] + self.assertEqual(a, l) + a2 = a[:] + a2[:i] = a[:i] + self.assertEqual(a2, a) + a[i:] = l[i:] + self.assertEqual(a, l) + a2 = a[:] + a2[i:] = a[i:] + self.assertEqual(a2, a) + for j in range(-3, 4): + a[i:j] = l[i:j] + self.assertEqual(a, l) + a2 = a[:] + a2[i:j] = a[i:j] + self.assertEqual(a2, a) + + aa2 = a2[:] + aa2[:0] = [-2, -1] + self.assertEqual(aa2, [-2, -1, 0, 1]) + aa2[0:] = [] + self.assertEqual(aa2, []) + + a = self.type2test([1, 2, 3, 4, 5]) + a[:-1] = a + self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 5])) + a = self.type2test([1, 2, 3, 4, 5]) + a[1:] = a + self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5])) + a = self.type2test([1, 2, 3, 4, 5]) + a[1:-1] = a + self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5, 5])) + + a = self.type2test([]) + a[:] = tuple(range(10)) + self.assertEqual(a, self.type2test(range(10))) + + self.assertRaises(TypeError, a.__setslice__, 0, 1, 5) + + self.assertRaises(TypeError, a.__setslice__) + + def test_delslice(self): + a = self.type2test([0, 1]) + del a[1:2] + del a[0:1] + self.assertEqual(a, self.type2test([])) + + a = self.type2test([0, 1]) + del a[1L:2L] + del a[0L:1L] + self.assertEqual(a, self.type2test([])) + + a = self.type2test([0, 1]) + del a[-2:-1] + self.assertEqual(a, self.type2test([1])) + + a = self.type2test([0, 1]) + del a[-2L:-1L] + self.assertEqual(a, self.type2test([1])) + + a = self.type2test([0, 1]) + del a[1:] + del a[:1] + self.assertEqual(a, self.type2test([])) + + a = self.type2test([0, 1]) + del a[1L:] + del a[:1L] + self.assertEqual(a, self.type2test([])) + + a = self.type2test([0, 1]) + del a[-1:] + self.assertEqual(a, self.type2test([0])) + + a = self.type2test([0, 1]) + del a[-1L:] + self.assertEqual(a, self.type2test([0])) + + a = self.type2test([0, 1]) + del a[:] + self.assertEqual(a, self.type2test([])) + + def test_append(self): + a = self.type2test([]) + a.append(0) + a.append(1) + a.append(2) + self.assertEqual(a, self.type2test([0, 1, 2])) + + self.assertRaises(TypeError, a.append) + + def test_extend(self): + a1 = self.type2test([0]) + a2 = self.type2test((0, 1)) + a = a1[:] + a.extend(a2) + self.assertEqual(a, a1 + a2) + + a.extend(self.type2test([])) + self.assertEqual(a, a1 + a2) + + a.extend(a) + self.assertEqual(a, self.type2test([0, 0, 1, 0, 0, 1])) + + a = self.type2test("spam") + a.extend("eggs") + self.assertEqual(a, list("spameggs")) + + self.assertRaises(TypeError, a.extend, None) + + self.assertRaises(TypeError, a.extend) + + def test_insert(self): + a = self.type2test([0, 1, 2]) + a.insert(0, -2) + a.insert(1, -1) + a.insert(2, 0) + self.assertEqual(a, [-2, -1, 0, 0, 1, 2]) + + b = a[:] + b.insert(-2, "foo") + b.insert(-200, "left") + b.insert(200, "right") + self.assertEqual(b, self.type2test(["left",-2,-1,0,0,"foo",1,2,"right"])) + + self.assertRaises(TypeError, a.insert) + + def test_pop(self): + a = self.type2test([-1, 0, 1]) + a.pop() + self.assertEqual(a, [-1, 0]) + a.pop(0) + self.assertEqual(a, [0]) + self.assertRaises(IndexError, a.pop, 5) + a.pop(0) + self.assertEqual(a, []) + self.assertRaises(IndexError, a.pop) + + self.assertRaises(TypeError, a.pop, 42, 42) + + def test_remove(self): + a = self.type2test([0, 0, 1]) + a.remove(1) + self.assertEqual(a, [0, 0]) + a.remove(0) + self.assertEqual(a, [0]) + a.remove(0) + self.assertEqual(a, []) + + self.assertRaises(ValueError, a.remove, 0) + + self.assertRaises(TypeError, a.remove) + + class BadExc(Exception): + pass + + class BadCmp: + def __eq__(self, other): + if other == 2: + raise BadExc() + return False + + a = self.type2test([0, 1, 2, 3]) + self.assertRaises(BadExc, a.remove, BadCmp()) + + def test_count(self): + a = self.type2test([0, 1, 2])*3 + self.assertEqual(a.count(0), 3) + self.assertEqual(a.count(1), 3) + self.assertEqual(a.count(3), 0) + + self.assertRaises(TypeError, a.count) + + class BadExc(Exception): + pass + + class BadCmp: + def __eq__(self, other): + if other == 2: + raise BadExc() + return False + + self.assertRaises(BadExc, a.count, BadCmp()) + + def test_index(self): + u = self.type2test([0, 1]) + self.assertEqual(u.index(0), 0) + self.assertEqual(u.index(1), 1) + self.assertRaises(ValueError, u.index, 2) + + u = self.type2test([-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) + + self.assertRaises(TypeError, u.index) + + class BadExc(Exception): + pass + + class BadCmp: + def __eq__(self, other): + if other == 2: + raise BadExc() + return False + + a = self.type2test([0, 1, 2, 3]) + self.assertRaises(BadExc, a.index, BadCmp()) + + a = self.type2test([-2, -1, 0, 0, 1, 2]) + self.assertEqual(a.index(0), 2) + self.assertEqual(a.index(0, 2), 2) + self.assertEqual(a.index(0, -4), 2) + self.assertEqual(a.index(-2, -10), 0) + self.assertEqual(a.index(0, 3), 3) + self.assertEqual(a.index(0, -3), 3) + self.assertEqual(a.index(0, 3, 4), 3) + self.assertEqual(a.index(0, -3, -2), 3) + self.assertEqual(a.index(0, -4*sys.maxint, 4*sys.maxint), 2) + self.assertRaises(ValueError, a.index, 0, 4*sys.maxint,-4*sys.maxint) + self.assertRaises(ValueError, a.index, 2, 0, -10) + a.remove(0) + self.assertRaises(ValueError, a.index, 2, 0, 4) + self.assertEqual(a, self.type2test([-2, -1, 0, 1, 2])) + + def test_reverse(self): + u = self.type2test([-2, -1, 0, 1, 2]) + u2 = u[:] + u.reverse() + self.assertEqual(u, [2, 1, 0, -1, -2]) + u.reverse() + self.assertEqual(u, u2) + + self.assertRaises(TypeError, u.reverse, 42) + + def test_sort(self): + u = self.type2test([1, 0]) + u.sort() + self.assertEqual(u, [0, 1]) + + u = self.type2test([2,1,0,-1,-2]) + u.sort() + self.assertEqual(u, self.type2test([-2,-1,0,1,2])) + + self.assertRaises(TypeError, u.sort, 42, 42) + + def revcmp(a, b): + return cmp(b, a) + u.sort(revcmp) + self.assertEqual(u, self.type2test([2,1,0,-1,-2])) + + # The following dumps core in unpatched Python 1.5: + def myComparison(x,y): + return cmp(x%3, y%7) + z = self.type2test(range(12)) + z.sort(myComparison) + + self.assertRaises(TypeError, z.sort, 2) + + def selfmodifyingComparison(x,y): + z.append(1) + return cmp(x, y) + self.assertRaises(ValueError, z.sort, selfmodifyingComparison) + + self.assertRaises(TypeError, z.sort, lambda x, y: 's') + + self.assertRaises(TypeError, z.sort, 42, 42, 42, 42) + + def test_slice(self): + u = self.type2test("spam") + u[:2] = "h" + self.assertEqual(u, list("ham")) + + def test_iadd(self): + super(CommonTest, self).test_iadd() + u = self.type2test([0, 1]) + u2 = u + u += [2, 3] + self.assert_(u is u2) + + u = self.type2test("spam") + u += "eggs" + self.assertEqual(u, self.type2test("spameggs")) + + self.assertRaises(TypeError, u.__iadd__, None) + + def test_imul(self): + u = self.type2test([0, 1]) + u *= 3 + self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1])) + u *= 0 + self.assertEqual(u, self.type2test([])) + + def test_extendedslicing(self): + # subscript + a = self.type2test([0,1,2,3,4]) + + # deletion + del a[::2] + self.assertEqual(a, self.type2test([1,3])) + a = self.type2test(range(5)) + del a[1::2] + self.assertEqual(a, self.type2test([0,2,4])) + a = self.type2test(range(5)) + del a[1::-2] + self.assertEqual(a, self.type2test([0,2,3,4])) + a = self.type2test(range(10)) + del a[::1000] + self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 6, 7, 8, 9])) + # assignment + a = self.type2test(range(10)) + a[::2] = [-1]*5 + self.assertEqual(a, self.type2test([-1, 1, -1, 3, -1, 5, -1, 7, -1, 9])) + a = self.type2test(range(10)) + a[::-4] = [10]*3 + self.assertEqual(a, self.type2test([0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) + a = self.type2test(range(4)) + a[::-1] = a + self.assertEqual(a, self.type2test([3, 2, 1, 0])) + a = self.type2test(range(10)) + b = a[:] + c = a[:] + a[2:3] = self.type2test(["two", "elements"]) + b[slice(2,3)] = self.type2test(["two", "elements"]) + c[2:3:] = self.type2test(["two", "elements"]) + self.assertEqual(a, b) + self.assertEqual(a, c) + a = self.type2test(range(10)) + a[::2] = tuple(range(5)) + self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9])) diff --git a/Lib/test/output/test_types b/Lib/test/output/test_types index 7e90dc0..b2f9429 100644 --- a/Lib/test/output/test_types +++ b/Lib/test/output/test_types @@ -9,8 +9,7 @@ test_types 6.4.3 Floating point numbers 6.5 Sequence types 6.5.1 Strings -6.5.2 Tuples -6.5.3 Lists -6.5.3a Additional list operations +6.5.2 Tuples [see test_tuple.py] +6.5.3 Lists [see test_list.py] 6.6 Mappings == Dictionaries Buffers diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py new file mode 100644 index 0000000..f702b09 --- /dev/null +++ b/Lib/test/seq_tests.py @@ -0,0 +1,171 @@ +""" +Tests common to tuple, list and UserList.UserList +""" + +import unittest +from test import test_support + +class CommonTest(unittest.TestCase): + # The type to be tested + type2test = None + + def test_constructors(self): + l0 = [] + l1 = [0] + l2 = [0, 1] + + u = self.type2test() + u0 = self.type2test(l0) + u1 = self.type2test(l1) + u2 = self.type2test(l2) + + uu = self.type2test(u) + uu0 = self.type2test(u0) + uu1 = self.type2test(u1) + uu2 = self.type2test(u2) + + v = self.type2test(tuple(u)) + class OtherSeq: + def __init__(self, initseq): + self.__data = initseq + def __len__(self): + return len(self.__data) + def __getitem__(self, i): + return self.__data[i] + s = OtherSeq(u0) + v0 = self.type2test(s) + self.assertEqual(len(v0), len(s)) + + s = "this is also a sequence" + vv = self.type2test(s) + self.assertEqual(len(vv), len(s)) + + def test_truth(self): + self.assert_(not self.type2test()) + self.assert_(self.type2test([42])) + + def test_getitem(self): + u = self.type2test([0, 1, 2, 3, 4]) + for i in xrange(len(u)): + self.assertEqual(u[i], i) + for i in xrange(-len(u), -1): + self.assertEqual(u[i], len(u)+i) + self.assertRaises(IndexError, u.__getitem__, -len(u)-1) + self.assertRaises(IndexError, u.__getitem__, len(u)) + + u = self.type2test() + self.assertRaises(IndexError, u.__getitem__, 0) + self.assertRaises(IndexError, u.__getitem__, -1) + + self.assertRaises(TypeError, u.__getitem__) + + def test_getslice(self): + l = [0, 1, 2, 3, 4] + u = self.type2test(l) + + self.assertEqual(u[0:0], self.type2test()) + self.assertEqual(u[1:2], self.type2test([1])) + self.assertEqual(u[-2:-1], self.type2test([3])) + self.assertEqual(u[-1000:1000], u) + self.assertEqual(u[1000:-1000], self.type2test([])) + self.assertEqual(u[:], u) + self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4])) + self.assertEqual(u[None:3], self.type2test([0, 1, 2])) + + # Extended slices + self.assertEqual(u[::], u) + self.assertEqual(u[::2], self.type2test([0, 2, 4])) + self.assertEqual(u[1::2], self.type2test([1, 3])) + self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0])) + self.assertEqual(u[::-2], self.type2test([4, 2, 0])) + self.assertEqual(u[3::-2], self.type2test([3, 1])) + self.assertEqual(u[3:3:-2], self.type2test([])) + self.assertEqual(u[3:2:-2], self.type2test([3])) + self.assertEqual(u[3:1:-2], self.type2test([3])) + self.assertEqual(u[3:0:-2], self.type2test([3, 1])) + self.assertEqual(u[::-100], self.type2test([4])) + self.assertEqual(u[100:-100:], self.type2test([])) + self.assertEqual(u[-100:100:], u) + self.assertEqual(u[100:-100:-1], u[::-1]) + self.assertEqual(u[-100:100:-1], self.type2test([])) + self.assertEqual(u[-100L:100L:2L], self.type2test([0, 2, 4])) + + # Test extreme cases with long ints + a = self.type2test([0,1,2,3,4]) + self.assertEqual(a[ -pow(2,128L): 3 ], self.type2test([0,1,2])) + self.assertEqual(a[ 3: pow(2,145L) ], self.type2test([3,4])) + + self.assertRaises(TypeError, u.__getslice__) + + def test_contains(self): + u = self.type2test([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) + + self.assertRaises(TypeError, u.__contains__) + + def test_len(self): + self.assertEqual(len(self.type2test()), 0) + self.assertEqual(len(self.type2test([])), 0) + self.assertEqual(len(self.type2test([0])), 1) + self.assertEqual(len(self.type2test([0, 1, 2])), 3) + + def test_minmax(self): + u = self.type2test([0, 1, 2]) + self.assertEqual(min(u), 0) + self.assertEqual(max(u), 2) + + def test_addmul(self): + u1 = self.type2test([0]) + u2 = self.type2test([0, 1]) + self.assertEqual(u1, u1 + self.type2test()) + self.assertEqual(u1, self.type2test() + u1) + self.assertEqual(u1 + self.type2test([1]), u2) + self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0])) + self.assertEqual(self.type2test(), u2*0) + self.assertEqual(self.type2test(), 0*u2) + self.assertEqual(self.type2test(), u2*0L) + self.assertEqual(self.type2test(), 0L*u2) + self.assertEqual(u2, u2*1) + self.assertEqual(u2, 1*u2) + self.assertEqual(u2, u2*1L) + self.assertEqual(u2, 1L*u2) + self.assertEqual(u2+u2, u2*2) + self.assertEqual(u2+u2, 2*u2) + self.assertEqual(u2+u2, u2*2L) + self.assertEqual(u2+u2, 2L*u2) + self.assertEqual(u2+u2+u2, u2*3) + self.assertEqual(u2+u2+u2, 3*u2) + + class subclass(self.type2test): + pass + u3 = subclass([0, 1]) + self.assertEqual(u3, u3*1) + self.assert_(u3 is not u3*1) + + def test_iadd(self): + u = self.type2test([0, 1]) + u += self.type2test() + self.assertEqual(u, self.type2test([0, 1])) + u += self.type2test([2, 3]) + self.assertEqual(u, self.type2test([0, 1, 2, 3])) + u += self.type2test([4, 5]) + self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5])) + + u = self.type2test("spam") + u += self.type2test("eggs") + self.assertEqual(u, self.type2test("spameggs")) + + def test_imul(self): + u = self.type2test([0, 1]) + u *= 3 + self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1])) + + def test_getitemoverwriteiter(self): + # Verify that __getitem__ overrides are not recognized by __iter__ + class T(self.type2test): + def __getitem__(self, key): + return str(key) + '!!!' + self.assertEqual(iter(T((1,2))).next(), 1) diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py new file mode 100644 index 0000000..ba2f901 --- /dev/null +++ b/Lib/test/test_list.py @@ -0,0 +1,25 @@ +import unittest +from test import test_support, list_tests + +class ListTest(list_tests.CommonTest): + type2test = list + + def test_truth(self): + super(ListTest, self).test_truth() + self.assert_(not []) + self.assert_([42]) + + def test_identity(self): + self.assert_([] is not []) + + def test_len(self): + super(ListTest, self).test_len() + self.assertEqual(len([]), 0) + self.assertEqual(len([0]), 1) + self.assertEqual(len([0, 1, 2]), 3) + +def test_main(): + test_support.run_unittest(ListTest) + +if __name__=="__main__": + test_main() diff --git a/Lib/test/test_tuple.py b/Lib/test/test_tuple.py new file mode 100644 index 0000000..4fe299f --- /dev/null +++ b/Lib/test/test_tuple.py @@ -0,0 +1,49 @@ +import unittest +from test import test_support, seq_tests + +class TupleTest(seq_tests.CommonTest): + type2test = tuple + + def test_constructors(self): + super(TupleTest, self).test_len() + # calling built-in types without argument must return empty + self.assertEqual(tuple(), ()) + + def test_truth(self): + super(TupleTest, self).test_truth() + self.assert_(not ()) + self.assert_((42, )) + + def test_len(self): + super(TupleTest, self).test_len() + self.assertEqual(len(()), 0) + self.assertEqual(len((0,)), 1) + self.assertEqual(len((0, 1, 2)), 3) + + def test_iadd(self): + super(TupleTest, self).test_iadd() + u = (0, 1) + u2 = u + u += (2, 3) + self.assert_(u is not u2) + + def test_imul(self): + super(TupleTest, self).test_imul() + u = (0, 1) + u2 = u + u *= 3 + self.assert_(u is not u2) + + def test_tupleresizebug(self): + # Check that a specific bug in _PyTuple_Resize() is squashed. + def f(): + for i in range(1000): + yield i + self.assertEqual(list(tuple(f())), range(1000)) + + +def test_main(): + test_support.run_unittest(TupleTest) + +if __name__=="__main__": + test_main() diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index f39c51a..7dbae72 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -10,15 +10,11 @@ if 0: raise TestFailed, '0 is true instead of false' if 0L: raise TestFailed, '0L is true instead of false' if 0.0: raise TestFailed, '0.0 is true instead of false' if '': raise TestFailed, '\'\' is true instead of false' -if (): raise TestFailed, '() is true instead of false' -if []: raise TestFailed, '[] is true instead of false' if {}: raise TestFailed, '{} is true instead of false' if not 1: raise TestFailed, '1 is false instead of true' if not 1L: raise TestFailed, '1L is false instead of true' if not 1.0: raise TestFailed, '1.0 is false instead of true' if not 'x': raise TestFailed, '\'x\' is false instead of true' -if not (1, 1): raise TestFailed, '(1, 1) is false instead of true' -if not [1]: raise TestFailed, '[1] is false instead of true' if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' def f(): pass class C: pass @@ -44,9 +40,7 @@ if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass else: raise TestFailed, 'float comparisons failed' if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass else: raise TestFailed, 'string comparisons failed' -if 0 in [0] and 0 not in [1]: pass -else: raise TestFailed, 'membership test failed' -if None is None and [] is not []: pass +if None is None: pass else: raise TestFailed, 'identity test failed' try: float('') @@ -217,276 +211,9 @@ if have_unicode: vereq(a[-100L:100L:2L], unicode('02468', 'ascii')) -print '6.5.2 Tuples' -# calling built-in types without argument must return empty -if tuple() != (): raise TestFailed,'tuple() does not return ()' -if len(()) != 0: raise TestFailed, 'len(())' -if len((1,)) != 1: raise TestFailed, 'len((1,))' -if len((1,2,3,4,5,6)) != 6: raise TestFailed, 'len((1,2,3,4,5,6))' -if (1,2)+(3,4) != (1,2,3,4): raise TestFailed, 'tuple concatenation' -if (1,2)*3 != (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3' -if 0*(1,2,3) != (): raise TestFailed, 'tuple repetition 0*' -if min((1,2)) != 1 or max((1,2)) != 2: raise TestFailed, 'min/max tuple' -if 0 in (0,1,2) and 1 in (0,1,2) and 2 in (0,1,2) and 3 not in (0,1,2): pass -else: raise TestFailed, 'in/not in tuple' -try: ()[0] -except IndexError: pass -else: raise TestFailed, "tuple index error didn't raise IndexError" -x = () -x += () -if x != (): raise TestFailed, 'tuple inplace add from () to () failed' -x += (1,) -if x != (1,): raise TestFailed, 'tuple resize from () failed' - -# extended slicing - subscript only for tuples -a = (0,1,2,3,4) -vereq(a[::], a) -vereq(a[::2], (0,2,4)) -vereq(a[1::2], (1,3)) -vereq(a[::-1], (4,3,2,1,0)) -vereq(a[::-2], (4,2,0)) -vereq(a[3::-2], (3,1)) -vereq(a[-100:100:], a) -vereq(a[100:-100:-1], a[::-1]) -vereq(a[-100L:100L:2L], (0,2,4)) - -# Check that a specific bug in _PyTuple_Resize() is squashed. -def f(): - for i in range(1000): - yield i -vereq(list(tuple(f())), range(1000)) +print '6.5.2 Tuples [see test_tuple.py]' -# Verify that __getitem__ overrides are not recognized by __iter__ -class T(tuple): - def __getitem__(self, key): - return str(key) + '!!!' -vereq(iter(T((1,2))).next(), 1) - -print '6.5.3 Lists' -# calling built-in types without argument must return empty -if list() != []: raise TestFailed,'list() does not return []' -if len([]) != 0: raise TestFailed, 'len([])' -if len([1,]) != 1: raise TestFailed, 'len([1,])' -if len([1,2,3,4,5,6]) != 6: raise TestFailed, 'len([1,2,3,4,5,6])' -if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation' -if [1,2]*3 != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3' -if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L' -if 0*[1,2,3] != []: raise TestFailed, 'list repetition 0*' -if 0L*[1,2,3] != []: raise TestFailed, 'list repetition 0L*' -if min([1,2]) != 1 or max([1,2]) != 2: raise TestFailed, 'min/max list' -if 0 in [0,1,2] and 1 in [0,1,2] and 2 in [0,1,2] and 3 not in [0,1,2]: pass -else: raise TestFailed, 'in/not in list' -a = [1, 2, 3, 4, 5] -a[:-1] = a -if a != [1, 2, 3, 4, 5, 5]: - raise TestFailed, "list self-slice-assign (head)" -a = [1, 2, 3, 4, 5] -a[1:] = a -if a != [1, 1, 2, 3, 4, 5]: - raise TestFailed, "list self-slice-assign (tail)" -a = [1, 2, 3, 4, 5] -a[1:-1] = a -if a != [1, 1, 2, 3, 4, 5, 5]: - raise TestFailed, "list self-slice-assign (center)" -try: [][0] -except IndexError: pass -else: raise TestFailed, "list index error didn't raise IndexError" -try: [][0] = 5 -except IndexError: pass -else: raise TestFailed, "list assignment index error didn't raise IndexError" -try: [].pop() -except IndexError: pass -else: raise TestFailed, "empty list.pop() didn't raise IndexError" -try: [1].pop(5) -except IndexError: pass -else: raise TestFailed, "[1].pop(5) didn't raise IndexError" -try: [][0:1] = 5 -except TypeError: pass -else: raise TestFailed, "bad list slice assignment didn't raise TypeError" -try: [].extend(None) -except TypeError: pass -else: raise TestFailed, "list.extend(None) didn't raise TypeError" -a = [1, 2, 3, 4] -a *= 0 -if a != []: - raise TestFailed, "list inplace repeat" - -a = [] -a[:] = tuple(range(10)) -if a != range(10): - raise TestFailed, "assigning tuple to slice" - -print '6.5.3a Additional list operations' -a = [0,1,2,3,4] -a[0L] = 1 -a[1L] = 2 -a[2L] = 3 -if a != [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]' -a[0] = 5 -a[1] = 6 -a[2] = 7 -if a != [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]' -a[-2L] = 88 -a[-1L] = 99 -if a != [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]' -a[-2] = 8 -a[-1] = 9 -if a != [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]' -a[:2] = [0,4] -a[-3:] = [] -a[1:1] = [1,2,3] -if a != [0,1,2,3,4]: raise TestFailed, 'list slice assignment' -a[ 1L : 4L] = [7,8,9] -if a != [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints' -del a[1:4] -if a != [0,4]: raise TestFailed, 'list slice deletion' -del a[0] -if a != [4]: raise TestFailed, 'list item deletion [0]' -del a[-1] -if a != []: raise TestFailed, 'list item deletion [-1]' -a=range(0,5) -del a[1L:4L] -if a != [0,4]: raise TestFailed, 'list slice deletion' -del a[0L] -if a != [4]: raise TestFailed, 'list item deletion [0]' -del a[-1L] -if a != []: raise TestFailed, 'list item deletion [-1]' -a.append(0) -a.append(1) -a.append(2) -if a != [0,1,2]: raise TestFailed, 'list append' -a.insert(0, -2) -a.insert(1, -1) -a.insert(2,0) -if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert' -b = a[:] -b.insert(-2, "foo") -b.insert(-200, "left") -b.insert(200, "right") -if b != ["left",-2,-1,0,0,"foo",1,2,"right"]: raise TestFailed, 'list insert2' -# a = [-2,-1,0,0,1,2] -if a.count(0) != 2: raise TestFailed, ' list count' -if a.index(0) != 2: raise TestFailed, 'list index' -if a.index(0,2) != 2: raise TestFailed, 'list index, start argument' -if a.index(0,-4) != 2: raise TestFailed, 'list index, -start argument' -if a.index(-2,-10) != 0: raise TestFailed, 'list index, very -start argument' -if a.index(0,3) != 3: raise TestFailed, 'list index, start argument' -if a.index(0,-3) != 3: raise TestFailed, 'list index, -start argument' -if a.index(0,3,4) != 3: raise TestFailed, 'list index, stop argument' -if a.index(0,-3,-2) != 3: raise TestFailed, 'list index, -stop argument' -if a.index(0,-4*sys.maxint,4*sys.maxint) != 2: - raise TestFailed, 'list index, -maxint, maxint argument' -try: - a.index(0, 4*sys.maxint,-4*sys.maxint) -except ValueError: - pass -else: - raise TestFailed, 'list index, maxint,-maxint argument' - -try: - a.index(2,0,-10) -except ValueError: - pass -else: - raise TestFailed, 'list index, very -stop argument' -a.remove(0) -try: - a.index(2,0,4) -except ValueError: - pass -else: - raise TestFailed, 'list index, stop argument.' -if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove' -a.reverse() -if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse' -a.sort() -if a != [-2,-1,0,1,2]: raise TestFailed, 'list sort' -def revcmp(a, b): return cmp(b, a) -a.sort(revcmp) -if a != [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func' -# The following dumps core in unpatched Python 1.5: -def myComparison(x,y): - return cmp(x%3, y%7) -z = range(12) -z.sort(myComparison) - -try: z.sort(2) -except TypeError: pass -else: raise TestFailed, 'list sort compare function is not callable' - -def selfmodifyingComparison(x,y): - z.append(1) - return cmp(x, y) -try: z.sort(selfmodifyingComparison) -except ValueError: pass -else: raise TestFailed, 'modifying list during sort' - -try: z.sort(lambda x, y: 's') -except TypeError: pass -else: raise TestFailed, 'list sort compare function does not return int' - -# Test extreme cases with long ints -a = [0,1,2,3,4] -if a[ -pow(2,128L): 3 ] != [0,1,2]: - raise TestFailed, "list slicing with too-small long integer" -if a[ 3: pow(2,145L) ] != [3,4]: - raise TestFailed, "list slicing with too-large long integer" - - -# extended slicing - -# subscript -a = [0,1,2,3,4] -vereq(a[::], a) -vereq(a[::2], [0,2,4]) -vereq(a[1::2], [1,3]) -vereq(a[::-1], [4,3,2,1,0]) -vereq(a[::-2], [4,2,0]) -vereq(a[3::-2], [3,1]) -vereq(a[-100:100:], a) -vereq(a[100:-100:-1], a[::-1]) -vereq(a[-100L:100L:2L], [0,2,4]) -vereq(a[1000:2000:2], []) -vereq(a[-1000:-2000:-2], []) -# deletion -del a[::2] -vereq(a, [1,3]) -a = range(5) -del a[1::2] -vereq(a, [0,2,4]) -a = range(5) -del a[1::-2] -vereq(a, [0,2,3,4]) -a = range(10) -del a[::1000] -vereq(a, [1, 2, 3, 4, 5, 6, 7, 8, 9]) -# assignment -a = range(10) -a[::2] = [-1]*5 -vereq(a, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]) -a = range(10) -a[::-4] = [10]*3 -vereq(a, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]) -a = range(4) -a[::-1] = a -vereq(a, [3, 2, 1, 0]) -a = range(10) -b = a[:] -c = a[:] -a[2:3] = ["two", "elements"] -b[slice(2,3)] = ["two", "elements"] -c[2:3:] = ["two", "elements"] -vereq(a, b) -vereq(a, c) -a = range(10) -a[::2] = tuple(range(5)) -vereq(a, [0, 1, 1, 3, 2, 5, 3, 7, 4, 9]) - -# Verify that __getitem__ overrides are not recognized by __iter__ -class L(list): - def __getitem__(self, key): - return str(key) + '!!!' -vereq(iter(L([1,2])).next(), 1) +print '6.5.3 Lists [see test_list.py]' print '6.6 Mappings == Dictionaries' 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() |