blob: 2df9271da7c7667c9497d20e5e515f160d63d92f [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 Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Guido van Rossum99603b02007-07-20 00:22:32 +00005from pickle import loads, dumps
Raymond Hettinger5d2e7772003-09-02 01:53:01 +00006
Michael W. Hudson173f11d2002-11-05 15:28:51 +00007import sys
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +00008
Raymond Hettinger5d2e7772003-09-02 01:53:01 +00009class SliceTest(unittest.TestCase):
Michael W. Hudson173f11d2002-11-05 15:28:51 +000010
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000011 def test_constructor(self):
12 self.assertRaises(TypeError, slice)
13 self.assertRaises(TypeError, slice, 1, 2, 3, 4)
14
15 def test_repr(self):
16 self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
17
Raymond Hettingerb859c072003-09-05 14:27:30 +000018 def test_hash(self):
19 # Verify clearing of SF bug #800796
20 self.assertRaises(TypeError, hash, slice(5))
21 self.assertRaises(TypeError, slice(5).__hash__)
22
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000023 def test_cmp(self):
24 s1 = slice(1, 2, 3)
25 s2 = slice(1, 2, 3)
26 s3 = slice(1, 2, 4)
27 self.assertEqual(s1, s2)
28 self.assertNotEqual(s1, s3)
Thomas Wouters3e57b522007-08-28 23:07:26 +000029 self.assertNotEqual(s1, None)
30 self.assertNotEqual(s1, (1, 2, 3))
31 self.assertNotEqual(s1, "")
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000032
33 class Exc(Exception):
34 pass
35
36 class BadCmp(object):
37 def __eq__(self, other):
38 raise Exc
39
40 s1 = slice(BadCmp())
41 s2 = slice(BadCmp())
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000042 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000043 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000044
45 s1 = slice(1, BadCmp())
46 s2 = slice(1, BadCmp())
47 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000048 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000049
50 s1 = slice(1, 2, BadCmp())
51 s2 = slice(1, 2, BadCmp())
52 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000053 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000054
55 def test_members(self):
56 s = slice(1)
57 self.assertEqual(s.start, None)
58 self.assertEqual(s.stop, 1)
59 self.assertEqual(s.step, None)
60
61 s = slice(1, 2)
62 self.assertEqual(s.start, 1)
63 self.assertEqual(s.stop, 2)
64 self.assertEqual(s.step, None)
65
66 s = slice(1, 2, 3)
67 self.assertEqual(s.start, 1)
68 self.assertEqual(s.stop, 2)
69 self.assertEqual(s.step, 3)
70
71 class AnyClass:
72 pass
73
74 obj = AnyClass()
75 s = slice(obj)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000076 self.assertTrue(s.stop is obj)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000077
78 def test_indices(self):
79 self.assertEqual(slice(None ).indices(10), (0, 10, 1))
80 self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2))
81 self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2))
82 self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1))
83 self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2))
84 self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2))
Benjamin Petersonfea6a942008-07-02 16:11:42 +000085 # issue 3004 tests
86 self.assertEqual(slice(None, -9).indices(10), (0, 1, 1))
87 self.assertEqual(slice(None, -10).indices(10), (0, 0, 1))
88 self.assertEqual(slice(None, -11).indices(10), (0, 0, 1))
89 self.assertEqual(slice(None, -10, -1).indices(10), (9, 0, -1))
90 self.assertEqual(slice(None, -11, -1).indices(10), (9, -1, -1))
91 self.assertEqual(slice(None, -12, -1).indices(10), (9, -1, -1))
92 self.assertEqual(slice(None, 9).indices(10), (0, 9, 1))
93 self.assertEqual(slice(None, 10).indices(10), (0, 10, 1))
94 self.assertEqual(slice(None, 11).indices(10), (0, 10, 1))
95 self.assertEqual(slice(None, 8, -1).indices(10), (9, 8, -1))
96 self.assertEqual(slice(None, 9, -1).indices(10), (9, 9, -1))
97 self.assertEqual(slice(None, 10, -1).indices(10), (9, 9, -1))
98
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000099 self.assertEqual(
100 slice(-100, 100 ).indices(10),
101 slice(None).indices(10)
102 )
103 self.assertEqual(
104 slice(100, -100, -1).indices(10),
105 slice(None, None, -1).indices(10)
106 )
Guido van Rossume2a383d2007-01-15 16:59:06 +0000107 self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2))
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000108
Christian Heimesa37d4c62007-12-04 23:02:19 +0000109 self.assertEqual(list(range(10))[::sys.maxsize - 1], [0])
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000110
Guido van Rossume2a383d2007-01-15 16:59:06 +0000111 self.assertRaises(OverflowError, slice(None).indices, 1<<100)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000112
Guido van Rossumd8faa362007-04-27 19:54:29 +0000113 def test_setslice_without_getslice(self):
114 tmp = []
115 class X(object):
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000116 def __setitem__(self, i, k):
117 tmp.append((i, k))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000118
119 x = X()
120 x[1:2] = 42
Ezio Melottib3aedd42010-11-20 19:04:17 +0000121 self.assertEqual(tmp, [(slice(1, 2), 42)])
Guido van Rossumd8faa362007-04-27 19:54:29 +0000122
123 def test_pickle(self):
124 s = slice(10, 20, 3)
125 for protocol in (0,1,2):
126 t = loads(dumps(s, protocol))
127 self.assertEqual(s, t)
128 self.assertEqual(s.indices(15), t.indices(15))
129 self.assertNotEqual(id(s), id(t))
130
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000131def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000132 support.run_unittest(SliceTest)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000133
134if __name__ == "__main__":
135 test_main()