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