matlab.unittest.qualifications.Verifiable.verifyNumElements - Verify value has specified element count - MATLAB (original) (raw)

Class: matlab.unittest.qualifications.Verifiable
Namespace: matlab.unittest.qualifications

Verify value has specified element count

Syntax

Description

verifyNumElements([testCase](#bt00q8g-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fe9741768-f85e-4ab8-85ee-2bcd78a9b1ac),[expectedNumElements](#mw%5F40c4f5e5-ec8c-494a-82a5-1cc49570e853)) verifies that actual is a MATLABĀ® array with the expected number of elements.

example

verifyNumElements([testCase](#bt00q8g-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fe9741768-f85e-4ab8-85ee-2bcd78a9b1ac),[expectedNumElements](#mw%5F40c4f5e5-ec8c-494a-82a5-1cc49570e853),[diagnostic](#mw%5Fa85b70ee-58d4-429b-8191-7d514608b02d)) also associates the diagnostic information in diagnostic with the qualification.

example

Input Arguments

expand all

Test case, specified as a matlab.unittest.qualifications.Verifiable object. Because the matlab.unittest.TestCase class subclasses matlab.unittest.qualifications.Verifiable and inherits its methods, testCase is typically amatlab.unittest.TestCase object.

Value to test, specified as an array of any data type.

Expected number of elements in the array, specified as a nonnegative integer scalar.

Diagnostic information to display when the qualification passes or fails, specified as a string array, character array, function handle, or array of matlab.automation.diagnostics.Diagnostic objects.

Depending on the test runner configuration, the testing framework can display diagnostics when the qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics.

Example: "My Custom Diagnostic"

Example: @dir

Examples

expand all

Test if the actual value has the specified number of elements.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that a scalar has an element count of one.

verifyNumElements(testCase,3,1)

Test the element count of a matrix [1 2 3; 4 5 6]. The test fails because the matrix has six elements.

verifyNumElements(testCase,[1 2 3; 4 5 6],5)

Verification failed. --------------------- Framework Diagnostic: --------------------- verifyNumElements failed. --> The value did not have the correct number of elements.

    Actual Number of Elements:
         6
    Expected Number of Elements:
         5

Actual Value:
     1     2     3
     4     5     6
------------------
Stack Information:
------------------
In C:\work\TestForNumberOfElementsExample.m (TestForNumberOfElementsExample) at 18

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

n = 7; verifyNumElements(testCase,eye(n),n^2)

Test the element count of a cell array of character vectors.

actual = {'Mercury','Gemini','Apollo'}; verifyNumElements(testCase,actual,3)

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); verifyNumElements(testCase,s,2,"Value must have two elements.")

Verification failed. ---------------- Test Diagnostic: ---------------- Value must have two elements. --------------------- Framework Diagnostic: --------------------- verifyNumElements 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]
------------------
Stack Information:
------------------
In C:\work\TestForNumberOfElementsExample.m (TestForNumberOfElementsExample) at 35

Tips

Version History

Introduced in R2013a