0001 # tests for slice objects; in particular the indices method. 0002 0003 import unittest 0004 from test import test_support 0005 0006 import sys 0007 0008 class SliceTest(unittest.TestCase): 0009 0010 def test_constructor(self): 0011 self.assertRaises(TypeError, slice) 0012 self.assertRaises(TypeError, slice, 1, 2, 3, 4) 0013 0014 def test_repr(self): 0015 self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)") 0016 0017 def test_hash(self): 0018 # Verify clearing of SF bug #800796 0019 self.assertRaises(TypeError, hash, slice(5)) 0020 self.assertRaises(TypeError, slice(5).__hash__) 0021 0022 def test_cmp(self): 0023 s1 = slice(1, 2, 3) 0024 s2 = slice(1, 2, 3) 0025 s3 = slice(1, 2, 4) 0026 self.assertEqual(s1, s2) 0027 self.assertNotEqual(s1, s3) 0028 0029 class Exc(Exception): 0030 pass 0031 0032 class BadCmp(object): 0033 def __eq__(self, other): 0034 raise Exc 0035 0036 s1 = slice(BadCmp()) 0037 s2 = slice(BadCmp()) 0038 self.assertRaises(Exc, cmp, s1, s2) 0039 self.assertEqual(s1, s1) 0040 0041 s1 = slice(1, BadCmp()) 0042 s2 = slice(1, BadCmp()) 0043 self.assertEqual(s1, s1) 0044 self.assertRaises(Exc, cmp, s1, s2) 0045 0046 s1 = slice(1, 2, BadCmp()) 0047 s2 = slice(1, 2, BadCmp()) 0048 self.assertEqual(s1, s1) 0049 self.assertRaises(Exc, cmp, s1, s2) 0050 0051 def test_members(self): 0052 s = slice(1) 0053 self.assertEqual(s.start, None) 0054 self.assertEqual(s.stop, 1) 0055 self.assertEqual(s.step, None) 0056 0057 s = slice(1, 2) 0058 self.assertEqual(s.start, 1) 0059 self.assertEqual(s.stop, 2) 0060 self.assertEqual(s.step, None) 0061 0062 s = slice(1, 2, 3) 0063 self.assertEqual(s.start, 1) 0064 self.assertEqual(s.stop, 2) 0065 self.assertEqual(s.step, 3) 0066 0067 class AnyClass: 0068 pass 0069 0070 obj = AnyClass() 0071 s = slice(obj) 0072 self.assert_(s.stop is obj) 0073 0074 def test_indices(self): 0075 self.assertEqual(slice(None ).indices(10), (0, 10, 1)) 0076 self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2)) 0077 self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2)) 0078 self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1)) 0079 self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2)) 0080 self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2)) 0081 self.assertEqual( 0082 slice(-100, 100 ).indices(10), 0083 slice(None).indices(10) 0084 ) 0085 self.assertEqual( 0086 slice(100, -100, -1).indices(10), 0087 slice(None, None, -1).indices(10) 0088 ) 0089 self.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10, 2)) 0090 0091 self.assertEqual(range(10)[::sys.maxint - 1], [0]) 0092 0093 self.assertRaises(OverflowError, slice(None).indices, 1L<<100) 0094 0095 def test_main(): 0096 test_support.run_unittest(SliceTest) 0097 0098 if __name__ == "__main__": 0099 test_main() 0100
Generated by PyXR 0.9.4