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