Test equivalence between mex and (.m) matlab functions
Show older comments
What is the best way to create a test that compares outputs from a .mex and .m function? It has become a common workflow to write a processing pipeline in matlab then, if needed, find bottlenecks and try to rewrite them in a lower language with mex. I would like to confirm that my mex functions and matlab function produce equivalent (or nearly equivalent) results. Is there a builtin for running a matlab file even when a mex file of the same name exists? Being that matlab fallsback to a .m file if no mex file is compilled, it should be best practice to have both, matlab and mex, versions of the function and, therefore, it should be a common need to write tests that ensure the two are the same.
If there is no function for running the matlab file, how should I go about writing this test? I could write the functions in different file names (i.e. foo_matlab.m foo_C.c) then have a wrapper function to call the desired function (i.e. default to C if it exists but have a 'useC' option). But this is clearly not the matlab way about this as it skirts around the default behavior and then rewrites similar logic. The other option I can think of would be to delete the mex file (if it exists) in the test set up, run the function, then compile within the same test, and rerun the function in the same way, and compare the results. This has the benefit of ensuring the compiled function is up to date but it requires recompiling for every test or using an ugly method to run all tests with the matlab function, storing results somewhere, compiling the mex function, then reruning tests and comparing. But that doesn't follow normal testing convention where both parts should be run in the same test function.
Edit
Forgot to mention originally but, I do not have ground truth values for many of my test cases so it is not enough for both versions to pass the tests (see my response to Jan's answers for more details). Secondly, the solution should be able to work with packages (where functions are called in a different way than stray functions in matlab's path) since any code I test is written as a package.
Accepted Answer
More Answers (0)
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!