3 from pybind11_tests
import stl_binders
as m
7 v_int = m.VectorInt([0, 0])
9 assert bool(v_int)
is True 12 v_int1 = m.VectorInt(x
for x
in range(5))
13 assert v_int1 == m.VectorInt([0, 1, 2, 3, 4])
15 v_int2 = m.VectorInt([0, 0])
16 assert v_int == v_int2
18 assert v_int != v_int2
25 assert str(v_int2) ==
"VectorInt[3, 2, 1, 0, 1, 2, 3]" 26 with pytest.raises(IndexError):
31 assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
33 assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
35 assert v_int2 == m.VectorInt([0, 99, 2, 3])
37 v_int2.extend(m.VectorInt([4, 5]))
38 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5])
41 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
44 with pytest.raises(RuntimeError):
45 v_int2.extend([8,
"a"])
47 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
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])
54 assert v_int2[-1] == 4
58 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88, 4])
62 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88])
65 assert len(v_int2) == 0
80 v = m.VectorUChar(mv[::2])
83 with pytest.raises(RuntimeError)
as excinfo:
84 m.create_undeclstruct()
85 assert "NumPy type info missing for " in str(excinfo.value)
89 np = pytest.importorskip(
"numpy")
90 a = np.array([1, 2, 3, 4], dtype=np.int32)
91 with pytest.raises(TypeError):
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, :])
102 v = m.VectorInt(a[:, 1])
106 v = m.get_vectorstruct()
116 [(
"w",
"bool"), (
"x",
"I"), (
"y",
"float64"), (
"z",
"bool")], align=
True 122 b = np.array([1, 2, 3, 4], dtype=np.uint8)
123 v = m.VectorUChar(b[::2])
128 import pybind11_cross_module_tests
as cm
130 vv_c = cm.VectorBool()
132 vv_c.append(i % 2 == 0)
134 assert vv_c[i] == (i % 2 == 0)
135 assert str(vv_c) ==
"VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]" 142 assert str(v_a) ==
"VectorEl[El{1}, El{2}]" 144 vv_a = m.VectorVectorEl()
147 assert str(vv_b) ==
"VectorEl[El{1}, El{2}]" 151 mm = m.MapStringDouble()
155 assert list(mm) == [
"a",
"b"]
156 assert str(mm) ==
"MapStringDouble{a: 1, b: 2.5}" 165 assert list(keys) == [
"a",
"b"]
166 assert len(keys) == 2
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
178 assert 2
not in values
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)]
185 um = m.UnorderedMapStringDouble()
189 assert sorted(
list(um)) == [
"ua",
"ub"]
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)
197 mc = m.MapStringDoubleConst()
200 assert str(mc) ==
"MapStringDoubleConst{a: 10, b: 20.5}" 202 umc = m.UnorderedMapStringDoubleConst()
212 for i
in range(0, 5):
213 assert vnc[i].value == i + 1
215 for i, j
in enumerate(vnc, start=1):
220 for i
in range(0, 5):
221 assert dnc[i].value == i + 1
230 for i
in range(1, 6):
231 assert mnc[i].value == 10 * i
234 for k, v
in mnc.items():
235 assert v.value == 10 * k
242 for i
in range(1, 6):
243 assert mnc[i].value == 10 * i
246 for k, v
in mnc.items():
247 assert v.value == 10 * k
254 for i
in range(1, 6):
255 for j
in range(0, 5):
256 assert nvnc[i][j].value == j + 1
259 for _, v
in nvnc.items():
260 for i, j
in enumerate(v, start=1):
265 for i
in range(1, 6):
266 for j
in range(10, 60, 10):
267 assert nmnc[i][j].value == 10 * j
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
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
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
293 mm = m.MapStringDouble()
297 assert list(mm) == [
"a",
"b"]
298 assert list(mm.items()) == [(
"a", 1), (
"b", 2.5)]
300 assert list(mm) == [
"b"]
301 assert list(mm.items()) == [(
"b", 2.5)]
303 um = m.UnorderedMapStringDouble()
307 assert sorted(
list(um)) == [
"ua",
"ub"]
308 assert sorted(
list(um.items())) == [(
"ua", 1.1), (
"ub", 2.6)]
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()
Double_ range(const Point2_ &p, const Point2_ &q)
size_t len(handle h)
Get the length of a Python object.
def test_map_string_double()