__assert_equal__
- : assert_equal
(observed, expected)¶ - : assert_equal
(observed, expected, tol)¶ - : assert_equal
(observed, expected, "AbsoluteTolerance", atol)¶ - : assert_equal
(observed, expected, "RelativeTolerance", rtol)¶ - : assert_equal
(observed, expected, "AbsoluteTolerance", atol, "RelativeTolerance", rtol)¶ -
Produce an error if observed is not the same as expected.
assert_equal (observed, expected)tests for the two input arguments being equal with respect to their class, size, and value. All nexus values are supported as input for observed and expected and compared for equality according to the following rules.- Numeric inputs that support exceptional values (i.e., Inf, -Inf, NaN, NA) are treated under the additional assumption that they are equal. They are also tested for sparsity and complexity.
- Logical inputs are additionally tested for sparsity.
- Character arrays are compared with the
strcmpfunction. - Structures are additionally checked against having the same unordered fieldnames.
- Cell arrays are recursively tested on an element by element basis.
- Function handles are tested with the
isequalfunction. - Classdef objects are explicitly tested if and only if they have an
overloaded
eqmethod and unlessall (eq (observed, expected), "all")is true, an error is produced. The handling of missing values is strictly defined by the overloadedeqmethod.
assert_equal (observed, expected, tol)further specifies a tolerance value, which is explicitly used for numerical inputs. Specifying tol as 0, is equivalent to callingassert_equalwith only two input arguments. Any nonzero tolerance is also ignored when observed and expected are non-numeric. Logical and character arrays are not considered to be numeric.If tol is positive then it is an absolute tolerance which will produce an error if
abs (observed - expected) > abs (tol).If tol is negative then it is a relative tolerance which will produce an error if
abs (observed - expected) > abs (tol * expected).If expected is zero tol will always be interpreted as an absolute tolerance.
If tol is not scalar its dimensions must agree with those of observed and expected and tests are performed on an element-by-element basis.
assert_equalalso accepts name-value pair arguments to specify absolute and relative tolerances independently, matching MATLAB’smatlab.unittestassertEqual:"AbsoluteTolerance"— non-negative scalar specifying the absolute tolerance (atol). Default is 0."RelativeTolerance"— non-negative scalar specifying the relative tolerance (rtol). Default is 1e-6.
When both tolerances are supplied the combined threshold is
abs (atol) + abs (rtol * expected). If only"AbsoluteTolerance"is given it applies alone (rtol = 0). If only"RelativeTolerance"is given it applies alone (atol = 0). Tolerance name-value pairs are ignored for non-numeric types.Positional tol and name-value tolerances are combined: if tol is positive it sets
AbsoluteTolerance, if negative it setsRelativeTolerance; name-value pairs always override.