Skip to content

Commit

Permalink
Add test_format_descriptor_format
Browse files Browse the repository at this point in the history
  • Loading branch information
rwgk committed May 18, 2023
1 parent 50eaa3a commit 82ce80f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/test_buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand Down

0 comments on commit 82ce80f

Please sign in to comment.