# HG changeset patch # User Thorsten Meyer # Date 1233086659 -3600 # Node ID 4c68e26e3ba167fb15f4a2e80b9618c353c228de # Parent 827f0285a2016fd6ce72d2906e08faea48fb205a Add tests to prevent regression of struct array indexing diff -r 827f0285a201 -r 4c68e26e3ba1 test/ChangeLog --- a/test/ChangeLog Tue Jan 27 12:48:34 2009 -0500 +++ b/test/ChangeLog Tue Jan 27 21:04:19 2009 +0100 @@ -1,3 +1,7 @@ +2009-01-25 Thorsten Meyer + + * test_struct.m: Add struct array tests. + 2009-01-23 Søren Hauberg * test_prefer.m: Update to match new API of the 'type' function. diff -r 827f0285a201 -r 4c68e26e3ba1 test/test_struct.m --- a/test/test_struct.m Tue Jan 27 12:48:34 2009 -0500 +++ b/test/test_struct.m Tue Jan 27 21:04:19 2009 +0100 @@ -109,3 +109,93 @@ %! s.a = 1; %! fail("isstruct (s, 1)","Invalid call to isstruct.*"); +## increment element of matrix stored in struct array field +%!test +%! a = struct("c", {[1, 2, 3], [4, 5, 6], [7, 8, 9]}); +%! a(2).c(3)++; +%! assert(a(2).c, [4, 5, 7]); + +## create struct array by assignment to cs-list +%!test +%! [a(1:2).x] = deal (1, 3); +%! assert(a, struct("x", {1, 3})); +%! assert({a(1:2).x}, {1, 3}); + +## assign to subrange of struct array field +%!test +%! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 100); +%! [b(1:2, [1,3]).name] = deal("aaa", "ddd", "ccc", "fff"); +%! assert ({b.name}, {"aaa", "ddd", "b", "e", "ccc", "fff"}); + +## index into nested struct arrays +%!test +%! a = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); +%! a(2).value = a; +%! assert (a(2).value(2,3).name, "f"); + +## assign to subrange of field in nested struct array +%!test +%! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); +%! b(3, 1).value = b; +%! [b(3, 1).value(1, [1, 3]).name] = deal ("aaa", "ccc"); +%! assert (size (b), [3, 3]); +%! assert (b(3,1).value(1, 3).name, "ccc"); + +## test 4 dimensional struct array +%!test +%! c(4, 4, 4, 4).name = "a"; +%! c(3, 3, 3, 3).value = 1; +%! assert (c(2,2,2,2), struct ("name", [], "value", [])); + +## assign to subrange of field in 4d struct array +%!test +%! c(4, 4, 4, 4).name = "a"; +%! c(3, 3, 3, 3).value = 1; +%! [c([1, 3], 2, :, [3, 4]).value] = deal (1); +%! assert (length(find([c.value] == 1)), 17); +%! assert (length(find([c.value])), 17); + +## swap elements of struct array +%!test +%! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); +%! [b([2, 1], [3, 1]).name] = deal(b([1, 2], [1, 2]).name); +%! assert ({b.name}, {"e", "b", "b", "e", "d", "a"}); + +## test internal ordering of struct array fields +%!test +%! c(4, 4, 4, 4).value = 3; +%! c(1, 2, 3, 4).value = 2; +%! c(3, 3, 3, 3).value = 1; +%! d = reshape ({c.value}, size(c)); +%! assert ([d{4, 4, 4, 4}, d{1, 2, 3, 4}, d{3, 3, 3, 3}], +%! [3, 2, 1]); + +## test assignment to mixed cs-list of field element subranges +%!test +%! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 100); +%! [b(1:2, [1, 3]).name, b(2, 1:3).value] = ... +%! deal (1, 2, 3, 4, "5", "6", "7"); +%! assert ({b.name}, {1, 2, "b", "e", 3, 4}); +%! assert ({b.value}, {100, "5", 100, "6", 100, "7"}); + +%!error +%! [a(1:3).x] = deal ([1, 5], [3, 7], [8, 9]); +%! [a(2:3).x(2)] = deal (10, 11); + +%!error +%! [a(1:3).x] = deal ([1, 5], [3, 7], [8, 9]); +%! a(2:3).x(2); + +%!error +%! a(1).x.x = 1; +%! a(2).x; + +%!error +%! a = struct ("value", {1, 2, 3, 4, 5}); +%! [a(2:4).value] = 1; + +%!error +%! c(4, 4, 4, 4).name = "a"; +%! c(3, 3, 3, 3).value = 1; +%! c([1, 3], 2, :, [3, 4]).value = 1; +