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