6 from pybind11_tests
import stl_binders
as m
10 v_int = m.VectorInt([0, 0])
11 assert len(v_int) == 2
12 assert bool(v_int)
is True 15 v_int1 = m.VectorInt(x
for x
in range(5))
16 assert v_int1 == m.VectorInt([0, 1, 2, 3, 4])
18 v_int2 = m.VectorInt([0, 0])
19 assert v_int == v_int2
21 assert v_int != v_int2
28 assert str(v_int2) ==
"VectorInt[3, 2, 1, 0, 1, 2, 3]" 29 with pytest.raises(IndexError):
34 assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
36 assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
38 assert v_int2 == m.VectorInt([0, 99, 2, 3])
40 v_int2.extend(m.VectorInt([4, 5]))
41 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5])
44 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
47 with pytest.raises(RuntimeError):
48 v_int2.extend([8,
'a'])
50 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
53 v_int2.extend(x
for x
in range(5))
54 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4])
57 assert v_int2[-1] == 4
61 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88, 4])
65 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88])
68 assert len(v_int2) == 0
73 b = bytearray([1, 2, 3, 4])
82 assert mv[2] ==
'\x05' 88 v = m.VectorUChar(mv[::2])
91 with pytest.raises(RuntimeError)
as excinfo:
92 m.create_undeclstruct()
93 assert "NumPy type info missing for " in str(excinfo.value)
97 np = pytest.importorskip(
"numpy")
98 a = np.array([1, 2, 3, 4], dtype=np.int32)
99 with pytest.raises(TypeError):
102 a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uintc)
103 v = m.VectorInt(a[0, :])
110 v = m.VectorInt(a[:, 1])
114 v = m.get_vectorstruct()
120 v = m.VectorStruct(np.zeros(3, dtype=np.dtype([(
'w',
'bool'), (
'x',
'I'),
121 (
'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 list(mm.items()) == [(
'a', 1), (
'b', 2.5)]
159 assert str(mm) ==
"MapStringDouble{a: 1, b: 2.5}" 161 um = m.UnorderedMapStringDouble()
165 assert sorted(
list(um)) == [
'ua',
'ub']
166 assert sorted(
list(um.items())) == [(
'ua', 1.1), (
'ub', 2.6)]
167 assert "UnorderedMapStringDouble" in str(um)
171 mc = m.MapStringDoubleConst()
174 assert str(mc) ==
"MapStringDoubleConst{a: 10, b: 20.5}" 176 umc = m.UnorderedMapStringDoubleConst()
186 for i
in range(0, 5):
187 assert vnc[i].value == i + 1
189 for i, j
in enumerate(vnc, start=1):
194 for i
in range(0, 5):
195 assert dnc[i].value == i + 1
204 for i
in range(1, 6):
205 assert mnc[i].value == 10 * i
208 for k, v
in mnc.items():
209 assert v.value == 10 * k
216 for i
in range(1, 6):
217 assert mnc[i].value == 10 * i
220 for k, v
in mnc.items():
221 assert v.value == 10 * k
228 for i
in range(1, 6):
229 for j
in range(0, 5):
230 assert nvnc[i][j].value == j + 1
233 for _, v
in nvnc.items():
234 for i, j
in enumerate(v, start=1):
239 for i
in range(1, 6):
240 for j
in range(10, 60, 10):
241 assert nmnc[i][j].value == 10 * j
244 for _, v_o
in nmnc.items():
245 for k_i, v_i
in v_o.items():
246 assert v_i.value == 10 * k_i
252 numnc = m.get_numnc(5)
253 for i
in range(1, 6):
254 for j
in range(10, 60, 10):
255 assert numnc[i][j].value == 10 * j
258 for _, v_o
in numnc.items():
259 for k_i, v_i
in v_o.items():
260 assert v_i.value == 10 * k_i
267 mm = m.MapStringDouble()
271 assert list(mm) == [
'a',
'b']
272 assert list(mm.items()) == [(
'a', 1), (
'b', 2.5)]
274 assert list(mm) == [
'b']
275 assert list(mm.items()) == [(
'b', 2.5)]
277 um = m.UnorderedMapStringDouble()
281 assert sorted(
list(um)) == [
'ua',
'ub']
282 assert sorted(
list(um.items())) == [(
'ua', 1.1), (
'ub', 2.6)]
284 assert sorted(
list(um)) == [
'ub']
285 assert sorted(
list(um.items())) == [(
'ub', 2.6)]
def test_map_string_double_const()
def test_vector_buffer_numpy()
def test_noncopyable_containers()
def test_map_string_double()