tests: check simple iteration of pairs (#3296)

This commit is contained in:
Henry Schreiner
2021-09-23 08:01:06 -04:00
committed by GitHub
parent 2fa3fcfda5
commit 5f46e47da8
2 changed files with 22 additions and 0 deletions

View File

@@ -295,6 +295,8 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
public:
explicit IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {}
const std::pair<int, int>* begin() const { return data_.data(); }
// .end() only required for py::make_iterator(self) overload
const std::pair<int, int>* end() const { return data_.data() + data_.size(); }
private:
std::vector<std::pair<int, int>> data_;
};
@@ -306,6 +308,17 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
.def("nonzero_keys", [](const IntPairs& s) {
return py::make_key_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
}, py::keep_alive<0, 1>())
.def("simple_iterator", [](IntPairs& self) {
return py::make_iterator(self);
}, py::keep_alive<0, 1>())
.def("simple_keys", [](IntPairs& self) {
return py::make_key_iterator(self);
}, py::keep_alive<0, 1>())
// test iterator with keep_alive (doesn't work so not used at runtime, but tests compile)
.def("make_iterator_keep_alive", [](IntPairs& self) {
return py::make_iterator(self, py::keep_alive<0, 1>());
}, py::keep_alive<0, 1>())
;