Mercurial > octave-antonio
comparison scripts/testfun/test.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | 3c6e8aaa9555 |
children | 890e89c3dfeb |
comparison
equal
deleted
inserted
replaced
11586:12df7854fa7c | 11587:c792872f8942 |
---|---|
24 ## @deftypefnx {Function File} {@var{success} =} test (@dots{}) | 24 ## @deftypefnx {Function File} {@var{success} =} test (@dots{}) |
25 ## @deftypefnx {Function File} {[@var{n}, @var{max}] =} test (@dots{}) | 25 ## @deftypefnx {Function File} {[@var{n}, @var{max}] =} test (@dots{}) |
26 ## @deftypefnx {Function File} {[@var{code}, @var{idx}] =} test ('@var{name}', 'grabdemo') | 26 ## @deftypefnx {Function File} {[@var{code}, @var{idx}] =} test ('@var{name}', 'grabdemo') |
27 ## | 27 ## |
28 ## Perform tests from the first file in the loadpath matching @var{name}. | 28 ## Perform tests from the first file in the loadpath matching @var{name}. |
29 ## @code{test} can be called as a command or as a function. Called with | 29 ## @code{test} can be called as a command or as a function. Called with |
30 ## a single argument @var{name}, the tests are run interactively and stop | 30 ## a single argument @var{name}, the tests are run interactively and stop |
31 ## after the first error is encountered. | 31 ## after the first error is encountered. |
32 ## | 32 ## |
33 ## With a second argument the tests which are performed and the amount of | 33 ## With a second argument the tests which are performed and the amount of |
34 ## output is selected. | 34 ## output is selected. |
36 ## @table @asis | 36 ## @table @asis |
37 ## @item 'quiet' | 37 ## @item 'quiet' |
38 ## Don't report all the tests as they happen, just the errors. | 38 ## Don't report all the tests as they happen, just the errors. |
39 ## | 39 ## |
40 ## @item 'normal' | 40 ## @item 'normal' |
41 ## Report all tests as they happen, but don't do tests which require | 41 ## Report all tests as they happen, but don't do tests which require |
42 ## user interaction. | 42 ## user interaction. |
43 ## | 43 ## |
44 ## @item 'verbose' | 44 ## @item 'verbose' |
45 ## Do tests which require user interaction. | 45 ## Do tests which require user interaction. |
46 ## @end table | 46 ## @end table |
47 ## | 47 ## |
48 ## The argument @var{fid} can be used to allow batch processing. Errors | 48 ## The argument @var{fid} can be used to allow batch processing. Errors |
49 ## can be written to the already open file defined by @var{fid}, and | 49 ## can be written to the already open file defined by @var{fid}, and |
50 ## hopefully when Octave crashes this file will tell you what was happening | 50 ## hopefully when Octave crashes this file will tell you what was happening |
51 ## when it did. You can use @code{stdout} if you want to see the results as | 51 ## when it did. You can use @code{stdout} if you want to see the results as |
52 ## they happen. You can also give a file name rather than an @var{fid}, in | 52 ## they happen. You can also give a file name rather than an @var{fid}, in |
53 ## which case the contents of the file will be replaced with the log from | 53 ## which case the contents of the file will be replaced with the log from |
54 ## the current test. | 54 ## the current test. |
55 ## | 55 ## |
56 ## Called with a single output argument @var{success}, @code{test} returns | 56 ## Called with a single output argument @var{success}, @code{test} returns |
57 ## true if all of the tests were successful. Called with two output arguments | 57 ## true if all of the tests were successful. Called with two output arguments |
58 ## @var{n} and @var{max}, the number of successful tests and the total number | 58 ## @var{n} and @var{max}, the number of successful tests and the total number |
86 | 86 |
87 if (nargin < 2 || isempty (__flag)) | 87 if (nargin < 2 || isempty (__flag)) |
88 __flag = "quiet"; | 88 __flag = "quiet"; |
89 endif | 89 endif |
90 if (nargin < 3) | 90 if (nargin < 3) |
91 __fid = []; | 91 __fid = []; |
92 endif | 92 endif |
93 if (nargin < 1 || nargin > 3 | 93 if (nargin < 1 || nargin > 3 |
94 || (! ischar (__name) && ! isempty (__name)) || ! ischar (__flag)) | 94 || (! ischar (__name) && ! isempty (__name)) || ! ischar (__flag)) |
95 print_usage (); | 95 print_usage (); |
96 endif | 96 endif |
204 endif | 204 endif |
205 return; | 205 return; |
206 else | 206 else |
207 ## Add a dummy comment block to the end for ease of indexing. | 207 ## Add a dummy comment block to the end for ease of indexing. |
208 if (__body (length(__body)) == "\n") | 208 if (__body (length(__body)) == "\n") |
209 __body = sprintf ("\n%s#", __body); | 209 __body = sprintf ("\n%s#", __body); |
210 else | 210 else |
211 __body = sprintf ("\n%s\n#", __body); | 211 __body = sprintf ("\n%s\n#", __body); |
212 endif | 212 endif |
213 endif | 213 endif |
214 | 214 |
215 ## Chop it up into blocks for evaluation. | 215 ## Chop it up into blocks for evaluation. |
216 __lineidx = find (__body == "\n"); | 216 __lineidx = find (__body == "\n"); |
287 clear __test__; | 287 clear __test__; |
288 | 288 |
289 endif | 289 endif |
290 ## Code already processed. | 290 ## Code already processed. |
291 __code = ""; | 291 __code = ""; |
292 | 292 |
293 ### SHARED | 293 ### SHARED |
294 | 294 |
295 elseif (strcmp (__type, "shared")) | 295 elseif (strcmp (__type, "shared")) |
296 __istest = 0; | 296 __istest = 0; |
297 | 297 |
302 __code = ""; | 302 __code = ""; |
303 else | 303 else |
304 __vars = __code (1:__idx(1)-1); | 304 __vars = __code (1:__idx(1)-1); |
305 __code = __code (__idx(1):length(__code)); | 305 __code = __code (__idx(1):length(__code)); |
306 endif | 306 endif |
307 | 307 |
308 ## Strip comments off the variables. | 308 ## Strip comments off the variables. |
309 __idx = find (__vars == "%" | __vars == "#"); | 309 __idx = find (__vars == "%" | __vars == "#"); |
310 if (! isempty (__idx)) | 310 if (! isempty (__idx)) |
311 __vars = __vars(1:__idx(1)-1); | 311 __vars = __vars(1:__idx(1)-1); |
312 endif | 312 endif |
313 | 313 |
314 ## Assign default values to variables. | 314 ## Assign default values to variables. |
315 try | 315 try |
316 __vars = deblank (__vars); | 316 __vars = deblank (__vars); |
317 if (! isempty (__vars)) | 317 if (! isempty (__vars)) |
318 eval (cstrcat (strrep (__vars, ",", "=[];"), "=[];")); | 318 eval (cstrcat (strrep (__vars, ",", "=[];"), "=[];")); |
331 end_try_catch | 331 end_try_catch |
332 | 332 |
333 ## Clear shared function definitions. | 333 ## Clear shared function definitions. |
334 eval (__clear, ""); | 334 eval (__clear, ""); |
335 __clear = ""; | 335 __clear = ""; |
336 | 336 |
337 ## Initialization code will be evaluated below. | 337 ## Initialization code will be evaluated below. |
338 | 338 |
339 ### FUNCTION | 339 ### FUNCTION |
340 | 340 |
341 elseif (strcmp (__type, "function")) | 341 elseif (strcmp (__type, "function")) |
342 __istest = 0; | 342 __istest = 0; |
343 persistent __fn = 0; | 343 persistent __fn = 0; |
357 __msg = sprintf ("%stest failed: syntax error\n%s", | 357 __msg = sprintf ("%stest failed: syntax error\n%s", |
358 __signal_fail, __error_text__); | 358 __signal_fail, __error_text__); |
359 end_try_catch | 359 end_try_catch |
360 endif | 360 endif |
361 __code = ""; | 361 __code = ""; |
362 | 362 |
363 ### ASSERT/FAIL | 363 ### ASSERT/FAIL |
364 | 364 |
365 elseif (strcmp (__type, "assert") || strcmp (__type, "fail")) | 365 elseif (strcmp (__type, "assert") || strcmp (__type, "fail")) |
366 __istest = 1; | 366 __istest = 1; |
367 ## Put the keyword back on the code. | 367 ## Put the keyword back on the code. |
368 __code = __block; | 368 __code = __block; |
369 ## The code will be evaluated below as a test block. | 369 ## The code will be evaluated below as a test block. |
370 | 370 |
371 ### ERROR/WARNING | 371 ### ERROR/WARNING |
372 | 372 |
373 elseif (strcmp (__type, "error") || strcmp(__type, "warning")) | 373 elseif (strcmp (__type, "error") || strcmp(__type, "warning")) |
374 __istest = 1; | 374 __istest = 1; |
375 __warning = strcmp (__type, "warning"); | 375 __warning = strcmp (__type, "warning"); |
385 catch | 385 catch |
386 __success = 0; | 386 __success = 0; |
387 __msg = sprintf ("%stest failed: syntax error\n%s", | 387 __msg = sprintf ("%stest failed: syntax error\n%s", |
388 __signal_fail, __error_text__); | 388 __signal_fail, __error_text__); |
389 end_try_catch | 389 end_try_catch |
390 | 390 |
391 if (__success) | 391 if (__success) |
392 __success = 0; | 392 __success = 0; |
393 __warnstate = warning ("query", "quiet"); | 393 __warnstate = warning ("query", "quiet"); |
394 warning ("on", "quiet"); | 394 warning ("on", "quiet"); |
395 try | 395 try |
438 end_try_catch | 438 end_try_catch |
439 clear __test__; | 439 clear __test__; |
440 endif | 440 endif |
441 ## Code already processed. | 441 ## Code already processed. |
442 __code = ""; | 442 __code = ""; |
443 | 443 |
444 ### TESTIF | 444 ### TESTIF |
445 | 445 |
446 elseif (strcmp (__type, "testif")) | 446 elseif (strcmp (__type, "testif")) |
447 [__e, __feat] = regexp (__code, '^\s*(\S+)', 'end', 'tokens'); | 447 [__e, __feat] = regexp (__code, '^\s*(\S+)', 'end', 'tokens'); |
448 if (isempty (findstr (octave_config_info ("DEFS"), __feat{1}{1}))) | 448 if (isempty (findstr (octave_config_info ("DEFS"), __feat{1}{1}))) |
489 else | 489 else |
490 __msg = sprintf ("%stest failed\n%s", __signal_fail, __error_text__); | 490 __msg = sprintf ("%stest failed\n%s", __signal_fail, __error_text__); |
491 __success = 0; | 491 __success = 0; |
492 endif | 492 endif |
493 if (isempty (__error_text__)) | 493 if (isempty (__error_text__)) |
494 error ("empty error text, probably Ctrl-C --- aborting"); | 494 error ("empty error text, probably Ctrl-C --- aborting"); |
495 endif | 495 endif |
496 end_try_catch | 496 end_try_catch |
497 clear __test__; | 497 clear __test__; |
498 endif | 498 endif |
499 | 499 |
500 ## All done. Remember if we were successful and print any messages. | 500 ## All done. Remember if we were successful and print any messages. |
501 if (! isempty (__msg)) | 501 if (! isempty (__msg)) |
502 ## Make sure the user knows what caused the error. | 502 ## Make sure the user knows what caused the error. |
503 if (! __verbose) | 503 if (! __verbose) |
504 fprintf (__fid, "%s%s\n", __signal_block, __block); | 504 fprintf (__fid, "%s%s\n", __signal_block, __block); |
509 fflush (__fid); | 509 fflush (__fid); |
510 ## Show the variable context. | 510 ## Show the variable context. |
511 if (! strcmp (__type, "error") && ! strcmp (__type, "testif") | 511 if (! strcmp (__type, "error") && ! strcmp (__type, "testif") |
512 && ! all (__shared == " ")) | 512 && ! all (__shared == " ")) |
513 fputs (__fid, "shared variables "); | 513 fputs (__fid, "shared variables "); |
514 eval (sprintf ("fdisp(__fid,bundle(%s));", __shared)); | 514 eval (sprintf ("fdisp(__fid,bundle(%s));", __shared)); |
515 fflush (__fid); | 515 fflush (__fid); |
516 endif | 516 endif |
517 endif | 517 endif |
518 if (__success == 0) | 518 if (__success == 0) |
519 __all_success = 0; | 519 __all_success = 0; |
549 endif | 549 endif |
550 elseif (__grabdemo) | 550 elseif (__grabdemo) |
551 __ret1 = __demo_code; | 551 __ret1 = __demo_code; |
552 __ret2 = __demo_idx; | 552 __ret2 = __demo_idx; |
553 elseif (nargout == 1) | 553 elseif (nargout == 1) |
554 __ret1 = __all_success; | 554 __ret1 = __all_success; |
555 else | 555 else |
556 __ret1 = __successes; | 556 __ret1 = __successes; |
557 __ret2 = __tests; | 557 __ret2 = __tests; |
558 __ret3 = __xfail; | 558 __ret3 = __xfail; |
559 __ret4 = __xskip; | 559 __ret4 = __xskip; |
593 ## Strip <pattern> from '<pattern> code'. | 593 ## Strip <pattern> from '<pattern> code'. |
594 ## Also handles 'id=ID code' | 594 ## Also handles 'id=ID code' |
595 function [pattern, id, rest] = getpattern (str) | 595 function [pattern, id, rest] = getpattern (str) |
596 pattern = "."; | 596 pattern = "."; |
597 id = []; | 597 id = []; |
598 rest = str; | 598 rest = str; |
599 str = trimleft (str); | 599 str = trimleft (str); |
600 if (! isempty (str) && str(1) == "<") | 600 if (! isempty (str) && str(1) == "<") |
601 close = index (str, ">"); | 601 close = index (str, ">"); |
602 if (close) | 602 if (close) |
603 pattern = str(2:close-1); | 603 pattern = str(2:close-1); |
683 %!test assert (isempty (kron ([], rand(3, 4)))) | 683 %!test assert (isempty (kron ([], rand(3, 4)))) |
684 %!test assert (isempty (kron (rand (3, 4), []))) | 684 %!test assert (isempty (kron (rand (3, 4), []))) |
685 %!test assert (isempty (kron ([], []))) | 685 %!test assert (isempty (kron ([], []))) |
686 %!shared A, B | 686 %!shared A, B |
687 %!test | 687 %!test |
688 %! A = [1, 2, 3; 4, 5, 6]; | 688 %! A = [1, 2, 3; 4, 5, 6]; |
689 %! B = [1, -1; 2, -2]; | 689 %! B = [1, -1; 2, -2]; |
690 %!assert (size (kron (zeros (3, 0), A)), [ 3*rows(A), 0 ]) | 690 %!assert (size (kron (zeros (3, 0), A)), [ 3*rows(A), 0 ]) |
691 %!assert (size (kron (zeros (0, 3), A)), [ 0, 3*columns(A) ]) | 691 %!assert (size (kron (zeros (0, 3), A)), [ 0, 3*columns(A) ]) |
692 %!assert (size (kron (A, zeros (3, 0))), [ 3*rows(A), 0 ]) | 692 %!assert (size (kron (A, zeros (3, 0))), [ 3*rows(A), 0 ]) |
693 %!assert (size (kron (A, zeros (0, 3))), [ 0, 3*columns(A) ]) | 693 %!assert (size (kron (A, zeros (0, 3))), [ 0, 3*columns(A) ]) |
694 %!assert (kron (pi, e), pi*e) | 694 %!assert (kron (pi, e), pi*e) |
695 %!assert (kron (pi, A), pi*A) | 695 %!assert (kron (pi, A), pi*A) |
696 %!assert (kron (A, e), e*A) | 696 %!assert (kron (A, e), e*A) |
697 %!assert (kron ([1, 2, 3], A), [ A, 2*A, 3*A ]) | 697 %!assert (kron ([1, 2, 3], A), [ A, 2*A, 3*A ]) |
698 %!assert (kron ([1; 2; 3], A), [ A; 2*A; 3*A ]) | 698 %!assert (kron ([1; 2; 3], A), [ A; 2*A; 3*A ]) |
699 %!assert (kron ([1, 2; 3, 4], A), [ A, 2*A; 3*A, 4*A ]) | 699 %!assert (kron ([1, 2; 3, 4], A), [ A, 2*A; 3*A, 4*A ]) |
700 %!test | 700 %!test |
701 %! 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]; | 701 %! 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]; |
702 %! assert (kron (A, B), res) | 702 %! assert (kron (A, B), res) |
703 | 703 |
704 ### an extended demo from specgram | 704 ### an extended demo from specgram |
705 %!#demo | 705 %!#demo |
706 %! ## Speech spectrogram | 706 %! ## Speech spectrogram |
707 %! [x, Fs] = auload(file_in_loadpath("sample.wav")); # audio file | 707 %! [x, Fs] = auload(file_in_loadpath("sample.wav")); # audio file |
708 %! step = fix(5*Fs/1000); # one spectral slice every 5 ms | 708 %! step = fix(5*Fs/1000); # one spectral slice every 5 ms |
709 %! window = fix(40*Fs/1000); # 40 ms data window | 709 %! window = fix(40*Fs/1000); # 40 ms data window |
710 %! fftn = 2^nextpow2(window); # next highest power of 2 | 710 %! fftn = 2^nextpow2(window); # next highest power of 2 |
738 %!warning <warning message> warning('warning message'); | 738 %!warning <warning message> warning('warning message'); |
739 | 739 |
740 %!## test of shared variables | 740 %!## test of shared variables |
741 %!shared a # create a shared variable | 741 %!shared a # create a shared variable |
742 %!test a=3; # assign to a shared variable | 742 %!test a=3; # assign to a shared variable |
743 %!test assert(a,3) # variable should equal 3 | 743 %!test assert(a,3) # variable should equal 3 |
744 %!shared b,c # replace shared variables | 744 %!shared b,c # replace shared variables |
745 %!test assert (!exist("a")); # a no longer exists | 745 %!test assert (!exist("a")); # a no longer exists |
746 %!test assert (isempty(b)); # variables start off empty | 746 %!test assert (isempty(b)); # variables start off empty |
747 %!shared a,b,c # recreate a shared variable | 747 %!shared a,b,c # recreate a shared variable |
748 %!test assert (isempty(a)); # value is empty even if it had a previous value | 748 %!test assert (isempty(a)); # value is empty even if it had a previous value |
767 %!function [x,z] = __test_a (y) | 767 %!function [x,z] = __test_a (y) |
768 %! x = 2*y; | 768 %! x = 2*y; |
769 %! z = 3*y; | 769 %! z = 3*y; |
770 %!test # Test a test function with multiple returns | 770 %!test # Test a test function with multiple returns |
771 %! [x,z] = __test_a(3); | 771 %! [x,z] = __test_a(3); |
772 %! assert(x,6); | 772 %! assert(x,6); |
773 %! assert(z,9); | 773 %! assert(z,9); |
774 | 774 |
775 %!## test of assert block | 775 %!## test of assert block |
776 %!assert (isempty([])) # support for test assert shorthand | 776 %!assert (isempty([])) # support for test assert shorthand |
777 | 777 |
799 % !shared garbage in # variables must be comma separated | 799 % !shared garbage in # variables must be comma separated |
800 % !error syntax++error # error test fails on syntax errors | 800 % !error syntax++error # error test fails on syntax errors |
801 % !error "succeeds."; # error test fails if code succeeds | 801 % !error "succeeds."; # error test fails if code succeeds |
802 % !error <wrong pattern> error("message") # error pattern must match | 802 % !error <wrong pattern> error("message") # error pattern must match |
803 % !demo with syntax error # syntax errors in demo fail properly | 803 % !demo with syntax error # syntax errors in demo fail properly |
804 % !shared a,b,c | 804 % !shared a,b,c |
805 % !demo # shared variables not available in demo | 805 % !demo # shared variables not available in demo |
806 % ! assert(exist("a")) | 806 % ! assert(exist("a")) |
807 % !error | 807 % !error |
808 % ! test('/etc/passwd'); | 808 % ! test('/etc/passwd'); |
809 % ! test("nonexistent file"); | 809 % ! test("nonexistent file"); |
810 % ! ## These don't signal an error, so the test for an error fails. Note | 810 % ! ## These don't signal an error, so the test for an error fails. Note |
811 % ! ## that the call doesn't reference the current fid (it is unavailable), | 811 % ! ## that the call doesn't reference the current fid (it is unavailable), |
812 % ! ## so of course the informational message is not printed in the log. | 812 % ! ## so of course the informational message is not printed in the log. |