Add NEWS entry as per RDM's suggestion (the bug was actually present
[python.git] / Lib / test / test_slice.py
blob08de8e3ad708c83ccb6785e9ed7f74ea46ee9729
1 # tests for slice objects; in particular the indices method.
3 import unittest
4 from test import test_support
5 from cPickle import loads, dumps
7 import sys
9 class SliceTest(unittest.TestCase):
11 def test_constructor(self):
12 self.assertRaises(TypeError, slice)
13 self.assertRaises(TypeError, slice, 1, 2, 3, 4)
15 def test_repr(self):
16 self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
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__)
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)
30 class Exc(Exception):
31 pass
33 class BadCmp(object):
34 def __eq__(self, other):
35 raise Exc
36 __hash__ = None # Silence Py3k warning
38 s1 = slice(BadCmp())
39 s2 = slice(BadCmp())
40 self.assertRaises(Exc, cmp, s1, s2)
41 self.assertEqual(s1, s1)
43 s1 = slice(1, BadCmp())
44 s2 = slice(1, BadCmp())
45 self.assertEqual(s1, s1)
46 self.assertRaises(Exc, cmp, s1, s2)
48 s1 = slice(1, 2, BadCmp())
49 s2 = slice(1, 2, BadCmp())
50 self.assertEqual(s1, s1)
51 self.assertRaises(Exc, cmp, s1, s2)
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)
59 s = slice(1, 2)
60 self.assertEqual(s.start, 1)
61 self.assertEqual(s.stop, 2)
62 self.assertEqual(s.step, None)
64 s = slice(1, 2, 3)
65 self.assertEqual(s.start, 1)
66 self.assertEqual(s.stop, 2)
67 self.assertEqual(s.step, 3)
69 class AnyClass:
70 pass
72 obj = AnyClass()
73 s = slice(obj)
74 self.assertTrue(s.stop is obj)
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))
97 self.assertEqual(
98 slice(-100, 100 ).indices(10),
99 slice(None).indices(10)
101 self.assertEqual(
102 slice(100, -100, -1).indices(10),
103 slice(None, None, -1).indices(10)
105 self.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10, 2))
107 self.assertEqual(range(10)[::sys.maxint - 1], [0])
109 self.assertRaises(OverflowError, slice(None).indices, 1L<<100)
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))
117 x = X()
118 x[1:2] = 42
119 self.assertEquals(tmp, [(1, 2, 42)])
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))
129 def test_main():
130 test_support.run_unittest(SliceTest)
132 if __name__ == "__main__":
133 test_main()