Main Content

matlab.unittest.constraints.AnyElementOf Class

Namespace: matlab.unittest.constraints

Test if any element of array satisfies constraint

Description

The matlab.unittest.constraints.AnyElementOf class provides a proxy of the actual value, so you can test if at least one element of an array satisfies a given constraint. When you include the proxy in your test, the testing framework examines the constraint on an element-by-element basis.

You can use an instance of this class in tests performed with the qualification methods assertThat, assumeThat, fatalAssertThat, or verifyThat. The class does not modify the supplied actual value. It serves only as a wrapper to perform the constraint analysis.

Creation

Description

example

p = matlab.unittest.constraints.AnyElementOf(actualValue) creates a proxy to test if any element of the specified array satisfies a constraint and sets the ActualValue property. When you use this proxy to test against a constraint, the test passes if at least one element of actualValue satisfies the constraint.

Properties

expand all

Actual value to test against the constraint, returned as a value of any data type. Specify the value of this property during creation of the proxy. The class of the actual value must support array indexing and be compatible with the constraint.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Test if at least one element of an array satisfies a constraint by using the AnyElementOf class.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.AnyElementOf
import matlab.unittest.constraints.IsFinite
import matlab.unittest.constraints.IsLessThan
import matlab.unittest.constraints.IsReal
import matlab.unittest.constraints.Matches

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that at least one element of the vector [NaN Inf 5] is finite.

testCase.verifyThat(AnyElementOf([NaN Inf 5]),IsFinite)
Verification passed.

Test if at least one element of the matrix [1 5; 0 6] is negative. The test fails because all the elements are greater than or equal to zero.

testCase.verifyThat(AnyElementOf([1 5; 0 6]),IsLessThan(0))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    All elements failed. The first element failed because:
    --> IsLessThan failed.
        --> The value must be less than the maximum value.
        
        Actual Value:
             1
        Maximum Value (Exclusive):
             0
    
    Actual Value Array:
         1     5
         0     6

Verify that at least one element of the vector [0 4i] is complex.

testCase.verifyThat(AnyElementOf([0 4i]),~IsReal)
Verification passed.

Test if at least one element of the vector ["Coffee" "Tea" "Water"] matches "tea" regardless of case. The test passes.

testCase.verifyThat(AnyElementOf(["Coffee" "Tea" "Water"]), ...
    Matches("tea","IgnoringCase",true))
Verification passed.

Tips

  • You can use AnyElementOf to check if any element of an array satisfies a constraint. However, there are some constraints, such as HasInf and HasNaN, that natively validate if any of the elements satisfy a condition. In these situations, the use of AnyElementOf is unnecessary and degrades qualification performance.

Version History

Introduced in R2013a