7017
|
1 ## Copyright (C) 2005, 2006, 2007 Paul Kienzle |
5589
|
2 ## |
7016
|
3 ## This file is part of Octave. |
5589
|
4 ## |
7016
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
5589
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5589
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} test @var{name} |
|
21 ## @deftypefnx {Function File} {} test @var{name} quiet|normal|verbose |
|
22 ## @deftypefnx {Function File} {} test ('@var{name}', 'quiet|normal|verbose', @var{fid}) |
|
23 ## @deftypefnx {Function File} {} test ([], 'explain', @var{fid}) |
|
24 ## @deftypefnx {Function File} {@var{success} =} test (@dots{}) |
|
25 ## @deftypefnx {Function File} {[@var{n}, @var{max}] =} test (@dots{}) |
|
26 ## @deftypefnx {Function File} {[@var{code}, @var{idx}] =} test ('@var{name}','grabdemo') |
|
27 ## |
|
28 ## Perform tests from the first file in the loadpath matching @var{name}. |
|
29 ## @code{test} can be called as a command or as a function. Called with |
|
30 ## a single argument @var{name}, the tests are run interactively and stop |
|
31 ## after the first error is encountered. |
|
32 ## |
|
33 ## With a second argument the tests which are performed and the amount of |
|
34 ## output is selected. |
|
35 ## |
|
36 ## @table @asis |
|
37 ## @item 'quiet' |
|
38 ## Don't report all the tests as they happen, just the errors. |
|
39 ## |
|
40 ## @item 'normal' |
|
41 ## Report all tests as they happen, but don't do tests which require |
|
42 ## user interaction. |
|
43 ## |
|
44 ## @item 'verbose' |
|
45 ## Do tests which require user interaction. |
|
46 ## @end table |
|
47 ## |
|
48 ## The argument @var{fid} can be used to allow batch processing. Errors |
|
49 ## can be written to the already open file defined by @var{fid}, and |
|
50 ## hopefully when octave crashes this file will tell you what was happening |
|
51 ## when it did. You can use @code{stdout} if you want to see the results as |
|
52 ## they happen. You can also give a file name rather than an @var{fid}, in |
|
53 ## which case the contents of the file will be replaced with the log from |
|
54 ## the current test. |
|
55 ## |
|
56 ## Called with a single output argument @var{success}, @code{test} returns |
6730
|
57 ## true if all of the tests were successful. Called with two output arguments |
7001
|
58 ## @var{n} and @var{max}, the number of successful tests and the total number |
5589
|
59 ## of tests in the file @var{name} are returned. |
|
60 ## |
|
61 ## If the second argument is the string 'grabdemo', the contents of the demo |
|
62 ## blocks are extracted but not executed. Code for all code blocks is |
7001
|
63 ## concatenated and returned as @var{code} with @var{idx} being a vector of |
5589
|
64 ## positions of the ends of the demo blocks. |
|
65 ## |
|
66 ## If the second argument is 'explain', then @var{name} is ignored and an |
|
67 ## explanation of the line markers used is written to the file @var{fid}. |
5642
|
68 ## @seealso{error, assert, fail, demo, example} |
5589
|
69 ## @end deftypefn |
|
70 |
|
71 ## TODO: * Consider using keyword fail rather then error? This allows us |
|
72 ## TODO: to make a functional form of error blocks, which means we |
|
73 ## TODO: can include them in test sections which means that we can use |
|
74 ## TODO: octave flow control for both kinds of tests. |
|
75 |
|
76 ## PKG_ADD: mark_as_command test |
|
77 |
6730
|
78 function [__ret1, __ret2, __ret3] = test (__name, __flag, __fid) |
5589
|
79 ## information from test will be introduced by "key" |
|
80 persistent __signal_fail = "!!!!! "; |
|
81 persistent __signal_empty = "????? "; |
|
82 persistent __signal_block = " ***** "; |
|
83 persistent __signal_file = ">>>>> "; |
|
84 |
6730
|
85 __xfail = 0; |
|
86 |
6494
|
87 if (nargin < 2 || isempty (__flag)) |
5589
|
88 __flag = "quiet"; |
|
89 endif |
6494
|
90 if (nargin < 3) |
5589
|
91 __fid = []; |
|
92 endif |
6494
|
93 if (nargin < 1 || nargin > 3 |
|
94 || (! ischar (__name) && ! isempty (__name)) || ! ischar (__flag)) |
6046
|
95 print_usage (); |
5589
|
96 endif |
6494
|
97 if (isempty (__name) && (nargin != 3 || ! strcmp (__flag, "explain"))) |
6046
|
98 print_usage (); |
5589
|
99 endif |
6494
|
100 __batch = (! isempty (__fid)); |
5589
|
101 |
|
102 ## decide if error messages should be collected |
|
103 __close_fid = 0; |
|
104 if (__batch) |
6494
|
105 if (ischar (__fid)) |
|
106 __fid = fopen (__fid, "wt"); |
|
107 if (__fid < 0) |
|
108 error ("could not open log file"); |
|
109 endif |
5589
|
110 __close_fid = 1; |
|
111 endif |
|
112 fprintf (__fid, "%sprocessing %s\n", __signal_file, __name); |
5908
|
113 fflush (__fid); |
5589
|
114 else |
|
115 __fid = stdout; |
|
116 endif |
|
117 |
6494
|
118 if (strcmp (__flag, "normal")) |
5589
|
119 __grabdemo = 0; |
|
120 __rundemo = 0; |
|
121 __verbose = __batch; |
6494
|
122 elseif (strcmp (__flag, "quiet")) |
5589
|
123 __grabdemo = 0; |
|
124 __rundemo = 0; |
|
125 __verbose = 0; |
6494
|
126 elseif (strcmp (__flag, "verbose")) |
5589
|
127 __grabdemo = 0; |
|
128 __rundemo = 1; |
|
129 __verbose = 1; |
6494
|
130 elseif (strcmp (__flag, "grabdemo")) |
5589
|
131 __grabdemo = 1; |
|
132 __rundemo = 0; |
|
133 __verbose = 0; |
|
134 __demo_code = ""; |
|
135 __demo_idx = 1; |
6494
|
136 elseif (strcmp (__flag, "explain")) |
|
137 fprintf (__fid, "# %s new test file\n", __signal_file); |
|
138 fprintf (__fid, "# %s no tests in file\n", __signal_empty); |
|
139 fprintf (__fid, "# %s test had an unexpected result\n", __signal_fail); |
|
140 fprintf (__fid, "# %s code for the test\n", __signal_block); |
5589
|
141 fprintf (__fid, "# Search for the unexpected results in the file\n"); |
|
142 fprintf (__fid, "# then page back to find the file name which caused it.\n"); |
|
143 fprintf (__fid, "# The result may be an unexpected failure (in which\n"); |
|
144 fprintf (__fid, "# case an error will be reported) or an unexpected\n"); |
|
145 fprintf (__fid, "# success (in which case no error will be reported).\n"); |
5908
|
146 fflush (__fid); |
6494
|
147 if (__close_fid) |
|
148 fclose(__fid); |
|
149 endif |
5589
|
150 return; |
|
151 else |
6494
|
152 error ("test unknown flag '%s'", __flag); |
5589
|
153 endif |
|
154 |
|
155 ## locate the file to test |
6249
|
156 __file = file_in_loadpath (__name, "all"); |
|
157 if (isempty (__file)) |
6494
|
158 __file = file_in_loadpath (strcat (__name, ".m"), "all"); |
5589
|
159 endif |
|
160 if (isempty (__file)) |
6494
|
161 __file = file_in_loadpath (strcat (__name, ".cc"), "all"); |
6249
|
162 endif |
|
163 if (iscell (__file)) |
6365
|
164 ## If repeats, return first in path. |
|
165 if (isempty (__file)) |
|
166 __file = ""; |
|
167 else |
|
168 __file = __file{1}; |
|
169 endif |
5589
|
170 endif |
|
171 if (isempty (__file)) |
|
172 if (__grabdemo) |
|
173 __ret1 = ""; |
|
174 __ret2 = []; |
|
175 else |
6494
|
176 fprintf (__fid, "%s%s does not exist in path\n", __signal_empty, __name); |
5908
|
177 fflush (__fid); |
6494
|
178 if (nargout > 0) |
|
179 __ret1 = __ret2 = 0; |
|
180 endif |
5589
|
181 endif |
6494
|
182 if (__close_fid) |
|
183 fclose(__fid); |
|
184 endif |
5589
|
185 return; |
|
186 endif |
|
187 |
|
188 ## grab the test code from the file |
|
189 __body = __extract_test_code (__file); |
|
190 |
|
191 if (isempty (__body)) |
|
192 if (__grabdemo) |
|
193 __ret1 = ""; |
|
194 __ret2 = []; |
|
195 else |
6494
|
196 fprintf (__fid, "%s%s has no tests available\n", __signal_empty, __file); |
5908
|
197 fflush (__fid); |
6494
|
198 if (nargout > 0) |
|
199 __ret1 = __ret2 = 0; |
|
200 endif |
5589
|
201 endif |
6494
|
202 if (__close_fid) |
|
203 fclose(__fid); |
|
204 endif |
5589
|
205 return; |
|
206 else |
|
207 ## add a dummy comment block to the end for ease of indexing |
|
208 if (__body (length(__body)) == "\n") |
6494
|
209 __body = sprintf ("\n%s#", __body); |
5589
|
210 else |
6494
|
211 __body = sprintf ("\n%s\n#", __body); |
5589
|
212 endif |
|
213 endif |
|
214 |
|
215 ## chop it up into blocks for evaluation |
6494
|
216 __lineidx = find (__body == "\n"); |
|
217 __blockidx = __lineidx(find (! isspace (__body(__lineidx+1))))+1; |
5589
|
218 |
|
219 ## ready to start tests ... if in batch mode, tell us what is happening |
|
220 if (__verbose) |
6494
|
221 disp (strcat ( __signal_file, __file)); |
5589
|
222 endif |
|
223 |
|
224 ## assume all tests will pass |
|
225 __all_success = 1; |
|
226 |
|
227 ## process each block separately, initially with no shared variables |
|
228 __tests = __successes = 0; |
|
229 __shared = " "; |
|
230 __shared_r = " "; |
|
231 __clear = ""; |
6494
|
232 for __i = 1:length(__blockidx)-1 |
5589
|
233 |
|
234 ## extract the block |
|
235 __block = __body(__blockidx(__i):__blockidx(__i+1)-2); |
|
236 |
|
237 ## let the user/logfile know what is happening |
|
238 if (__verbose) |
|
239 fprintf (__fid, "%s%s\n", __signal_block, __block); |
5908
|
240 fflush (__fid); |
5589
|
241 endif |
|
242 |
|
243 ## split __block into __type and __code |
6494
|
244 __idx = find (! isletter (__block)); |
|
245 if (isempty (__idx)) |
5589
|
246 __type = __block; |
|
247 __code = ""; |
|
248 else |
|
249 __type = __block(1:__idx(1)-1); |
|
250 __code = __block(__idx(1):length(__block)); |
|
251 endif |
|
252 |
|
253 ## assume the block will succeed; |
|
254 __success = 1; |
|
255 __msg = []; |
|
256 |
|
257 ## DEMO |
|
258 ## If in __grabdemo mode, then don't process any other block type. |
|
259 ## So that the other block types don't have to worry about |
|
260 ## this __grabdemo mode, the demo block processor grabs all block |
|
261 ## types and skips those which aren't demo blocks. |
|
262 __isdemo = strcmp (__type, "demo"); |
|
263 if (__grabdemo || __isdemo) |
|
264 __istest = 0; |
|
265 |
|
266 if (__grabdemo && __isdemo) |
|
267 if (isempty(__demo_code)) |
|
268 __demo_code = __code; |
6494
|
269 __demo_idx = [1, length(__demo_code)+1]; |
5589
|
270 else |
|
271 __demo_code = strcat(__demo_code, __code); |
6494
|
272 __demo_idx = [__demo_idx, length(__demo_code)+1]; |
5589
|
273 endif |
|
274 |
|
275 elseif (__rundemo && __isdemo) |
|
276 try |
|
277 ## process the code in an environment without variables |
6494
|
278 eval (sprintf ("function __test__()\n%s\nendfunction", __code)); |
5589
|
279 __test__; |
6494
|
280 input ("Press <enter> to continue: ", "s"); |
5589
|
281 catch |
|
282 __success = 0; |
6494
|
283 __msg = sprintf ("%sdemo failed\n%s", __signal_fail, __error_text__); |
5589
|
284 end_try_catch |
|
285 clear __test__; |
|
286 |
|
287 endif |
|
288 __code = ""; # code already processed |
|
289 |
|
290 ## SHARED |
|
291 elseif strcmp (__type, "shared") |
|
292 __istest = 0; |
|
293 |
|
294 ## separate initialization code from variables |
6494
|
295 __idx = find (__code == "\n"); |
|
296 if (isempty (__idx)) |
5589
|
297 __vars = __code; |
|
298 __code = ""; |
|
299 else |
|
300 __vars = __code (1:__idx(1)-1); |
|
301 __code = __code (__idx(1):length(__code)); |
|
302 endif |
|
303 |
|
304 ## strip comments off the variables |
6730
|
305 __idx = find (__vars == "%" | __vars == "#"); |
6494
|
306 if (! isempty (__idx)) |
5589
|
307 __vars = __vars(1:__idx(1)-1); |
|
308 endif |
|
309 |
|
310 ## assign default values to variables |
|
311 try |
6494
|
312 __vars = deblank (__vars); |
|
313 if (! isempty (__vars)) |
|
314 eval (strcat (strrep (__vars, ",", "=[];"), "=[];")); |
5589
|
315 __shared = __vars; |
6494
|
316 __shared_r = strcat ("[ ", __vars, "] = "); |
5589
|
317 else |
|
318 __shared = " "; |
|
319 __shared_r = " "; |
|
320 endif |
|
321 catch |
|
322 __code = ""; # couldn't declare, so don't initialize |
|
323 __success = 0; |
6494
|
324 __msg = sprintf ("%sshared variable initialization failed\n", |
|
325 __signal_fail); |
5589
|
326 end_try_catch |
|
327 |
|
328 ## clear shared function definitions |
6494
|
329 eval (__clear, ""); |
|
330 __clear = ""; |
5589
|
331 |
|
332 ## initialization code will be evaluated below |
|
333 |
|
334 ## FUNCTION |
6494
|
335 elseif (strcmp (__type, "function")) |
5589
|
336 __istest = 0; |
|
337 persistent __fn = 0; |
6494
|
338 __name_position = function_name (__block); |
|
339 if (isempty (__name_position)) |
5589
|
340 __success = 0; |
6494
|
341 __msg = sprintf ("%stest failed: missing function name\n", |
|
342 __signal_fail); |
5589
|
343 else |
|
344 __name = __block(__name_position(1):__name_position(2)); |
|
345 __code = __block; |
|
346 try |
|
347 eval(__code); ## Define the function |
6494
|
348 __clear = sprintf ("%sclear %s;\n", __clear, __name); |
5589
|
349 catch |
|
350 __success = 0; |
6494
|
351 __msg = sprintf ("%stest failed: syntax error\n%s", |
|
352 __signal_fail, __error_text__); |
5589
|
353 end_try_catch |
|
354 endif |
|
355 __code = ""; |
|
356 |
|
357 |
|
358 ## ASSERT/FAIL |
6494
|
359 elseif (strcmp (__type, "assert") || strcmp (__type, "fail")) |
5589
|
360 __istest = 1; |
|
361 __code = __block; # put the keyword back on the code |
|
362 ## the code will be evaluated below as a test block |
|
363 |
|
364 ## ERROR/WARNING |
6494
|
365 elseif (strcmp (__type, "error") || strcmp(__type, "warning")) |
5589
|
366 __istest = 1; |
6494
|
367 __warning = strcmp (__type, "warning"); |
|
368 [__pattern, __code] = getpattern (__code); |
5589
|
369 try |
6494
|
370 eval (sprintf ("function __test__(%s)\n%s\nendfunction", |
|
371 __shared, __code)); |
5589
|
372 catch |
|
373 __success = 0; |
6494
|
374 __msg = sprintf ("%stest failed: syntax error\n%s", |
|
375 __signal_fail, __error_text__); |
5589
|
376 end_try_catch |
|
377 |
|
378 if (__success) |
|
379 __success = 0; |
6494
|
380 __warnstate = warning ("query", "quiet"); |
|
381 warning ("on", "quiet"); |
5589
|
382 try |
6494
|
383 eval (sprintf ("__test__(%s);", __shared)); |
|
384 __err = trimerr (lastwarn, "warning"); |
|
385 warning (__warnstate.state, "quiet"); |
5589
|
386 |
6494
|
387 if (! __warning) |
|
388 __msg = sprintf ("%sexpected <%s> but got no error\n", |
|
389 __signal_fail, __pattern); |
|
390 elseif (isempty (__err)) |
|
391 __msg = sprintf ("%sexpected <%s> but got no warning\n", |
|
392 __signal_fail, __pattern); |
|
393 elseif (isempty (regexp (__err, __pattern, "once"))) |
|
394 __msg = sprintf ("%sexpected <%s> but got %s\n", |
5589
|
395 __signal_fail, __pattern, __err); |
|
396 else |
|
397 __success = 1; |
|
398 endif |
|
399 |
|
400 catch |
6494
|
401 __err = trimerr (lasterr, "error"); |
|
402 warning (__warnstate.state, "quiet"); |
|
403 if (__warning) |
|
404 __msg = sprintf ("%sexpected warning <%s> but got error %s\n", |
|
405 __signal_fail, __pattern, __err); |
|
406 elseif (isempty (regexp (__err, __pattern, "once"))) |
|
407 __msg = sprintf ("%sexpected <%s> but got %s\n", |
|
408 __signal_fail, __pattern, __err); |
5589
|
409 else |
|
410 __success = 1; |
|
411 endif |
|
412 end_try_catch |
|
413 clear __test__; |
|
414 endif |
|
415 __code = ""; # code already processed |
|
416 |
|
417 ## TEST |
6728
|
418 elseif (strcmp (__type, "test") || strcmp (__type, "xtest")) |
5589
|
419 __istest = 1; |
|
420 ## code will be evaluated below |
|
421 |
|
422 ## comment block |
6494
|
423 elseif (strcmp (__block(1:1), "#")) |
5589
|
424 __istest = 0; |
|
425 __code = ""; # skip the code |
|
426 |
|
427 else |
|
428 ## unknown block |
|
429 __istest = 1; |
|
430 __success = 0; |
6494
|
431 __msg = sprintf ("%sunknown test type!\n", __signal_fail); |
5589
|
432 __code = ""; # skip the code |
|
433 endif |
|
434 |
|
435 ## evaluate code for test, shared, and assert. |
6494
|
436 if (! isempty(__code)) |
5589
|
437 try |
6494
|
438 eval (sprintf ("function %s__test__(%s)\n%s\nendfunction", |
|
439 __shared_r,__shared, __code)); |
|
440 eval (sprintf ("%s__test__(%s);", __shared_r, __shared)); |
5589
|
441 catch |
6728
|
442 if (strcmp (__type, "xtest")) |
|
443 __msg = sprintf ("%sknown failure\n%s", __signal_fail, __error_text__); |
6730
|
444 __xfail++; |
6728
|
445 else |
|
446 __msg = sprintf ("%stest failed\n%s", __signal_fail, __error_text__); |
|
447 __success = 0; |
|
448 endif |
6494
|
449 if (isempty (__error_text__)) |
|
450 error ("empty error text, probably Ctrl-C --- aborting"); |
5589
|
451 endif |
|
452 end_try_catch |
|
453 clear __test__; |
|
454 endif |
|
455 |
|
456 ## All done. Remember if we were successful and print any messages |
6494
|
457 if (! isempty (__msg)) |
5589
|
458 ## make sure the user knows what caused the error |
6494
|
459 if (! __verbose) |
5589
|
460 fprintf (__fid, "%s%s\n", __signal_block, __block); |
5908
|
461 fflush (__fid); |
5589
|
462 endif |
|
463 fputs (__fid, __msg); |
5908
|
464 fflush (__fid); |
5589
|
465 ## show the variable context |
6494
|
466 if (! strcmp (__type, "error") && ! all (__shared == " ")) |
|
467 fputs (__fid, "shared variables "); |
|
468 eval (sprintf ("fdisp(__fid,bundle(%s));", __shared)); |
5908
|
469 fflush (__fid); |
5589
|
470 endif |
|
471 endif |
|
472 if (__success == 0) |
|
473 __all_success = 0; |
|
474 ## stop after one error if not in batch mode |
6494
|
475 if (! __batch) |
|
476 if (nargout > 0) |
|
477 __ret1 = __ret2 = 0; |
|
478 endif |
|
479 if (__close_fid) |
|
480 fclose(__fid); |
|
481 endif |
5589
|
482 return; |
|
483 endif |
|
484 endif |
|
485 __tests += __istest; |
6494
|
486 __successes += __success * __istest; |
5589
|
487 endfor |
6494
|
488 eval (__clear, ""); |
5589
|
489 |
|
490 if (nargout == 0) |
6730
|
491 if (__xfail) |
|
492 printf ("PASSES %d out of %d tests (%d expected failures)\n", |
|
493 __successes, __tests, __xfail); |
|
494 else |
|
495 printf ("PASSES %d out of %d tests\n", __successes, __tests); |
|
496 endif |
5589
|
497 elseif (__grabdemo) |
|
498 __ret1 = __demo_code; |
|
499 __ret2 = __demo_idx; |
6494
|
500 elseif (nargout == 1) |
5589
|
501 __ret1 = __all_success; |
|
502 else |
|
503 __ret1 = __successes; |
|
504 __ret2 = __tests; |
6730
|
505 __ret3 = __xfail; |
5589
|
506 endif |
|
507 endfunction |
|
508 |
|
509 ## create structure with fieldnames the name of the input variables |
6494
|
510 function s = varstruct (varargin) |
|
511 for i = 1:nargin |
|
512 s.(deblank (argn(i,:))) = varargin{i}; |
5589
|
513 endfor |
|
514 endfunction |
|
515 |
|
516 ## find [start,end] of fn in 'function [a,b] = fn' |
6494
|
517 function pos = function_name (def) |
5589
|
518 pos = []; |
|
519 |
|
520 ## Find the end of the name |
6494
|
521 right = find (def == "(", 1); |
|
522 if (isempty (right)) |
|
523 return; |
|
524 endif |
|
525 right = find (def(1:right-1) != " ", 1, "last"); |
5589
|
526 |
|
527 ## Find the beginning of the name |
6494
|
528 left = max ([find(def(1:right)==" ", 1, "last"), ... |
|
529 find(def(1:right)=="=", 1, "last")]); |
|
530 if (isempty (left)) |
|
531 return; |
|
532 endif |
5589
|
533 left++; |
|
534 |
|
535 ## Return the end points of the name |
6494
|
536 pos = [left, right]; |
5589
|
537 endfunction |
|
538 |
|
539 ## strip <pattern> from '<pattern> code' |
6494
|
540 function [pattern, rest] = getpattern (str) |
|
541 pattern = "."; |
5589
|
542 rest = str; |
6494
|
543 str = trimleft (str); |
|
544 if (! isempty (str) && str(1) == "<") |
|
545 close = index (str, ">"); |
|
546 if (close) |
5589
|
547 pattern = str(2:close-1); |
|
548 rest = str(close+1:end); |
|
549 endif |
|
550 endif |
|
551 endfunction |
|
552 |
|
553 ## strip '.*prefix:' from '.*prefix: msg\n' and strip trailing blanks |
6494
|
554 function msg = trimerr (msg, prefix) |
|
555 idx = index (msg, strcat (prefix, ":")); |
|
556 if (idx > 0) |
|
557 msg(1:idx+length(prefix)) = []; |
|
558 endif |
|
559 msg = trimleft (deblank (msg)); |
5589
|
560 endfunction |
|
561 |
|
562 ## strip leading blanks from string |
6494
|
563 function str = trimleft (str) |
|
564 idx = find (isspace (str)); |
|
565 leading = find (idx == 1:length(idx)); |
|
566 if (! isempty (leading)) |
5589
|
567 str = str(leading(end)+1:end); |
|
568 endif |
|
569 endfunction |
|
570 |
|
571 ## make a structure out of the named variables |
|
572 ## (based on Etienne Grossmann's tar function) |
6494
|
573 function s = bundle (varargin) |
|
574 for i = 1:nargin |
|
575 s.(deblank (argn(i,:))) = varargin{i}; |
5589
|
576 end |
|
577 endfunction |
|
578 |
|
579 function body = __extract_test_code (nm) |
|
580 fid = fopen (nm, "rt"); |
|
581 body = []; |
|
582 if (fid >= 0) |
6494
|
583 while (! feof (fid)) |
5589
|
584 ln = fgetl (fid); |
6494
|
585 if (length (ln) >= 2 && strcmp (ln(1:2), "%!")) |
5589
|
586 body = [body, "\n"]; |
|
587 if (length(ln) > 2) |
6494
|
588 body = strcat (body, ln(3:end)); |
5589
|
589 endif |
|
590 endif |
|
591 endwhile |
|
592 fclose (fid); |
|
593 endif |
|
594 endfunction |
|
595 |
6728
|
596 ### Test for a known failure |
|
597 %!xtest error("This test is known to fail") |
|
598 |
5589
|
599 ### example from toeplitz |
|
600 %!shared msg |
|
601 %! msg="expecting vector arguments"; |
|
602 %!fail ('toeplitz([])', msg); |
|
603 %!fail ('toeplitz([1,2],[])', msg); |
|
604 %!fail ('toeplitz([1,2;3,4])', msg); |
|
605 %!fail ('toeplitz([1,2],[1,2;3,4])', msg); |
|
606 %!fail ('toeplitz ([1,2;3,4],[1,2])', msg); |
|
607 % !fail ('toeplitz','usage: toeplitz'); # usage doesn't generate an error |
|
608 % !fail ('toeplitz(1, 2, 3)', 'usage: toeplitz'); |
|
609 %!test assert (toeplitz ([1,2,3], [1,4]), [1,4; 2,1; 3,2]); |
|
610 %!demo toeplitz ([1,2,3,4],[1,5,6]) |
|
611 |
|
612 ### example from kron |
5775
|
613 %!#error kron # FIXME suppress these until we can handle output |
5589
|
614 %!#error kron(1,2,3) |
|
615 %!test assert (isempty (kron ([], rand(3, 4)))) |
|
616 %!test assert (isempty (kron (rand (3, 4), []))) |
|
617 %!test assert (isempty (kron ([], []))) |
|
618 %!shared A, B |
|
619 %!test |
|
620 %! A = [1, 2, 3; 4, 5, 6]; |
|
621 %! B = [1, -1; 2, -2]; |
|
622 %!assert (size (kron (zeros (3, 0), A)), [ 3*rows(A), 0 ]) |
|
623 %!assert (size (kron (zeros (0, 3), A)), [ 0, 3*columns(A) ]) |
|
624 %!assert (size (kron (A, zeros (3, 0))), [ 3*rows(A), 0 ]) |
|
625 %!assert (size (kron (A, zeros (0, 3))), [ 0, 3*columns(A) ]) |
|
626 %!assert (kron (pi, e), pi*e) |
|
627 %!assert (kron (pi, A), pi*A) |
|
628 %!assert (kron (A, e), e*A) |
|
629 %!assert (kron ([1, 2, 3], A), [ A, 2*A, 3*A ]) |
|
630 %!assert (kron ([1; 2; 3], A), [ A; 2*A; 3*A ]) |
|
631 %!assert (kron ([1, 2; 3, 4], A), [ A, 2*A; 3*A, 4*A ]) |
|
632 %!test |
|
633 %! res = [1,-1,2,-2,3,-3; 2,-2,4,-4,6,-6; 4,-4,5,-5,6,-6; 8,-8,10,-10,12,-12]; |
|
634 %! assert (kron (A, B), res) |
|
635 |
|
636 ### an extended demo from specgram |
|
637 %!#demo |
|
638 %! ## Speech spectrogram |
|
639 %! [x, Fs] = auload(file_in_loadpath("sample.wav")); # audio file |
|
640 %! step = fix(5*Fs/1000); # one spectral slice every 5 ms |
|
641 %! window = fix(40*Fs/1000); # 40 ms data window |
|
642 %! fftn = 2^nextpow2(window); # next highest power of 2 |
|
643 %! [S, f, t] = specgram(x, fftn, Fs, window, window-step); |
|
644 %! S = abs(S(2:fftn*4000/Fs,:)); # magnitude in range 0<f<=4000 Hz. |
|
645 %! S = S/max(max(S)); # normalize magnitude so that max is 0 dB. |
|
646 %! S = max(S, 10^(-40/10)); # clip below -40 dB. |
|
647 %! S = min(S, 10^(-3/10)); # clip above -3 dB. |
|
648 %! imagesc(flipud(20*log10(S)), 1); |
|
649 %! % you should now see a spectrogram in the image window |
|
650 |
|
651 |
|
652 ### now test test itself |
|
653 |
|
654 %!## usage and error testing |
|
655 % !fail ('test','usage.*test') # no args, generates usage() |
|
656 % !fail ('test(1,2,3,4)','usage.*test') # too many args, generates usage() |
|
657 %!fail ('test("test", "bogus")','unknown flag') # incorrect args |
|
658 %!fail ('garbage','garbage.*undefined') # usage on nonexistent function should be |
|
659 |
|
660 %!error <usage.*test> test # no args, generates usage() |
|
661 %!error <usage.*test> test(1,2,3,4) # too many args, generates usage() |
|
662 %!error <unknown flag> test("test", 'bogus'); # incorrect args, generates error() |
|
663 %!error <garbage' undefined> garbage # usage on nonexistent function should be |
|
664 |
|
665 %!error test("test", 'bogus'); # test without pattern |
|
666 |
5681
|
667 %!test |
|
668 %! lastwarn(); # clear last warning just in case |
|
669 |
5781
|
670 %!warning <warning message> warning('warning message'); |
5589
|
671 |
|
672 %!## test of shared variables |
|
673 %!shared a # create a shared variable |
|
674 %!test a=3; # assign to a shared variable |
|
675 %!test assert(a,3) # variable should equal 3 |
|
676 %!shared b,c # replace shared variables |
|
677 %!test assert (!exist("a")); # a no longer exists |
|
678 %!test assert (isempty(b)); # variables start off empty |
|
679 %!shared a,b,c # recreate a shared variable |
|
680 %!test assert (isempty(a)); # value is empty even if it had a previous value |
|
681 %!test a=1; b=2; c=3; # give values to all variables |
|
682 %!test assert ([a,b,c],[1,2,3]); # test all of them together |
|
683 %!test c=6; # update a value |
|
684 %!test assert([a, b, c],[1, 2, 6]); # show that the update sticks |
|
685 %!shared # clear all shared variables |
|
686 %!test assert(!exist("a")) # show that they are cleared |
|
687 %!shared a,b,c # support for initializer shorthand |
|
688 %! a=1; b=2; c=4; |
|
689 |
|
690 %!function x = __test_a(y) |
|
691 %! x = 2*y; |
|
692 %!assert(__test_a(2),4); # Test a test function |
|
693 |
|
694 %!function __test_a (y) |
|
695 %! x = 2*y; |
|
696 %!test |
|
697 %! __test_a(2); # Test a test function with no return value |
|
698 |
|
699 %!function [x,z] = __test_a (y) |
|
700 %! x = 2*y; |
|
701 %! z = 3*y; |
|
702 %!test # Test a test function with multiple returns |
|
703 %! [x,z] = __test_a(3); |
|
704 %! assert(x,6); |
|
705 %! assert(z,9); |
|
706 |
|
707 %!## test of assert block |
|
708 %!assert (isempty([])) # support for test assert shorthand |
|
709 |
|
710 %!## demo blocks |
|
711 %!demo # multiline demo block |
|
712 %! t=[0:0.01:2*pi]; x=sin(t); |
|
713 %! plot(t,x); |
|
714 %! % you should now see a sine wave in your figure window |
|
715 %!demo a=3 # single line demo blocks work too |
|
716 |
|
717 %!## this is a comment block. it can contain anything. |
|
718 %!## |
|
719 %! it is the "#" as the block type that makes it a comment |
|
720 %! and it stays as a comment even through continuation lines |
|
721 %! which means that it works well with commenting out whole tests |
|
722 |
|
723 % !# failure tests. All the following should fail. These tests should |
|
724 % !# be disabled unless you are developing test() since users don't |
|
725 % !# like to be presented with expected failures. I use % ! to disable. |
|
726 % !test error("---------Failure tests. Use test('test','verbose',1)"); |
|
727 % !test assert([a,b,c],[1,3,6]); # variables have wrong values |
|
728 % !bogus # unknown block type |
|
729 % !error toeplitz([1,2,3]); # correct usage |
|
730 % !test syntax errors) # syntax errors fail properly |
|
731 % !shared garbage in # variables must be comma separated |
|
732 % !error syntax++error # error test fails on syntax errors |
|
733 % !error "succeeds."; # error test fails if code succeeds |
|
734 % !error <wrong pattern> error("message") # error pattern must match |
|
735 % !demo with syntax error # syntax errors in demo fail properly |
|
736 % !shared a,b,c |
|
737 % !demo # shared variables not available in demo |
|
738 % ! assert(exist("a")) |
|
739 % !error |
|
740 % ! test('/etc/passwd'); |
|
741 % ! test("nonexistent file"); |
|
742 % ! ## These don't signal an error, so the test for an error fails. Note |
|
743 % ! ## that the call doesn't reference the current fid (it is unavailable), |
|
744 % ! ## so of course the informational message is not printed in the log. |