PageRenderTime 53ms CodeModel.GetById 13ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_slice.py

http://unladen-swallow.googlecode.com/
Python | 133 lines | 104 code | 26 blank | 3 comment | 3 complexity | 4f42b50779b91efd42490efdf9aec674 MD5 | raw file
  1# tests for slice objects; in particular the indices method.
  2
  3import unittest
  4from test import test_support
  5from cPickle import loads, dumps
  6
  7import sys
  8
  9class SliceTest(unittest.TestCase):
 10
 11    def test_constructor(self):
 12        self.assertRaises(TypeError, slice)
 13        self.assertRaises(TypeError, slice, 1, 2, 3, 4)
 14
 15    def test_repr(self):
 16        self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
 17
 18    def test_hash(self):
 19        # Verify clearing of SF bug #800796
 20        self.assertRaises(TypeError, hash, slice(5))
 21        self.assertRaises(TypeError, slice(5).__hash__)
 22
 23    def test_cmp(self):
 24        s1 = slice(1, 2, 3)
 25        s2 = slice(1, 2, 3)
 26        s3 = slice(1, 2, 4)
 27        self.assertEqual(s1, s2)
 28        self.assertNotEqual(s1, s3)
 29
 30        class Exc(Exception):
 31            pass
 32
 33        class BadCmp(object):
 34            def __eq__(self, other):
 35                raise Exc
 36            __hash__ = None # Silence Py3k warning
 37
 38        s1 = slice(BadCmp())
 39        s2 = slice(BadCmp())
 40        self.assertRaises(Exc, cmp, s1, s2)
 41        self.assertEqual(s1, s1)
 42
 43        s1 = slice(1, BadCmp())
 44        s2 = slice(1, BadCmp())
 45        self.assertEqual(s1, s1)
 46        self.assertRaises(Exc, cmp, s1, s2)
 47
 48        s1 = slice(1, 2, BadCmp())
 49        s2 = slice(1, 2, BadCmp())
 50        self.assertEqual(s1, s1)
 51        self.assertRaises(Exc, cmp, s1, s2)
 52
 53    def test_members(self):
 54        s = slice(1)
 55        self.assertEqual(s.start, None)
 56        self.assertEqual(s.stop, 1)
 57        self.assertEqual(s.step, None)
 58
 59        s = slice(1, 2)
 60        self.assertEqual(s.start, 1)
 61        self.assertEqual(s.stop, 2)
 62        self.assertEqual(s.step, None)
 63
 64        s = slice(1, 2, 3)
 65        self.assertEqual(s.start, 1)
 66        self.assertEqual(s.stop, 2)
 67        self.assertEqual(s.step, 3)
 68
 69        class AnyClass:
 70            pass
 71
 72        obj = AnyClass()
 73        s = slice(obj)
 74        self.assert_(s.stop is obj)
 75
 76    def test_indices(self):
 77        self.assertEqual(slice(None           ).indices(10), (0, 10,  1))
 78        self.assertEqual(slice(None,  None,  2).indices(10), (0, 10,  2))
 79        self.assertEqual(slice(1,     None,  2).indices(10), (1, 10,  2))
 80        self.assertEqual(slice(None,  None, -1).indices(10), (9, -1, -1))
 81        self.assertEqual(slice(None,  None, -2).indices(10), (9, -1, -2))
 82        self.assertEqual(slice(3,     None, -2).indices(10), (3, -1, -2))
 83        # issue 3004 tests
 84        self.assertEqual(slice(None, -9).indices(10), (0, 1, 1))
 85        self.assertEqual(slice(None, -10).indices(10), (0, 0, 1))
 86        self.assertEqual(slice(None, -11).indices(10), (0, 0, 1))
 87        self.assertEqual(slice(None, -10, -1).indices(10), (9, 0, -1))
 88        self.assertEqual(slice(None, -11, -1).indices(10), (9, -1, -1))
 89        self.assertEqual(slice(None, -12, -1).indices(10), (9, -1, -1))
 90        self.assertEqual(slice(None, 9).indices(10), (0, 9, 1))
 91        self.assertEqual(slice(None, 10).indices(10), (0, 10, 1))
 92        self.assertEqual(slice(None, 11).indices(10), (0, 10, 1))
 93        self.assertEqual(slice(None, 8, -1).indices(10), (9, 8, -1))
 94        self.assertEqual(slice(None, 9, -1).indices(10), (9, 9, -1))
 95        self.assertEqual(slice(None, 10, -1).indices(10), (9, 9, -1))
 96
 97        self.assertEqual(
 98            slice(-100,  100     ).indices(10),
 99            slice(None).indices(10)
100        )
101        self.assertEqual(
102            slice(100,  -100,  -1).indices(10),
103            slice(None, None, -1).indices(10)
104        )
105        self.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10,  2))
106
107        self.assertEqual(range(10)[::sys.maxint - 1], [0])
108
109        self.assertRaises(OverflowError, slice(None).indices, 1L<<100)
110
111    def test_setslice_without_getslice(self):
112        tmp = []
113        class X(object):
114            def __setslice__(self, i, j, k):
115                tmp.append((i, j, k))
116
117        x = X()
118        x[1:2] = 42
119        self.assertEquals(tmp, [(1, 2, 42)])
120
121    def test_pickle(self):
122        s = slice(10, 20, 3)
123        for protocol in (0,1,2):
124            t = loads(dumps(s, protocol))
125            self.assertEqual(s, t)
126            self.assertEqual(s.indices(15), t.indices(15))
127            self.assertNotEqual(id(s), id(t))
128
129def test_main():
130    test_support.run_unittest(SliceTest)
131
132if __name__ == "__main__":
133    test_main()