-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the test for pybind11 type_caster
- Loading branch information
1 parent
609923d
commit 25d5728
Showing
4 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|