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