2015-07-05 18:05:44 +00:00
|
|
|
/*
|
2016-08-12 11:50:00 +00:00
|
|
|
tests/test_buffers.cpp -- supporting Pythons' buffer protocol
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include "constructor_stats.h"
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
class Matrix {
|
|
|
|
public:
|
2017-04-14 20:33:44 +00:00
|
|
|
Matrix(ssize_t rows, ssize_t cols) : m_rows(rows), m_cols(cols) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
|
2017-04-14 20:33:44 +00:00
|
|
|
m_data = new float[(size_t) (rows*cols)];
|
|
|
|
memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
|
2017-04-14 20:33:44 +00:00
|
|
|
m_data = new float[(size_t) (m_rows * m_cols)];
|
|
|
|
memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_move_created(this);
|
2015-07-05 18:05:44 +00:00
|
|
|
s.m_rows = 0;
|
|
|
|
s.m_cols = 0;
|
|
|
|
s.m_data = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Matrix() {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
|
2015-07-05 18:05:44 +00:00
|
|
|
delete[] m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix &operator=(const Matrix &s) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
|
2015-07-05 18:05:44 +00:00
|
|
|
delete[] m_data;
|
|
|
|
m_rows = s.m_rows;
|
|
|
|
m_cols = s.m_cols;
|
2017-04-14 20:33:44 +00:00
|
|
|
m_data = new float[(size_t) (m_rows * m_cols)];
|
|
|
|
memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
|
2015-07-05 18:05:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix &operator=(Matrix &&s) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
|
2015-07-05 18:05:44 +00:00
|
|
|
if (&s != this) {
|
|
|
|
delete[] m_data;
|
|
|
|
m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
|
|
|
|
s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-04-14 20:33:44 +00:00
|
|
|
float operator()(ssize_t i, ssize_t j) const {
|
|
|
|
return m_data[(size_t) (i*m_cols + j)];
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 20:33:44 +00:00
|
|
|
float &operator()(ssize_t i, ssize_t j) {
|
|
|
|
return m_data[(size_t) (i*m_cols + j)];
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float *data() { return m_data; }
|
|
|
|
|
2017-04-14 20:33:44 +00:00
|
|
|
ssize_t rows() const { return m_rows; }
|
|
|
|
ssize_t cols() const { return m_cols; }
|
2015-07-05 18:05:44 +00:00
|
|
|
private:
|
2017-04-14 20:33:44 +00:00
|
|
|
ssize_t m_rows;
|
|
|
|
ssize_t m_cols;
|
2015-07-05 18:05:44 +00:00
|
|
|
float *m_data;
|
|
|
|
};
|
|
|
|
|
2016-09-03 18:54:22 +00:00
|
|
|
test_initializer buffers([](py::module &m) {
|
2016-12-16 14:00:46 +00:00
|
|
|
py::class_<Matrix> mtx(m, "Matrix", py::buffer_protocol());
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2017-04-14 20:33:44 +00:00
|
|
|
mtx.def(py::init<ssize_t, ssize_t>())
|
2015-07-05 18:05:44 +00:00
|
|
|
/// Construct from a buffer
|
|
|
|
.def("__init__", [](Matrix &v, py::buffer b) {
|
|
|
|
py::buffer_info info = b.request();
|
2016-06-26 11:42:34 +00:00
|
|
|
if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
|
2015-07-05 18:05:44 +00:00
|
|
|
throw std::runtime_error("Incompatible buffer format!");
|
|
|
|
new (&v) Matrix(info.shape[0], info.shape[1]);
|
2017-04-14 20:33:44 +00:00
|
|
|
memcpy(v.data(), info.ptr, sizeof(float) * (size_t) (v.rows() * v.cols()));
|
2015-07-05 18:05:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
.def("rows", &Matrix::rows)
|
|
|
|
.def("cols", &Matrix::cols)
|
|
|
|
|
|
|
|
/// Bare bones interface
|
2017-04-14 20:33:44 +00:00
|
|
|
.def("__getitem__", [](const Matrix &m, std::pair<ssize_t, ssize_t> i) {
|
2015-07-05 18:05:44 +00:00
|
|
|
if (i.first >= m.rows() || i.second >= m.cols())
|
|
|
|
throw py::index_error();
|
|
|
|
return m(i.first, i.second);
|
|
|
|
})
|
2017-04-14 20:33:44 +00:00
|
|
|
.def("__setitem__", [](Matrix &m, std::pair<ssize_t, ssize_t> i, float v) {
|
2015-07-05 18:05:44 +00:00
|
|
|
if (i.first >= m.rows() || i.second >= m.cols())
|
|
|
|
throw py::index_error();
|
|
|
|
m(i.first, i.second) = v;
|
|
|
|
})
|
|
|
|
/// Provide buffer access
|
|
|
|
.def_buffer([](Matrix &m) -> py::buffer_info {
|
|
|
|
return py::buffer_info(
|
2016-06-26 11:42:34 +00:00
|
|
|
m.data(), /* Pointer to buffer */
|
|
|
|
sizeof(float), /* Size of one scalar */
|
|
|
|
py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
|
|
|
|
2, /* Number of dimensions */
|
|
|
|
{ m.rows(), m.cols() }, /* Buffer dimensions */
|
2017-04-14 20:33:44 +00:00
|
|
|
{ sizeof(float) * size_t(m.rows()), /* Strides (in bytes) for each index */
|
|
|
|
sizeof(float) }
|
2015-07-05 18:05:44 +00:00
|
|
|
);
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
})
|
|
|
|
;
|
2016-09-03 18:54:22 +00:00
|
|
|
});
|