Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_range.RangeTest Class Reference

List of all members.

Public Member Functions

def assert_iterators_equal
def test_range
def test_large_operands
def test_large_range
def test_invalid_invocation
def test_index
def test_user_index_method
def test_count
def test_repr
def test_pickling
def test_odd_bug
def test_types
def test_strided_limits
def test_empty
def test_range_iterators
def test_slice
def test_contains
def test_reverse_iteration
def test_issue11845

Public Attributes

 n

Detailed Description

Definition at line 23 of file test_range.py.


Member Function Documentation

def test.test_range.RangeTest.assert_iterators_equal (   self,
  xs,
  ys,
  test_id,
  limit = None 
)

Definition at line 24 of file test_range.py.

00024 
00025     def assert_iterators_equal(self, xs, ys, test_id, limit=None):
00026         # check that an iterator xs matches the expected results ys,
00027         # up to a given limit.
00028         if limit is not None:
00029             xs = itertools.islice(xs, limit)
00030             ys = itertools.islice(ys, limit)
00031         sentinel = object()
00032         pairs = itertools.zip_longest(xs, ys, fillvalue=sentinel)
00033         for i, (x, y) in enumerate(pairs):
00034             if x == y:
00035                 continue
00036             elif x == sentinel:
00037                 self.fail('{}: iterator ended unexpectedly '
00038                           'at position {}; expected {}'.format(test_id, i, y))
00039             elif y == sentinel:
00040                 self.fail('{}: unexpected excess element {} at '
00041                           'position {}'.format(test_id, x, i))
00042             else:
00043                 self.fail('{}: wrong element at position {};'
00044                           'expected {}, got {}'.format(test_id, i, y, x))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 458 of file test_range.py.

00458 
00459     def test_contains(self):
00460         r = range(10)
00461         self.assertIn(0, r)
00462         self.assertIn(1, r)
00463         self.assertIn(5.0, r)
00464         self.assertNotIn(5.1, r)
00465         self.assertNotIn(-1, r)
00466         self.assertNotIn(10, r)
00467         self.assertNotIn("", r)
00468         r = range(9, -1, -1)
00469         self.assertIn(0, r)
00470         self.assertIn(1, r)
00471         self.assertIn(5.0, r)
00472         self.assertNotIn(5.1, r)
00473         self.assertNotIn(-1, r)
00474         self.assertNotIn(10, r)
00475         self.assertNotIn("", r)
00476         r = range(0, 10, 2)
00477         self.assertIn(0, r)
00478         self.assertNotIn(1, r)
00479         self.assertNotIn(5.0, r)
00480         self.assertNotIn(5.1, r)
00481         self.assertNotIn(-1, r)
00482         self.assertNotIn(10, r)
00483         self.assertNotIn("", r)
00484         r = range(9, -1, -2)
00485         self.assertNotIn(0, r)
00486         self.assertIn(1, r)
00487         self.assertIn(5.0, r)
00488         self.assertNotIn(5.1, r)
00489         self.assertNotIn(-1, r)
00490         self.assertNotIn(10, r)
00491         self.assertNotIn("", r)

Here is the call graph for this function:

Definition at line 315 of file test_range.py.

00315 
00316     def test_count(self):
00317         self.assertEqual(range(3).count(-1), 0)
00318         self.assertEqual(range(3).count(0), 1)
00319         self.assertEqual(range(3).count(1), 1)
00320         self.assertEqual(range(3).count(2), 1)
00321         self.assertEqual(range(3).count(3), 0)
00322         self.assertIs(type(range(3).count(-1)), int)
00323         self.assertIs(type(range(3).count(1)), int)
00324         self.assertEqual(range(10**20).count(1), 1)
00325         self.assertEqual(range(10**20).count(10**20), 0)
00326         self.assertEqual(range(3).index(1), 1)
00327         self.assertEqual(range(1, 2**100, 2).count(2**87), 0)
00328         self.assertEqual(range(1, 2**100, 2).count(2**87+1), 1)
00329 
00330         class AlwaysEqual(object):
00331             def __eq__(self, other):
00332                 return True
00333         always_equal = AlwaysEqual()
00334         self.assertEqual(range(10).count(always_equal), 10)
00335 
00336         self.assertEqual(len(range(sys.maxsize, sys.maxsize+10)), 10)

Here is the call graph for this function:

Definition at line 404 of file test_range.py.

00404 
00405     def test_empty(self):
00406         r = range(0)
00407         self.assertNotIn(0, r)
00408         self.assertNotIn(1, r)
00409 
00410         r = range(0, -10)
00411         self.assertNotIn(0, r)
00412         self.assertNotIn(-1, r)
00413         self.assertNotIn(1, r)

