Main Content

matlab.unittest.constraints.StructComparator クラス

名前空間: matlab.unittest.constraints

構造体配列の比較演算子

説明

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

作成

説明

c = matlab.unittest.constraints.StructComparator は、空の構造体配列およびフィールドをもたない構造体配列の比較演算子を作成します。この比較演算子は、実際の値と期待される値が同じサイズとフィールドをもつ構造体配列である場合に満たされます。

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

c = matlab.unittest.constraints.StructComparator(___,Name,Value) は、前述の構文の任意の入力引数の組み合わせに加え、1 つ以上の名前と値の引数を使用して追加のオプションを設定します。たとえば、c = matlab.unittest.constraints.StructComparator("Recursively",true) は、構造体配列に格納された値を比較するときに再帰的に演算を行う比較演算子を作成します。

入力引数

すべて展開する

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

例: matlab.unittest.constraints.NumericComparator

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

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

名前と値の引数

引数のオプションのペアを Name1=Value1,...,NameN=ValueN として指定します。ここで Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後になければなりませんが、ペアの順序は重要ではありません。

例: c = matlab.unittest.constraints.StructComparator(Recursively=true)

R2021a より前では、コンマを使用してそれぞれの名前と値を区切り、Name を引用符で囲みます。

例: c = matlab.unittest.constraints.StructComparator("Recursively",true)

比較時に無視するフィールド。string 配列または文字ベクトルの cell 配列として指定します。指定したフィールドの値は比較演算子で比較されません。

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

例: "IgnoringFields","field1"

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

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

import matlab.unittest.constraints.StructComparator
import matlab.unittest.constraints.NumericComparator

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

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

プロパティ

すべて展開する

比較時に無視するフィールド。文字ベクトルの cell 配列として返されます。

このプロパティは名前と値の引数 IgnoringFields によって設定されます。

属性:

GetAccess
public
SetAccess
private

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

このプロパティは名前と値の引数 Recursively によって設定されます。

属性:

GetAccess
public
SetAccess
private

すべて折りたたむ

StructComparator クラスを使用して、データが格納されていない構造体配列を比較します。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.StructComparator

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

testCase = TestCase.forInteractiveUse;

StructComparator インスタンスを使用して、フィールドをもたない 2 つの構造体配列を比較します。サイズが一致しないため、テストは失敗します。

testCase.verifyThat(struct,IsEqualTo([struct struct], ...
    "Using",StructComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> StructComparator failed.
        --> Sizes do not match.
            
            Actual size:
                 1     1
            Expected size:
                 1     2
        
        Actual Value:
          struct with no fields.
        Expected Value:
          1×2 struct array with no fields.
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructureArraysThatContainNoDataExample.m (CompareStructureArraysThatContainNoDataExample) at 18

2 つの空の構造体配列を比較します。実際の構造体配列と期待される構造体配列のフィールドが同じでないため、サイズが等しくてもテストは失敗します。

testCase.verifyThat(struct([]),IsEqualTo(struct("f1",{},"f2",{}), ...
    "Using",StructComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> StructComparator failed.
        --> Structures contain different fields.
            --> Missing fields in Actual:
                    'f1'
                    'f2'
        
        Actual Value:
          0×0 empty struct array with no fields.
        Expected Value:
          0×0 empty struct array with fields:
        
            f1
            f2
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructureArraysThatContainNoDataExample.m (CompareStructureArraysThatContainNoDataExample) at 24

struct([])struct.empty の結果が同じ空の構造体配列になることを検証します。

testCase.verifyThat(struct([]),IsEqualTo(struct.empty, ...
    "Using",StructComparator))
Verification passed.

StructComparator クラスを使用して、データが格納された構造体を比較します。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.StructComparator
import matlab.unittest.constraints.NumericComparator
import matlab.unittest.constraints.StringComparator
import matlab.unittest.constraints.RelativeTolerance

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

testCase = TestCase.forInteractiveUse;

フィールドに数値が格納された 2 つの構造体を作成します。

s1 = struct("ID",10,"score",90);
s2 = struct("score",90,"ID",10);

StructComparator インスタンスを使用して、構造体を比較します。構造体配列にデータが格納されている場合は、適切な比較演算子を StructComparator コンストラクターに渡します。テストはパスします。

testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",StructComparator(NumericComparator)))
Verification passed.

s2 に格納された値の 1 つを変更し、構造体をもう一度比較します。テストは失敗します。

s2.score = 95;
testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",StructComparator(NumericComparator)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.score
        --> NumericComparator failed.
            --> The numeric values are not equal using "isequaln".
            --> Failure table:
                    Actual    Expected    Error       RelativeError   
                    ______    ________    _____    ___________________
                                                                      
                      90         95        -5      -0.0526315789473684
            
            Actual Value:
                90
            Expected Value:
                95
    
    Actual Value:
      struct with fields:
    
           ID: 10
        score: 90
    Expected Value:
      struct with fields:
    
        score: 95
           ID: 10
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructuresThatContainDataExample.m (CompareStructuresThatContainDataExample) at 31

テストにパスするように、対応する値が相対許容誤差 0.1 の範囲内で等価でなければならないものと指定します。

testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",StructComparator(NumericComparator( ...
    "Within",RelativeTolerance(0.1)))))
Verification passed.

比較演算子に再帰的に演算するように指示して、文字列を格納する入れ子にされた構造体を比較します。入れ子にされた構造体が等しくないため、テストは失敗します。

s3 = struct("name",struct("first","Mary","last","Smith"), ...
    "location","Apartment 4");
s4 = struct("name",struct("first","Sam","last","Smith"), ...
    "location","Apartment 4");
testCase.verifyThat(s3,IsEqualTo(s4, ...
    "Using",StructComparator(StringComparator,"Recursively",true)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.name.first
        --> StringComparator failed.
            --> The strings are not equal.
            
            Actual Value:
                "Mary"
            Expected Value:
                "Sam"
    
    Actual Value:
      struct with fields:
    
            name: [1×1 struct]
        location: "Apartment 4"
    Expected Value:
      struct with fields:
    
            name: [1×1 struct]
        location: "Apartment 4"
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructuresThatContainDataExample.m (CompareStructuresThatContainDataExample) at 47

ヒント

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

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

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.StructComparator
    import matlab.unittest.constraints.NumericComparator
    
    testCase = TestCase.forInteractiveUse;
    exp = struct("f1",zeros(1,10),"f2",'a',"f3",{'b','c'});
    act = exp;
    testCase.verifyThat(act,IsEqualTo(exp,"Using",StructComparator(NumericComparator)))
    

バージョン履歴

R2013a で導入