Main Content

matlab.unittest.constraints.HasUniqueElements クラス

名前空間: matlab.unittest.constraints
スーパークラス: matlab.unittest.constraints.BooleanConstraint

セットに含まれる要素が一意であるかどうかをテスト

説明

matlab.unittest.constraints.HasUniqueElements クラスは、セットに含まれる要素が一意であるかどうかをテストするための制約を提供します。

この制約では、関数 unique を使用してセットを解析します。そのため、制約で使用する実際の値は関数 unique でサポートされていなければなりません。

作成

説明

c = matlab.unittest.constraints.HasUniqueElements は、セットに含まれる要素が一意であるかどうかをテストするための制約を作成します。実際のセット actual について、numel(unique(actual))numel(actual) と等しければ制約が満たされます。

すべて折りたたむ

HasUniqueElements 制約を使用してセットをテストします。

最初に、この例で使用するクラスをインポートします。

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

対話型テスト用にテスト ケースを作成します。

testCase = TestCase.forInteractiveUse;

'abc' が一意の文字で構成されることを検証します。

testCase.verifyThat('abc',HasUniqueElements)
Verification passed.

'Mississippi' を実際のセットとして、テストをもう一度実行します。指定した値に繰り返される文字が含まれているため、テストは失敗します。

testCase.verifyThat('Mississippi',HasUniqueElements)
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

数値行列 eye(5) に含まれる要素が一意でないことを検証します。

testCase.verifyThat(eye(5),~HasUniqueElements)
Verification passed.

数値ベクトル abs(-3:3) に含まれる要素が一意であるかどうかをテストします。テストは失敗します。

testCase.verifyThat(abs(-3:3),HasUniqueElements)
Verification 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 Value:
         3     2     1     0     1     2     3

cell 配列 {'a','b','ab','bb'} の要素の一意性をテストします。テストはパスします。

testCase.verifyThat({'a','b','ab','bb'},HasUniqueElements)
Verification passed.

HasUniqueElements 制約を使用して、行が一意である table をテストします。テストはパスします。

T = table([3;3;5],{'A';'C';'E'},logical([1;0;0]));
testCase.verifyThat(T,HasUniqueElements)
Verification passed.

バージョン履歴

R2016a で導入