Here is the call graph for this function:

Definition at line 249 of file test_range.py.

00249 
00250     def test_index(self):
00251         u = range(2)
00252         self.assertEqual(u.index(0), 0)
00253         self.assertEqual(u.index(1), 1)
00254         self.assertRaises(ValueError, u.index, 2)
00255 
00256         u = range(-2, 3)
00257         self.assertEqual(u.count(0), 1)
00258         self.assertEqual(u.index(0), 2)
00259         self.assertRaises(TypeError, u.index)
00260 
00261         class BadExc(Exception):
00262             pass
00263 
00264         class BadCmp:
00265             def __eq__(self, other):
00266                 if other == 2:
00267                     raise BadExc()
00268                 return False
00269 
00270         a = range(4)
00271         self.assertRaises(BadExc, a.index, BadCmp())
00272 
00273         a = range(-2, 3)
00274         self.assertEqual(a.index(0), 2)
00275         self.assertEqual(range(1, 10, 3).index(4), 1)
00276         self.assertEqual(range(1, -10, -3).index(-5), 2)
00277 
00278         self.assertEqual(range(10**20).index(1), 1)
00279         self.assertEqual(range(10**20).index(10**20 - 1), 10**20 - 1)
00280 
00281         self.assertRaises(ValueError, range(1, 2**100, 2).index, 2**87)
00282         self.assertEqual(range(1, 2**100, 2).index(2**87+1), 2**86)
00283 
00284         class AlwaysEqual(object):
00285             def __eq__(self, other):
00286                 return True
00287         always_equal = AlwaysEqual()
00288         self.assertEqual(range(10).index(always_equal), 0)

Here is the call graph for this function:

Definition at line 225 of file test_range.py.

00225 
00226     def test_invalid_invocation(self):
00227         self.assertRaises(TypeError, range)
00228         self.assertRaises(TypeError, range, 1, 2, 3, 4)
00229         self.assertRaises(ValueError, range, 1, 2, 0)
00230         a = int(10 * sys.maxsize)
00231         self.assertRaises(ValueError, range, a, a + 1, int(0))
00232         self.assertRaises(TypeError, range, 1., 1., 1.)
00233         self.assertRaises(TypeError, range, 1e100, 1e101, 1e101)
00234         self.assertRaises(TypeError, range, 0, "spam")
00235         self.assertRaises(TypeError, range, 0, 42, "spam")
00236         # Exercise various combinations of bad arguments, to check
00237         # refcounting logic
00238         self.assertRaises(TypeError, range, 0.0)
00239         self.assertRaises(TypeError, range, 0, 0.0)
00240         self.assertRaises(TypeError, range, 0.0, 0)
00241         self.assertRaises(TypeError, range, 0.0, 0.0)
00242         self.assertRaises(TypeError, range, 0, 0, 1.0)
00243         self.assertRaises(TypeError, range, 0, 0.0, 1)
00244         self.assertRaises(TypeError, range, 0, 0.0, 1.0)
00245         self.assertRaises(TypeError, range, 0.0, 0, 1)
00246         self.assertRaises(TypeError, range, 0.0, 0, 1.0)
00247         self.assertRaises(TypeError, range, 0.0, 0.0, 1)
00248         self.assertRaises(TypeError, range, 0.0, 0.0, 1.0)

Here is the call graph for this function:

Definition at line 501 of file test_range.py.

00501 
00502     def test_issue11845(self):
00503         r = range(*slice(1, 18, 2).indices(20))
00504         values = {None, 0, 1, -1, 2, -2, 5, -5, 19, -19,
00505                   20, -20, 21, -21, 30, -30, 99, -99}
00506         for i in values:
00507             for j in values:
00508                 for k in values - {0}:
00509                     r[i:j:k]
00510 

Definition at line 93 of file test_range.py.

