Skip to content

Commit e9d0531

Browse files
committed
separate cpp and python tests, and generate variants
configure_file will overrite boost_optional.cpp only if the content changes. This allow to a quick check on the actual code, and simplifies the cmake
1 parent c94fd5b commit e9d0531

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+770
-22
lines changed

unittest/cpp/.clang-format-ignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
boost_optional.cpp
2+
boost_variant.cpp
3+
std_variant.cpp
4+
std_optional.cpp

unittest/cpp/boost_optional.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
///
2+
/// Copyright (c) 2023 CNRS INRIA
3+
///
4+
5+
#include "eigenpy/eigenpy.hpp"
6+
#include "eigenpy/optional.hpp"
7+
#ifdef EIGENPY_WITH_CXX17_SUPPORT
8+
#include <optional>
9+
#endif
10+
11+
#define TEST_TYPE boost::optional
12+
#define OPTIONAL TEST_TYPE
13+
14+
typedef eigenpy::detail::nullopt_helper<OPTIONAL> none_helper;
15+
static auto OPT_NONE = none_helper::value();
16+
typedef OPTIONAL<double> opt_dbl;
17+
18+
struct mystruct {
19+
OPTIONAL<int> a;
20+
opt_dbl b;
21+
OPTIONAL<std::string> msg{"i am struct"};
22+
mystruct() : a(OPT_NONE), b(OPT_NONE) {}
23+
mystruct(int a, const opt_dbl &b = OPT_NONE) : a(a), b(b) {}
24+
};
25+
26+
OPTIONAL<int> none_if_zero(int i) {
27+
if (i == 0)
28+
return OPT_NONE;
29+
else
30+
return i;
31+
}
32+
33+
OPTIONAL<mystruct> create_if_true(bool flag, opt_dbl b = OPT_NONE) {
34+
if (flag) {
35+
return mystruct(0, b);
36+
} else {
37+
return OPT_NONE;
38+
}
39+
}
40+
41+
OPTIONAL<Eigen::MatrixXd> random_mat_if_true(bool flag) {
42+
if (flag)
43+
return Eigen::MatrixXd(Eigen::MatrixXd::Random(4, 4));
44+
else
45+
return OPT_NONE;
46+
}
47+
48+
BOOST_PYTHON_MODULE(boost_optional) {
49+
using namespace eigenpy;
50+
OptionalConverter<int, OPTIONAL>::registration();
51+
OptionalConverter<double, OPTIONAL>::registration();
52+
OptionalConverter<std::string, OPTIONAL>::registration();
53+
OptionalConverter<mystruct, OPTIONAL>::registration();
54+
OptionalConverter<Eigen::MatrixXd, OPTIONAL>::registration();
55+
enableEigenPy();
56+
57+
bp::class_<mystruct>("mystruct", bp::no_init)
58+
.def(bp::init<>(bp::args("self")))
59+
.def(bp::init<int, bp::optional<const opt_dbl &> >(
60+
bp::args("self", "a", "b")))
61+
.add_property(
62+
"a",
63+
bp::make_getter(&mystruct::a,
64+
bp::return_value_policy<bp::return_by_value>()),
65+
bp::make_setter(&mystruct::a))
66+
.add_property(
67+
"b",
68+
bp::make_getter(&mystruct::b,
69+
bp::return_value_policy<bp::return_by_value>()),
70+
bp::make_setter(&mystruct::b))
71+
.add_property(
72+
"msg",
73+
bp::make_getter(&mystruct::msg,
74+
bp::return_value_policy<bp::return_by_value>()),
75+
bp::make_setter(&mystruct::msg));
76+
77+
bp::def("none_if_zero", none_if_zero, bp::args("i"));
78+
bp::def("create_if_true", create_if_true,
79+
(bp::arg("flag"), bp::arg("b") = OPT_NONE));
80+
bp::def("random_mat_if_true", random_mat_if_true, bp::args("flag"));
81+
}

unittest/cpp/boost_variant.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/// @file
2+
/// @copyright Copyright 2024 CNRS INRIA
3+
4+
#include <eigenpy/eigenpy.hpp>
5+
#include <eigenpy/variant.hpp>
6+
7+
#include <string>
8+
#include <complex>
9+
10+
#define TEST_TYPE boost::variant
11+
#define VARIANT TEST_TYPE
12+
13+
namespace bp = boost::python;
14+
15+
struct V1 {
16+
int v;
17+
};
18+
struct V2 {
19+
char v;
20+
};
21+
typedef VARIANT<V1, V2> MyVariant;
22+
23+
template <typename Variant>
24+
struct MyVariantNoneHelper {};
25+
26+
template <typename... Alternatives>
27+
struct MyVariantNoneHelper<boost::variant<Alternatives...> > {
28+
typedef VARIANT<boost::blank, Alternatives...> type;
29+
};
30+
31+
#ifdef EIGENPY_WITH_CXX17_SUPPORT
32+
template <typename... Alternatives>
33+
struct MyVariantNoneHelper<std::variant<Alternatives...> > {
34+
typedef VARIANT<std::monostate, Alternatives...> type;
35+
};
36+
#endif
37+
38+
typedef typename MyVariantNoneHelper<
39+
VARIANT<V1, bool, int, double, std::string, std::complex<double> > >::type
40+
MyVariantFull;
41+
42+
MyVariant make_variant() { return V1(); }
43+
44+
MyVariantFull make_variant_full_none() { return MyVariantFull(); }
45+
MyVariantFull make_variant_full_float() { return 3.14; }
46+
MyVariantFull make_variant_full_int() { return 3; }
47+
MyVariantFull make_variant_full_bool() { return false; }
48+
MyVariantFull make_variant_full_str() { return std::string("str"); }
49+
MyVariantFull make_variant_full_complex() { return std::complex<double>(1., 0.); }
50+
51+
struct VariantHolder {
52+
MyVariant variant;
53+
};
54+
55+
struct VariantFullHolder {
56+
MyVariantFull variant;
57+
};
58+
59+
BOOST_PYTHON_MODULE(boost_variant) {
60+
using namespace eigenpy;
61+
62+
enableEigenPy();
63+
64+
bp::class_<V1>("V1", bp::init<>()).def_readwrite("v", &V1::v);
65+
bp::class_<V2>("V2", bp::init<>()).def_readwrite("v", &V2::v);
66+
67+
typedef eigenpy::VariantConverter<MyVariant> Converter;
68+
Converter::registration();
69+
70+
bp::def("make_variant", make_variant);
71+
72+
boost::python::class_<VariantHolder>("VariantHolder", bp::init<>())
73+
.add_property("variant",
74+
bp::make_getter(&VariantHolder::variant,
75+
Converter::return_internal_reference()),
76+
bp::make_setter(&VariantHolder::variant));
77+
78+
typedef eigenpy::VariantConverter<MyVariantFull> ConverterFull;
79+
ConverterFull::registration();
80+
bp::def("make_variant_full_none", make_variant_full_none);
81+
bp::def("make_variant_full_float", make_variant_full_float);
82+
bp::def("make_variant_full_int", make_variant_full_int);
83+
bp::def("make_variant_full_bool", make_variant_full_bool);
84+
bp::def("make_variant_full_str", make_variant_full_str);
85+
bp::def("make_variant_full_complex", make_variant_full_complex);
86+
87+
boost::python::class_<VariantFullHolder>("VariantFullHolder", bp::init<>())
88+
.add_property("variant",
89+
bp::make_getter(&VariantFullHolder::variant,
90+
ConverterFull::return_internal_reference()),
91+
bp::make_setter(&VariantFullHolder::variant));
92+
}

0 commit comments

Comments
 (0)