2 Unit tests for Matlab wrap program 3 Author: Matthew Sklar, Varun Agrawal 14 from loguru
import logger
16 sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
25 Test the Matlab wrapper 27 TEST_DIR = osp.dirname(osp.realpath(__file__))
28 INTERFACE_DIR = osp.join(TEST_DIR,
"fixtures")
29 MATLAB_TEST_DIR = osp.join(TEST_DIR,
"expected",
"matlab")
30 MATLAB_ACTUAL_DIR = osp.join(TEST_DIR,
"actual",
"matlab")
33 os.makedirs(MATLAB_ACTUAL_DIR, exist_ok=
True)
37 logger.add(sys.stderr, format=
"{time} {level} {message}", level=
"INFO")
40 """Generate files and folders from matlab wrapper content. 43 cc_content -- the content to generate formatted as 44 (file_name, file_content) or 45 (folder_name, [(file_name, file_content)]) 46 path -- the path to the files parent folder within the main folder 52 logger.debug(
"c object: {}".
format(c[0][0]))
53 path_to_folder = osp.join(path, c[0][0])
55 if not osp.isdir(path_to_folder):
57 os.makedirs(path_to_folder, exist_ok=
True)
62 logger.debug(
"sub object: {}".
format(sub_content[1][0][0]))
66 path_to_folder = osp.join(path, c[0])
69 "[generate_content_global]: {}".
format(path_to_folder))
70 if not osp.isdir(path_to_folder):
72 os.makedirs(path_to_folder, exist_ok=
True)
75 for sub_content
in c[1]:
76 path_to_file = osp.join(path_to_folder, sub_content[0])
78 "[generate_global_method]: {}".
format(path_to_file))
79 with open(path_to_file,
'w')
as f:
80 f.write(sub_content[1])
83 path_to_file = osp.join(path, c[0])
85 logger.debug(
"[generate_content]: {}".
format(path_to_file))
86 if not osp.isdir(path_to_file):
92 with open(path_to_file,
'w')
as f:
97 Compute the comparison between the expected and actual file, 98 and assert if diff is zero. 102 success = filecmp.cmp(output, expected)
105 os.system(
"diff {} {}".
format(output, expected))
106 self.assertTrue(success,
"Mismatch for file {0}".
format(file))
110 Check generation of matlab geometry wrapper. 111 python3 wrap/matlab_wrapper.py --src wrap/tests/geometry.h 112 --module_name geometry --out wrap/tests/actual-matlab 114 with open(osp.join(self.
INTERFACE_DIR,
'geometry.i'),
'r') as f: 120 module = parser.Module.parseString(content)
122 instantiator.instantiate_namespace_inplace(module)
127 module_name=
'geometry',
128 top_module_namespace=[
'gtsam'],
132 cc_content = wrapper.wrap()
138 files = [
'+gtsam/Point2.m',
'+gtsam/Point3.m',
'geometry_wrapper.cpp']
144 """Test interface file with function info.""" 145 with open(osp.join(self.
INTERFACE_DIR,
'functions.i'),
'r') as f: 151 module = parser.Module.parseString(content)
153 instantiator.instantiate_namespace_inplace(module)
157 module_name=
'functions',
158 top_module_namespace=[
'gtsam'],
162 cc_content = wrapper.wrap()
167 'functions_wrapper.cpp',
'aGlobalFunction.m',
'load2D.m',
168 'MultiTemplatedFunctionDoubleSize_tDouble.m',
169 'MultiTemplatedFunctionStringSize_tDouble.m',
170 'overloadedGlobalFunction.m',
'TemplatedFunctionRot3.m' 177 """Test interface file with only class info.""" 178 with open(osp.join(self.
INTERFACE_DIR,
'class.i'),
'r') as f: 184 module = parser.Module.parseString(content)
186 instantiator.instantiate_namespace_inplace(module)
191 top_module_namespace=[
'gtsam'],
195 cc_content = wrapper.wrap()
200 'class_wrapper.cpp',
'FunDouble.m',
'FunRange.m',
201 'MultipleTemplatesIntDouble.m',
'MultipleTemplatesIntFloat.m',
202 'MyFactorPosePoint2.m',
'MyVector3.m',
'MyVector12.m',
203 'PrimitiveRefDouble.m',
'Test.m' 210 """Test interface file with class inheritance definitions.""" 211 with open(osp.join(self.
INTERFACE_DIR,
'inheritance.i'),
'r') as f: 217 module = parser.Module.parseString(content)
219 instantiator.instantiate_namespace_inplace(module)
223 module_name=
'inheritance',
224 top_module_namespace=[
'gtsam'],
228 cc_content = wrapper.wrap()
233 'inheritance_wrapper.cpp',
'MyBase.m',
'MyTemplateMatrix.m',
242 Test interface file with full namespace definition. 244 with open(osp.join(self.
INTERFACE_DIR,
'namespaces.i'),
'r') as f: 250 module = parser.Module.parseString(content)
252 instantiator.instantiate_namespace_inplace(module)
256 module_name=
'namespaces',
257 top_module_namespace=[
'gtsam'],
261 cc_content = wrapper.wrap()
266 'namespaces_wrapper.cpp',
'+ns1/aGlobalFunction.m',
267 '+ns1/ClassA.m',
'+ns1/ClassB.m',
'+ns2/+ns3/ClassB.m',
268 '+ns2/aGlobalFunction.m',
'+ns2/ClassA.m',
'+ns2/ClassC.m',
269 '+ns2/overloadedGlobalFunction.m',
'ClassD.m' 277 Tests for some unique, non-trivial features. 279 with open(osp.join(self.
INTERFACE_DIR,
'special_cases.i'),
'r') as f: 285 module = parser.Module.parseString(content)
287 instantiator.instantiate_namespace_inplace(module)
291 module_name=
'special_cases',
292 top_module_namespace=[
'gtsam'],
296 cc_content = wrapper.wrap()
301 'special_cases_wrapper.cpp',
302 '+gtsam/PinholeCameraCal3Bundler.m',
303 '+gtsam/NonlinearFactorGraph.m',
309 if __name__ ==
'__main__':
void print(const Matrix &A, const string &s, ostream &stream)
def test_special_cases(self)
bool isinstance(handle obj)
def test_inheritance(self)
def compare_and_diff(self, file)
def generate_content(self, cc_content, path=MATLAB_ACTUAL_DIR)