| 1 |
import pytest |
| 2 |
|
| 3 |
from itertools import pairwise, count, islice |
| 4 |
|
| 5 |
def test_pairwise(): |
| 6 |
assert list(pairwise([])) == [] |
| 7 |
assert list(pairwise([1])) == [] |
| 8 |
assert list(pairwise([1, 2])) == [(1, 2)] |
| 9 |
assert list(pairwise([1, 2, 3])) == [(1, 2), (2, 3)] |
| 10 |
|
| 11 |
def test_posonly(): |
| 12 |
with pytest.raises(TypeError): |
| 13 |
pairwise(iterable='abc') |
| 14 |
|
| 15 |
def test_pairwise_reenter(): |
| 16 |
def check(reenter_at, expected): |
| 17 |
class I: |
| 18 |
count = 0 |
| 19 |
def __iter__(self): |
| 20 |
return self |
| 21 |
def __next__(self): |
| 22 |
self.count += 1 |
| 23 |
if self.count in reenter_at: |
| 24 |
return next(it) |
| 25 |
return [self.count] |
| 26 |
|
| 27 |
it = pairwise(I()) |
| 28 |
for item in expected: |
| 29 |
assert next(it) == item |
| 30 |
|
| 31 |
check({1}, [ |
| 32 |
(([2], [3]), [4]), |
| 33 |
([4], [5]), |
| 34 |
]) |
| 35 |
check({2}, [ |
| 36 |
([1], ([1], [3])), |
| 37 |
(([1], [3]), [4]), |
| 38 |
([4], [5]), |
| 39 |
]) |
| 40 |
check({3}, [ |
| 41 |
([1], [2]), |
| 42 |
([2], ([2], [4])), |
| 43 |
(([2], [4]), [5]), |
| 44 |
([5], [6]), |
| 45 |
]) |
| 46 |
check({1, 2}, [ |
| 47 |
((([3], [4]), [5]), [6]), |
| 48 |
([6], [7]), |
| 49 |
]) |
| 50 |
check({1, 3}, [ |
| 51 |
(([2], ([2], [4])), [5]), |
| 52 |
([5], [6]), |
| 53 |
]) |
| 54 |
check({1, 4}, [ |
| 55 |
(([2], [3]), (([2], [3]), [5])), |
| 56 |
((([2], [3]), [5]), [6]), |
| 57 |
([6], [7]), |
| 58 |
]) |
| 59 |
check({2, 3}, [ |
| 60 |
([1], ([1], ([1], [4]))), |
| 61 |
(([1], ([1], [4])), [5]), |
| 62 |
([5], [6]), |
| 63 |
]) |
| 64 |
|
| 65 |
def test_count_complex(): |
| 66 |
assert list(islice(count(3.25-4j), 3)) == [3.25-4j, 4.25-4j, 5.25-4j] |