Main Content

matlab.unittest.constraints.HasLength Class

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

Test if array has specified length

Description

The matlab.unittest.constraints.HasLength class provides a constraint to test if an array has the specified length. The length of an array is defined as the length of the largest dimension of that array.

Creation

Description

c = matlab.unittest.constraints.HasLength(length) creates a constraint to test if an array has the specified length and sets the Length property. The constraint is satisfied if the array length is equal to length.

example

Properties

expand all

Expected array length, returned as a nonnegative integer scalar. Specify the value of this property during creation of the constraint.

Attributes:

GetAccess
public
SetAccess
private

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Examples

collapse all

Test for array lengths using the HasLength constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasLength

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that the row vector [1 3 5] has a length of three.

testCase.verifyThat([1 3 5],HasLength(3))
Verification passed.

Verify that the length of an array is the length of its largest dimension.

testCase.verifyThat(ones(2,5,3),HasLength(5))
Verification passed.

Test if the length of a 2-by-2 identity matrix is not two. The test fails.

testCase.verifyThat(eye(2),~HasLength(2))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Negated HasLength failed.
    --> The array must not have the specified length.
    
    Actual Value:
         1     0
         0     1
    Prohibited Length:
         2

Test for the length of a cell array of character vectors. The test passes.

C = {'Mercury','Gemini','Apollo'; 'Skylab','Skylab B','ISS'};
testCase.verifyThat(C,HasLength(3))
Verification passed.

Version History

Introduced in R2013a