Main Content

matlab.unittest.constraints.IsSparse Class

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

Test if array is sparse

Description

The matlab.unittest.constraints.IsSparse class provides a constraint to test if an array is sparse.

Creation

Description

c = matlab.unittest.constraints.IsSparse creates a constraint to test if an array is sparse. The constraint is satisfied if the storage class of the array is sparse.

example

Examples

collapse all

Test numeric arrays using the IsSparse constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsSparse

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Test if an identity matrix is sparse. The test fails.

I = eye(7);
testCase.verifyThat(I,IsSparse)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSparse failed.
    --> The value must be sparse.
    
    Actual Value:
         1     0     0     0     0     0     0
         0     1     0     0     0     0     0
         0     0     1     0     0     0     0
         0     0     0     1     0     0     0
         0     0     0     0     1     0     0
         0     0     0     0     0     1     0
         0     0     0     0     0     0     1

Convert I to a sparse matrix and test again. The test passes.

S = sparse(I);
testCase.verifyThat(S,IsSparse)
Verification passed.

Version History

Introduced in R2013a