Add test_format_descriptor_format

This commit is contained in:
Ralf W. Grosse-Kunstleve 2023-05-17 17:52:43 -07:00
parent 50eaa3a746
commit 82ce80fae1
2 changed files with 54 additions and 0 deletions

View File

@ -7,12 +7,40 @@
BSD-style license that can be found in the LICENSE file.
*/
#include <pybind11/complex.h>
#include <pybind11/stl.h>
#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<float>)
PYBIND11_LOCAL_DEF(std::complex<double>)
PYBIND11_LOCAL_DEF(std::complex<long double>)
return std::string("UNKNOWN");
});
#undef PYBIND11_LOCAL_DEF
// test_from_python / test_to_python:
class Matrix {
public:

View File

@ -11,6 +11,32 @@ from pybind11_tests import buffers as m
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<float>", ["Zf"]),
("std::complex<double>", ["Zd"]),
("std::complex<long double>", ["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