Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# matlab.unittest.constraints.IsEqualTo class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

General constraint to compare for equality

## Description

The IsEqualTo class creates a constraint that compares data for equality. The type of comparison used is governed by the type of the expected value. The first check performed is to check whether the expected value is an object. This check is performed first because in this case it is possible for the object to have overridden methods that are used in subsequent checks (e.g. islogical). The following list categorizes and describes the various tests.

Data TypeEquality Comparison Method
MATLAB® & Java® Objects

If the expected value is a MATLAB or Java object, the IsEqualTo constraint calls the isequal method on the expected value object. If isequal returns false and a supported tolerance has been specified, the actual and expected values are checked for equivalent class, size, and sparsity. If these checks fail, the constraint is not satisfied. If these checks pass, the tolerance is used for comparison.

Logicals

If the expected value is a logical, the actual and expected values are checked for equivalent sparsity. If the sparsity matches, the values are compared using the isequal method. Otherwise, the constraint is not satisfied.

Numerics

If the expected value is numeric, the actual and expected values are checked for equivalent class, size, and sparsity. If the all these checks match, isequaln is used. If isequaln returns true, the constraint is satisfied. If the complexity does not match or isequaln returns false, and a supported tolerance has been supplied, it is used to perform the comparison. Otherwise, the constraint is not satisfied.

Strings

If the expected value is a string, the strcmp function is used to check the actual and expected values for equality unless the IgnoreCase property is true, in which case the strings are compared using strcmpi. If the IgnoreWhitespace is true, all whitespace characters are removed from the actual and expected strings before passing them to strcmp or strcmpi.

Structures

If the expected value is a struct, the field count of the actual and expected values is compared. If not equal, the constraint is not satisfied. Otherwise, each field of the expected value struct must exist on the actual value struct. If any field names are different, the constraint is not satisfied. The fields are then recursively compared in a depth first examination. The recursion continues until a fundamental data type is encountered (i.e. logical, numeric, string, or object), and the values are compared as described above.

Cell Arrays

If the expected value is a cell array, the actual and expected values are checked for size equality. If they are not equal in size, the constraint is not satisfied. Otherwise, each element of the array is recursively compared in a manner identical to fields in a structure, described above.

## Construction

IsEqualTo(expVal) provides a general constraint to compare for equality.

IsEqualTo(expVal,Name,Value) provides a constraint with additional options specified by one or more Name,Value pair arguments. Name must appear inside single quotes (''). You can specify several name-value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

### Input Arguments

 expVal The expected value that will be compared to the actual value.

#### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

 'IgnoringCase' Indicator whether the constraint is insensitive to case, specified as false or true (logical 0 or 1) Default: false 'IgnoringWhitespace' Indicator whether the constraint is insensitive to whitespace, specified as false or true (logical 0 or 1) Default: false 'Using' Particular comparator to use for constraint construction, specified as a matlab.unittest.constraints.Comparator object Default: (empty) 'Within' Tolerance to use in constraint construction, specified as a matlab.unittest.constraints.Tolerance object Default: (empty)

## Properties

 Comparator Specific comparator used in construction of the constraint, specified as a matlab.unittest.constraints.Comparator object in the name-value pair argument, 'Using'. Expected The expected value that will be compared to the actual value specified in the expVal input argument. IgnoreCase Indicator whether the constraint is insensitive to case, specified in the name-value pair argument, 'IgnoringCase'. This property applies at all levels of recursion, such as nested structures. IgnoreWhitespace Indicator whether the constraint is insensitive to whitespace, specified in the name-value pair argument, 'IgnoringWhitespace'. This property applies at all levels of recursion, such as nested structures. Tolerance Specific tolerance used in construction of the constraint, specified as a matlab.unittest.constraints.Tolerance object in the name-value pair argument, 'Within'. This property applies at all levels of recursion, such as nested structures.

## Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB documentation.

## Examples

expand all

### Instantiate IsEqualTo Object

```import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.AbsoluteTolerance;

% Create a TestCase for interactive use
testCase = matlab.unittest.TestCase;

%%% Simple equality check %%%
testCase.verifyThat(5, IsEqualTo(5));

%%% Comparison using tolerance %%%
testCase.verifyThat(5, IsEqualTo(4.95, 'Within', AbsoluteTolerance(0.1)));

%%% Comparison ignoring case %%%
testCase.verifyThat('aBc', IsEqualTo('ABC', 'IgnoringCase', true));

%%% Comparison ignoring whitespace %%%
testCase.verifyThat('a bc', IsEqualTo('ab c', 'IgnoringWhitespace', true));```