changeset 31278:076c8f94d7f4

Allow empty input for dimension vector (bug #63181). * libinterp/corefcn/utils.cc (get_dimensions): Allow empty input (additionally to row or column vector input). Change comment to describe actual behavior. * libinterp/corefcn/data.cc: Adjust test code and description.
author Markus Mützel <markus.muetzel@gmx.de>
date Sun, 09 Oct 2022 13:21:29 +0200
parents 185799b2a566
children 025b37e4a17d
files libinterp/corefcn/data.cc libinterp/corefcn/utils.cc
diffstat 2 files changed, 7 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc	Sat Oct 08 13:14:02 2022 -0400
+++ b/libinterp/corefcn/data.cc	Sun Oct 09 13:21:29 2022 +0200
@@ -4653,7 +4653,8 @@
 ## Tests for bug #47298
 ## Matlab requires the size to be a row vector.  In that logic, it supports
 ## n to be a 1x0 vector (returns 0x0) but not a 0x1 vector.  Octave supports
-## any vector and therefore must support 0x1, 1x0, and 0x0x1 (but not 0x1x1).
+## row and column vectors and therefore must support 0x1, 1x0, and 0x1x1.
+## Also any empty input results in a 0x0 output.
 %!test <*47298>
 %! fcns = {@zeros, @ones, @inf, @nan, @NA, @i, @pi, @e};
 %! for idx = 1:numel (fcns)
@@ -4661,8 +4662,8 @@
 %!   assert (fcn (zeros (1, 0)), zeros (0, 0));
 %!   assert (fcn (zeros (0, 1)), zeros (0, 0));
 %!   assert (fcn (zeros (0, 1, 1)), zeros (0, 0));
-%!   fail ([func2str(fcn) " ([])"]);
-%!   fail ([func2str(fcn) " (zeros (0, 0, 1))"]);
+%!   assert (fcn (zeros ([])), zeros (0, 0));
+%!   assert (fcn (zeros (0, 0, 1)), zeros (0, 0));
 %! endfor
 */
 
--- a/libinterp/corefcn/utils.cc	Sat Oct 08 13:14:02 2022 -0400
+++ b/libinterp/corefcn/utils.cc	Sun Oct 09 13:21:29 2022 +0200
@@ -1339,10 +1339,9 @@
   void get_dimensions (const octave_value& a, const char *warn_for,
                        dim_vector& dim)
   {
-    // We support dimensions to be specified by any vector, even if it's a
-    // vector of dimensions 0x1, 1x0, 1x1x0, or 1x1x6.  If the vector ends
-    // up being empty, the final dimensions end up being 0x0.
-    if (! a.dims ().isvector ())
+    // We support dimensions to be specified by a vector, even if it's empty.
+    // If the vector is empty, the final dimensions end up being 0x0.
+    if (! a.dims ().isvector () && a.dims ().numel () != 0)
       error ("%s (A): use %s (size (A)) instead", warn_for, warn_for);
 
     const Array<octave_idx_type> v = a.octave_idx_type_vector_value (true);