test_stl_binders.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pytest
4 
5 from 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  assert mv[2] == 5
78  mv[2] = 6
79  assert v[2] == 6
80 
81  mv = memoryview(b)
82  v = m.VectorUChar(mv[::2])
83  assert v[1] == 3
84 
85  with pytest.raises(RuntimeError) as excinfo:
86  m.create_undeclstruct() # Undeclared struct contents, no buffer interface
87  assert "NumPy type info missing for " in str(excinfo.value)
88 
89 
91  np = pytest.importorskip("numpy")
92  a = np.array([1, 2, 3, 4], dtype=np.int32)
93  with pytest.raises(TypeError):
94  m.VectorInt(a)
95 
96  a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uintc)
97  v = m.VectorInt(a[0, :])
98  assert len(v) == 4
99  assert v[2] == 3
100  ma = np.asarray(v)
101  ma[2] = 5
102  assert v[2] == 5
103 
104  v = m.VectorInt(a[:, 1])
105  assert len(v) == 3
106  assert v[2] == 10
107 
108  v = m.get_vectorstruct()
109  assert v[0].x == 5
110  ma = np.asarray(v)
111  ma[1]["x"] = 99
112  assert v[1].x == 99
113 
114  v = m.VectorStruct(
115  np.zeros(
116  3,
117  dtype=np.dtype(
118  [("w", "bool"), ("x", "I"), ("y", "float64"), ("z", "bool")], align=True
119  ),
120  )
121  )
122  assert len(v) == 3
123 
124  b = np.array([1, 2, 3, 4], dtype=np.uint8)
125  v = m.VectorUChar(b[::2])
126  assert v[1] == 3
127 
128 
130  import pybind11_cross_module_tests as cm
131 
132  vv_c = cm.VectorBool()
133  for i in range(10):
134  vv_c.append(i % 2 == 0)
135  for i in range(10):
136  assert vv_c[i] == (i % 2 == 0)
137  assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
138 
139 
141  v_a = m.VectorEl()
142  v_a.append(m.El(1))
143  v_a.append(m.El(2))
144  assert str(v_a) == "VectorEl[El{1}, El{2}]"
145 
146  vv_a = m.VectorVectorEl()
147  vv_a.append(v_a)
148  vv_b = vv_a[0]
149  assert str(vv_b) == "VectorEl[El{1}, El{2}]"
150 
151 
153  mm = m.MapStringDouble()
154  mm["a"] = 1
155  mm["b"] = 2.5
156 
157  assert list(mm) == ["a", "b"]
158  assert str(mm) == "MapStringDouble{a: 1, b: 2.5}"
159  assert "b" in mm
160  assert "c" not in mm
161  assert 123 not in mm
162 
163  # Check that keys, values, items are views, not merely iterable
164  keys = mm.keys()
165  values = mm.values()
166  items = mm.items()
167  assert list(keys) == ["a", "b"]
168  assert len(keys) == 2
169  assert "a" in keys
170  assert "c" not in keys
171  assert 123 not in keys
172  assert list(items) == [("a", 1), ("b", 2.5)]
173  assert len(items) == 2
174  assert ("b", 2.5) in items
175  assert "hello" not in items
176  assert ("b", 2.5, None) not in items
177  assert list(values) == [1, 2.5]
178  assert len(values) == 2
179  assert 1 in values
180  assert 2 not in values
181  # Check that views update when the map is updated
182  mm["c"] = -1
183  assert list(keys) == ["a", "b", "c"]
184  assert list(values) == [1, 2.5, -1]
185  assert list(items) == [("a", 1), ("b", 2.5), ("c", -1)]
186 
187  um = m.UnorderedMapStringDouble()
188  um["ua"] = 1.1
189  um["ub"] = 2.6
190 
191  assert sorted(um) == ["ua", "ub"]
192  assert list(um.keys()) == list(um)
193  assert sorted(um.items()) == [("ua", 1.1), ("ub", 2.6)]
194  assert list(zip(um.keys(), um.values())) == list(um.items())
195  assert "UnorderedMapStringDouble" in str(um)
196 
197 
199  mc = m.MapStringDoubleConst()
200  mc["a"] = 10
201  mc["b"] = 20.5
202  assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
203 
204  umc = m.UnorderedMapStringDoubleConst()
205  umc["a"] = 11
206  umc["b"] = 21.5
207 
208  str(umc)
209 
210 
212  # std::vector
213  vnc = m.get_vnc(5)
214  for i in range(5):
215  assert vnc[i].value == i + 1
216 
217  for i, j in enumerate(vnc, start=1):
218  assert j.value == i
219 
220  # std::deque
221  dnc = m.get_dnc(5)
222  for i in range(5):
223  assert dnc[i].value == i + 1
224 
225  i = 1
226  for j in dnc:
227  assert j.value == i
228  i += 1
229 
230  # std::map
231  mnc = m.get_mnc(5)
232  for i in range(1, 6):
233  assert mnc[i].value == 10 * i
234 
235  vsum = 0
236  for k, v in mnc.items():
237  assert v.value == 10 * k
238  vsum += v.value
239 
240  assert vsum == 150
241 
242  # std::unordered_map
243  mnc = m.get_umnc(5)
244  for i in range(1, 6):
245  assert mnc[i].value == 10 * i
246 
247  vsum = 0
248  for k, v in mnc.items():
249  assert v.value == 10 * k
250  vsum += v.value
251 
252  assert vsum == 150
253 
254  # nested std::map<std::vector>
255  nvnc = m.get_nvnc(5)
256  for i in range(1, 6):
257  for j in range(5):
258  assert nvnc[i][j].value == j + 1
259 
260  # Note: maps do not have .values()
261  for _, v in nvnc.items():
262  for i, j in enumerate(v, start=1):
263  assert j.value == i
264 
265  # nested std::map<std::map>
266  nmnc = m.get_nmnc(5)
267  for i in range(1, 6):
268  for j in range(10, 60, 10):
269  assert nmnc[i][j].value == 10 * j
270 
271  vsum = 0
272  for _, v_o in nmnc.items():
273  for k_i, v_i in v_o.items():
274  assert v_i.value == 10 * k_i
275  vsum += v_i.value
276 
277  assert vsum == 7500
278 
279  # nested std::unordered_map<std::unordered_map>
280  numnc = m.get_numnc(5)
281  for i in range(1, 6):
282  for j in range(10, 60, 10):
283  assert numnc[i][j].value == 10 * j
284 
285  vsum = 0
286  for _, v_o in numnc.items():
287  for k_i, v_i in v_o.items():
288  assert v_i.value == 10 * k_i
289  vsum += v_i.value
290 
291  assert vsum == 7500
292 
293 
295  mm = m.MapStringDouble()
296  mm["a"] = 1
297  mm["b"] = 2.5
298 
299  assert list(mm) == ["a", "b"]
300  assert list(mm.items()) == [("a", 1), ("b", 2.5)]
301  del mm["a"]
302  assert list(mm) == ["b"]
303  assert list(mm.items()) == [("b", 2.5)]
304 
305  um = m.UnorderedMapStringDouble()
306  um["ua"] = 1.1
307  um["ub"] = 2.6
308 
309  assert sorted(um) == ["ua", "ub"]
310  assert sorted(um.items()) == [("ua", 1.1), ("ub", 2.6)]
311  del um["ua"]
312  assert sorted(um) == ["ub"]
313  assert sorted(um.items()) == [("ub", 2.6)]
314 
315 
317  map_string_double = m.MapStringDouble()
318  unordered_map_string_double = m.UnorderedMapStringDouble()
319  map_string_double_const = m.MapStringDoubleConst()
320  unordered_map_string_double_const = m.UnorderedMapStringDoubleConst()
321 
322  assert map_string_double.keys().__class__.__name__ == "KeysView"
323  assert map_string_double.values().__class__.__name__ == "ValuesView"
324  assert map_string_double.items().__class__.__name__ == "ItemsView"
325 
326  keys_type = type(map_string_double.keys())
327  assert type(unordered_map_string_double.keys()) is keys_type
328  assert type(map_string_double_const.keys()) is keys_type
329  assert type(unordered_map_string_double_const.keys()) is keys_type
330 
331  values_type = type(map_string_double.values())
332  assert type(unordered_map_string_double.values()) is values_type
333  assert type(map_string_double_const.values()) is values_type
334  assert type(unordered_map_string_double_const.values()) is values_type
335 
336  items_type = type(map_string_double.items())
337  assert type(unordered_map_string_double.items()) is items_type
338  assert type(map_string_double_const.items()) is items_type
339  assert type(unordered_map_string_double_const.items()) is items_type
340 
341  map_string_float = m.MapStringFloat()
342  unordered_map_string_float = m.UnorderedMapStringFloat()
343 
344  assert type(map_string_float.keys()) is keys_type
345  assert type(unordered_map_string_float.keys()) is keys_type
346  assert type(map_string_float.values()) is values_type
347  assert type(unordered_map_string_float.values()) is values_type
348  assert type(map_string_float.items()) is items_type
349  assert type(unordered_map_string_float.items()) is items_type
350 
351  map_pair_double_int_int32 = m.MapPairDoubleIntInt32()
352  map_pair_double_int_int64 = m.MapPairDoubleIntInt64()
353 
354  assert type(map_pair_double_int_int32.values()) is values_type
355  assert type(map_pair_double_int_int64.values()) is values_type
356 
357  map_int_object = m.MapIntObject()
358  map_string_object = m.MapStringObject()
359 
360  assert type(map_int_object.keys()) is keys_type
361  assert type(map_string_object.keys()) is keys_type
362  assert type(map_int_object.items()) is items_type
363  assert type(map_string_object.items()) is items_type
364 
365 
367  recursive_vector = m.RecursiveVector()
368  recursive_vector.append(m.RecursiveVector())
369  recursive_vector[0].append(m.RecursiveVector())
370  recursive_vector[0].append(m.RecursiveVector())
371  # Can't use len() since test_stl_binders.cpp does not include stl.h,
372  # so the necessary conversion is missing
373  assert recursive_vector[0].count(m.RecursiveVector()) == 2
374 
375 
377  recursive_map = m.RecursiveMap()
378  recursive_map[100] = m.RecursiveMap()
379  recursive_map[100][101] = m.RecursiveMap()
380  recursive_map[100][102] = m.RecursiveMap()
381  assert list(recursive_map[100].keys()) == [101, 102]
382 
383 
385  vec = m.UserVectorLike()
386  vec.append(2)
387  assert vec[0] == 2
388  assert len(vec) == 1
389 
390 
392  map = m.UserMapLike()
393  map[33] = 44
394  assert map[33] == 44
395  assert len(map) == 1
list
Definition: pytypes.h:2166
test_stl_binders.test_vector_buffer
def test_vector_buffer()
Definition: test_stl_binders.py:71
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
test_stl_binders.test_map_string_double_const
def test_map_string_double_const()
Definition: test_stl_binders.py:198
type
Definition: pytypes.h:1525
test_stl_binders.test_vector_custom
def test_vector_custom()
Definition: test_stl_binders.py:140
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
test_stl_binders.test_recursive_map
def test_recursive_map()
Definition: test_stl_binders.py:376
test_stl_binders.test_recursive_vector
def test_recursive_vector()
Definition: test_stl_binders.py:366
test_stl_binders.test_vector_int
def test_vector_int()
Definition: test_stl_binders.py:8
bytearray
Definition: pytypes.h:1756
memoryview
Definition: pytypes.h:2288
test_stl_binders.test_user_like_map
def test_user_like_map()
Definition: test_stl_binders.py:391
test_stl_binders.test_map_string_double
def test_map_string_double()
Definition: test_stl_binders.py:152
test_stl_binders.test_map_delitem
def test_map_delitem()
Definition: test_stl_binders.py:294
str
Definition: pytypes.h:1558
test_stl_binders.test_vector_bool
def test_vector_bool()
Definition: test_stl_binders.py:129
test_stl_binders.test_map_view_types
def test_map_view_types()
Definition: test_stl_binders.py:316
test_stl_binders.test_vector_buffer_numpy
def test_vector_buffer_numpy()
Definition: test_stl_binders.py:90
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
test_stl_binders.test_user_vector_like
def test_user_vector_like()
Definition: test_stl_binders.py:384
test_stl_binders.test_noncopyable_containers
def test_noncopyable_containers()
Definition: test_stl_binders.py:211


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:16:02