Skip to content

Commit 5f46e47

Browse files
authored
tests: check simple iteration of pairs (#3296)
1 parent 2fa3fcf commit 5f46e47

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

tests/test_sequences_and_iterators.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
295295
public:
296296
explicit IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {}
297297
const std::pair<int, int>* begin() const { return data_.data(); }
298+
// .end() only required for py::make_iterator(self) overload
299+
const std::pair<int, int>* end() const { return data_.data() + data_.size(); }
298300
private:
299301
std::vector<std::pair<int, int>> data_;
300302
};
@@ -306,6 +308,17 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
306308
.def("nonzero_keys", [](const IntPairs& s) {
307309
return py::make_key_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
308310
}, py::keep_alive<0, 1>())
311+
.def("simple_iterator", [](IntPairs& self) {
312+
return py::make_iterator(self);
313+
}, py::keep_alive<0, 1>())
314+
.def("simple_keys", [](IntPairs& self) {
315+
return py::make_key_iterator(self);
316+
}, py::keep_alive<0, 1>())
317+
318+
// test iterator with keep_alive (doesn't work so not used at runtime, but tests compile)
319+
.def("make_iterator_keep_alive", [](IntPairs& self) {
320+
return py::make_iterator(self, py::keep_alive<0, 1>());
321+
}, py::keep_alive<0, 1>())
309322
;
310323

311324

tests/test_sequences_and_iterators.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ def test_generalized_iterators():
4848
next(it)
4949

5050

51+
def test_generalized_iterators_simple():
52+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [
53+
(1, 2),
54+
(3, 4),
55+
(0, 5),
56+
]
57+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0]
58+
59+
5160
def test_sliceable():
5261
sliceable = m.Sliceable(100)
5362
assert sliceable[::] == (0, 100, 1)

0 commit comments

Comments
 (0)