Main Content

matlab.unittest.constraints.HasSize 类

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

测试数组是否具有指定的大小

描述

matlab.unittest.constraints.HasSize 类提供一个约束来测试数组是否具有指定的大小。

创建对象

描述

示例

c = matlab.unittest.constraints.HasSize(size) 创建一个约束来测试数组是否具有指定的大小并设置 Size 属性。如果数组大小等于 size,则满足该约束。

属性

全部展开

预期的数组大小,以非负整数组成的行向量形式返回。在创建约束的过程中指定此属性的值。

属性:

GetAccess
public
SetAccess
private

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

示例

全部折叠

使用 HasSize 约束测试数组大小。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasSize

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

testCase = TestCase.forInteractiveUse;

验证行向量 [1 3 5] 的大小是否为 [1 3]

testCase.verifyThat([1 3 5],HasSize([1 3]))
Verification passed.

测试一个 2×5×3 数组的大小。测试通过。

testCase.verifyThat(rand(2,5,3),HasSize([2 5 3]))
Verification passed.

测试一个 2×2 矩阵的大小是否不为 [2 2]。测试失败。

testCase.verifyThat(eye(2),~HasSize([2 2]))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Negated HasSize failed.
    --> The value must not have the size specified.
    
    Actual Value:
         1     0
         0     1
    Prohibited Size:
         2     2

测试一个字符向量元胞数组的大小。测试通过。

C = {'Mercury','Gemini','Apollo'};
testCase.verifyThat(C,HasSize([1 3]))
Verification passed.

版本历史记录

在 R2013a 中推出