Main Content

matlab.unittest.constraints.IsScalar 类

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

测试值是否为标量

描述

matlab.unittest.constraints.IsScalar 类提供一个约束来测试值是否为标量。

创建对象

描述

示例

c = matlab.unittest.constraints.IsScalar 创建一个约束来测试值是否为标量。1×1 数组满足该约束。

示例

全部折叠

使用 IsScalar 约束测试值。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsScalar

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

testCase = TestCase.forInteractiveUse;

验证值 1 是否满足 IsScalar 约束。

testCase.verifyThat(1,IsScalar)
Verification passed.

测试向量 [1 1 2 3 5 8 13] 是否满足该约束。测试失败。

testCase.verifyThat([1 1 2 3 5 8 13],IsScalar)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsScalar failed.
    --> The value must be a scalar.
    --> The value has a size of [1  7].
    
    Actual Value:
         1     1     2     3     5     8    13

测试值 "Hello, World!"。测试通过,因为该值是字符串标量。

testCase.verifyThat("Hello, World!",IsScalar)
Verification passed.

测试空结构体。测试失败,因为该值不是 1×1 数组。

testCase.verifyThat(struct([]),IsScalar)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsScalar failed.
    --> The value must be a scalar.
    --> The value has a size of [0  0].
    
    Actual Value:
      0×0 empty struct array with no fields.

版本历史记录

在 R2014b 中推出