test_matlab_wrapper.py
Go to the documentation of this file.
1 """
2 Unit tests for Matlab wrap program
3 Author: Matthew Sklar, Varun Agrawal
4 Date: March 2019
5 """
6 # pylint: disable=import-error, wrong-import-position
7 
8 import filecmp
9 import os
10 import os.path as osp
11 import sys
12 import unittest
13 
14 sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
15 
16 from gtwrap.matlab_wrapper import MatlabWrapper
17 
18 
19 class TestWrap(unittest.TestCase):
20  """
21  Test the Matlab wrapper
22  """
23 
24  def setUp(self) -> None:
25  super().setUp()
26 
27  # Set up all the directories
28  self.TEST_DIR = osp.dirname(osp.realpath(__file__))
29  self.INTERFACE_DIR = osp.join(self.TEST_DIR, "fixtures")
30  self.MATLAB_TEST_DIR = osp.join(self.TEST_DIR, "expected", "matlab")
31  self.MATLAB_ACTUAL_DIR = osp.join(self.TEST_DIR, "actual", "matlab")
32 
33  if not osp.exists(self.MATLAB_ACTUAL_DIR):
34  os.mkdir(self.MATLAB_ACTUAL_DIR)
35 
36  # Generate the matlab.h file if it does not exist
37  template_file = osp.join(self.TEST_DIR, "..", "gtwrap",
38  "matlab_wrapper", "matlab_wrapper.tpl")
39  if not osp.exists(template_file):
40  with open(template_file, 'w', encoding="UTF-8") as tpl:
41  tpl.write("#include <gtwrap/matlab.h>\n#include <map>\n")
42 
43  # Create the `actual/matlab` directory
44  os.makedirs(self.MATLAB_ACTUAL_DIR, exist_ok=True)
45 
46  def compare_and_diff(self, file, actual):
47  """
48  Compute the comparison between the expected and actual file,
49  and assert if diff is zero.
50  """
51  expected = osp.join(self.MATLAB_TEST_DIR, file)
52  success = filecmp.cmp(actual, expected)
53 
54  if not success:
55  os.system(f"diff {actual} {expected}")
56  self.assertTrue(success, f"Mismatch for file {file}")
57 
58  def test_geometry(self):
59  """
60  Check generation of matlab geometry wrapper.
61  python3 wrap/matlab_wrapper.py --src wrap/tests/geometry.h
62  --module_name geometry --out wrap/tests/actual-matlab
63  """
64  file = osp.join(self.INTERFACE_DIR, 'geometry.i')
65 
66  # Create MATLAB wrapper instance
67  wrapper = MatlabWrapper(module_name='geometry',
68  top_module_namespace=['gtsam'],
69  ignore_classes=[''],
70  use_boost_serialization=True)
71 
72  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
73 
74  files = ['+gtsam/Point2.m', '+gtsam/Point3.m', 'geometry_wrapper.cpp']
75 
76  self.assertTrue(osp.isdir(osp.join(self.MATLAB_ACTUAL_DIR, '+gtsam')))
77 
78  for file in files:
79  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
80  self.compare_and_diff(file, actual)
81 
82  def test_functions(self):
83  """Test interface file with function info."""
84  file = osp.join(self.INTERFACE_DIR, 'functions.i')
85 
86  wrapper = MatlabWrapper(
87  module_name='functions',
88  top_module_namespace=['gtsam'],
89  ignore_classes=[''],
90  )
91 
92  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
93 
94  files = [
95  'functions_wrapper.cpp',
96  'aGlobalFunction.m',
97  'load2D.m',
98  'MultiTemplatedFunctionDoubleSize_tDouble.m',
99  'MultiTemplatedFunctionStringSize_tDouble.m',
100  'overloadedGlobalFunction.m',
101  'TemplatedFunctionRot3.m',
102  'DefaultFuncInt.m',
103  'DefaultFuncObj.m',
104  'DefaultFuncString.m',
105  'DefaultFuncVector.m',
106  'DefaultFuncZero.m',
107  'setPose.m',
108  ]
109 
110  for file in files:
111  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
112  self.compare_and_diff(file, actual)
113 
114  def test_class(self):
115  """Test interface file with only class info."""
116  file = osp.join(self.INTERFACE_DIR, 'class.i')
117 
118  wrapper = MatlabWrapper(
119  module_name='class',
120  top_module_namespace=['gtsam'],
121  ignore_classes=[''],
122  )
123 
124  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
125 
126  files = [
127  'class_wrapper.cpp',
128  'FunDouble.m',
129  'FunRange.m',
130  'MultipleTemplatesIntDouble.m',
131  'MultipleTemplatesIntFloat.m',
132  'MyFactorPosePoint2.m',
133  'MyVector3.m',
134  'MyVector12.m',
135  'PrimitiveRefDouble.m',
136  'Test.m',
137  'ForwardKinematics.m',
138  ]
139 
140  for file in files:
141  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
142  self.compare_and_diff(file, actual)
143 
144  def test_enum(self):
145  """Test interface file with only enum info."""
146  file = osp.join(self.INTERFACE_DIR, 'enum.i')
147 
148  wrapper = MatlabWrapper(
149  module_name='enum',
150  top_module_namespace=['gtsam'],
151  ignore_classes=[''],
152  )
153 
154  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
155 
156  files = [
157  'enum_wrapper.cpp',
158  'Color.m',
159  '+Pet/Kind.m',
160  '+gtsam/VerbosityLM.m',
161  '+gtsam/+MCU/Avengers.m',
162  '+gtsam/+MCU/GotG.m',
163  '+gtsam/+OptimizerGaussNewtonParams/Verbosity.m',
164  ]
165 
166  for file in files:
167  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
168  self.compare_and_diff(file, actual)
169 
170  def test_templates(self):
171  """Test interface file with template info."""
172  file = osp.join(self.INTERFACE_DIR, 'templates.i')
173 
174  wrapper = MatlabWrapper(
175  module_name='template',
176  top_module_namespace=['gtsam'],
177  ignore_classes=[''],
178  )
179 
180  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
181 
182  files = [
183  'template_wrapper.cpp', 'ScopedTemplateResult.m',
184  'TemplatedConstructor.m'
185  ]
186 
187  for file in files:
188  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
189  self.compare_and_diff(file, actual)
190 
191  def test_inheritance(self):
192  """Test interface file with class inheritance definitions."""
193  file = osp.join(self.INTERFACE_DIR, 'inheritance.i')
194 
195  wrapper = MatlabWrapper(
196  module_name='inheritance',
197  top_module_namespace=['gtsam'],
198  ignore_classes=[''],
199  )
200  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
201 
202  files = [
203  'inheritance_wrapper.cpp',
204  'MyBase.m',
205  'MyTemplateMatrix.m',
206  'MyTemplatePoint2.m',
207  'ForwardKinematicsFactor.m',
208  ]
209 
210  for file in files:
211  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
212  self.compare_and_diff(file, actual)
213 
214  def test_namespaces(self):
215  """
216  Test interface file with full namespace definition.
217  """
218  file = osp.join(self.INTERFACE_DIR, 'namespaces.i')
219 
220  wrapper = MatlabWrapper(
221  module_name='namespaces',
222  top_module_namespace=['gtsam'],
223  ignore_classes=[''],
224  )
225 
226  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
227 
228  files = [
229  'namespaces_wrapper.cpp',
230  '+ns1/aGlobalFunction.m',
231  '+ns1/ClassA.m',
232  '+ns1/ClassB.m',
233  '+ns2/+ns3/ClassB.m',
234  '+ns2/aGlobalFunction.m',
235  '+ns2/ClassA.m',
236  '+ns2/ClassC.m',
237  '+ns2/overloadedGlobalFunction.m',
238  'ClassD.m',
239  '+gtsam/Values.m',
240  ]
241 
242  for file in files:
243  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
244  self.compare_and_diff(file, actual)
245 
247  """
248  Tests for some unique, non-trivial features.
249  """
250  file = osp.join(self.INTERFACE_DIR, 'special_cases.i')
251 
252  wrapper = MatlabWrapper(
253  module_name='special_cases',
254  top_module_namespace=['gtsam'],
255  ignore_classes=[''],
256  )
257  wrapper.wrap([file], path=self.MATLAB_ACTUAL_DIR)
258 
259  files = [
260  'special_cases_wrapper.cpp',
261  '+gtsam/GeneralSFMFactorCal3Bundler.m',
262  '+gtsam/NonlinearFactorGraph.m',
263  '+gtsam/PinholeCameraCal3Bundler.m',
264  '+gtsam/SfmTrack.m',
265  ]
266 
267  for file in files:
268  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
269  self.compare_and_diff(file, actual)
270 
272  """
273  Test for when multiple interface files are specified.
274  """
275  file1 = osp.join(self.INTERFACE_DIR, 'part1.i')
276  file2 = osp.join(self.INTERFACE_DIR, 'part2.i')
277 
278  wrapper = MatlabWrapper(
279  module_name='multiple_files',
280  top_module_namespace=['gtsam'],
281  ignore_classes=[''],
282  )
283 
284  wrapper.wrap([file1, file2], path=self.MATLAB_ACTUAL_DIR)
285 
286  files = [
287  'multiple_files_wrapper.cpp',
288  '+gtsam/Class1.m',
289  '+gtsam/Class2.m',
290  '+gtsam/ClassA.m',
291  ]
292 
293  for file in files:
294  actual = osp.join(self.MATLAB_ACTUAL_DIR, file)
295  self.compare_and_diff(file, actual)
296 
297 
298 if __name__ == '__main__':
299  unittest.main()
def compare_and_diff(self, file, actual)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46