1import pytest
2from pybind11_tests import sequences_and_iterators as m
3from pybind11_tests import ConstructorStats
4
5
6def isclose(a, b, rel_tol=1e-05, abs_tol=0.0):
7    """Like math.isclose() from Python 3.5"""
8    return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
9
10
11def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0):
12    return all(isclose(a, b, rel_tol=rel_tol, abs_tol=abs_tol) for a, b in zip(a_list, b_list))
13
14
15def test_generalized_iterators():
16    assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
17    assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
18    assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
19
20    assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
21    assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
22    assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
23
24    # __next__ must continue to raise StopIteration
25    it = m.IntPairs([(0, 0)]).nonzero()
26    for _ in range(3):
27        with pytest.raises(StopIteration):
28            next(it)
29
30    it = m.IntPairs([(0, 0)]).nonzero_keys()
31    for _ in range(3):
32        with pytest.raises(StopIteration):
33            next(it)
34
35
36def test_sliceable():
37    sliceable = m.Sliceable(100)
38    assert sliceable[::] == (0, 100, 1)
39    assert sliceable[10::] == (10, 100, 1)
40    assert sliceable[:10:] == (0, 10, 1)
41    assert sliceable[::10] == (0, 100, 10)
42    assert sliceable[-10::] == (90, 100, 1)
43    assert sliceable[:-10:] == (0, 90, 1)
44    assert sliceable[::-10] == (99, -1, -10)
45    assert sliceable[50:60:1] == (50, 60, 1)
46    assert sliceable[50:60:-1] == (50, 60, -1)
47
48
49def test_sequence():
50    cstats = ConstructorStats.get(m.Sequence)
51
52    s = m.Sequence(5)
53    assert cstats.values() == ['of size', '5']
54
55    assert "Sequence" in repr(s)
56    assert len(s) == 5
57    assert s[0] == 0 and s[3] == 0
58    assert 12.34 not in s
59    s[0], s[3] = 12.34, 56.78
60    assert 12.34 in s
61    assert isclose(s[0], 12.34) and isclose(s[3], 56.78)
62
63    rev = reversed(s)
64    assert cstats.values() == ['of size', '5']
65
66    rev2 = s[::-1]
67    assert cstats.values() == ['of size', '5']
68
69    it = iter(m.Sequence(0))
70    for _ in range(3):  # __next__ must continue to raise StopIteration
71        with pytest.raises(StopIteration):
72            next(it)
73    assert cstats.values() == ['of size', '0']
74
75    expected = [0, 56.78, 0, 0, 12.34]
76    assert allclose(rev, expected)
77    assert allclose(rev2, expected)
78    assert rev == rev2
79
80    rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
81    assert cstats.values() == ['of size', '3', 'from std::vector']
82
83    assert allclose(rev, [2, 56.78, 2, 0, 2])
84
85    assert cstats.alive() == 4
86    del it
87    assert cstats.alive() == 3
88    del s
89    assert cstats.alive() == 2
90    del rev
91    assert cstats.alive() == 1
92    del rev2
93    assert cstats.alive() == 0
94
95    assert cstats.values() == []
96    assert cstats.default_constructions == 0
97    assert cstats.copy_constructions == 0
98    assert cstats.move_constructions >= 1
99    assert cstats.copy_assignments == 0
100    assert cstats.move_assignments == 0
101
102
103def test_map_iterator():
104    sm = m.StringMap({'hi': 'bye', 'black': 'white'})
105    assert sm['hi'] == 'bye'
106    assert len(sm) == 2
107    assert sm['black'] == 'white'
108
109    with pytest.raises(KeyError):
110        assert sm['orange']
111    sm['orange'] = 'banana'
112    assert sm['orange'] == 'banana'
113
114    expected = {'hi': 'bye', 'black': 'white', 'orange': 'banana'}
115    for k in sm:
116        assert sm[k] == expected[k]
117    for k, v in sm.items():
118        assert v == expected[k]
119
120    it = iter(m.StringMap({}))
121    for _ in range(3):  # __next__ must continue to raise StopIteration
122        with pytest.raises(StopIteration):
123            next(it)
124
125
126def test_python_iterator_in_cpp():
127    t = (1, 2, 3)
128    assert m.object_to_list(t) == [1, 2, 3]
129    assert m.object_to_list(iter(t)) == [1, 2, 3]
130    assert m.iterator_to_list(iter(t)) == [1, 2, 3]
131
132    with pytest.raises(TypeError) as excinfo:
133        m.object_to_list(1)
134    assert "object is not iterable" in str(excinfo.value)
135
136    with pytest.raises(TypeError) as excinfo:
137        m.iterator_to_list(1)
138    assert "incompatible function arguments" in str(excinfo.value)
139
140    def bad_next_call():
141        raise RuntimeError("py::iterator::advance() should propagate errors")
142
143    with pytest.raises(RuntimeError) as excinfo:
144        m.iterator_to_list(iter(bad_next_call, None))
145    assert str(excinfo.value) == "py::iterator::advance() should propagate errors"
146
147    lst = [1, None, 0, None]
148    assert m.count_none(lst) == 2
149    assert m.find_none(lst) is True
150    assert m.count_nonzeros({"a": 0, "b": 1, "c": 2}) == 2
151
152    r = range(5)
153    assert all(m.tuple_iterator(tuple(r)))
154    assert all(m.list_iterator(list(r)))
155    assert all(m.sequence_iterator(r))
156
157
158def test_iterator_passthrough():
159    """#181: iterator passthrough did not compile"""
160    from pybind11_tests.sequences_and_iterators import iterator_passthrough
161
162    assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
163
164
165def test_iterator_rvp():
166    """#388: Can't make iterators via make_iterator() with different r/v policies """
167    import pybind11_tests.sequences_and_iterators as m
168
169    assert list(m.make_iterator_1()) == [1, 2, 3]
170    assert list(m.make_iterator_2()) == [1, 2, 3]
171    assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2()))
172