octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fix orderfields.m


From: Jason Riedy
Subject: Fix orderfields.m
Date: Mon, 26 Jan 2009 22:17:26 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

# HG changeset patch
# User Jason Riedy <address@hidden>
# Date 1233025980 18000
# Node ID d3fd39d9c8bee2a56e700e7c7a877b47c168a8ff
# Parent  dacfd030633a4b6359c93418159fa80f8d381da2
Fix orderfields.m

Also add tests for three usage styles.

diff -r dacfd030633a -r d3fd39d9c8be scripts/miscellaneous/orderfields.m
--- a/scripts/miscellaneous/orderfields.m       Mon Jan 26 15:46:33 2009 +0100
+++ b/scripts/miscellaneous/orderfields.m       Mon Jan 26 22:13:00 2009 -0500
@@ -87,16 +87,37 @@
   endif
 
   ## Permute the names in the structure.
+  args = cell (1, 2 * numel (names));
+  args(1:2:end) = names;
   if (numel (s1) == 0)
-    args = cell (1, 2 * numel (names));
-    args(1:2:end) = names;
     args(2:2:end) = {[]};
-    t = struct (args{:});
   else
-    for i = 1:numel (names)
-      el = names(i);
-      t(:).(el) = s1(:).(el);
-    endfor
+    args(2:2:end) = {s1.(names)};
   endif
+  t = struct (args{:});
 
 endfunction
+
+%!test
+%!  a = struct ("foo", {1, 2}, "bar", {3, 4});
+%!  b = struct ("bar", 6, "foo", 5);
+%!  a(2) = orderfields (b, a);
+%!  if (a(2).foo != 5), 
+%!    error ("Wrong result: expected %d, found %d.", b.foo, a.foo(2));
+%!  endif
+
+%!test
+%!  a = struct ("foo", {1, 2}, "bar", {3, 4});
+%!  b = struct ("bar", 6, "foo", 5);
+%!  a(2) = orderfields (b, [2 1]);
+%!  if (a(2).foo != 5), 
+%!    error ("Wrong result: expected %d, found %d.", b.foo, a.foo(2));
+%!  endif
+
+%!test
+%!  a = struct ("foo", {1, 2}, "bar", {3, 4});
+%!  b = struct ("bar", 6, "foo", 5);
+%!  a(2) = orderfields (b, fieldnames (a));
+%!  if (a(2).foo != 5), 
+%!    error ("Wrong result: expected %d, found %d.", b.foo, a.foo(2));
+%!  endif


reply via email to

[Prev in Thread] Current Thread [Next in Thread]