changeset 23880:1b232c0c19e6

Empty indexing of a struct should return the entire struct (bug #51633). * ov-struct.cc (do_index_op): Check size of idx list. If it is zero, return the struct itself, otherwise continue and perform indexing operation. * test/struct.tst: Add test for proper behavior.
author Rik <rik@octave.org>
date Fri, 11 Aug 2017 08:05:01 -0700
parents 92a3b165689e
children 5aec653c88e7
files libinterp/octave-value/ov-struct.cc test/struct.tst
diffstat 2 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-struct.cc	Thu Aug 10 21:17:09 2017 -0700
+++ b/libinterp/octave-value/ov-struct.cc	Fri Aug 11 08:05:01 2017 -0700
@@ -537,8 +537,10 @@
 octave_value
 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok)
 {
-  // octave_map handles indexing itself.
-  return map.index (idx, resize_ok);
+  if (idx.length () == 0)
+    return map;
+  else  // octave_map handles indexing itself.
+    return map.index (idx, resize_ok);
 }
 
 size_t
--- a/test/struct.tst	Thu Aug 10 21:17:09 2017 -0700
+++ b/test/struct.tst	Fri Aug 11 08:05:01 2017 -0700
@@ -230,9 +230,19 @@
 %! s(3).foo = 42;
 %! assert (s(3), struct ("foo", 42));
 
+## empty index should return entire struct
+%!test <51633>
+%! x.a = 1:10;
+%! y = x;
+%! assert (! isempty (y));
+%! assert (y, x);
+%! z = x();
+%! assert (! isempty (z));
+%! assert (z, x);
+%! assert (z, y);
+
 ## test assigning to multi-dim struct with trailing singleton dimensions,
-## Bug #35841.
-%!test
+%!test <35841>
 %! a(1,1,1).b(1) = 1;
 %! a(1,1,1).b(1) = 2;
 %! a(1,1,:).b(1) = 3;