1 # tests for slice objects; in particular the indices method.
4 from test
import test_support
5 from cPickle
import loads
, dumps
9 class SliceTest(unittest
.TestCase
):
11 def test_constructor(self
):
12 self
.assertRaises(TypeError, slice)
13 self
.assertRaises(TypeError, slice, 1, 2, 3, 4)
16 self
.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
19 # Verify clearing of SF bug #800796
20 self
.assertRaises(TypeError, hash, slice(5))
21 self
.assertRaises(TypeError, slice(5).__hash
__)
27 self
.assertEqual(s1
, s2
)
28 self
.assertNotEqual(s1
, s3
)
34 def __eq__(self
, other
):
36 __hash__
= None # Silence Py3k warning
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
):
55 self
.assertEqual(s
.start
, None)
56 self
.assertEqual(s
.stop
, 1)
57 self
.assertEqual(s
.step
, None)
60 self
.assertEqual(s
.start
, 1)
61 self
.assertEqual(s
.stop
, 2)
62 self
.assertEqual(s
.step
, None)
65 self
.assertEqual(s
.start
, 1)
66 self
.assertEqual(s
.stop
, 2)
67 self
.assertEqual(s
.step
, 3)
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))
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))
98 slice(-100, 100 ).indices(10),
99 slice(None).indices(10)
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
):
114 def __setslice__(self
, i
, j
, k
):
115 tmp
.append((i
, j
, k
))
118 with test_support
.check_py3k_warnings():
120 self
.assertEquals(tmp
, [(1, 2, 42)])
122 def test_pickle(self
):
124 for protocol
in (0,1,2):
125 t
= loads(dumps(s
, protocol
))
126 self
.assertEqual(s
, t
)
127 self
.assertEqual(s
.indices(15), t
.indices(15))
128 self
.assertNotEqual(id(s
), id(t
))
131 test_support
.run_unittest(SliceTest
)
133 if __name__
== "__main__":