test_return_by_ref.py
Go to the documentation of this file.
1 import return_by_ref
2 from return_by_ref import Matrix, RowMatrix, Vector
3 import numpy as np
4 
5 
6 def test_shared(mat):
7  m_ref = mat.ref()
8  m_ref.fill(0)
9  m_copy = mat.copy()
10  assert np.array_equal(m_ref, m_copy)
11 
12  m_const_ref = mat.const_ref()
13  assert np.array_equal(m_const_ref, m_copy)
14  assert np.array_equal(m_const_ref, m_ref)
15 
16  m_ref.fill(1)
17  assert not np.array_equal(m_ref, m_copy)
18  assert np.array_equal(m_const_ref, m_ref)
19 
20  try:
21  m_const_ref.fill(2)
22  assert False
23  except Exception:
24  assert True
25 
26 
27 def test_not_shared(mat):
28  m_ref = mat.ref()
29  m_ref.fill(100.0)
30  m_copy = mat.copy()
31  assert not np.array_equal(m_ref, m_copy)
32 
33  m_const_ref = mat.const_ref()
34  assert np.array_equal(m_const_ref, m_copy)
35  assert not np.array_equal(m_const_ref, m_ref)
36 
37  m_ref.fill(10.0)
38  assert not np.array_equal(m_ref, m_copy)
39  assert not np.array_equal(m_const_ref, m_ref)
40 
41  try:
42  m_const_ref.fill(2)
43  assert True
44  except Exception:
45  assert False
46 
47 
48 rows = 10
49 cols = 30
50 
51 mat = Matrix(rows, cols)
52 row_mat = RowMatrix(rows, cols)
53 vec = Vector(rows, 1)
54 
55 test_shared(mat)
56 test_shared(row_mat)
57 test_shared(vec)
58 
59 return_by_ref.sharedMemory(False)
60 test_not_shared(mat)
61 test_not_shared(row_mat)
62 test_not_shared(vec)


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 2 2023 02:10:26