Skip to content

Commit 391c754

Browse files
committed
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and comment styles started in #898. For the most part, the test coverage here is unchanged, with a few minor exceptions as noted below. - test_constants_and_functions: this adds more overload tests with overloads with different number of arguments for more comprehensive overload_cast testing. The test style conversion broke the overload tests under MSVC 2015, prompting the additional tests while looking for a workaround. - test_eigen: this dropped the unused functions `get_cm_corners` and `get_cm_corners_const`--these same tests were duplicates of the same things provided (and used) via ReturnTester methods. - test_opaque_types: this test had a hidden dependence on ExampleMandA which is now fixed by using the global UserType which suffices for the relevant test. - test_methods_and_attributes: this required some additions to UserType to make it usable as a replacement for the test's previous SimpleType: UserType gained a value mutator, and the `value` property is not mutable (it was previously readonly). Some overload tests were also added to better test overload_cast (as described above). - test_numpy_array: removed the untemplated mutate_data/mutate_data_t: the templated versions with an empty parameter pack expand to the same thing. - test_stl: this was already mostly in the new style; this just tweaks things a bit, localizing a class, and adding some missing `// test_whatever` comments. - test_virtual_functions: like `test_stl`, this was mostly in the new test style already, but needed some `// test_whatever` comments. This commit also moves the inherited virtual example code to the end of the file, after the main set of tests (since it is less important than the other tests, and rather length); it also got renamed to `test_inherited_virtuals` (from `test_inheriting_repeat`) because it tests both inherited virtual approaches, not just the repeat approach.
1 parent 9866a0f commit 391c754

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2292
-2771
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def pytest_namespace():
196196
except ImportError:
197197
scipy = None
198198
try:
199-
from pybind11_tests import have_eigen
199+
from pybind11_tests.eigen import have_eigen
200200
except ImportError:
201201
have_eigen = False
202202
pypy = platform.python_implementation() == "PyPy"

tests/pybind11_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ PYBIND11_MODULE(pybind11_tests, m) {
7777
.def(py::init<>())
7878
.def(py::init<int>())
7979
.def("get_value", &UserType::value, "Get value using a method")
80-
.def_property_readonly("value", &UserType::value, "Get value using a property")
80+
.def("set_value", &UserType::set, "Set value using a method")
81+
.def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
8182
.def("__repr__", [](const UserType& u) { return "UserType({})"_s.format(u.value()); });
8283

8384
py::class_<IncType, UserType>(m, "IncType")

tests/pybind11_tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class UserType {
3333
UserType(int i) : i(i) { }
3434

3535
int value() const { return i; }
36+
void set(int set) { i = set; }
3637

3738
private:
3839
int i = -1;

tests/test_buffers.cpp

Lines changed: 103 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,105 +10,73 @@
1010
#include "pybind11_tests.h"
1111
#include "constructor_stats.h"
1212

13-
class Matrix {
14-
public:
15-
Matrix(ssize_t rows, ssize_t cols) : m_rows(rows), m_cols(cols) {
16-
print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
17-
m_data = new float[(size_t) (rows*cols)];
18-
memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
19-
}
20-
21-
Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
22-
print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
23-
m_data = new float[(size_t) (m_rows * m_cols)];
24-
memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
25-
}
26-
27-
Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
28-
print_move_created(this);
29-
s.m_rows = 0;
30-
s.m_cols = 0;
31-
s.m_data = nullptr;
32-
}
33-
34-
~Matrix() {
35-
print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
36-
delete[] m_data;
37-
}
38-
39-
Matrix &operator=(const Matrix &s) {
40-
print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
41-
delete[] m_data;
42-
m_rows = s.m_rows;
43-
m_cols = s.m_cols;
44-
m_data = new float[(size_t) (m_rows * m_cols)];
45-
memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
46-
return *this;
47-
}
48-
49-
Matrix &operator=(Matrix &&s) {
50-
print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
51-
if (&s != this) {
52-
delete[] m_data;
53-
m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
54-
s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
13+
TEST_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));
5521
}
56-
return *this;
57-
}
58-
59-
float operator()(ssize_t i, ssize_t j) const {
60-
return m_data[(size_t) (i*m_cols + j)];
61-
}
62-
63-
float &operator()(ssize_t i, ssize_t j) {
64-
return m_data[(size_t) (i*m_cols + j)];
65-
}
66-
67-
float *data() { return m_data; }
6822

