1 from __future__
import annotations
5 from pybind11_tests
import stl_binders
as m
9 v_int = m.VectorInt([0, 0])
10 assert len(v_int) == 2
11 assert bool(v_int)
is True
14 v_int1 = m.VectorInt(x
for x
in range(5))
15 assert v_int1 == m.VectorInt([0, 1, 2, 3, 4])
17 v_int2 = m.VectorInt([0, 0])
18 assert v_int == v_int2
20 assert v_int != v_int2
27 assert str(v_int2) ==
"VectorInt[3, 2, 1, 0, 1, 2, 3]"
28 with pytest.raises(IndexError):
33 assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
35 assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
37 assert v_int2 == m.VectorInt([0, 99, 2, 3])
39 v_int2.extend(m.VectorInt([4, 5]))
40 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5])
43 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
46 with pytest.raises(RuntimeError):
47 v_int2.extend([8,
"a"])
49 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
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])
56 assert v_int2[-1] == 4
60 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88, 4])
64 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88])
67 assert len(v_int2) == 0
82 v = m.VectorUChar(mv[::2])
85 with pytest.raises(RuntimeError)
as excinfo:
86 m.create_undeclstruct()
87 assert "NumPy type info missing for " in str(excinfo.value)
91 np = pytest.importorskip(
"numpy")
92 a = np.array([1, 2, 3, 4], dtype=np.int32)
93 with pytest.raises(TypeError):
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, :])
104 v = m.VectorInt(a[:, 1])
108 v = m.get_vectorstruct()
118 [(
"w",
"bool"), (
"x",
"I"), (
"y",
"float64"), (
"z",
"bool")], align=
True
124 b = np.array([1, 2, 3, 4], dtype=np.uint8)
125 v = m.VectorUChar(b[::2])
130 import pybind11_cross_module_tests
as cm
132 vv_c = cm.VectorBool()
134 vv_c.append(i % 2 == 0)
136 assert vv_c[i] == (i % 2 == 0)
137 assert str(vv_c) ==
"VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
144 assert str(v_a) ==
"VectorEl[El{1}, El{2}]"
146 vv_a = m.VectorVectorEl()
149 assert str(vv_b) ==
"VectorEl[El{1}, El{2}]"
153 mm = m.MapStringDouble()
157 assert list(mm) == [
"a",
"b"]
158 assert str(mm) ==
"MapStringDouble{a: 1, b: 2.5}"
167 assert list(keys) == [
"a",
"b"]
168 assert len(keys) == 2
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
180 assert 2
not in values
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)]
187 um = m.UnorderedMapStringDouble()
191 assert sorted(um) == [
"ua",
"ub"]
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)
199 mc = m.MapStringDoubleConst()
202 assert str(mc) ==
"MapStringDoubleConst{a: 10, b: 20.5}"
204 umc = m.UnorderedMapStringDoubleConst()
215 assert vnc[i].value == i + 1
217 for i, j
in enumerate(vnc, start=1):
223 assert dnc[i].value == i + 1
232 for i
in range(1, 6):
233 assert mnc[i].value == 10 * i
236 for k, v
in mnc.items():
237 assert v.value == 10 * k
244 for i
in range(1, 6):
245 assert mnc[i].value == 10 * i
248 for k, v
in mnc.items():
249 assert v.value == 10 * k
256 for i
in range(1, 6):
258 assert nvnc[i][j].value == j + 1
261 for _, v
in nvnc.items():
262 for i, j
in enumerate(v, start=1):
267 for i
in range(1, 6):
268 for j
in range(10, 60, 10):
269 assert nmnc[i][j].value == 10 * j
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
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
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
295 mm = m.MapStringDouble()
299 assert list(mm) == [
"a",
"b"]
300 assert list(mm.items()) == [(
"a", 1), (
"b", 2.5)]
302 assert list(mm) == [
"b"]
303 assert list(mm.items()) == [(
"b", 2.5)]
305 um = m.UnorderedMapStringDouble()
309 assert sorted(um) == [
"ua",
"ub"]
310 assert sorted(um.items()) == [(
"ua", 1.1), (
"ub", 2.6)]
312 assert sorted(um) == [
"ub"]
313 assert sorted(um.items()) == [(
"ub", 2.6)]
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()
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"
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
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
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
341 map_string_float = m.MapStringFloat()
342 unordered_map_string_float = m.UnorderedMapStringFloat()
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
351 map_pair_double_int_int32 = m.MapPairDoubleIntInt32()
352 map_pair_double_int_int64 = m.MapPairDoubleIntInt64()
354 assert type(map_pair_double_int_int32.values())
is values_type
355 assert type(map_pair_double_int_int64.values())
is values_type
357 map_int_object = m.MapIntObject()
358 map_string_object = m.MapStringObject()
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
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())
373 assert recursive_vector[0].count(m.RecursiveVector()) == 2
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]
385 vec = m.UserVectorLike()
392 map = m.UserMapLike()