Commit c32dda59 authored by Mark Dickinson's avatar Mark Dickinson

Issue #7532: Expanded tests for extended slicing. This is a forward

port of r77391 to py3k, along with some additional tests and cleanup.
Patch by Florent Xicluna.
parent 5e15868f
......@@ -2,13 +2,8 @@ import unittest
from test import support
import operator
maxsize = support.MAX_Py_ssize_t
minsize = -maxsize-1
class oldstyle:
def __index__(self):
return self.ind
class newstyle(object):
class newstyle:
def __index__(self):
return self.ind
......@@ -16,13 +11,9 @@ class TrapInt(int):
def __index__(self):
return self
class TrapLong(int):
def __index__(self):
return self
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.o = oldstyle()
self.o = newstyle()
self.n = newstyle()
def test_basic(self):
......@@ -52,9 +43,7 @@ class BaseTestCase(unittest.TestCase):
def test_subclasses(self):
r = list(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))
def test_error(self):
self.o.ind = 'dumb'
......@@ -69,9 +58,9 @@ 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.o = newstyle()
self.n = newstyle()
self.o2 = oldstyle()
self.o2 = newstyle()
self.n2 = newstyle()
def test_index(self):
......@@ -88,6 +77,21 @@ class SeqTestCase(unittest.TestCase):
self.assertEqual(self.seq[self.o:self.o2], self.seq[1:3])
self.assertEqual(self.seq[self.n:self.n2], self.seq[2:4])
def test_slice_bug7532(self):
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)
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])
def test_repeat(self):
self.o.ind = 3
self.n.ind = 2
......@@ -108,7 +112,6 @@ class SeqTestCase(unittest.TestCase):
def test_subclasses(self):
self.assertEqual(self.seq[TrapInt()], self.seq[0])
self.assertEqual(self.seq[TrapLong()], self.seq[0])
def test_error(self):
self.o.ind = 'dumb'
......@@ -155,14 +158,52 @@ class ListTestCase(SeqTestCase):
self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3)
class NewSeq:
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 TupleTestCase(SeqTestCase):
seq = (0,10,20,30,40,50)
class ByteArrayTestCase(SeqTestCase):
seq = bytearray(b"this is a test")
class BytesTestCase(SeqTestCase):
seq = b"this is a test"
class StringTestCase(SeqTestCase):
seq = "this is a test"
class UnicodeTestCase(SeqTestCase):
seq = "this is a test"
class NewSeqTestCase(SeqTestCase):
seq = NewSeq((0,10,20,30,40,50))
class RangeTestCase(unittest.TestCase):
def test_range(self):
n = newstyle()
n.ind = 5
self.assertEqual(range(1, 20)[n], 6)
self.assertEqual(range(1, 20).__getitem__(n), 6)
class OverflowTestCase(unittest.TestCase):
......@@ -176,9 +217,9 @@ class OverflowTestCase(unittest.TestCase):
self.assertEqual(self.neg.__index__(), self.neg)
def test_getitem(self):
class GetItem(object):
class GetItem:
def __len__(self):
return sys.maxsize
assert False, "__len__ should not be invoked"
def __getitem__(self, key):
return key
x = GetItem()
......@@ -186,6 +227,8 @@ class OverflowTestCase(unittest.TestCase):
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 test_sequence_repeat(self):
self.assertRaises(OverflowError, lambda: "a" * self.pos)
......@@ -197,8 +240,11 @@ def test_main():
BaseTestCase,
ListTestCase,
TupleTestCase,
BytesTestCase,
ByteArrayTestCase,
StringTestCase,
UnicodeTestCase,
NewSeqTestCase,
RangeTestCase,
OverflowTestCase,
)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment