Main Content

matlab.unittest.constraints.IsSubsetOf Class

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

Test if set is subset of specified set

Description

The matlab.unittest.constraints.IsSubsetOf class provides a constraint to test if a set is a subset of another set.

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.IsSubsetOf(superset) creates a constraint to test if a set is a subset of the specified superset and sets the Superset property. For an actual set actual, the constraint is satisfied if ismember(actual,superset) returns an array that contains all true values and at least one of these conditions is met:

  • actual and superset are of the same class.

  • isobject(actual) returns true.

  • isobject(superset) returns true.

example

Properties

expand all

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

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Test for subsets using the IsSubsetOf constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsSubsetOf

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Test if {'a';'b'} is a subset of {'c','a','b'}. The test passes because the elements of the actual set appear in the expected superset and the two sets are of the same class.

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

Test again with {'a';'d'} as the actual set. The test fails because an element of the actual set does not appear in the expected superset.

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

Verify that the numeric vector [1 2 3] is not a subset of the numeric matrix eye(3).

testCase.verifyThat([1 2 3],~IsSubsetOf(eye(3)))
Verification passed.

Test if the numeric vector single(1:3) is a subset of the numeric vector 0:5. The test fails because the actual and expected values have different types.

testCase.verifyThat(single(1:3),IsSubsetOf(0:5))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSubsetOf failed.
    --> Classes do not match.
        
        Actual Class:
            single
        Expected Class:
            double
    
    Actual Value:
      1×3 single row vector
    
         1     2     3
    Expected Superset:
         0     1     2     3     4     5

Compare two tables using the IsSubsetOf constraint. This test passes because the rows of the actual table appear in the expected table.

actual = table([3,1]',{'C';'A'},logical([0;1]));
expected = table([1:2:5]',{'A';'C';'E'},logical([1;0;0]));
testCase.verifyThat(actual,IsSubsetOf(expected))
Verification passed.

Version History

Introduced in R2016a