Main Content

matlab.unittest.constraints.TableComparator クラス

名前空間: matlab.unittest.constraints

table 配列の比較演算子

説明

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

作成

説明

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

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

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

入力引数

すべて展開する

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

import matlab.unittest.constraints.TableComparator
import matlab.unittest.constraints.NumericComparator

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

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

プロパティ

すべて展開する

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

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

属性:

GetAccess
public
SetAccess
private

すべて折りたたむ

TableComparator クラスを使用して、空の table を比較します。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.TableComparator

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

testCase = TestCase.forInteractiveUse;

TableComparator インスタンスを使用して、2 つの空の table を比較します。tabletable.empty の結果が同じ空の table になることを検証します。

testCase.verifyThat(table,IsEqualTo(table.empty,"Using",TableComparator))
Verification passed.

プロパティ値が異なる 2 つの空の table が等しいかどうかをテストします。テストは失敗します。

T1 = table;
T1.Properties.Description = "First Empty Table";
T2 = table;
T2.Properties.Description = "Second Empty Table";
testCase.verifyThat(T1,IsEqualTo(T2,"Using",TableComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> TableComparator failed.
        --> Table properties do not match.
            Path to failure: <table>.Properties
            --> ObjectComparator failed.
                --> The objects are not equal using "isequaln".
                
                Actual Value:
                  TableProperties with properties:
                
                             Description: 'First Empty Table'
                                UserData: []
                          DimensionNames: {'Row'  'Variables'}
                           VariableNames: {1×0 cell}
                    VariableDescriptions: {}
                           VariableUnits: {}
                      VariableContinuity: []
                                RowNames: {}
                        CustomProperties: No custom properties are set.
                      Use addprop and rmprop to modify CustomProperties.
                Expected Value:
                  TableProperties with properties:
                
                             Description: 'Second Empty Table'
                                UserData: []
                          DimensionNames: {'Row'  'Variables'}
                           VariableNames: {1×0 cell}
                    VariableDescriptions: {}
                           VariableUnits: {}
                      VariableContinuity: []
                                RowNames: {}
                        CustomProperties: No custom properties are set.
                      Use addprop and rmprop to modify CustomProperties.
        
        Actual Value:
          0×0 empty table
        Expected Value:
          0×0 empty table
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareEmptyTablesExample.m (CompareEmptyTablesExample) at 25

TableComparator クラスを使用して、空でない変数が格納された table を比較します。

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

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

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

testCase = TestCase.forInteractiveUse;

変数に数値が格納された 2 つの table を作成します。

LastName = ["Lin";"Jones";"Brown"];
Age = [38;40;49];
Height = [64;67;64];
Weight = [131;133;119];
BloodPressure = [125 83; 117 75; 122 80];
T1 = table(Age,Height,Weight,BloodPressure);
T2 = table(Age,Height,Weight,BloodPressure,'RowNames',LastName);

TableComparator インスタンスを使用して、table を比較します。空でない table を比較するには、適切な比較演算子を TableComparator コンストラクターに渡します。

testCase.verifyThat(T1,IsEqualTo(T2, ...
    "Using",TableComparator(NumericComparator)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> TableComparator failed.
        --> Table properties do not match.
            Path to failure: <table>.Properties
            --> ObjectComparator failed.
                --> The objects are not equal using "isequaln".
                
                Actual Value:
                  TableProperties with properties:
                
                             Description: ''
                                UserData: []
                          DimensionNames: {'Row'  'Variables'}
                           VariableNames: {'Age'  'Height'  'Weight'  'BloodPressure'}
                    VariableDescriptions: {}
                           VariableUnits: {}
                      VariableContinuity: []
                                RowNames: {}
                        CustomProperties: No custom properties are set.
                      Use addprop and rmprop to modify CustomProperties.
                Expected Value:
                  TableProperties with properties:
                
                             Description: ''
                                UserData: []
                          DimensionNames: {'Row'  'Variables'}
                           VariableNames: {'Age'  'Height'  'Weight'  'BloodPressure'}
                    VariableDescriptions: {}
                           VariableUnits: {}
                      VariableContinuity: []
                                RowNames: {3×1 cell}
                        CustomProperties: No custom properties are set.
                      Use addprop and rmprop to modify CustomProperties.
        
        Actual Value:
          3×4 table
        
            Age    Height    Weight    BloodPressure
            ___    ______    ______    _____________
        
            38       64       131       125     83  
            40       67       133       117     75  
            49       64       119       122     80  
        Expected Value:
          3×4 table
        
                     Age    Height    Weight    BloodPressure
                     ___    ______    ______    _____________
        
            Lin      38       64       131       125     83  
            Jones    40       67       133       117     75  
            Brown    49       64       119       122     80  
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareNonemptyTablesExample.m (CompareNonemptyTablesExample) at 30

table 変数に同じ数値データが格納されていますが、RowNames プロパティの値が T1T2 で異なるためテストは失敗します。テストにパスするように、T1RowNames プロパティを設定します。

T1.Properties.RowNames = LastName;
testCase.verifyThat(T1,IsEqualTo(T2, ...
    "Using",TableComparator(NumericComparator)))
Verification passed.

T2 の値の 1 つを変更し、table をもう一度比較します。テストは失敗します。

T2.Age(end) = 50;
testCase.verifyThat(T1,IsEqualTo(T2, ...
    "Using",TableComparator(NumericComparator)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.Age
        --> NumericComparator failed.
            --> The numeric values are not equal using "isequaln".
            --> Failure table:
                    Index    Actual    Expected    Error    RelativeError
                    _____    ______    ________    _____    _____________
                                                                         
                      3        49         50        -1          -0.02    
            
            Actual Value:
                38
                40
                49
            Expected Value:
                38
                40
                50
    
    Actual Value:
      3×4 table
    
                 Age    Height    Weight    BloodPressure
                 ___    ______    ______    _____________
    
        Lin      38       64       131       125     83  
        Jones    40       67       133       117     75  
        Brown    49       64       119       122     80  
    Expected Value:
      3×4 table
    
                 Age    Height    Weight    BloodPressure
                 ___    ______    ______    _____________
    
        Lin      38       64       131       125     83  
        Jones    40       67       133       117     75  
        Brown    50       64       119       122     80  
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareNonemptyTablesExample.m (CompareNonemptyTablesExample) at 43

table 変数の対応する値が絶対許容誤差 1 の範囲内で等価でなければならないものと指定します。テストはパスします。

testCase.verifyThat(T1,IsEqualTo(T2, ...
    "Using",TableComparator( ...
    NumericComparator("Within",AbsoluteTolerance(1)))))
Verification passed.

table を変更して、入れ子にされた table を格納します。変更された table を比較するには、TableComparator に再帰的に演算するように指示します。テストはパスします。

T1.BloodPressure = table([125;117;122],[83;75;80]);
T2 = T1;
testCase.verifyThat(T1,IsEqualTo(T2, ...
    "Using",TableComparator(NumericComparator,"Recursively",true)))
Verification passed.

ヒント

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

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

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.TableComparator
    import matlab.unittest.constraints.NumericComparator
     
    testCase = TestCase.forInteractiveUse;
    exp = table([45;32;34],logical([1;0;0]),'VariableNames',["Age" "Vote"]); 
    act = exp;
    testCase.verifyThat(act,IsEqualTo(exp,"Using",TableComparator(NumericComparator)))
    

バージョン履歴

R2017a で導入