From fb093e17c8166931ff5011a74e0be621e2122d99 Mon Sep 17 00:00:00 2001 From: Justin Carpentier <justin.carpentier@inria.fr> Date: Sun, 23 Feb 2020 09:49:12 +0100 Subject: [PATCH] test: add Python test for return-by-ref --- unittest/CMakeLists.txt | 1 + unittest/python/test_return_by_ref.py | 34 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 unittest/python/test_return_by_ref.py diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 747401e2..17bf479f 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -40,6 +40,7 @@ ENDIF() ADD_PYTHON_UNIT_TEST("py-matrix" "unittest/python/test_matrix.py" "unittest") ADD_PYTHON_UNIT_TEST("py-geometry" "unittest/python/test_geometry.py" "unittest") ADD_PYTHON_UNIT_TEST("py-complex" "unittest/python/test_complex.py" "unittest") +ADD_PYTHON_UNIT_TEST("py-return-by-ref" "unittest/python/test_return_by_ref.py" "unittest") ADD_PYTHON_UNIT_TEST("py-switch" "unittest/python/test_switch.py" "python/eigenpy") SET_TESTS_PROPERTIES("py-switch" PROPERTIES DEPENDS ${PYWRAP}) diff --git a/unittest/python/test_return_by_ref.py b/unittest/python/test_return_by_ref.py new file mode 100644 index 00000000..969f8467 --- /dev/null +++ b/unittest/python/test_return_by_ref.py @@ -0,0 +1,34 @@ +from return_by_ref import Matrix, RowMatrix, Vector +import numpy as np + +def test(mat): + + m_ref = mat.ref() + m_ref.fill(0) + m_copy = mat.copy() + assert np.array_equal(m_ref,m_copy) + + m_const_ref = mat.const_ref() + assert np.array_equal(m_const_ref,m_copy) + assert np.array_equal(m_const_ref,m_ref) + + m_ref.fill(1) + assert not np.array_equal(m_ref,m_copy) + assert np.array_equal(m_const_ref,m_ref) + + try: + m_const_ref.fill(2) + assert False + except: + assert True + +rows = 10 +cols = 30 + +mat = Matrix(rows,cols) +row_mat = RowMatrix(rows,cols) +vec = Vector(rows,1) + +test(mat) +test(row_mat) +test(vec) -- GitLab