This file is indexed.

/usr/share/doc/octave/octave.html/Test-Functions.html is in octave-doc 4.2.2-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Functions (GNU Octave)</title>

<meta name="description" content="Test Functions (GNU Octave)">
<meta name="keywords" content="Test Functions (GNU Octave)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
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.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<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> &nbsp; [<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>: <em></em> <strong>test</strong> <em><var>name</var></em></dt>
<dt><a name="index-test-1"></a>: <em></em> <strong>test</strong> <em><var>name</var> quiet|normal|verbose</em></dt>
<dt><a name="index-test-2"></a>: <em></em> <strong>test</strong> <em>(&quot;<var>name</var>&quot;, &quot;quiet|normal|verbose&quot;, <var>fid</var>)</em></dt>
<dt><a name="index-test-3"></a>: <em></em> <strong>test</strong> <em>(&quot;<var>name</var>&quot;, &quot;quiet|normal|verbose&quot;, <var>fname</var>)</em></dt>
<dt><a name="index-test-4"></a>: <em><var>success</var> =</em> <strong>test</strong> <em>(&hellip;)</em></dt>
<dt><a name="index-test-5"></a>: <em>[<var>n</var>, <var>nmax</var>, <var>nxfail</var>, <var>nskip</var>] =</em> <strong>test</strong> <em>(&hellip;)</em></dt>
<dt><a name="index-test-6"></a>: <em>[<var>code</var>, <var>idx</var>] =</em> <strong>test</strong> <em>(&quot;<var>name</var>&quot;, &quot;grabdemo&quot;)</em></dt>
<dt><a name="index-test-7"></a>: <em></em> <strong>test</strong> <em>([], &quot;explain&quot;, <var>fid</var>)</em></dt>
<dt><a name="index-test-8"></a>: <em></em> <strong>test</strong> <em>([], &quot;explain&quot;, <var>fname</var>)</em></dt>
<dd>
<p>Perform built-in self-tests from the first file in the loadpath matching
<var>name</var>.
</p>
<p><code>test</code> can be called in either command or functional form.  The exact
operation of test is determined by a combination of mode (interactive or
batch), reporting level (<code>&quot;quiet&quot;</code>, <code>&quot;normal&quot;</code>,
<code>&quot;verbose&quot;</code>), and whether a logfile or summary output variable is
used.
</p>
<p>The default mode when <code>test</code> is called from the command line is
interactive.  In this mode, tests will be run until the first error is
encountered, or all tests complete successfully.  In batch mode, all tests
are run regardless of any failures, and the results are collected for
reporting.  Tests which require user interaction, i.e., demo blocks,
are never run in batch mode.
</p>
<p>Batch mode is enabled by either 1) specifying a logfile using the third
argument <var>fname</var> or <var>fid</var>, or 2) requesting an output argument
such as <var>success</var>, <var>n</var>, etc.
</p>
<p>The optional second argument determines the amount of output to generate and
which types of tests to run.  The default value is <code>&quot;normal&quot;</code>.
Requesting an output argument will suppress printing the final summary
message and any intermediate warnings, unless verbose reporting is
enabled.
</p>
<dl compact="compact">
<dt><code>&quot;quiet&quot;</code></dt>
<dd><p>Print a summary message when all tests pass, or print an error with the
results of the first bad test when a failure occurs.  Don&rsquo;t run tests which
require user interaction.
</p>
</dd>
<dt><code>&quot;normal&quot;</code></dt>
<dd><p>Display warning messages about skipped tests or failing xtests during test
execution.
Print a summary message when all tests pass, or print an error with the
results of the first bad test when a failure occurs.  Don&rsquo;t run tests which
require user interaction.
</p>
</dd>
<dt><code>&quot;verbose&quot;</code></dt>
<dd><p>Display tests before execution.  Print all warning messages.  In interactive
mode, run all tests including those which require user interaction.
</p></dd>
</dl>

