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 | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 4 | import weakref |
| 5 | |
Raymond Hettinger | 1393669 | 2007-04-11 18:40:58 +0000 | [diff] [blame] | 6 | from cPickle import loads, dumps |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 7 | from test import test_support |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 8 | |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 9 | import sys |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 11 | class SliceTest(unittest.TestCase): |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 12 | |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 13 | 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 Hettinger | b859c07 | 2003-09-05 14:27:30 +0000 | [diff] [blame] | 20 | def test_hash(self): |
| 21 | # Verify clearing of SF bug #800796 |
| 22 | self.assertRaises(TypeError, hash, slice(5)) |
Serhiy Storchaka | 3220849 | 2015-05-20 18:37:37 +0300 | [diff] [blame] | 23 | with self.assertRaises(TypeError): |
| 24 | slice(5).__hash__() |
Raymond Hettinger | b859c07 | 2003-09-05 14:27:30 +0000 | [diff] [blame] | 25 | |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 26 | 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 Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 39 | __hash__ = None # Silence Py3k warning |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 40 | |
| 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 Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 77 | self.assertTrue(s.stop is obj) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 78 | |
| 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 Dickinson | 1ec2fcd | 2008-06-20 14:53:43 +0000 | [diff] [blame] | 86 | # 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 100 | 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 Brandl | 3071a1a | 2007-03-06 11:51:14 +0000 | [diff] [blame] | 114 | 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 Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 121 | with test_support.check_py3k_warnings(): |
| 122 | x[1:2] = 42 |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 123 | self.assertEqual(tmp, [(1, 2, 42)]) |
Georg Brandl | 3071a1a | 2007-03-06 11:51:14 +0000 | [diff] [blame] | 124 | |
Raymond Hettinger | 1393669 | 2007-04-11 18:40:58 +0000 | [diff] [blame] | 125 | 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 Brandl | 3071a1a | 2007-03-06 11:51:14 +0000 | [diff] [blame] | 132 | |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 133 | 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 142 | def test_main(): |
| 143 | test_support.run_unittest(SliceTest) |
| 144 | |
| 145 | if __name__ == "__main__": |
| 146 | test_main() |