Main Content

matlab.unittest.constraints.IsSameSetAs Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Test if set has same elements as another set

Description

The matlab.unittest.constraints.IsSameSetAs class provides a constraint to test if a set is the same as another set. The sets can have the same elements in different orders, different numbers, or different shapes.

The constraint uses the ismember function to compare sets. So, the actual and expected values you use with the constraint must be supported by the ismember function.

Creation

Description

c = matlab.unittest.constraints.IsSameSetAs(expected) creates a constraint to test if a set has the same elements as the expected set. For an actual set actual, the constraint is satisfied if ismember(actual,expected) and ismember(expected,actual) both return arrays that contain all true values and at least one of these conditions is met:

  • actual and expected are of the same class.

  • isobject(actual) returns true.

  • isobject(expected) returns true.

example

c = matlab.unittest.constraints.IsSameSetAs(expected,RespectingCount=tf) also specifies whether to respect the element count. If tf is true, the constraint is not satisfied if an element occurs a different number of times in the actual and expected sets. (since R2024a)

example

Input Arguments

expand all

Expected set, specified as a value of any data type supported by the ismember function.

This argument sets the ExpectedSet property.

Example: {'a','b','c'}

Example: [1 1 2 3 5 8]

Since R2024a

Option to respect the element count when comparing the actual and expected sets, specified as a numeric or logical 0 (false) or 1 (true). If the value is true, the constraint is not satisfied if the number of times that an element occurs in the actual set differs from the number of times that the element occurs in expected. By default, the constraint ignores the number of times that the elements occur.

This argument sets the RespectCount property.

Properties

expand all

Expected set, returned as a value of any data type supported by the ismember function. Specify the value of this property during creation of the constraint.

This property is set by the expected input argument.

Attributes:

GetAccess
public
SetAccess
private

Since R2024a

Option to respect the element count when comparing the actual and expected sets, returned as a 0 or 1 of data type logical. By default, the constraint ignores the number of times that the elements occur.

This property is set by the tf input argument.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Compare sets using the IsSameSetAs constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsSameSetAs

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that {'a','b','c'} is the same set as {'c','a','b'}. Even though the order of elements in the actual and expected sets differs, the test passes because the sets contain the same elements.

testCase.verifyThat({'a','b','c'},IsSameSetAs({'c','a','b'}))
Verification passed.

Test again with {'a','b','d'} as the expected set. The test fails because the sets contain different elements.

testCase.verifyThat({'a','b','c'},IsSameSetAs({'a','b','d'}))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameSetAs failed.
    --> The actual value contains 1 element(s) not found in the expected set:
        --> Element at index 3:
                {'c'}
    --> The actual value is missing 1 element(s) found in the expected set:
        --> Element at index 3:
                {'d'}
    
    Actual Value:
      1×3 cell array
    
        {'a'}    {'b'}    {'c'}
    Expected Set:
      1×3 cell array
    
        {'a'}    {'b'}    {'d'}

Verify that {'a','b','c'} is not the same set as {'a'}.

testCase.verifyThat({'a','b','c'},~IsSameSetAs({'a'}))
Verification passed.

Compare the numeric vectors [1 2 3] and [3; 2; 1] using the IsSameSetAs constraint. Even though the order of the elements and the shape of the vectors are different, the test passes because the sets contain the same elements.

testCase.verifyThat([1 2 3],IsSameSetAs([3; 2; 1]))
Verification passed.

Test if [1 2 3] is the same set as [1 2 3 1 2]. The test passes because the sets have the same elements. By default, the constraint ignores the number of times that the elements occur when comparing the actual and expected sets.

testCase.verifyThat([1 2 3],IsSameSetAs([1 2 3 1 2]))
Verification passed.

Compare the actual and expected sets more strictly by respecting the element count. The test fails because the elements 1 and 2 occur different numbers of times in the actual and expected sets.

testCase.verifyThat([1 2 3],IsSameSetAs([1 2 3 1 2], ...
    RespectingCount=true))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameSetAs failed.
    --> The actual value contains at least one element with a different count in the expected set.
        --> Actual value at index 1 does not respect the element count.
             Value:
                 1
             Actual Count: 1
             Expected Count: 2
    
    Actual Value:
         1     2     3
    Expected Set:
         1     2     3     1     2

Version History

Introduced in R2018a

expand all