| 134 | } |
| 135 | |
| 136 | TEST_SUBMODULE(sequences_and_iterators, m) { |
| 137 | // test_sliceable |
| 138 | class Sliceable { |
| 139 | public: |
| 140 | explicit Sliceable(int n) : size(n) {} |
| 141 | int start, stop, step; |
| 142 | int size; |
| 143 | }; |
| 144 | py::class_<Sliceable>(m, "Sliceable") |
| 145 | .def(py::init<int>()) |
| 146 | .def("__getitem__", [](const Sliceable &s, const py::slice &slice) { |
| 147 | py::ssize_t start = 0, stop = 0, step = 0, slicelength = 0; |
| 148 | if (!slice.compute(s.size, &start, &stop, &step, &slicelength)) { |
| 149 | throw py::error_already_set(); |
| 150 | } |
| 151 | int istart = static_cast<int>(start); |
| 152 | int istop = static_cast<int>(stop); |
| 153 | int istep = static_cast<int>(step); |
| 154 | return std::make_tuple(istart, istop, istep); |
| 155 | }); |
| 156 | |
| 157 | m.def("make_forward_slice_size_t", []() { return py::slice(0, -1, 1); }); |
| 158 | m.def("make_reversed_slice_object", |
| 159 | []() { return py::slice(py::none(), py::none(), py::int_(-1)); }); |
| 160 | #ifdef PYBIND11_HAS_OPTIONAL |
| 161 | m.attr("has_optional") = true; |
| 162 | m.def("make_reversed_slice_size_t_optional_verbose", |
| 163 | []() { return py::slice(std::nullopt, std::nullopt, -1); }); |
| 164 | // Warning: The following spelling may still compile if optional<> is not present and give |
| 165 | // wrong answers. Please use with caution. |
| 166 | m.def("make_reversed_slice_size_t_optional", []() { return py::slice({}, {}, -1); }); |
| 167 | #else |
| 168 | m.attr("has_optional") = false; |
| 169 | #endif |
| 170 | |
| 171 | // test_sequence |
| 172 | class Sequence { |
| 173 | public: |
| 174 | explicit Sequence(size_t size) : m_size(size) { |
| 175 | print_created(this, "of size", m_size); |
| 176 | // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) |
| 177 | m_data = new float[size]; |
| 178 | memset(m_data, 0, sizeof(float) * size); |
| 179 | } |
| 180 | explicit Sequence(const std::vector<float> &value) : m_size(value.size()) { |
| 181 | print_created(this, "of size", m_size, "from std::vector"); |
| 182 | // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) |
| 183 | m_data = new float[m_size]; |
| 184 | memcpy(m_data, &value[0], sizeof(float) * m_size); |
| 185 | } |
| 186 | Sequence(const Sequence &s) : m_size(s.m_size) { |
| 187 | print_copy_created(this); |
| 188 | // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) |
| 189 | m_data = new float[m_size]; |
| 190 | memcpy(m_data, s.m_data, sizeof(float) * m_size); |
| 191 | } |
| 192 | Sequence(Sequence &&s) noexcept : m_size(s.m_size), m_data(s.m_data) { |
| 193 | print_move_created(this); |
nothing calls this directly
no test coverage detected