2006-03-30 07:28:43 -04:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
import operator
|
2006-08-12 14:03:09 -03:00
|
|
|
from sys import maxint
|
2007-05-03 17:09:56 -03:00
|
|
|
maxsize = test_support.MAX_Py_ssize_t
|
|
|
|
minsize = -maxsize-1
|
2006-03-30 07:28:43 -04:00
|
|
|
|
|
|
|
class oldstyle:
|
|
|
|
def __index__(self):
|
|
|
|
return self.ind
|
|
|
|
|
|
|
|
class newstyle(object):
|
|
|
|
def __index__(self):
|
|
|
|
return self.ind
|
|
|
|
|
2006-08-12 14:03:09 -03:00
|
|
|
class TrapInt(int):
|
|
|
|
def __index__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
class TrapLong(long):
|
|
|
|
def __index__(self):
|
|
|
|
return self
|
|
|
|
|
2006-03-30 10:04:02 -04:00
|
|
|
class BaseTestCase(unittest.TestCase):
|
2006-03-30 07:28:43 -04:00
|
|
|
def setUp(self):
|
|
|
|
self.o = oldstyle()
|
|
|
|
self.n = newstyle()
|
2006-03-30 08:59:11 -04:00
|
|
|
|
2006-03-30 07:28:43 -04:00
|
|
|
def test_basic(self):
|
|
|
|
self.o.ind = -2
|
|
|
|
self.n.ind = 2
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(operator.index(self.o), -2)
|
|
|
|
self.assertEqual(operator.index(self.n), 2)
|
2006-08-14 21:25:04 -03:00
|
|
|
|
2006-08-12 14:03:09 -03:00
|
|
|
def test_slice(self):
|
|
|
|
self.o.ind = 1
|
|
|
|
self.n.ind = 2
|
|
|
|
slc = slice(self.o, self.o, self.o)
|
|
|
|
check_slc = slice(1, 1, 1)
|
|
|
|
self.assertEqual(slc.indices(self.o), check_slc.indices(1))
|
|
|
|
slc = slice(self.n, self.n, self.n)
|
|
|
|
check_slc = slice(2, 2, 2)
|
|
|
|
self.assertEqual(slc.indices(self.n), check_slc.indices(2))
|
2006-03-30 08:59:11 -04:00
|
|
|
|
2006-08-12 14:03:09 -03:00
|
|
|
def test_wrappers(self):
|
|
|
|
self.o.ind = 4
|
|
|
|
self.n.ind = 5
|
|
|
|
self.assertEqual(6 .__index__(), 6)
|
|
|
|
self.assertEqual(-7L.__index__(), -7)
|
|
|
|
self.assertEqual(self.o.__index__(), 4)
|
|
|
|
self.assertEqual(self.n.__index__(), 5)
|
2010-03-30 15:49:45 -03:00
|
|
|
self.assertEqual(True.__index__(), 1)
|
|
|
|
self.assertEqual(False.__index__(), 0)
|
2006-08-12 14:03:09 -03:00
|
|
|
|
2006-08-15 03:29:03 -03:00
|
|
|
def test_subclasses(self):
|
|
|
|
r = range(10)
|
|
|
|
self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
|
|
|
|
self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
|
|
|
|
self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
|
|
|
|
self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
|
2006-08-14 21:25:04 -03:00
|
|
|
|
2006-03-30 07:28:43 -04:00
|
|
|
def test_error(self):
|
|
|
|
self.o.ind = 'dumb'
|
|
|
|
self.n.ind = 'bad'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(TypeError, operator.index, self.o)
|
|
|
|
self.assertRaises(TypeError, operator.index, self.n)
|
|
|
|
self.assertRaises(TypeError, slice(self.o).indices, 0)
|
|
|
|
self.assertRaises(TypeError, slice(self.n).indices, 0)
|
2006-08-12 14:03:09 -03:00
|
|
|
|
|
|
|
|
|
|
|
class SeqTestCase(unittest.TestCase):
|
|
|
|
# This test case isn't run directly. It just defines common tests
|
|
|
|
# to the different sequence types below
|
|
|
|
def setUp(self):
|
|
|
|
self.o = oldstyle()
|
|
|
|
self.n = newstyle()
|
|
|
|
self.o2 = oldstyle()
|
|
|
|
self.n2 = newstyle()
|
|
|
|
|
|
|
|
def test_index(self):
|
|
|
|
self.o.ind = -2
|
|
|
|
self.n.ind = 2
|
|
|
|
self.assertEqual(self.seq[self.n], self.seq[2])
|
|
|
|
self.assertEqual(self.seq[self.o], self.seq[-2])
|
2006-03-30 07:28:43 -04:00
|
|
|
|
|
|
|
def test_slice(self):
|
|
|
|
self.o.ind = 1
|
|
|
|
self.o2.ind = 3
|
|
|
|
self.n.ind = 2
|
|
|
|
self.n2.ind = 4
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
|
|
|
|
self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
|
2006-03-30 07:28:43 -04:00
|
|
|
|
2013-12-10 16:09:20 -04:00
|
|
|
def test_slice_bug7532a(self):
|
2010-01-09 14:50:50 -04:00
|
|
|
seqlen = len(self.seq)
|
|
|
|
self.o.ind = int(seqlen * 1.5)
|
|
|
|
self.n.ind = seqlen + 2
|
|
|
|
self.assertEqual(self.seq[self.o:], self.seq[0:0])
|
|
|
|
self.assertEqual(self.seq[:self.o], self.seq)
|
|
|
|
self.assertEqual(self.seq[self.n:], self.seq[0:0])
|
|
|
|
self.assertEqual(self.seq[:self.n], self.seq)
|
2013-12-10 16:09:20 -04:00
|
|
|
|
|
|
|
def test_slice_bug7532b(self):
|
2010-01-09 14:50:50 -04:00
|
|
|
if isinstance(self.seq, ClassicSeq):
|
2013-12-10 16:09:20 -04:00
|
|
|
self.skipTest('test fails for ClassicSeq')
|
2010-01-09 14:50:50 -04:00
|
|
|
# These tests fail for ClassicSeq (see bug #7532)
|
2013-12-10 16:09:20 -04:00
|
|
|
seqlen = len(self.seq)
|
2010-01-09 14:50:50 -04:00
|
|
|
self.o2.ind = -seqlen - 2
|
|
|
|
self.n2.ind = -int(seqlen * 1.5)
|
|
|
|
self.assertEqual(self.seq[self.o2:], self.seq)
|
|
|
|
self.assertEqual(self.seq[:self.o2], self.seq[0:0])
|
|
|
|
self.assertEqual(self.seq[self.n2:], self.seq)
|
|
|
|
self.assertEqual(self.seq[:self.n2], self.seq[0:0])
|
|
|
|
|
2006-03-30 10:04:02 -04:00
|
|
|
def test_repeat(self):
|
|
|
|
self.o.ind = 3
|
|
|
|
self.n.ind = 2
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(self.seq * self.o, self.seq * 3)
|
|
|
|
self.assertEqual(self.seq * self.n, self.seq * 2)
|
|
|
|
self.assertEqual(self.o * self.seq, self.seq * 3)
|
|
|
|
self.assertEqual(self.n * self.seq, self.seq * 2)
|
2006-03-30 10:04:02 -04:00
|
|
|
|
|
|
|
def test_wrappers(self):
|
2006-08-12 14:03:09 -03:00
|
|
|
self.o.ind = 4
|
|
|
|
self.n.ind = 5
|
|
|
|
self.assertEqual(self.seq.__getitem__(self.o), self.seq[4])
|
|
|
|
self.assertEqual(self.seq.__mul__(self.o), self.seq * 4)
|
|
|
|
self.assertEqual(self.seq.__rmul__(self.o), self.seq * 4)
|
|
|
|
self.assertEqual(self.seq.__getitem__(self.n), self.seq[5])
|
|
|
|
self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
|
|
|
|
self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
|
|
|
|
|
2006-08-15 03:29:03 -03:00
|
|
|
def test_subclasses(self):
|
|
|
|
self.assertEqual(self.seq[TrapInt()], self.seq[0])
|
|
|
|
self.assertEqual(self.seq[TrapLong()], self.seq[0])
|
2006-08-12 14:03:09 -03:00
|
|
|
|
|
|
|
def test_error(self):
|
|
|
|
self.o.ind = 'dumb'
|
|
|
|
self.n.ind = 'bad'
|
|
|
|
indexobj = lambda x, obj: obj.seq[x]
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(TypeError, indexobj, self.o, self)
|
|
|
|
self.assertRaises(TypeError, indexobj, self.n, self)
|
2006-08-12 14:03:09 -03:00
|
|
|
sliceobj = lambda x, obj: obj.seq[x:]
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(TypeError, sliceobj, self.o, self)
|
|
|
|
self.assertRaises(TypeError, sliceobj, self.n, self)
|
2006-08-12 14:03:09 -03:00
|
|
|
|
|
|
|
|
|
|
|
class ListTestCase(SeqTestCase):
|
2006-03-30 10:04:02 -04:00
|
|
|
seq = [0,10,20,30,40,50]
|
|
|
|
|
|
|
|
def test_setdelitem(self):
|
2006-03-30 07:28:43 -04:00
|
|
|
self.o.ind = -2
|
|
|
|
self.n.ind = 2
|
2006-03-30 10:04:02 -04:00
|
|
|
lst = list('ab!cdefghi!j')
|
|
|
|
del lst[self.o]
|
|
|
|
del lst[self.n]
|
|
|
|
lst[self.o] = 'X'
|
|
|
|
lst[self.n] = 'Y'
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(lst, list('abYdefghXj'))
|
2006-03-30 08:59:11 -04:00
|
|
|
|
2006-03-30 10:04:02 -04:00
|
|
|
lst = [5, 6, 7, 8, 9, 10, 11]
|
|
|
|
lst.__setitem__(self.n, "here")
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(lst, [5, 6, "here", 8, 9, 10, 11])
|
2006-03-30 10:04:02 -04:00
|
|
|
lst.__delitem__(self.n)
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(lst, [5, 6, 8, 9, 10, 11])
|
2006-03-30 07:28:43 -04:00
|
|
|
|
2006-03-30 10:04:02 -04:00
|
|
|
def test_inplace_repeat(self):
|
|
|
|
self.o.ind = 2
|
|
|
|
self.n.ind = 3
|
|
|
|
lst = [6, 4]
|
|
|
|
lst *= self.o
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(lst, [6, 4, 6, 4])
|
2006-03-30 10:04:02 -04:00
|
|
|
lst *= self.n
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(lst, [6, 4, 6, 4] * 3)
|
2006-03-30 07:28:43 -04:00
|
|
|
|
2006-03-30 10:04:02 -04:00
|
|
|
lst = [5, 6, 7, 8, 9, 11]
|
|
|
|
l2 = lst.__imul__(self.n)
|
2010-03-19 15:34:55 -03:00
|
|
|
self.assertIs(l2, lst)
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
|
2006-03-30 08:59:11 -04:00
|
|
|
|
|
|
|
|
2010-01-09 14:50:50 -04:00
|
|
|
class _BaseSeq:
|
|
|
|
|
|
|
|
def __init__(self, iterable):
|
|
|
|
self._list = list(iterable)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self._list)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self._list == other
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._list)
|
|
|
|
|
|
|
|
def __mul__(self, n):
|
|
|
|
return self.__class__(self._list*n)
|
|
|
|
__rmul__ = __mul__
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
return self._list[index]
|
|
|
|
|
|
|
|
|
|
|
|
class _GetSliceMixin:
|
|
|
|
|
|
|
|
def __getslice__(self, i, j):
|
|
|
|
return self._list.__getslice__(i, j)
|
|
|
|
|
|
|
|
|
|
|
|
class ClassicSeq(_BaseSeq): pass
|
|
|
|
class NewSeq(_BaseSeq, object): pass
|
|
|
|
class ClassicSeqDeprecated(_GetSliceMixin, ClassicSeq): pass
|
|
|
|
class NewSeqDeprecated(_GetSliceMixin, NewSeq): pass
|
|
|
|
|
|
|
|
|
2006-08-12 14:03:09 -03:00
|
|
|
class TupleTestCase(SeqTestCase):
|
2006-03-30 10:04:02 -04:00
|
|
|
seq = (0,10,20,30,40,50)
|
2006-03-30 08:59:11 -04:00
|
|
|
|
2006-08-12 14:03:09 -03:00
|
|
|
class StringTestCase(SeqTestCase):
|
2006-03-30 10:04:02 -04:00
|
|
|
seq = "this is a test"
|
2006-03-30 07:28:43 -04:00
|
|
|
|
2010-03-19 15:34:55 -03:00
|
|
|
class ByteArrayTestCase(SeqTestCase):
|
|
|
|
seq = bytearray("this is a test")
|
|
|
|
|
2006-08-12 14:03:09 -03:00
|
|
|
class UnicodeTestCase(SeqTestCase):
|
2006-03-30 10:04:02 -04:00
|
|
|
seq = u"this is a test"
|
2006-03-30 08:59:11 -04:00
|
|
|
|
2010-01-09 14:50:50 -04:00
|
|
|
class ClassicSeqTestCase(SeqTestCase):
|
|
|
|
seq = ClassicSeq((0,10,20,30,40,50))
|
|
|
|
|
|
|
|
class NewSeqTestCase(SeqTestCase):
|
|
|
|
seq = NewSeq((0,10,20,30,40,50))
|
|
|
|
|
|
|
|
class ClassicSeqDeprecatedTestCase(SeqTestCase):
|
|
|
|
seq = ClassicSeqDeprecated((0,10,20,30,40,50))
|
|
|
|
|
|
|
|
class NewSeqDeprecatedTestCase(SeqTestCase):
|
|
|
|
seq = NewSeqDeprecated((0,10,20,30,40,50))
|
|
|
|
|
2006-03-30 08:59:11 -04:00
|
|
|
|
2006-03-30 10:04:02 -04:00
|
|
|
class XRangeTestCase(unittest.TestCase):
|
2006-03-30 07:28:43 -04:00
|
|
|
|
2006-03-30 10:04:02 -04:00
|
|
|
def test_xrange(self):
|
|
|
|
n = newstyle()
|
|
|
|
n.ind = 5
|
2006-08-12 14:03:09 -03:00
|
|
|
self.assertEqual(xrange(1, 20)[n], 6)
|
|
|
|
self.assertEqual(xrange(1, 20).__getitem__(n), 6)
|
|
|
|
|
|
|
|
class OverflowTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.pos = 2**100
|
|
|
|
self.neg = -self.pos
|
|
|
|
|
|
|
|
def test_large_longs(self):
|
|
|
|
self.assertEqual(self.pos.__index__(), self.pos)
|
|
|
|
self.assertEqual(self.neg.__index__(), self.neg)
|
|
|
|
|
2006-08-12 15:44:06 -03:00
|
|
|
def _getitem_helper(self, base):
|
2010-01-09 14:50:50 -04:00
|
|
|
class GetItem(base):
|
|
|
|
def __len__(self):
|
|
|
|
return maxint # cannot return long here
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return key
|
|
|
|
x = GetItem()
|
|
|
|
self.assertEqual(x[self.pos], self.pos)
|
|
|
|
self.assertEqual(x[self.neg], self.neg)
|
|
|
|
self.assertEqual(x[self.neg:self.pos].indices(maxsize),
|
|
|
|
(0, maxsize, 1))
|
|
|
|
self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
|
|
|
|
(0, maxsize, 1))
|
|
|
|
|
|
|
|
def _getslice_helper_deprecated(self, base):
|
2006-08-12 15:44:06 -03:00
|
|
|
class GetItem(base):
|
2006-08-12 14:03:09 -03:00
|
|
|
def __len__(self):
|
2010-03-19 15:34:55 -03:00
|
|
|
return maxint # cannot return long here
|
2006-08-12 14:03:09 -03:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return key
|
|
|
|
def __getslice__(self, i, j):
|
|
|
|
return i, j
|
|
|
|
x = GetItem()
|
|
|
|
self.assertEqual(x[self.pos], self.pos)
|
|
|
|
self.assertEqual(x[self.neg], self.neg)
|
2007-05-03 17:09:56 -03:00
|
|
|
self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize))
|
2010-01-09 14:50:50 -04:00
|
|
|
self.assertEqual(x[self.neg:self.pos:1].indices(maxsize),
|
|
|
|
(0, maxsize, 1))
|
2006-08-12 14:03:09 -03:00
|
|
|
|
2006-08-12 15:44:06 -03:00
|
|
|
def test_getitem(self):
|
|
|
|
self._getitem_helper(object)
|
2010-03-07 08:18:33 -04:00
|
|
|
with test_support.check_py3k_warnings():
|
2010-01-09 14:50:50 -04:00
|
|
|
self._getslice_helper_deprecated(object)
|
2006-08-12 15:44:06 -03:00
|
|
|
|
|
|
|
def test_getitem_classic(self):
|
|
|
|
class Empty: pass
|
2010-01-09 14:50:50 -04:00
|
|
|
# XXX This test fails (see bug #7532)
|
|
|
|
#self._getitem_helper(Empty)
|
2010-03-07 08:18:33 -04:00
|
|
|
with test_support.check_py3k_warnings():
|
2010-01-09 14:50:50 -04:00
|
|
|
self._getslice_helper_deprecated(Empty)
|
2006-08-12 15:44:06 -03:00
|
|
|
|
2006-08-12 14:03:09 -03:00
|
|
|
def test_sequence_repeat(self):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(OverflowError, lambda: "a" * self.pos)
|
|
|
|
self.assertRaises(OverflowError, lambda: "a" * self.neg)
|
2006-03-30 07:28:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(
|
2006-08-12 14:03:09 -03:00
|
|
|
BaseTestCase,
|
2006-03-30 07:28:43 -04:00
|
|
|
ListTestCase,
|
|
|
|
TupleTestCase,
|
2010-03-19 15:34:55 -03:00
|
|
|
ByteArrayTestCase,
|
2006-03-30 07:28:43 -04:00
|
|
|
StringTestCase,
|
2006-03-30 10:04:02 -04:00
|
|
|
UnicodeTestCase,
|
2010-01-09 14:50:50 -04:00
|
|
|
ClassicSeqTestCase,
|
|
|
|
NewSeqTestCase,
|
2006-03-30 10:04:02 -04:00
|
|
|
XRangeTestCase,
|
2006-08-12 14:03:09 -03:00
|
|
|
OverflowTestCase,
|
2006-03-30 07:28:43 -04:00
|
|
|
)
|
2010-03-07 08:18:33 -04:00
|
|
|
with test_support.check_py3k_warnings():
|
2010-01-09 14:50:50 -04:00
|
|
|
test_support.run_unittest(
|
|
|
|
ClassicSeqDeprecatedTestCase,
|
|
|
|
NewSeqDeprecatedTestCase,
|
|
|
|
)
|
|
|
|
|
2006-03-30 07:28:43 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|