Main Content

matlab.unittest.constraints.EveryElementOf class

Package: matlab.unittest.constraints

Test if every element of array satisfies constraint

Description

The matlab.unittest.constraints.EveryElementOf class provides a proxy of the actual value, so you can test if every 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.EveryElementOf(actualValue) creates a proxy to test if every 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 every 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 all elements of an array satisfy a constraint by using the EveryElementOf class.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.EveryElementOf
import matlab.unittest.constraints.ContainsSubstring
import matlab.unittest.constraints.HasNaN
import matlab.unittest.constraints.IsReal

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that every element of the vector ["Rain" "Main" "Plain"] contains the substring "ain".

testCase.verifyThat(EveryElementOf(["Rain" "Main" "Plain"]), ...
    ContainsSubstring("ain"))
Verification passed.

Test if every element of the vector [NaN 0/0 5] is a NaN value. The test fails because the third element is not a NaN value.

testCase.verifyThat(EveryElementOf([NaN 0/0 5]),HasNaN)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    At least one element failed.
    
    Failing indices:
        3
    The first failing element failed because:
    --> HasNaN failed.
        --> The value must be NaN.
        
        Actual Value:
             5
    
    Actual Value Array:
       NaN   NaN     5

Test if every element of the vector [0 4i] is complex. The test fails because the first element is a real value.

testCase.verifyThat(EveryElementOf([0 4i]),~IsReal)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    At least one element failed.
    
    Failing indices:
        1
    The first failing element failed because:
    --> Negated IsReal failed.
        --> The value must not be real.
        
        Actual Value:
             0
    
    Actual Value Array:
      0.000000000000000 + 0.000000000000000i  0.000000000000000 + 4.000000000000000i

Tips

  • You can use EveryElementOf to check if every element of an array satisfies a constraint. However, there are some constraints, such as IsEqualTo, IsGreaterThan, and IsLessThan, that natively validate if every element satisfies a condition. In these situations, the use of EveryElementOf is unnecessary and degrades qualification performance.

Version History

Introduced in R2013a