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