blob: eafb7fad569e1ac260f87fccd6f545542fe0866c [file] [log] [blame]
Neal Norwitz1c0423a2004-07-08 01:59:55 +00001# Python test set -- built-in functions
2
3import test.test_support, unittest
4import sys
5
6import warnings
7warnings.filterwarnings("ignore", "integer argument expected",
8 DeprecationWarning, "unittest")
9
10class XrangeTest(unittest.TestCase):
11 def test_xrange(self):
12 self.assertEqual(list(xrange(3)), [0, 1, 2])
13 self.assertEqual(list(xrange(1, 5)), [1, 2, 3, 4])
14 self.assertEqual(list(xrange(0)), [])
15 self.assertEqual(list(xrange(-3)), [])
16 self.assertEqual(list(xrange(1, 10, 3)), [1, 4, 7])
17 self.assertEqual(list(xrange(5, -5, -3)), [5, 2, -1, -4])
18
19 a = 10
20 b = 100
21 c = 50
22
23 self.assertEqual(list(xrange(a, a+2)), [a, a+1])
24 self.assertEqual(list(xrange(a+2, a, -1L)), [a+2, a+1])
25 self.assertEqual(list(xrange(a+4, a, -2)), [a+4, a+2])
26
27 seq = list(xrange(a, b, c))
28 self.assert_(a in seq)
29 self.assert_(b not in seq)
30 self.assertEqual(len(seq), 2)
31
32 seq = list(xrange(b, a, -c))
33 self.assert_(b in seq)
34 self.assert_(a not in seq)
35 self.assertEqual(len(seq), 2)
36
37 seq = list(xrange(-a, -b, -c))
38 self.assert_(-a in seq)
39 self.assert_(-b not in seq)
40 self.assertEqual(len(seq), 2)
41
42 self.assertRaises(TypeError, xrange)
43 self.assertRaises(TypeError, xrange, 1, 2, 3, 4)
44 self.assertRaises(ValueError, xrange, 1, 2, 0)
45
46 self.assertRaises(OverflowError, xrange, 1e100, 1e101, 1e101)
47
48 self.assertRaises(TypeError, xrange, 0, "spam")
49 self.assertRaises(TypeError, xrange, 0, 42, "spam")
50
Tim Petersfeec4532004-08-08 07:17:39 +000051 self.assertEqual(len(xrange(0, sys.maxint, sys.maxint-1)), 2)
52
Neal Norwitz1c0423a2004-07-08 01:59:55 +000053 self.assertRaises(OverflowError, xrange, -sys.maxint, sys.maxint)
54 self.assertRaises(OverflowError, xrange, 0, 2*sys.maxint)
55
Neal Norwitz64fa74d2004-10-14 03:46:18 +000056 r = xrange(-sys.maxint, sys.maxint, 2)
Thomas Woutersf4d8f392006-04-04 17:28:12 +000057 self.assertEqual(len(r), sys.maxint)
Tim Petersfeec4532004-08-08 07:17:39 +000058 self.assertRaises(OverflowError, xrange, -sys.maxint-1, sys.maxint, 2)
59
Neal Norwitz1c0423a2004-07-08 01:59:55 +000060def test_main():
61 test.test_support.run_unittest(XrangeTest)
62
63if __name__ == "__main__":
64 test_main()