changeset 29356:65c57984a65e

compatibility with undocumented Matlab behavior for exist function (bug #59950) * variables.cc (Fexist): If one argument, return 0 for any empty builtin object. If two arguments, return 0 if second argument is any empty builtin object. * test/bug-59950.tst: New test file. * test/module.mk: Update.
author John W. Eaton <jwe@octave.org>
date Tue, 09 Feb 2021 12:18:31 -0500
parents 2a4998b97990
children 7854d5752dd2
files libinterp/corefcn/variables.cc test/bug-59950.tst test/module.mk
diffstat 3 files changed, 61 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/variables.cc	Sun Feb 07 11:16:54 2021 +0100
+++ b/libinterp/corefcn/variables.cc	Tue Feb 09 12:18:31 2021 -0500
@@ -469,11 +469,26 @@
   if (nargin < 1 || nargin > 2)
     print_usage ();
 
+  // For compatibility with undocumented Matlab behavior, return 0 if
+  // there is an empty built-in object as the only argument.
+
+  if (nargin == 1 && args(0).builtin_type () != btyp_unknown
+      && args(0).isempty ())
+    return ovl (0);
+
+  // Also for compatibility, return 0 if the second argument is an empty
+  // built-in object.
+
+  if (nargin == 2 && args(1).builtin_type () != btyp_unknown
+      && args(1).isempty ())
+    return ovl (0);
+
   std::string name = args(0).xstring_value ("exist: NAME must be a string");
 
   if (nargin == 2)
     {
-      std::string type = args(1).xstring_value ("exist: TYPE must be a string");
+      std::string type
+        = args(1).xstring_value ("exist: TYPE must be a string");
 
       if (type == "class")
         warning (R"(exist: "class" type argument is not implemented)");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-59950.tst	Tue Feb 09 12:18:31 2021 -0500
@@ -0,0 +1,44 @@
+########################################################################
+##
+## Copyright (C) 2021 The Octave Project Developers
+##
+## See the file COPYRIGHT.md in the top-level directory of this
+## distribution or <https://octave.org/copyright/>.
+##
+## This file is part of Octave.
+##
+## Octave is free software: you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <https://www.gnu.org/licenses/>.
+##
+########################################################################
+
+## The following tests are for undocumented Matlab behavior.
+
+%!shared x
+%! x = 13;
+%!assert <59950>  (exist (''), 0)
+%!assert <59950>  (exist ([]), 0)
+%!assert <59950>  (exist ({}), 0)
+%!assert <59950>  (exist (1:0), 0)
+%!error  exist (containers.Map ())
+%!assert <59950>  (exist ('x', ''), 0)
+%!assert <59950>  (exist ('x', []), 0)
+%!assert <59950>  (exist ('x', {}), 0)
+%!assert <59950>  (exist ('x', struct ([])), 0)
+%!error exist ('x', containers.Map ())
+%!assert <59950> (exist (containers.Map (), ''), 0)
+%!assert <59950> (exist (containers.Map (), []), 0)
+%!assert <59950> (exist (containers.Map (), {}), 0)
+%!assert <59950> (exist (containers.Map (), struct ([])), 0)
+%!error exist (containers.Map (), containers.Map ())
--- a/test/module.mk	Sun Feb 07 11:16:54 2021 +0100
+++ b/test/module.mk	Tue Feb 09 12:18:31 2021 -0500
@@ -19,6 +19,7 @@
   %reldir%/bug-55308.tst \
   %reldir%/bug-55321.tst \
   %reldir%/bug-55322.tst \
+  %reldir%/bug-59950.tst \
   %reldir%/colormaps.tst \
   %reldir%/command.tst \
   %reldir%/complex.tst \