changeset 19157:d171e23cac81

setxor.m: Fix bug with empty set input and multiple outputs (bug #43140). * scripts/set/setxor.m: Fix a bug that occurs when one of the input sets is empty and more than one output argument is required (bug #43140). Add regression tests.
author Julien Bect <julien.bect@supelec.fr>
date Thu, 04 Sep 2014 09:51:29 +0200
parents 38b9849cd907
children 9c28728a3041
files scripts/set/setxor.m
diffstat 1 files changed, 33 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/set/setxor.m	Tue Sep 23 20:55:42 2014 -0700
+++ b/scripts/set/setxor.m	Thu Sep 04 09:51:29 2014 +0200
@@ -1,3 +1,4 @@
+## Copyright (C) 2014 Julien Bect
 ## Copyright (C) 2008-2013 Jaroslav Hajek
 ## Copyright (C) 2000, 2006-2007 Paul Kienzle
 ##
@@ -94,11 +95,11 @@
         c = c.';
       endif
     endif
-  endif
 
-  if (nargout > 1)
-    ia = ia(i(i <= na));
-    ib = ib(i(i > na) - na);
+    if (nargout > 1)
+      ia = ia(i(i <= na));
+      ib = ib(i(i > na) - na);
+    endif
   endif
 
 endfunction
@@ -121,6 +122,34 @@
 %! assert (c, [1 1; 1 3; 2 10]);
 %! assert (c, sortrows ([a(ia,:); b(ib,:)]));
 
+%!assert (setxor (1, []), 1)
+%!assert (setxor ([], 1), 1)
+
+%!test
+%! [c, ia, ib] = setxor (1, []);
+%! assert (c, 1);
+%! assert (ia, 1);
+%! assert (isempty (ib));
+
+%!test
+%! [c, ia, ib] = setxor ([], 1);
+%! assert (c, 1);
+%! assert (isempty (ia));
+%! assert (ib, 1);
+
+%!test
+%! a = [2 1; 4 3];  b = [];
+%! [c, ia, ib] = setxor (a, b);
+%! assert (c, [1; 2; 3; 4]);
+%! assert (ia, [3; 1; 4; 2]);
+%! assert (isempty (ib));
+
+%!test
+%! a = [];  b = [2 1; 4 3];
+%! [c, ia, ib] = setxor (a, b);
+%! assert (c, [1; 2; 3; 4]);
+%! assert (isempty (ia));
+%! assert (ib, [3; 1; 4; 2]);
 ## Test orientation of output
 %!shared x,y
 %! x = 1:3;