Main Content

resetWorkingDirectory

Reset working directory in formal generated C/C++ equivalence tests

Since R2023a

Description

example

resetWorkingDirectory(tester) resets the working directory to enable the test framework to create a new temporary working directory for the next build for the equivalence tester tester. The test framework does not delete working directories until the test case is out of scope.

Input Arguments

expand all

Equivalence tester, specified as a matlabtest.coder.MATLABCoderTester object.

Examples

expand all

This example shows how to generate C code from a MATLAB® function and test for equivalence by using matlabtest.coder.MATLABCoderTester. The tester generates code twice in two different working directories.

The function myAdd takes two numbers as inputs, adds them together, and outputs the result.

function y = myAdd(a,b) %#codegen
y = a+b;
end

This class definition file defines an equivalence test case that inherits from matlab.unittest.TestCase. The test case in the methods block defines a test case that:

  1. Imports the equivalence tester matlabtest.coder.MATLABCoderTester and the constraint matlabtest.constraints.ExecutionMatchesMATLAB

  2. Instantiates the tester for a MEX build target for the entry-point function myAdd with the build-time inputs set to (1,2)

  3. Builds C code from the myAdd function

  4. Executes the C code with the same inputs that were used during the build

  5. Verifies the execution of the C code against the execution of the MATLAB function myAdd

  6. Resets the temporary working directory

  7. Adds additional code generation arguments for the build

  8. Builds C code from the myAdd function

  9. Executes the C code with the same inputs that were used during the build

  10. Verifies the execution of the C code against the execution of the MATLAB function myAdd

classdef tEquivalenceResetWD < matlab.unittest.TestCase
    methods(Test)
        function tMyAdd(testCase)
            import matlabtest.coder.MATLABCoderTester
            import matlabtest.constraints.ExecutionMatchesMATLAB

            tester = MATLABCoderTester.forMEXCoderConfiguration("myAdd",Inputs={1,2});
            build(tester,testCase);
            execute(tester,testCase);
            verifyThat(testCase,tester,ExecutionMatchesMATLAB)

            resetWorkingDirectory(tester);

            tester.CodeGenerationArguments = {'-o','customMEX'};
            build(tester,testCase);
            execute(tester,testCase);
            verifyThat(testCase,tester,ExecutionMatchesMATLAB)
        end
    end
end

Run the tMyAdd test.

runtests("tEquivalence", ...
    procedureName="tMyAdd")
Running tMyMath
..
Done tMyMath
__________


ans = 
  TestResult with properties:

          Name: 'tEquivalence/tMyAdd'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 2.7680
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   2.768 seconds testing time.

Version History

Introduced in R2023a