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) |
| 76 | self.assert_(s.stop is obj) |
| 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)) |
| 85 | self.assertEqual( |
| 86 | slice(-100, 100 ).indices(10), |
| 87 | slice(None).indices(10) |
| 88 | ) |
| 89 | self.assertEqual( |
| 90 | slice(100, -100, -1).indices(10), |
| 91 | slice(None, None, -1).indices(10) |
| 92 | ) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 93 | self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2)) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 94 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 95 | self.assertEqual(list(range(10))[::sys.maxsize - 1], [0]) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 97 | self.assertRaises(OverflowError, slice(None).indices, 1<<100) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 99 | def test_setslice_without_getslice(self): |
| 100 | tmp = [] |
| 101 | class X(object): |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 102 | def __setitem__(self, i, k): |
| 103 | tmp.append((i, k)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 104 | |
| 105 | x = X() |
| 106 | x[1:2] = 42 |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 107 | self.assertEquals(tmp, [(slice(1, 2), 42)]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 108 | |
| 109 | def test_pickle(self): |
| 110 | s = slice(10, 20, 3) |
| 111 | for protocol in (0,1,2): |
| 112 | t = loads(dumps(s, protocol)) |
| 113 | self.assertEqual(s, t) |
| 114 | self.assertEqual(s.indices(15), t.indices(15)) |
| 115 | self.assertNotEqual(id(s), id(t)) |
| 116 | |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 117 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 118 | support.run_unittest(SliceTest) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 119 | |
| 120 | if __name__ == "__main__": |
| 121 | test_main() |