Skip to content
Snippets Groups Projects
Commit 63ccfabe authored by Wilson Jallet's avatar Wilson Jallet :clapper:
Browse files

unittest: add test for std::optional if available

+ unittest: make bind_optional and test_optional.py into template files
+ cmake: instantiate boost and std optional tests using configure_file
parent 0afe3c9c
No related branches found
No related tags found
No related merge requests found
Pipeline #27110 canceled
...@@ -39,7 +39,24 @@ if(NOT NUMPY_WITH_BROKEN_UFUNC_SUPPORT) ...@@ -39,7 +39,24 @@ if(NOT NUMPY_WITH_BROKEN_UFUNC_SUPPORT)
endif() endif()
add_lib_unit_test(std_vector) add_lib_unit_test(std_vector)
add_lib_unit_test(user_struct) add_lib_unit_test(user_struct)
add_lib_unit_test(bind_optional)
function(config_bind_optional tagname opttype)
set(MODNAME bind_optional_${tagname})
set(OPTIONAL ${opttype})
configure_file(bind_optional.cpp.in ${MODNAME}.cpp)
set(py_file test_optional_${tagname}.py)
configure_file(python/test_optional.py.in ${CMAKE_CURRENT_SOURCE_DIR}/python/${py_file})
add_lib_unit_test(${MODNAME})
message(STATUS "Adding unit test py-optional-${tagname} with file ${py_file} and module ${MODNAME}")
add_python_unit_test("py-optional-${tagname}" "unittest/python/${py_file}"
"unittest")
endfunction()
config_bind_optional(boost "boost::optional")
if(CMAKE_CXX_STANDARD GREATER 14 AND CMAKE_CXX_STANDARD LESS 98)
config_bind_optional(std "std::optional")
endif()
add_python_unit_test("py-matrix" "unittest/python/test_matrix.py" "unittest") add_python_unit_test("py-matrix" "unittest/python/test_matrix.py" "unittest")
...@@ -98,6 +115,3 @@ set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP}) ...@@ -98,6 +115,3 @@ set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP})
add_python_unit_test("py-user-struct" "unittest/python/test_user_struct.py" add_python_unit_test("py-user-struct" "unittest/python/test_user_struct.py"
"python;unittest") "python;unittest")
set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP}) set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP})
add_python_unit_test("py-optional" "unittest/python/test_optional.py"
"unittest")
#include "eigenpy/eigenpy.hpp" #include "eigenpy/eigenpy.hpp"
#include "eigenpy/optional.hpp" #include "eigenpy/optional.hpp"
#ifdef EIGENPY_WITH_CXX17_SUPPORT
#include <optional>
#endif
#define OPTIONAL boost::optional #cmakedefine OPTIONAL @OPTIONAL@
#define OPT_NONE boost::none
using opt_dbl = OPTIONAL<double>; typedef eigenpy::detail::nullopt_helper<OPTIONAL> none_helper;
static auto OPT_NONE = none_helper::value();
typedef OPTIONAL<double> opt_dbl;
struct mystruct { struct mystruct {
OPTIONAL<int> a; OPTIONAL<int> a;
opt_dbl b; opt_dbl b;
OPTIONAL<std::string> msg{"i am struct"}; OPTIONAL<std::string> msg{"i am struct"};
mystruct() : a(OPT_NONE), b(boost::none) {} mystruct() : a(OPT_NONE), b(OPT_NONE) {}
mystruct(int a, const opt_dbl &b = OPT_NONE) : a(a), b(b) {} mystruct(int a, const opt_dbl &b = OPT_NONE) : a(a), b(b) {}
}; };
...@@ -36,13 +40,13 @@ OPTIONAL<Eigen::MatrixXd> random_mat_if_true(bool flag) { ...@@ -36,13 +40,13 @@ OPTIONAL<Eigen::MatrixXd> random_mat_if_true(bool flag) {
return OPT_NONE; return OPT_NONE;
} }
BOOST_PYTHON_MODULE(bind_optional) { BOOST_PYTHON_MODULE(@MODNAME@) {
using namespace eigenpy; using namespace eigenpy;
OptionalConverter<int>::registration(); OptionalConverter<int, OPTIONAL>::registration();
OptionalConverter<double>::registration(); OptionalConverter<double, OPTIONAL>::registration();
OptionalConverter<std::string>::registration(); OptionalConverter<std::string, OPTIONAL>::registration();
OptionalConverter<mystruct>::registration(); OptionalConverter<mystruct, OPTIONAL>::registration();
OptionalConverter<Eigen::MatrixXd>::registration(); OptionalConverter<Eigen::MatrixXd, OPTIONAL>::registration();
enableEigenPy(); enableEigenPy();
bp::class_<mystruct>("mystruct", bp::no_init) bp::class_<mystruct>("mystruct", bp::no_init)
......
import bind_optional import importlib
bind_optional = importlib.import_module("@MODNAME@")
def test_none_if_zero(): def test_none_if_zero():
...@@ -58,8 +60,8 @@ def test_random_mat(): ...@@ -58,8 +60,8 @@ def test_random_mat():
assert M.shape == (4, 4) assert M.shape == (4, 4)
if __name__ == "__main__": test_none_if_zero()
import pytest test_struct_ctors()
import sys test_struct_setters()
test_factory()
sys.exit(pytest.main(sys.argv)) test_random_mat()
import importlib
bind_optional = importlib.import_module("bind_optional_boost")
def test_none_if_zero():
x = bind_optional.none_if_zero(0)
y = bind_optional.none_if_zero(-1)
assert x is None
assert y == -1
def test_struct_ctors():
# test struct ctors
struct = bind_optional.mystruct()
assert struct.a is None
assert struct.b is None
assert struct.msg == "i am struct"
## no 2nd arg automatic overload using bp::optional
struct = bind_optional.mystruct(2)
assert struct.a == 2
assert struct.b is None
struct = bind_optional.mystruct(13, -1.0)
assert struct.a == 13
assert struct.b == -1.0
def test_struct_setters():
struct = bind_optional.mystruct()
struct.a = 1
assert struct.a == 1
struct.b = -3.14
assert struct.b == -3.14
# set to None
struct.a = None
struct.b = None
struct.msg = None
assert struct.a is None
assert struct.b is None
assert struct.msg is None
def test_factory():
struct = bind_optional.create_if_true(False, None)
assert struct is None
struct = bind_optional.create_if_true(True, None)
assert struct.a == 0
assert struct.b is None
def test_random_mat():
M = bind_optional.random_mat_if_true(False)
assert M is None
M = bind_optional.random_mat_if_true(True)
assert M.shape == (4, 4)
test_none_if_zero()
test_struct_ctors()
test_struct_setters()
test_factory()
test_random_mat()
import importlib
bind_optional = importlib.import_module("bind_optional_std")
def test_none_if_zero():
x = bind_optional.none_if_zero(0)
y = bind_optional.none_if_zero(-1)
assert x is None
assert y == -1
def test_struct_ctors():
# test struct ctors
struct = bind_optional.mystruct()
assert struct.a is None
assert struct.b is None
assert struct.msg == "i am struct"
## no 2nd arg automatic overload using bp::optional
struct = bind_optional.mystruct(2)
assert struct.a == 2
assert struct.b is None
struct = bind_optional.mystruct(13, -1.0)
assert struct.a == 13
assert struct.b == -1.0
def test_struct_setters():
struct = bind_optional.mystruct()
struct.a = 1
assert struct.a == 1
struct.b = -3.14
assert struct.b == -3.14
# set to None
struct.a = None
struct.b = None
struct.msg = None
assert struct.a is None
assert struct.b is None
assert struct.msg is None
def test_factory():
struct = bind_optional.create_if_true(False, None)
assert struct is None
struct = bind_optional.create_if_true(True, None)
assert struct.a == 0
assert struct.b is None
def test_random_mat():
M = bind_optional.random_mat_if_true(False)
assert M is None
M = bind_optional.random_mat_if_true(True)
assert M.shape == (4, 4)
test_none_if_zero()
test_struct_ctors()
test_struct_setters()
test_factory()
test_random_mat()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment