diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2004-07-08 01:59:55 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2004-07-08 01:59:55 (GMT) |
commit | 1c0423a2da8c98fad1e82b849fada06426db62f1 (patch) | |
tree | e5ffe18b1db9c3b4b7b06f71d9b26e44a29ef8cb | |
parent | d7be118626386ffd0f98efc3518855aa8654c6bd (diff) | |
download | cpython-1c0423a2da8c98fad1e82b849fada06426db62f1.zip cpython-1c0423a2da8c98fad1e82b849fada06426db62f1.tar.gz cpython-1c0423a2da8c98fad1e82b849fada06426db62f1.tar.bz2 |
Exercise xrange a bit
-rw-r--r-- | Lib/test/test_xrange.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Lib/test/test_xrange.py b/Lib/test/test_xrange.py new file mode 100644 index 0000000..ce5284d --- /dev/null +++ b/Lib/test/test_xrange.py @@ -0,0 +1,59 @@ +# Python test set -- built-in functions + +import test.test_support, unittest +import sys + +import warnings +warnings.filterwarnings("ignore", "integer argument expected", + DeprecationWarning, "unittest") + +class XrangeTest(unittest.TestCase): + def test_xrange(self): + self.assertEqual(list(xrange(3)), [0, 1, 2]) + self.assertEqual(list(xrange(1, 5)), [1, 2, 3, 4]) + self.assertEqual(list(xrange(0)), []) + self.assertEqual(list(xrange(-3)), []) + self.assertEqual(list(xrange(1, 10, 3)), [1, 4, 7]) + self.assertEqual(list(xrange(5, -5, -3)), [5, 2, -1, -4]) + + a = 10 + b = 100 + c = 50 + + self.assertEqual(list(xrange(a, a+2)), [a, a+1]) + self.assertEqual(list(xrange(a+2, a, -1L)), [a+2, a+1]) + self.assertEqual(list(xrange(a+4, a, -2)), [a+4, a+2]) + + seq = list(xrange(a, b, c)) + self.assert_(a in seq) + self.assert_(b not in seq) + self.assertEqual(len(seq), 2) + + seq = list(xrange(b, a, -c)) + self.assert_(b in seq) + self.assert_(a not in seq) + self.assertEqual(len(seq), 2) + + seq = list(xrange(-a, -b, -c)) + self.assert_(-a in seq) + self.assert_(-b not in seq) + self.assertEqual(len(seq), 2) + + self.assertRaises(TypeError, xrange) + self.assertRaises(TypeError, xrange, 1, 2, 3, 4) + self.assertRaises(ValueError, xrange, 1, 2, 0) + + self.assertRaises(OverflowError, xrange, 1e100, 1e101, 1e101) + + self.assertRaises(TypeError, xrange, 0, "spam") + self.assertRaises(TypeError, xrange, 0, 42, "spam") + + self.assertRaises(OverflowError, xrange, 0, sys.maxint, sys.maxint-1) + self.assertRaises(OverflowError, xrange, -sys.maxint, sys.maxint) + self.assertRaises(OverflowError, xrange, 0, 2*sys.maxint) + +def test_main(): + test.test_support.run_unittest(XrangeTest) + +if __name__ == "__main__": + test_main() |