# HG changeset patch # User John W. Eaton # Date 1612891111 18000 # Node ID 65c57984a65e52a7b0ebd42d7dd0dd98d849d427 # Parent 2a4998b97990b4777afcd8005a644be61083522c 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. diff -r 2a4998b97990 -r 65c57984a65e libinterp/corefcn/variables.cc --- 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)"); diff -r 2a4998b97990 -r 65c57984a65e test/bug-59950.tst --- /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 . +## +## 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 +## . +## +######################################################################## + +## 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 ()) diff -r 2a4998b97990 -r 65c57984a65e test/module.mk --- 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 \