<p>The optional third input argument specifies a logfile where results of the
tests should be written.  The logfile may be a character string
(<var>fname</var>) or an open file descriptor ID (<var>fid</var>).  To enable batch
processing, but still print the results to the screen, use <code>stdout</code> for
<var>fid</var>.
</p>
<p>When called with just a single output argument <var>success</var>, <code>test</code>
returns true if all of the tests were successful.  If called with more
than one output argument then the number of successful tests (<var>n</var>),
the total number of tests in the file (<var>nmax</var>), the number of xtest
failures (<var>nxfail</var>), and the number of skipped tests (<var>nskip</var>) are
returned.
</p>
<p>Example
</p>
<div class="example">
<pre class="example">test sind
&rArr;
PASSES 5 out of 5 tests

[n, nmax] = test (&quot;sind&quot;)
&rArr;
n =  5
nmax =  5
</pre></div>

<p>Additional Calling Syntaxes
</p>
<p>If the second argument is the string <code>&quot;grabdemo&quot;</code>, the contents of
any built-in demo blocks are extracted but not executed.  The text 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 each demo block.  For an easier way to
extract demo blocks from files, See <a href="Demonstration-Functions.html#XREFexample">example</a>.
</p>
<p>If the second argument is <code>&quot;explain&quot;</code> then <var>name</var> is ignored and
an explanation of the line markers used in <code>test</code> output reports is
written to the file specified by <var>fname</var> or <var>fid</var>.
</p>

<p><strong>See also:</strong> <a href="#XREFassert">assert</a>, <a href="#XREFfail">fail</a>, <a href="Demonstration-Functions.html#XREFdemo">demo</a>, <a href="Demonstration-Functions.html#XREFexample">example</a>, <a href="Raising-Errors.html#XREFerror">error</a>.
</p></dd></dl>


<p><code>test</code> scans the named script file looking for lines which start
with the identifier &lsquo;<samp>%!</samp>&rsquo;.  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 &lsquo;<samp>%!</samp>&rsquo;.
A block is defined by indentation as in Python.  Lines beginning with
&lsquo;<samp>%!&lt;whitespace&gt;</samp>&rsquo; are part of the preceeding block.
</p>
<p>For example:
</p>
<div class="example">
<pre class="example">%!test error (&quot;this test fails!&quot;)
%!test &quot;test doesn't fail.  it doesn't generate an error&quot;
</pre></div>

<p>When a test fails, you will see something like:
</p>
<div class="example">
<pre class="example">  ***** test error (&quot;this test fails!&quot;)
!!!!! 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 (&quot;wrong size: expected %d,%d but got %d,%d&quot;,
%!          size (expect), size (get));
%! elseif (any (any (expect != get)))
%!   error (&quot;didn't get what was expected.&quot;);
%! 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] &gt; 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 (&hellip;)
</pre></div>

<p>which is equivalent to:
</p>
<div class="example">
<pre class="example">%!test assert (&hellip;)
</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
&lsquo;<samp>HAVE_XXX</samp>&rsquo;.  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__ (&quot;build_features&quot;)</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 (&quot;success=1&quot;, &quot;error&quot;)
</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 &lsquo;<samp>%!&lt;space&gt;</samp>&rsquo; 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 &lsquo;<samp>%!</samp>&rsquo; 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>&lt;pattern&gt;</code>.
For example:
</p>
<div class="example">
<pre class="example">%!error &lt;passes!&gt; error (&quot;this test passes!&quot;)
</pre></div>

<p>If the code doesn&rsquo;t generate an error, the test fails.  For example:
</p>
<div class="example">
<pre class="example">%!error &quot;this is an error because it succeeds.&quot;
</pre></div>

<p>produces
</p>
<div class="example">
<pre class="example">  ***** error &quot;this is an error because it succeeds.&quot;
!!!!! 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 &lt;enter&gt; 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 <var>output</var> = must_be_zero (<var>input</var>)
  if (<var>input</var> != 0)
    error (&quot;Nonzero input!&quot;)
  endif
  <var>output</var> = <var>input</var>;
