Main Content

matlab.unittest.constraints.LogicalComparator クラス

名前空間: matlab.unittest.constraints

logical 配列の比較演算子

説明

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

作成

説明

c = matlab.unittest.constraints.LogicalComparator は、logical 配列の比較演算子を作成します。この比較演算子は、実際の値と期待される値が同じサイズとスパース性をもつ logical 配列で、それらの対応する要素が等しい場合に満たされます。

すべて折りたたむ

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

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.LogicalComparator

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

testCase = TestCase.forInteractiveUse;

LogicalComparator インスタンスを使用して、実際の値と期待される値を比較します。実際の値と期待される値がどちらも true であるため、テストはパスします。

testCase.verifyThat(true,IsEqualTo(true,"Using",LogicalComparator))
Verification passed.

false の値を true と比較します。テストは失敗します。

testCase.verifyThat(false,IsEqualTo(true,"Using",LogicalComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> LogicalComparator failed.
        --> The logical values are not equal.
        
        Actual Value:
          logical
        
           0
        Expected Value:
          logical
        
           1
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingLogicalComparatorExample.m (CompareValuesUsingLogicalComparatorExample) at 21

[true true]true と比較します。実際の値と期待される値のサイズが同じでないため、テストは失敗します。

testCase.verifyThat([true true],IsEqualTo(true,"Using",LogicalComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> LogicalComparator failed.
        --> Sizes do not match.
            
            Actual size:
                 1     2
            Expected size:
                 1     1
        
        Actual Value:
          1×2 logical array
        
           1   1
        Expected Value:
          logical
        
           1
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingLogicalComparatorExample.m (CompareValuesUsingLogicalComparatorExample) at 25

1true と比較します。実際の値が double 型であるため、テストは失敗します。

testCase.verifyThat(1,IsEqualTo(true,"Using",LogicalComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> LogicalComparator failed.
        --> Classes do not match.
            
            Actual Class:
                double
            Expected Class:
                logical
        
        Actual Value:
             1
        Expected Value:
          logical
        
           1
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingLogicalComparatorExample.m (CompareValuesUsingLogicalComparatorExample) at 29

ヒント

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

    LogicalComparator インスタンスは、IsEqualTo クラスで実行される比較をオーバーライドする必要がある場合に使用します。たとえば、実際の値と期待される値が logical でない場合に比較を失敗とするには、LogicalComparator インスタンスをテストに含めます。LogicalComparator を使用して、cell 配列、構造体、ディクショナリ、table、および MATLAB® オブジェクト配列のパブリック プロパティに格納される値を制限することもできます。次の例では、実際の値と期待される値が数値配列であるため、MATLAB からエラーがスローされます。

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.LogicalComparator
    
    testCase = TestCase.forInteractiveUse;
    exp = magic(5); 
    act = exp;
    testCase.verifyThat(act,IsEqualTo(exp,"Using",LogicalComparator))
    

バージョン履歴

R2013a で導入