μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_stl_binders.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2import pytest
3
4import env
5from pybind11_tests import stl_binders as m
6
7
9 v_int = m.VectorInt([0, 0])
10 assert len(v_int) == 2
11 assert bool(v_int) is True
12
13 # test construction from a generator
14 v_int1 = m.VectorInt(x for x in range(5))
15 assert v_int1 == m.VectorInt([0, 1, 2, 3, 4])
16
17 v_int2 = m.VectorInt([0, 0])
18 assert v_int == v_int2
19 v_int2[1] = 1
20 assert v_int != v_int2
21
22 v_int2.append(2)
23 v_int2.insert(0, 1)
24 v_int2.insert(0, 2)
25 v_int2.insert(0, 3)
26 v_int2.insert(6, 3)
27 assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
28 with pytest.raises(IndexError):
29 v_int2.insert(8, 4)
30
31 v_int.append(99)
32 v_int2[2:-2] = v_int
33 assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
34 del v_int2[1:3]
35 assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
36 del v_int2[0]
37 assert v_int2 == m.VectorInt([0, 99, 2, 3])
38
39 v_int2.extend(m.VectorInt([4, 5]))
40 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5])
41
42 v_int2.extend([6, 7])
43 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
44
45 # test error handling, and that the vector is unchanged
46 with pytest.raises(RuntimeError):
47 v_int2.extend([8, "a"])
48
49 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
50
51 # test extending from a generator
52 v_int2.extend(x for x in range(5))
53 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4])
54
55 # test negative indexing
56 assert v_int2[-1] == 4
57
58 # insert with negative index
59 v_int2.insert(-1, 88)
60 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88, 4])
61
62 # delete negative index
63 del v_int2[-1]
64 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88])
65
66 v_int2.clear()
67 assert len(v_int2) == 0
68
69
70# Older PyPy's failed here, related to the PyPy's buffer protocol.
72 b = bytearray([1, 2, 3, 4])
73 v = m.VectorUChar(b)
74 assert v[1] == 2
75 v[2] = 5
76 mv = memoryview(v) # We expose the buffer interface
77 if not env.PY2:
78 assert mv[2] == 5
79 mv[2] = 6
80 else:
81 assert mv[2] == "\x05"
82 mv[2] = "\x06"
83 assert v[2] == 6
84
85 if not env.PY2:
86 mv = memoryview(b)
87 v = m.VectorUChar(mv[::2])
88 assert v[1] == 3
89
90 with pytest.raises(RuntimeError) as excinfo:
91 m.create_undeclstruct() # Undeclared struct contents, no buffer interface
92 assert "NumPy type info missing for " in str(excinfo.value)
93
94
96 np = pytest.importorskip("numpy")
97 a = np.array([1, 2, 3, 4], dtype=np.int32)
98 with pytest.raises(TypeError):
99 m.VectorInt(a)
100
101 a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uintc)
102 v = m.VectorInt(a[0, :])
103 assert len(v) == 4
104 assert v[2] == 3
105 ma = np.asarray(v)
106 ma[2] = 5
107 assert v[2] == 5
108
109 v = m.VectorInt(a[:, 1])
110 assert len(v) == 3
111 assert v[2] == 10
112
113 v = m.get_vectorstruct()
114 assert v[0].x == 5
115 ma = np.asarray(v)
116 ma[1]["x"] = 99
117 assert v[1].x == 99
118
119 v = m.VectorStruct(
120 np.zeros(
121 3,
122 dtype=np.dtype(
123 [("w", "bool"), ("x", "I"), ("y", "float64"), ("z", "bool")], align=True
124 ),
125 )
126 )
127 assert len(v) == 3
128
129 b = np.array([1, 2, 3, 4], dtype=np.uint8)
130 v = m.VectorUChar(b[::2])
131 assert v[1] == 3
132
133
135 import pybind11_cross_module_tests as cm
136
137 vv_c = cm.VectorBool()
138 for i in range(10):
139 vv_c.append(i % 2 == 0)
140 for i in range(10):
141 assert vv_c[i] == (i % 2 == 0)
142 assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
143
144
146 v_a = m.VectorEl()
147 v_a.append(m.El(1))
148 v_a.append(m.El(2))
149 assert str(v_a) == "VectorEl[El{1}, El{2}]"
150
151 vv_a = m.VectorVectorEl()
152 vv_a.append(v_a)
153 vv_b = vv_a[0]
154 assert str(vv_b) == "VectorEl[El{1}, El{2}]"
155
156
158 mm = m.MapStringDouble()
159 mm["a"] = 1
160 mm["b"] = 2.5
161
162 assert list(mm) == ["a", "b"]
163 assert str(mm) == "MapStringDouble{a: 1, b: 2.5}"
164 assert "b" in mm
165 assert "c" not in mm
166 assert 123 not in mm
167
168 # Check that keys, values, items are views, not merely iterable
169 keys = mm.keys()
170 values = mm.values()
171 items = mm.items()
172 assert list(keys) == ["a", "b"]
173 assert len(keys) == 2
174 assert "a" in keys
175 assert "c" not in keys
176 assert 123 not in keys
177 assert list(items) == [("a", 1), ("b", 2.5)]
178 assert len(items) == 2
179 assert ("b", 2.5) in items
180 assert "hello" not in items
181 assert ("b", 2.5, None) not in items
182 assert list(values) == [1, 2.5]
183 assert len(values) == 2
184 assert 1 in values
185 assert 2 not in values
186 # Check that views update when the map is updated
187 mm["c"] = -1
188 assert list(keys) == ["a", "b", "c"]
189 assert list(values) == [1, 2.5, -1]
190 assert list(items) == [("a", 1), ("b", 2.5), ("c", -1)]
191
192 um = m.UnorderedMapStringDouble()
193 um["ua"] = 1.1
194 um["ub"] = 2.6
195
196 assert sorted(list(um)) == ["ua", "ub"]
197 assert list(um.keys()) == list(um)
198 assert sorted(list(um.items())) == [("ua", 1.1), ("ub", 2.6)]
199 assert list(zip(um.keys(), um.values())) == list(um.items())
200 assert "UnorderedMapStringDouble" in str(um)
201
202
204 mc = m.MapStringDoubleConst()
205 mc["a"] = 10
206 mc["b"] = 20.5
207 assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
208
209 umc = m.UnorderedMapStringDoubleConst()
210 umc["a"] = 11
211 umc["b"] = 21.5
212
213 str(umc)
214
215
217 # std::vector
218 vnc = m.get_vnc(5)
219 for i in range(0, 5):
220 assert vnc[i].value == i + 1
221
222 for i, j in enumerate(vnc, start=1):
223 assert j.value == i
224
225 # std::deque
226 dnc = m.get_dnc(5)
227 for i in range(0, 5):
228 assert dnc[i].value == i + 1
229
230 i = 1
231 for j in dnc:
232 assert j.value == i
233 i += 1
234
235 # std::map
236 mnc = m.get_mnc(5)
237 for i in range(1, 6):
238 assert mnc[i].value == 10 * i
239
240 vsum = 0
241 for k, v in mnc.items():
242 assert v.value == 10 * k
243 vsum += v.value
244
245 assert vsum == 150
246
247 # std::unordered_map
248 mnc = m.get_umnc(5)
249 for i in range(1, 6):
250 assert mnc[i].value == 10 * i
251
252 vsum = 0
253 for k, v in mnc.items():
254 assert v.value == 10 * k
255 vsum += v.value
256
257 assert vsum == 150
258
259 # nested std::map<std::vector>
260 nvnc = m.get_nvnc(5)
261 for i in range(1, 6):
262 for j in range(0, 5):
263 assert nvnc[i][j].value == j + 1
264
265 # Note: maps do not have .values()
266 for _, v in nvnc.items():
267 for i, j in enumerate(v, start=1):
268 assert j.value == i
269
270 # nested std::map<std::map>
271 nmnc = m.get_nmnc(5)
272 for i in range(1, 6):
273 for j in range(10, 60, 10):
274 assert nmnc[i][j].value == 10 * j
275
276 vsum = 0
277 for _, v_o in nmnc.items():
278 for k_i, v_i in v_o.items():
279 assert v_i.value == 10 * k_i
280 vsum += v_i.value
281
282 assert vsum == 7500
283
284 # nested std::unordered_map<std::unordered_map>
285 numnc = m.get_numnc(5)
286 for i in range(1, 6):
287 for j in range(10, 60, 10):
288 assert numnc[i][j].value == 10 * j
289
290 vsum = 0
291 for _, v_o in numnc.items():
292 for k_i, v_i in v_o.items():
293 assert v_i.value == 10 * k_i
294 vsum += v_i.value
295
296 assert vsum == 7500
297
298
300 mm = m.MapStringDouble()
301 mm["a"] = 1
302 mm["b"] = 2.5
303
304 assert list(mm) == ["a", "b"]
305 assert list(mm.items()) == [("a", 1), ("b", 2.5)]
306 del mm["a"]
307 assert list(mm) == ["b"]
308 assert list(mm.items()) == [("b", 2.5)]
309
310 um = m.UnorderedMapStringDouble()
311 um["ua"] = 1.1
312 um["ub"] = 2.6
313
314 assert sorted(list(um)) == ["ua", "ub"]
315 assert sorted(list(um.items())) == [("ua", 1.1), ("ub", 2.6)]
316 del um["ua"]
317 assert sorted(list(um)) == ["ub"]
318 assert sorted(list(um.items())) == [("ub", 2.6)]
Definition: pytypes.h:1746
Definition: pytypes.h:1200
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2002
def test_map_string_double_const()
def test_noncopyable_containers()
def test_vector_buffer_numpy()