1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Test Functions</title>
<meta name="description" content="GNU Octave: Test Functions">
<meta name="keywords" content="GNU Octave: Test Functions">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Test-and-Demo-Functions.html#Test-and-Demo-Functions" rel="up" title="Test and Demo Functions">
<link href="Demonstration-Functions.html#Demonstration-Functions" rel="next" title="Demonstration Functions">
<link href="Test-and-Demo-Functions.html#Test-and-Demo-Functions" rel="prev" title="Test and Demo Functions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Test-Functions"></a>
<div class="header">
<p>
Next: <a href="Demonstration-Functions.html#Demonstration-Functions" accesskey="n" rel="next">Demonstration Functions</a>, Up: <a href="Test-and-Demo-Functions.html#Test-and-Demo-Functions" accesskey="u" rel="up">Test and Demo Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Test-Functions-1"></a>
<h3 class="section">B.1 Test Functions</h3>
<a name="XREFtest"></a><dl>
<dt><a name="index-test"></a>Command: <em></em> <strong>test</strong> <em><var>name</var></em></dt>
<dt><a name="index-test-1"></a>Command: <em></em> <strong>test</strong> <em><var>name</var> quiet|normal|verbose</em></dt>
<dt><a name="index-test-2"></a>Function File: <em></em> <strong>test</strong> <em>("<var>name</var>", "quiet|normal|verbose", <var>fid</var>)</em></dt>
<dt><a name="index-test-3"></a>Function File: <em></em> <strong>test</strong> <em>([], "explain", <var>fid</var>)</em></dt>
<dt><a name="index-test-4"></a>Function File: <em><var>success</var> =</em> <strong>test</strong> <em>(…)</em></dt>
<dt><a name="index-test-5"></a>Function File: <em>[<var>n</var>, <var>max</var>] =</em> <strong>test</strong> <em>(…)</em></dt>
<dt><a name="index-test-6"></a>Function File: <em>[<var>code</var>, <var>idx</var>] =</em> <strong>test</strong> <em>("<var>name</var>", "grabdemo")</em></dt>
<dd>
<p>Perform tests from the first file in the loadpath matching <var>name</var>.
<code>test</code> can be called as a command or as a function. Called with
a single argument <var>name</var>, the tests are run interactively and stop
after the first error is encountered.
</p>
<p>With a second argument the tests which are performed and the amount of
output is selected.
</p>
<dl compact="compact">
<dt><code>"quiet"</code></dt>
<dd><p>Don’t report all the tests as they happen, just the errors.
</p>
</dd>
<dt><code>"normal"</code></dt>
<dd><p>Report all tests as they happen, but don’t do tests which require
user interaction.
</p>
</dd>
<dt><code>"verbose"</code></dt>
<dd><p>Do tests which require user interaction.
</p></dd>
</dl>
<p>The argument <var>fid</var> can be used to allow batch processing. Errors
can be written to the already open file defined by <var>fid</var>, and
hopefully when Octave crashes this file will tell you what was happening
when it did. You can use <code>stdout</code> if you want to see the results as
they happen. You can also give a file name rather than an <var>fid</var>, in
which case the contents of the file will be replaced with the log from
the current test.
</p>
<p>Called with a single output argument <var>success</var>, <code>test</code> returns
true if all of the tests were successful. Called with two output arguments
<var>n</var> and <var>max</var>, the number of successful tests and the total number
of tests in the file <var>name</var> are returned.
</p>
<p>If the second argument is the string <code>"grabdemo"</code>, the contents of
the demo blocks are extracted but not executed. Code for all code blocks
is concatenated and returned as <var>code</var> with <var>idx</var> being a vector
of positions of the ends of the demo blocks.
</p>
<p>If the second argument is <code>"explain"</code>, then <var>name</var> is ignored
and an explanation of the line markers used is written to the file
<var>fid</var>.
</p>
<p><strong>See also:</strong> <a href="#XREFassert">assert</a>, <a href="#XREFfail">fail</a>, <a href="Raising-Errors.html#XREFerror">error</a>, <a href="Demonstration-Functions.html#XREFdemo">demo</a>, <a href="Demonstration-Functions.html#XREFexample">example</a>.
</p></dd></dl>
<p><code>test</code> scans the named script file looking for lines which start
with the identifier ‘<samp>%!</samp>’. The prefix is stripped off and the rest
of the line is processed through the Octave interpreter. If the code
generates an error, then the test is said to fail.
</p>
<p>Since <code>eval()</code> will stop at the first error it encounters, you must
divide your tests up into blocks, with anything in a separate
block evaluated separately. Blocks are introduced by valid keywords like
<code>test</code>, <code>function</code>, or <code>assert</code> immediately following ‘<samp>%!</samp>’.
A block is defined by indentation as in Python. Lines beginning with
‘<samp>%!<whitespace></samp>’ are part of the preceeding block.
</p>
<p>For example:
</p>
<div class="example">
<pre class="example">%!test error ("this test fails!");
%!test "test doesn't fail. it doesn't generate an error";
</pre></div>
<p>When a test fails, you will see something like:
</p>
<div class="example">
<pre class="example"> ***** test error ("this test fails!")
!!!!! test failed
this test fails!
</pre></div>
<p>Generally, to test if something works, you want to assert that it
produces a correct value. A real test might look something like
</p>
<div class="example">
<pre class="example">%!test
%! <var>a</var> = [1, 2, 3; 4, 5, 6]; B = [1; 2];
%! expect = [ <var>a</var> ; 2*<var>a</var> ];
%! get = kron (<var>b</var>, <var>a</var>);
%! if (any (size (expect) != size (get)))
%! error ("wrong size: expected %d,%d but got %d,%d",
%! size (expect), size (get));
%! elseif (any (any (expect != get)))
%! error ("didn't get what was expected.");
%! endif
</pre></div>
<p>To make the process easier, use the <code>assert</code> function. For example,
with <code>assert</code> the previous test is reduced to:
</p>
<div class="example">
<pre class="example">%!test
%! <var>a</var> = [1, 2, 3; 4, 5, 6]; <var>b</var> = [1; 2];
%! assert (kron (<var>b</var>, <var>a</var>), [ <var>a</var>; 2*<var>a</var> ]);
</pre></div>
<p><code>assert</code> can accept a tolerance so that you can compare results
absolutely or relatively. For example, the following all succeed:
</p>
<div class="example">
<pre class="example">%!test assert (1+eps, 1, 2*eps) # absolute error
%!test assert (100+100*eps, 100, -2*eps) # relative error
</pre></div>
<p>You can also do the comparison yourself, but still have assert
generate the error:
</p>
<div class="example">
<pre class="example">%!test assert (isempty ([]))
%!test assert ([1, 2; 3, 4] > 0)
</pre></div>
<p>Because <code>assert</code> is so frequently used alone in a test block, there
is a shorthand form:
</p>
<div class="example">
<pre class="example">%!assert (…)
</pre></div>
<p>which is equivalent to:
</p>
<div class="example">
<pre class="example">%!test assert (…)
</pre></div>
<p>Occasionally a block of tests will depend on having optional
functionality in Octave. Before testing such blocks the availability of
the required functionality must be checked. A <code>%!testif HAVE_XXX</code>
block will only be run if Octave was compiled with functionality
‘<samp>HAVE_XXX</samp>’. For example, the sparse single value decomposition,
<code>svds()</code>, depends on having the <small>ARPACK</small> library. All of the tests
for <code>svds</code> begin with
</p>
<div class="example">
<pre class="example">%!testif HAVE_ARPACK
</pre></div>
<p>Review <samp>config.h</samp> or <code>octave_config_info ("features")</code> to see some
of the possible values to check.
</p>
<p>Sometimes during development there is a test that should work but is
known to fail. You still want to leave the test in because when the
final code is ready the test should pass, but you may not be able to
fix it immediately. To avoid unnecessary bug reports for these known
failures, mark the block with <code>xtest</code> rather than <code>test</code>:
</p>
<div class="example">
<pre class="example">%!xtest assert (1==0)
%!xtest fail ("success=1", "error")
</pre></div>
<p>In this case, the test will run and any failure will be reported.
However, testing is not aborted and subsequent test blocks will be
processed normally. Another use of <code>xtest</code> is for statistical
tests which should pass most of the time but are known to fail
occasionally.
</p>
<p>Each block is evaluated in its own function environment, which means
that variables defined in one block are not automatically shared
with other blocks. If you do want to share variables, then you
must declare them as <code>shared</code> before you use them. For example, the
following declares the variable <var>a</var>, gives it an initial value (default
is empty), and then uses it in several subsequent tests.
</p>
<div class="example">
<pre class="example">%!shared <var>a</var>
%! <var>a</var> = [1, 2, 3; 4, 5, 6];
%!assert (kron ([1; 2], <var>a</var>), [ <var>a</var>; 2*<var>a</var> ]);
%!assert (kron ([1, 2], <var>a</var>), [ <var>a</var>, 2*<var>a</var> ]);
%!assert (kron ([1,2; 3,4], <var>a</var>), [ <var>a</var>,2*<var>a</var>; 3*<var>a</var>,4*<var>a</var> ]);
</pre></div>
<p>You can share several variables at the same time:
</p>
<div class="example">
<pre class="example">%!shared <var>a</var>, <var>b</var>
</pre></div>
<p>You can also share test functions:
</p>
<div class="example">
<pre class="example">%!function <var>a</var> = fn (<var>b</var>)
%! <var>a</var> = 2*<var>b</var>;
%!endfunction
%!assert (fn(2), 4);
</pre></div>
<p>Note that all previous variables and values are lost when a new
shared block is declared.
</p>
<p>Remember that <code>%!function</code> begins a new block and that
<code>%!endfunction</code> ends this block. Be aware that until a new block
is started, lines starting with ‘<samp>%!<space></samp>’ will be discarded as comments.
The following is nearly identical to the example above, but does nothing.
</p>
<div class="example">
<pre class="example">%!function <var>a</var> = fn (<var>b</var>)
%! <var>a</var> = 2*<var>b</var>;
%!endfunction
%! assert (fn(2), 4);
</pre></div>
<p>Because there is a space after ‘<samp>%!</samp>’ the <code>assert</code> statement does
not begin a new block and this line is treated as a comment.
</p>
<p>Error and warning blocks are like test blocks, but they only succeed
if the code generates an error. You can check the text of the error
is correct using an optional regular expression <code><pattern></code>.
For example:
</p>
<div class="example">
<pre class="example">%!error <passes!> error ("this test passes!");
</pre></div>
<p>If the code doesn’t generate an error, the test fails. For example:
</p>
<div class="example">
<pre class="example">%!error "this is an error because it succeeds.";
</pre></div>
<p>produces
</p>
<div class="example">
<pre class="example"> ***** error "this is an error because it succeeds.";
!!!!! test failed: no error
</pre></div>
<p>It is important to automate the tests as much as possible, however
some tests require user interaction. These can be isolated into
demo blocks, which if you are in batch mode, are only run when
called with <code>demo</code> or the <code>verbose</code> option to <code>test</code>.
The code is displayed before it is executed. For example,
</p>
<div class="example">
<pre class="example">%!demo
%! <var>t</var> = [0:0.01:2*pi]; <var>x</var> = sin (<var>t</var>);
%! plot (<var>t</var>, <var>x</var>);
%! # you should now see a sine wave in your figure window
</pre></div>
<p>produces
</p>
<div class="example">
<pre class="example">funcname example 1:
<var>t</var> = [0:0.01:2*pi]; <var>x</var> = sin (<var>t</var>);
plot (<var>t</var>, <var>x</var>);
# you should now see a sine wave in your figure window
Press <enter> to continue:
</pre></div>
<p>Note that demo blocks cannot use any shared variables. This is so
that they can be executed by themselves, ignoring all other tests.
</p>
<p>If you want to temporarily disable a test block, put <code>#</code> in place
of the block type. This creates a comment block which is echoed
in the log file but not executed. For example:
</p>
<div class="example">
<pre class="example">%!#demo
%! <var>t</var> = [0:0.01:2*pi]; <var>x</var> = sin (<var>t</var>);
%! plot (<var>t</var>, <var>x</var>);
%! # you should now see a sine wave in your figure window
</pre></div>
<p>The following trivial code snippet provides examples for the use of
fail, assert, error and xtest:
</p>
<div class="example">
<pre class="example">function output = must_be_zero (<var>input</var>)
if (<var>input</var> != 0)
error ("Non-zero input!")
endif
output = input;
endfunction
%!fail ("must_be_zero (1)");
%!assert (must_be_zero (0), 0);
%!error <Non-zero> must_be_zero (1);
%!xtest error ("This code generates an error");
</pre></div>
<p>When putting this a file <samp>must_be_zero.m</samp>, and running the test, we see
</p>
<div class="example">
<pre class="example">test must_be_zero verbose
⇒
>>>>> /path/to/must_be_zero.m
***** fail ("must_be_zero (1)");
***** assert (must_be_zero (0), 0);
***** error <Non-zero> must_be_zero (1);
***** xtest error ("This code generates an error");
!!!!! known failure
This code generates an error
PASSES 4 out of 4 tests (1 expected failures)
</pre></div>
<a name="Block-type-summary_003a"></a>
<h4 class="subsubheading">Block type summary:</h4>
<dl compact="compact">
<dt><code>%!test</code></dt>
<dd><p>check that entire block is correct
</p>
</dd>
<dt><code>%!testif HAVE_XXX</code></dt>
<dd><p>check block only if Octave was compiled with feature HAVE_XXX.
</p>
</dd>
<dt><code>%!xtest</code></dt>
<dd><p>check block, report a test failure but do not abort testing.
</p>
</dd>
<dt><code>%!error</code></dt>
<dd><p>check for correct error message
</p>
</dd>
<dt><code>%!warning</code></dt>
<dd><p>check for correct warning message
</p>
</dd>
<dt><code>%!demo</code></dt>
<dd><p>demo only executes in interactive mode
</p>
</dd>
<dt><code>%!#</code></dt>
<dd><p>comment: ignore everything within the block
</p>
</dd>
<dt><code>%!shared x,y,z</code></dt>
<dd><p>declare variables for use in multiple tests
</p>
</dd>
<dt><code>%!function</code></dt>
<dd><p>define a function for use in multiple tests
</p>
</dd>
<dt><code>%!endfunction</code></dt>
<dd><p>close a function definition
</p>
</dd>
<dt><code>%!assert (x, y, tol)</code></dt>
<dd><p>shorthand for <code>%!test assert (x, y, tol)</code>
</p></dd>
</dl>
<p>You can also create test scripts for built-in functions and your own C++
functions. To do so, put a file with the bare function name (no .m
extension) in a directory in the load path and it will be discovered by
the <code>test</code> function. Alternatively, you can embed tests directly in your
C++ code:
</p>
<div class="example">
<pre class="example">/*
%!test disp ("this is a test")
*/
</pre></div>
<p>or
</p>
<div class="example">
<pre class="example">#if 0
%!test disp ("this is a test")
#endif
</pre></div>
<p>However, in this case the raw source code will need to be on the load
path and the user will have to remember to type
<code>test ("funcname.cc")</code>.
</p>
<a name="XREFassert"></a><dl>
<dt><a name="index-assert"></a>Function File: <em></em> <strong>assert</strong> <em>(<var>cond</var>)</em></dt>
<dt><a name="index-assert-1"></a>Function File: <em></em> <strong>assert</strong> <em>(<var>cond</var>, <var>errmsg</var>, …)</em></dt>
<dt><a name="index-assert-2"></a>Function File: <em></em> <strong>assert</strong> <em>(<var>cond</var>, <var>msg_id</var>, <var>errmsg</var>, …)</em></dt>
<dt><a name="index-assert-3"></a>Function File: <em></em> <strong>assert</strong> <em>(<var>observed</var>, <var>expected</var>)</em></dt>
<dt><a name="index-assert-4"></a>Function File: <em></em> <strong>assert</strong> <em>(<var>observed</var>, <var>expected</var>, <var>tol</var>)</em></dt>
<dd>
<p>Produce an error if the specified condition is not met. <code>assert</code> can
be called in three different ways.
</p>
<dl compact="compact">
<dt><code>assert (<var>cond</var>)</code></dt>
<dt><code>assert (<var>cond</var>, <var>errmsg</var>, …)</code></dt>
<dt><code>assert (<var>cond</var>, <var>msg_id</var>, <var>errmsg</var>, …)</code></dt>
<dd><p>Called with a single argument <var>cond</var>, <code>assert</code> produces an
error if <var>cond</var> is zero. When called with more than one argument the
additional arguments are passed to the <code>error</code> function.
</p>
</dd>
<dt><code>assert (<var>observed</var>, <var>expected</var>)</code></dt>
<dd><p>Produce an error if observed is not the same as expected. Note that
<var>observed</var> and <var>expected</var> can be scalars, vectors, matrices,
strings, cell arrays, or structures.
</p>
</dd>
<dt><code>assert (<var>observed</var>, <var>expected</var>, <var>tol</var>)</code></dt>
<dd><p>Produce an error if observed is not the same as expected but equality
comparison for numeric data uses a tolerance <var>tol</var>.
If <var>tol</var> is positive then it is an absolute tolerance which will produce
an error if <code>abs (<var>observed</var> - <var>expected</var>) > abs (<var>tol</var>)</code>.
If <var>tol</var> is negative then it is a relative tolerance which will produce
an error if <code>abs (<var>observed</var> - <var>expected</var>) >
abs (<var>tol</var> * <var>expected</var>)</code>. If <var>expected</var> is zero <var>tol</var> will
always be interpreted as an absolute tolerance. If <var>tol</var> is not scalar
its dimensions must agree with those of <var>observed</var> and <var>expected</var>
and tests are performed on an element-wise basis.
</p></dd>
</dl>
<p><strong>See also:</strong> <a href="#XREFtest">test</a>, <a href="#XREFfail">fail</a>, <a href="Raising-Errors.html#XREFerror">error</a>.
</p></dd></dl>
<a name="XREFfail"></a><dl>
<dt><a name="index-fail"></a>Function File: <em></em> <strong>fail</strong> <em>(<var>code</var>)</em></dt>
<dt><a name="index-fail-1"></a>Function File: <em></em> <strong>fail</strong> <em>(<var>code</var>, <var>pattern</var>)</em></dt>
<dt><a name="index-fail-2"></a>Function File: <em></em> <strong>fail</strong> <em>(<var>code</var>, "warning", <var>pattern</var>)</em></dt>
<dd>
<p>Return true if <var>code</var> fails with an error message matching
<var>pattern</var>, otherwise produce an error. Note that <var>code</var>
is a string and if <var>code</var> runs successfully, the error produced is:
</p>
<div class="example">
<pre class="example"> expected error <.> but got none
</pre></div>
<p>Code must be in the form of a string that may be passed by
<code>fail</code> to the Octave interpreter via the <code>evalin</code> function,
that is, a (quoted) string constant or a string variable.
</p>
<p>If called with two arguments, the behavior is similar to
<code>fail (<var>code</var>)</code>, except the return value will only be true if
code fails with an error message containing pattern (case sensitive).
If the code fails with a different error to that given in pattern,
the message produced is:
</p>
<div class="example">
<pre class="example"> expected <pattern>
but got <text of actual error>
</pre></div>
<p>The angle brackets are not part of the output.
</p>
<p>Called with three arguments, the behavior is similar to
<code>fail (<var>code</var>, <var>pattern</var>)</code>, but produces an error if no
warning is given during code execution or if the code fails.
</p>
<p><strong>See also:</strong> <a href="#XREFassert">assert</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Demonstration-Functions.html#Demonstration-Functions" accesskey="n" rel="next">Demonstration Functions</a>, Up: <a href="Test-and-Demo-Functions.html#Test-and-Demo-Functions" accesskey="u" rel="up">Test and Demo Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|