Main Content

matlab.unittest.constraints.IsSubsetOf Class

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

Test if set is subset of specified set


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.




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.


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.




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.

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.

Verification failed.
    Framework Diagnostic:
    IsSubsetOf failed.
    --> The actual value contains 1 element(s) not found in the expected superset:
        --> Element at index 2:
    Actual Value:
      2×1 cell array
    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.

Verification failed.
    Framework Diagnostic:
    IsSubsetOf failed.
    --> Classes do not match.
        Actual Class:
        Expected Class:
    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]));
Verification passed.

Version History

Introduced in R2016a