blob: 4d89aa6a397f1f972663f1504973be4c76bab146 [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
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)
29
30 class Exc(Exception):
31 pass
32
33 class BadCmp(object):
34 def __eq__(self, other):
35 raise Exc
36
37 s1 = slice(BadCmp())
38 s2 = slice(BadCmp())
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000039 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000040 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000041
42 s1 = slice(1, BadCmp())
43 s2 = slice(1, BadCmp())
44 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000045 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000046
47 s1 = slice(1, 2, BadCmp())
48 s2 = slice(1, 2, BadCmp())
49 self.assertEqual(s1, s1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +000050 self.assertRaises(Exc, lambda: s1 == s2)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000051
52 def test_members(self):
53 s = slice(1)
54 self.assertEqual(s.start, None)
55 self.assertEqual(s.stop, 1)
56 self.assertEqual(s.step, None)
57
58 s = slice(1, 2)
59 self.assertEqual(s.start, 1)
60 self.assertEqual(s.stop, 2)
61 self.assertEqual(s.step, None)
62
63 s = slice(1, 2, 3)
64 self.assertEqual(s.start, 1)
65 self.assertEqual(s.stop, 2)
66 self.assertEqual(s.step, 3)
67
68 class AnyClass:
69 pass
70
71 obj = AnyClass()
72 s = slice(obj)
73 self.assert_(s.stop is obj)
74
75 def test_indices(self):
76 self.assertEqual(slice(None ).indices(10), (0, 10, 1))
77 self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2))
78 self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2))
79 self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1))
80 self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2))
81 self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2))
82 self.assertEqual(
83 slice(-100, 100 ).indices(10),
84 slice(None).indices(10)
85 )
86 self.assertEqual(
87 slice(100, -100, -1).indices(10),
88 slice(None, None, -1).indices(10)
89 )
Guido van Rossume2a383d2007-01-15 16:59:06 +000090 self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2))
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000091
Guido van Rossum805365e2007-05-07 22:24:25 +000092 self.assertEqual(list(range(10))[::sys.maxint - 1], [0])
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000093
Guido van Rossume2a383d2007-01-15 16:59:06 +000094 self.assertRaises(OverflowError, slice(None).indices, 1<<100)
Raymond Hettinger5d2e7772003-09-02 01:53:01 +000095
Guido van Rossumd8faa362007-04-27 19:54:29 +000096 def test_setslice_without_getslice(self):
97 tmp = []
98 class X(object):
99 def __setslice__(self, i, j, k):
100 tmp.append((i, j, k))
101
102 x = X()
103 x[1:2] = 42
104 self.assertEquals(tmp, [(1, 2, 42)])
105
106 def test_pickle(self):
107 s = slice(10, 20, 3)
108 for protocol in (0,1,2):
109 t = loads(dumps(s, protocol))
110 self.assertEqual(s, t)
111 self.assertEqual(s.indices(15), t.indices(15))
112 self.assertNotEqual(id(s), id(t))
113
Raymond Hettinger5d2e7772003-09-02 01:53:01 +0000114def test_main():
115 test_support.run_unittest(SliceTest)
116
117if __name__ == "__main__":
118 test_main()