Skip to content

Commit 79ced4d

Browse files
authored
Merge pull request #350 from ManifoldFR/wj/add-test-user-struct
Tests: add user-struct test
2 parents 04e3c68 + e0c0ebd commit 79ced4d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

unittest/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ if(NOT NUMPY_WITH_BROKEN_UFUNC_SUPPORT)
3838
add_lib_unit_test(user_type)
3939
endif()
4040
add_lib_unit_test(std_vector)
41+
add_lib_unit_test(user_struct)
4142

4243
add_python_unit_test("py-matrix" "unittest/python/test_matrix.py" "unittest")
4344

@@ -92,3 +93,7 @@ endif(NOT WIN32)
9293
add_python_unit_test("py-std-vector" "unittest/python/test_std_vector.py"
9394
"python;unittest")
9495
set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP})
96+
97+
add_python_unit_test("py-user-struct" "unittest/python/test_user_struct.py"
98+
"python;unittest")
99+
set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
from user_struct import *
3+
4+
5+
x = np.ones(3)
6+
y = np.ones(4)
7+
ms = MyStruct(x, y)
8+
print(ms.x)
9+
print(ms.y)
10+
11+
ms.x[0] = 0.0
12+
13+
ms.x = x # ok
14+
assert np.allclose(ms.x, x)
15+
16+
ms.y[:] = y
17+
ms.y = y # segfault

unittest/user_struct.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "eigenpy/eigenpy.hpp"
2+
3+
struct mystruct {
4+
Eigen::Vector3d x_;
5+
Eigen::Vector4d y_;
6+
7+
mystruct(const Eigen::Vector3d& x, const Eigen::Vector4d& y) : x_(x), y_(y) {}
8+
};
9+
10+
BOOST_PYTHON_MODULE(user_struct) {
11+
using namespace Eigen;
12+
namespace bp = boost::python;
13+
eigenpy::enableEigenPy();
14+
bp::class_<mystruct>("MyStruct", bp::init<const Vector3d&, const Vector4d&>())
15+
.add_property(
16+
"x",
17+
bp::make_getter(&mystruct::x_, bp::return_internal_reference<>()),
18+
bp::make_setter(&mystruct::x_))
19+
.add_property(
20+
"y",
21+
bp::make_getter(&mystruct::y_, bp::return_internal_reference<>()),
22+
bp::make_setter(&mystruct::y_));
23+
}

0 commit comments

Comments
 (0)