test_stl_binders.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pytest
3 
4 import env # noqa: F401
5 
6 from pybind11_tests import stl_binders as m
7 
8 
10  v_int = m.VectorInt([0, 0])
11  assert len(v_int) == 2
12  assert bool(v_int) is True
13 
14  # test construction from a generator
15  v_int1 = m.VectorInt(x for x in range(5))
16  assert v_int1 == m.VectorInt([0, 1, 2, 3, 4])
17 
18  v_int2 = m.VectorInt([0, 0])
19  assert v_int == v_int2
20  v_int2[1] = 1
21  assert v_int != v_int2
22 
23  v_int2.append(2)
24  v_int2.insert(0, 1)
25  v_int2.insert(0, 2)
26  v_int2.insert(0, 3)
27  v_int2.insert(6, 3)
28  assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
29  with pytest.raises(IndexError):
30  v_int2.insert(8, 4)
31 
32  v_int.append(99)
33  v_int2[2:-2] = v_int
34  assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
35  del v_int2[1:3]
36  assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
37  del v_int2[0]
38  assert v_int2 == m.VectorInt([0, 99, 2, 3])
39 
40  v_int2.extend(m.VectorInt([4, 5]))
41  assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5])
42 
43  v_int2.extend([6, 7])
44  assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
45 
46  # test error handling, and that the vector is unchanged
47  with pytest.raises(RuntimeError):
48  v_int2.extend([8, 'a'])
49 
50  assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
51 
52  # test extending from a generator
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])
55 
56  # test negative indexing
57  assert v_int2[-1] == 4
58 
59  # insert with negative index
60  v_int2.insert(-1, 88)
61  assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88, 4])
62 
63  # delete negative index
64  del v_int2[-1]
65  assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88])
66 
67  v_int2.clear()
68  assert len(v_int2) == 0
69 
70 
71 # Older PyPy's failed here, related to the PyPy's buffer protocol.
73  b = bytearray([1, 2, 3, 4])
74  v = m.VectorUChar(b)
75  assert v[1] == 2
76  v[2] = 5
77  mv = memoryview(v) # We expose the buffer interface
78  if not env.PY2:
79  assert mv[2] == 5
80  mv[2] = 6
81  else:
82  assert mv[2] == '\x05'
83  mv[2] = '\x06'
84  assert v[2] == 6
85 
86  if not env.PY2:
87  mv = memoryview(b)
88  v = m.VectorUChar(mv[::2])
89  assert v[1] == 3
90 
91  with pytest.raises(RuntimeError) as excinfo:
92  m.create_undeclstruct() # Undeclared struct contents, no buffer interface
93  assert "NumPy type info missing for " in str(excinfo.value)
94 
95 
97  np = pytest.importorskip("numpy")
98  a = np.array([1, 2, 3, 4], dtype=np.int32)
99  with pytest.raises(TypeError):
100  m.VectorInt(a)
101 
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, :])
104  assert len(v) == 4
105  assert v[2] == 3
106  ma = np.asarray(v)
107  ma[2] = 5
108  assert v[2] == 5
109 
110  v = m.VectorInt(a[:, 1])
111  assert len(v) == 3
112  assert v[2] == 10
113 
114  v = m.get_vectorstruct()
115  assert v[0].x == 5
116  ma = np.asarray(v)
117  ma[1]['x'] = 99
118  assert v[1].x == 99
119 
120  v = m.VectorStruct(np.zeros(3, dtype=np.dtype([('w', 'bool'), ('x', 'I'),
121  ('y', 'float64'), ('z', 'bool')], align=True)))
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 list(mm.items()) == [('a', 1), ('b', 2.5)]
159  assert str(mm) == "MapStringDouble{a: 1, b: 2.5}"
160 
161  um = m.UnorderedMapStringDouble()
162  um['ua'] = 1.1
163  um['ub'] = 2.6
164 
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)
168 
169 
171  mc = m.MapStringDoubleConst()
172  mc['a'] = 10
173  mc['b'] = 20.5
174  assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
175 
176  umc = m.UnorderedMapStringDoubleConst()
177  umc['a'] = 11
178  umc['b'] = 21.5
179 
180  str(umc)
181 
182 
184  # std::vector
185  vnc = m.get_vnc(5)
186  for i in range(0, 5):
187  assert vnc[i].value == i + 1
188 
189  for i, j in enumerate(vnc, start=1):
190  assert j.value == i
191 
192  # std::deque
193  dnc = m.get_dnc(5)
194  for i in range(0, 5):
195  assert dnc[i].value == i + 1
196 
197  i = 1
198  for j in dnc:
199  assert(j.value == i)
200  i += 1
201 
202  # std::map
203  mnc = m.get_mnc(5)
204  for i in range(1, 6):
205  assert mnc[i].value == 10 * i
206 
207  vsum = 0
208  for k, v in mnc.items():
209  assert v.value == 10 * k
210  vsum += v.value
211 
212  assert vsum == 150
213 
214  # std::unordered_map
215  mnc = m.get_umnc(5)
216  for i in range(1, 6):
217  assert mnc[i].value == 10 * i
218 
219  vsum = 0
220  for k, v in mnc.items():
221  assert v.value == 10 * k
222  vsum += v.value
223 
224  assert vsum == 150
225 
226  # nested std::map<std::vector>
227  nvnc = m.get_nvnc(5)
228  for i in range(1, 6):
229  for j in range(0, 5):
230  assert nvnc[i][j].value == j + 1
231 
232  # Note: maps do not have .values()
233  for _, v in nvnc.items():
234  for i, j in enumerate(v, start=1):
235  assert j.value == i
236 
237  # nested std::map<std::map>
238  nmnc = m.get_nmnc(5)
239  for i in range(1, 6):
240  for j in range(10, 60, 10):
241  assert nmnc[i][j].value == 10 * j
242 
243  vsum = 0
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
247  vsum += v_i.value
248 
249  assert vsum == 7500
250 
251  # nested std::unordered_map<std::unordered_map>
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
256 
257  vsum = 0
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
261  vsum += v_i.value
262 
263  assert vsum == 7500
264 
265 
267  mm = m.MapStringDouble()
268  mm['a'] = 1
269  mm['b'] = 2.5
270 
271  assert list(mm) == ['a', 'b']
272  assert list(mm.items()) == [('a', 1), ('b', 2.5)]
273  del mm['a']
274  assert list(mm) == ['b']
275  assert list(mm.items()) == [('b', 2.5)]
276 
277  um = m.UnorderedMapStringDouble()
278  um['ua'] = 1.1
279  um['ub'] = 2.6
280 
281  assert sorted(list(um)) == ['ua', 'ub']
282  assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)]
283  del um['ua']
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()
Definition: pytypes.h:928
Definition: pytypes.h:1301
size_t len(handle h)
Definition: pytypes.h:1514


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04