endfunction

%!fail (&quot;must_be_zero (1)&quot;)
%!assert (must_be_zero (0), 0)
%!error &lt;Nonzero&gt; must_be_zero (1)
%!xtest error (&quot;This code generates an error&quot;)
</pre></div>

<p>When putting this in 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

&rArr;
&gt;&gt;&gt;&gt;&gt; /path/to/must_be_zero.m
***** fail (&quot;must_be_zero (1)&quot;)
***** assert (must_be_zero (0), 0)
***** error &lt;Nonzero&gt; must_be_zero (1)
***** xtest error (&quot;This code generates an error&quot;)
!!!!! known failure
This code generates an error
PASSES 3 out of 4 tests (1 expected failure)
</pre></div>

<a name="Block-type-summary_003a"></a>
<h4 class="subsubheading">Block type summary:</h4>

<dl compact="compact">
<dt><code>%!test</code></dt>
<dt><code>%!test &lt;MESSAGE&gt;</code></dt>
<dd><p>Check that entire block is correct.  If <code>&lt;MESSAGE&gt;</code> is present, the
test block is interpreted as for <code>xtest</code>.
</p>
</dd>
<dt><code>%!testif HAVE_XXX</code></dt>
<dt><code>%!testif HAVE_XXX, HAVE_YYY, &hellip;</code></dt>
<dt><code>%!testif &hellip; &lt;MESSAGE&gt;</code></dt>
<dd><p>Check block only if Octave was compiled with feature HAVE_XXX.  If
<code>&lt;MESSAGE&gt;</code> is present, the test block is interpreted as for
<code>xtest</code>.
</p>
</dd>
<dt><code>%!xtest</code></dt>
<dt><code>%!xtest &lt;MESSAGE&gt;</code></dt>
<dd><p>Check block, report a test failure but do not abort testing.  If
<code>&lt;MESSAGE&gt;</code> is present, then the text of the message is displayed
if the test fails, like this:
</p>
<div class="example">
<pre class="example">!!!!! Known bug:  MESSAGE
</pre></div>

<p>If the message is an integer, it is interpreted as a bug ID for the
Octave bug tracker and reported as
</p>
<div class="example">
<pre class="example">!!!!! Known bug: http://octave.org/testfailure/?BUG-ID
</pre></div>

<p>in which BUG-ID is the integer bug number.  The intent is to allow
clearer documentation of known problems.
</p>
</dd>
<dt><code>%!error</code></dt>
<dt><code>%!error &lt;MESSAGE&gt;</code></dt>
<dt><code>%!warning</code></dt>
<dt><code>%!warning &lt;MESSAGE&gt;</code></dt>
<dd><p>Check for correct error or warning message.  If <code>&lt;MESSAGE&gt;</code> is
supplied it is interpreted as a regular expression pattern that is
expected to match the error or 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>
<dt><code>%!assert &lt;MESSAGE&gt; (x, y, tol)</code></dt>
<dt><code>%!fail (CODE, PATTERN)</code></dt>
<dt><code>%!fail &lt;MESSAGE&gt; (CODE, PATTERN)</code></dt>
<dd><p>Shorthand for <code>%!test assert (x, y, tol)</code> or
<code>%!test fail (CODE, PATTERN)</code>.  If <code>&lt;MESSAGE&gt;</code> is present, the
test block is interpreted as for <code>xtest</code>.
</p>
</dd>
</dl>

<a name="test_002dmessage_002danchor"></a>
<p>When coding tests the Octave convention is that lines that begin with a block
type do not have a semicolon at the end.  Any code that is within a block,
however, is normal Octave code and usually will have a trailing semicolon.
For example,
</p>
<div class="example">
<pre class="example">## bare block instantiation
%!assert (sin (0), 0)
</pre></div>

<p>but
</p>
<div class="example">
<pre class="example">## test block with normal Octave code
%!test
%! assert (sin (0), 0);
</pre></div>

<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 (&quot;this is a test&quot;)
*/
</pre></div>

<p>or
</p>
<div class="example">
<pre class="example">#if 0
%!test disp (&quot;this is a test&quot;)
#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 (&quot;funcname.cc&quot;)</code>.
</p>
<a name="XREFassert"></a><dl>
<dt><a name="index-assert"></a>: <em></em> <strong>assert</strong> <em>(<var>cond</var>)</em></dt>
<dt><a name="index-assert-1"></a>: <em></em> <strong>assert</strong> <em>(<var>cond</var>, <var>errmsg</var>)</em></dt>
<dt><a name="index-assert-2"></a>: <em></em> <strong>assert</strong> <em>(<var>cond</var>, <var>errmsg</var>, &hellip;)</em></dt>
<dt><a name="index-assert-3"></a>: <em></em> <strong>assert</strong> <em>(<var>cond</var>, <var>msg_id</var>, <var>errmsg</var>, &hellip;)</em></dt>
<dt><a name="index-assert-4"></a>: <em></em> <strong>assert</strong> <em>(<var>observed</var>, <var>expected</var>)</em></dt>
<dt><a name="index-assert-5"></a>: <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.
</p>
<p><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>errmsg</var>, &hellip;)</code></dt>
<dt><code>assert (<var>cond</var>, <var>msg_id</var>, <var>errmsg</var>, &hellip;)</code></dt>
<dd><p>Called with a single argument <var>cond</var>, <code>assert</code> produces an error if
<var>cond</var> is false (numeric zero).
</p>
<p>Any additional arguments are passed to the <code>error</code> function for
processing.
</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.
</p>
<p>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>.
</p>
<p>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>) &gt; abs (<var>tol</var>)</code>.
</p>
<p>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>) &gt;
abs (<var>tol</var> * <var>expected</var>)</code>.
</p>
<p>If <var>expected</var> is zero <var>tol</var> will always be interpreted as an
absolute tolerance.
</p>
<p>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-by-element basis.
</p></dd>
</dl>

<p><strong>See also:</strong> <a href="#XREFfail">fail</a>, <a href="#XREFtest">test</a>, <a href="Raising-Errors.html#XREFerror">error</a>, <a href="Comparison-Ops.html#XREFisequal">isequal</a>.
</p></dd></dl>


<a name="XREFfail"></a><dl>
<dt><a name="index-fail"></a>: <em></em> <strong>fail</strong> <em>(<var>code</var>)</em></dt>
<dt><a name="index-fail-1"></a>: <em></em> <strong>fail</strong> <em>(<var>code</var>, <var>pattern</var>)</em></dt>
<dt><a name="index-fail-2"></a>: <em></em> <strong>fail</strong> <em>(<var>code</var>, &quot;warning&quot;)</em></dt>
<dt><a name="index-fail-3"></a>: <em></em> <strong>fail</strong> <em>(<var>code</var>, &quot;warning&quot;, <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.
</p>
<p><var>code</var> must be in the form of a string that is passed to the Octave
interpreter via the <code>evalin</code> function, i.e., a (quoted) string constant
or a string variable.
</p>
<p>Note that if <var>code</var> runs successfully, rather than failing, the error
printed is:
</p>
<div class="example">
<pre class="example">          expected error &lt;.&gt; but got none
</pre></div>

<p>If called with two arguments, the return value will be true only if
<var>code</var> fails with an error message containing <var>pattern</var>
(case sensitive).  If the code fails with a different error than the one
specified in <var>pattern</var> then the message produced is:
</p>
<div class="example">
<pre class="example">          expected &lt;<var>pattern</var>&gt;
          but got &lt;text of actual error&gt;
</pre></div>

<p>The angle brackets are not part of the output.
</p>
<p>When called with the <code>&quot;warning&quot;</code> option <code>fail</code> will produce an
error if executing the code produces no warning.
</p>
<p><strong>See also:</strong> <a href="#XREFassert">assert</a>, <a href="Raising-Errors.html#XREFerror">error</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> &nbsp; [<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>