0001 # Python test set -- built-in functions 0002 0003 import test.test_support, unittest 0004 import sys 0005 0006 import warnings 0007 warnings.filterwarnings("ignore", "integer argument expected", 0008 DeprecationWarning, "unittest") 0009 0010 class XrangeTest(unittest.TestCase): 0011 def test_xrange(self): 0012 self.assertEqual(list(xrange(3)), [0, 1, 2]) 0013 self.assertEqual(list(xrange(1, 5)), [1, 2, 3, 4]) 0014 self.assertEqual(list(xrange(0)), []) 0015 self.assertEqual(list(xrange(-3)), []) 0016 self.assertEqual(list(xrange(1, 10, 3)), [1, 4, 7]) 0017 self.assertEqual(list(xrange(5, -5, -3)), [5, 2, -1, -4]) 0018 0019 a = 10 0020 b = 100 0021 c = 50 0022 0023 self.assertEqual(list(xrange(a, a+2)), [a, a+1]) 0024 self.assertEqual(list(xrange(a+2, a, -1L)), [a+2, a+1]) 0025 self.assertEqual(list(xrange(a+4, a, -2)), [a+4, a+2]) 0026 0027 seq = list(xrange(a, b, c)) 0028 self.assert_(a in seq) 0029 self.assert_(b not in seq) 0030 self.assertEqual(len(seq), 2) 0031 0032 seq = list(xrange(b, a, -c)) 0033 self.assert_(b in seq) 0034 self.assert_(a not in seq) 0035 self.assertEqual(len(seq), 2) 0036 0037 seq = list(xrange(-a, -b, -c)) 0038 self.assert_(-a in seq) 0039 self.assert_(-b not in seq) 0040 self.assertEqual(len(seq), 2) 0041 0042 self.assertRaises(TypeError, xrange) 0043 self.assertRaises(TypeError, xrange, 1, 2, 3, 4) 0044 self.assertRaises(ValueError, xrange, 1, 2, 0) 0045 0046 self.assertRaises(OverflowError, xrange, 1e100, 1e101, 1e101) 0047 0048 self.assertRaises(TypeError, xrange, 0, "spam") 0049 self.assertRaises(TypeError, xrange, 0, 42, "spam") 0050 0051 self.assertEqual(len(xrange(0, sys.maxint, sys.maxint-1)), 2) 0052 0053 self.assertRaises(OverflowError, xrange, -sys.maxint, sys.maxint) 0054 self.assertRaises(OverflowError, xrange, 0, 2*sys.maxint) 0055 0056 r = xrange(-sys.maxint, sys.maxint, 2) 0057 if sys.maxint > 0x7fffffff: 0058 # XXX raising ValueError is less than ideal, but this can't 0059 # be fixed until range_length() returns a long in rangeobject.c 0060 self.assertRaises(ValueError, len, r) 0061 else: 0062 self.assertEqual(len(r), sys.maxint) 0063 self.assertRaises(OverflowError, xrange, -sys.maxint-1, sys.maxint, 2) 0064 0065 def test_main(): 0066 test.test_support.run_unittest(XrangeTest) 0067 0068 if __name__ == "__main__": 0069 test_main() 0070
Generated by PyXR 0.9.4