创建一个供交互测试的测试用例。
import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.AbsoluteTolerance
import matlab.unittest.constraints.RelativeTolerance
testCase = TestCase.forInteractiveUse;
定义两个包含真空吸尘器的电磁属性的结构体。一个结构体 approxVacuumProps 包含真空吸尘器中光源的渗透率和速度的近似值。
approxVacuumProps.Permeability = 1.2566e-06; % Approximate
approxVacuumProps.Permitivity = 8.854187817*10^-12;
approxVacuumProps.LightSpeed = 2.9979e+08; % Approximate
baselineVacuumProps.Permeability = 4*pi*10^-7;
baselineVacuumProps.Permitivity = 8.854187817*10^-12;
baselineVacuumProps.LightSpeed = 1/sqrt(...
baselineVacuumProps.Permeability*baselineVacuumProps.Permitivity);
测试近似值和基准值之间的相对差是否处于 eps*1e11 内。
testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ...
'Within', RelativeTolerance(eps*1e11)))
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> Path to failure: .Permeability
--> NumericComparator failed.
--> The numeric values are not equal using "isequaln".
--> RelativeTolerance failed.
--> The error was not within relative tolerance.
--> Failure table:
Actual Expected Error RelativeError RelativeTolerance
__________ ____________________ _____________________ _____________________ ____________________
1.2566e-06 1.25663706143592e-06 -3.70614359173257e-11 -2.94925536216295e-05 2.22044604925031e-05
Actual double:
1.256600000000000e-06
Expected double:
1.256637061435917e-06
Actual struct:
Permeability: 1.256600000000000e-06
Permitivity: 8.854187816999999e-12
LightSpeed: 299790000
Expected struct:
Permeability: 1.256637061435917e-06
Permitivity: 8.854187816999999e-12
LightSpeed: 2.997924580105029e+08
测试失败,原因是渗透率的相对差不处于容差范围内。两个值之间的差较小,但数值接近于零,因此该差相对于其大小来说并不足够小,不满足该容差。
构造一个容差对象,以测试是否近似值和基准值之间的绝对差处于 1e-4 范围内。
testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ...
'Within', AbsoluteTolerance(1e-4)))
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> Path to failure: .LightSpeed
--> NumericComparator failed.
--> The numeric values are not equal using "isequaln".
--> AbsoluteTolerance failed.
--> The error was not within absolute tolerance.
--> Failure table:
Actual Expected Error RelativeError AbsoluteTolerance
_________ ________________ _________________ ____________________ _________________
299790000 299792458.010503 -2458.01050287485 -8.1990404935028e-06 0.0001
Actual double:
299790000
Expected double:
2.997924580105029e+08
Actual struct:
Permeability: 1.256600000000000e-06
Permitivity: 8.854187816999999e-12
LightSpeed: 299790000
Expected struct:
Permeability: 1.256637061435917e-06
Permitivity: 8.854187816999999e-12
LightSpeed: 2.997924580105029e+08
测试失败,原因是光速的绝对差不处于容差范围内。两个值之间的差相对于其大小来说较小,但要满足容差又太大。
构造容差对象的逻辑析取,以测试是否近似值和基准值之间的绝对差处于 1e-4 范围内或相对差处于 eps*1e11 范围内。测试使用此容差,这样接近于零的渗透率值满足绝对(下限)容差,较大的光速值满足相对误差。
testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ...
'Within', RelativeTolerance(eps*1e11)| AbsoluteTolerance(1e-4)))
Verification passed.