# HG changeset patch
# User Kai T. Ohlhus <k.ohlhus@gmail.com>
# Date 1400855317 -7200
#      Fri May 23 16:28:37 2014 +0200
# Node ID ddd3736a14f0c28d021962c36d3ab0cb10431ded
# Parent  70ea5a2856fe0ca6780f4b70f976790cf6a719ff
errorless empty value and correct alphanumerical value comparision of isequal (bug #42408)

* __isequal__.m: comparison of empty and sparse empty values without error and corrected comparison of alphanumerical values.
* isequal.m: New tests.

diff -r 70ea5a2856fe -r ddd3736a14f0 scripts/general/isequal.m
--- a/scripts/general/isequal.m	Wed May 21 23:27:44 2014 +0200
+++ b/scripts/general/isequal.m	Fri May 23 16:28:37 2014 +0200
@@ -33,53 +33,104 @@
 endfunction
 
 
-## test size and shape
-%!assert (isequal ([1,2,3,4],[1,2,3,4]), true)
-%!assert (isequal ([1;2;3;4],[1;2;3;4]), true)
-%!assert (isequal ([1,2,3,4],[1;2;3;4]), false)
-%!assert (isequal ([1,2,3,4],[1,2;3,4]), false)
-%!assert (isequal ([1,2,3,4],[1,3;2,4]), false)
+%!# test wrong input
+%!error isequal ()
+%!error isequal (1)
+%!error isequal ([1,1])
 
+%!# test empty input
+%!assert (isequal ([], []), true)
+%!assert (isequal ([], [], 1), false)
+%!assert (isequal ([], 1, []), false)
+%!assert (isequal (1, [], []), false)
+%!assert (isequal (1, [], []), false)
+
+%!# test all numeric build-in primitives
+%!assert (isequal (false, logical (0), char (0), ...
+%!    int8 (0), int16 (0), int32 (0), int64 (0), ...
+%!    uint8 (0), uint16 (0), uint32 (0), uint64 (0), ...
+%!    double (0), single (0), double (complex (0,0)), ...
+%!    single  (complex (0,0))), true)
+%!assert (isequal (true, logical (1), char (1), ...
+%!    int8 (1), int16 (1), int32 (1), int64 (1), ...
+%!    uint8 (1), uint16 (1), uint32 (1), uint64 (1), ...
+%!    double (1), single (1), double (complex (1,0)), ...
+%!    single  (complex (1,0))), true)
+
+%!# test characters and strings
+%assert (isequal ('a',"a"), true)
+%!assert (isequal ('abab',['a', 'b', 'a', 'b']), true)
+%!assert (isequal (['a','b','c','d'],['a','b','c','d']), true)
+%!assert (isequal (['test   ';'strings'],['test   ';'strings'],...
+%!    ['test   ';'strings']), true)
+%!assert (isequal (['a','b','c','d'],['a';'b';'c';'d']), false)
+
+%!# test function_handle
+%!test
+%! fcn = @(x) x.^2;
+%! assert (isequal (fcn, fcn), true)
+%! assert (isequal (fcn, @(x) x.^2), false)
+%! assert (isequal (@(x) x.^2, fcn), false)
+
+%!# test size and shape
+assert (isequal ([1,2,3,4],[1,2,3,4]), true)
+assert (isequal ([1;2;3;4],[1;2;3;4]), true)
+assert (isequal ([1,2,3,4],[1;2;3;4]), false)
+assert (isequal ([1,2,3,4],[1,2;3,4]), false)
+assert (isequal ([1,2,3,4],[1,3;2,4]), false)
 %!test
 %! A = 1:8;
 %! B = reshape (A, 2, 2, 2);
 %! assert (isequal (A, B), false);
-
 %!test
 %! A = reshape (1:8, 2, 2, 2);
 %! B = A;
 %! assert (isequal (A, B), true);
-
 %!test
 %! A = reshape (1:8, 2, 4);
 %! B = reshape (A, 2, 2, 2);
 %! assert (isequal (A, B), false);
 
-## test for equality
-%!assert (isequal ([1,2,3,4],[1,2,3,4]), true)
-%!assert (isequal (['a','b','c','d'],['a','b','c','d']), true)
-## Test multi-line strings
-%!assert (isequal (["test";"strings"],["test";"strings"],["test";"strings"]), true)
-## test for inequality
-%!assert (isequal ([1,2,3,4],[1;2;3;4]), false)
-%!assert (isequal ({1,2,3,4},[1,2,3,4]), false)
-%!assert (isequal ([1,2,3,4],{1,2,3,4}), false)
+%!# test structures
+%!assert (isequal (struct ([]),struct ([])), true)
+%!assert (isequal (struct ('a',1),struct ('a',1)), true)
+%!assert (isequal (struct ('a',1),struct ('a',2)), false)
+%!assert (isequal (struct ('a',1),struct ('b',1)), false)
+%!assert (isequal (struct ('a',1,'b',2),struct ('a',1,'b',2)), true)
+%!assert (isequal (struct ('a',1,'b',2),struct ('b',2,'a',1)), true)
+%!assert (isequal (struct ('a',1,'b',2),struct ('a',1,'b',2),...
+%!    struct ('a',1,'b',2)), true)
+%!assert (isequal (struct ('a','abc','b',2),...
+%!    struct ('a','abc','b',2)), true)
+
+%!# test cells
+%!assert (isequal (cell(1,1),cell(1,1)), true)
+%!assert (isequal (cell(1,1),cell(1,2)), false)
+%!assert (isequal ({'a',1},{'a',1}), true)
+%!assert (isequal ({'a',1},{'a',2}), false)
+%!assert (isequal ({'a',1},{'b',1}), false)
+%!assert (isequal ({'a',1,'b',2},{'a',1,'b',2}), true)
+%!assert (isequal ({'a',1,'b',2},{'b',2,'a',1}), false)
+%!assert (isequal ({'a',1,'b',2},{'a',1,'b',2},{'a',1,'b',2}), true)
+%!assert (isequal ({'a','abc','b',2}, {'a','abc','b',2}), true)
+%!assert (isequal ({'a','b','c','d'},{'a','b','c','d'}), true)
+%!assert (isequal ({'a','b','c','d'},{'a';'b';'c';'d'}), false)
+%!assert (isequal (['a','b','c','d'],{'a','b','c','d'}), false)
+
+%!# test for sparse matrices
+%!assert (isequal (sparse ([]), []), true)
+%!assert (isequal ([], sparse ([])), true)
+%!assert (isequal (sparse (0,1), sparse (0,1)), true)
+%!assert (isequal (sparse (0,1), zeros (0,1)), true)
+%!assert (isequal (sparse (2,2), sparse (2,2)), true)
+%!assert (isequal (zeros (2,2), sparse (2,2)), true)
+%!assert (isequal (speye (1), eye (1)), true)
+%!assert (isequal (eye (300), speye (300)), true)
+%!assert (isequal (sparse (0,1), sparse (1,0)), false)
+
+%!# test NaN
+%!assert (isequal (NaN,NaN), false)
+%!assert (isequal (NaN,inf), false)
+%!assert (isequal (NaN,1.0), false)
 %!assert (isequal ([1,2,NaN,4],[1,2,NaN,4]), false)
-%!assert (isequal (['a','b','c','d'],['a';'b';'c';'d']), false)
-%!assert (isequal ({'a','b','c','d'},{'a';'b';'c';'d'}), false)
-## test for equality (struct)
-%!assert (isequal (struct ('a',1,'b',2),struct ('a',1,'b',2)), true)
-%!assert (isequal (struct ('a',1,'b',2),struct ('a',1,'b',2),struct ('a',1,'b',2)), true)
-%!assert (isequal (struct ('a',"abc",'b',2),struct ('a',"abc",'b',2)), true)
-## test for inequality (struct)
 %!assert (isequal (struct ('a',NaN,'b',2),struct ('a',NaN,'b',2),struct ('a',NaN,'b',2)), false)
-
-## test for sparse matrices
-%!assert (isequal (sparse (0,1), sparse (0,1)), true)
-%!assert (isequal (sparse (0,1), sparse (1,0)), false)
-%!assert (isequal (sparse (2, 2), sparse (2, 2)), true)
-
-## Input validation
-%!error isequal ()
-%!error isequal (1)
-
diff -r 70ea5a2856fe -r ddd3736a14f0 scripts/general/private/__isequal__.m
--- a/scripts/general/private/__isequal__.m	Wed May 21 23:27:44 2014 +0200
+++ b/scripts/general/private/__isequal__.m	Fri May 23 16:28:37 2014 +0200
@@ -55,10 +55,11 @@
   ## All arguments must either be of the same class or they must be
   ## numeric values.
   t = (all (strcmp (class (x),
-                    cellfun ("class", varargin, "uniformoutput", false)))
-       || ((isnumeric (x) || islogical (x))
-           && all (cellfun ("isnumeric", varargin)
-                   | cellfun ("islogical", varargin))));
+                    cellfun (@class, varargin, "uniformoutput", false)))
+       || ((isnumeric (x) || islogical (x) || ischar (x))
+           && all (cellfun (@isnumeric, varargin)
+                   | cellfun (@islogical, varargin)
+                   | cellfun (@ischar, varargin))));
 
   if (t)
     ## Test that everything has the same number of dimensions.
@@ -151,6 +152,11 @@
     else
       ## Check the numeric types.
 
+      ## All elements must have the same emptiness state
+      if (isempty (x) && all(cellfun (@isempty, varargin)))
+        t = true;
+        return;
+      endif
       f_x = find (x);
       l_f_x = length (f_x);
       x = x(f_x);
