Skip to content

Commit 82ce80f

Browse files
committed
Add test_format_descriptor_format
1 parent 50eaa3a commit 82ce80f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

tests/test_buffers.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,40 @@
77
BSD-style license that can be found in the LICENSE file.
88
*/
99

10+
#include <pybind11/complex.h>
1011
#include <pybind11/stl.h>
1112

1213
#include "constructor_stats.h"
1314
#include "pybind11_tests.h"
1415

1516
TEST_SUBMODULE(buffers, m) {
17+
18+
#define PYBIND11_LOCAL_DEF(...) \
19+
if (cpp_name == #__VA_ARGS__) \
20+
return py::format_descriptor<__VA_ARGS__>::format();
21+
22+
m.def("format_descriptor_format", [](const std::string &cpp_name) {
23+
PYBIND11_LOCAL_DEF(PyObject *)
24+
PYBIND11_LOCAL_DEF(bool)
25+
PYBIND11_LOCAL_DEF(std::int8_t)
26+
PYBIND11_LOCAL_DEF(std::uint8_t)
27+
PYBIND11_LOCAL_DEF(std::int16_t)
28+
PYBIND11_LOCAL_DEF(std::uint16_t)
29+
PYBIND11_LOCAL_DEF(std::int32_t)
30+
PYBIND11_LOCAL_DEF(std::uint32_t)
31+
PYBIND11_LOCAL_DEF(std::int64_t)
32+
PYBIND11_LOCAL_DEF(std::uint64_t)
33+
PYBIND11_LOCAL_DEF(float)
34+
PYBIND11_LOCAL_DEF(double)
35+
PYBIND11_LOCAL_DEF(long double)
36+
PYBIND11_LOCAL_DEF(std::complex<float>)
37+
PYBIND11_LOCAL_DEF(std::complex<double>)
38+
PYBIND11_LOCAL_DEF(std::complex<long double>)
39+
return std::string("UNKNOWN");
40+
});
41+
42+
#undef PYBIND11_LOCAL_DEF
43+
1644
// test_from_python / test_to_python:
1745
class Matrix {
1846
public:

tests/test_buffers.py

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@
1111
np = pytest.importorskip("numpy")
1212

1313

14+
@pytest.mark.parametrize(
15+
("cpp_name", "expected_codes"),
16+
[
17+
("PyObject *", ["O"]),
18+
("bool", ["?"]),
19+
("std::int8_t", ["b"]),
20+
("std::uint8_t", ["B"]),
21+
("std::int16_t", ["h"]),
22+
("std::uint16_t", ["H"]),
23+
("std::int32_t", ["i"]),
24+
("std::uint32_t", ["I"]),
25+
("std::int64_t", ["q"]),
26+
("std::uint64_t", ["Q"]),
27+
("float", ["f"]),
28+
("double", ["d"]),
29+
("long double", ["g", "d"]),
30+
("std::complex<float>", ["Zf"]),
31+
("std::complex<double>", ["Zd"]),
32+
("std::complex<long double>", ["Zg", "Zd"]),
33+
("", ["UNKNOWN"]),
34+
],
35+
)
36+
def test_format_descriptor_format(cpp_name, expected_codes):
37+
assert m.format_descriptor_format(cpp_name) in expected_codes
38+
39+
1440
def test_from_python():
1541
with pytest.raises(RuntimeError) as excinfo:
1642
m.Matrix(np.array([1, 2, 3])) # trying to assign a 1D array

0 commit comments

Comments
 (0)