blob: f22f619aba4c2353a16ad9fcfc5aab40d017a2b4 [file] [log] [blame]
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +00001# tests for slice objects; in particular the indices method.
2
Raymond Hettinger5d2e7772003-09-02 01:53:01 +00003import unittest
4from test import test_support
5
Michael W. Hudson173f11d2002-11-05 15:28:51 +00006import sys
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +00007
Raymond Hettinger5d2e7772003-09-02 01:53:01 +00008class SliceTest(unittest.TestCase):
Michael W. Hudson173f11d2002-11-05 15:28:51 +00009
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000010 def test_constructor(self):
11 self.assertRaises(TypeError, slice)
12 self.assertRaises(TypeError, slice, 1, 2, 3, 4)
13
14 def test_repr(self):
15 self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
16
Raymond Hettingerb859c072003-09-05 14:27:30 +000017 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__)
21
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000022 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)
28
29 class Exc(Exception):
30 pass
31
32 class BadCmp(object):
33 def __eq__(self, other):
34 raise Exc
35
36 s1 = slice(BadCmp())
37 s2 = slice(BadCmp())
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000038 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000039 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000040
41 s1 = slice(1, BadCmp())
42 s2 = slice(1, BadCmp())
43 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000044 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000045
46 s1 = slice(1, 2, BadCmp())
47 s2 = slice(1, 2, BadCmp())
48 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000049 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000050
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)
56
57 s = slice(1, 2)
58 self.assertEqual(s.start, 1)
59 self.assertEqual(s.stop, 2)
60 self.assertEqual(s.step, None)
61
62 s = slice(1, 2, 3)
63 self.assertEqual(s.start, 1)
64 self.assertEqual(s.stop, 2)
65 self.assertEqual(s.step, 3)
66
67 class AnyClass:
68 pass
69
70 obj = AnyClass()
71 s = slice(obj)
72 self.assert_(s.stop is obj)
73
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)
84 )
85 self.assertEqual(
86 slice(100, -100, -1).indices(10),
87 slice(None, None, -1).indices(10)
88 )
Guido van Rossume2a383d2007-01-15 16:59:06 +000089 self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2))
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000090
91 self.assertEqual(range(10)[::sys.maxint - 1], [0])
92
Guido van Rossume2a383d2007-01-15 16:59:06 +000093 self.assertRaises(OverflowError, slice(None).indices, 1<<100)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000094
95def test_main():
96 test_support.run_unittest(SliceTest)
97
98if __name__ == "__main__":
99 test_main()