Replaced my dumb way of calculating seconds to midnight with Tim Peters' much more...
[python.git] / Lib / test / test_slice.py
blobd8eb88205c7102f984e018ec320fc18963e78d49
1 # tests for slice objects; in particular the indices method.
3 import unittest
4 from test import test_support
6 import sys
8 class SliceTest(unittest.TestCase):
10 def test_constructor(self):
11 self.assertRaises(TypeError, slice)
12 self.assertRaises(TypeError, slice, 1, 2, 3, 4)
14 def test_repr(self):
15 self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
17 def test_hash(self):
18 # Verify clearing of SF bug #800796
19 self.assertRaises(TypeError, hash, slice(5))
20 self.assertRaises(TypeError, slice(5).__hash__)
22 def test_cmp(self):
23 s1 = slice(1, 2, 3)
24 s2 = slice(1, 2, 3)
25 s3 = slice(1, 2, 4)
26 self.assertEqual(s1, s2)
27 self.assertNotEqual(s1, s3)
29 class Exc(Exception):
30 pass
32 class BadCmp(object):
33 def __eq__(self, other):
34 raise Exc
36 s1 = slice(BadCmp())
37 s2 = slice(BadCmp())
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):
52 s = slice(1)
53 self.assertEqual(s.start, None)
54 self.assertEqual(s.stop, 1)
55 self.assertEqual(s.step, None)
57 s = slice(1, 2)
58 self.assertEqual(s.start, 1)
59 self.assertEqual(s.stop, 2)
60 self.assertEqual(s.step, None)
62 s = slice(1, 2, 3)
63 self.assertEqual(s.start, 1)
64 self.assertEqual(s.stop, 2)
65 self.assertEqual(s.step, 3)
67 class AnyClass:
68 pass
70 obj = AnyClass()
71 s = slice(obj)
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))
81 self.assertEqual(
82 slice(-100, 100 ).indices(10),
83 slice(None).indices(10)
85 self.assertEqual(
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)
95 def test_main():
96 test_support.run_unittest(SliceTest)
98 if __name__ == "__main__":
99 test_main()