matlab.unittest.constraints.ObjectComparator class

Package: matlab.unittest.constraints

Comparator for MATLAB or Java objects

Construction

ObjectComparator creates a comparator for MATLAB® or Java® objects. The comparator is satisfied if isequaln returns true. However, if the class of the expected value defines an isequal method, whether visible or hidden, but not an isequaln method, the ObjectComparator uses that method for comparison instead of isequaln.

ObjectComparator('Within',tolObj) creates a comparator using a specified tolerance. ObjectComparator first checks that a call to isequaln or isequal returns true. If the check fails, the ObjectComparator checks for equivalent class, size, and sparsity of the actual and expected values. If these checks pass, ObjectComparator delegates comparison to the supplied tolerance, tolObj. The value of this tolerance must be of the same class as the actual and expected values.

Input Arguments

tolObj

Tolerance instance

Properties

Tolerance

Specific tolerance used in construction of the comparator, specified as a matlab.unittest.constraints.Tolerance object in the tolObj input argument

Copy Semantics

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

Examples

collapse all

In a file, MyInt.m, in your working folder, create a subclass of int8.

classdef MyInt < int8
    methods
        function i = MyInt(value)
            i@int8(value);
        end
    end
end

At the command prompt, create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.ObjectComparator
import matlab.unittest.constraints.IsEqualTo

testCase = TestCase.forInteractiveUse;

Use an ObjectComparator to test that two instances of MyInt are equal to each other.

testCase.verifyThat(MyInt(10), ...
    IsEqualTo(MyInt(10), 'Using', ObjectComparator))
Interactive verification passed.

Test the equality of two instances of MyInt that are constructed with different input values.

testCase.verifyThat(MyInt(11), ...
    IsEqualTo(MyInt(10), 'Using', ObjectComparator))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> ObjectComparator failed.
    --> The objects are not equal using "isequal".

Actual Object:
      MyInt:
    
      int8 data:
       11
Expected Object:
      MyInt:
    
      int8 data:
       10

One instance of MyInt has a value of 11, and the other has a value of 10.

Repeat the test and specify that values must be equal within an absolute tolerance of 1.

import matlab.unittest.constraints.AbsoluteTolerance

testCase.verifyThat(MyInt(11), IsEqualTo(MyInt(10), ...
    'Using', ObjectComparator('Within', AbsoluteTolerance(MyInt(1)))))
Interactive verification passed.
Introduced in R2013a