Main Content

matlab.unittest.constraints.HasSize class

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

Test if array has specified size


The matlab.unittest.constraints.HasSize class provides a constraint to test if an array has the specified size.




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


expand all

Expected array size, returned as a row vector of nonnegative integers. Specify the value of this property during creation of the constraint.



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


collapse all

Test for array sizes using the HasSize constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasSize

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that the size of the row vector [1 3 5] is [1 3].

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

Test the size of a 2-by-5-by-3 array. The test passes.

testCase.verifyThat(rand(2,5,3),HasSize([2 5 3]))
Verification passed.

Test if the size of a 2-by-2 matrix is not [2 2]. The test fails.

testCase.verifyThat(eye(2),~HasSize([2 2]))
Verification failed.
    Framework Diagnostic:
    Negated HasSize failed.
    --> The value must not have the size specified.
    Actual Value:
         1     0
         0     1
    Prohibited Size:
         2     2

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

C = {'Mercury','Gemini','Apollo'};
testCase.verifyThat(C,HasSize([1 3]))
Verification passed.

Version History

Introduced in R2013a