Repository navigation
Expand file tree
/
Copy pathcycle_test.py
More file actions
82 lines (58 loc) · 2.31 KB
/
Copy pathcycle_test.py
File metadata and controls
82 lines (58 loc) · 2.31 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pytest
from itertools import cycle as itertools_cycle
from src.repeatable import cycle
from .helpers import (
counter, repeatable_counter, check_same_result, assert_memory_under,
assert_memory_exceeds
)
@pytest.mark.parametrize(
'args',
[
pytest.param((range(4),), id='single iterable'),
pytest.param(([],), id='empty iterable'),
pytest.param((repeatable_counter(4),), id='repeatable iterable'),
pytest.param((repeatable_counter(0),), id='empty repeatable iterable'),
pytest.param(tuple(), id='no inputs'),
# `itertools.cycle` can yield some results before memory overflow
pytest.param(
(repeatable_counter(),),
id='infinite repeatable'
),
]
)
def test_same_result(args):
# `cycle` produces values indefinitely, so we will just check that
# the generators begin the same way
check_same_result(cycle, itertools_cycle, *args, stop=100)
''' this package's `cycle` implementation '''
@assert_memory_under(1) # memory usage should not exceed 1MiB
def test_low_memory_usage_with_infinite_repeatables():
result = cycle(repeatable_counter())
for _ in range(int(1e6)):
next(result)
@assert_memory_under(1) # memory usage should not exceed 1MiB
def test_low_memory_usage_with_large_repeatables():
result = cycle(repeatable_counter(stop=1e6))
for _ in range(int(1e6)):
next(result)
@assert_memory_exceeds(1) # expect memory usage to be high
def test_high_memory_usage_with_infinite_vanilla_iterables():
result = cycle(counter())
for _ in range(int(1e6)):
next(result)
''' existing `itertools.cycle` '''
@assert_memory_exceeds(1) # expect memory usage to be high
def test_high_memory_usage_with_infinite_repeatables():
result = itertools_cycle(repeatable_counter())
for _ in range(int(1e6)):
next(result)
@assert_memory_exceeds(1) # expect memory usage to be high
def test_high_memory_usage_with_large_repeatables():
result = itertools_cycle(repeatable_counter(stop=1e6))
for _ in range(int(1e6)):
next(result)
@assert_memory_exceeds(1) # expect memory usage to be high
def test_itertools_high_memory_usage_with_infinite_vanilla_iterables():
result = itertools_cycle(counter())
for _ in range(int(1e6)):
next(result)