1 | n/a | # Python test set -- built-in functions |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | import sys |
---|
5 | n/a | import pickle |
---|
6 | n/a | import itertools |
---|
7 | n/a | |
---|
8 | n/a | # pure Python implementations (3 args only), for comparison |
---|
9 | n/a | def pyrange(start, stop, step): |
---|
10 | n/a | if (start - stop) // step < 0: |
---|
11 | n/a | # replace stop with next element in the sequence of integers |
---|
12 | n/a | # that are congruent to start modulo step. |
---|
13 | n/a | stop += (start - stop) % step |
---|
14 | n/a | while start != stop: |
---|
15 | n/a | yield start |
---|
16 | n/a | start += step |
---|
17 | n/a | |
---|
18 | n/a | def pyrange_reversed(start, stop, step): |
---|
19 | n/a | stop += (start - stop) % step |
---|
20 | n/a | return pyrange(stop - step, start - step, -step) |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | class RangeTest(unittest.TestCase): |
---|
24 | n/a | def assert_iterators_equal(self, xs, ys, test_id, limit=None): |
---|
25 | n/a | # check that an iterator xs matches the expected results ys, |
---|
26 | n/a | # up to a given limit. |
---|
27 | n/a | if limit is not None: |
---|
28 | n/a | xs = itertools.islice(xs, limit) |
---|
29 | n/a | ys = itertools.islice(ys, limit) |
---|
30 | n/a | sentinel = object() |
---|
31 | n/a | pairs = itertools.zip_longest(xs, ys, fillvalue=sentinel) |
---|
32 | n/a | for i, (x, y) in enumerate(pairs): |
---|
33 | n/a | if x == y: |
---|
34 | n/a | continue |
---|
35 | n/a | elif x == sentinel: |
---|
36 | n/a | self.fail('{}: iterator ended unexpectedly ' |
---|
37 | n/a | 'at position {}; expected {}'.format(test_id, i, y)) |
---|
38 | n/a | elif y == sentinel: |
---|
39 | n/a | self.fail('{}: unexpected excess element {} at ' |
---|
40 | n/a | 'position {}'.format(test_id, x, i)) |
---|
41 | n/a | else: |
---|
42 | n/a | self.fail('{}: wrong element at position {};' |
---|
43 | n/a | 'expected {}, got {}'.format(test_id, i, y, x)) |
---|
44 | n/a | |
---|
45 | n/a | def test_range(self): |
---|
46 | n/a | self.assertEqual(list(range(3)), [0, 1, 2]) |
---|
47 | n/a | self.assertEqual(list(range(1, 5)), [1, 2, 3, 4]) |
---|
48 | n/a | self.assertEqual(list(range(0)), []) |
---|
49 | n/a | self.assertEqual(list(range(-3)), []) |
---|
50 | n/a | self.assertEqual(list(range(1, 10, 3)), [1, 4, 7]) |
---|
51 | n/a | self.assertEqual(list(range(5, -5, -3)), [5, 2, -1, -4]) |
---|
52 | n/a | |
---|
53 | n/a | a = 10 |
---|
54 | n/a | b = 100 |
---|
55 | n/a | c = 50 |
---|
56 | n/a | |
---|
57 | n/a | self.assertEqual(list(range(a, a+2)), [a, a+1]) |
---|
58 | n/a | self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1]) |
---|
59 | n/a | self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2]) |
---|
60 | n/a | |
---|
61 | n/a | seq = list(range(a, b, c)) |
---|
62 | n/a | self.assertIn(a, seq) |
---|
63 | n/a | self.assertNotIn(b, seq) |
---|
64 | n/a | self.assertEqual(len(seq), 2) |
---|
65 | n/a | |
---|
66 | n/a | seq = list(range(b, a, -c)) |
---|
67 | n/a | self.assertIn(b, seq) |
---|
68 | n/a | self.assertNotIn(a, seq) |
---|
69 | n/a | self.assertEqual(len(seq), 2) |
---|
70 | n/a | |
---|
71 | n/a | seq = list(range(-a, -b, -c)) |
---|
72 | n/a | self.assertIn(-a, seq) |
---|
73 | n/a | self.assertNotIn(-b, seq) |
---|
74 | n/a | self.assertEqual(len(seq), 2) |
---|
75 | n/a | |
---|
76 | n/a | self.assertRaises(TypeError, range) |
---|
77 | n/a | self.assertRaises(TypeError, range, 1, 2, 3, 4) |
---|
78 | n/a | self.assertRaises(ValueError, range, 1, 2, 0) |
---|
79 | n/a | |
---|
80 | n/a | self.assertRaises(TypeError, range, 0.0, 2, 1) |
---|
81 | n/a | self.assertRaises(TypeError, range, 1, 2.0, 1) |
---|
82 | n/a | self.assertRaises(TypeError, range, 1, 2, 1.0) |
---|
83 | n/a | self.assertRaises(TypeError, range, 1e100, 1e101, 1e101) |
---|
84 | n/a | |
---|
85 | n/a | self.assertRaises(TypeError, range, 0, "spam") |
---|
86 | n/a | self.assertRaises(TypeError, range, 0, 42, "spam") |
---|
87 | n/a | |
---|
88 | n/a | self.assertEqual(len(range(0, sys.maxsize, sys.maxsize-1)), 2) |
---|
89 | n/a | |
---|
90 | n/a | r = range(-sys.maxsize, sys.maxsize, 2) |
---|
91 | n/a | self.assertEqual(len(r), sys.maxsize) |
---|
92 | n/a | |
---|
93 | n/a | def test_large_operands(self): |
---|
94 | n/a | x = range(10**20, 10**20+10, 3) |
---|
95 | n/a | self.assertEqual(len(x), 4) |
---|
96 | n/a | self.assertEqual(len(list(x)), 4) |
---|
97 | n/a | |
---|
98 | n/a | x = range(10**20+10, 10**20, 3) |
---|
99 | n/a | self.assertEqual(len(x), 0) |
---|
100 | n/a | self.assertEqual(len(list(x)), 0) |
---|
101 | n/a | |
---|
102 | n/a | x = range(10**20, 10**20+10, -3) |
---|
103 | n/a | self.assertEqual(len(x), 0) |
---|
104 | n/a | self.assertEqual(len(list(x)), 0) |
---|
105 | n/a | |
---|
106 | n/a | x = range(10**20+10, 10**20, -3) |
---|
107 | n/a | self.assertEqual(len(x), 4) |
---|
108 | n/a | self.assertEqual(len(list(x)), 4) |
---|
109 | n/a | |
---|
110 | n/a | # Now test range() with longs |
---|
111 | n/a | self.assertEqual(list(range(-2**100)), []) |
---|
112 | n/a | self.assertEqual(list(range(0, -2**100)), []) |
---|
113 | n/a | self.assertEqual(list(range(0, 2**100, -1)), []) |
---|
114 | n/a | self.assertEqual(list(range(0, 2**100, -1)), []) |
---|
115 | n/a | |
---|
116 | n/a | a = int(10 * sys.maxsize) |
---|
117 | n/a | b = int(100 * sys.maxsize) |
---|
118 | n/a | c = int(50 * sys.maxsize) |
---|
119 | n/a | |
---|
120 | n/a | self.assertEqual(list(range(a, a+2)), [a, a+1]) |
---|
121 | n/a | self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1]) |
---|
122 | n/a | self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2]) |
---|
123 | n/a | |
---|
124 | n/a | seq = list(range(a, b, c)) |
---|
125 | n/a | self.assertIn(a, seq) |
---|
126 | n/a | self.assertNotIn(b, seq) |
---|
127 | n/a | self.assertEqual(len(seq), 2) |
---|
128 | n/a | self.assertEqual(seq[0], a) |
---|
129 | n/a | self.assertEqual(seq[-1], a+c) |
---|
130 | n/a | |
---|
131 | n/a | seq = list(range(b, a, -c)) |
---|
132 | n/a | self.assertIn(b, seq) |
---|
133 | n/a | self.assertNotIn(a, seq) |
---|
134 | n/a | self.assertEqual(len(seq), 2) |
---|
135 | n/a | self.assertEqual(seq[0], b) |
---|
136 | n/a | self.assertEqual(seq[-1], b-c) |
---|
137 | n/a | |
---|
138 | n/a | seq = list(range(-a, -b, -c)) |
---|
139 | n/a | self.assertIn(-a, seq) |
---|
140 | n/a | self.assertNotIn(-b, seq) |
---|
141 | n/a | self.assertEqual(len(seq), 2) |
---|
142 | n/a | self.assertEqual(seq[0], -a) |
---|
143 | n/a | self.assertEqual(seq[-1], -a-c) |
---|
144 | n/a | |
---|
145 | n/a | def test_large_range(self): |
---|
146 | n/a | # Check long ranges (len > sys.maxsize) |
---|
147 | n/a | # len() is expected to fail due to limitations of the __len__ protocol |
---|
148 | n/a | def _range_len(x): |
---|
149 | n/a | try: |
---|
150 | n/a | length = len(x) |
---|
151 | n/a | except OverflowError: |
---|
152 | n/a | step = x[1] - x[0] |
---|
153 | n/a | length = 1 + ((x[-1] - x[0]) // step) |
---|
154 | n/a | return length |
---|
155 | n/a | a = -sys.maxsize |
---|
156 | n/a | b = sys.maxsize |
---|
157 | n/a | expected_len = b - a |
---|
158 | n/a | x = range(a, b) |
---|
159 | n/a | self.assertIn(a, x) |
---|
160 | n/a | self.assertNotIn(b, x) |
---|
161 | n/a | self.assertRaises(OverflowError, len, x) |
---|
162 | n/a | self.assertEqual(_range_len(x), expected_len) |
---|
163 | n/a | self.assertEqual(x[0], a) |
---|
164 | n/a | idx = sys.maxsize+1 |
---|
165 | n/a | self.assertEqual(x[idx], a+idx) |
---|
166 | n/a | self.assertEqual(x[idx:idx+1][0], a+idx) |
---|
167 | n/a | with self.assertRaises(IndexError): |
---|
168 | n/a | x[-expected_len-1] |
---|
169 | n/a | with self.assertRaises(IndexError): |
---|
170 | n/a | x[expected_len] |
---|
171 | n/a | |
---|
172 | n/a | a = 0 |
---|
173 | n/a | b = 2 * sys.maxsize |
---|
174 | n/a | expected_len = b - a |
---|
175 | n/a | x = range(a, b) |
---|
176 | n/a | self.assertIn(a, x) |
---|
177 | n/a | self.assertNotIn(b, x) |
---|
178 | n/a | self.assertRaises(OverflowError, len, x) |
---|
179 | n/a | self.assertEqual(_range_len(x), expected_len) |
---|
180 | n/a | self.assertEqual(x[0], a) |
---|
181 | n/a | idx = sys.maxsize+1 |
---|
182 | n/a | self.assertEqual(x[idx], a+idx) |
---|
183 | n/a | self.assertEqual(x[idx:idx+1][0], a+idx) |
---|
184 | n/a | with self.assertRaises(IndexError): |
---|
185 | n/a | x[-expected_len-1] |
---|
186 | n/a | with self.assertRaises(IndexError): |
---|
187 | n/a | x[expected_len] |
---|
188 | n/a | |
---|
189 | n/a | a = 0 |
---|
190 | n/a | b = sys.maxsize**10 |
---|
191 | n/a | c = 2*sys.maxsize |
---|
192 | n/a | expected_len = 1 + (b - a) // c |
---|
193 | n/a | x = range(a, b, c) |
---|
194 | n/a | self.assertIn(a, x) |
---|
195 | n/a | self.assertNotIn(b, x) |
---|
196 | n/a | self.assertRaises(OverflowError, len, x) |
---|
197 | n/a | self.assertEqual(_range_len(x), expected_len) |
---|
198 | n/a | self.assertEqual(x[0], a) |
---|
199 | n/a | idx = sys.maxsize+1 |
---|
200 | n/a | self.assertEqual(x[idx], a+(idx*c)) |
---|
201 | n/a | self.assertEqual(x[idx:idx+1][0], a+(idx*c)) |
---|
202 | n/a | with self.assertRaises(IndexError): |
---|
203 | n/a | x[-expected_len-1] |
---|
204 | n/a | with self.assertRaises(IndexError): |
---|
205 | n/a | x[expected_len] |
---|
206 | n/a | |
---|
207 | n/a | a = sys.maxsize**10 |
---|
208 | n/a | b = 0 |
---|
209 | n/a | c = -2*sys.maxsize |
---|
210 | n/a | expected_len = 1 + (b - a) // c |
---|
211 | n/a | x = range(a, b, c) |
---|
212 | n/a | self.assertIn(a, x) |
---|
213 | n/a | self.assertNotIn(b, x) |
---|
214 | n/a | self.assertRaises(OverflowError, len, x) |
---|
215 | n/a | self.assertEqual(_range_len(x), expected_len) |
---|
216 | n/a | self.assertEqual(x[0], a) |
---|
217 | n/a | idx = sys.maxsize+1 |
---|
218 | n/a | self.assertEqual(x[idx], a+(idx*c)) |
---|
219 | n/a | self.assertEqual(x[idx:idx+1][0], a+(idx*c)) |
---|
220 | n/a | with self.assertRaises(IndexError): |
---|
221 | n/a | x[-expected_len-1] |
---|
222 | n/a | with self.assertRaises(IndexError): |
---|
223 | n/a | x[expected_len] |
---|
224 | n/a | |
---|
225 | n/a | def test_invalid_invocation(self): |
---|
226 | n/a | self.assertRaises(TypeError, range) |
---|
227 | n/a | self.assertRaises(TypeError, range, 1, 2, 3, 4) |
---|
228 | n/a | self.assertRaises(ValueError, range, 1, 2, 0) |
---|
229 | n/a | a = int(10 * sys.maxsize) |
---|
230 | n/a | self.assertRaises(ValueError, range, a, a + 1, int(0)) |
---|
231 | n/a | self.assertRaises(TypeError, range, 1., 1., 1.) |
---|
232 | n/a | self.assertRaises(TypeError, range, 1e100, 1e101, 1e101) |
---|
233 | n/a | self.assertRaises(TypeError, range, 0, "spam") |
---|
234 | n/a | self.assertRaises(TypeError, range, 0, 42, "spam") |
---|
235 | n/a | # Exercise various combinations of bad arguments, to check |
---|
236 | n/a | # refcounting logic |
---|
237 | n/a | self.assertRaises(TypeError, range, 0.0) |
---|
238 | n/a | self.assertRaises(TypeError, range, 0, 0.0) |
---|
239 | n/a | self.assertRaises(TypeError, range, 0.0, 0) |
---|
240 | n/a | self.assertRaises(TypeError, range, 0.0, 0.0) |
---|
241 | n/a | self.assertRaises(TypeError, range, 0, 0, 1.0) |
---|
242 | n/a | self.assertRaises(TypeError, range, 0, 0.0, 1) |
---|
243 | n/a | self.assertRaises(TypeError, range, 0, 0.0, 1.0) |
---|
244 | n/a | self.assertRaises(TypeError, range, 0.0, 0, 1) |
---|
245 | n/a | self.assertRaises(TypeError, range, 0.0, 0, 1.0) |
---|
246 | n/a | self.assertRaises(TypeError, range, 0.0, 0.0, 1) |
---|
247 | n/a | self.assertRaises(TypeError, range, 0.0, 0.0, 1.0) |
---|
248 | n/a | |
---|
249 | n/a | def test_index(self): |
---|
250 | n/a | u = range(2) |
---|
251 | n/a | self.assertEqual(u.index(0), 0) |
---|
252 | n/a | self.assertEqual(u.index(1), 1) |
---|
253 | n/a | self.assertRaises(ValueError, u.index, 2) |
---|
254 | n/a | |
---|
255 | n/a | u = range(-2, 3) |
---|
256 | n/a | self.assertEqual(u.count(0), 1) |
---|
257 | n/a | self.assertEqual(u.index(0), 2) |
---|
258 | n/a | self.assertRaises(TypeError, u.index) |
---|
259 | n/a | |
---|
260 | n/a | class BadExc(Exception): |
---|
261 | n/a | pass |
---|
262 | n/a | |
---|
263 | n/a | class BadCmp: |
---|
264 | n/a | def __eq__(self, other): |
---|
265 | n/a | if other == 2: |
---|
266 | n/a | raise BadExc() |
---|
267 | n/a | return False |
---|
268 | n/a | |
---|
269 | n/a | a = range(4) |
---|
270 | n/a | self.assertRaises(BadExc, a.index, BadCmp()) |
---|
271 | n/a | |
---|
272 | n/a | a = range(-2, 3) |
---|
273 | n/a | self.assertEqual(a.index(0), 2) |
---|
274 | n/a | self.assertEqual(range(1, 10, 3).index(4), 1) |
---|
275 | n/a | self.assertEqual(range(1, -10, -3).index(-5), 2) |
---|
276 | n/a | |
---|
277 | n/a | self.assertEqual(range(10**20).index(1), 1) |
---|
278 | n/a | self.assertEqual(range(10**20).index(10**20 - 1), 10**20 - 1) |
---|
279 | n/a | |
---|
280 | n/a | self.assertRaises(ValueError, range(1, 2**100, 2).index, 2**87) |
---|
281 | n/a | self.assertEqual(range(1, 2**100, 2).index(2**87+1), 2**86) |
---|
282 | n/a | |
---|
283 | n/a | class AlwaysEqual(object): |
---|
284 | n/a | def __eq__(self, other): |
---|
285 | n/a | return True |
---|
286 | n/a | always_equal = AlwaysEqual() |
---|
287 | n/a | self.assertEqual(range(10).index(always_equal), 0) |
---|
288 | n/a | |
---|
289 | n/a | def test_user_index_method(self): |
---|
290 | n/a | bignum = 2*sys.maxsize |
---|
291 | n/a | smallnum = 42 |
---|
292 | n/a | |
---|
293 | n/a | # User-defined class with an __index__ method |
---|
294 | n/a | class I: |
---|
295 | n/a | def __init__(self, n): |
---|
296 | n/a | self.n = int(n) |
---|
297 | n/a | def __index__(self): |
---|
298 | n/a | return self.n |
---|
299 | n/a | self.assertEqual(list(range(I(bignum), I(bignum + 1))), [bignum]) |
---|
300 | n/a | self.assertEqual(list(range(I(smallnum), I(smallnum + 1))), [smallnum]) |
---|
301 | n/a | |
---|
302 | n/a | # User-defined class with a failing __index__ method |
---|
303 | n/a | class IX: |
---|
304 | n/a | def __index__(self): |
---|
305 | n/a | raise RuntimeError |
---|
306 | n/a | self.assertRaises(RuntimeError, range, IX()) |
---|
307 | n/a | |
---|
308 | n/a | # User-defined class with an invalid __index__ method |
---|
309 | n/a | class IN: |
---|
310 | n/a | def __index__(self): |
---|
311 | n/a | return "not a number" |
---|
312 | n/a | |
---|
313 | n/a | self.assertRaises(TypeError, range, IN()) |
---|
314 | n/a | |
---|
315 | n/a | # Test use of user-defined classes in slice indices. |
---|
316 | n/a | self.assertEqual(range(10)[:I(5)], range(5)) |
---|
317 | n/a | |
---|
318 | n/a | with self.assertRaises(RuntimeError): |
---|
319 | n/a | range(0, 10)[:IX()] |
---|
320 | n/a | |
---|
321 | n/a | with self.assertRaises(TypeError): |
---|
322 | n/a | range(0, 10)[:IN()] |
---|
323 | n/a | |
---|
324 | n/a | def test_count(self): |
---|
325 | n/a | self.assertEqual(range(3).count(-1), 0) |
---|
326 | n/a | self.assertEqual(range(3).count(0), 1) |
---|
327 | n/a | self.assertEqual(range(3).count(1), 1) |
---|
328 | n/a | self.assertEqual(range(3).count(2), 1) |
---|
329 | n/a | self.assertEqual(range(3).count(3), 0) |
---|
330 | n/a | self.assertIs(type(range(3).count(-1)), int) |
---|
331 | n/a | self.assertIs(type(range(3).count(1)), int) |
---|
332 | n/a | self.assertEqual(range(10**20).count(1), 1) |
---|
333 | n/a | self.assertEqual(range(10**20).count(10**20), 0) |
---|
334 | n/a | self.assertEqual(range(3).index(1), 1) |
---|
335 | n/a | self.assertEqual(range(1, 2**100, 2).count(2**87), 0) |
---|
336 | n/a | self.assertEqual(range(1, 2**100, 2).count(2**87+1), 1) |
---|
337 | n/a | |
---|
338 | n/a | class AlwaysEqual(object): |
---|
339 | n/a | def __eq__(self, other): |
---|
340 | n/a | return True |
---|
341 | n/a | always_equal = AlwaysEqual() |
---|
342 | n/a | self.assertEqual(range(10).count(always_equal), 10) |
---|
343 | n/a | |
---|
344 | n/a | self.assertEqual(len(range(sys.maxsize, sys.maxsize+10)), 10) |
---|
345 | n/a | |
---|
346 | n/a | def test_repr(self): |
---|
347 | n/a | self.assertEqual(repr(range(1)), 'range(0, 1)') |
---|
348 | n/a | self.assertEqual(repr(range(1, 2)), 'range(1, 2)') |
---|
349 | n/a | self.assertEqual(repr(range(1, 2, 3)), 'range(1, 2, 3)') |
---|
350 | n/a | |
---|
351 | n/a | def test_pickling(self): |
---|
352 | n/a | testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), |
---|
353 | n/a | (13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)] |
---|
354 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
355 | n/a | for t in testcases: |
---|
356 | n/a | with self.subTest(proto=proto, test=t): |
---|
357 | n/a | r = range(*t) |
---|
358 | n/a | self.assertEqual(list(pickle.loads(pickle.dumps(r, proto))), |
---|
359 | n/a | list(r)) |
---|
360 | n/a | |
---|
361 | n/a | def test_iterator_pickling(self): |
---|
362 | n/a | testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1), |
---|
363 | n/a | (13, 21, 3), (-2, 2, 2), (2**65, 2**65+2)] |
---|
364 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
365 | n/a | for t in testcases: |
---|
366 | n/a | it = itorg = iter(range(*t)) |
---|
367 | n/a | data = list(range(*t)) |
---|
368 | n/a | |
---|
369 | n/a | d = pickle.dumps(it, proto) |
---|
370 | n/a | it = pickle.loads(d) |
---|
371 | n/a | self.assertEqual(type(itorg), type(it)) |
---|
372 | n/a | self.assertEqual(list(it), data) |
---|
373 | n/a | |
---|
374 | n/a | it = pickle.loads(d) |
---|
375 | n/a | try: |
---|
376 | n/a | next(it) |
---|
377 | n/a | except StopIteration: |
---|
378 | n/a | continue |
---|
379 | n/a | d = pickle.dumps(it, proto) |
---|
380 | n/a | it = pickle.loads(d) |
---|
381 | n/a | self.assertEqual(list(it), data[1:]) |
---|
382 | n/a | |
---|
383 | n/a | def test_exhausted_iterator_pickling(self): |
---|
384 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
385 | n/a | r = range(2**65, 2**65+2) |
---|
386 | n/a | i = iter(r) |
---|
387 | n/a | while True: |
---|
388 | n/a | r = next(i) |
---|
389 | n/a | if r == 2**65+1: |
---|
390 | n/a | break |
---|
391 | n/a | d = pickle.dumps(i, proto) |
---|
392 | n/a | i2 = pickle.loads(d) |
---|
393 | n/a | self.assertEqual(list(i), []) |
---|
394 | n/a | self.assertEqual(list(i2), []) |
---|
395 | n/a | |
---|
396 | n/a | def test_large_exhausted_iterator_pickling(self): |
---|
397 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
398 | n/a | r = range(20) |
---|
399 | n/a | i = iter(r) |
---|
400 | n/a | while True: |
---|
401 | n/a | r = next(i) |
---|
402 | n/a | if r == 19: |
---|
403 | n/a | break |
---|
404 | n/a | d = pickle.dumps(i, proto) |
---|
405 | n/a | i2 = pickle.loads(d) |
---|
406 | n/a | self.assertEqual(list(i), []) |
---|
407 | n/a | self.assertEqual(list(i2), []) |
---|
408 | n/a | |
---|
409 | n/a | def test_odd_bug(self): |
---|
410 | n/a | # This used to raise a "SystemError: NULL result without error" |
---|
411 | n/a | # because the range validation step was eating the exception |
---|
412 | n/a | # before NULL was returned. |
---|
413 | n/a | with self.assertRaises(TypeError): |
---|
414 | n/a | range([], 1, -1) |
---|
415 | n/a | |
---|
416 | n/a | def test_types(self): |
---|
417 | n/a | # Non-integer objects *equal* to any of the range's items are supposed |
---|
418 | n/a | # to be contained in the range. |
---|
419 | n/a | self.assertIn(1.0, range(3)) |
---|
420 | n/a | self.assertIn(True, range(3)) |
---|
421 | n/a | self.assertIn(1+0j, range(3)) |
---|
422 | n/a | |
---|
423 | n/a | class C1: |
---|
424 | n/a | def __eq__(self, other): return True |
---|
425 | n/a | self.assertIn(C1(), range(3)) |
---|
426 | n/a | |
---|
427 | n/a | # Objects are never coerced into other types for comparison. |
---|
428 | n/a | class C2: |
---|
429 | n/a | def __int__(self): return 1 |
---|
430 | n/a | def __index__(self): return 1 |
---|
431 | n/a | self.assertNotIn(C2(), range(3)) |
---|
432 | n/a | # ..except if explicitly told so. |
---|
433 | n/a | self.assertIn(int(C2()), range(3)) |
---|
434 | n/a | |
---|
435 | n/a | # Check that the range.__contains__ optimization is only |
---|
436 | n/a | # used for ints, not for instances of subclasses of int. |
---|
437 | n/a | class C3(int): |
---|
438 | n/a | def __eq__(self, other): return True |
---|
439 | n/a | self.assertIn(C3(11), range(10)) |
---|
440 | n/a | self.assertIn(C3(11), list(range(10))) |
---|
441 | n/a | |
---|
442 | n/a | def test_strided_limits(self): |
---|
443 | n/a | r = range(0, 101, 2) |
---|
444 | n/a | self.assertIn(0, r) |
---|
445 | n/a | self.assertNotIn(1, r) |
---|
446 | n/a | self.assertIn(2, r) |
---|
447 | n/a | self.assertNotIn(99, r) |
---|
448 | n/a | self.assertIn(100, r) |
---|
449 | n/a | self.assertNotIn(101, r) |
---|
450 | n/a | |
---|
451 | n/a | r = range(0, -20, -1) |
---|
452 | n/a | self.assertIn(0, r) |
---|
453 | n/a | self.assertIn(-1, r) |
---|
454 | n/a | self.assertIn(-19, r) |
---|
455 | n/a | self.assertNotIn(-20, r) |
---|
456 | n/a | |
---|
457 | n/a | r = range(0, -20, -2) |
---|
458 | n/a | self.assertIn(-18, r) |
---|
459 | n/a | self.assertNotIn(-19, r) |
---|
460 | n/a | self.assertNotIn(-20, r) |
---|
461 | n/a | |
---|
462 | n/a | def test_empty(self): |
---|
463 | n/a | r = range(0) |
---|
464 | n/a | self.assertNotIn(0, r) |
---|
465 | n/a | self.assertNotIn(1, r) |
---|
466 | n/a | |
---|
467 | n/a | r = range(0, -10) |
---|
468 | n/a | self.assertNotIn(0, r) |
---|
469 | n/a | self.assertNotIn(-1, r) |
---|
470 | n/a | self.assertNotIn(1, r) |
---|
471 | n/a | |
---|
472 | n/a | def test_range_iterators(self): |
---|
473 | n/a | # exercise 'fast' iterators, that use a rangeiterobject internally. |
---|
474 | n/a | # see issue 7298 |
---|
475 | n/a | limits = [base + jiggle |
---|
476 | n/a | for M in (2**32, 2**64) |
---|
477 | n/a | for base in (-M, -M//2, 0, M//2, M) |
---|
478 | n/a | for jiggle in (-2, -1, 0, 1, 2)] |
---|
479 | n/a | test_ranges = [(start, end, step) |
---|
480 | n/a | for start in limits |
---|
481 | n/a | for end in limits |
---|
482 | n/a | for step in (-2**63, -2**31, -2, -1, 1, 2)] |
---|
483 | n/a | |
---|
484 | n/a | for start, end, step in test_ranges: |
---|
485 | n/a | iter1 = range(start, end, step) |
---|
486 | n/a | iter2 = pyrange(start, end, step) |
---|
487 | n/a | test_id = "range({}, {}, {})".format(start, end, step) |
---|
488 | n/a | # check first 100 entries |
---|
489 | n/a | self.assert_iterators_equal(iter1, iter2, test_id, limit=100) |
---|
490 | n/a | |
---|
491 | n/a | iter1 = reversed(range(start, end, step)) |
---|
492 | n/a | iter2 = pyrange_reversed(start, end, step) |
---|
493 | n/a | test_id = "reversed(range({}, {}, {}))".format(start, end, step) |
---|
494 | n/a | self.assert_iterators_equal(iter1, iter2, test_id, limit=100) |
---|
495 | n/a | |
---|
496 | n/a | def test_range_iterators_invocation(self): |
---|
497 | n/a | # verify range iterators instances cannot be created by |
---|
498 | n/a | # calling their type |
---|
499 | n/a | rangeiter_type = type(iter(range(0))) |
---|
500 | n/a | self.assertRaises(TypeError, rangeiter_type, 1, 3, 1) |
---|
501 | n/a | long_rangeiter_type = type(iter(range(1 << 1000))) |
---|
502 | n/a | self.assertRaises(TypeError, long_rangeiter_type, 1, 3, 1) |
---|
503 | n/a | |
---|
504 | n/a | def test_slice(self): |
---|
505 | n/a | def check(start, stop, step=None): |
---|
506 | n/a | i = slice(start, stop, step) |
---|
507 | n/a | self.assertEqual(list(r[i]), list(r)[i]) |
---|
508 | n/a | self.assertEqual(len(r[i]), len(list(r)[i])) |
---|
509 | n/a | for r in [range(10), |
---|
510 | n/a | range(0), |
---|
511 | n/a | range(1, 9, 3), |
---|
512 | n/a | range(8, 0, -3), |
---|
513 | n/a | range(sys.maxsize+1, sys.maxsize+10), |
---|
514 | n/a | ]: |
---|
515 | n/a | check(0, 2) |
---|
516 | n/a | check(0, 20) |
---|
517 | n/a | check(1, 2) |
---|
518 | n/a | check(20, 30) |
---|
519 | n/a | check(-30, -20) |
---|
520 | n/a | check(-1, 100, 2) |
---|
521 | n/a | check(0, -1) |
---|
522 | n/a | check(-1, -3, -1) |
---|
523 | n/a | |
---|
524 | n/a | def test_contains(self): |
---|
525 | n/a | r = range(10) |
---|
526 | n/a | self.assertIn(0, r) |
---|
527 | n/a | self.assertIn(1, r) |
---|
528 | n/a | self.assertIn(5.0, r) |
---|
529 | n/a | self.assertNotIn(5.1, r) |
---|
530 | n/a | self.assertNotIn(-1, r) |
---|
531 | n/a | self.assertNotIn(10, r) |
---|
532 | n/a | self.assertNotIn("", r) |
---|
533 | n/a | r = range(9, -1, -1) |
---|
534 | n/a | self.assertIn(0, r) |
---|
535 | n/a | self.assertIn(1, r) |
---|
536 | n/a | self.assertIn(5.0, r) |
---|
537 | n/a | self.assertNotIn(5.1, r) |
---|
538 | n/a | self.assertNotIn(-1, r) |
---|
539 | n/a | self.assertNotIn(10, r) |
---|
540 | n/a | self.assertNotIn("", r) |
---|
541 | n/a | r = range(0, 10, 2) |
---|
542 | n/a | self.assertIn(0, r) |
---|
543 | n/a | self.assertNotIn(1, r) |
---|
544 | n/a | self.assertNotIn(5.0, r) |
---|
545 | n/a | self.assertNotIn(5.1, r) |
---|
546 | n/a | self.assertNotIn(-1, r) |
---|
547 | n/a | self.assertNotIn(10, r) |
---|
548 | n/a | self.assertNotIn("", r) |
---|
549 | n/a | r = range(9, -1, -2) |
---|
550 | n/a | self.assertNotIn(0, r) |
---|
551 | n/a | self.assertIn(1, r) |
---|
552 | n/a | self.assertIn(5.0, r) |
---|
553 | n/a | self.assertNotIn(5.1, r) |
---|
554 | n/a | self.assertNotIn(-1, r) |
---|
555 | n/a | self.assertNotIn(10, r) |
---|
556 | n/a | self.assertNotIn("", r) |
---|
557 | n/a | |
---|
558 | n/a | def test_reverse_iteration(self): |
---|
559 | n/a | for r in [range(10), |
---|
560 | n/a | range(0), |
---|
561 | n/a | range(1, 9, 3), |
---|
562 | n/a | range(8, 0, -3), |
---|
563 | n/a | range(sys.maxsize+1, sys.maxsize+10), |
---|
564 | n/a | ]: |
---|
565 | n/a | self.assertEqual(list(reversed(r)), list(r)[::-1]) |
---|
566 | n/a | |
---|
567 | n/a | def test_issue11845(self): |
---|
568 | n/a | r = range(*slice(1, 18, 2).indices(20)) |
---|
569 | n/a | values = {None, 0, 1, -1, 2, -2, 5, -5, 19, -19, |
---|
570 | n/a | 20, -20, 21, -21, 30, -30, 99, -99} |
---|
571 | n/a | for i in values: |
---|
572 | n/a | for j in values: |
---|
573 | n/a | for k in values - {0}: |
---|
574 | n/a | r[i:j:k] |
---|
575 | n/a | |
---|
576 | n/a | def test_comparison(self): |
---|
577 | n/a | test_ranges = [range(0), range(0, -1), range(1, 1, 3), |
---|
578 | n/a | range(1), range(5, 6), range(5, 6, 2), |
---|
579 | n/a | range(5, 7, 2), range(2), range(0, 4, 2), |
---|
580 | n/a | range(0, 5, 2), range(0, 6, 2)] |
---|
581 | n/a | test_tuples = list(map(tuple, test_ranges)) |
---|
582 | n/a | |
---|
583 | n/a | # Check that equality of ranges matches equality of the corresponding |
---|
584 | n/a | # tuples for each pair from the test lists above. |
---|
585 | n/a | ranges_eq = [a == b for a in test_ranges for b in test_ranges] |
---|
586 | n/a | tuples_eq = [a == b for a in test_tuples for b in test_tuples] |
---|
587 | n/a | self.assertEqual(ranges_eq, tuples_eq) |
---|
588 | n/a | |
---|
589 | n/a | # Check that != correctly gives the logical negation of == |
---|
590 | n/a | ranges_ne = [a != b for a in test_ranges for b in test_ranges] |
---|
591 | n/a | self.assertEqual(ranges_ne, [not x for x in ranges_eq]) |
---|
592 | n/a | |
---|
593 | n/a | # Equal ranges should have equal hashes. |
---|
594 | n/a | for a in test_ranges: |
---|
595 | n/a | for b in test_ranges: |
---|
596 | n/a | if a == b: |
---|
597 | n/a | self.assertEqual(hash(a), hash(b)) |
---|
598 | n/a | |
---|
599 | n/a | # Ranges are unequal to other types (even sequence types) |
---|
600 | n/a | self.assertIs(range(0) == (), False) |
---|
601 | n/a | self.assertIs(() == range(0), False) |
---|
602 | n/a | self.assertIs(range(2) == [0, 1], False) |
---|
603 | n/a | |
---|
604 | n/a | # Huge integers aren't a problem. |
---|
605 | n/a | self.assertEqual(range(0, 2**100 - 1, 2), |
---|
606 | n/a | range(0, 2**100, 2)) |
---|
607 | n/a | self.assertEqual(hash(range(0, 2**100 - 1, 2)), |
---|
608 | n/a | hash(range(0, 2**100, 2))) |
---|
609 | n/a | self.assertNotEqual(range(0, 2**100, 2), |
---|
610 | n/a | range(0, 2**100 + 1, 2)) |
---|
611 | n/a | self.assertEqual(range(2**200, 2**201 - 2**99, 2**100), |
---|
612 | n/a | range(2**200, 2**201, 2**100)) |
---|
613 | n/a | self.assertEqual(hash(range(2**200, 2**201 - 2**99, 2**100)), |
---|
614 | n/a | hash(range(2**200, 2**201, 2**100))) |
---|
615 | n/a | self.assertNotEqual(range(2**200, 2**201, 2**100), |
---|
616 | n/a | range(2**200, 2**201 + 1, 2**100)) |
---|
617 | n/a | |
---|
618 | n/a | # Order comparisons are not implemented for ranges. |
---|
619 | n/a | with self.assertRaises(TypeError): |
---|
620 | n/a | range(0) < range(0) |
---|
621 | n/a | with self.assertRaises(TypeError): |
---|
622 | n/a | range(0) > range(0) |
---|
623 | n/a | with self.assertRaises(TypeError): |
---|
624 | n/a | range(0) <= range(0) |
---|
625 | n/a | with self.assertRaises(TypeError): |
---|
626 | n/a | range(0) >= range(0) |
---|
627 | n/a | |
---|
628 | n/a | |
---|
629 | n/a | def test_attributes(self): |
---|
630 | n/a | # test the start, stop and step attributes of range objects |
---|
631 | n/a | self.assert_attrs(range(0), 0, 0, 1) |
---|
632 | n/a | self.assert_attrs(range(10), 0, 10, 1) |
---|
633 | n/a | self.assert_attrs(range(-10), 0, -10, 1) |
---|
634 | n/a | self.assert_attrs(range(0, 10, 1), 0, 10, 1) |
---|
635 | n/a | self.assert_attrs(range(0, 10, 3), 0, 10, 3) |
---|
636 | n/a | self.assert_attrs(range(10, 0, -1), 10, 0, -1) |
---|
637 | n/a | self.assert_attrs(range(10, 0, -3), 10, 0, -3) |
---|
638 | n/a | |
---|
639 | n/a | def assert_attrs(self, rangeobj, start, stop, step): |
---|
640 | n/a | self.assertEqual(rangeobj.start, start) |
---|
641 | n/a | self.assertEqual(rangeobj.stop, stop) |
---|
642 | n/a | self.assertEqual(rangeobj.step, step) |
---|
643 | n/a | |
---|
644 | n/a | with self.assertRaises(AttributeError): |
---|
645 | n/a | rangeobj.start = 0 |
---|
646 | n/a | with self.assertRaises(AttributeError): |
---|
647 | n/a | rangeobj.stop = 10 |
---|
648 | n/a | with self.assertRaises(AttributeError): |
---|
649 | n/a | rangeobj.step = 1 |
---|
650 | n/a | |
---|
651 | n/a | with self.assertRaises(AttributeError): |
---|
652 | n/a | del rangeobj.start |
---|
653 | n/a | with self.assertRaises(AttributeError): |
---|
654 | n/a | del rangeobj.stop |
---|
655 | n/a | with self.assertRaises(AttributeError): |
---|
656 | n/a | del rangeobj.step |
---|
657 | n/a | |
---|
658 | n/a | if __name__ == "__main__": |
---|
659 | n/a | unittest.main() |
---|