From cdbe8e7bb46ed2f9e5ca8115c64652b55d830f97 Mon Sep 17 00:00:00 2001
From: ManifoldFR <wilson.jallet@polytechnique.org>
Date: Wed, 5 Oct 2022 16:18:29 +0200
Subject: [PATCH] Add unit tests for exposing std::vector

* tests we can expose std::vector<Matrix3d>
* test input and returning std::vector<Matrix> objects
* test specific functions for specific dims
---
 unittest/CMakeLists.txt            |  5 ++++
 unittest/python/test_std_vector.py | 33 +++++++++++++++++++++++---
 unittest/vector.cpp                | 38 ++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 3 deletions(-)
 create mode 100644 unittest/vector.cpp

diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
index 55d6b569..507f1456 100644
--- a/unittest/CMakeLists.txt
+++ b/unittest/CMakeLists.txt
@@ -33,6 +33,7 @@ if(NOT ${EIGEN3_VERSION} VERSION_LESS "3.2.0")
   add_lib_unit_test(eigen_ref)
 endif()
 add_lib_unit_test(user_type)
+add_lib_unit_test(vector)
 
 add_python_unit_test("py-matrix" "unittest/python/test_matrix.py" "unittest")
 add_python_unit_test("py-geometry" "unittest/python/test_geometry.py"
@@ -78,3 +79,7 @@ if(NOT WIN32)
                        "python;unittest")
   set_tests_properties("py-MINRES" PROPERTIES DEPENDS ${PYWRAP})
 endif(NOT WIN32)
+
+add_python_unit_test("py-std-vector" "unittest/python/test_std_vector.py"
+                     "python;unittest")
+set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP})
diff --git a/unittest/python/test_std_vector.py b/unittest/python/test_std_vector.py
index 3d679cd6..ddc900f8 100644
--- a/unittest/python/test_std_vector.py
+++ b/unittest/python/test_std_vector.py
@@ -1,9 +1,14 @@
 import numpy as np
 import eigenpy
+import inspect
+import vector
+from vector import printVectorOfMatrix, printVectorOf3x3, copyStdVector
 
-x0 = np.random.randn(3)
-l1 = [x0, x0, x0]
-l2 = eigenpy.StdVec_VectorXd(3, x0)
+np.random.seed(0)
+
+l1 = [np.random.randn(3), np.random.randn(2)]
+l2 = eigenpy.StdVec_VectorXd(l1)
+l3 = [np.random.randn(2, 2), np.random.randn(1, 2), np.random.randn(2, 6)]
 
 
 def checkAllValues(li1, li2):
@@ -14,3 +19,25 @@ def checkAllValues(li1, li2):
 
 
 checkAllValues(l1, l2)
+checkAllValues(l1, copyStdVector(l1))
+
+
+printVectorOfMatrix(l1)
+print()
+printVectorOfMatrix(l2)
+print()
+printVectorOfMatrix(l3)
+print()
+
+
+l4 = [np.random.randn(3, 3) for _ in range(4)]
+assert "StdVec_Mat3d" in printVectorOf3x3.__doc__
+printVectorOf3x3(l4)
+
+l4_copy = copyStdVector(l4)
+assert isinstance(l4_copy, eigenpy.StdVec_MatrixXd)
+print(l4_copy)
+
+l4_copy2 = vector.copyStdVec_3x3(l4)
+assert isinstance(l4_copy2, vector.StdVec_Mat3d)
+print(l4_copy2)
diff --git a/unittest/vector.cpp b/unittest/vector.cpp
new file mode 100644
index 00000000..f18fa5f2
--- /dev/null
+++ b/unittest/vector.cpp
@@ -0,0 +1,38 @@
+#include "eigenpy/eigenpy.hpp"
+#include "eigenpy/std-vector.hpp"
+
+
+#include <ostream>
+#include <type_traits>
+
+template<typename MatType>
+void printVectorOfMatrix(const std::vector<MatType> &Ms) {
+  const std::size_t n = Ms.size();
+  for (std::size_t i = 0; i < n; i++) {
+    std::cout << "el[" << i << "] =\n" << Ms[i] << '\n';
+  }
+}
+
+template<typename MatType>
+std::vector<MatType> copy(const std::vector<MatType> &Ms) {
+  std::vector<MatType> out = Ms;
+  return out;
+}
+
+
+BOOST_PYTHON_MODULE(vector) {
+  namespace bp = boost::python;
+
+  eigenpy::enableEigenPy();
+
+  bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::VectorXd>);
+  bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::MatrixXd>);
+
+  bp::def("copyStdVector", copy<Eigen::MatrixXd>);
+  bp::def("copyStdVector", copy<Eigen::VectorXd>);
+
+  eigenpy::StdVectorPythonVisitor<std::vector<Eigen::Matrix3d>>::expose("StdVec_Mat3d", "3D matrices.");
+  bp::def("printVectorOf3x3", printVectorOfMatrix<Eigen::Matrix3d>);
+  bp::def("copyStdVec_3x3", copy<Eigen::Matrix3d>, bp::args("mats"));
+
+}
-- 
GitLab