~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Lib/test/test_xrange.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import test.test_support, unittest
4
4
import sys
5
5
import pickle
 
6
import itertools
6
7
 
7
8
import warnings
8
9
warnings.filterwarnings("ignore", "integer argument expected",
9
10
                        DeprecationWarning, "unittest")
10
11
 
 
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
 
18
        while start != stop:
 
19
            yield start
 
20
            start += step
 
21
 
 
22
def pyrange_reversed(start, stop, step):
 
23
    stop += (start - stop) % step
 
24
    return pyrange(stop - step, start - step, -step)
 
25
 
 
26
 
11
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.
 
31
        if limit is not None:
 
32
            xs = itertools.islice(xs, limit)
 
33
            ys = itertools.islice(ys, limit)
 
34
        sentinel = object()
 
35
        pairs = itertools.izip_longest(xs, ys, fillvalue=sentinel)
 
36
        for i, (x, y) in enumerate(pairs):
 
37
            if x == y:
 
38
                continue
 
39
            elif x == sentinel:
 
40
                self.fail('{0}: iterator ended unexpectedly '
 
41
                          'at position {1}; expected {2}'.format(test_id, i, y))
 
42
            elif y == sentinel:
 
43
                self.fail('{0}: unexpected excess element {1} at '
 
44
                          'position {2}'.format(test_id, x, i))
 
45
            else:
 
46
                self.fail('{0}: wrong element at position {1};'
 
47
                          'expected {2}, got {3}'.format(test_id, i, y, x))
 
48
 
12
49
    def test_xrange(self):
13
50
        self.assertEqual(list(xrange(3)), [0, 1, 2])
14
51
        self.assertEqual(list(xrange(1, 5)), [1, 2, 3, 4])
67
104
                self.assertEquals(list(pickle.loads(pickle.dumps(r, proto))),
68
105
                                  list(r))
69
106
 
 
107
    def test_range_iterators(self):
 
108
        # see issue 7298
 
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)
 
114
                       for start in limits
 
115
                       for end in limits
 
116
                       for step in (-2**63, -2**31, -2, -1, 1, 2)]
 
117
 
 
118
        for start, end, step in test_ranges:
 
119
            try:
 
120
                iter1 = xrange(start, end, step)
 
121
            except OverflowError:
 
122
                pass
 
123
            else:
 
124
                iter2 = pyrange(start, end, step)
 
125
                test_id = "xrange({0}, {1}, {2})".format(start, end, step)
 
126
                # check first 100 entries
 
127
                self.assert_iterators_equal(iter1, iter2, test_id, limit=100)
 
128
 
 
129
            try:
 
130
                iter1 = reversed(xrange(start, end, step))
 
131
            except OverflowError:
 
132
                pass
 
133
            else:
 
134
                iter2 = pyrange_reversed(start, end, step)
 
135
                test_id = "reversed(xrange({0}, {1}, {2}))".format(
 
136
                    start, end, step)
 
137
                self.assert_iterators_equal(iter1, iter2, test_id, limit=100)
 
138
 
70
139
 
71
140
def test_main():
72
141
    test.test_support.run_unittest(XrangeTest)