MCPcopy Create free account
hub / github.com/pybind/pybind11 / TEST_SUBMODULE

Function TEST_SUBMODULE

tests/test_stl.cpp:180–667  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

178}
179
180TEST_SUBMODULE(stl, m) {
181 // test_vector
182 m.def("cast_vector", []() { return std::vector<int>{1}; });
183 m.def("load_vector", [](const std::vector<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
184 // `std::vector<bool>` is special because it returns proxy objects instead of references
185 m.def("cast_bool_vector", []() { return std::vector<bool>{true, false}; });
186 m.def("load_bool_vector",
187 [](const std::vector<bool> &v) { return v.at(0) == true && v.at(1) == false; });
188 // Unnumbered regression (caused by #936): pointers to stl containers aren't castable
189 m.def(
190 "cast_ptr_vector",
191 []() {
192 // Using no-destructor idiom to side-step warnings from overzealous compilers.
193 static auto *v = new std::vector<RValueCaster>{2};
194 return v;
195 },
196 py::return_value_policy::reference);
197
198 // test_deque
199 m.def("cast_deque", []() { return std::deque<int>{1}; });
200 m.def("load_deque", [](const std::deque<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
201
202 // test_array
203 m.def("cast_array", []() { return std::array<int, 2>{{1, 2}}; });
204 m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });
205
206 struct NoDefaultCtor {
207 explicit constexpr NoDefaultCtor(int val) : val{val} {}
208 int val;
209 };
210
211 struct NoDefaultCtorArray {
212 explicit constexpr NoDefaultCtorArray(int i)
213 : arr{{NoDefaultCtor(10 + i), NoDefaultCtor(20 + i)}} {}
214 std::array<NoDefaultCtor, 2> arr;
215 };
216
217 // test_array_no_default_ctor
218 py::class_<NoDefaultCtor>(m, "NoDefaultCtor").def_readonly("val", &NoDefaultCtor::val);
219 py::class_<NoDefaultCtorArray>(m, "NoDefaultCtorArray")
220 .def(py::init<int>())
221 .def_readwrite("arr", &NoDefaultCtorArray::arr);
222
223 // test_valarray
224 m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; });
225 m.def("load_valarray", [](const std::valarray<int> &v) {
226 return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9;
227 });
228
229 // test_map
230 m.def("cast_map", []() { return std::map<std::string, std::string>{{"key", "value"}}; });
231 m.def("load_map", [](const std::map<std::string, std::string> &map) {
232 return map.at("key") == "value" && map.at("key2") == "value2";
233 });
234
235 // test_set
236 m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; });
237 m.def("load_set", [](const std::set<std::string> &set) {

Callers

nothing calls this directly

Calls 15

make_tupleFunction · 0.85
visitorClass · 0.85
refClass · 0.85
pass_std_vector_intFunction · 0.85
atMethod · 0.80
emplace_backMethod · 0.80
attrMethod · 0.80
arg_vClass · 0.50
argClass · 0.50
sizeMethod · 0.45
valueMethod · 0.45
reserveMethod · 0.45

Tested by

no test coverage detected