Main Content

matlab.unittest.constraints.HasNaN 类

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

测试数组是否包含 NaN

描述

matlab.unittest.constraints.HasNaN 类提供一个约束来测试数组是否包含 NaN 值。

创建对象

描述

示例

c = matlab.unittest.constraints.HasNaN 创建一个约束来测试数组是否包含 NaN 值。包含至少一个 NaN 值的数值数组满足该约束。

示例

全部折叠

使用 HasNaN 约束测试数值数组。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasNaN

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

testCase = TestCase.forInteractiveUse;

验证 NaN 满足 HasNaN 约束。

testCase.verifyThat(NaN,HasNaN)
Verification passed.

测试向量 [1 1 2 3 5 8 13] 是否包含 NaN 值。测试失败。

testCase.verifyThat([1 1 2 3 5 8 13],HasNaN)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    HasNaN failed.
    --> At least one element must be NaN.
    
    Actual Value:
         1     1     2     3     5     8    13

测试 [-Inf 5 NaN]。测试通过,因为向量包含 NaN 值。

testCase.verifyThat([-Inf 5 NaN],HasNaN)
Verification passed.

测试虚部为 NaN 的复数是否满足该约束。测试通过。

testCase.verifyThat(3+1i*NaN,HasNaN)
Verification passed.

测试矩阵 [1 NaN; -Inf 3] 是否不包含任何 NaN 值。测试失败。

testCase.verifyThat([1 NaN; -Inf 3],~HasNaN)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Negated HasNaN failed.
    --> All elements must be non-NaN.
        Failing indices:
            3
    
    Actual Value:
         1   NaN
      -Inf     3

版本历史记录

在 R2013a 中推出