Main Content

matlab.unittest.constraints.HasElementCount Class

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

Test if array has specified number of elements

Description

The matlab.unittest.constraints.HasElementCount class provides a constraint to test if an array has the specified element count.

Creation

Description

c = matlab.unittest.constraints.HasElementCount(count) creates a constraint to test if an array has the specified element count and sets the Count property. The constraint is satisfied if the number of array elements is equal to count.

example

Properties

expand all

Expected element count, 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 numbers of array elements using the HasElementCount constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that a scalar has an element count of one.

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

Verify that the matrix [1 2 3; 4 5 6] does not have three elements.

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

Verify that an identity matrix has the expected number of elements.

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

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

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

Test the element count of a scalar structure with two fields. The test fails because the structure has only one element.

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]

Version History

Introduced in R2013a