Mercurial > hg > octave-nkf
comparison test/test_struct.m @ 14131:c3309e1ec50d stable
test: Use Octave coding and spacing conventions for fixed test scripts
* build_bc_overload_tests.sh, build_bc_overloads_expected.m,
build_sparse_tests.sh, test_args.m, test_contin.m, test_diag_perm.m,
test_error.m, test_eval-catch.m, test_for.m, test_func.m, test_global.m,
test_if.m, test_index.m, test_io.m, test_logical_index.m, test_null_assign.m,
test_parser.m, test_prefer.m, test_range.m, test_recursion.m, test_return.m,
test_slice.m, test_struct.m, test_switch.m, test_system.m, test_transpose.m,
test_try.m, test_unwind.m, test_while.m:
Use Octave coding and spacing conventions for fixed test scripts.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 01 Jan 2012 20:04:52 -0800 |
parents | 5fa482628bf6 |
children | 72c96de7a403 |
comparison
equal
deleted
inserted
replaced
14130:e13bfbc2c4c5 | 14131:c3309e1ec50d |
---|---|
18 | 18 |
19 %% test/octave.test/struct/fieldnames-1.m | 19 %% test/octave.test/struct/fieldnames-1.m |
20 %!test | 20 %!test |
21 %! s.a = 1; | 21 %! s.a = 1; |
22 %! c = fieldnames (s); | 22 %! c = fieldnames (s); |
23 %! assert(iscell (c) && strcmp (c{1}, "a")); | 23 %! assert (iscell (c) && strcmp (c{1}, "a")); |
24 | 24 |
25 %% test/octave.test/struct/fieldnames-2.m | 25 %% test/octave.test/struct/fieldnames-2.m |
26 %!test | 26 %!test |
27 %! s.a.b = 1; | 27 %! s.a.b = 1; |
28 %! c = fieldnames (s.a); | 28 %! c = fieldnames (s.a); |
29 %! assert(iscell (c) && strcmp (c{1}, "b")); | 29 %! assert (iscell (c) && strcmp (c{1}, "b")); |
30 | 30 |
31 %% test/octave.test/struct/fieldnames-3.m | 31 %% test/octave.test/struct/fieldnames-3.m |
32 %!error <Invalid call to fieldnames> fieldnames (); | 32 %!error <Invalid call to fieldnames> fieldnames (); |
33 | 33 |
34 %% test/octave.test/struct/fieldnames-4.m | 34 %% test/octave.test/struct/fieldnames-4.m |
35 %!test | 35 %!test |
36 %! s.a = 1; | 36 %! s.a = 1; |
37 %! fail("fieldnames (s, 1)","Invalid call to fieldnames.*"); | 37 %! fail ("fieldnames (s, 1)", "Invalid call to fieldnames"); |
38 | 38 |
39 %% test/octave.test/struct/fieldnames-5.m | 39 %% test/octave.test/struct/fieldnames-5.m |
40 %!error fieldnames (1); | 40 %!error fieldnames (1); |
41 | 41 |
42 %% test/octave.test/struct/isfield-1.m | 42 %% test/octave.test/struct/isfield-1.m |
43 %!test | 43 %!test |
44 %! s.aaa = 1; | 44 %! s.aaa = 1; |
45 %! s.a = 2; | 45 %! s.a = 2; |
46 %! assert(isfield (s, "a")); | 46 %! assert (isfield (s, "a")); |
47 | 47 |
48 %% test/octave.test/struct/isfield-2.m | 48 %% test/octave.test/struct/isfield-2.m |
49 %!test | 49 %!test |
50 %! s.aaa = 1; | 50 %! s.aaa = 1; |
51 %! s.a = 2; | 51 %! s.a = 2; |
52 %! assert(!(isfield (s, "b"))); | 52 %! assert (!(isfield (s, "b"))); |
53 | 53 |
54 %% test/octave.test/struct/isfield-3.m | 54 %% test/octave.test/struct/isfield-3.m |
55 %!error <Invalid call to isfield> isfield (); | 55 %!error <Invalid call to isfield> isfield (); |
56 | 56 |
57 %% test/octave.test/struct/isfield-4.m | 57 %% test/octave.test/struct/isfield-4.m |
58 %!test | 58 %!test |
59 %! s.aaa = 1; | 59 %! s.aaa = 1; |
60 %! s.a = 2; | 60 %! s.a = 2; |
61 %! fail("isfield (s, 'a', 3);","Invalid call to isfield.*"); | 61 %! fail ("isfield (s, 'a', 3);", "Invalid call to isfield"); |
62 | 62 |
63 %% test/octave.test/struct/isfield-5.m | 63 %% test/octave.test/struct/isfield-5.m |
64 %!assert(isfield (1, "m") == 0); | 64 %!assert (isfield (1, "m") == 0); |
65 | 65 |
66 %% test/octave.test/struct/isfield-6.m | 66 %% test/octave.test/struct/isfield-6.m |
67 %!test | 67 %!test |
68 %! s.a = 2; | 68 %! s.a = 2; |
69 %! assert(isfield (s, 2) == 0); | 69 %! assert (isfield (s, 2) == 0); |
70 | 70 |
71 %% test/octave.test/struct/isstruct-1.m | 71 %% test/octave.test/struct/isstruct-1.m |
72 %!assert(!(isstruct (1))); | 72 %!assert (!(isstruct (1))) |
73 | 73 |
74 %% test/octave.test/struct/isstruct-2.m | 74 %% test/octave.test/struct/isstruct-2.m |
75 %!assert(!(isstruct ([1, 2]))); | 75 %!assert (!(isstruct ([1, 2]))) |
76 | 76 |
77 %% test/octave.test/struct/isstruct-3.m | 77 %% test/octave.test/struct/isstruct-3.m |
78 %!assert(!(isstruct ([]))); | 78 %!assert (!(isstruct ([]))) |
79 | 79 |
80 %% test/octave.test/struct/isstruct-4.m | 80 %% test/octave.test/struct/isstruct-4.m |
81 %!assert(!(isstruct ([1, 2; 3, 4]))); | 81 %!assert (!(isstruct ([1, 2; 3, 4]))) |
82 | 82 |
83 %% test/octave.test/struct/isstruct-5.m | 83 %% test/octave.test/struct/isstruct-5.m |
84 %!assert(!(isstruct ("t"))); | 84 %!assert (!(isstruct ("t"))) |
85 | 85 |
86 %% test/octave.test/struct/isstruct-6.m | 86 %% test/octave.test/struct/isstruct-6.m |
87 %!assert(!(isstruct ("test"))); | 87 %!assert (!(isstruct ("test"))) |
88 | 88 |
89 %% test/octave.test/struct/isstruct-7.m | 89 %% test/octave.test/struct/isstruct-7.m |
90 %!assert(!(isstruct (["test"; "ing"]))); | 90 %!assert (!(isstruct (["test"; "ing"]))) |
91 | |
92 %!assert (!(isstruct ({1}))) | |
91 | 93 |
92 %% test/octave.test/struct/isstruct-8.m | 94 %% test/octave.test/struct/isstruct-8.m |
93 %!test | 95 %!test |
94 %! s.a = 1; | 96 %! s.a = 1; |
95 %! assert(isstruct (s)); | 97 %! assert (isstruct (s)); |
96 | 98 |
97 %% test/octave.test/struct/isstruct-9.m | 99 %% test/octave.test/struct/isstruct-9.m |
98 %!test | 100 %!test |
99 %! s.a.b = 1; | 101 %! s.a.b = 1; |
100 %! assert(isstruct (s.a)); | 102 %! assert (isstruct (s.a)); |
101 | 103 |
102 %% test/octave.test/struct/isstruct-10.m | 104 %% test/octave.test/struct/isstruct-10.m |
103 %!error <Invalid call to isstruct> isstruct (); | 105 %!error <Invalid call to isstruct> isstruct (); |
104 | 106 |
105 %% test/octave.test/struct/isstruct-11.m | 107 %% test/octave.test/struct/isstruct-11.m |
106 %!test | 108 %!test |
107 %! s.a = 1; | 109 %! s.a = 1; |
108 %! fail("isstruct (s, 1)","Invalid call to isstruct.*"); | 110 %! fail ("isstruct (s, 1)", "Invalid call to isstruct"); |
109 | 111 |
110 ## increment element of matrix stored in struct array field | 112 ## increment element of matrix stored in struct array field |
111 %!test | 113 %!test |
112 %! a = struct("c", {[1, 2, 3], [4, 5, 6], [7, 8, 9]}); | 114 %! a = struct ("c", {[1, 2, 3], [4, 5, 6], [7, 8, 9]}); |
113 %! a(2).c(3)++; | 115 %! a(2).c(3)++; |
114 %! assert(a(2).c, [4, 5, 7]); | 116 %! assert (a(2).c, [4, 5, 7]); |
115 | 117 |
116 ## create struct array by assignment to cs-list | 118 ## create struct array by assignment to cs-list |
117 %!test | 119 %!test |
118 %! [a(1:2).x] = deal (1, 3); | 120 %! [a(1:2).x] = deal (1, 3); |
119 %! assert(a, struct("x", {1, 3})); | 121 %! assert (a, struct ("x", {1, 3})); |
120 %! assert({a(1:2).x}, {1, 3}); | 122 %! assert ({a(1:2).x}, {1, 3}); |
121 | 123 |
122 ## assign to subrange of struct array field | 124 ## assign to subrange of struct array field |
123 %!test | 125 %!test |
124 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 100); | 126 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 100); |
125 %! [b(1:2, [1,3]).name] = deal("aaa", "ddd", "ccc", "fff"); | 127 %! [b(1:2, [1,3]).name] = deal ("aaa", "ddd", "ccc", "fff"); |
126 %! assert ({b.name}, {"aaa", "ddd", "b", "e", "ccc", "fff"}); | 128 %! assert ({b.name}, {"aaa", "ddd", "b", "e", "ccc", "fff"}); |
127 | 129 |
128 ## index into nested struct arrays | 130 ## index into nested struct arrays |
129 %!test | 131 %!test |
130 %! a = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); | 132 %! a = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); |
131 %! a(2).value = a; | 133 %! a(2).value = a; |
132 %! assert (a(2).value(2,3).name, "f"); | 134 %! assert (a(2).value(2,3).name, "f"); |
133 | 135 |
134 ## assign to subrange of field in nested struct array | 136 ## assign to subrange of field in nested struct array |
135 %!test | 137 %!test |
136 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); | 138 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); |
137 %! b(3, 1).value = b; | 139 %! b(3, 1).value = b; |
138 %! [b(3, 1).value(1, [1, 3]).name] = deal ("aaa", "ccc"); | 140 %! [b(3, 1).value(1, [1, 3]).name] = deal ("aaa", "ccc"); |
139 %! assert (size (b), [3, 3]); | 141 %! assert (size (b), [3, 3]); |
140 %! assert (b(3,1).value(1, 3).name, "ccc"); | 142 %! assert (b(3,1).value(1, 3).name, "ccc"); |
141 | 143 |
142 ## test 4 dimensional struct array | 144 ## test 4-dimensional struct array |
143 %!test | 145 %!test |
144 %! c(4, 4, 4, 4).name = "a"; | 146 %! c(4, 4, 4, 4).name = "a"; |
145 %! c(3, 3, 3, 3).value = 1; | 147 %! c(3, 3, 3, 3).value = 1; |
146 %! assert (c(2,2,2,2), struct ("name", [], "value", [])); | 148 %! assert (c(2,2,2,2), struct ("name", [], "value", [])); |
147 | 149 |
148 ## assign to subrange of field in 4d struct array | 150 ## assign to subrange of field in 4D struct array |
149 %!test | 151 %!test |
150 %! c(4, 4, 4, 4).name = "a"; | 152 %! c(4, 4, 4, 4).name = "a"; |
151 %! c(3, 3, 3, 3).value = 1; | 153 %! c(3, 3, 3, 3).value = 1; |
152 %! [c([1, 3], 2, :, [3, 4]).value] = deal (1); | 154 %! [c([1, 3], 2, :, [3, 4]).value] = deal (1); |
153 %! assert (length(find([c.value] == 1)), 17); | 155 %! assert (length(find([c.value] == 1)), 17); |
154 %! assert (length(find([c.value])), 17); | 156 %! assert (length(find([c.value])), 17); |
155 | 157 |
156 ## swap elements of struct array | 158 ## swap elements of struct array |
157 %!test | 159 %!test |
158 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); | 160 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 0); |
159 %! [b([2, 1], [3, 1]).name] = deal(b([1, 2], [1, 2]).name); | 161 %! [b([2, 1], [3, 1]).name] = deal (b([1, 2], [1, 2]).name); |
160 %! assert ({b.name}, {"e", "b", "b", "e", "d", "a"}); | 162 %! assert ({b.name}, {"e", "b", "b", "e", "d", "a"}); |
161 | 163 |
162 ## test internal ordering of struct array fields | 164 ## test internal ordering of struct array fields |
163 %!test | 165 %!test |
164 %! c(4, 4, 4, 4).value = 3; | 166 %! c(4, 4, 4, 4).value = 3; |
165 %! c(1, 2, 3, 4).value = 2; | 167 %! c(1, 2, 3, 4).value = 2; |
166 %! c(3, 3, 3, 3).value = 1; | 168 %! c(3, 3, 3, 3).value = 1; |
167 %! d = reshape ({c.value}, size(c)); | 169 %! d = reshape ({c.value}, size(c)); |
168 %! assert ([d{4, 4, 4, 4}, d{1, 2, 3, 4}, d{3, 3, 3, 3}], | 170 %! assert ([d{4, 4, 4, 4}, d{1, 2, 3, 4}, d{3, 3, 3, 3}], |
169 %! [3, 2, 1]); | 171 %! [3, 2, 1]); |
170 | 172 |
171 ## test assignment to mixed cs-list of field element subranges | 173 ## test assignment to mixed cs-list of field element subranges |
172 %!test | 174 %!test |
173 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 100); | 175 %! b = struct ("name", {"a", "b", "c"; "d", "e", "f"}, "value", 100); |
174 %! [b(1:2, [1, 3]).name, b(2, 1:3).value] = ... | 176 %! [b(1:2, [1, 3]).name, b(2, 1:3).value] = ... |
175 %! deal (1, 2, 3, 4, "5", "6", "7"); | 177 %! deal (1, 2, 3, 4, "5", "6", "7"); |
176 %! assert ({b.name}, {1, 2, "b", "e", 3, 4}); | 178 %! assert ({b.name}, {1, 2, "b", "e", 3, 4}); |
177 %! assert ({b.value}, {100, "5", 100, "6", 100, "7"}); | 179 %! assert ({b.value}, {100, "5", 100, "6", 100, "7"}); |
178 | 180 |
179 %!error <a cs-list cannot be further indexed> | 181 %!error <a cs-list cannot be further indexed> |
180 %! [a(1:3).x] = deal ([1, 5], [3, 7], [8, 9]); | 182 %! [a(1:3).x] = deal ([1, 5], [3, 7], [8, 9]); |
181 %! [a(2:3).x(2)] = deal (10, 11); | 183 %! [a(2:3).x(2)] = deal (10, 11); |
182 | 184 |
183 %!error <a cs-list cannot be further indexed> | 185 %!error <a cs-list cannot be further indexed> |
184 %! [a(1:3).x] = deal ([1, 5], [3, 7], [8, 9]); | 186 %! [a(1:3).x] = deal ([1, 5], [3, 7], [8, 9]); |
185 %! a(2:3).x(2); | 187 %! a(2:3).x(2); |
186 | 188 |
187 %!error id=Octave:index-out-of-bounds | 189 %!error id=Octave:index-out-of-bounds |
188 %! a(1).x.x = 1; | 190 %! a(1).x.x = 1; |
189 %! a(2).x; | 191 %! a(2).x; |
190 | 192 |
191 %!error <invalid number of output arguments for constant expression> | 193 %!error <invalid number of output arguments for constant expression> |
192 %! a = struct ("value", {1, 2, 3, 4, 5}); | 194 %! a = struct ("value", {1, 2, 3, 4, 5}); |
193 %! [a(2:4).value] = 1; | 195 %! [a(2:4).value] = 1; |
194 | 196 |
195 %!error <invalid assignment to cs-list outside multiple assignment> | 197 %!error <invalid assignment to cs-list outside multiple assignment> |
196 %! c(4, 4, 4, 4).name = "a"; | 198 %! c(4, 4, 4, 4).name = "a"; |
197 %! c(3, 3, 3, 3).value = 1; | 199 %! c(3, 3, 3, 3).value = 1; |
198 %! c([1, 3], 2, :, [3, 4]).value = 1; | 200 %! c([1, 3], 2, :, [3, 4]).value = 1; |
199 | 201 |
200 ## test lazy copying in structs: nested assignment to self | 202 ## test lazy copying in structs: nested assignment to self |
201 %!test | 203 %!test |
202 %! a.a = 1; | 204 %! a.a = 1; |
203 %! a.b = a; | 205 %! a.b = a; |
204 %! a.b.c = a; | 206 %! a.b.c = a; |
205 %! assert (a.b.c.b, struct ("a", 1)); | 207 %! assert (a.b.c.b, struct ("a", 1)); |
206 | 208 |
207 ## test lazy copying in structs: indirect nested assignment to self | 209 ## test lazy copying in structs: indirect nested assignment to self |
208 %!test | 210 %!test |
209 %! a.a = 1; | 211 %! a.a = 1; |
210 %! a.b = 2; | 212 %! a.b = 2; |
211 %! b.c = a; | 213 %! b.c = a; |
212 %! b.d = 3; | 214 %! b.d = 3; |
213 %! c.d = b; | 215 %! c.d = b; |
214 %! c.e = 4; | 216 %! c.e = 4; |
215 %! a.b = c; | 217 %! a.b = c; |
216 %! a.b.e = a; | 218 %! a.b.e = a; |
217 %! assert (a.b.e.b.d.c, struct ("a", 1, "b", 2)); | 219 %! assert (a.b.e.b.d.c, struct ("a", 1, "b", 2)); |
218 | 220 |
219 ## test lazy copying in structs: nested assignment via function | 221 ## test lazy copying in structs: nested assignment via function |
220 %!function aa = do_nest (a); | 222 %!function aa = do_nest (a); |
221 %! aa = a; | 223 %! aa = a; |
222 %! aa.b = a; | 224 %! aa.b = a; |
223 %! endfunction | 225 %!endfunction |
224 %!test | 226 %!test |
225 %! a.c = 1; | 227 %! a.c = 1; |
226 %! a = do_nest (a); | 228 %! a = do_nest (a); |
227 %! a = do_nest (a); | 229 %! a = do_nest (a); |
228 %! a = do_nest (a); | 230 %! a = do_nest (a); |
229 %! assert (a.b.b.b, struct ("c", 1)); | 231 %! assert (a.b.b.b, struct ("c", 1)); |
230 | 232 |
231 ## test lazy copying in structs: nested assignment via function | 233 ## test lazy copying in structs: nested assignment via function |
232 %!function aa = do_nest (a); | 234 %!function aa = do_nest (a); |
233 %! aa = a; | 235 %! aa = a; |
234 %! aa.b = a; | 236 %! aa.b = a; |
235 %! aa.b.c = aa; | 237 %! aa.b.c = aa; |
236 %! endfunction | 238 %!endfunction |
237 %!test | 239 %!test |
238 %! a.c = 1; | 240 %! a.c = 1; |
239 %! a = do_nest (a); | 241 %! a = do_nest (a); |
240 %! a = do_nest (a); | 242 %! a = do_nest (a); |
241 %! a = do_nest (a); | 243 %! a = do_nest (a); |
242 %! assert (a.b.c.b.b.c.b.b.c.b, struct ("c", 1)); | 244 %! assert (a.b.c.b.b.c.b.b.c.b, struct ("c", 1)); |
243 | 245 |
244 ## test lazy copying in structs: nested assignment on different levels. | 246 ## test lazy copying in structs: nested assignment on different levels. |
245 %!test | 247 %!test |
246 %! a.b = 1; | 248 %! a.b = 1; |
247 %! b.c = a; | 249 %! b.c = a; |
248 %! b.d.e = a; | 250 %! b.d.e = a; |
249 %! b.f.g.h = a; | 251 %! b.f.g.h = a; |
250 %! b.i.j.k.l = a; | 252 %! b.i.j.k.l = a; |
251 %! a.m = b; | 253 %! a.m = b; |
252 %! a.m.c.b = a; | 254 %! a.m.c.b = a; |
253 %! assert (a.m.c.b.m.i.j.k.l, struct ("b", 1)); | 255 %! assert (a.m.c.b.m.i.j.k.l, struct ("b", 1)); |
254 | 256 |
255 ## test indexed assignment into empty struct array | 257 ## test indexed assignment into empty struct array |
256 %!test | 258 %!test |
257 %! s = resize(struct(),3,2); | 259 %! s = resize (struct (), 3,2); |
258 %! s(3).foo = 42; | 260 %! s(3).foo = 42; |
259 %! assert (s(3), struct ("foo", 42)); | 261 %! assert (s(3), struct ("foo", 42)); |
260 | 262 |
261 %!error id=Octave:index-out-of-bounds | 263 %!error id=Octave:index-out-of-bounds |
262 %! s = resize(struct(),3,2); | 264 %! s = resize (struct (),3,2); |
263 %! s(3).foo = 42; | 265 %! s(3).foo = 42; |
264 %! s(7); | 266 %! s(7); |