00093 
00094     def test_large_operands(self):
00095         x = range(10**20, 10**20+10, 3)
00096         self.assertEqual(len(x), 4)
00097         self.assertEqual(len(list(x)), 4)
00098 
00099         x = range(10**20+10, 10**20, 3)
00100         self.assertEqual(len(x), 0)
00101         self.assertEqual(len(list(x)), 0)
00102 
00103         x = range(10**20, 10**20+10, -3)
00104         self.assertEqual(len(x), 0)
00105         self.assertEqual(len(list(x)), 0)
00106 
00107         x = range(10**20+10, 10**20, -3)
00108         self.assertEqual(len(x), 4)
00109         self.assertEqual(len(list(x)), 4)
00110 
00111         # Now test range() with longs
00112         self.assertEqual(list(range(-2**100)), [])
00113         self.assertEqual(list(range(0, -2**100)), [])
00114         self.assertEqual(list(range(0, 2**100, -1)), [])
00115         self.assertEqual(list(range(0, 2**100, -1)), [])
00116 
00117         a = int(10 * sys.maxsize)
00118         b = int(100 * sys.maxsize)
00119         c = int(50 * sys.maxsize)
00120 
00121         self.assertEqual(list(range(a, a+2)), [a, a+1])
00122         self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1])
00123         self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2])
00124 
00125         seq = list(range(a, b, c))
00126         self.assertIn(a, seq)
00127         self.assertNotIn(b, seq)
00128         self.assertEqual(len(seq), 2)
00129         self.assertEqual(seq[0], a)
00130         self.assertEqual(seq[-1], a+c)
00131 
00132         seq = list(range(b, a, -c))
00133         self.assertIn(b, seq)
00134         self.assertNotIn(a, seq)
00135         self.assertEqual(len(seq), 2)
00136         self.assertEqual(seq[0], b)
00137         self.assertEqual(seq[-1], b-c)
00138 
00139         seq = list(range(-a, -b, -c))
00140         self.assertIn(-a, seq)
00141         self.assertNotIn(-b, seq)
00142         self.assertEqual(len(seq), 2)
00143         self.assertEqual(seq[0], -a)
00144         self.assertEqual(seq[-1], -a-c)

Here is the call graph for this function:

Definition at line 145 of file test_range.py.

00145 
00146     def test_large_range(self):
00147         # Check long ranges (len > sys.maxsize)
00148         # len() is expected to fail due to limitations of the __len__ protocol
00149         def _range_len(x):
00150             try:
00151                 length = len(x)
00152             except OverflowError:
00153                 step = x[1] - x[0]
00154                 length = 1 + ((x[-1] - x[0]) // step)
00155             return length
00156         a = -sys.maxsize
00157         b = sys.maxsize
00158         expected_len = b - a
00159         x = range(a, b)
00160         self.assertIn(a, x)
00161         self.assertNotIn(b, x)
00162         self.assertRaises(OverflowError, len, x)
00163         self.assertEqual(_range_len(x), expected_len)
00164         self.assertEqual(x[0], a)
00165         idx = sys.maxsize+1
00166         self.assertEqual(x[idx], a+idx)
00167         self.assertEqual(x[idx:idx+1][0], a+idx)
00168         with self.assertRaises(IndexError):
00169             x[-expected_len-1]
00170         with self.assertRaises(IndexError):
00171             x[expected_len]
00172 
00173         a = 0
00174         b = 2 * sys.maxsize
00175         expected_len = b - a
00176         x = range(a, b)
00177         self.assertIn(a, x)
00178         self.assertNotIn(b, x)
00179         self.assertRaises(OverflowError, len, x)
00180         self.assertEqual(_range_len(x), expected_len)
00181         self.assertEqual(x[0], a)
00182         idx = sys.maxsize+1
00183         self.assertEqual(x[idx], a+idx)
00184         self.assertEqual(x[idx:idx+1][0], a+idx)
00185         with self.assertRaises(IndexError):
00186             x[-expected_len-1]
00187         with self.assertRaises(IndexError):
00188             x[expected_len]
00189 
00190         a = 0
00191         b = sys.maxsize**10
00192         c = 2*sys.maxsize
00193         expected_len = 1 + (b - a) // c
00194         x = range(a, b, c)
00195         self.assertIn(a, x)
00196         self.assertNotIn(b, x)
00197         self.assertRaises(OverflowError, len, x)
00198         self.assertEqual(_range_len(x), expected_len)
00199         self.assertEqual(x[0], a)
00200         idx = sys.maxsize+1
00201         self.assertEqual(x[idx], a+(idx*c))
00202         self.assertEqual(x[idx:idx+1][0], a+(idx*c))
00203         with self.assertRaises(IndexError):
00204             x[-expected_len-1]
00205         with self.assertRaises(IndexError):
00206             x[expected_len]
00207 
00208         a = sys.maxsize**10
00209         b = 0
00210         c = -2*sys.maxsize
00211         expected_len = 1 + (b - a) // c
00212         x = range(a, b, c)
00213         self.assertIn(a, x)
00214         self.assertNotIn(b, x)
00215         self.assertRaises(OverflowError, len, x)
00216         self.assertEqual(_range_len(x), expected_len)
00217         self.assertEqual(x[0], a)
00218         idx = sys.maxsize+1
00219         self.assertEqual(x[idx], a+(idx*c))
00220         self.assertEqual(x[idx:idx+1][0], a+(idx*c))
00221         with self.assertRaises(IndexError):
00222             x[-expected_len-1]
00223         with self.assertRaises(IndexError):
00224             x[expected_len]

Here is the call graph for this function:

Definition at line 351 of file test_range.py.

00351 
00352     def test_odd_bug(self):
00353         # This used to raise a "SystemError: NULL result without error"
00354         # because the range validation step was eating the exception
00355         # before NULL was returned.
00356         with self.assertRaises(TypeError):
00357             range([], 1, -1)

Here is the call graph for this function:

Definition at line 342 of file test_range.py.

00342 
00343     def test_pickling(self):
00344         testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1),
00345                      (13, 21, 3), (-2, 2, 2)]
00346         for proto in range(pickle.HIGHEST_PROTOCOL + 1):
00347             for t in testcases:
00348                 r = range(*t)
00349                 self.assertEqual(list(pickle.loads(pickle.dumps(r, proto))),
00350                                  list(r))

