matlab.unittest.constraints.EveryElementOf class

Package: matlab.unittest.constraints

Test if all elements of array meet constraint

Description

The EveryElementOf class creates a proxy of the actual value to the framework. The proxy enables a test writer to apply a constraint against each element of an array, which ensures that a passing result occurs when every element of the array that satisfies the constraint.

It is intended that you use this class through matlab.unittest qualifications as shown in the examples. The class does not modify the provided actual value, but serves as a wrapper to perform the constraint analysis. The testing framework analyzes the constraint on an element-by-element basis.

Construction

EveryElementOf(actVal) creates a proxy instance that tests if every element of a provided array, actVal, meets a constraint. The test passes if all elements satisfy the constraint.

Tips

  • EveryElementOf checks if every element in the provided array satisfies an associated constraint. However, there are some constraints, such as IsEqualTo and IsGreaterThan, IsLessThan, that natively validate if all elements in the array satisfy a condition. In these situations, use of EveryElementOf is unnecessary and impedes qualification performance.

Input Arguments

actVal

Actual value to test against constraint

Properties

ActualValue

Actual value to test against constraint. Set this property through the constructor via the actVal input argument.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.EveryElementOf

testCase = TestCase.forInteractiveUse;

Test that every element of actVal is less than 55.

import matlab.unittest.constraints.IsLessThan
actVal = [1 1 2 3 5 8 13 21 34];
testCase.verifyThat(EveryElementOf(actVal), IsLessThan(55))
Interactive verification passed.

Test that every element of the actual value array is complex.

import matlab.unittest.constraints.IsReal
testCase.verifyThat(EveryElementOf([1+2i 4i]), ~IsReal)
Interactive verification passed.

Test that every element of the actual value array is less than zero.

import matlab.unittest.constraints.IsLessThan
testCase.verifyThat(EveryElementOf([1 -5]), IsLessThan(0))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
At least one element failed.

Failing indices:
         1
The first failing 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

Only the second element is less than zero.

Test that every element of the actual value array has a NaN value.

import matlab.unittest.constraints.HasNaN
testCase.verifyThat(EveryElementOf([NaN 0/0 5]), HasNaN)
Interactive 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

Only the third element has a NaN value.