/* tests/test_numpy_scalars.cpp -- strict NumPy scalars Copyright (c) 2021 Steve R. Sun All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #include #include #include "pybind11_tests.h" #include namespace py = pybind11; template struct add { T x; add(T x) : x(x) {} T operator()(T y) const { return static_cast(x + y); } }; template void register_test(py::module& m, const char *name, F&& func) { m.def((std::string("test_") + name).c_str(), [=](py::numpy_scalar v) { return std::make_tuple(name, py::make_scalar(static_cast(func(v.value)))); }, py::arg("x")); } TEST_SUBMODULE(numpy_scalars, m) { using cfloat = std::complex; using cdouble = std::complex; register_test(m, "bool", [](bool x) { return !x; }); register_test(m, "int8", add(-8)); register_test(m, "int16", add(-16)); register_test(m, "int32", add(-32)); register_test(m, "int64", add(-64)); register_test(m, "uint8", add(8)); register_test(m, "uint16", add(16)); register_test(m, "uint32", add(32)); register_test(m, "uint64", add(64)); register_test(m, "float32", add(0.125f)); register_test(m, "float64", add(0.25f)); register_test(m, "complex64", add({0, -0.125f})); register_test(m, "complex128", add({0, -0.25f})); }