1 # tests for slice objects; in particular the indices method.
4 from test
import test_support
8 class SliceTest(unittest
.TestCase
):
10 def test_constructor(self
):
11 self
.assertRaises(TypeError, slice)
12 self
.assertRaises(TypeError, slice, 1, 2, 3, 4)
15 self
.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
18 # Verify clearing of SF bug #800796
19 self
.assertRaises(TypeError, hash, slice(5))
20 self
.assertRaises(TypeError, slice(5).__hash
__)
26 self
.assertEqual(s1
, s2
)
27 self
.assertNotEqual(s1
, s3
)
33 def __eq__(self
, other
):
38 self
.assertRaises(Exc
, cmp, s1
, s2
)
39 self
.assertEqual(s1
, s1
)
41 s1
= slice(1, BadCmp())
42 s2
= slice(1, BadCmp())
43 self
.assertEqual(s1
, s1
)
44 self
.assertRaises(Exc
, cmp, s1
, s2
)
46 s1
= slice(1, 2, BadCmp())
47 s2
= slice(1, 2, BadCmp())
48 self
.assertEqual(s1
, s1
)
49 self
.assertRaises(Exc
, cmp, s1
, s2
)
51 def test_members(self
):
53 self
.assertEqual(s
.start
, None)
54 self
.assertEqual(s
.stop
, 1)
55 self
.assertEqual(s
.step
, None)
58 self
.assertEqual(s
.start
, 1)
59 self
.assertEqual(s
.stop
, 2)
60 self
.assertEqual(s
.step
, None)
63 self
.assertEqual(s
.start
, 1)
64 self
.assertEqual(s
.stop
, 2)
65 self
.assertEqual(s
.step
, 3)
72 self
.assert_(s
.stop
is obj
)
74 def test_indices(self
):
75 self
.assertEqual(slice(None ).indices(10), (0, 10, 1))
76 self
.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2))
77 self
.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2))
78 self
.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1))
79 self
.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2))
80 self
.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2))
82 slice(-100, 100 ).indices(10),
83 slice(None).indices(10)
86 slice(100, -100, -1).indices(10),
87 slice(None, None, -1).indices(10)
89 self
.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10, 2))
91 self
.assertEqual(range(10)[::sys
.maxint
- 1], [0])
93 self
.assertRaises(OverflowError, slice(None).indices
, 1L<<100)
96 test_support
.run_unittest(SliceTest
)
98 if __name__
== "__main__":