Main Content

matlab.unittest.constraints.LogicalComparator Class

Namespace: matlab.unittest.constraints

Comparator for logical arrays

Description

The matlab.unittest.constraints.LogicalComparator class provides a comparator for logical arrays. To use this comparator in your tests, create a LogicalComparator instance, and specify it as the value of the Using name-value argument of the IsEqualTo constraint constructor.

Creation

Description

example

c = matlab.unittest.constraints.LogicalComparator creates a comparator for logical arrays. The comparator is satisfied if the actual and expected values are logical arrays with the same size and sparsity, and their corresponding elements are equal.

Examples

collapse all

Compare actual and expected values using the LogicalComparator class.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Use a LogicalComparator instance to compare the actual and expected values. The test passes because the actual and expected values are both true.

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

Compare the value of false to true. The test fails.

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

Compare [true true] to true. The test fails because the actual and expected values are not the same size.

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

Compare the value 1 to true. The test fails because the actual value is of type 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

Tips

  • In most cases, you are not required to use a LogicalComparator instance. The IsEqualTo class creates a constraint to test for the equality of various data types, including logical arrays.

    Use a LogicalComparator instance when you need to override the comparison performed by the IsEqualTo class. For example, if you want the comparison to fail when actual and expected values are not logical, include a LogicalComparator instance in your test. You also can use LogicalComparator to restrict the values contained in cell arrays, structures, dictionaries, tables, and public properties of MATLAB® object arrays. In this example, MATLAB throws an error because the actual and expected values are numeric arrays.

    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))
    

Version History

Introduced in R2013a