Main Content

matlab.unittest.constraints.CellComparator クラス

名前空間: matlab.unittest.constraints

cell 配列の比較演算子

説明

matlab.unittest.constraints.CellComparator クラスは、cell 配列の比較演算子を提供します。この比較演算子をテストで使用するには、CellComparator インスタンスを作成し、それを IsEqualTo 制約コンストラクターの名前と値の引数 Using の値として指定します。

作成

説明

c = matlab.unittest.constraints.CellComparator は、空の cell 配列の比較演算子を作成します。この比較演算子は、実際の値と期待される値が同じサイズをもつ空の cell 配列である場合に満たされます。

c = matlab.unittest.constraints.CellComparator(comp) は、指定された比較演算子 comp を使用して、cell 配列に格納された値を比較します。この構文を使用する場合、実際の値と期待される値が同じサイズをもつ cell 配列で、cell 配列の対応する要素が comp の比較演算子のいずれかを満たしていれば比較演算子が満たされます。

c = matlab.unittest.constraints.CellComparator(___,"Recursively",tf) は、cell 配列に格納された値を比較するときに再帰的に演算するかどうかも指定します。tftrue の場合、入れ子にされたすべての値の等価性を調べるまで再帰が続行されます。この構文では、前述の構文の入力引数を任意に組み合わせて使用できます。

入力引数

すべて展開する

cell 配列に格納された値の比較に使用する比較演算子。matlab.unittest.constraints 名前空間において比較演算子として分類されるクラスのオブジェクト配列として指定します。

例: matlab.unittest.constraints.NumericComparator

例: matlab.unittest.constraints.StringComparator("IgnoringCase",true)

例: [matlab.unittest.constraints.LogicalComparator matlab.unittest.constraints.NumericComparator]

再帰的に演算するかどうか。数値または logical 0 (false) または 1 (true) として指定します。

値が true の場合、実際の cell 配列と期待される cell 配列の要素も cell 配列にすることができ、それらの要素が比較演算子で再帰的に比較されます。値が false の場合は、実際の cell 配列と期待される cell 配列のすべての要素が comp でサポートされる型でなければなりません。たとえば、次のコードでは、c1c2 のどちらでも数値の cell 配列を比較できます。ただし、要素として cell 配列または数値のいずれかを格納する cell 配列を比較できるのは c2 のみです。

import matlab.unittest.constraints.CellComparator
import matlab.unittest.constraints.NumericComparator

c1 = CellComparator(NumericComparator);
c2 = CellComparator(NumericComparator,"Recursively",true);

この引数は Recursive プロパティを設定します。

プロパティ

すべて展開する

再帰的に演算するかどうか。logical 0 (false) または 1 (true) として返されます。

このプロパティは入力引数 tf によって設定されます。

属性:

GetAccess
public
SetAccess
private

すべて折りたたむ

CellComparator クラスを使用して、実際の値と期待される値を比較します。

最初に、この例で使用するクラスをインポートします。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.CellComparator
import matlab.unittest.constraints.NumericComparator
import matlab.unittest.constraints.AbsoluteTolerance

対話型テスト用にテスト ケースを作成します。

testCase = TestCase.forInteractiveUse;

CellComparator インスタンスを使用して、2 つの空の cell 配列を比較します。サイズが一致しないため、テストは失敗します。

testCase.verifyThat({},IsEqualTo(cell(0,2),"Using",CellComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> CellComparator failed.
        --> Sizes do not match.
            
            Actual size:
                 0     0
            Expected size:
                 0     2
        
        Actual Value:
          0×0 empty cell array
        Expected Value:
          0×2 empty cell array
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingCellComparatorExample.m (CompareValuesUsingCellComparatorExample) at 19

空でない cell 配列を比較するには、適切な比較演算子を CellComparator コンストラクターに渡します。たとえば、数値の cell 配列をそれ自体と比較します。テストはパスします。

testCase.verifyThat({1,2,4},IsEqualTo({1,2,4}, ...
    "Using",CellComparator(NumericComparator)))
Verification passed.

{1,2,3}{1,2,4} と比較します。テストにパスするように、対応する要素が絶対許容誤差 1 の範囲内で等価でなければならないものと指定します。

testCase.verifyThat({1,2,3},IsEqualTo({1,2,4}, ...
    "Using",CellComparator(NumericComparator( ...
    "Within",AbsoluteTolerance(1)))))
Verification passed.

比較演算子に再帰的に演算するように指示して、入れ子にされた数値の cell 配列を比較します。入れ子にされた cell 配列が同じでないため、テストは失敗します。

testCase.verifyThat({1,2,{4,8}},IsEqualTo({1,2,{4,16}}, ...
    "Using",CellComparator(NumericComparator,"Recursively",true)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>{3}{2}
        --> NumericComparator failed.
            --> The numeric values are not equal using "isequaln".
            --> Failure table:
                    Actual    Expected    Error    RelativeError
                    ______    ________    _____    _____________
                                                                
                      8          16        -8          -0.5     
            
            Actual Value:
                 8
            Expected Value:
                16
    
    Actual Value:
      1×3 cell array
    
        {[1]}    {[2]}    {1×2 cell}
    Expected Value:
      1×3 cell array
    
        {[1]}    {[2]}    {1×2 cell}
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingCellComparatorExample.m (CompareValuesUsingCellComparatorExample) at 36

ヒント

  • ほとんどの場合、CellComparator インスタンスを使用する必要はありません。cell 配列を含むさまざまなデータ型の等価性をテストする制約が IsEqualTo クラスで作成されます。

    CellComparator インスタンスは、IsEqualTo クラスで実行される比較をオーバーライドする必要がある場合に使用します。たとえば、cell 配列に数値以外の値が格納されている場合に比較を失敗とするには、CellComparator インスタンスをテストに含めます。次の例では、実際の cell 配列と期待される cell 配列に数値以外の値が格納されているため、MATLAB® からエラーがスローされます。

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.CellComparator
    import matlab.unittest.constraints.NumericComparator
    
    testCase = TestCase.forInteractiveUse;
    exp = {1,2,{3},'abc'}; 
    act = exp;
    testCase.verifyThat(act,IsEqualTo(exp,"Using",CellComparator(NumericComparator)))
    

バージョン履歴

R2013a で導入