diff --git a/tests/test_buffers.cpp b/tests/test_buffers.cpp index 6b6e8cba7f..24fa46232b 100644 --- a/tests/test_buffers.cpp +++ b/tests/test_buffers.cpp @@ -7,12 +7,40 @@ BSD-style license that can be found in the LICENSE file. */ +#include #include #include "constructor_stats.h" #include "pybind11_tests.h" TEST_SUBMODULE(buffers, m) { + +#define PYBIND11_LOCAL_DEF(...) \ + if (cpp_name == #__VA_ARGS__) \ + return py::format_descriptor<__VA_ARGS__>::format(); + + m.def("format_descriptor_format", [](const std::string &cpp_name) { + PYBIND11_LOCAL_DEF(PyObject *) + PYBIND11_LOCAL_DEF(bool) + PYBIND11_LOCAL_DEF(std::int8_t) + PYBIND11_LOCAL_DEF(std::uint8_t) + PYBIND11_LOCAL_DEF(std::int16_t) + PYBIND11_LOCAL_DEF(std::uint16_t) + PYBIND11_LOCAL_DEF(std::int32_t) + PYBIND11_LOCAL_DEF(std::uint32_t) + PYBIND11_LOCAL_DEF(std::int64_t) + PYBIND11_LOCAL_DEF(std::uint64_t) + PYBIND11_LOCAL_DEF(float) + PYBIND11_LOCAL_DEF(double) + PYBIND11_LOCAL_DEF(long double) + PYBIND11_LOCAL_DEF(std::complex) + PYBIND11_LOCAL_DEF(std::complex) + PYBIND11_LOCAL_DEF(std::complex) + return std::string("UNKNOWN"); + }); + +#undef PYBIND11_LOCAL_DEF + // test_from_python / test_to_python: class Matrix { public: diff --git a/tests/test_buffers.py b/tests/test_buffers.py index eb58c4675e..943699c0de 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -11,6 +11,32 @@ np = pytest.importorskip("numpy") +@pytest.mark.parametrize( + ("cpp_name", "expected_codes"), + [ + ("PyObject *", ["O"]), + ("bool", ["?"]), + ("std::int8_t", ["b"]), + ("std::uint8_t", ["B"]), + ("std::int16_t", ["h"]), + ("std::uint16_t", ["H"]), + ("std::int32_t", ["i"]), + ("std::uint32_t", ["I"]), + ("std::int64_t", ["q"]), + ("std::uint64_t", ["Q"]), + ("float", ["f"]), + ("double", ["d"]), + ("long double", ["g", "d"]), + ("std::complex", ["Zf"]), + ("std::complex", ["Zd"]), + ("std::complex", ["Zg", "Zd"]), + ("", ["UNKNOWN"]), + ], +) +def test_format_descriptor_format(cpp_name, expected_codes): + assert m.format_descriptor_format(cpp_name) in expected_codes + + def test_from_python(): with pytest.raises(RuntimeError) as excinfo: m.Matrix(np.array([1, 2, 3])) # trying to assign a 1D array