diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-01-09 18:50:50 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-01-09 18:50:50 (GMT) |
commit | e36561352895170f28f77f0b4ec4292e35d1cc01 (patch) | |
tree | b8d9368bba50b471a0502e3c871090cbfc3d4f72 /Lib | |
parent | dd6d92a560463c0d5730a4a08bf2a6d7f5d45f03 (diff) | |
download | cpython-e36561352895170f28f77f0b4ec4292e35d1cc01.zip cpython-e36561352895170f28f77f0b4ec4292e35d1cc01.tar.gz cpython-e36561352895170f28f77f0b4ec4292e35d1cc01.tar.bz2 |
Issue #7532: Add additional slicing test cases for new- and old-style
classes. Patch by Florent Xicluna.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_index.py | 99 |
1 files changed, 97 insertions, 2 deletions
diff --git a/Lib/test/test_index.py b/Lib/test/test_index.py index 2e997c7..1a7bd01 100644 --- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -89,6 +89,24 @@ 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) + if isinstance(self.seq, ClassicSeq): + return + # These tests fail for ClassicSeq (see bug #7532) + 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 @@ -156,6 +174,40 @@ class ListTestCase(SeqTestCase): self.assertEqual(lst, [5, 6, 7, 8, 9, 11] * 3) +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 + + class TupleTestCase(SeqTestCase): seq = (0,10,20,30,40,50) @@ -165,6 +217,18 @@ class StringTestCase(SeqTestCase): class UnicodeTestCase(SeqTestCase): seq = u"this is a test" +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)) + class XRangeTestCase(unittest.TestCase): @@ -187,6 +251,20 @@ class OverflowTestCase(unittest.TestCase): def _getitem_helper(self, base): 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): + class GetItem(base): + def __len__(self): return maxint #cannot return long here def __getitem__(self, key): return key @@ -196,14 +274,22 @@ class OverflowTestCase(unittest.TestCase): self.assertEqual(x[self.pos], self.pos) self.assertEqual(x[self.neg], self.neg) self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize)) - self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), (0, maxsize, 1)) + self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), + (0, maxsize, 1)) def test_getitem(self): self._getitem_helper(object) + # Silence Py3k warning + with test_support.check_warnings(): + self._getslice_helper_deprecated(object) def test_getitem_classic(self): class Empty: pass - self._getitem_helper(Empty) + # XXX This test fails (see bug #7532) + #self._getitem_helper(Empty) + # Silence Py3k warning + with test_support.check_warnings(): + self._getslice_helper_deprecated(Empty) def test_sequence_repeat(self): self.assertRaises(OverflowError, lambda: "a" * self.pos) @@ -217,9 +303,18 @@ def test_main(): TupleTestCase, StringTestCase, UnicodeTestCase, + ClassicSeqTestCase, + NewSeqTestCase, XRangeTestCase, OverflowTestCase, ) + # Silence Py3k warning + with test_support.check_warnings(): + test_support.run_unittest( + ClassicSeqDeprecatedTestCase, + NewSeqDeprecatedTestCase, + ) + if __name__ == "__main__": test_main() |