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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46