38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
import pytest
|
|
from itertools import count
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from slic.utils.xrange import *
|
|
|
|
|
|
def is_count_iterator(obj):
|
|
return isinstance(obj, type(count()))
|
|
|
|
@pytest.mark.parametrize("args, kwargs, expected", [
|
|
((10,), {}, list(range(10))), # xrange(10)
|
|
((0, 10), {}, list(range(0, 10))), # xrange(0, 10)
|
|
((0, 10, 2), {}, list(range(0, 10, 2))), # xrange(0, 10, 2)
|
|
((10,), {"step": 2}, list(range(0, 10, 2))), # xrange(10, step=2)
|
|
])
|
|
def test_xrange_finite(args, kwargs, expected):
|
|
assert list(xrange(*args, **kwargs)) == expected
|
|
|
|
@pytest.mark.parametrize("args, kwargs, expected_repr", [
|
|
((), {}, repr(count(0))), # xrange()
|
|
((), {"step": 2}, repr(count(0, 2))), # xrange(step=2)
|
|
])
|
|
def test_xrange_infinite(args, kwargs, expected_repr):
|
|
# Only test first few elements to avoid infinite loop
|
|
result = xrange(*args, **kwargs)
|
|
assert is_count_iterator(result)
|
|
for i, val in zip(range(20), result):
|
|
assert val == i * kwargs.get("step", 1)
|
|
|
|
@pytest.mark.parametrize("args", [
|
|
(1, 2, 3, 4), # too many positional arguments
|
|
])
|
|
def test_xrange_too_many_args(args):
|
|
with pytest.raises(TypeError, match="xrange takes from 2 to 3 positional arguments"):
|
|
xrange(*args)
|