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