changeset 29190:7f11d59e3af8 stable

Fix incorrect results for set functions with "legacy" option (bug #59708). * intersect.m: Don't transpose output in "legacy" mode if by_rows is true. Add BIST test for "legacy" and "rows" options present at same time. * setdiff.m: In "legacy" BIST test, also verify setdiff() result when "legacy" is not present. * setxor.m: Don't transpose output in "legacy" mode if by_rows is true. Add 4 BIST tests for "legacy" output orientation for vectors. Add BIST test for "legacy" and "rows" options present at same time. * union.m: Add 4 BIST tests for "legacy" output orientation for vectors. Add BIST test for "legacy" and "rows" options present at same time.
author Rik <rik@octave.org>
date Thu, 17 Dec 2020 14:02:18 -0800
parents 0fc400f15e35
children ae5d758c10e1
files scripts/set/intersect.m scripts/set/setdiff.m scripts/set/setxor.m scripts/set/union.m
diffstat 4 files changed, 53 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/set/intersect.m	Thu Dec 17 07:23:49 2020 +0100
+++ b/scripts/set/intersect.m	Thu Dec 17 14:02:18 2020 -0800
@@ -134,11 +134,11 @@
       ## FIXME: Is there a way to avoid a call to sort?
       c = c(sort (ic(match)));
     endif
-  endif
 
-  ## Adjust output orientation for Matlab compatibility
-  if (isrowvec)
-    c = c.';
+    ## Adjust output orientation for Matlab compatibility
+    if (isrowvec)
+      c = c.';
+    endif
   endif
 
   if (nargout > 1)
@@ -150,7 +150,7 @@
       [~, idx] = min (ib);
       ib = [ib(idx:end); ib(1:idx-1)];
     endif
-    if (optlegacy && isrowvec)
+    if (optlegacy && isrowvec && ! by_rows)
       ia = ia.';
       ib = ib.';
     endif
@@ -252,6 +252,18 @@
 %! assert (ia, [5, 4]);
 %! assert (ib, [4, 1]);
 
+%!test  # "legacy" + "rows"
+%! A = [ 1 2; 3 4; 5 6; 3 4; 7 8 ];
+%! B = [ 3 4; 7 8; 9 10 ];
+%! [c, ia, ib] = intersect (A, B, "rows");
+%! assert (c, [3, 4; 7, 8]);
+%! assert (ia, [2; 5]);
+%! assert (ib, [1; 2]);
+%! [c, ia, ib] = intersect (A, B, "rows", "legacy");
+%! assert (c, [3, 4; 7, 8]);
+%! assert (ia, [4; 5]);
+%! assert (ib, [1; 2]);
+
 ## Test orientation of output
 %!shared a,b
 %! a = 1:4;
--- a/scripts/set/setdiff.m	Thu Dec 17 07:23:49 2020 +0100
+++ b/scripts/set/setdiff.m	Thu Dec 17 14:02:18 2020 -0800
@@ -191,6 +191,9 @@
 %!test
 %! a = [7 9 7; 0 0 0; 7 9 7; 5 5 5; 1 4 5];
 %! b = [0 0 0; 5 5 5];
+%! [c, ia] = setdiff (a, b, "rows");
+%! assert (c, [1, 4 ,5; 7, 9 7]);
+%! assert (ia, [5; 1]);
 %! [c, ia] = setdiff (a, b, "rows", "legacy");
 %! assert (c, [1, 4 ,5; 7, 9 7]);
 %! assert (ia, [5; 3]);
--- a/scripts/set/setxor.m	Thu Dec 17 07:23:49 2020 +0100
+++ b/scripts/set/setxor.m	Thu Dec 17 14:02:18 2020 -0800
@@ -140,7 +140,7 @@
     if (nargout > 1)
       ia = ia(i(i <= na));
       ib = ib(i(i > na) - na);
-      if (optlegacy && isrowvec)
+      if (optlegacy && isrowvec && ! by_rows)
         ia = ia(:).';
         ib = ib(:).';
       endif
@@ -233,6 +233,10 @@
 %!assert (size (setxor (x', y)), [3 1])
 %!assert (size (setxor (x, y')), [3 1])
 %!assert (size (setxor (x', y')), [3 1])
+%!assert (size (setxor (x, y, "legacy")), [1, 3])
+%!assert (size (setxor (x', y, "legacy")), [1, 3])
+%!assert (size (setxor (x, y', "legacy")), [1, 3])
+%!assert (size (setxor (x', y', "legacy")), [3, 1])
 
 ## Test "legacy" input
 %!test
@@ -246,3 +250,15 @@
 %! assert (c, [2, 3, 4, 5]);
 %! assert (ia, [5, 1]);
 %! assert (ib, [4, 1]);
+
+%!test  # "legacy" + "rows"
+%! A = [1 2; 3 4; 5 6; 3 4; 7 8];
+%! B = [3 4; 7 8; 9 10];
+%! [c, ia, ib] = setxor (A, B, "rows");
+%! assert (c, [1, 2; 5, 6; 9, 10]);
+%! assert (ia, [1; 3]);
+%! assert (ib, [3]);
+%! [c, ia, ib] = setxor (A, B, "rows", "legacy");
+%! assert (c, [1, 2; 5, 6; 9, 10]);
+%! assert (ia, [1; 3]);
+%! assert (ib, [3]);
--- a/scripts/set/union.m	Thu Dec 17 07:23:49 2020 +0100
+++ b/scripts/set/union.m	Thu Dec 17 14:02:18 2020 -0800
@@ -167,6 +167,18 @@
 %! assert (ia, [1, 2]);
 %! assert (ib, [3, 1]);
 
+%!test  # "legacy" + "rows"
+%! A = [1 2; 3 4; 5 6; 3 4; 7 8];
+%! B = [3 4; 7 8; 9 10];
+%! [c, ia, ib] = union (A, B, "rows");
+%! assert (c, [1, 2; 3, 4; 5, 6; 7, 8; 9, 10]);
+%! assert (ia, [1; 2; 3; 5]);
+%! assert (ib, [3]);
+%! [c, ia, ib] = union (A, B, "rows", "legacy");
+%! assert (c, [1, 2; 3, 4; 5, 6; 7, 8; 9, 10]);
+%! assert (ia, [1; 3]);
+%! assert (ib, [1; 2; 3]);
+
 ## Test orientation of output
 %!shared x,y
 %! x = 1:3;
@@ -176,6 +188,10 @@
 %!assert (size (union (x', y)), [5 1])
 %!assert (size (union (x, y')), [5 1])
 %!assert (size (union (x', y')), [5 1])
+%!assert (size (union (x, y, "legacy")), [1, 5])
+%!assert (size (union (x', y, "legacy")), [1, 5])
+%!assert (size (union (x, y', "legacy")), [1, 5])
+%!assert (size (union (x', y', "legacy")), [5, 1])
 
 ## Clear shared variables
 %!shared