Skip to content

Commit ee660a0

Browse files
numpy: Add test for explicit dtype checks. At present, int64 + uint64 do not exactly match dtype(...).num
1 parent 81da988 commit ee660a0

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

tests/test_numpy_array.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,67 @@
1414

1515
#include <cstdint>
1616

17+
// Size / dtype checks.
18+
struct DtypeCheck {
19+
py::dtype numpy{};
20+
py::dtype pybind11{};
21+
};
22+
23+
template <typename T>
24+
DtypeCheck get_dtype_check(const char* name) {
25+
py::module np = py::module::import("numpy");
26+
DtypeCheck check{};
27+
check.numpy = np.attr("dtype")(np.attr(name));
28+
check.pybind11 = py::dtype::of<T>();
29+
return check;
30+
}
31+
32+
std::vector<DtypeCheck> get_concrete_dtype_checks() {
33+
return {
34+
// Normalization
35+
get_dtype_check<std::int8_t>("int8"),
36+
get_dtype_check<std::uint8_t>("uint8"),
37+
get_dtype_check<std::int16_t>("int16"),
38+
get_dtype_check<std::uint16_t>("uint16"),
39+
get_dtype_check<std::int32_t>("int32"),
40+
get_dtype_check<std::uint32_t>("uint32"),
41+
get_dtype_check<std::int64_t>("int64"),
42+
get_dtype_check<std::uint64_t>("uint64")
43+
};
44+
}
45+
46+
struct DtypeSizeCheck {
47+
std::string name{};
48+
int size_cpp{};
49+
int size_numpy{};
50+
// For debugging.
51+
py::dtype dtype{};
52+
};
53+
54+
template <typename T>
55+
DtypeSizeCheck get_dtype_size_check() {
56+
DtypeSizeCheck check{};
57+
check.name = py::type_id<T>();
58+
check.size_cpp = sizeof(T);
59+
check.dtype = py::dtype::of<T>();
60+
check.size_numpy = check.dtype.attr("itemsize").template cast<int>();
61+
return check;
62+
}
63+
64+
std::vector<DtypeSizeCheck> get_platform_dtype_size_checks() {
65+
return {
66+
get_dtype_size_check<short>(),
67+
get_dtype_size_check<unsigned short>(),
68+
get_dtype_size_check<int>(),
69+
get_dtype_size_check<unsigned int>(),
70+
get_dtype_size_check<long>(),
71+
get_dtype_size_check<unsigned long>(),
72+
get_dtype_size_check<long long>(),
73+
get_dtype_size_check<unsigned long long>(),
74+
};
75+
}
76+
77+
// Arrays.
1778
using arr = py::array;
1879
using arr_t = py::array_t<uint16_t, 0>;
1980
static_assert(std::is_same<arr_t::value_type, uint16_t>::value, "");
@@ -72,6 +133,26 @@ TEST_SUBMODULE(numpy_array, sm) {
72133
try { py::module::import("numpy"); }
73134
catch (...) { return; }
74135

136+
// test_dtypes
137+
py::class_<DtypeCheck>(sm, "DtypeCheck")
138+
.def_readonly("numpy", &DtypeCheck::numpy)
139+
.def_readonly("pybind11", &DtypeCheck::pybind11)
140+
.def("__repr__", [](const DtypeCheck& self) {
141+
return py::str("<DtypeCheck numpy={} pybind11={}>").format(
142+
self.numpy, self.pybind11);
143+
});
144+
sm.def("get_concrete_dtype_checks", &get_concrete_dtype_checks);
145+
146+
py::class_<DtypeSizeCheck>(sm, "DtypeSizeCheck")
147+
.def_readonly("name", &DtypeSizeCheck::name)
148+
.def_readonly("size_cpp", &DtypeSizeCheck::size_cpp)
149+
.def_readonly("size_numpy", &DtypeSizeCheck::size_numpy)
150+
.def("__repr__", [](const DtypeSizeCheck& self) {
151+
return py::str("<DtypeSizeCheck name='{}' size_cpp={} size_numpy={} dtype={}>").format(
152+
self.name, self.size_cpp, self.size_numpy, self.dtype);
153+
});
154+
sm.def("get_platform_dtype_size_checks", &get_platform_dtype_size_checks);
155+
75156
// test_array_attributes
76157
sm.def("ndim", [](const arr& a) { return a.ndim(); });
77158
sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); });

tests/test_numpy_array.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
import numpy as np
88

99

10+
def test_dtypes():
11+
# See issue #1328.
12+
# - Platform-dependent sizes.
13+
for size_check in m.get_platform_dtype_size_checks():
14+
print(size_check)
15+
assert size_check.size_cpp == size_check.size_numpy, size_check
16+
# - Concrete sizes.
17+
for check in m.get_concrete_dtype_checks():
18+
print(check)
19+
assert check.numpy == check.pybind11, check
20+
if check.numpy.num != check.pybind11.num:
21+
print("NOTE: typenum mismatch for {}: {} != {}".format(
22+
check, check.numpy.num, check.pybind11.num))
23+
24+
1025
@pytest.fixture(scope='function')
1126
def arr():
1227
return np.array([[1, 2, 3], [4, 5, 6]], '=u2')

0 commit comments

Comments
 (0)