def test_range_iter(): r = range(10) for i, v in enumerate(r): assert r[i] == v r = range(3, 10, 3) for i, v in enumerate(r): assert r[i] == v
def test_range_reversed(): r = range(10) for i, v in enumerate(reversed(r)): assert r[::-1][i] == v r = range(3, 10, 3) for i, v in enumerate(reversed(r)): assert r[::-1][i] == v
def test_vector_coverage(): # Miscellaneous tests purely for the sake of coverage assert rmod(3, 3, range(10)) == set() assert rmod(3, 2, []) == set() with pytest.raises(ValueError): rmod(0, 1, range(10)) with pytest.raises(ValueError): rdiv(0, 1)
def test_range_init(): assert range(10).start == 0 assert range(1, 10).step == 1 assert range(3, 10, 3) == range(3, 10, 3) with pytest.raises(ValueError): range(0, 10, 0) with pytest.raises(TypeError): range(0.0, 10.0, 0.5)
def test_range_index(): assert range(10).index(0) == 0 assert range(10).index(9) == 9 assert range(3, 10, 3).index(3) == 0 assert range(3, 10, 3).index(9) == 2 assert range(3, 10, 3)[::-1].index(9) == 0 with pytest.raises(ValueError): range(10).index(10)
def test_range_get(): r = range(10) t = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for v1, v2 in zip_longest(r, t): assert v1 == v2 r = range(3, 10, 3) t = [3, 6, 9] for v1, v2 in zip_longest(r, t): assert v1 == v2 with pytest.raises(IndexError): range(10)[10] with pytest.raises(IndexError): range(10)[-11]
def test_range_get_slice(): assert range(10)[1:] == range(1, 10) assert range(10)[-5:] == range(5, 10) assert range(10)[20:] == range(1, 1) assert range(10)[:5] == range(5) assert range(10)[:-2] == range(8) assert range(10)[:20] == range(10) assert range(10)[1:-1] == range(1, 9) assert range(10)[::-1] == range(9, -1, -1) assert range(10)[2::-1] == range(2, -1, -1) assert range(3, 10, 3)[1:] == range(6, 12, 3) assert range(3, 10, 3)[::-1] == range(9, 0, -3) assert range(3, 10, 3)[2::-1] == range(9, 0, -3) assert range(0, 10, 3)[:-3:-1] == range(9, 3, -3) with pytest.raises(ValueError): range(10)[::0]
def test_range_bool(): assert range(10) assert not range(10, 10) assert range(3, 10, 3)
def test_range_len(): assert len(range(10)) == 10 assert len(range(10, 10)) == 0 assert len(range(10)[::-1]) == 10 assert len(range(3, 10, 3)) == 3
def test_range_equality(): assert range(10) == range(10) assert range(10, 10) == range(1, 1) assert range(10, 11) == range(10, 9, -1) assert range(0, 10, 3) == range(0, 11, 3) assert range(0, 10, 3) != range(0, 13, 3) # Yeah ... this one's weird, but it matches Py3 behaviour ... assert range(10) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def test_range_count(): assert range(10).count(0) == 1 assert range(10).count(10) == 0 assert range(3, 10, 3).count(4) == 0 assert range(3, 10, 3).count(6) == 1
def test_range_contains(): assert 0 in range(10) assert 10 not in range(10) assert 3 in range(3, 10, 3) assert 3 in range(3, 10, 3)[::-1] assert 0 not in range(3, 10, 3)