Here is the call graph for this function:

Definition at line 45 of file test_range.py.

00045 
00046     def test_range(self):
00047         self.assertEqual(list(range(3)), [0, 1, 2])
00048         self.assertEqual(list(range(1, 5)), [1, 2, 3, 4])
00049         self.assertEqual(list(range(0)), [])
00050         self.assertEqual(list(range(-3)), [])
00051         self.assertEqual(list(range(1, 10, 3)), [1, 4, 7])
00052         self.assertEqual(list(range(5, -5, -3)), [5, 2, -1, -4])
00053 
00054         a = 10
00055         b = 100
00056         c = 50
00057 
00058         self.assertEqual(list(range(a, a+2)), [a, a+1])
00059         self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1])
00060         self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2])
00061 
00062         seq = list(range(a, b, c))
00063         self.assertIn(a, seq)
00064         self.assertNotIn(b, seq)
00065         self.assertEqual(len(seq), 2)
00066 
00067         seq = list(range(b, a, -c))
00068         self.assertIn(b, seq)
00069         self.assertNotIn(a, seq)
00070         self.assertEqual(len(seq), 2)
00071 
00072         seq = list(range(-a, -b, -c))
00073         self.assertIn(-a, seq)
00074         self.assertNotIn(-b, seq)
00075         self.assertEqual(len(seq), 2)
00076 
00077         self.assertRaises(TypeError, range)
00078         self.assertRaises(TypeError, range, 1, 2, 3, 4)
00079         self.assertRaises(ValueError, range, 1, 2, 0)
00080 
00081         self.assertRaises(TypeError, range, 0.0, 2, 1)
00082         self.assertRaises(TypeError, range, 1, 2.0, 1)
00083         self.assertRaises(TypeError, range, 1, 2, 1.0)
00084         self.assertRaises(TypeError, range, 1e100, 1e101, 1e101)
00085 
00086         self.assertRaises(TypeError, range, 0, "spam")
00087         self.assertRaises(TypeError, range, 0, 42, "spam")
00088 
00089         self.assertEqual(len(range(0, sys.maxsize, sys.maxsize-1)), 2)
00090 
00091         r = range(-sys.maxsize, sys.maxsize, 2)
00092         self.assertEqual(len(r), sys.maxsize)

Here is the call graph for this function:

Definition at line 414 of file test_range.py.

