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