Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 1 | # tests for slice objects; in particular the indices method. |
| 2 | |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 5 | from pickle import loads, dumps |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 6 | |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 7 | import sys |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 8 | |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 9 | class SliceTest(unittest.TestCase): |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 11 | 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 Hettinger | b859c07 | 2003-09-05 14:27:30 +0000 | [diff] [blame] | 18 | 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 23 | 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 Wouters | 3e57b52 | 2007-08-28 23:07:26 +0000 | [diff] [blame] | 29 | self.assertNotEqual(s1, None) |
| 30 | self.assertNotEqual(s1, (1, 2, 3)) |
| 31 | self.assertNotEqual(s1, "") |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 32 | |
| 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 42 | self.assertEqual(s1, s1) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 43 | self.assertRaises(Exc, lambda: s1 == s2) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 44 | |
| 45 | s1 = slice(1, BadCmp()) |
| 46 | s2 = slice(1, BadCmp()) |
| 47 | self.assertEqual(s1, s1) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 48 | self.assertRaises(Exc, lambda: s1 == s2) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 49 | |
| 50 | s1 = slice(1, 2, BadCmp()) |
| 51 | s2 = slice(1, 2, BadCmp()) |
| 52 | self.assertEqual(s1, s1) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 53 | self.assertRaises(Exc, lambda: s1 == s2) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 54 | |
| 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 76 | self.assertTrue(s.stop is obj) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 77 | |
| 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 Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 85 | # 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 99 | 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 Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 107 | self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2)) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 108 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 109 | self.assertEqual(list(range(10))[::sys.maxsize - 1], [0]) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 111 | self.assertRaises(OverflowError, slice(None).indices, 1<<100) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 113 | def test_setslice_without_getslice(self): |
| 114 | tmp = [] |
| 115 | class X(object): |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 116 | def __setitem__(self, i, k): |
| 117 | tmp.append((i, k)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 118 | |
| 119 | x = X() |
| 120 | x[1:2] = 42 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 121 | self.assertEqual(tmp, [(slice(1, 2), 42)]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 122 | |
| 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 131 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 132 | support.run_unittest(SliceTest) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 133 | |
| 134 | if __name__ == "__main__": |
| 135 | test_main() |