blob: c7f081d68ed25aa8345ab801f3571c4187552618 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_buffers.cpp -- supporting Pythons' buffer protocol
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02005
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
11#include "constructor_stats.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012
Jason Rhinelander391c7542017-07-25 16:47:36 -040013TEST_SUBMODULE(buffers, m) {
14 // test_from_python / test_to_python:
15 class Matrix {
16 public:
17 Matrix(ssize_t rows, ssize_t cols) : m_rows(rows), m_cols(cols) {
18 print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
19 m_data = new float[(size_t) (rows*cols)];
20 memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
Wenzel Jakob38bd7112015-07-05 20:05:44 +020021 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020022
Jason Rhinelander391c7542017-07-25 16:47:36 -040023 Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
24 print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
25 m_data = new float[(size_t) (m_rows * m_cols)];
26 memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
27 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020028
Jason Rhinelander391c7542017-07-25 16:47:36 -040029 Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
30 print_move_created(this);
31 s.m_rows = 0;
32 s.m_cols = 0;
33 s.m_data = nullptr;
34 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020035
Jason Rhinelander391c7542017-07-25 16:47:36 -040036 ~Matrix() {
37 print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
38 delete[] m_data;
39 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020040
Jason Rhinelander391c7542017-07-25 16:47:36 -040041 Matrix &operator=(const Matrix &s) {
42 print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
43 delete[] m_data;
44 m_rows = s.m_rows;
45 m_cols = s.m_cols;
46 m_data = new float[(size_t) (m_rows * m_cols)];
47 memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
48 return *this;
49 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020050
Jason Rhinelander391c7542017-07-25 16:47:36 -040051 Matrix &operator=(Matrix &&s) {
52 print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
53 if (&s != this) {
54 delete[] m_data;
55 m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
56 s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
57 }
58 return *this;
59 }
Dean Moldovan427e4af2017-05-28 16:35:02 +020060
Jason Rhinelander391c7542017-07-25 16:47:36 -040061 float operator()(ssize_t i, ssize_t j) const {
62 return m_data[(size_t) (i*m_cols + j)];
63 }
Bruce Merryfe0cf8b2017-05-17 10:52:33 +020064
Jason Rhinelander391c7542017-07-25 16:47:36 -040065 float &operator()(ssize_t i, ssize_t j) {
66 return m_data[(size_t) (i*m_cols + j)];
67 }
Bruce Merryfe0cf8b2017-05-17 10:52:33 +020068
Jason Rhinelander391c7542017-07-25 16:47:36 -040069 float *data() { return m_data; }
Bruce Merryfe0cf8b2017-05-17 10:52:33 +020070
Jason Rhinelander391c7542017-07-25 16:47:36 -040071 ssize_t rows() const { return m_rows; }
72 ssize_t cols() const { return m_cols; }
73 private:
74 ssize_t m_rows;
75 ssize_t m_cols;
76 float *m_data;
77 };
78 py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
79 .def(py::init<ssize_t, ssize_t>())
Wenzel Jakob38bd7112015-07-05 20:05:44 +020080 /// Construct from a buffer
81 .def("__init__", [](Matrix &v, py::buffer b) {
82 py::buffer_info info = b.request();
Ivan Smirnov5e71e172016-06-26 12:42:34 +010083 if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020084 throw std::runtime_error("Incompatible buffer format!");
85 new (&v) Matrix(info.shape[0], info.shape[1]);
Cris Luengo30d43c42017-04-14 14:33:44 -060086 memcpy(v.data(), info.ptr, sizeof(float) * (size_t) (v.rows() * v.cols()));
Wenzel Jakob38bd7112015-07-05 20:05:44 +020087 })
88
89 .def("rows", &Matrix::rows)
90 .def("cols", &Matrix::cols)
91
92 /// Bare bones interface
Cris Luengo30d43c42017-04-14 14:33:44 -060093 .def("__getitem__", [](const Matrix &m, std::pair<ssize_t, ssize_t> i) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020094 if (i.first >= m.rows() || i.second >= m.cols())
95 throw py::index_error();
96 return m(i.first, i.second);
97 })
Cris Luengo30d43c42017-04-14 14:33:44 -060098 .def("__setitem__", [](Matrix &m, std::pair<ssize_t, ssize_t> i, float v) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020099 if (i.first >= m.rows() || i.second >= m.cols())
100 throw py::index_error();
101 m(i.first, i.second) = v;
102 })
103 /// Provide buffer access
104 .def_buffer([](Matrix &m) -> py::buffer_info {
105 return py::buffer_info(
Ivan Smirnov5e71e172016-06-26 12:42:34 +0100106 m.data(), /* Pointer to buffer */
Ivan Smirnov5e71e172016-06-26 12:42:34 +0100107 { m.rows(), m.cols() }, /* Buffer dimensions */
Cris Luengo30d43c42017-04-14 14:33:44 -0600108 { sizeof(float) * size_t(m.rows()), /* Strides (in bytes) for each index */
109 sizeof(float) }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200110 );
Jason Rhinelander3f589372016-08-07 13:05:26 -0400111 })
112 ;
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200113
Jason Rhinelander391c7542017-07-25 16:47:36 -0400114
115 // test_inherited_protocol
116 class SquareMatrix : public Matrix {
117 public:
118 SquareMatrix(ssize_t n) : Matrix(n, n) { }
119 };
Dean Moldovan427e4af2017-05-28 16:35:02 +0200120 // Derived classes inherit the buffer protocol and the buffer access function
121 py::class_<SquareMatrix, Matrix>(m, "SquareMatrix")
122 .def(py::init<ssize_t>());
123
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200124
Jason Rhinelander391c7542017-07-25 16:47:36 -0400125 // test_pointer_to_member_fn
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200126 // Tests that passing a pointer to member to the base class works in
127 // the derived class.
Jason Rhinelander391c7542017-07-25 16:47:36 -0400128 struct Buffer {
129 int32_t value = 0;
130
131 py::buffer_info get_buffer_info() {
132 return py::buffer_info(&value, sizeof(value),
133 py::format_descriptor<int32_t>::format(), 1);
134 }
135 };
136 py::class_<Buffer>(m, "Buffer", py::buffer_protocol())
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200137 .def(py::init<>())
Jason Rhinelander391c7542017-07-25 16:47:36 -0400138 .def_readwrite("value", &Buffer::value)
139 .def_buffer(&Buffer::get_buffer_info);
140
141
142 class ConstBuffer {
143 std::unique_ptr<int32_t> value;
144
145 public:
146 int32_t get_value() const { return *value; }
147 void set_value(int32_t v) { *value = v; }
148
149 py::buffer_info get_buffer_info() const {
150 return py::buffer_info(value.get(), sizeof(*value),
151 py::format_descriptor<int32_t>::format(), 1);
152 }
153
154 ConstBuffer() : value(new int32_t{0}) { };
155 };
156 py::class_<ConstBuffer>(m, "ConstBuffer", py::buffer_protocol())
157 .def(py::init<>())
158 .def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value)
159 .def_buffer(&ConstBuffer::get_buffer_info);
160
161 struct DerivedBuffer : public Buffer { };
162 py::class_<DerivedBuffer>(m, "DerivedBuffer", py::buffer_protocol())
163 .def(py::init<>())
164 .def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value)
165 .def_buffer(&DerivedBuffer::get_buffer_info);
166
167}