Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_range.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 458340ed0606 |
| children |
| rev | line source |
|---|---|
| 32535 | 1 # Python test set -- built-in functions |
| 2 | |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
44160
diff
changeset
|
3 import test.support, unittest |
| 32535 | 4 import sys |
|
47536
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47201
diff
changeset
|
5 import pickle |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
6 import itertools |
| 32535 | 7 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
8 # pure Python implementations (3 args only), for comparison |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
9 def pyrange(start, stop, step): |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
10 if (start - stop) // step < 0: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
11 # replace stop with next element in the sequence of integers |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
12 # that are congruent to start modulo step. |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
13 stop += (start - stop) % step |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
14 while start != stop: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
15 yield start |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
16 start += step |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
17 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
18 def pyrange_reversed(start, stop, step): |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
19 stop += (start - stop) % step |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
20 return pyrange(stop - step, start - step, -step) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
21 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
22 |
|
41803
3d3bf27409e6
Rename test_xrange.py to test_range.py and fix the
Walter Dörwald <walter@livinglogic.de>
parents:
41800
diff
changeset
|
23 class RangeTest(unittest.TestCase): |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
24 def assert_iterators_equal(self, xs, ys, test_id, limit=None): |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
25 # check that an iterator xs matches the expected results ys, |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
26 # up to a given limit. |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
27 if limit is not None: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
28 xs = itertools.islice(xs, limit) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
29 ys = itertools.islice(ys, limit) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
30 sentinel = object() |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
31 pairs = itertools.zip_longest(xs, ys, fillvalue=sentinel) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
32 for i, (x, y) in enumerate(pairs): |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
33 if x == y: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
34 continue |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
35 elif x == sentinel: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
36 self.fail('{}: iterator ended unexpectedly ' |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
37 'at position {}; expected {}'.format(test_id, i, y)) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
38 elif y == sentinel: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
39 self.fail('{}: unexpected excess element {} at ' |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
40 'position {}'.format(test_id, x, i)) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
41 else: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
42 self.fail('{}: wrong element at position {};' |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
43 'expected {}, got {}'.format(test_id, i, y, x)) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
44 |
|
41803
3d3bf27409e6
Rename test_xrange.py to test_range.py and fix the
Walter Dörwald <walter@livinglogic.de>
parents:
41800
diff
changeset
|
45 def test_range(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
46 self.assertEqual(list(range(3)), [0, 1, 2]) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
47 self.assertEqual(list(range(1, 5)), [1, 2, 3, 4]) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
48 self.assertEqual(list(range(0)), []) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
49 self.assertEqual(list(range(-3)), []) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
50 self.assertEqual(list(range(1, 10, 3)), [1, 4, 7]) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
51 self.assertEqual(list(range(5, -5, -3)), [5, 2, -1, -4]) |
| 32535 | 52 |
| 53 a = 10 | |
| 54 b = 100 | |
| 55 c = 50 | |
| 56 | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
57 self.assertEqual(list(range(a, a+2)), [a, a+1]) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
58 self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1]) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
59 self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2]) |
| 32535 | 60 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
61 seq = list(range(a, b, c)) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
62 self.assertIn(a, seq) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
63 self.assertNotIn(b, seq) |
| 32535 | 64 self.assertEqual(len(seq), 2) |
| 65 | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
66 seq = list(range(b, a, -c)) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
67 self.assertIn(b, seq) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
68 self.assertNotIn(a, seq) |
| 32535 | 69 self.assertEqual(len(seq), 2) |
| 70 | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
71 seq = list(range(-a, -b, -c)) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
72 self.assertIn(-a, seq) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
73 self.assertNotIn(-b, seq) |
| 32535 | 74 self.assertEqual(len(seq), 2) |
| 75 | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
76 self.assertRaises(TypeError, range) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
77 self.assertRaises(TypeError, range, 1, 2, 3, 4) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
78 self.assertRaises(ValueError, range, 1, 2, 0) |
| 32535 | 79 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
80 self.assertRaises(TypeError, range, 0.0, 2, 1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
81 self.assertRaises(TypeError, range, 1, 2.0, 1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
82 self.assertRaises(TypeError, range, 1, 2, 1.0) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
83 self.assertRaises(TypeError, range, 1e100, 1e101, 1e101) |
|
33047
c81ed079576f
Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents:
32535
diff
changeset
|
84 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
85 self.assertRaises(TypeError, range, 0, "spam") |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
86 self.assertRaises(TypeError, range, 0, 42, "spam") |
| 32535 | 87 |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
41804
diff
changeset
|
88 self.assertEqual(len(range(0, sys.maxsize, sys.maxsize-1)), 2) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
89 |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
41804
diff
changeset
|
90 r = range(-sys.maxsize, sys.maxsize, 2) |
|
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
41804
diff
changeset
|
91 self.assertEqual(len(r), sys.maxsize) |
|
33047
c81ed079576f
Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents:
32535
diff
changeset
|
92 |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
93 def test_large_operands(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
94 x = range(10**20, 10**20+10, 3) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
95 self.assertEqual(len(x), 4) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
96 self.assertEqual(len(list(x)), 4) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
97 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
98 x = range(10**20+10, 10**20, 3) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
99 self.assertEqual(len(x), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
100 self.assertEqual(len(list(x)), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
101 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
102 x = range(10**20, 10**20+10, -3) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
103 self.assertEqual(len(x), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
104 self.assertEqual(len(list(x)), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
105 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
106 x = range(10**20+10, 10**20, -3) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
107 self.assertEqual(len(x), 4) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
108 self.assertEqual(len(list(x)), 4) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
109 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
110 # Now test range() with longs |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
111 self.assertEqual(list(range(-2**100)), []) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
112 self.assertEqual(list(range(0, -2**100)), []) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
113 self.assertEqual(list(range(0, 2**100, -1)), []) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
114 self.assertEqual(list(range(0, 2**100, -1)), []) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
115 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
116 a = int(10 * sys.maxsize) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
117 b = int(100 * sys.maxsize) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
118 c = int(50 * sys.maxsize) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
119 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
120 self.assertEqual(list(range(a, a+2)), [a, a+1]) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
121 self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1]) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
122 self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2]) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
123 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
124 seq = list(range(a, b, c)) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
125 self.assertIn(a, seq) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
126 self.assertNotIn(b, seq) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
127 self.assertEqual(len(seq), 2) |
|
67543
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
128 self.assertEqual(seq[0], a) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
129 self.assertEqual(seq[-1], a+c) |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
130 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
131 seq = list(range(b, a, -c)) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
132 self.assertIn(b, seq) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
133 self.assertNotIn(a, seq) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
134 self.assertEqual(len(seq), 2) |
|
67543
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
135 self.assertEqual(seq[0], b) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
136 self.assertEqual(seq[-1], b-c) |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
137 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
138 seq = list(range(-a, -b, -c)) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
139 self.assertIn(-a, seq) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
140 self.assertNotIn(-b, seq) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
141 self.assertEqual(len(seq), 2) |
|
67543
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
142 self.assertEqual(seq[0], -a) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
143 self.assertEqual(seq[-1], -a-c) |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
144 |
|
67543
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
145 def test_large_range(self): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
146 # Check long ranges (len > sys.maxsize) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
147 # len() is expected to fail due to limitations of the __len__ protocol |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
148 def _range_len(x): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
149 try: |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
150 length = len(x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
151 except OverflowError: |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
152 step = x[1] - x[0] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
153 length = 1 + ((x[-1] - x[0]) // step) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
154 return length |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
155 a = -sys.maxsize |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
156 b = sys.maxsize |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
157 expected_len = b - a |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
158 x = range(a, b) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
159 self.assertIn(a, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
160 self.assertNotIn(b, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
161 self.assertRaises(OverflowError, len, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
162 self.assertEqual(_range_len(x), expected_len) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
163 self.assertEqual(x[0], a) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
164 idx = sys.maxsize+1 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
165 self.assertEqual(x[idx], a+idx) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
166 self.assertEqual(x[idx:idx+1][0], a+idx) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
167 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
168 x[-expected_len-1] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
169 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
170 x[expected_len] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
171 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
172 a = 0 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
173 b = 2 * sys.maxsize |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
174 expected_len = b - a |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
175 x = range(a, b) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
176 self.assertIn(a, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
177 self.assertNotIn(b, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
178 self.assertRaises(OverflowError, len, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
179 self.assertEqual(_range_len(x), expected_len) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
180 self.assertEqual(x[0], a) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
181 idx = sys.maxsize+1 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
182 self.assertEqual(x[idx], a+idx) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
183 self.assertEqual(x[idx:idx+1][0], a+idx) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
184 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
185 x[-expected_len-1] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
186 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
187 x[expected_len] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
188 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
189 a = 0 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
190 b = sys.maxsize**10 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
191 c = 2*sys.maxsize |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
192 expected_len = 1 + (b - a) // c |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
193 x = range(a, b, c) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
194 self.assertIn(a, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
195 self.assertNotIn(b, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
196 self.assertRaises(OverflowError, len, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
197 self.assertEqual(_range_len(x), expected_len) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
198 self.assertEqual(x[0], a) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
199 idx = sys.maxsize+1 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
200 self.assertEqual(x[idx], a+(idx*c)) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
201 self.assertEqual(x[idx:idx+1][0], a+(idx*c)) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
202 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
203 x[-expected_len-1] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
204 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
205 x[expected_len] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
206 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
207 a = sys.maxsize**10 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
208 b = 0 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
209 c = -2*sys.maxsize |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
210 expected_len = 1 + (b - a) // c |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
211 x = range(a, b, c) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
212 self.assertIn(a, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
213 self.assertNotIn(b, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
214 self.assertRaises(OverflowError, len, x) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
215 self.assertEqual(_range_len(x), expected_len) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
216 self.assertEqual(x[0], a) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
217 idx = sys.maxsize+1 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
218 self.assertEqual(x[idx], a+(idx*c)) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
219 self.assertEqual(x[idx:idx+1][0], a+(idx*c)) |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
220 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
221 x[-expected_len-1] |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
222 with self.assertRaises(IndexError): |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66641
diff
changeset
|
223 x[expected_len] |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
224 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
225 def test_invalid_invocation(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
226 self.assertRaises(TypeError, range) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
227 self.assertRaises(TypeError, range, 1, 2, 3, 4) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
228 self.assertRaises(ValueError, range, 1, 2, 0) |
|
66334
1b448fd7e1a2
fix two broken tests
Benjamin Peterson <benjamin@python.org>
parents:
66333
diff
changeset
|
229 a = int(10 * sys.maxsize) |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
230 self.assertRaises(ValueError, range, a, a + 1, int(0)) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
231 self.assertRaises(TypeError, range, 1., 1., 1.) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
232 self.assertRaises(TypeError, range, 1e100, 1e101, 1e101) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
233 self.assertRaises(TypeError, range, 0, "spam") |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
234 self.assertRaises(TypeError, range, 0, 42, "spam") |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
235 # Exercise various combinations of bad arguments, to check |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
236 # refcounting logic |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
237 self.assertRaises(TypeError, range, 0.0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
238 self.assertRaises(TypeError, range, 0, 0.0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
239 self.assertRaises(TypeError, range, 0.0, 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
240 self.assertRaises(TypeError, range, 0.0, 0.0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
241 self.assertRaises(TypeError, range, 0, 0, 1.0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
242 self.assertRaises(TypeError, range, 0, 0.0, 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
243 self.assertRaises(TypeError, range, 0, 0.0, 1.0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
244 self.assertRaises(TypeError, range, 0.0, 0, 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
245 self.assertRaises(TypeError, range, 0.0, 0, 1.0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
246 self.assertRaises(TypeError, range, 0.0, 0.0, 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
247 self.assertRaises(TypeError, range, 0.0, 0.0, 1.0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
248 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
249 def test_index(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
250 u = range(2) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
251 self.assertEqual(u.index(0), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
252 self.assertEqual(u.index(1), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
253 self.assertRaises(ValueError, u.index, 2) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
254 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
255 u = range(-2, 3) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
256 self.assertEqual(u.count(0), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
257 self.assertEqual(u.index(0), 2) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
258 self.assertRaises(TypeError, u.index) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
259 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
260 class BadExc(Exception): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
261 pass |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
262 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
263 class BadCmp: |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
264 def __eq__(self, other): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
265 if other == 2: |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
266 raise BadExc() |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
267 return False |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
268 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
269 a = range(4) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
270 self.assertRaises(BadExc, a.index, BadCmp()) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
271 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
272 a = range(-2, 3) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
273 self.assertEqual(a.index(0), 2) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
274 self.assertEqual(range(1, 10, 3).index(4), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
275 self.assertEqual(range(1, -10, -3).index(-5), 2) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
276 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
277 self.assertEqual(range(10**20).index(1), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
278 self.assertEqual(range(10**20).index(10**20 - 1), 10**20 - 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
279 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
280 self.assertRaises(ValueError, range(1, 2**100, 2).index, 2**87) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
281 self.assertEqual(range(1, 2**100, 2).index(2**87+1), 2**86) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
282 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
283 class AlwaysEqual(object): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
284 def __eq__(self, other): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
285 return True |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
286 always_equal = AlwaysEqual() |
|
66334
1b448fd7e1a2
fix two broken tests
Benjamin Peterson <benjamin@python.org>
parents:
66333
diff
changeset
|
287 self.assertEqual(range(10).index(always_equal), 0) |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
288 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
289 def test_user_index_method(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
290 bignum = 2*sys.maxsize |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
291 smallnum = 42 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
292 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
293 # User-defined class with an __index__ method |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
294 class I: |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
295 def __init__(self, n): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
296 self.n = int(n) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
297 def __index__(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
298 return self.n |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
299 self.assertEqual(list(range(I(bignum), I(bignum + 1))), [bignum]) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
300 self.assertEqual(list(range(I(smallnum), I(smallnum + 1))), [smallnum]) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
301 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
302 # User-defined class with a failing __index__ method |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
303 class IX: |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
304 def __index__(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
305 raise RuntimeError |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
306 self.assertRaises(RuntimeError, range, IX()) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
307 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
308 # User-defined class with an invalid __index__ method |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
309 class IN: |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
310 def __index__(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
311 return "not a number" |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
312 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
313 self.assertRaises(TypeError, range, IN()) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
314 |
|
80236
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
315 # Test use of user-defined classes in slice indices. |
|
80238
f02555353544
Issue #16402: Merge fix from 3.3
Mark Dickinson <mdickinson@enthought.com>
parents:
80237
diff
changeset
|
316 self.assertEqual(range(10)[:I(5)], range(5)) |
|
80236
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
317 |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
318 with self.assertRaises(RuntimeError): |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
319 range(0, 10)[:IX()] |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
320 |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
321 with self.assertRaises(TypeError): |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
322 range(0, 10)[:IN()] |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
323 |
|
66333
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
324 def test_count(self): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
325 self.assertEqual(range(3).count(-1), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
326 self.assertEqual(range(3).count(0), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
327 self.assertEqual(range(3).count(1), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
328 self.assertEqual(range(3).count(2), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
329 self.assertEqual(range(3).count(3), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
330 self.assertIs(type(range(3).count(-1)), int) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
331 self.assertIs(type(range(3).count(1)), int) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
332 self.assertEqual(range(10**20).count(1), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
333 self.assertEqual(range(10**20).count(10**20), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
334 self.assertEqual(range(3).index(1), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
335 self.assertEqual(range(1, 2**100, 2).count(2**87), 0) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
336 self.assertEqual(range(1, 2**100, 2).count(2**87+1), 1) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
337 |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
338 class AlwaysEqual(object): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
339 def __eq__(self, other): |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
340 return True |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
341 always_equal = AlwaysEqual() |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
342 self.assertEqual(range(10).count(always_equal), 10) |
|
257512b97f82
merge all range tests into test_range
Benjamin Peterson <benjamin@python.org>
parents:
66310
diff
changeset
|
343 |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
344 self.assertEqual(len(range(sys.maxsize, sys.maxsize+10)), 10) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
345 |
|
41786
ecc685925b52
Add a few simple repr tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41642
diff
changeset
|
346 def test_repr(self): |
|
41800
993f8a9b8ab3
repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents:
41786
diff
changeset
|
347 self.assertEqual(repr(range(1)), 'range(0, 1)') |
|
41786
ecc685925b52
Add a few simple repr tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41642
diff
changeset
|
348 self.assertEqual(repr(range(1, 2)), 'range(1, 2)') |
|
ecc685925b52
Add a few simple repr tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41642
diff
changeset
|
349 self.assertEqual(repr(range(1, 2, 3)), 'range(1, 2, 3)') |
|
ecc685925b52
Add a few simple repr tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41642
diff
changeset
|
350 |
|
47536
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47201
diff
changeset
|
351 def test_pickling(self): |
|
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47201
diff
changeset
|
352 testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
353 (13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)] |
|
49574
bbab11cdb175
Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
47536
diff
changeset
|
354 for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
|
47536
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47201
diff
changeset
|
355 for t in testcases: |
|
87446
458340ed0606
Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents:
80238
diff
changeset
|
356 with self.subTest(proto=proto, test=t): |
|
458340ed0606
Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents:
80238
diff
changeset
|
357 r = range(*t) |
|
458340ed0606
Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents:
80238
diff
changeset
|
358 self.assertEqual(list(pickle.loads(pickle.dumps(r, proto))), |
|
458340ed0606
Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents:
80238
diff
changeset
|
359 list(r)) |
|
47536
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47201
diff
changeset
|
360 |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
361 def test_iterator_pickling(self): |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
362 testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
363 (13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)] |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
364 for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
365 for t in testcases: |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
366 it = itorg = iter(range(*t)) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
367 data = list(range(*t)) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
368 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
369 d = pickle.dumps(it) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
370 it = pickle.loads(d) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
371 self.assertEqual(type(itorg), type(it)) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
372 self.assertEqual(list(it), data) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
373 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
374 it = pickle.loads(d) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
375 try: |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
376 next(it) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
377 except StopIteration: |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
378 continue |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
379 d = pickle.dumps(it) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
380 it = pickle.loads(d) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
381 self.assertEqual(list(it), data[1:]) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
382 |
|
54715
6492b6ece598
Fix SystemError and a wasps nest of ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
49574
diff
changeset
|
383 def test_odd_bug(self): |
|
6492b6ece598
Fix SystemError and a wasps nest of ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
49574
diff
changeset
|
384 # This used to raise a "SystemError: NULL result without error" |
|
6492b6ece598
Fix SystemError and a wasps nest of ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
49574
diff
changeset
|
385 # because the range validation step was eating the exception |
|
6492b6ece598
Fix SystemError and a wasps nest of ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
49574
diff
changeset
|
386 # before NULL was returned. |
|
6492b6ece598
Fix SystemError and a wasps nest of ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
49574
diff
changeset
|
387 with self.assertRaises(TypeError): |
|
6492b6ece598
Fix SystemError and a wasps nest of ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
49574
diff
changeset
|
388 range([], 1, -1) |
|
6492b6ece598
Fix SystemError and a wasps nest of ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
49574
diff
changeset
|
389 |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
390 def test_types(self): |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
391 # Non-integer objects *equal* to any of the range's items are supposed |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
392 # to be contained in the range. |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
393 self.assertIn(1.0, range(3)) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
394 self.assertIn(True, range(3)) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
395 self.assertIn(1+0j, range(3)) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
396 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
397 class C1: |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
398 def __eq__(self, other): return True |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
399 self.assertIn(C1(), range(3)) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
400 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
401 # Objects are never coerced into other types for comparison. |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
402 class C2: |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
403 def __int__(self): return 1 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
404 def __index__(self): return 1 |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
405 self.assertNotIn(C2(), range(3)) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
406 # ..except if explicitly told so. |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
407 self.assertIn(int(C2()), range(3)) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
408 |
|
56210
0f4e95ff859d
Issue #1766304: The range.__contains__ optimization should only be
Mark Dickinson <dickinsm@gmail.com>
parents:
56192
diff
changeset
|
409 # Check that the range.__contains__ optimization is only |
|
0f4e95ff859d
Issue #1766304: The range.__contains__ optimization should only be
Mark Dickinson <dickinsm@gmail.com>
parents:
56192
diff
changeset
|
410 # used for ints, not for instances of subclasses of int. |
|
0f4e95ff859d
Issue #1766304: The range.__contains__ optimization should only be
Mark Dickinson <dickinsm@gmail.com>
parents:
56192
diff
changeset
|
411 class C3(int): |
|
0f4e95ff859d
Issue #1766304: The range.__contains__ optimization should only be
Mark Dickinson <dickinsm@gmail.com>
parents:
56192
diff
changeset
|
412 def __eq__(self, other): return True |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
413 self.assertIn(C3(11), range(10)) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
414 self.assertIn(C3(11), list(range(10))) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
415 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
416 def test_strided_limits(self): |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
417 r = range(0, 101, 2) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
418 self.assertIn(0, r) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
419 self.assertNotIn(1, r) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
420 self.assertIn(2, r) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
421 self.assertNotIn(99, r) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
422 self.assertIn(100, r) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
423 self.assertNotIn(101, r) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
424 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
425 r = range(0, -20, -1) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
426 self.assertIn(0, r) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
427 self.assertIn(-1, r) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
428 self.assertIn(-19, r) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
429 self.assertNotIn(-20, r) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
430 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
431 r = range(0, -20, -2) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57285
diff
changeset
|
432 self.assertIn(-18, r) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
433 self.assertNotIn(-19, r) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
434 self.assertNotIn(-20, r) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
435 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
436 def test_empty(self): |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
437 r = range(0) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
438 self.assertNotIn(0, r) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
439 self.assertNotIn(1, r) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
440 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
441 r = range(0, -10) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
442 self.assertNotIn(0, r) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
443 self.assertNotIn(-1, r) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58415
diff
changeset
|
444 self.assertNotIn(1, r) |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
445 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
446 def test_range_iterators(self): |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
447 # exercise 'fast' iterators, that use a rangeiterobject internally. |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
448 # see issue 7298 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
449 limits = [base + jiggle |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
450 for M in (2**32, 2**64) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
451 for base in (-M, -M//2, 0, M//2, M) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
452 for jiggle in (-2, -1, 0, 1, 2)] |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
453 test_ranges = [(start, end, step) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
454 for start in limits |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
455 for end in limits |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
456 for step in (-2**63, -2**31, -2, -1, 1, 2)] |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
457 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
458 for start, end, step in test_ranges: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
459 iter1 = range(start, end, step) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
460 iter2 = pyrange(start, end, step) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
461 test_id = "range({}, {}, {})".format(start, end, step) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
462 # check first 100 entries |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
463 self.assert_iterators_equal(iter1, iter2, test_id, limit=100) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
464 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
465 iter1 = reversed(range(start, end, step)) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
466 iter2 = pyrange_reversed(start, end, step) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
467 test_id = "reversed(range({}, {}, {}))".format(start, end, step) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
468 self.assert_iterators_equal(iter1, iter2, test_id, limit=100) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55593
diff
changeset
|
469 |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
470 def test_slice(self): |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
471 def check(start, stop, step=None): |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
472 i = slice(start, stop, step) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
473 self.assertEqual(list(r[i]), list(r)[i]) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
474 self.assertEqual(len(r[i]), len(list(r)[i])) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
475 for r in [range(10), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
476 range(0), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
477 range(1, 9, 3), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
478 range(8, 0, -3), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
479 range(sys.maxsize+1, sys.maxsize+10), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
480 ]: |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
481 check(0, 2) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
482 check(0, 20) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
483 check(1, 2) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
484 check(20, 30) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
485 check(-30, -20) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
486 check(-1, 100, 2) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
487 check(0, -1) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
488 check(-1, -3, -1) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
489 |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
490 def test_contains(self): |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
491 r = range(10) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
492 self.assertIn(0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
493 self.assertIn(1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
494 self.assertIn(5.0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
495 self.assertNotIn(5.1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
496 self.assertNotIn(-1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
497 self.assertNotIn(10, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
498 self.assertNotIn("", r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
499 r = range(9, -1, -1) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
500 self.assertIn(0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
501 self.assertIn(1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
502 self.assertIn(5.0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
503 self.assertNotIn(5.1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
504 self.assertNotIn(-1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
505 self.assertNotIn(10, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
506 self.assertNotIn("", r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
507 r = range(0, 10, 2) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
508 self.assertIn(0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
509 self.assertNotIn(1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
510 self.assertNotIn(5.0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
511 self.assertNotIn(5.1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
512 self.assertNotIn(-1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
513 self.assertNotIn(10, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
514 self.assertNotIn("", r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
515 r = range(9, -1, -2) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
516 self.assertNotIn(0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
517 self.assertIn(1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
518 self.assertIn(5.0, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
519 self.assertNotIn(5.1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
520 self.assertNotIn(-1, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
521 self.assertNotIn(10, r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
522 self.assertNotIn("", r) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
523 |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
524 def test_reverse_iteration(self): |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
525 for r in [range(10), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
526 range(0), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
527 range(1, 9, 3), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
528 range(8, 0, -3), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
529 range(sys.maxsize+1, sys.maxsize+10), |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
530 ]: |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
531 self.assertEqual(list(reversed(r)), list(r)[::-1]) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
532 |
|
69359
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
533 def test_issue11845(self): |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
534 r = range(*slice(1, 18, 2).indices(20)) |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
535 values = {None, 0, 1, -1, 2, -2, 5, -5, 19, -19, |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
536 20, -20, 21, -21, 30, -30, 99, -99} |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
537 for i in values: |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
538 for j in values: |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
539 for k in values - {0}: |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
540 r[i:j:k] |
|
8025fb83dece
#11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
541 |
|
73072
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
542 def test_comparison(self): |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
543 test_ranges = [range(0), range(0, -1), range(1, 1, 3), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
544 range(1), range(5, 6), range(5, 6, 2), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
545 range(5, 7, 2), range(2), range(0, 4, 2), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
546 range(0, 5, 2), range(0, 6, 2)] |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
547 test_tuples = list(map(tuple, test_ranges)) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
548 |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
549 # Check that equality of ranges matches equality of the corresponding |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
550 # tuples for each pair from the test lists above. |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
551 ranges_eq = [a == b for a in test_ranges for b in test_ranges] |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
552 tuples_eq = [a == b for a in test_tuples for b in test_tuples] |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
553 self.assertEqual(ranges_eq, tuples_eq) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
554 |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
555 # Check that != correctly gives the logical negation of == |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
556 ranges_ne = [a != b for a in test_ranges for b in test_ranges] |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
557 self.assertEqual(ranges_ne, [not x for x in ranges_eq]) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
558 |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
559 # Equal ranges should have equal hashes. |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
560 for a in test_ranges: |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
561 for b in test_ranges: |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
562 if a == b: |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
563 self.assertEqual(hash(a), hash(b)) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
564 |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
565 # Ranges are unequal to other types (even sequence types) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
566 self.assertIs(range(0) == (), False) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
567 self.assertIs(() == range(0), False) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
568 self.assertIs(range(2) == [0, 1], False) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
569 |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
570 # Huge integers aren't a problem. |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
571 self.assertEqual(range(0, 2**100 - 1, 2), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
572 range(0, 2**100, 2)) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
573 self.assertEqual(hash(range(0, 2**100 - 1, 2)), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
574 hash(range(0, 2**100, 2))) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
575 self.assertNotEqual(range(0, 2**100, 2), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
576 range(0, 2**100 + 1, 2)) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
577 self.assertEqual(range(2**200, 2**201 - 2**99, 2**100), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
578 range(2**200, 2**201, 2**100)) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
579 self.assertEqual(hash(range(2**200, 2**201 - 2**99, 2**100)), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
580 hash(range(2**200, 2**201, 2**100))) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
581 self.assertNotEqual(range(2**200, 2**201, 2**100), |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
582 range(2**200, 2**201 + 1, 2**100)) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
583 |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
584 # Order comparisons are not implemented for ranges. |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
585 with self.assertRaises(TypeError): |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
586 range(0) < range(0) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
587 with self.assertRaises(TypeError): |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
588 range(0) > range(0) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
589 with self.assertRaises(TypeError): |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
590 range(0) <= range(0) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
591 with self.assertRaises(TypeError): |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
592 range(0) >= range(0) |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
593 |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
594 |
|
73378
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
595 def test_attributes(self): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
596 # test the start, stop and step attributes of range objects |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
597 self.assert_attrs(range(0), 0, 0, 1) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
598 self.assert_attrs(range(10), 0, 10, 1) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
599 self.assert_attrs(range(-10), 0, -10, 1) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
600 self.assert_attrs(range(0, 10, 1), 0, 10, 1) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
601 self.assert_attrs(range(0, 10, 3), 0, 10, 3) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
602 self.assert_attrs(range(10, 0, -1), 10, 0, -1) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
603 self.assert_attrs(range(10, 0, -3), 10, 0, -3) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
604 |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
605 def assert_attrs(self, rangeobj, start, stop, step): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
606 self.assertEqual(rangeobj.start, start) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
607 self.assertEqual(rangeobj.stop, stop) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
608 self.assertEqual(rangeobj.step, step) |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
609 |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
610 with self.assertRaises(AttributeError): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
611 rangeobj.start = 0 |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
612 with self.assertRaises(AttributeError): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
613 rangeobj.stop = 10 |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
614 with self.assertRaises(AttributeError): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
615 rangeobj.step = 1 |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
616 |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
617 with self.assertRaises(AttributeError): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
618 del rangeobj.start |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
619 with self.assertRaises(AttributeError): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
620 del rangeobj.stop |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
621 with self.assertRaises(AttributeError): |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
622 del rangeobj.step |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66334
diff
changeset
|
623 |
| 32535 | 624 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
44160
diff
changeset
|
625 test.support.run_unittest(RangeTest) |
| 32535 | 626 |
| 627 if __name__ == "__main__": | |
| 628 test_main() |
