MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Create Test Function for Real Solutions


Create a test function, testRealSolution, to verify that quadraticSolver returns the
correct value for real solutions. For example, the equation x^2 - 3x + 2 = 0 has real
solutions x = 1 and x = 2. This function calls quadraticSolver with the inputs of this
equation. The expected solution, expSolution, is [2,1].


Use the qualification function, verifyEqual, to compare the output of the function,
actSolution, to the desired output, expSolution. If the qualification fails, the
framework continues executing the test. Typically, when using verifyEqual on floating
point values, you specify a tolerance for the comparison. For more information, see
matlab.unittest.constraints.


Add this function to the solverTest.m file.


function testRealSolution(testCase)
actSolution = quadraticSolver(1,-3,2);
expSolution = [2 1];
verifyEqual(testCase,actSolution,expSolution)
end


Create Test Function for Imaginary Solutions


Create a test to verify that quadraticSolver returns the right value for imaginary
solutions. For example, the equation x^2 + 2x + 10 = 0 has imaginary solutions x = -1



  • 3i and x = -1 - 3i. Typically, when using verifyEqual on floating point values,
    you specify a tolerance for the comparison. For more information, see
    matlab.unittest.constraints.


Add this function, testImaginarySolution, to the solverTest.m file.


function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);
expSolution = [-1+3i -1-3i];
verifyEqual(testCase,actSolution,expSolution)
end


The order of the tests within the solverTest.m file does not matter because they are
fully independent test cases.


Save solverTest Function


The following is the complete solverTest.m test file. Save this file in a folder on your
MATLAB path.


Write Simple Test Case Using Functions
Free download pdf