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 |
| 4 | from test import test_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) |
| 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 39 | self.assertEqual(s1, s1) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 40 | self.assertRaises(Exc, lambda: s1 == s2) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 41 | |
| 42 | s1 = slice(1, BadCmp()) |
| 43 | s2 = slice(1, BadCmp()) |
| 44 | self.assertEqual(s1, s1) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 45 | self.assertRaises(Exc, lambda: s1 == s2) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 46 | |
| 47 | s1 = slice(1, 2, BadCmp()) |
| 48 | s2 = slice(1, 2, BadCmp()) |
| 49 | self.assertEqual(s1, s1) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 50 | self.assertRaises(Exc, lambda: s1 == s2) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 51 | |
| 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 Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 90 | self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2)) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 92 | self.assertEqual(list(range(10))[::sys.maxint - 1], [0]) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 94 | self.assertRaises(OverflowError, slice(None).indices, 1<<100) |
Raymond Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 96 | 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 Hettinger | 5d2e777 | 2003-09-02 01:53:01 +0000 | [diff] [blame] | 114 | def test_main(): |
| 115 | test_support.run_unittest(SliceTest) |
| 116 | |
| 117 | if __name__ == "__main__": |
| 118 | test_main() |