00414 
00415     def test_range_iterators(self):
00416         # exercise 'fast' iterators, that use a rangeiterobject internally.
00417         # see issue 7298
00418         limits = [base + jiggle
00419                   for M in (2**32, 2**64)
00420                   for base in (-M, -M//2, 0, M//2, M)
00421                   for jiggle in (-2, -1, 0, 1, 2)]
00422         test_ranges = [(start, end, step)
00423                        for start in limits
00424                        for end in limits
00425                        for step in (-2**63, -2**31, -2, -1, 1, 2)]
00426 
00427         for start, end, step in test_ranges:
00428             iter1 = range(start, end, step)
00429             iter2 = pyrange(start, end, step)
00430             test_id = "range({}, {}, {})".format(start, end, step)
00431             # check first 100 entries
00432             self.assert_iterators_equal(iter1, iter2, test_id, limit=100)
00433 
00434             iter1 = reversed(range(start, end, step))
00435             iter2 = pyrange_reversed(start, end, step)
00436             test_id = "reversed(range({}, {}, {}))".format(start, end, step)
00437             self.assert_iterators_equal(iter1, iter2, test_id, limit=100)

Here is the call graph for this function:

Definition at line 337 of file test_range.py.

00337 
00338     def test_repr(self):
00339         self.assertEqual(repr(range(1)), 'range(0, 1)')
00340         self.assertEqual(repr(range(1, 2)), 'range(1, 2)')
00341         self.assertEqual(repr(range(1, 2, 3)), 'range(1, 2, 3)')

Here is the call graph for this function:

Definition at line 492 of file test_range.py.

00492 
00493     def test_reverse_iteration(self):
00494         for r in [range(10),
00495                   range(0),
00496                   range(1, 9, 3),
00497                   range(8, 0, -3),
00498                   range(sys.maxsize+1, sys.maxsize+10),
00499                   ]:
00500             self.assertEqual(list(reversed(r)), list(r)[::-1])

Here is the call graph for this function:

Definition at line 438 of file test_range.py.

00438 
00439     def test_slice(self):
00440         def check(start, stop, step=None):
00441             i = slice(start, stop, step)
00442             self.assertEqual(list(r[i]), list(r)[i])
00443             self.assertEqual(len(r[i]), len(list(r)[i]))
00444         for r in [range(10),
00445                   range(0),
00446                   range(1, 9, 3),
00447                   range(8, 0, -3),
00448                   range(sys.maxsize+1, sys.maxsize+10),
00449                   ]:
00450             check(0, 2)
00451             check(0, 20)
00452             check(1, 2)
00453             check(20, 30)
00454             check(-30, -20)
00455             check(-1, 100, 2)
00456             check(0, -1)
00457             check(-1, -3, -1)

Here is the call graph for this function:

Definition at line 384 of file test_range.py.

00384 
00385     def test_strided_limits(self):
00386         r = range(0, 101, 2)
00387         self.assertIn(0, r)
00388         self.assertNotIn(1, r)
00389         self.assertIn(2, r)
00390         self.assertNotIn(99, r)
00391         self.assertIn(100, r)
00392         self.assertNotIn(101, r)
00393 
00394         r = range(0, -20, -1)
00395         self.assertIn(0, r)
00396         self.assertIn(-1, r)
00397         self.assertIn(-19, r)
00398         self.assertNotIn(-20, r)
00399 
00400         r = range(0, -20, -2)
00401         self.assertIn(-18, r)
00402         self.assertNotIn(-19, r)
00403         self.assertNotIn(-20, r)

Here is the call graph for this function:

Definition at line 358 of file test_range.py.

00358 
00359     def test_types(self):
00360         # Non-integer objects *equal* to any of the range's items are supposed
00361         # to be contained in the range.
00362         self.assertIn(1.0, range(3))
00363         self.assertIn(True, range(3))
00364         self.assertIn(1+0j, range(3))
00365 
00366         class C1:
00367             def __eq__(self, other): return True
00368         self.assertIn(C1(), range(3))
00369 
00370         # Objects are never coerced into other types for comparison.
00371         class C2:
00372             def __int__(self): return 1
00373             def __index__(self): return 1
00374         self.assertNotIn(C2(), range(3))
00375         # ..except if explicitly told so.
00376         self.assertIn(int(C2()), range(3))
00377 
00378         # Check that the range.__contains__ optimization is only
00379         # used for ints, not for instances of subclasses of int.
00380         class C3(int):
00381             def __eq__(self, other): return True
00382         self.assertIn(C3(11), range(10))
00383         self.assertIn(C3(11), list(range(10)))

Here is the call graph for this function:

Definition at line 289 of file test_range.py.

00289 
00290     def test_user_index_method(self):
00291         bignum = 2*sys.maxsize
00292         smallnum = 42
00293 
00294         # User-defined class with an __index__ method
00295         class I:
00296             def __init__(self, n):
00297                 self.n = int(n)
00298             def __index__(self):
00299                 return self.n
00300         self.assertEqual(list(range(I(bignum), I(bignum + 1))), [bignum])
00301         self.assertEqual(list(range(I(smallnum), I(smallnum + 1))), [smallnum])
00302 
00303         # User-defined class with a failing __index__ method
00304         class IX:
00305             def __index__(self):
00306                 raise RuntimeError
00307         self.assertRaises(RuntimeError, range, IX())
00308 
00309         # User-defined class with an invalid __index__ method
00310         class IN:
00311             def __index__(self):
00312                 return "not a number"
00313 
00314         self.assertRaises(TypeError, range, IN())

Here is the call graph for this function:


Member Data Documentation

Definition at line 296 of file test_range.py.


The documentation for this class was generated from the following file: