changeset 27296:538468f901dd

Clean up BIST tests in set functions. * intersect.m: Re-order BIST tests to follow order in other files. * setdiff.m: Rename variable "tmp" to "csort" for clarity. sortrows ([c; b]); Re-order BIST tests to follow order in other files. * setxor.m: Use keyword "end" rather than variable 'n' in indexing to match other files. Re-order BIST tests to follow order in other files. * union.m: Add BIST tests for orientation of output. * unique.m: Rewrite BIST tests to use semicolons for column vectors rather than transpose of row vector. Add comments describing each BIST test block.
author Rik <rik@octave.org>
date Sun, 28 Jul 2019 17:56:28 -0700
parents 7ec25367bdc5
children 6ac42f6615bb
files scripts/set/intersect.m scripts/set/setdiff.m scripts/set/setxor.m scripts/set/union.m scripts/set/unique.m
diffstat 5 files changed, 73 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/set/intersect.m	Sun Jul 28 07:54:21 2019 -0700
+++ b/scripts/set/intersect.m	Sun Jul 28 17:56:28 2019 -0700
@@ -157,6 +157,12 @@
 %!assert (intersect ([1 2; 2 3; 4 5], [2 3; 3 4; 5 6], "rows"), [2 3])
 %!assert (intersect ([1 NaN], [NaN NaN 5]), zeros (1,0))
 
+%!test
+%! a = [1 1 1 2 2 2];
+%! b = [1 2 3 4 5 6];
+%! c = intersect (a, b);
+%! assert (c, [1,2]);
+
 ## Test multi-dimensional arrays
 %!test
 %! a = rand (3,3,3);
@@ -175,12 +181,6 @@
 %! assert (a(ia), c);
 %! assert (b(ib), c);
 
-%!test
-%! a = [1 1 1 2 2 2];
-%! b = [1 2 3 4 5 6];
-%! c = intersect (a, b);
-%! assert(c, [1,2]);
-
 ## Test "rows" argument
 %!test
 %! a = [1,1,2;1,4,5;2,1,7];
@@ -201,6 +201,12 @@
 
 ## Test "stable" argument
 %!test
+%! a = [2 2 2 1 1 1];
+%! b = [1 2 3 4 5 6];
+%! c = intersect (a, b, "stable");
+%! assert (c, [2,1]);
+
+%!test
 %! a = [3 2 4 5 7 6 5 1 0 13 13];
 %! b = [3 5 12 1 1 7];
 %! [c, ia, ib] = intersect (a, b, "stable");
@@ -211,12 +217,6 @@
 %! assert (b(ib), c);
 
 %!test
-%! a = [2 2 2 1 1 1];
-%! b = [1 2 3 4 5 6];
-%! c = intersect (a, b, "stable");
-%! assert(c, [2,1]);
-
-%!test
 %! a = [1,4,5;1,1,2;2,1,7];
 %! b = [1,4,5;2,3,4;1,1,2;9,8,7];
 %! [c, ia, ib] = intersect (a, b, "rows", "stable");
--- a/scripts/set/setdiff.m	Sun Jul 28 07:54:21 2019 -0700
+++ b/scripts/set/setdiff.m	Sun Jul 28 17:56:28 2019 -0700
@@ -76,9 +76,9 @@
     if (! isempty (c) && ! isempty (b))
       ## Form A and B into combined set.
       b = unique (b, varargin{:});
-      [tmp, idx] = sortrows ([c; b]);
+      [csort, idx] = sortrows ([c; b]);
       ## Eliminate those elements of A that are the same as in B.
-      dups = find (all (tmp(1:end-1,:) == tmp(2:end,:), 2));
+      dups = find (all (csort(1:end-1,:) == csort(2:end,:), 2));
       c(idx(dups),:) = [];
       if (nargout > 1)
         ia(idx(dups),:) = [];
@@ -93,20 +93,22 @@
     if (! isempty (c) && ! isempty (b))
       ## Form a and b into combined set.
       b = unique (b);
-      [tmp, idx] = sort ([c(:); b(:)]);
+      [csort, idx] = sort ([c(:); b(:)]);
       ## Eliminate those elements of a that are the same as in b.
-      if (iscellstr (tmp))
-        dups = find (strcmp (tmp(1:end-1), tmp(2:end)));
+      if (iscellstr (csort))
+        dups = find (strcmp (csort(1:end-1), csort(2:end)));
       else
-        dups = find (tmp(1:end-1) == tmp(2:end));
+        dups = find (csort(1:end-1) == csort(2:end));
       endif
       c(idx(dups)) = [];
+
       ## Reshape if necessary for Matlab compatibility.
       if (isrowvec)
         c = c(:).';
       else
         c = c(:);
       endif
+
       if (nargout > 1)
         ia(idx(dups)) = [];
         if (optlegacy && isrowvec)
@@ -127,6 +129,27 @@
 %!assert (setdiff ([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 4])
 %!assert (setdiff ({"one","two";"three","four"}, {"one","two";"three","six"}), {"four"})
 
+## Test multi-dimensional input
+%!test
+%! a = rand (3,3,3);
+%! b = a(1);
+%! assert (setdiff (a, b), sort (a(2:end)'));
+
+## Test "rows"
+%!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]);
+
+%!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", "stable");
+%! assert (c, [7, 9 7; 1, 4 ,5]);
+%! assert (ia, [1; 5]);
+
 ## Test sorting order
 %!test
 %! a = [5, 1, 4, 1, 3];
@@ -142,12 +165,6 @@
 %! assert (c, [5, 3]);
 %! assert (ia, [1; 5]);
 
-## Test multi-dimensional input
-%!test
-%! a = rand (3,3,3);
-%! b = a(1);
-%! assert (setdiff (a, b), sort (a(2:end)'));
-
 ## Test output orientation compatibility
 %!assert <*42577> (setdiff ([1:5], 2), [1,3,4,5])
 %!assert <*42577> (setdiff ([1:5]', 2), [1;3;4;5])
@@ -156,21 +173,6 @@
 %!assert <*42577> (setdiff ([1:5]', [2:3]), [1;4;5])
 %!assert <*42577> (setdiff ([1:5]', [2:3]'), [1;4;5])
 
-## Test "rows"
-%!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]);
-
-%!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", "stable");
-%! assert (c, [7, 9 7; 1, 4 ,5]);
-%! assert (ia, [1; 5]);
-
 ## Test "legacy" option
 %!test
 %! a = [3, 6, 2, 1, 5, 1, 1];
--- a/scripts/set/setxor.m	Sun Jul 28 07:54:21 2019 -0700
+++ b/scripts/set/setxor.m	Sun Jul 28 17:56:28 2019 -0700
@@ -106,11 +106,10 @@
     else
       na = numel (a);  nb = numel (b);
       [c, i] = sort ([a(:); b(:)]);
-      n = numel (c);
       if (iscell (c))
-        idx = find (strcmp (c(1:n-1), c(2:n)));
+        idx = find (strcmp (c(1:end-1), c(2:end)));
       else
-        idx = find (c(1:n-1) == c(2:n));
+        idx = find (c(1:end-1) == c(2:end));
       endif
       if (optsorted)
         if (! isempty (idx))
@@ -158,6 +157,13 @@
 %! assert (ia, [5]);
 %! assert (ib, [2]);
 
+## Test multi-dimensional arrays
+%!test
+%! a = rand (3,3,3);
+%! b = a;
+%! b(1,1,1) = 2;
+%! assert (intersect (a, b), sort (a(2:end)'));
+
 ## Test "rows" input
 %!test
 %! a = [1 2; 4 5; 1 3];
@@ -223,13 +229,6 @@
 %!assert (size (setxor (x, y')), [3 1])
 %!assert (size (setxor (x', y')), [3 1])
 
-## Test multi-dimensional arrays
-%!test
-%! a = rand (3,3,3);
-%! b = a;
-%! b(1,1,1) = 2;
-%! assert (intersect (a, b), sort (a(2:end)'));
-
 ## Test "legacy" input
 %!test
 %! a = [5 1 3 3 3];
--- a/scripts/set/union.m	Sun Jul 28 07:54:21 2019 -0700
+++ b/scripts/set/union.m	Sun Jul 28 17:56:28 2019 -0700
@@ -163,6 +163,19 @@
 %! assert (ia, [1, 2]);
 %! assert (ib, [3, 1]);
 
+## Test orientation of output
+%!shared x,y
+%! x = 1:3;
+%! y = 2:5;
+
+%!assert (size (union (x, y)), [1 5])
+%!assert (size (union (x', y)), [5 1])
+%!assert (size (union (x, y')), [5 1])
+%!assert (size (union (x', y')), [5 1])
+
+## Clear shared variables
+%!shared
+
 ## Test empty cell string array unions
 %!assert (union ({}, []), cell (0,1))
 %!assert (union ([], {}), cell (0,1))
--- a/scripts/set/unique.m	Sun Jul 28 07:54:21 2019 -0700
+++ b/scripts/set/unique.m	Sun Jul 28 17:56:28 2019 -0700
@@ -277,19 +277,20 @@
 %!assert (unique ({}), {})
 %!assert (unique ([1,2,2,3,2,4], "rows"), [1,2,2,3,2,4])
 %!assert (unique ([1,2,2,3,2,4]), [1,2,3,4])
-%!assert (unique ([1,2,2,3,2,4]', "rows"), [1,2,3,4]')
-%!assert (unique (sparse ([2,0;2,0])), [0,2]')
-%!assert (unique (sparse ([1,2;2,3])), [1,2,3]')
-%!assert (unique ([1,2,2,3,2,4]', "rows"), [1,2,3,4]')
+%!assert (unique ([1,2,2,3,2,4]', "rows"), [1;2;3;4])
+%!assert (unique (sparse ([2,0;2,0])), [0;2])
+%!assert (unique (sparse ([1,2;2,3])), [1;2;3])
+%!assert (unique ([1,2,2,3,2,4]', "rows"), [1;2;3;4])
 %!assert (unique (single ([1,2,2,3,2,4]), "rows"), single ([1,2,2,3,2,4]))
 %!assert (unique (single ([1,2,2,3,2,4])), single ([1,2,3,4]))
-%!assert (unique (single ([1,2,2,3,2,4]'), "rows"), single ([1,2,3,4]'))
+%!assert (unique (single ([1,2,2,3,2,4]'), "rows"), single ([1;2;3;4]))
 %!assert (unique (uint8 ([1,2,2,3,2,4]), "rows"), uint8 ([1,2,2,3,2,4]))
 %!assert (unique (uint8 ([1,2,2,3,2,4])), uint8 ([1,2,3,4]))
-%!assert (unique (uint8 ([1,2,2,3,2,4]'), "rows"), uint8 ([1,2,3,4]'))
+%!assert (unique (uint8 ([1,2,2,3,2,4]'), "rows"), uint8 ([1;2;3;4]))
 
+## Test options with numeric inputs
 %!test
-%! [y,i,j] = unique ([1,1,2,3,3,3,4]);
+%! [y,i,j] = unique ([1,1,2,3,3,3,4], "sorted");
 %! assert (y, [1,2,3,4]);
 %! assert (i, [1;3;4;7]);
 %! assert (j, [1;1;2;3;3;3;4]);
@@ -306,6 +307,7 @@
 %! assert (i, [2;3;6;7]);
 %! assert (j, [1;1;2;3;3;3;4]);
 
+## Test options with cellstr inputs
 %!test
 %! [y,i,j] = unique ({"z"; "z"; "z"});
 %! assert (y, {"z"});
@@ -331,6 +333,7 @@
 %! assert (A(i,:), y);
 %! ##assert (y(j,:), A);
 
+## Test "legacy" option
 %!test
 %! [y,i,j] = unique ([1,1,2,3,3,3,4], "legacy");
 %! assert (y, [1,2,3,4]);