assertWithRelTol(cosApprox,1)
%% Test pi over 4 equality
% Test sine and cosine of pi/4 are equal
[sinApprox,cosApprox] = approxSinCos(pi/4);
assertWithRelTol(sinApprox,cosApprox,'sine and cosine should be equal')
%% Test matches MATLAB fcn
% Test values of 2pi/3 match MATLAB output for the sin and cos functions
x = 2*pi/3;
[sinApprox,cosApprox] = approxSinCos(x);
assertWithRelTol(sinApprox,sin(x),'sin does not match')
assertWithRelTol(cosApprox,cos(x),'cos does not match')
function assertWithAbsTol(actVal,expVal,varargin)
% Helper function to assert equality within an absolute tolerance.
% Takes two values and an optional message and compares
% them within an absolute tolerance of 1e-6.
tol = 1e-6;
tf = abs(actVal-expVal) <= tol;
assert(tf, varargin{:});
end
function assertWithRelTol(actVal,expVal,varargin)
% Helper function to assert equality within a relative tolerance.
% Takes two values and an optional message and compares
% them within a relative tolerance of 0.1%.
relTol = 0.001;
tf = abs(expVal - actVal) <= relTol.*abs(expVal);
assert(tf, varargin{:});
end
Each unit test uses assert to check different output of the approxSinCos function.
Typically, when you compare floating-point values, you specify a tolerance for the
comparison. The local functions assertWithAbsTol and assertWithRelTol are helper
functions to compute whether the actual and expected values are equal within the
specified absolute or relative tolerance.
- Test 0rad tests whether the computed and expected values for an angle of 0 radians
are within an absolute tolerance of 1e-6 or a relative tolerance 0.1%. Typically, you
use absolute tolerance to compare values close to 0.
33 Unit Testing