ยปCore Development>Code coverage>Lib/test/test_range.py

Python code coverage for Lib/test/test_range.py

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