comparison src/ov-struct.cc @ 14429:eff4a5933e28

Update %!tests in src/ directory with Octave coding conventions. * data.cc, defaults.cc, dirfns.cc, file-io.cc, graphics.cc, mappers.cc, oct-map.cc, octave.cc, ov-base.cc, ov-bool-mat.cc, ov-cell.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov-flt-re-mat.cc, ov-int16.cc, ov-int32.cc, ov-int64.cc, ov-int8.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-range.cc, ov-re-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-uint16.cc, ov-uint32.cc, ov-uint64.cc, ov-uint8.cc, ov.cc, pr-output.cc, pt-binop.cc, pt-eval.cc, pt-idx.cc, pt-mat.cc, sighandlers.cc, strfns.cc, symtab.cc, syscalls.cc, sysdep.cc, toplev.cc, utils.cc, variables.cc: Update %!tests in src/ directory with Octave coding conventions.
author Rik <octave@nomad.inbox5.com>
date Sun, 04 Mar 2012 12:21:10 -0800
parents 97883071e8e4
children bb5ecda3b975
comparison
equal deleted inserted replaced
14428:099bd779466c 14429:eff4a5933e28
247 return retval; 247 return retval;
248 } 248 }
249 249
250 /* 250 /*
251 %!test 251 %!test
252 %! x(1).a.a = 1; x(2).a.a = 2; 252 %! x(1).a.a = 1;
253 %! x(2).a.a = 2;
253 %! assert (size (x), [1, 2]); 254 %! assert (size (x), [1, 2]);
254 %! assert (x(1).a.a, 1); 255 %! assert (x(1).a.a, 1);
255 %! assert (x(2).a.a, 2); 256 %! assert (x(2).a.a, 2);
256 */ 257 */
257 258
689 scalar (const dim_vector& dims) 690 scalar (const dim_vector& dims)
690 { 691 {
691 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1; 692 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1;
692 } 693 }
693 694
694 /*
695 %!shared x
696 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3;
697 %!assert(struct('a',1,'b',3),x(1))
698 %!assert(isempty(x([])))
699 %!assert(isempty(struct('a',{},'b',{})))
700 %!assert(struct('a',{1,2},'b',{3,3}),x)
701 %!assert(struct('a',{1,2},'b',3),x)
702 %!assert(struct('a',{1,2},'b',{3}),x)
703 %!assert(struct('b',3,'a',{1,2}),x)
704 %!assert(struct('b',{3},'a',{1,2}),x)
705 %!test x=struct([]);
706 %!assert(size(x),[0,0]);
707 %!assert(isstruct(x));
708 %!assert(isempty(fieldnames(x)));
709 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4")
710 %!fail("struct(1,2,3,4)","struct: expecting alternating \"field\", VALUE pairs");
711 %!fail("struct('1',2,'3')","struct: expecting alternating \"field\", VALUE pairs");
712 */
713 695
714 bool 696 bool
715 octave_struct::save_ascii (std::ostream& os) 697 octave_struct::save_ascii (std::ostream& os)
716 { 698 {
717 octave_map m = map_value (); 699 octave_map m = map_value ();
1187 return retval; 1169 return retval;
1188 } 1170 }
1189 1171
1190 /* 1172 /*
1191 %!test 1173 %!test
1192 %! x(1).a.a = 1; x(2).a.a = 2; 1174 %! x(1).a.a = 1;
1175 %! x(2).a.a = 2;
1193 %! assert (size (x), [1, 2]); 1176 %! assert (size (x), [1, 2]);
1194 %! assert (x(1).a.a, 1); 1177 %! assert (x(1).a.a, 1);
1195 %! assert (x(2).a.a, 2); 1178 %! assert (x(2).a.a, 2);
1196 */ 1179 */
1197 1180
1710 return true; 1693 return true;
1711 } 1694 }
1712 else 1695 else
1713 return false; 1696 return false;
1714 } 1697 }
1715 /*
1716 %!shared x
1717 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3;
1718 %!assert(struct('a',1,'b',3),x(1))
1719 %!assert(isempty(x([])))
1720 %!assert(isempty(struct('a',{},'b',{})))
1721 %!assert(struct('a',{1,2},'b',{3,3}),x)
1722 %!assert(struct('a',{1,2},'b',3),x)
1723 %!assert(struct('a',{1,2},'b',{3}),x)
1724 %!assert(struct('b',3,'a',{1,2}),x)
1725 %!assert(struct('b',{3},'a',{1,2}),x)
1726 %!test x=struct([]);
1727 %!assert(size(x),[0,0]);
1728 %!assert(isstruct(x));
1729 %!assert(isempty(fieldnames(x)));
1730 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4")
1731 %!fail("struct(1,2,3,4)","struct: expecting alternating \"field\", VALUE pairs");
1732 %!fail("struct('1',2,'3')","struct: expecting alternating \"field\", VALUE pairs");
1733 */
1734 1698
1735 DEFUN (struct, args, , 1699 DEFUN (struct, args, ,
1736 "-*- texinfo -*-\n\ 1700 "-*- texinfo -*-\n\
1737 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\ 1701 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\
1738 \n\ 1702 \n\
1872 } 1836 }
1873 1837
1874 return octave_value (map); 1838 return octave_value (map);
1875 } 1839 }
1876 1840
1841 /*
1842 %!shared x
1843 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3;
1844 %!assert (struct ("a",1, "b",3), x(1))
1845 %!assert (isempty (x([])))
1846 %!assert (isempty (struct ("a",{}, "b",{})))
1847 %!assert (struct ("a",{1,2}, "b",{3,3}), x)
1848 %!assert (struct ("a",{1,2}, "b",3), x)
1849 %!assert (struct ("a",{1,2}, "b",{3}), x)
1850 %!assert (struct ("b",3, "a",{1,2}), x)
1851 %!assert (struct ("b",{3}, "a",{1,2}), x)
1852 %!test x = struct ([]);
1853 %!assert (size (x), [0,0])
1854 %!assert (isstruct (x))
1855 %!assert (isempty (fieldnames (x)))
1856 %!fail ('struct ("a",{1,2},"b",{1,2,3})', 'dimensions of parameter 2 do not match those of parameter 4')
1857 %!fail ('struct (1,2,3,4)', 'struct: expecting alternating "field", VALUE pairs')
1858 %!fail ('struct ("1",2,"3")', 'struct: expecting alternating "field", VALUE pairs')
1859 */
1860
1877 DEFUN (isstruct, args, , 1861 DEFUN (isstruct, args, ,
1878 "-*- texinfo -*-\n\ 1862 "-*- texinfo -*-\n\
1879 @deftypefn {Built-in Function} {} isstruct (@var{x})\n\ 1863 @deftypefn {Built-in Function} {} isstruct (@var{x})\n\
1880 Return true if @var{x} is a structure or a structure array.\n\ 1864 Return true if @var{x} is a structure or a structure array.\n\
1881 @seealso{ismatrix, iscell, isa}\n\ 1865 @seealso{ismatrix, iscell, isa}\n\
1926 1910
1927 return retval; 1911 return retval;
1928 } 1912 }
1929 1913
1930 /* 1914 /*
1931 %!# test preservation of fieldname order 1915 ## test preservation of fieldname order
1932 %!test 1916 %!test
1933 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; 1917 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3;
1934 %! assert(fieldnames(x), {"d"; "a"; "b"; "c"}); 1918 %! assert (fieldnames (x), {"d"; "a"; "b"; "c"});
1935 */ 1919 */
1936 1920
1937 DEFUN (isfield, args, , 1921 DEFUN (isfield, args, ,
1938 "-*- texinfo -*-\n\ 1922 "-*- texinfo -*-\n\
1939 @deftypefn {Built-in Function} {} isfield (@var{x}, @var{name})\n\ 1923 @deftypefn {Built-in Function} {} isfield (@var{x}, @var{name})\n\
2010 1994
2011 return retval; 1995 return retval;
2012 } 1996 }
2013 1997
2014 /* 1998 /*
2015 %!# test isfield 1999 ## test isfield
2016 %!test 2000 %!test
2017 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; 2001 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3;
2018 %! assert (isfield (x, 'b')); 2002 %! assert (isfield (x, "b"));
2019 %!assert (isfield (struct('a', '1'), 'a')); 2003 %!assert (isfield (struct("a", "1"), "a"))
2020 %!assert (isfield ({1}, 'c'), false); 2004 %!assert (isfield ({1}, "c"), false)
2021 %!assert (isfield (struct('a', '1'), 10), false); 2005 %!assert (isfield (struct("a", "1"), 10), false)
2022 %!assert (isfield (struct('a', 'b'), "a "), false); 2006 %!assert (isfield (struct("a", "b"), "a "), false)
2023 %!assert (isfield (struct('a', 1, 'b', 2), {'a', 'c'}), [true, false]); 2007 %!assert (isfield (struct("a", 1, "b", 2), {"a", "c"}), [true, false])
2024 */ 2008 */
2025 2009
2026 DEFUN (cell2struct, args, , 2010 DEFUN (cell2struct, args, ,
2027 "-*- texinfo -*-\n\ 2011 "-*- texinfo -*-\n\
2028 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\ 2012 @deftypefn {Built-in Function} {} cell2struct (@var{cell}, @var{fields}, @var{dim})\n\
2136 2120
2137 return retval; 2121 return retval;
2138 } 2122 }
2139 2123
2140 /* 2124 /*
2141 %!# test cell2struct versus struct2cell 2125 ## test cell2struct versus struct2cell
2142 %!test 2126 %!test
2143 %! keys = cellstr (char (floor (rand (100,10)*24+65)))'; 2127 %! keys = cellstr (char (floor (rand (100,10)*24+65)))';
2144 %! vals = mat2cell(rand (100,1), ones (100,1), 1)'; 2128 %! vals = mat2cell (rand (100,1), ones (100,1), 1)';
2145 %! s = struct ([keys; vals]{:}); 2129 %! s = struct ([keys; vals]{:});
2146 %! t = cell2struct (vals, keys, 2); 2130 %! t = cell2struct (vals, keys, 2);
2147 %! assert (s, t); 2131 %! assert (s, t);
2148 %! assert (struct2cell (s), vals'); 2132 %! assert (struct2cell (s), vals');
2149 %! assert (fieldnames (s), keys'); 2133 %! assert (fieldnames (s), keys');
2150 2134
2151 %!assert (cell2struct ({1; 2}, {"a"; "b"}), struct ("a", 1, "b", 2)); 2135 %!assert (cell2struct ({1; 2}, {"a"; "b"}), struct ("a", 1, "b", 2));
2152 */ 2136 */
2153 2137
2154 2138
2202 2186
2203 return retval; 2187 return retval;
2204 } 2188 }
2205 2189
2206 /* 2190 /*
2207 %!# test rmfield 2191 ## test rmfield
2208 %!test 2192 %!test
2209 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; x(6).f="abc123"; 2193 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; x(6).f="abc123";
2210 %! y = rmfield (x, {"a", "f"}); 2194 %! y = rmfield (x, {"a", "f"});
2211 %! assert (fieldnames (y), {"d"; "b"; "c"}); 2195 %! assert (fieldnames (y), {"d"; "b"; "c"});
2212 %! assert (size (y), [1, 6]); 2196 %! assert (size (y), [1, 6]);
2213 */ 2197 */
2214 2198
2215 DEFUN (struct_levels_to_print, args, nargout, 2199 DEFUN (struct_levels_to_print, args, nargout,
2216 "-*- texinfo -*-\n\ 2200 "-*- texinfo -*-\n\
2217 @deftypefn {Built-in Function} {@var{val} =} struct_levels_to_print ()\n\ 2201 @deftypefn {Built-in Function} {@var{val} =} struct_levels_to_print ()\n\