Main Content

ローカル関数を使用したスクリプトベースのテストの記述

この例では、ローカル関数を補助関数として使用するスクリプトベースのテストを記述する方法を示します。サンプル関数は、角度の正弦と余弦を近似します。スクリプトベースのテストでは、ローカル関数を使用して許容誤差内での等価性を確認することにより近似をチェックします。

テストする関数 approxSinCos の作成

現在の MATLAB フォルダーのファイル approxSinCos.m にこの関数を作成します。この関数は、ラジアン単位の角度を受け入れ、テイラー級数を使用して角度の正弦と余弦を近似します。

function [sinA,cosA] = approxSinCos(x)
% For a given angle in radians, approximate the sine and cosine of the angle
% using Taylor series.
sinA = x;
cosA = 1;
altSign = -1;
for n = 3:2:26
sinA = sinA + altSign*(x^n)/factorial(n);
cosA = cosA + altSign*(x^(n-1))/factorial(n-1);
altSign = -altSign;
end

テスト スクリプトの作成

現在の MATLAB フォルダーで、新しいスクリプト approxSinCosTest.m を作成します。

メモ: スクリプトに関数を含めるには、MATLAB® R2016b 以降が必要です。

%% Test 0rad
% Test expected values of 0
[sinApprox,cosApprox] = approxSinCos(0);
assertWithAbsTol(sinApprox,0)
assertWithRelTol(cosApprox,1)

%% Test 2pi
% Test expected values of 2pi
[sinApprox,cosApprox] = approxSinCos(2*pi);
assertWithAbsTol(sinApprox,0)
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

各ユニット テストは、assert を使用して関数 approxSinCos の異なる出力をチェックします。浮動小数点数を比較するときは、通常、比較の許容誤差を指定します。ローカル関数 assertWithAbsTol および assertWithRelTol は、指定した絶対許容誤差または相対許容誤差の範囲内で実際の値と期待値が等しいかどうかを計算する補助関数です。

  • Test 0rad は、0 ラジアンの角度の計算値と期待値が、絶対許容誤差 1e-6 または相対許容誤差 0.1% の範囲内かどうかをテストします。通常、0 に近い値を比較する場合は絶対許容誤差を使用します。

  • Test 2pi は、$2\pi$ ラジアンの角度の計算値と期待値が、絶対許容誤差 1e-6 または相対許容誤差 0.1% の範囲内で等しいかどうかをテストします。

  • Test pi over 4 equality は、$pi/4$ の正弦と余弦が相対許容誤差 0.1% の範囲内で等しいかどうかをテストします。

  • Test matches MATLAB fcn は、$2pi/3$ の計算された正弦と余弦が、相対許容誤差 0.1% の範囲内で関数 sin および cos の値と等しいかどうかをテストします。

テストの実行

関数 runtests を実行して approxSinCosTest.m の 4 つのテストを実行します。関数 runtests は、各テストを個別に実行します。1 つのテストが失敗しても、MATLAB は引き続き残りのテストを実行します。runtests を使用せずに approxSinCosTest をスクリプトとして実行した場合、MATLAB はアサーション失敗を検出するとスクリプト全体の実行を中止します。また、関数 runtests を使用してテストを実行すると、MATLAB は参考になるテスト診断を提供します。

results = runtests('approxSinCosTest');
Running approxSinCosTest
....
Done approxSinCosTest
__________

すべてのテストがパスします。

テスト結果の表を作成します。

rt = table(results)
rt =

  4x6 table

                      Name                       Passed    Failed    Incomplete    Duration      Details   
    _________________________________________    ______    ______    __________    ________    ____________

    {'approxSinCosTest/Test0rad'            }    true      false       false        0.22676    {1x1 struct}
    {'approxSinCosTest/Test2pi'             }    true      false       false       0.016352    {1x1 struct}
    {'approxSinCosTest/TestPiOver4Equality' }    true      false       false       0.017292    {1x1 struct}
    {'approxSinCosTest/TestMatchesMATLABFcn'}    true      false       false       0.036842    {1x1 struct}

参考

|

関連する例

詳細