-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathislice.py
168 lines (141 loc) · 4.38 KB
/
islice.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from itertools import count, islice
import pytest
class islice:
def __init__(self, iterable, *args):
raise NotImplementedError()
def test_islice_is_iterator():
"""Verify that `islice` is (or, at least, looks like) an iterator."""
assert hasattr(islice, "__next__")
assert hasattr(islice, "__iter__")
nw = islice([], 0)
assert iter(nw) is nw
def test_islice_is_class():
"""Verify that `islice` was defined as a class."""
assert isinstance(islice, type) # Check that `islice` is defined as a class...
assert islice.__name__ == "islice" # ... with the correct name.
@pytest.mark.parametrize(
["stop"],
[
(0,),
(1,),
(2,),
(5,),
(10,),
(15,),
],
)
def test_islice_only_stop(stop):
"""Verify that `islice` works when only `stop` is specified."""
squares = (x**2 for x in range(10))
expected = [x**2 for x in range(min(stop, 10))]
assert list(islice(squares, stop)) == expected
def test_islice_stop_larger_than_iterable():
"""Test `islice` when the value of `stop` is larger than the iterable."""
squares = (x**2 for x in range(10))
expected = [x**2 for x in range(10)]
assert list(islice(squares, 1073)) == expected
def test_islice_only_stop_infinite_iterable():
"""Test `islice` with a value for `stop` and an infinite iterable."""
assert list(islice(count(), 10)) == list(range(10))
def test_islice_only_stop_none():
"""Test `islice` with a single argument `None`."""
assert list(islice(range(10), None)) == list(range(10))
def test_islice_only_stop_none_infinite_iterable():
"""Test `islice` with a single argument `None` and an infinite iterable."""
iterable = count()
pairs = list(zip(range(150), islice(iterable, None)))
assert len(pairs) == 150
for expected, value in pairs:
assert expected == value
@pytest.mark.parametrize(
["start", "stop"],
[
(None, None),
(None, 3),
(0, 4),
(1, 7),
(3, 10),
(5, 15),
(7, 4),
(8, None),
],
)
def test_islice_start_stop(start, stop):
"""Test `islice` with values for `start` and `stop`."""
squares = (x**2 for x in range(10))
expected_range = range(
start or 0,
10 if stop is None or stop > 10 else stop,
)
expected = [x**2 for x in expected_range]
assert list(islice(squares, start, stop)) == expected
@pytest.mark.parametrize(
["start", "stop", "step"],
[
(None, None, None),
(None, None, 2),
(None, None, 3),
(None, 3, None),
(None, 3, 2),
(None, 3, 3),
(0, 4, 2),
(0, 4, 3),
(1, 7, 2),
(1, 7, 3),
(3, 10, 2),
(3, 10, 3),
(5, 15, 2),
(5, 15, 3),
(7, 4, 2),
(7, 4, 3),
(8, None, None),
(8, None, 2),
(8, None, 3),
],
)
def test_islice_start_stop_step(start, stop, step):
"""Test `islice` with values for `start`, `stop`, and `step`."""
squares = (x**2 for x in range(10))
expected_range = range(
start or 0,
10 if stop is None or stop > 10 else stop,
1 if step is None else step,
)
expected = [x**2 for x in expected_range]
assert list(islice(squares, start, stop, step)) == expected
@pytest.mark.parametrize(
["start", "stop", "step"],
[
(None, 3, None),
(None, 3, 2),
(None, 3, 3),
(0, 4, 2),
(0, 4, 3),
(1, 7, 2),
(1, 7, 3),
(3, 10, 2),
(3, 10, 3),
(5, 15, 2),
(5, 15, 3),
(7, 4, 2),
(7, 4, 3),
],
)
def test_islice_start_stop_step_infinite_iterable(start, stop, step):
"""Test `islice` with all values and an infinite iterable."""
expected_range = range(
start or 0,
stop,
1 if step is None else step,
)
assert list(islice(count(), start, stop, step)) == list(expected_range)
def test_islice_does_not_advance_too_much():
"""Test `islice` does not advance the iterator past what is needed."""
squares = (x**2 for x in range(10))
list(islice(squares, 5))
assert list(squares) == [25, 36, 49, 64, 81]
def test_islice_advances_at_least_the_value_of_start():
"""Test `islice` advances at least `start` items when `stop < start`."""
squares = (x**2 for x in range(10))
list(islice(squares, 3, 1))
assert next(squares) == 9