changeset 28856:dece83bfab89 stable

Improve tests for bug #58593. * bug-58593/myclass2.m: Don't call subsref or subsasgn with empty index structure. bug-58593.tst: New tests for END in subsasgn expressions.
author Fernando Alvarruiz <feralber@upvnet.upv.es>
date Thu, 01 Oct 2020 19:07:18 +0200
parents 2219027f5bd4
children eb46f92a9ef4 ba6e10c316d3
files test/bug-58593/bug-58593.tst test/bug-58593/myclass2.m
diffstat 2 files changed, 69 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/test/bug-58593/bug-58593.tst	Tue Jul 14 00:45:39 2020 -0400
+++ b/test/bug-58593/bug-58593.tst	Thu Oct 01 19:07:18 2020 +0200
@@ -25,16 +25,32 @@
 
 %!test <*58593>
 %! obj = myclass1 ();
-%! assert (obj.data (plus (minus (end,1), 1)), 1005)
+%! assert (obj.data (plus (minus (end,1), 1)), 1005);
+
+%!test <*58593>
+%! obj = myclass2 ();
+%! assert (obj.alldata, 1001:1005);
+
+%!test <*58593>
+%! obj = myclass2 ();
+%! assert (obj(end), 1004);
 
 %!test <*58593>
 %! obj = myclass2 ();
-%! assert (obj.alldata, 1001:1005)
+%! assert (obj.data(end), 1005);
+
+%!test <*58593>
+%! obj = myclass2 ();
+%! obj.alldata = 1:5;
+%! assert (obj.data, 1:5);
 
 %!test <*58593>
 %! obj = myclass2 ();
-%! assert (obj(end), 1004)
+%! obj(end) = -1;
+%! assert (obj.data, [1001:1003, -1, 1005]);
 
 %!test <*58593>
 %! obj = myclass2 ();
-%! assert (obj.data(end), 1005)
+%! obj.data(end) = -1;
+%! assert (obj.data, [1001:1004, -1]);
+
--- a/test/bug-58593/myclass2.m	Tue Jul 14 00:45:39 2020 -0400
+++ b/test/bug-58593/myclass2.m	Thu Oct 01 19:07:18 2020 +0200
@@ -4,37 +4,74 @@
   end
 
   methods
-    function obj = myclass2(data)
+    function obj = myclass2 ()
       obj.data_ = 1001:1005;
     end
 
-    function r = subsref(obj,S)
-      switch S(1).type
+    function r = subsref (obj, S)
+      switch (S(1).type)
       case '.'
-        switch S(1).subs
+        switch (S(1).subs)
         case 'data'
           % Transform: obj.data --> obj.data_
-          r = subsref (obj.data_, S(2:end));
+          r = obj.data_;
+          if (length (S) > 1)
+            r = subsref (r, S(2:end));
+          end
         case 'alldata'
           % Transform: obj.data --> obj.data_(1:end)
-          % This expression should trigger *builtin* subsref *twice* (one
+          % This statement should trigger *builtin* subsref *twice* (one
           % for the evaluation of 'end', and the other for the whole rvalue).
           % 'end' here is also builtin 'end'
           r = obj.data_(1:end);
+
+          if (length (S) > 1)
+            r = subsref (r, S(2:end));
+          end
         otherwise
-          error('Incorrect usage')
+          error ('Incorrect usage');
         end
       case '()'
-        % Transform: obj(index) --> obj.data_(1:end)
-        r = subsref(obj.data_,S);
+        % Transform: obj(index) --> obj.data_(index)
+        r = subsref (obj.data_, S);
       otherwise
-        error('Incorrect usage')
+        error ('Incorrect usage');
       end
     end
 
-    function r = end(obj,k,n)
+    function obj = subsasgn (obj, S, B)
+      switch (S(1).type)
+      case '.'
+        switch (S(1).subs)
+        case 'data'
+          % Transform: obj.data --> obj.data_
+          if length(S)>1
+            B = subsasgn (obj.data_, S(2:end), B);
+          end
+          obj.data_ = B;
+        case 'alldata'
+          % Transform: obj.data --> obj.data_(1:end)
+          if length(S)>1
+            B = subsasgn (obj.data_(1:end), S(2:end), B);
+          end
+          % This statement should trigger *builtin* subsref to evaluate 'end',
+          % then *builtin* subsasgn for the whole assignment expression
+          % 'end' here is also builtin 'end'
+          obj.data_(1:end) = B;
+        otherwise
+          error('Incorrect usage');
+        end
+      case '()'
+        % Transform: obj(index) --> obj.data_(index)
+        obj.data_ = subsasgn (obj.data_, S, B);
+      otherwise
+        error ('Incorrect usage');
+      end
+    end
+
+    function r = end (obj, k, n)
       % We subtract 1 from the "real" end of obj.data_
-      r = builtin('end',obj.data_,k,n)-1;
+      r = builtin ('end', obj.data_, k, n) - 1;
     end
   end
 end