69-
ssize_t rows() const { return m_rows; }
70-
ssize_t cols() const { return m_cols; }
71-
private:
72-
ssize_t m_rows;
73-
ssize_t m_cols;
74-
float *m_data;
75-
};
76-
77-
class SquareMatrix : public Matrix {
78-
public:
79-
SquareMatrix(ssize_t n) : Matrix(n, n) { }
80-
};
81-
82-
struct PTMFBuffer {
83-
int32_t value = 0;
84-
85-
py::buffer_info get_buffer_info() {
86-
return py::buffer_info(&value, sizeof(value),
87-
py::format_descriptor<int32_t>::format(), 1);
88-
}
89-
};
23+
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+
}
9028

91-
class ConstPTMFBuffer {
92-
std::unique_ptr<int32_t> value;
29+
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+
}
9335

94-
public:
95-
int32_t get_value() const { return *value; }
96-
void set_value(int32_t v) { *value = v; }
36+
~Matrix() {
37+
print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
38+
delete[] m_data;
39+
}
9740

98-
py::buffer_info get_buffer_info() const {
99-
return py::buffer_info(value.get(), sizeof(*value),
100-
py::format_descriptor<int32_t>::format(), 1);
101-
}
41+
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+
}
10250

103-
ConstPTMFBuffer() : value(new int32_t{0}) { };
104-
};
51+
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+
}
10560

106-
struct DerivedPTMFBuffer : public PTMFBuffer { };
61+
float operator()(ssize_t i, ssize_t j) const {
62+
return m_data[(size_t) (i*m_cols + j)];
63+
}
10764

108-
test_initializer buffers([](py::module &m) {
109-
py::class_<Matrix> mtx(m, "Matrix", py::buffer_protocol());
65+
float &operator()(ssize_t i, ssize_t j) {
66+
return m_data[(size_t) (i*m_cols + j)];
67+
}
11068

111-
mtx.def(py::init<ssize_t, ssize_t>())
69+
float *data() { return m_data; }
70+
71+
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>())
11280
/// Construct from a buffer
11381
.def("__init__", [](Matrix &v, py::buffer b) {
11482
py::buffer_info info = b.request();
@@ -143,24 +111,57 @@ test_initializer buffers([](py::module &m) {
143111
})
144112
;
145113

114+
115+
// test_inherited_protocol
116+
class SquareMatrix : public Matrix {
117+
public:
118+
SquareMatrix(ssize_t n) : Matrix(n, n) { }
119+
};
146120
// Derived classes inherit the buffer protocol and the buffer access function
147121
py::class_<SquareMatrix, Matrix>(m, "SquareMatrix")
148122
.def(py::init<ssize_t>());
149123

150-
py::class_<PTMFBuffer>(m, "PTMFBuffer", py::buffer_protocol())
124+
125+
// test_pointer_to_member_fn
126+
// Tests that passing a pointer to member to the base class works in
127+
// the derived class.
128+
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())
151137
.def(py::init<>())
152-
.def_readwrite("value", &PTMFBuffer::value)
153-
.def_buffer(&PTMFBuffer::get_buffer_info);
138+
.def_readwrite("value", &Buffer::value)
139+
.def_buffer(&Buffer::get_buffer_info);
140+
154141

155-
py::class_<ConstPTMFBuffer>(m, "ConstPTMFBuffer", py::buffer_protocol())
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())
156157
.def(py::init<>())
157-
.def_property("value", &ConstPTMFBuffer::get_value, &ConstPTMFBuffer::set_value)
158-
.def_buffer(&ConstPTMFBuffer::get_buffer_info);
158+
.def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value)
159+
.def_buffer(&ConstBuffer::get_buffer_info);
159160

160-
// Tests that passing a pointer to member to the base class works in
161-
// the derived class.
162-
py::class_<DerivedPTMFBuffer>(m, "DerivedPTMFBuffer", py::buffer_protocol())
161+
struct DerivedBuffer : public Buffer { };
162+
py::class_<DerivedBuffer>(m, "DerivedBuffer", py::buffer_protocol())
163163
.def(py::init<>())
164-
.def_readwrite("value", (int32_t DerivedPTMFBuffer::*) &DerivedPTMFBuffer::value)
165-
.def_buffer(&DerivedPTMFBuffer::get_buffer_info);
166-
});
164+
.def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value)
165+
.def_buffer(&DerivedBuffer::get_buffer_info);
166+
167+
}

