Skip to content

Commit

Permalink
Implement the test for pybind11 type_caster
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRomualdi committed Nov 25, 2021
1 parent 609923d commit 25d5728
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bindings/pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ set_target_properties(pybind11_idyntree PROPERTIES

# if compile tests execute also python tests
if(IDYNTREE_COMPILE_TESTS)
add_subdirectory(tests)
add_subdirectory(tests idyntree_pybind11_test)
endif()

# Output package is:
Expand Down
10 changes: 10 additions & 0 deletions bindings/pybind11/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
pybind11_add_module(test_idyntree_vector_casters test_idyntree_vector_casters.cpp)
target_link_libraries(test_idyntree_vector_casters PUBLIC idyntree-pybind11)

# The generated Python dynamic module must have the same name as the pybind11
# module, i.e. `bindings`.
set_target_properties (test_idyntree_vector_casters PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
OUTPUT_NAME "vector_casters"
)

add_test (NAME pybind11_idyntree_test
COMMAND ${Python3_EXECUTABLE} -m unittest discover -s ${CMAKE_CURRENT_SOURCE_DIR}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/bindings/pybind11
Expand Down
61 changes: 61 additions & 0 deletions bindings/pybind11/tests/test_idyntree_vector_casters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <pybind11/pybind11.h>
#include <iDynTree/pybind11/VectorCasters.h>

#include <iDynTree/Core/VectorDynSize.h>
#include <iDynTree/Core/VectorFixSize.h>
#include <iDynTree/Core/MatrixDynSize.h>
#include <iDynTree/Core/MatrixFixSize.h>


class TestClass
{
iDynTree::VectorDynSize vectorDyn;
iDynTree::VectorFixSize<3> vectorFix;
iDynTree::MatrixDynSize matrixDyn;
iDynTree::MatrixFixSize<4,5> matrixFix;

public:

void setVectorDyn(const iDynTree::VectorDynSize& vector) {
vectorDyn = vector;
}

const iDynTree::VectorDynSize& getVectorDyn() const {
return vectorDyn;
}

void setMatrixDyn(const iDynTree::MatrixDynSize& matrix) {
matrixDyn = matrix;
}

const iDynTree::MatrixDynSize& getMatrixDyn() const {
return matrixDyn;
}

void setVectorFix(const iDynTree::VectorFixSize<3>& vector) {
vectorFix = vector;
}

const iDynTree::VectorFixSize<3>& getVectorFix() const {
return vectorFix;
}

void setMatrixFix(const iDynTree::MatrixFixSize<4,5>& matrix) {
matrixFix = matrix;
}

const iDynTree::MatrixFixSize<4,5>& getMatrixFix() const {
return matrixFix;
}
};


namespace py = ::pybind11;
PYBIND11_MODULE(vector_casters, m) {
py::class_<TestClass>(m, "TestClass")
.def(py::init())
.def_property("vector_dyn", &TestClass::getVectorDyn, &TestClass::setVectorDyn)
.def_property("vector_fix", &TestClass::getVectorFix, &TestClass::setVectorFix)
.def_property("matrix_dyn", &TestClass::getMatrixDyn, &TestClass::setMatrixDyn)
.def_property("matrix_fix", &TestClass::getMatrixFix, &TestClass::setMatrixFix);
}
44 changes: 44 additions & 0 deletions bindings/pybind11/tests/test_idyntree_vector_casters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Tests for idyntree-vector-casters."""
import unittest

from idyntree_pybind11_test.vector_casters import TestClass
import numpy as np


class TestClassTest(unittest.TestCase):

def test_vector_fix(self):
obj = TestClass()
vector = [1., 2., 3.]
obj.vector_fix = vector
self.assertEqual(len(obj.vector_fix), 3)
self.assertEqual(list(obj.vector_fix), list(vector))

def test_vector_dyn(self):
obj = TestClass()
vector = [1., 2., 3., 5., 6., 7.]
obj.vector_dyn = vector
self.assertEqual(len(obj.vector_dyn), len(vector))
self.assertEqual(list(obj.vector_dyn), list(vector))

def test_matrix_fix(self):
obj = TestClass()
matrix = np.random.rand(4, 5)
obj.matrix_fix = matrix
self.assertEqual(obj.matrix_fix.shape, matrix.shape)
for i in range(4):
for j in range(3):
self.assertEqual(obj.matrix_fix[i,j], matrix[i,j])

def test_matrix_dyn(self):
obj = TestClass()
matrix = np.random.rand(10, 10)
obj.matrix_dyn = matrix
self.assertEqual(obj.matrix_dyn.shape, matrix.shape)
for i in range(10):
for j in range(10):
self.assertEqual(obj.matrix_dyn[i,j], matrix[i,j])

if __name__ == "__main__":
unittest.main()

0 comments on commit 25d5728

Please sign in to comment.