1 # Python test set -- built-in functions
3 import test
.test_support
, unittest
9 warnings
.filterwarnings("ignore", "integer argument expected",
10 DeprecationWarning, "unittest")
12 # pure Python implementations (3 args only), for comparison
13 def pyrange(start
, stop
, step
):
14 if (start
- stop
) // step
< 0:
15 # replace stop with next element in the sequence of integers
16 # that are congruent to start modulo step.
17 stop
+= (start
- stop
) % step
22 def pyrange_reversed(start
, stop
, step
):
23 stop
+= (start
- stop
) % step
24 return pyrange(stop
- step
, start
- step
, -step
)
27 class XrangeTest(unittest
.TestCase
):
28 def assert_iterators_equal(self
, xs
, ys
, test_id
, limit
=None):
29 # check that an iterator xs matches the expected results ys,
30 # up to a given limit.
32 xs
= itertools
.islice(xs
, limit
)
33 ys
= itertools
.islice(ys
, limit
)
35 pairs
= itertools
.izip_longest(xs
, ys
, fillvalue
=sentinel
)
36 for i
, (x
, y
) in enumerate(pairs
):
40 self
.fail('{}: iterator ended unexpectedly '
41 'at position {}; expected {}'.format(test_id
, i
, y
))
43 self
.fail('{}: unexpected excess element {} at '
44 'position {}'.format(test_id
, x
, i
))
46 self
.fail('{}: wrong element at position {};'
47 'expected {}, got {}'.format(test_id
, i
, y
, x
))
49 def test_xrange(self
):
50 self
.assertEqual(list(xrange(3)), [0, 1, 2])
51 self
.assertEqual(list(xrange(1, 5)), [1, 2, 3, 4])
52 self
.assertEqual(list(xrange(0)), [])
53 self
.assertEqual(list(xrange(-3)), [])
54 self
.assertEqual(list(xrange(1, 10, 3)), [1, 4, 7])
55 self
.assertEqual(list(xrange(5, -5, -3)), [5, 2, -1, -4])
61 self
.assertEqual(list(xrange(a
, a
+2)), [a
, a
+1])
62 self
.assertEqual(list(xrange(a
+2, a
, -1L)), [a
+2, a
+1])
63 self
.assertEqual(list(xrange(a
+4, a
, -2)), [a
+4, a
+2])
65 seq
= list(xrange(a
, b
, c
))
67 self
.assertNotIn(b
, seq
)
68 self
.assertEqual(len(seq
), 2)
70 seq
= list(xrange(b
, a
, -c
))
72 self
.assertNotIn(a
, seq
)
73 self
.assertEqual(len(seq
), 2)
75 seq
= list(xrange(-a
, -b
, -c
))
76 self
.assertIn(-a
, seq
)
77 self
.assertNotIn(-b
, seq
)
78 self
.assertEqual(len(seq
), 2)
80 self
.assertRaises(TypeError, xrange)
81 self
.assertRaises(TypeError, xrange, 1, 2, 3, 4)
82 self
.assertRaises(ValueError, xrange, 1, 2, 0)
84 self
.assertRaises(OverflowError, xrange, 10**100, 10**101, 10**101)
86 self
.assertRaises(TypeError, xrange, 0, "spam")
87 self
.assertRaises(TypeError, xrange, 0, 42, "spam")
89 self
.assertEqual(len(xrange(0, sys
.maxint
, sys
.maxint
-1)), 2)
91 self
.assertRaises(OverflowError, xrange, -sys
.maxint
, sys
.maxint
)
92 self
.assertRaises(OverflowError, xrange, 0, 2*sys
.maxint
)
94 r
= xrange(-sys
.maxint
, sys
.maxint
, 2)
95 self
.assertEqual(len(r
), sys
.maxint
)
96 self
.assertRaises(OverflowError, xrange, -sys
.maxint
-1, sys
.maxint
, 2)
98 def test_pickling(self
):
99 testcases
= [(13,), (0, 11), (-22, 10), (20, 3, -1),
100 (13, 21, 3), (-2, 2, 2)]
101 for proto
in range(pickle
.HIGHEST_PROTOCOL
+ 1):
104 self
.assertEquals(list(pickle
.loads(pickle
.dumps(r
, proto
))),
107 def test_range_iterators(self
):
109 limits
= [base
+ jiggle
110 for M
in (2**32, 2**64)
111 for base
in (-M
, -M
//2, 0, M
//2, M
)
112 for jiggle
in (-2, -1, 0, 1, 2)]
113 test_ranges
= [(start
, end
, step
)
116 for step
in (-2**63, -2**31, -2, -1, 1, 2)]
118 for start
, end
, step
in test_ranges
:
120 iter1
= xrange(start
, end
, step
)
121 except OverflowError:
124 iter2
= pyrange(start
, end
, step
)
125 test_id
= "xrange({}, {}, {})".format(start
, end
, step
)
126 # check first 100 entries
127 self
.assert_iterators_equal(iter1
, iter2
, test_id
, limit
=100)
130 iter1
= reversed(xrange(start
, end
, step
))
131 except OverflowError:
134 iter2
= pyrange_reversed(start
, end
, step
)
135 test_id
= "reversed(xrange({}, {}, {}))".format(start
, end
, step
)
136 self
.assert_iterators_equal(iter1
, iter2
, test_id
, limit
=100)
140 test
.test_support
.run_unittest(XrangeTest
)
142 if __name__
== "__main__":