Mercurial > hg > octave-nkf
annotate scripts/testfun/test.m @ 14182:f8d99761244c stable
test: Expand %!testif functionality to multiple conditions
* test.m: Add testing for multiple conditions in %!testif statement.
* svds.m, eigs.cc: Test for ARPACK and other libraries before running
some tests.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 09 Jan 2012 13:32:44 -0800 |
parents | 72c96de7a403 |
children | 68fd9f95e1f9 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14066
diff
changeset
|
1 ## Copyright (C) 2005-2012 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 -*- | |
11412
a8a9f062d0ef
test.m: Use 'Command' for deftype in docstring.
Rik <octave@nomad.inbox5.com>
parents:
11032
diff
changeset
|
20 ## @deftypefn {Command} {} test @var{name} |
a8a9f062d0ef
test.m: Use 'Command' for deftype in docstring.
Rik <octave@nomad.inbox5.com>
parents:
11032
diff
changeset
|
21 ## @deftypefnx {Command} {} test @var{name} quiet|normal|verbose |
5589 | 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{}) | |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
26 ## @deftypefnx {Function File} {[@var{code}, @var{idx}] =} test ('@var{name}', 'grabdemo') |
5589 | 27 ## |
28 ## Perform tests from the first file in the loadpath matching @var{name}. | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
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' | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
41 ## Report all tests as they happen, but don't do tests which require |
5589 | 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 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
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 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
53 ## which case the contents of the file will be replaced with the log from |
5589 | 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}. | |
11412
a8a9f062d0ef
test.m: Use 'Command' for deftype in docstring.
Rik <octave@nomad.inbox5.com>
parents:
11032
diff
changeset
|
68 ## @seealso{assert, fail, error, 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) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
91 __fid = []; |
5589 | 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) | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10615
diff
changeset
|
108 error ("test: could not open log file"); |
6494 | 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 = ""; | |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12447
diff
changeset
|
135 __demo_idx = []; |
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 | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10615
diff
changeset
|
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 | |
12447
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
176 if (exist (__name) == 3) |
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
177 fprintf (__fid, "%s%s source code with tests for dynamically linked function not found\n", __signal_empty, __name); |
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
178 else |
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
179 fprintf (__fid, "%s%s does not exist in path\n", __signal_empty, __name); |
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
180 endif |
5908 | 181 fflush (__fid); |
6494 | 182 if (nargout > 0) |
10549 | 183 __ret1 = __ret2 = 0; |
6494 | 184 endif |
5589 | 185 endif |
6494 | 186 if (__close_fid) |
187 fclose(__fid); | |
188 endif | |
5589 | 189 return; |
190 endif | |
191 | |
8506 | 192 ## Grab the test code from the file. |
5589 | 193 __body = __extract_test_code (__file); |
194 | |
195 if (isempty (__body)) | |
196 if (__grabdemo) | |
197 __ret1 = ""; | |
198 __ret2 = []; | |
199 else | |
6494 | 200 fprintf (__fid, "%s%s has no tests available\n", __signal_empty, __file); |
5908 | 201 fflush (__fid); |
6494 | 202 if (nargout > 0) |
10549 | 203 __ret1 = __ret2 = 0; |
6494 | 204 endif |
5589 | 205 endif |
6494 | 206 if (__close_fid) |
207 fclose(__fid); | |
208 endif | |
5589 | 209 return; |
210 else | |
8506 | 211 ## Add a dummy comment block to the end for ease of indexing. |
5589 | 212 if (__body (length(__body)) == "\n") |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
213 __body = sprintf ("\n%s#", __body); |
5589 | 214 else |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
215 __body = sprintf ("\n%s\n#", __body); |
5589 | 216 endif |
217 endif | |
218 | |
8506 | 219 ## Chop it up into blocks for evaluation. |
6494 | 220 __lineidx = find (__body == "\n"); |
221 __blockidx = __lineidx(find (! isspace (__body(__lineidx+1))))+1; | |
5589 | 222 |
8506 | 223 ## Ready to start tests ... if in batch mode, tell us what is happening. |
5589 | 224 if (__verbose) |
8507 | 225 disp (cstrcat (__signal_file, __file)); |
5589 | 226 endif |
227 | |
8506 | 228 ## Assume all tests will pass. |
5589 | 229 __all_success = 1; |
230 | |
8506 | 231 ## Process each block separately, initially with no shared variables. |
5589 | 232 __tests = __successes = 0; |
233 __shared = " "; | |
234 __shared_r = " "; | |
235 __clear = ""; | |
6494 | 236 for __i = 1:length(__blockidx)-1 |
5589 | 237 |
8506 | 238 ## Extract the block. |
5589 | 239 __block = __body(__blockidx(__i):__blockidx(__i+1)-2); |
240 | |
8506 | 241 ## Let the user/logfile know what is happening. |
5589 | 242 if (__verbose) |
243 fprintf (__fid, "%s%s\n", __signal_block, __block); | |
5908 | 244 fflush (__fid); |
5589 | 245 endif |
246 | |
8506 | 247 ## Split __block into __type and __code. |
6494 | 248 __idx = find (! isletter (__block)); |
249 if (isempty (__idx)) | |
5589 | 250 __type = __block; |
251 __code = ""; | |
252 else | |
253 __type = __block(1:__idx(1)-1); | |
254 __code = __block(__idx(1):length(__block)); | |
255 endif | |
256 | |
8506 | 257 ## Assume the block will succeed. |
5589 | 258 __success = 1; |
259 __msg = []; | |
260 | |
8506 | 261 ### DEMO |
262 | |
5589 | 263 ## If in __grabdemo mode, then don't process any other block type. |
264 ## So that the other block types don't have to worry about | |
265 ## this __grabdemo mode, the demo block processor grabs all block | |
266 ## types and skips those which aren't demo blocks. | |
8506 | 267 |
5589 | 268 __isdemo = strcmp (__type, "demo"); |
269 if (__grabdemo || __isdemo) | |
270 __istest = 0; | |
271 | |
272 if (__grabdemo && __isdemo) | |
10549 | 273 if (isempty(__demo_code)) |
274 __demo_code = __code; | |
275 __demo_idx = [1, length(__demo_code)+1]; | |
276 else | |
277 __demo_code = cstrcat(__demo_code, __code); | |
278 __demo_idx = [__demo_idx, length(__demo_code)+1]; | |
279 endif | |
5589 | 280 |
281 elseif (__rundemo && __isdemo) | |
10549 | 282 try |
283 ## process the code in an environment without variables | |
284 eval (sprintf ("function __test__()\n%s\nendfunction", __code)); | |
285 __test__; | |
286 input ("Press <enter> to continue: ", "s"); | |
287 catch | |
288 __success = 0; | |
12687
6d4c18565de1
Deprecate error_text(), __error_text__()
Rik <octave@nomad.inbox5.com>
parents:
12643
diff
changeset
|
289 __msg = sprintf ("%sdemo failed\n%s", __signal_fail, lasterr ()); |
10549 | 290 end_try_catch |
291 clear __test__; | |
5589 | 292 |
293 endif | |
8506 | 294 ## Code already processed. |
295 __code = ""; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
296 |
8506 | 297 ### SHARED |
298 | |
299 elseif (strcmp (__type, "shared")) | |
5589 | 300 __istest = 0; |
301 | |
8506 | 302 ## Separate initialization code from variables. |
6494 | 303 __idx = find (__code == "\n"); |
304 if (isempty (__idx)) | |
10549 | 305 __vars = __code; |
306 __code = ""; | |
5589 | 307 else |
10549 | 308 __vars = __code (1:__idx(1)-1); |
309 __code = __code (__idx(1):length(__code)); | |
5589 | 310 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
311 |
8506 | 312 ## Strip comments off the variables. |
6730 | 313 __idx = find (__vars == "%" | __vars == "#"); |
6494 | 314 if (! isempty (__idx)) |
10549 | 315 __vars = __vars(1:__idx(1)-1); |
5589 | 316 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
317 |
8506 | 318 ## Assign default values to variables. |
5589 | 319 try |
10549 | 320 __vars = deblank (__vars); |
321 if (! isempty (__vars)) | |
322 eval (cstrcat (strrep (__vars, ",", "=[];"), "=[];")); | |
323 __shared = __vars; | |
324 __shared_r = cstrcat ("[ ", __vars, "] = "); | |
325 else | |
326 __shared = " "; | |
327 __shared_r = " "; | |
328 endif | |
5589 | 329 catch |
10549 | 330 ## Couldn't declare, so don't initialize. |
331 __code = ""; | |
332 __success = 0; | |
333 __msg = sprintf ("%sshared variable initialization failed\n", | |
334 __signal_fail); | |
5589 | 335 end_try_catch |
336 | |
8506 | 337 ## Clear shared function definitions. |
6494 | 338 eval (__clear, ""); |
339 __clear = ""; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
340 |
8506 | 341 ## Initialization code will be evaluated below. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
342 |
8506 | 343 ### FUNCTION |
344 | |
6494 | 345 elseif (strcmp (__type, "function")) |
5589 | 346 __istest = 0; |
347 persistent __fn = 0; | |
6494 | 348 __name_position = function_name (__block); |
349 if (isempty (__name_position)) | |
5589 | 350 __success = 0; |
6494 | 351 __msg = sprintf ("%stest failed: missing function name\n", |
10549 | 352 __signal_fail); |
5589 | 353 else |
354 __name = __block(__name_position(1):__name_position(2)); | |
10130
0c3609dd34cf
oct-parse.yy (eval_string): save and set reading_{script,fcn,classdef}_file vars
John W. Eaton <jwe@octave.org>
parents:
10128
diff
changeset
|
355 __code = __block; |
5589 | 356 try |
357 eval(__code); ## Define the function | |
6494 | 358 __clear = sprintf ("%sclear %s;\n", __clear, __name); |
5589 | 359 catch |
360 __success = 0; | |
6494 | 361 __msg = sprintf ("%stest failed: syntax error\n%s", |
12687
6d4c18565de1
Deprecate error_text(), __error_text__()
Rik <octave@nomad.inbox5.com>
parents:
12643
diff
changeset
|
362 __signal_fail, lasterr ()); |
5589 | 363 end_try_catch |
364 endif | |
365 __code = ""; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
366 |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
367 ### ENDFUNCTION |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
368 |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
369 elseif (strcmp (__type, "endfunction")) |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
370 ## endfunction simply declares the end of a previous function block. |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
371 ## There is no processing to be done here, just skip to next block. |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
372 __istest = 0; |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
373 __code = ""; |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
374 |
8506 | 375 ### ASSERT/FAIL |
5589 | 376 |
6494 | 377 elseif (strcmp (__type, "assert") || strcmp (__type, "fail")) |
5589 | 378 __istest = 1; |
8506 | 379 ## Put the keyword back on the code. |
380 __code = __block; | |
381 ## The code will be evaluated below as a test block. | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
382 |
8506 | 383 ### ERROR/WARNING |
384 | |
6494 | 385 elseif (strcmp (__type, "error") || strcmp(__type, "warning")) |
5589 | 386 __istest = 1; |
6494 | 387 __warning = strcmp (__type, "warning"); |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
388 [__pattern, __id, __code] = getpattern (__code); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
389 if (__id) |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
390 __patstr = ["id=",__id]; |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
391 else |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
392 __patstr = ["<",__pattern,">"]; |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
393 endif |
5589 | 394 try |
10549 | 395 eval (sprintf ("function __test__(%s)\n%s\nendfunction", |
396 __shared, __code)); | |
5589 | 397 catch |
10549 | 398 __success = 0; |
399 __msg = sprintf ("%stest failed: syntax error\n%s", | |
12687
6d4c18565de1
Deprecate error_text(), __error_text__()
Rik <octave@nomad.inbox5.com>
parents:
12643
diff
changeset
|
400 __signal_fail, lasterr ()); |
5589 | 401 end_try_catch |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
402 |
5589 | 403 if (__success) |
404 __success = 0; | |
10549 | 405 __warnstate = warning ("query", "quiet"); |
406 warning ("on", "quiet"); | |
407 try | |
408 eval (sprintf ("__test__(%s);", __shared)); | |
6494 | 409 if (! __warning) |
10549 | 410 __msg = sprintf ("%sexpected %s but got no error\n", |
411 __signal_fail, __patstr); | |
412 else | |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
413 if (! isempty (__id)) |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
414 [~, __err] = lastwarn; |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
415 __mismatch = ! strcmp (__err, __id); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
416 else |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
417 __err = trimerr (lastwarn, "warning"); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
418 __mismatch = isempty (regexp (__err, __pattern, "once")); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
419 endif |
7028 | 420 warning (__warnstate.state, "quiet"); |
421 if (isempty (__err)) | |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
422 __msg = sprintf ("%sexpected %s but got no warning\n", |
10549 | 423 __signal_fail, __patstr); |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
424 elseif (__mismatch) |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
425 __msg = sprintf ("%sexpected %s but got %s\n", |
10549 | 426 __signal_fail, __patstr, __err); |
7028 | 427 else |
428 __success = 1; | |
429 endif | |
10549 | 430 endif |
5589 | 431 |
10549 | 432 catch |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
433 if (! isempty (__id)) |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
434 [~, __err] = lasterr; |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
435 __mismatch = ! strcmp (__err, __id); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
436 else |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
437 __err = trimerr (lasterr, "error"); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
438 __mismatch = isempty (regexp (__err, __pattern, "once")); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
439 endif |
6494 | 440 warning (__warnstate.state, "quiet"); |
441 if (__warning) | |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
442 __msg = sprintf ("%sexpected warning %s but got error %s\n", |
10549 | 443 __signal_fail, __patstr, __err); |
444 elseif (__mismatch) | |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
445 __msg = sprintf ("%sexpected %s but got %s\n", |
10549 | 446 __signal_fail, __patstr, __err); |
5589 | 447 else |
10549 | 448 __success = 1; |
5589 | 449 endif |
10549 | 450 end_try_catch |
451 clear __test__; | |
5589 | 452 endif |
8506 | 453 ## Code already processed. |
454 __code = ""; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
455 |
8506 | 456 ### TESTIF |
457 | |
7242 | 458 elseif (strcmp (__type, "testif")) |
14182
f8d99761244c
test: Expand %!testif functionality to multiple conditions
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
459 __e = regexp (__code, '.$', 'lineanchors', 'once'); |
f8d99761244c
test: Expand %!testif functionality to multiple conditions
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
460 __feat = regexp (__code(1:__e), '\w+', 'match'); |
f8d99761244c
test: Expand %!testif functionality to multiple conditions
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
461 __have_feat = strfind (octave_config_info ("DEFS"), __feat); |
f8d99761244c
test: Expand %!testif functionality to multiple conditions
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
462 if (any (cellfun ("isempty", __have_feat))) |
7242 | 463 __xskip++; |
10549 | 464 __istest = 0; |
10615
08050f37ba49
Don't abort testing when merely skipping a testif block.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
465 __code = ""; # Skip the code. |
10549 | 466 __msg = sprintf ("%sskipped test\n", __signal_skip); |
7242 | 467 else |
468 __istest = 1; | |
10549 | 469 __code = __code(__e + 1 : end); |
7242 | 470 endif |
471 | |
8506 | 472 ### TEST |
473 | |
6728 | 474 elseif (strcmp (__type, "test") || strcmp (__type, "xtest")) |
5589 | 475 __istest = 1; |
8506 | 476 ## Code will be evaluated below. |
477 | |
478 ### Comment block. | |
479 | |
6494 | 480 elseif (strcmp (__block(1:1), "#")) |
5589 | 481 __istest = 0; |
482 __code = ""; # skip the code | |
483 | |
8506 | 484 ### Unknown block. |
485 | |
5589 | 486 else |
487 __istest = 1; | |
488 __success = 0; | |
6494 | 489 __msg = sprintf ("%sunknown test type!\n", __signal_fail); |
5589 | 490 __code = ""; # skip the code |
491 endif | |
492 | |
493 ## evaluate code for test, shared, and assert. | |
6494 | 494 if (! isempty(__code)) |
5589 | 495 try |
14066
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
496 ## FIXME: need to check for embedded test functions, which cause |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
497 ## segfaults, until issues with subfunctions in functions are resolved. |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
498 embed_func = regexp (__code, '^\s*function ', 'once', 'lineanchors'); |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
499 if (isempty (embed_func)) |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
500 eval (sprintf ("function %s__test__(%s)\n%s\nendfunction", |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
501 __shared_r,__shared, __code)); |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
502 eval (sprintf ("%s__test__(%s);", __shared_r, __shared)); |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
503 else |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
504 error (["Functions embedded in %!test blocks are not allowed.\n", ... |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
505 "Use the %!function/%!endfunction syntax instead to define shared functions for testing.\n"]); |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
506 endif |
5589 | 507 catch |
6728 | 508 if (strcmp (__type, "xtest")) |
12687
6d4c18565de1
Deprecate error_text(), __error_text__()
Rik <octave@nomad.inbox5.com>
parents:
12643
diff
changeset
|
509 __msg = sprintf ("%sknown failure\n%s", __signal_fail, lasterr ()); |
10549 | 510 __xfail++; |
6728 | 511 else |
12687
6d4c18565de1
Deprecate error_text(), __error_text__()
Rik <octave@nomad.inbox5.com>
parents:
12643
diff
changeset
|
512 __msg = sprintf ("%stest failed\n%s", __signal_fail, lasterr ()); |
10549 | 513 __success = 0; |
6728 | 514 endif |
12687
6d4c18565de1
Deprecate error_text(), __error_text__()
Rik <octave@nomad.inbox5.com>
parents:
12643
diff
changeset
|
515 if (isempty (lasterr ())) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
516 error ("empty error text, probably Ctrl-C --- aborting"); |
10549 | 517 endif |
5589 | 518 end_try_catch |
519 clear __test__; | |
520 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
521 |
8506 | 522 ## All done. Remember if we were successful and print any messages. |
6494 | 523 if (! isempty (__msg)) |
8506 | 524 ## Make sure the user knows what caused the error. |
6494 | 525 if (! __verbose) |
10549 | 526 fprintf (__fid, "%s%s\n", __signal_block, __block); |
527 fflush (__fid); | |
5589 | 528 endif |
529 fputs (__fid, __msg); | |
9996
929e53107f09
Add newline to message output of test.m
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
530 fputs (__fid, "\n"); |
5908 | 531 fflush (__fid); |
8506 | 532 ## Show the variable context. |
7244 | 533 if (! strcmp (__type, "error") && ! strcmp (__type, "testif") |
10549 | 534 && ! all (__shared == " ")) |
535 fputs (__fid, "shared variables "); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
536 eval (sprintf ("fdisp(__fid,bundle(%s));", __shared)); |
10549 | 537 fflush (__fid); |
5589 | 538 endif |
539 endif | |
540 if (__success == 0) | |
541 __all_success = 0; | |
8506 | 542 ## Stop after one error if not in batch mode. |
6494 | 543 if (! __batch) |
10549 | 544 if (nargout > 0) |
545 __ret1 = __ret2 = 0; | |
546 endif | |
547 if (__close_fid) | |
548 fclose(__fid); | |
549 endif | |
550 return; | |
5589 | 551 endif |
552 endif | |
553 __tests += __istest; | |
6494 | 554 __successes += __success * __istest; |
5589 | 555 endfor |
6494 | 556 eval (__clear, ""); |
5589 | 557 |
558 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
|
559 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
|
560 if (__xfail) |
10549 | 561 printf ("PASSES %d out of %d tests (%d expected failures)\n", |
562 __successes, __tests, __xfail); | |
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
|
563 else |
10549 | 564 printf ("PASSES %d out of %d tests\n", __successes, __tests); |
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
|
565 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
|
566 if (__xskip) |
10549 | 567 printf ("Skipped %d tests due to missing features\n", __xskip); |
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
|
568 endif |
6730 | 569 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
|
570 printf ("%s%s has no tests available\n", __signal_empty, __file); |
7242 | 571 endif |
5589 | 572 elseif (__grabdemo) |
573 __ret1 = __demo_code; | |
574 __ret2 = __demo_idx; | |
6494 | 575 elseif (nargout == 1) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
576 __ret1 = __all_success; |
5589 | 577 else |
578 __ret1 = __successes; | |
579 __ret2 = __tests; | |
6730 | 580 __ret3 = __xfail; |
7242 | 581 __ret4 = __xskip; |
5589 | 582 endif |
583 endfunction | |
584 | |
8506 | 585 ## Create structure with fieldnames the name of the input variables. |
6494 | 586 function s = varstruct (varargin) |
587 for i = 1:nargin | |
588 s.(deblank (argn(i,:))) = varargin{i}; | |
5589 | 589 endfor |
590 endfunction | |
591 | |
8506 | 592 ## Find [start,end] of fn in 'function [a,b] = fn'. |
6494 | 593 function pos = function_name (def) |
5589 | 594 pos = []; |
595 | |
8506 | 596 ## Find the end of the name. |
6494 | 597 right = find (def == "(", 1); |
598 if (isempty (right)) | |
599 return; | |
600 endif | |
601 right = find (def(1:right-1) != " ", 1, "last"); | |
5589 | 602 |
8506 | 603 ## Find the beginning of the name. |
6494 | 604 left = max ([find(def(1:right)==" ", 1, "last"), ... |
10549 | 605 find(def(1:right)=="=", 1, "last")]); |
6494 | 606 if (isempty (left)) |
607 return; | |
608 endif | |
5589 | 609 left++; |
610 | |
8506 | 611 ## Return the end points of the name. |
6494 | 612 pos = [left, right]; |
5589 | 613 endfunction |
614 | |
8506 | 615 ## Strip <pattern> from '<pattern> code'. |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
616 ## Also handles 'id=ID code' |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
617 function [pattern, id, rest] = getpattern (str) |
6494 | 618 pattern = "."; |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
619 id = []; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
620 rest = str; |
6494 | 621 str = trimleft (str); |
622 if (! isempty (str) && str(1) == "<") | |
623 close = index (str, ">"); | |
624 if (close) | |
5589 | 625 pattern = str(2:close-1); |
626 rest = str(close+1:end); | |
627 endif | |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
628 elseif (strncmp (str, "id=", 3)) |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
629 [id, rest] = strtok (str(4:end)); |
5589 | 630 endif |
631 endfunction | |
632 | |
8506 | 633 ## Strip '.*prefix:' from '.*prefix: msg\n' and strip trailing blanks. |
6494 | 634 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
|
635 idx = index (msg, cstrcat (prefix, ":")); |
6494 | 636 if (idx > 0) |
637 msg(1:idx+length(prefix)) = []; | |
638 endif | |
639 msg = trimleft (deblank (msg)); | |
5589 | 640 endfunction |
641 | |
8506 | 642 ## Strip leading blanks from string. |
6494 | 643 function str = trimleft (str) |
644 idx = find (isspace (str)); | |
645 leading = find (idx == 1:length(idx)); | |
646 if (! isempty (leading)) | |
5589 | 647 str = str(leading(end)+1:end); |
648 endif | |
649 endfunction | |
650 | |
8506 | 651 ## Make a structure out of the named variables |
652 ## (based on Etienne Grossmann's tar function). | |
6494 | 653 function s = bundle (varargin) |
654 for i = 1:nargin | |
655 s.(deblank (argn(i,:))) = varargin{i}; | |
7151 | 656 endfor |
5589 | 657 endfunction |
658 | |
659 function body = __extract_test_code (nm) | |
660 fid = fopen (nm, "rt"); | |
661 body = []; | |
662 if (fid >= 0) | |
6494 | 663 while (! feof (fid)) |
5589 | 664 ln = fgetl (fid); |
6494 | 665 if (length (ln) >= 2 && strcmp (ln(1:2), "%!")) |
5589 | 666 body = [body, "\n"]; |
667 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
|
668 body = cstrcat (body, ln(3:end)); |
5589 | 669 endif |
670 endif | |
671 endwhile | |
672 fclose (fid); | |
673 endif | |
674 endfunction | |
675 | |
8506 | 676 ### Test for test for missing features |
7242 | 677 %!testif OCTAVE_SOURCE |
678 %! ## This test should be run | |
679 %! assert (true); | |
7243 | 680 |
681 ### Disable this test to avoid spurious skipped test for "make check" | |
682 % !testif HAVE_FOOBAR | |
683 % ! ## missing feature. Fail if this test is run | |
684 % ! error("Failed missing feature test"); | |
7242 | 685 |
6728 | 686 ### Test for a known failure |
687 %!xtest error("This test is known to fail") | |
688 | |
5589 | 689 ### example from toeplitz |
13914
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
690 %!shared msg1,msg2 |
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
691 %! msg1="C must be a vector"; |
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
692 %! msg2="C and R must be vectors"; |
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
693 %!fail ('toeplitz([])', msg1); |
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
694 %!fail ('toeplitz([1,2;3,4])', msg1); |
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
695 %!fail ('toeplitz([1,2],[])', msg2); |
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
696 %!fail ('toeplitz([1,2],[1,2;3,4])', msg2); |
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
697 %!fail ('toeplitz ([1,2;3,4],[1,2])', msg2); |
5589 | 698 % !fail ('toeplitz','usage: toeplitz'); # usage doesn't generate an error |
699 % !fail ('toeplitz(1, 2, 3)', 'usage: toeplitz'); | |
700 %!test assert (toeplitz ([1,2,3], [1,4]), [1,4; 2,1; 3,2]); | |
701 %!demo toeplitz ([1,2,3,4],[1,5,6]) | |
702 | |
703 ### example from kron | |
5775 | 704 %!#error kron # FIXME suppress these until we can handle output |
5589 | 705 %!#error kron(1,2,3) |
706 %!test assert (isempty (kron ([], rand(3, 4)))) | |
707 %!test assert (isempty (kron (rand (3, 4), []))) | |
708 %!test assert (isempty (kron ([], []))) | |
709 %!shared A, B | |
710 %!test | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
711 %! A = [1, 2, 3; 4, 5, 6]; |
5589 | 712 %! B = [1, -1; 2, -2]; |
713 %!assert (size (kron (zeros (3, 0), A)), [ 3*rows(A), 0 ]) | |
714 %!assert (size (kron (zeros (0, 3), A)), [ 0, 3*columns(A) ]) | |
715 %!assert (size (kron (A, zeros (3, 0))), [ 3*rows(A), 0 ]) | |
716 %!assert (size (kron (A, zeros (0, 3))), [ 0, 3*columns(A) ]) | |
717 %!assert (kron (pi, e), pi*e) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
718 %!assert (kron (pi, A), pi*A) |
5589 | 719 %!assert (kron (A, e), e*A) |
720 %!assert (kron ([1, 2, 3], A), [ A, 2*A, 3*A ]) | |
721 %!assert (kron ([1; 2; 3], A), [ A; 2*A; 3*A ]) | |
722 %!assert (kron ([1, 2; 3, 4], A), [ A, 2*A; 3*A, 4*A ]) | |
723 %!test | |
724 %! 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]; | |
725 %! assert (kron (A, B), res) | |
726 | |
727 ### an extended demo from specgram | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
728 %!#demo |
5589 | 729 %! ## Speech spectrogram |
730 %! [x, Fs] = auload(file_in_loadpath("sample.wav")); # audio file | |
731 %! step = fix(5*Fs/1000); # one spectral slice every 5 ms | |
732 %! window = fix(40*Fs/1000); # 40 ms data window | |
733 %! fftn = 2^nextpow2(window); # next highest power of 2 | |
734 %! [S, f, t] = specgram(x, fftn, Fs, window, window-step); | |
735 %! S = abs(S(2:fftn*4000/Fs,:)); # magnitude in range 0<f<=4000 Hz. | |
736 %! S = S/max(max(S)); # normalize magnitude so that max is 0 dB. | |
737 %! S = max(S, 10^(-40/10)); # clip below -40 dB. | |
738 %! S = min(S, 10^(-3/10)); # clip above -3 dB. | |
739 %! imagesc(flipud(20*log10(S)), 1); | |
740 %! % you should now see a spectrogram in the image window | |
741 | |
742 | |
743 ### now test test itself | |
744 | |
745 %!## usage and error testing | |
746 % !fail ('test','usage.*test') # no args, generates usage() | |
747 % !fail ('test(1,2,3,4)','usage.*test') # too many args, generates usage() | |
748 %!fail ('test("test", "bogus")','unknown flag') # incorrect args | |
749 %!fail ('garbage','garbage.*undefined') # usage on nonexistent function should be | |
750 | |
7940 | 751 %!error test # no args, generates usage() |
752 %!error test(1,2,3,4) # too many args, generates usage() | |
5589 | 753 %!error <unknown flag> test("test", 'bogus'); # incorrect args, generates error() |
754 %!error <garbage' undefined> garbage # usage on nonexistent function should be | |
755 | |
756 %!error test("test", 'bogus'); # test without pattern | |
757 | |
5681 | 758 %!test |
759 %! lastwarn(); # clear last warning just in case | |
760 | |
5781 | 761 %!warning <warning message> warning('warning message'); |
5589 | 762 |
763 %!## test of shared variables | |
764 %!shared a # create a shared variable | |
765 %!test a=3; # assign to a shared variable | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
766 %!test assert(a,3) # variable should equal 3 |
5589 | 767 %!shared b,c # replace shared variables |
768 %!test assert (!exist("a")); # a no longer exists | |
769 %!test assert (isempty(b)); # variables start off empty | |
770 %!shared a,b,c # recreate a shared variable | |
771 %!test assert (isempty(a)); # value is empty even if it had a previous value | |
772 %!test a=1; b=2; c=3; # give values to all variables | |
773 %!test assert ([a,b,c],[1,2,3]); # test all of them together | |
774 %!test c=6; # update a value | |
775 %!test assert([a, b, c],[1, 2, 6]); # show that the update sticks | |
776 %!shared # clear all shared variables | |
777 %!test assert(!exist("a")) # show that they are cleared | |
778 %!shared a,b,c # support for initializer shorthand | |
779 %! a=1; b=2; c=4; | |
780 | |
781 %!function x = __test_a(y) | |
782 %! x = 2*y; | |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
783 %!endfunction |
5589 | 784 %!assert(__test_a(2),4); # Test a test function |
785 | |
786 %!function __test_a (y) | |
787 %! x = 2*y; | |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
788 %!endfunction |
5589 | 789 %!test |
790 %! __test_a(2); # Test a test function with no return value | |
791 | |
792 %!function [x,z] = __test_a (y) | |
793 %! x = 2*y; | |
794 %! z = 3*y; | |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
795 %!endfunction |
5589 | 796 %!test # Test a test function with multiple returns |
797 %! [x,z] = __test_a(3); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
798 %! assert(x,6); |
5589 | 799 %! assert(z,9); |
800 | |
801 %!## test of assert block | |
802 %!assert (isempty([])) # support for test assert shorthand | |
803 | |
804 %!## demo blocks | |
805 %!demo # multiline demo block | |
806 %! t=[0:0.01:2*pi]; x=sin(t); | |
807 %! plot(t,x); | |
808 %! % you should now see a sine wave in your figure window | |
809 %!demo a=3 # single line demo blocks work too | |
810 | |
811 %!## this is a comment block. it can contain anything. | |
812 %!## | |
813 %! it is the "#" as the block type that makes it a comment | |
814 %! and it stays as a comment even through continuation lines | |
815 %! which means that it works well with commenting out whole tests | |
816 | |
817 % !# failure tests. All the following should fail. These tests should | |
818 % !# be disabled unless you are developing test() since users don't | |
819 % !# like to be presented with expected failures. I use % ! to disable. | |
820 % !test error("---------Failure tests. Use test('test','verbose',1)"); | |
821 % !test assert([a,b,c],[1,3,6]); # variables have wrong values | |
822 % !bogus # unknown block type | |
823 % !error toeplitz([1,2,3]); # correct usage | |
824 % !test syntax errors) # syntax errors fail properly | |
825 % !shared garbage in # variables must be comma separated | |
826 % !error syntax++error # error test fails on syntax errors | |
827 % !error "succeeds."; # error test fails if code succeeds | |
828 % !error <wrong pattern> error("message") # error pattern must match | |
829 % !demo with syntax error # syntax errors in demo fail properly | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
830 % !shared a,b,c |
5589 | 831 % !demo # shared variables not available in demo |
832 % ! assert(exist("a")) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
833 % !error |
5589 | 834 % ! test('/etc/passwd'); |
835 % ! test("nonexistent file"); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
836 % ! ## These don't signal an error, so the test for an error fails. Note |
5589 | 837 % ! ## that the call doesn't reference the current fid (it is unavailable), |
838 % ! ## so of course the informational message is not printed in the log. |