Main Content

matlab.unittest.constraints.HasUniqueElements class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Constraint specifying set contains unique elements

Construction

HasUniqueElements creates a constraint specifying that a set contains unique elements. The constraint produces a qualification failure for any actual value set that does not contain unique elements. If numel(unique(actualSet)) is equal to numel(actualSet), the constraint considers the actual value set to have unique elements.

Copy Semantics

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

Examples

collapse all

Create a test case for interactive testing.

import matlab.unittest.TestCase;
import matlab.unittest.constraints.HasUniqueElements;

testCase = TestCase.forInteractiveUse;

Test 'abc' and 'Mississippi' for unique elements.

testCase.verifyThat('abc', HasUniqueElements);
Interactive verification passed.
testCase.verifyThat('Mississippi', HasUniqueElements);
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
HasUniqueElements failed.
--> The value contains 3 nonunique element(s):
    --> Nonunique element found at indices [2 5 8 11]:
            i
    --> Nonunique element found at indices [9 10]:
            p
    --> Nonunique element found at indices [3 4 6 7]:
            s

Actual char:
    Mississippi

Assert that a set of doubles has unique elements.

testCase.assertThat(magic(6), HasUniqueElements);
Interactive assertion passed.
testCase.assertThat(abs(-3:3), HasUniqueElements);
Interactive assertion failed.

---------------------
Framework Diagnostic:
---------------------
HasUniqueElements failed.
--> The value contains 3 nonunique element(s):
    --> Nonunique element found at indices [3 5]:
                 1
    --> Nonunique element found at indices [2 6]:
                 2
    --> Nonunique element found at indices [1 7]:
                 3

Actual double:
         3     2     1     0     1     2     3
Assertion failed.

Verify that a cell array has unique elements.

testCase.verifyThat({'abc','123';'abc','345'},HasUniqueElements);
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
HasUniqueElements failed.
--> The value contains 1 nonunique element(s):
    --> Nonunique element found at indices [1 2]:
                'abc'

Actual cell:
        'abc'    '123'
        'abc'    '345'

Assert that a table has unique rows.

t = table([3;3;5],{'A';'C';'E'},logical([1;0;0]));
testCase.assertThat(t,HasUniqueElements);
Interactive assertion passed.
Introduced in R2016a