changeset 30609:09012191bb0c

pinv: Improve input validation (Bug #61767). * pinv.cc (Fpinv): Early exit with error() for non-numeric input. Additional tests for input validation * inv.cc (Finv): Additional BIST tests for integer inputs.
author Antonius Burgers <arburgers@gmail.com>
date Thu, 06 Jan 2022 10:38:22 +0100
parents 22ecbc9551a5
children 2240baa6c73c
files libinterp/corefcn/inv.cc libinterp/corefcn/pinv.cc
diffstat 2 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/inv.cc	Fri Jan 07 11:50:43 2022 +0100
+++ b/libinterp/corefcn/inv.cc	Thu Jan 06 10:38:22 2022 +0100
@@ -238,6 +238,12 @@
 %! assert (xinv, single ([-2, 1; 1.5, -0.5]), 5*eps ("single"));
 %! assert (isa (rcond, "single"));
 
+## Basic test for integer inputs
+%!assert (inv (int32 (2)), 0.5)
+%!assert (inv (uint32 (2)), 0.5)
+%!assert (inv (int64 (2)), 0.5)
+%!assert (inv (uint64 (2)), 0.5)
+
 ## Normal scalar cases
 %!assert (inv (2), 0.5)
 %!test
@@ -380,6 +386,7 @@
 %!error <Invalid call> inv ([1, 2; 3, 4], 2)
 %!error <wrong type argument> inv ("Hello World")
 %!error <wrong type argument> inv ({1})
+%!error <wrong type argument> inv (true)
 %!error <must be a square matrix> inv ([1, 2; 3, 4; 5, 6])
 %!error <inverse of the null matrix not defined> inv (sparse (2, 2, 0))
 %!error <inverse of the null matrix not defined> inv (diag ([0, 0]))
--- a/libinterp/corefcn/pinv.cc	Fri Jan 07 11:50:43 2022 +0100
+++ b/libinterp/corefcn/pinv.cc	Thu Jan 06 10:38:22 2022 +0100
@@ -64,6 +64,9 @@
 
   octave_value arg = args(0);
 
+  if (! arg.isnumeric ())
+    err_wrong_type_arg ("pinv", arg);
+
   if (arg.isempty ())
     return ovl (Matrix ());
 
@@ -197,6 +200,12 @@
 %! y = pinv (x, 2);
 %! assert (diag (y), [1/3 1/2 0 0 0]');
 
+## Basic test for integer inputs
+%!assert (pinv (int32 (2)), 0.5)
+%!assert (pinv (uint32 (2)), 0.5)
+%!assert (pinv (int64 (2)), 0.5)
+%!assert (pinv (uint64 (2)), 0.5)
+
 ## Test special case of 0 scalars and vectors
 %!assert (pinv (0), 0)
 %!assert (pinv ([0, 0, 0]), [0; 0; 0])
@@ -206,6 +215,12 @@
 %!assert (pinv (complex ([0,0,0], [0,0,0])), [0; 0; 0])
 %!assert (pinv (complex (single (0),0)), single (0))
 %!assert (pinv (complex (single ([0,0,0]), [0,0,0])), single ([0; 0; 0]))
+
+## Test input validation
+%!error <wrong type argument> pinv ("Hello World")
+%!error <wrong type argument> pinv ({1})
+%!error <wrong type argument> pinv (true)
+
 */
 
 OCTAVE_NAMESPACE_END