changeset 26213:ff0eadb417ec

Add BIST tests to sparse.cc (patch #9011). * sparse.cc (Fissparse, Fspalloc): Add BIST tests for functions. * sparse.cc (Fsparse): Add single assert to mark function as tested. Real tests are in test/sparse.tst.
author Rik <rik@octave.org>
date Wed, 12 Dec 2018 17:22:08 -0800
parents 2be1833a93a5
children c0ac6fc191d7
files libinterp/corefcn/sparse.cc
diffstat 1 files changed, 53 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/sparse.cc	Sun May 28 13:42:02 2017 +0200
+++ b/libinterp/corefcn/sparse.cc	Wed Dec 12 17:22:08 2018 -0800
@@ -54,6 +54,27 @@
   return ovl (args(0).issparse ());
 }
 
+/*
+%!assert (issparse (sparse (1)), true)
+%!assert (issparse (1), false)
+%!assert (issparse (sparse (false)), true)
+%!assert (issparse (true), false)
+%!assert (issparse (sparse (single ([1 2]))), true)
+%!assert (issparse (single ([1, 2])), false)
+%!assert (issparse (sparse ([1+i, 2]')), true)
+%!assert (issparse ([1+i, 2]'), false)
+
+%!assert (issparse ([]), false)
+%!assert (issparse (sparse([])), true)
+%!assert (issparse ("test"), false)
+%!assert (issparse (struct ("one", {1})), false)
+%!assert (issparse (cell (1)), false)
+
+## Test input validation
+%!error issparse ()
+%!error issparse (1,2)
+*/
+
 DEFUN (sparse, args, ,
        doc: /* -*- texinfo -*-
 @deftypefn  {} {@var{s} =} sparse (@var{a})
@@ -153,10 +174,10 @@
 
       octave::get_dimensions (args(0), args(1), "sparse", m, n);
 
-      if (m >= 0 && n >= 0)
-        retval = SparseMatrix (m, n);
-      else
+      if (m < 0 || n < 0)
         error ("sparse: dimensions must be non-negative");
+
+      retval = SparseMatrix (m, n);
     }
   else if (nargin >= 3)
     {
@@ -220,6 +241,11 @@
   return retval;
 }
 
+/*
+## Tests for sparse constructor are in test/sparse.tst
+%!assert (1);
+*/
+
 DEFUN (spalloc, args, ,
        doc: /* -*- texinfo -*-
 @deftypefn {} {@var{s} =} spalloc (@var{m}, @var{n}, @var{nz})
@@ -270,8 +296,28 @@
   if (nargin == 3)
     nz = args(2).idx_type_value ();
 
-  if (m >= 0 && n >= 0 && nz >= 0)
-    return ovl (SparseMatrix (dim_vector (m, n), nz));
-  else
-    error ("spalloc: M,N,NZ must be non-negative");
+  if (m < 0 || n < 0 || nz < 0)
+    error ("spalloc: M, N, and NZ must be non-negative");
+
+  return ovl (SparseMatrix (dim_vector (m, n), nz));
 }
+
+/*
+%!assert (issparse (spalloc (1,1)))
+%!assert (spalloc (1,1), sparse (1,1))
+%!test
+%! s = spalloc (1,1,5);
+%! assert (s, sparse (1,1));
+%! assert (nzmax (s), 5);
+%!assert (spalloc (1,2), sparse (1,2))
+%!assert (spalloc (1,2,2), sparse (1,2))
+%!assert (spalloc (2,1), sparse (2,1))
+%!assert (spalloc (2,1,2), sparse (2,1))
+
+%!error spalloc ()
+%!error spalloc (1)
+%!error spalloc (1,2,3,4)
+%!error <M, N, and NZ must be non-negative> spalloc (-1, 1, 1)
+%!error <M, N, and NZ must be non-negative> spalloc (1, -1, 1)
+%!error <M, N, and NZ must be non-negative> spalloc (1, 1, -1)
+*/