Skip to content

Commit

Permalink
variant: Test str and complex type support
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisv committed Feb 5, 2024
1 parent 558e391 commit a75379e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
44 changes: 42 additions & 2 deletions unittest/python/test_variant.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ V2 = variant_module.V2
VariantHolder = variant_module.VariantHolder
VariantFullHolder = variant_module.VariantFullHolder
make_variant = variant_module.make_variant
make_variant_full = variant_module.make_variant_full
make_variant_full_none = variant_module.make_variant_full_none
make_variant_full_float = variant_module.make_variant_full_float
make_variant_full_int = variant_module.make_variant_full_int
make_variant_full_bool = variant_module.make_variant_full_bool
make_variant_full_str = variant_module.make_variant_full_str
make_variant_full_complex = variant_module.make_variant_full_complex

variant = make_variant()
assert isinstance(variant, V1)
Expand Down Expand Up @@ -44,9 +49,34 @@ assert isinstance(variant_holder.variant, V2)
assert variant_holder.variant.v == v2.v

# Test variant that hold a None value
v_full = make_variant_full()
v_full = make_variant_full_none()
assert v_full is None

# Test variant that hold a float value
v_full = make_variant_full_float()
assert v_full == 3.14
assert isinstance(v_full, float)

# Test variant that hold a int value
v_full = make_variant_full_int()
assert v_full == 3
assert isinstance(v_full, int)

# Test variant that hold a bool value
v_full = make_variant_full_bool()
assert not v_full
assert isinstance(v_full, bool)

# Test variant that hold a str value
v_full = make_variant_full_str()
assert v_full == "str"
assert isinstance(v_full, str)

# Test variant that hold a complex value
v_full = make_variant_full_complex()
assert v_full == 1 + 0j
assert isinstance(v_full, complex)

variant_full_holder = VariantFullHolder()

# Test None
Expand Down Expand Up @@ -81,3 +111,13 @@ assert isinstance(variant_full_holder.variant, int)
variant_full_holder.variant = 3.14
assert variant_full_holder.variant == 3.14
assert isinstance(variant_full_holder.variant, float)

# Test str
variant_full_holder.variant = "str"
assert variant_full_holder.variant == "str"
assert isinstance(variant_full_holder.variant, str)

# Test complex
variant_full_holder.variant = 1 + 0j
assert variant_full_holder.variant == 1 + 0j
assert isinstance(variant_full_holder.variant, complex)
20 changes: 17 additions & 3 deletions unittest/variant.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <eigenpy/eigenpy.hpp>
#include <eigenpy/variant.hpp>

#include <string>
#include <complex>

#cmakedefine TEST_TYPE @TEST_TYPE@
#define VARIANT TEST_TYPE

Expand Down Expand Up @@ -32,12 +35,18 @@ struct MyVariantNoneHelper<std::variant<Alternatives...> > {
};
#endif

typedef typename MyVariantNoneHelper<VARIANT<V1, bool, int, double> >::type
typedef typename MyVariantNoneHelper<
VARIANT<V1, bool, int, double, std::string, std::complex<double> > >::type
MyVariantFull;

MyVariant make_variant() { return V1(); }

MyVariantFull make_variant_full() { return MyVariantFull(); }
MyVariantFull make_variant_full_none() { return MyVariantFull(); }
MyVariantFull make_variant_full_float() { return 3.14; }
MyVariantFull make_variant_full_int() { return 3; }
MyVariantFull make_variant_full_bool() { return false; }
MyVariantFull make_variant_full_str() { return std::string("str"); }
MyVariantFull make_variant_full_complex() { return std::complex<double>(1., 0.); }

struct VariantHolder {
MyVariant variant;
Expand Down Expand Up @@ -68,7 +77,12 @@ BOOST_PYTHON_MODULE(@MODNAME@) {

typedef eigenpy::VariantConverter<MyVariantFull> ConverterFull;
ConverterFull::registration();
bp::def("make_variant_full", make_variant_full);
bp::def("make_variant_full_none", make_variant_full_none);
bp::def("make_variant_full_float", make_variant_full_float);
bp::def("make_variant_full_int", make_variant_full_int);
bp::def("make_variant_full_bool", make_variant_full_bool);
bp::def("make_variant_full_str", make_variant_full_str);
bp::def("make_variant_full_complex", make_variant_full_complex);

boost::python::class_<VariantFullHolder>("VariantFullHolder", bp::init<>())
.add_property("variant",
Expand Down

0 comments on commit a75379e

Please sign in to comment.