-
Notifications
You must be signed in to change notification settings - Fork 2
/
cycle.py
66 lines (51 loc) · 1.73 KB
/
cycle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from itertools import count, cycle as _cycle, islice
import pytest
def cycle(iterable):
raise NotImplementedError()
@pytest.mark.parametrize(
["iterable"],
[
([],),
(set(),),
("",),
({},),
],
)
def test_cycle_empty_iterable(iterable):
"""Test how cycle behaves if given an empty iterable."""
with pytest.raises(StopIteration):
next(cycle(iterable))
def test_cycle_single_element():
"""Test how cycle behaves given an iterable with a single element."""
VALUE = 15
c = cycle([VALUE])
values = list(islice(c, 1000))
assert len(values) == 1000
for value in values:
assert value == VALUE
def test_cycle_many_elements():
"""Test how cycle behaves given an iterable with multiple elements."""
VALUES = range(15)
pairs = list(islice(zip(cycle(VALUES), _cycle(VALUES)), 1000))
assert len(pairs) == 1000
for value, real in pairs:
assert value == real
def test_cycle_empty_arbitrary_iterator():
"""Test how cycle behaves with a more general empty iterator."""
VALUES = (value for value in [])
with pytest.raises(StopIteration):
next(cycle(VALUES))
def test_cycle_arbitrary_iterator():
"""Test how cycle behaves with a more general iterator."""
MAX = 15
VALUES = (value for value in range(MAX))
pairs = list(islice(enumerate(cycle(VALUES)), 1000))
assert len(pairs) == 1000
for idx, value in pairs:
assert value == (idx % MAX)
def test_cycle_works_with_infinite_iterator():
"""Test that cycle still works when fed an infinite iterator."""
pairs = list(enumerate(islice(cycle(count()), 1000)))
assert len(pairs) == 1000
for idx, value in pairs:
assert value == idx