Main Content

matlab.unittest.constraints.HasElementCount 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint

测试数组是否具有指定数量的元素

描述

matlab.unittest.constraints.HasElementCount 类提供一个约束来测试数组是否具有指定的元素计数。

创建对象

描述

示例

c = matlab.unittest.constraints.HasElementCount(count) 创建一个约束来测试数组是否具有指定的元素计数并设置 Count 属性。如果数组元素的数量等于 count,则满足该约束。

属性

全部展开

预期的元素计数,以非负整数标量形式返回。在创建约束的过程中指定此属性的值。

属性:

GetAccess
public
SetAccess
private

数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

示例

全部折叠

使用 HasElementCount 约束测试数组元素的数量。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasElementCount

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

验证标量的元素数是否为一个。

testCase.verifyThat(3,HasElementCount(1))
Verification passed.

验证矩阵 [1 2 3; 4 5 6] 并非包含三个元素。

testCase.verifyThat([1 2 3; 4 5 6],~HasElementCount(3))
Verification passed.

验证单位矩阵是否具有预期的元素数。

n = 7;
testCase.verifyThat(eye(n),HasElementCount(n^2))
Verification passed.

测试字符向量元胞数组的元素数目。测试通过。

C = {'Mercury','Gemini','Apollo'};
testCase.verifyThat(C,HasElementCount(3))
Verification passed.

测试一个具有两个字段的标量结构体的元素计数。测试失败,因为该结构体只有一个元素。

s.field1 = 1;
s.field2 = zeros(1,10);
testCase.verifyThat(s,HasElementCount(2))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    HasElementCount failed.
    --> The value did not have the correct number of elements.
        
        Actual Number of Elements:
             1
        Expected Number of Elements:
             2
    
    Actual Value:
      struct with fields:
    
        field1: 1
        field2: [0 0 0 0 0 0 0 0 0 0]

版本历史记录

在 R2013a 中推出