blob: 262ee12a5fe708c79f91f8d57c6c2f361e2223b2 [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
Benjamin Peterson414f8b92016-04-16 14:47:12 -07004import weakref
5
Raymond Hettinger13936692007-04-11 18:40:58 +00006from cPickle import loads, dumps
Benjamin Peterson414f8b92016-04-16 14:47:12 -07007from test import test_support
Raymond Hettinger5d2e7772003-09-02 01:53:01 +00008
Michael W. Hudson173f11d2002-11-05 15:28:51 +00009import sys
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +000010
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000011class SliceTest(unittest.TestCase):
Michael W. Hudson173f11d2002-11-05 15:28:51 +000012
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000013 def test_constructor(self):
14 self.assertRaises(TypeError, slice)
15 self.assertRaises(TypeError, slice, 1, 2, 3, 4)
16
17 def test_repr(self):
18 self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
19
Raymond Hettingerb859c072003-09-05 14:27:30 +000020 def test_hash(self):
21 # Verify clearing of SF bug #800796
22 self.assertRaises(TypeError, hash, slice(5))
Serhiy Storchaka32208492015-05-20 18:37:37 +030023 with self.assertRaises(TypeError):
24 slice(5).__hash__()
Raymond Hettingerb859c072003-09-05 14:27:30 +000025
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000026 def test_cmp(self):
27 s1 = slice(1, 2, 3)
28 s2 = slice(1, 2, 3)
29 s3 = slice(1, 2, 4)
30 self.assertEqual(s1, s2)
31 self.assertNotEqual(s1, s3)
32
33 class Exc(Exception):
34 pass
35
36 class BadCmp(object):
37 def __eq__(self, other):
38 raise Exc
Nick Coghlan48361f52008-08-11 15:45:58 +000039 __hash__ = None # Silence Py3k warning
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000040
41 s1 = slice(BadCmp())
42 s2 = slice(BadCmp())
43 self.assertRaises(Exc, cmp, s1, s2)
44 self.assertEqual(s1, s1)
45
46 s1 = slice(1, BadCmp())
47 s2 = slice(1, BadCmp())
48 self.assertEqual(s1, s1)
49 self.assertRaises(Exc, cmp, s1, s2)
50
51 s1 = slice(1, 2, BadCmp())
52 s2 = slice(1, 2, BadCmp())
53 self.assertEqual(s1, s1)
54 self.assertRaises(Exc, cmp, s1, s2)
55
56 def test_members(self):
57 s = slice(1)
58 self.assertEqual(s.start, None)
59 self.assertEqual(s.stop, 1)
60 self.assertEqual(s.step, None)
61
62 s = slice(1, 2)
63 self.assertEqual(s.start, 1)
64 self.assertEqual(s.stop, 2)
65 self.assertEqual(s.step, None)
66
67 s = slice(1, 2, 3)
68 self.assertEqual(s.start, 1)
69 self.assertEqual(s.stop, 2)
70 self.assertEqual(s.step, 3)
71
72 class AnyClass:
73 pass
74
75 obj = AnyClass()
76 s = slice(obj)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000077 self.assertTrue(s.stop is obj)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000078
79 def test_indices(self):
80 self.assertEqual(slice(None ).indices(10), (0, 10, 1))
81 self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2))
82 self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2))
83 self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1))
84 self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2))
85 self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2))
Mark Dickinson1ec2fcd2008-06-20 14:53:43 +000086 # issue 3004 tests
87 self.assertEqual(slice(None, -9).indices(10), (0, 1, 1))
88 self.assertEqual(slice(None, -10).indices(10), (0, 0, 1))
89 self.assertEqual(slice(None, -11).indices(10), (0, 0, 1))
90 self.assertEqual(slice(None, -10, -1).indices(10), (9, 0, -1))
91 self.assertEqual(slice(None, -11, -1).indices(10), (9, -1, -1))
92 self.assertEqual(slice(None, -12, -1).indices(10), (9, -1, -1))
93 self.assertEqual(slice(None, 9).indices(10), (0, 9, 1))
94 self.assertEqual(slice(None, 10).indices(10), (0, 10, 1))
95 self.assertEqual(slice(None, 11).indices(10), (0, 10, 1))
96 self.assertEqual(slice(None, 8, -1).indices(10), (9, 8, -1))
97 self.assertEqual(slice(None, 9, -1).indices(10), (9, 9, -1))
98 self.assertEqual(slice(None, 10, -1).indices(10), (9, 9, -1))
99
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000100 self.assertEqual(
101 slice(-100, 100 ).indices(10),
102 slice(None).indices(10)
103 )
104 self.assertEqual(
105 slice(100, -100, -1).indices(10),
106 slice(None, None, -1).indices(10)
107 )
108 self.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10, 2))
109
110 self.assertEqual(range(10)[::sys.maxint - 1], [0])
111
112 self.assertRaises(OverflowError, slice(None).indices, 1L<<100)
113
Georg Brandl3071a1a2007-03-06 11:51:14 +0000114 def test_setslice_without_getslice(self):
115 tmp = []
116 class X(object):
117 def __setslice__(self, i, j, k):
118 tmp.append((i, j, k))
119
120 x = X()
Florent Xicluna07627882010-03-21 01:14:24 +0000121 with test_support.check_py3k_warnings():
122 x[1:2] = 42
Ezio Melotti2623a372010-11-21 13:34:58 +0000123 self.assertEqual(tmp, [(1, 2, 42)])
Georg Brandl3071a1a2007-03-06 11:51:14 +0000124
Raymond Hettinger13936692007-04-11 18:40:58 +0000125 def test_pickle(self):
126 s = slice(10, 20, 3)
127 for protocol in (0,1,2):
128 t = loads(dumps(s, protocol))
129 self.assertEqual(s, t)
130 self.assertEqual(s.indices(15), t.indices(15))
131 self.assertNotEqual(id(s), id(t))
Georg Brandl3071a1a2007-03-06 11:51:14 +0000132
Benjamin Peterson414f8b92016-04-16 14:47:12 -0700133 def test_cycle(self):
134 class myobj(): pass
135 o = myobj()
136 o.s = slice(o)
137 w = weakref.ref(o)
138 o = None
139 test_support.gc_collect()
140 self.assertIsNone(w())
141
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000142def test_main():
143 test_support.run_unittest(SliceTest)
144
145if __name__ == "__main__":
146 test_main()