tests/test_buffers.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import struct
22
import pytest
3-
from pybind11_tests import Matrix, ConstructorStats, PTMFBuffer, ConstPTMFBuffer, DerivedPTMFBuffer
3+
from pybind11_tests import buffers as m
4+
from pybind11_tests import ConstructorStats
45

56
pytestmark = pytest.requires_numpy
67

@@ -10,17 +11,17 @@
1011

1112
def test_from_python():
1213
with pytest.raises(RuntimeError) as excinfo:
13-
Matrix(np.array([1, 2, 3])) # trying to assign a 1D array
14+
m.Matrix(np.array([1, 2, 3])) # trying to assign a 1D array
1415
assert str(excinfo.value) == "Incompatible buffer format!"
1516

1617
m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
17-
m4 = Matrix(m3)
18+
m4 = m.Matrix(m3)
1819

1920
for i in range(m4.rows()):
2021
for j in range(m4.cols()):
2122
assert m3[i, j] == m4[i, j]
2223

23-
cstats = ConstructorStats.get(Matrix)
24+
cstats = ConstructorStats.get(m.Matrix)
2425
assert cstats.alive() == 1
2526
del m3, m4
2627
assert cstats.alive() == 0
@@ -35,26 +36,26 @@ def test_from_python():
3536
# https://bitbucket.org/pypy/pypy/issues/2444
3637
@pytest.unsupported_on_pypy
3738
def test_to_python():
38-
m = Matrix(5, 5)
39-
assert memoryview(m).shape == (5, 5)
39+
mat = m.Matrix(5, 5)
40+
assert memoryview(mat).shape == (5, 5)
4041

41-
assert m[2, 3] == 0
42-
m[2, 3] = 4
43-
assert m[2, 3] == 4
42+
assert mat[2, 3] == 0
43+
mat[2, 3] = 4
44+
assert mat[2, 3] == 4
4445

45-
m2 = np.array(m, copy=False)
46-
assert m2.shape == (5, 5)
47-
assert abs(m2).sum() == 4
48-
assert m2[2, 3] == 4
49-
m2[2, 3] = 5
50-
assert m2[2, 3] == 5
46+
mat2 = np.array(mat, copy=False)
47+
assert mat2.shape == (5, 5)
48+
assert abs(mat2).sum() == 4
49+
assert mat2[2, 3] == 4
50+
mat2[2, 3] = 5
51+
assert mat2[2, 3] == 5
5152

52-
cstats = ConstructorStats.get(Matrix)
53+
cstats = ConstructorStats.get(m.Matrix)
5354
assert cstats.alive() == 1
54-
del m
55+
del mat
5556
pytest.gc_collect()
5657
assert cstats.alive() == 1
57-
del m2 # holds an m reference
58+
del mat2 # holds a mat reference
5859
pytest.gc_collect()
5960
assert cstats.alive() == 0
6061
assert cstats.values() == ["5x5 matrix"]
@@ -67,16 +68,15 @@ def test_to_python():
6768
@pytest.unsupported_on_pypy
6869
def test_inherited_protocol():
6970
"""SquareMatrix is derived from Matrix and inherits the buffer protocol"""
70-
from pybind11_tests import SquareMatrix
7171

72-
matrix = SquareMatrix(5)
72+
matrix = m.SquareMatrix(5)
7373
assert memoryview(matrix).shape == (5, 5)
7474
assert np.asarray(matrix).shape == (5, 5)
7575

7676

7777
@pytest.unsupported_on_pypy
78-
def test_ptmf():
79-
for cls in [PTMFBuffer, ConstPTMFBuffer, DerivedPTMFBuffer]:
78+
def test_pointer_to_member_fn():
79+
for cls in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]:
8080
buf = cls()
8181
buf.value = 0x12345678
8282
value = struct.unpack('i', bytearray(buf))[0]

0 commit comments

Comments
 (0)