matlab isa函数,使用函数编写简单测试用例

创建 quadraticSolver.m 函数

此 MATLAB 函数求二次方程的解。在您的 MATLAB 路径上的文件夹中创建此函数。

function roots = quadraticSolver(a, b, c)

% quadraticSolver returns solutions to the

% quadratic equation a*x^2 + b*x + c = 0.

if ~isa(a,'numeric') || ~isa(b,'numeric') || ~isa(c,'numeric')

error('quadraticSolver:InputMustBeNumeric', ...

'Coefficients must be numeric.');

end

roots(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);

roots(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);

end

创建 solverTest 函数

在您的 MATLAB 路径上的文件夹中创建此函数。

function tests = solverTest

tests = functiontests(localfunctions);

end

使用 localfunctions 作为输入调用 functiontests 可从 solverTest.m 文件中的每个局部函数创建一个测试数组。每个测试都是一个局部函数,它遵从在函数名称开头或末尾包含“test”的命名约定。不遵从此约定的局部函数不包括在测试数组中。测试函数必须接受测试框架向其传递函数测试用例对象的单个输入参数。该函数对验证、断言、假设和致命断言使用此对象。它包含一个允许在设置、测试和拆解函数之间传递数据的 TestData 结构体。

为实数解创建测试函数

创建一个测试函数 testRealSolution 来验证 quadraticSolver 是否返回实数解的正确值。例如,方程 x2 - 3x + 2 = 0 有实数解 x = 1 和 x = 2。此函数使用此方程的输入调用 quadraticSolver。预期的解 expSolution 为 [2,1]。

使用验证函数 verifyEqual 将函数的输出 actSolution 与所需的输出 expSolution 进行比较。如果验证失败,该框架继续执行测试。通常,在对浮点值使用 verifyEqual 时,可指定用于比较的容差。有关详细信息,请参阅 matlab.unittest.constraints。

将此函数添加到 solverTest.m 文件中。

function testRealSolution(testCase)

actSolution = quadraticSolver(1,-3,2);

expSolution = [2 1];

verifyEqual(testCase,actSolution,expSolution)

end

为虚数解创建测试函数

创建一个测试以验证 quadraticSolver 是否返回虚数解的正确值。例如,方程 x2 + 2x + 10 = 0 有虚数解 x = -1 + 3i 和 x = -1 - 3i。通常,在对浮点值使用 verifyEqual 时,可指定用于比较的容差。有关详细信息,请参阅 matlab.unittest.constraints。

将此函数 testImaginarySolution 添加到 solverTest.m 文件中。

function testImaginarySolution(testCase)

actSolution = quadraticSolver(1,2,10);

expSolution = [-1+3i -1-3i];

verifyEqual(testCase,actSolution,expSolution)

end

solverTest.m 文件中测试的顺序无关紧要,因为它们是完全独立的测试用例。

保存 solverTest 函数

下面是完整的 solverTest.m 测试文件。将此文件保存在您的 MATLAB 路径上的一个文件夹中。

function tests = solverTest

tests = functiontests(localfunctions);

end

function testRealSolution(testCase)

actSolution = quadraticSolver(1,-3,2);

expSolution = [2 1];

verifyEqual(testCase,actSolution,expSolution)

end

function testImaginarySolution(testCase)

actSolution = quadraticSolver(1,2,10);

expSolution = [-1+3i -1-3i];

verifyEqual(testCase,actSolution,expSolution)

end

在 solverTest 函数中运行测试

运行测试。

results = runtests('solverTest.m')

Running solverTest

..

Done solverTest

__________

results =

1x2 TestResult array with properties:

Name

Passed

Failed

Incomplete

Duration

Totals:

2 Passed, 0 Failed, 0 Incomplete.

0.19172 seconds testing time.

两个测试均通过。

在 quadraticSolver.m 中引入错误并运行测试

通过将 quadraticSolver.m 中的 roots 强制设置为实数来使其中一个测试失败。在结束函数之前,添加以下行:roots = real(roots);。(请不要更改 solverTest.m。)保存文件并运行测试。

results = runtests('solverTest.m')

Running solverTest

.

================================================================================

Verification failed in solverTest/testImaginarySolution.

---------------------

Framework Diagnostic:

---------------------

verifyEqual failed.

--> Complexity does not match.

Actual Complexity:

Real

Expected Complexity:

Complex

Actual Value:

-1 -1

Expected Value:

-1.000000000000000 + 3.000000000000000i -1.000000000000000 - 3.000000000000000i

------------------

Stack Information:

------------------

In C:\work\solverTest.m (testImaginarySolution) at 14

================================================================================

.

Done solverTest

__________

Failure Summary:

Name Failed Incomplete Reason(s)

===============================================================================

solverTest/testImaginarySolution X Failed by verification.

results =

1x2 TestResult array with properties:

Name

Passed

Failed

Incomplete

Duration

Totals:

1 Passed, 1 Failed, 0 Incomplete.

0.043751 seconds testing time.

虚数测试验证失败。

将 quadraticSolver.m 恢复为其以前的状态,通过删除 roots = real(roots); 代码更正版本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值