Skip to content

Commit a3657ee

Browse files
committed
created tests for copyability
1 parent 2ff3cb0 commit a3657ee

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

tests/test_stl_binders.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ template <class Map> Map *times_ten(int n) {
5454
return m;
5555
}
5656

57+
template <class NestMap> NestMap *times_hundred(int n) {
58+
auto m = new NestMap();
59+
for (int i = 1; i <= n; i++)
60+
for (int j = 1; j <= n; j++)
61+
(*m)[i].emplace(int(j*10), E_nc(100*j));
62+
return m;
63+
}
64+
5765
TEST_SUBMODULE(stl_binders, m) {
5866
// test_vector_int
5967
py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
@@ -85,6 +93,20 @@ TEST_SUBMODULE(stl_binders, m) {
8593
m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
8694
py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
8795
m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
96+
// Issue #1885: binding nested std::map<X, Container<E>> with E non-copyable
97+
py::bind_map<std::map<int, std::vector<E_nc>>>(m, "MapVecENC");
98+
m.def("get_nvnc", [](int n)
99+
{
100+
auto m = new std::map<int, std::vector<E_nc>>();
101+
for (int i = 1; i <= n; i++)
102+
for (int j = 1; j <= n; j++)
103+
(*m)[i].emplace_back(j);
104+
return m;
105+
}, py::return_value_policy::reference);
106+
py::bind_map<std::map<int, std::map<int, E_nc>>>(m, "MapMapENC");
107+
m.def("get_nmnc", &times_hundred<std::map<int, std::map<int, E_nc>>>, py::return_value_policy::reference);
108+
py::bind_map<std::unordered_map<int, std::unordered_map<int, E_nc>>>(m, "UmapUmapENC");
109+
m.def("get_numnc", &times_hundred<std::unordered_map<int, std::unordered_map<int, E_nc>>>, py::return_value_policy::reference);
88110

89111
// test_vector_buffer
90112
py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());

tests/test_stl_binders.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,44 @@ def test_noncopyable_containers():
212212

213213
assert vsum == 150
214214

215+
# nested std::map<std::vector>
216+
nvnc = m.get_nvnc(5)
217+
for i in range(1, 6):
218+
for j in range(0, 5):
219+
assert nvnc[i][j].value == j + 1
220+
221+
for k, v in nvnc.items():
222+
for i, j in enumerate(v, start=1):
223+
assert j.value == i
224+
225+
# nested std::map<std::map>
226+
nmnc = m.get_nmnc(5)
227+
for i in range(1, 6):
228+
for j in range(10, 60, 10):
229+
assert nmnc[i][j].value == 10 * j
230+
231+
vsum = 0
232+
for k_o, v_o in nmnc.items():
233+
for k_i, v_i in v_o.items():
234+
assert v_i.value == 10 * k_i
235+
vsum += v_i.value
236+
237+
assert vsum == 7500
238+
239+
# nested std::unordered_map<std::unordered_map>
240+
numnc = m.get_numnc(5)
241+
for i in range(1, 6):
242+
for j in range(10, 60, 10):
243+
assert numnc[i][j].value == 10 * j
244+
245+
vsum = 0
246+
for k_o, v_o in numnc.items():
247+
for k_i, v_i in v_o.items():
248+
assert v_i.value == 10 * k_i
249+
vsum += v_i.value
250+
251+
assert vsum == 7500
252+
215253

216254
def test_map_delitem():
217255
mm = m.MapStringDouble()

0 commit comments

Comments
 (0)