Main Content

matlab.unittest.constraints.LogicalComparator 类

命名空间: matlab.unittest.constraints

逻辑数组的比较器

描述

matlab.unittest.constraints.LogicalComparator 类为逻辑数组提供比较器。要在测试中使用此比较器,请创建一个 LogicalComparator 实例,并将其指定为 IsEqualTo 约束构造函数的 Using 名称-值参量的值。

创建对象

描述

示例

c = matlab.unittest.constraints.LogicalComparator 为逻辑数组创建一个比较器。如果实际值和预期值是具有相同大小和稀疏性的逻辑数组,并且它们对应的元素相等,则满足该比较器。

示例

全部折叠

使用 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 实例。IsEqualTo 类创建一个约束来测试各种数据类型的相等性,包括逻辑数组。

    当需要覆盖由 IsEqualTo 类执行的比较时,请使用 LogicalComparator 实例。例如,如果希望在实际值和预期值是非逻辑值时比较失败,请在测试中包含 LogicalComparator 实例。您还可以使用 LogicalComparator 来限制元胞数组、结构体、字典、表和 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 中推出