Main Content

matlab.unittest.constraints.HasElementCount クラス

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

配列に指定の数の要素があるかどうかをテスト

説明

matlab.unittest.constraints.HasElementCount クラスは、配列が指定の要素数であるかどうかをテストするための制約を提供します。

作成

説明

c = matlab.unittest.constraints.HasElementCount(count) は、配列が指定の要素数であるかどうかをテストするための制約を作成し、Count プロパティを設定します。この制約は、配列の要素数が count と等しい場合に満たされます。

プロパティ

すべて展開する

期待される要素数。非負の整数スカラーとして返されます。このプロパティの値は、制約の作成時に指定します。

属性:

GetAccess
public
SetAccess
private

データ型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

すべて折りたたむ

HasElementCount 制約を使用して、配列の要素数についてテストします。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasElementCount

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

testCase = TestCase.forInteractiveUse;

スカラーの要素数が 1 であることを検証します。

testCase.verifyThat(3,HasElementCount(1))
Verification passed.

行列 [1 2 3; 4 5 6] の要素数が 3 でないことを検証します。

testCase.verifyThat([1 2 3; 4 5 6],~HasElementCount(3))
Verification passed.

単位行列に期待される要素数があることを検証します。

n = 7;
testCase.verifyThat(eye(n),HasElementCount(n^2))
Verification passed.

文字ベクトルの cell 配列の要素数をテストします。テストはパスします。

C = {'Mercury','Gemini','Apollo'};
testCase.verifyThat(C,HasElementCount(3))
Verification passed.

2 つのフィールドをもつスカラー構造体の要素数をテストします。構造体が要素を 1 つしかもたないため、テストは失敗します。

s.field1 = 1;
s.field2 = zeros(1,10);
testCase.verifyThat(s,HasElementCount(2))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    HasElementCount failed.
    --> The value did not have the correct number of elements.
        
        Actual Number of Elements:
             1
        Expected Number of Elements:
             2
    
    Actual Value:
      struct with fields:
    
        field1: 1
        field2: [0 0 0 0 0 0 0 0 0 0]

バージョン履歴

R2013a で導入