Mercurial > octave
annotate doc/interpreter/testfun.txi @ 21226:a55b8ece1ecd
reorganize octave_config_info again
* build-env-features.sh: Don't include ENABLE_ items in the output.
* toplev.cc (find_config_info): New static function.
(Foctave_config_info): Put Octave configuration info in the main
struct. Put the build system features and build environment info in
substructures. Allow searching of all elements by keyword.
* __have_feature__.m, doc/interpreter/testfun.txi:
Fix name of build features substructure element.
* geometryimages.m, interpimages.m, plotimages.m, sparseimages.m,
splineimages.m: Use __have_feature__ to check for OSMESA.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 08 Feb 2016 23:14:56 -0500 |
parents | 4197fc428c7d |
children | a4faec57f4c8 |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19596
diff
changeset
|
1 @c Copyright (C) 2005-2015 David Bateman |
11523 | 2 @c Copyright (C) 2002-2005 Paul Kienzle |
7018 | 3 @c |
4 @c This file is part of Octave. | |
5 @c | |
6 @c Octave is free software; you can redistribute it and/or modify it | |
7 @c under the terms of the GNU General Public License as published by the | |
8 @c Free Software Foundation; either version 3 of the License, or (at | |
9 @c your option) any later version. | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
10 @c |
7018 | 11 @c Octave is distributed in the hope that it will be useful, but WITHOUT |
12 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 @c for more details. | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
15 @c |
7018 | 16 @c You should have received a copy of the GNU General Public License |
17 @c along with Octave; see the file COPYING. If not, see | |
18 @c <http://www.gnu.org/licenses/>. | |
5582 | 19 |
20 @node Test and Demo Functions | |
21 @appendix Test and Demo Functions | |
22 @cindex test functions | |
23 | |
24 Octave includes a number of functions to allow the integration of testing | |
25 and demonstration code in the source code of the functions themselves. | |
26 | |
27 @menu | |
28 * Test Functions:: | |
29 * Demonstration Functions:: | |
30 @end menu | |
31 | |
32 @node Test Functions | |
33 @section Test Functions | |
34 | |
35 @DOCSTRING(test) | |
36 | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
37 @code{test} scans the named script file looking for lines which start |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
38 with the identifier @samp{%!}. The prefix is stripped off and the rest |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
39 of the line is processed through the Octave interpreter. If the code |
5582 | 40 generates an error, then the test is said to fail. |
41 | |
42 Since @code{eval()} will stop at the first error it encounters, you must | |
43 divide your tests up into blocks, with anything in a separate | |
16247
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
44 block evaluated separately. Blocks are introduced by valid keywords like |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
45 @code{test}, @code{function}, or @code{assert} immediately following @samp{%!}. |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
46 A block is defined by indentation as in Python. Lines beginning with |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
47 @samp{%!<whitespace>} are part of the preceeding block. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
48 |
16247
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
49 For example: |
5582 | 50 |
51 @example | |
52 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
53 %!test error ("this test fails!"); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
54 %!test "test doesn't fail. it doesn't generate an error"; |
5582 | 55 @end group |
56 @end example | |
57 | |
58 When a test fails, you will see something like: | |
59 | |
60 @example | |
61 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
62 ***** test error ("this test fails!") |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
63 !!!!! test failed |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
64 this test fails! |
5582 | 65 @end group |
66 @end example | |
67 | |
68 Generally, to test if something works, you want to assert that it | |
69 produces a correct value. A real test might look something like | |
70 | |
71 @example | |
72 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
73 %!test |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
74 %! @var{a} = [1, 2, 3; 4, 5, 6]; B = [1; 2]; |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
75 %! expect = [ @var{a} ; 2*@var{a} ]; |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
76 %! get = kron (@var{b}, @var{a}); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
77 %! if (any (size (expect) != size (get))) |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
78 %! error ("wrong size: expected %d,%d but got %d,%d", |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
79 %! size (expect), size (get)); |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
80 %! elseif (any (any (expect != get))) |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
81 %! error ("didn't get what was expected."); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
82 %! endif |
5582 | 83 @end group |
84 @end example | |
85 | |
86 To make the process easier, use the @code{assert} function. For example, | |
87 with @code{assert} the previous test is reduced to: | |
88 | |
89 @example | |
90 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
91 %!test |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
92 %! @var{a} = [1, 2, 3; 4, 5, 6]; @var{b} = [1; 2]; |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
93 %! assert (kron (@var{b}, @var{a}), [ @var{a}; 2*@var{a} ]); |
5582 | 94 @end group |
95 @end example | |
96 | |
97 @code{assert} can accept a tolerance so that you can compare results | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
98 absolutely or relatively. For example, the following all succeed: |
5582 | 99 |
100 @example | |
101 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
102 %!test assert (1+eps, 1, 2*eps) # absolute error |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
103 %!test assert (100+100*eps, 100, -2*eps) # relative error |
5582 | 104 @end group |
105 @end example | |
106 | |
107 You can also do the comparison yourself, but still have assert | |
108 generate the error: | |
109 | |
110 @example | |
111 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
112 %!test assert (isempty ([])) |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
113 %!test assert ([1, 2; 3, 4] > 0) |
5582 | 114 @end group |
115 @end example | |
116 | |
117 Because @code{assert} is so frequently used alone in a test block, there | |
118 is a shorthand form: | |
119 | |
120 @example | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
121 %!assert (@dots{}) |
5582 | 122 @end example |
123 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
124 @noindent |
5582 | 125 which is equivalent to: |
126 | |
127 @example | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
128 %!test assert (@dots{}) |
5582 | 129 @end example |
130 | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
131 Occasionally a block of tests will depend on having optional |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
132 functionality in Octave. Before testing such blocks the availability of |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
133 the required functionality must be checked. A @code{%!testif HAVE_XXX} |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
134 block will only be run if Octave was compiled with functionality |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
135 @samp{HAVE_XXX}. For example, the sparse single value decomposition, |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
136 @code{svds()}, depends on having the @sc{arpack} library. All of the tests |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
137 for @code{svds} begin with |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
138 |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
139 @example |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
140 %!testif HAVE_ARPACK |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
141 @end example |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
142 |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
143 @noindent |
21226
a55b8ece1ecd
reorganize octave_config_info again
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
144 Review @file{config.h} or @code{octave_config_info ("build_features")} |
a55b8ece1ecd
reorganize octave_config_info again
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
145 to see some of the possible values to check. |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
146 |
6728 | 147 Sometimes during development there is a test that should work but is |
148 known to fail. You still want to leave the test in because when the | |
149 final code is ready the test should pass, but you may not be able to | |
7001 | 150 fix it immediately. To avoid unnecessary bug reports for these known |
6728 | 151 failures, mark the block with @code{xtest} rather than @code{test}: |
152 | |
153 @example | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
154 @group |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
155 %!xtest assert (1==0) |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
156 %!xtest fail ("success=1", "error") |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
157 @end group |
6728 | 158 @end example |
159 | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
160 @noindent |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
161 In this case, the test will run and any failure will be reported. |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
162 However, testing is not aborted and subsequent test blocks will be |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
163 processed normally. Another use of @code{xtest} is for statistical |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
164 tests which should pass most of the time but are known to fail |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
165 occasionally. |
6728 | 166 |
5582 | 167 Each block is evaluated in its own function environment, which means |
168 that variables defined in one block are not automatically shared | |
169 with other blocks. If you do want to share variables, then you | |
170 must declare them as @code{shared} before you use them. For example, the | |
171 following declares the variable @var{a}, gives it an initial value (default | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
172 is empty), and then uses it in several subsequent tests. |
5582 | 173 |
174 @example | |
175 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
176 %!shared @var{a} |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
177 %! @var{a} = [1, 2, 3; 4, 5, 6]; |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
178 %!assert (kron ([1; 2], @var{a}), [ @var{a}; 2*@var{a} ]); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
179 %!assert (kron ([1, 2], @var{a}), [ @var{a}, 2*@var{a} ]); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
180 %!assert (kron ([1,2; 3,4], @var{a}), [ @var{a},2*@var{a}; 3*@var{a},4*@var{a} ]); |
5582 | 181 @end group |
182 @end example | |
183 | |
184 You can share several variables at the same time: | |
185 | |
186 @example | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
187 %!shared @var{a}, @var{b} |
5582 | 188 @end example |
189 | |
190 You can also share test functions: | |
191 | |
192 @example | |
193 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
194 %!function @var{a} = fn (@var{b}) |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
195 %! @var{a} = 2*@var{b}; |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
196 %!endfunction |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
197 %!assert (fn(2), 4); |
5582 | 198 @end group |
199 @end example | |
200 | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
201 Note that all previous variables and values are lost when a new |
5582 | 202 shared block is declared. |
203 | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
204 Remember that @code{%!function} begins a new block and that |
16247
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
205 @code{%!endfunction} ends this block. Be aware that until a new block |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
206 is started, lines starting with @samp{%!<space>} will be discarded as comments. |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
207 The following is nearly identical to the example above, but does nothing. |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
208 |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
209 @example |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
210 @group |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
211 %!function @var{a} = fn (@var{b}) |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
212 %! @var{a} = 2*@var{b}; |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
213 %!endfunction |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
214 %! assert (fn(2), 4); |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
215 @end group |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
216 @end example |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
217 |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
218 @noindent |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
219 Because there is a space after @samp{%!} the @code{assert} statement does |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
220 not begin a new block and this line is treated as a comment. |
4dc4da5970e5
doc: Clarify how indentation defines %!test blocks.
Rik <rik@octave.org>
parents:
16216
diff
changeset
|
221 |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
222 Error and warning blocks are like test blocks, but they only succeed |
5582 | 223 if the code generates an error. You can check the text of the error |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
224 is correct using an optional regular expression @code{<pattern>}. |
5582 | 225 For example: |
226 | |
227 @example | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
228 %!error <passes!> error ("this test passes!"); |
5582 | 229 @end example |
230 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
231 If the code doesn't generate an error, the test fails. For example: |
5582 | 232 |
233 @example | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
234 %!error "this is an error because it succeeds."; |
5582 | 235 @end example |
236 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
237 @noindent |
5582 | 238 produces |
239 | |
240 @example | |
241 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
242 ***** error "this is an error because it succeeds."; |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
243 !!!!! test failed: no error |
5582 | 244 @end group |
245 @end example | |
246 | |
247 It is important to automate the tests as much as possible, however | |
248 some tests require user interaction. These can be isolated into | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
249 demo blocks, which if you are in batch mode, are only run when |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
250 called with @code{demo} or the @code{verbose} option to @code{test}. |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
251 The code is displayed before it is executed. For example, |
5582 | 252 |
253 @example | |
254 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
255 %!demo |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
256 %! @var{t} = [0:0.01:2*pi]; @var{x} = sin (@var{t}); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
257 %! plot (@var{t}, @var{x}); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
258 %! # you should now see a sine wave in your figure window |
5582 | 259 @end group |
260 @end example | |
261 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
262 @noindent |
5582 | 263 produces |
264 | |
265 @example | |
266 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
267 funcname example 1: |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
268 @var{t} = [0:0.01:2*pi]; @var{x} = sin (@var{t}); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
269 plot (@var{t}, @var{x}); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
270 # you should now see a sine wave in your figure window |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
271 |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18534
diff
changeset
|
272 Press <enter> to continue: |
5582 | 273 @end group |
274 @end example | |
275 | |
276 Note that demo blocks cannot use any shared variables. This is so | |
277 that they can be executed by themselves, ignoring all other tests. | |
278 | |
279 If you want to temporarily disable a test block, put @code{#} in place | |
280 of the block type. This creates a comment block which is echoed | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
281 in the log file but not executed. For example: |
5582 | 282 |
283 @example | |
284 @group | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
285 %!#demo |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
286 %! @var{t} = [0:0.01:2*pi]; @var{x} = sin (@var{t}); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
287 %! plot (@var{t}, @var{x}); |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
288 %! # you should now see a sine wave in your figure window |
5582 | 289 @end group |
290 @end example | |
291 | |
16190
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
292 @noindent |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
293 The following trivial code snippet provides examples for the use of |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
294 fail, assert, error and xtest: |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
295 |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
296 @example |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
297 @group |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
298 function output = must_be_zero (@var{input}) |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
299 if (@var{input} != 0) |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18534
diff
changeset
|
300 error ("Nonzero input!") |
16190
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
301 endif |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
302 output = input; |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
303 endfunction |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
304 |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
305 %!fail ("must_be_zero (1)"); |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
306 %!assert (must_be_zero (0), 0); |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18534
diff
changeset
|
307 %!error <Nonzero> must_be_zero (1); |
16190
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
308 %!xtest error ("This code generates an error"); |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
309 @end group |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
310 @end example |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
311 |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
312 @noindent |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
313 When putting this a file @file{must_be_zero.m}, and running the test, we see |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
314 |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
315 @example |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
316 @group |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
317 test must_be_zero verbose |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
318 |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
319 @result{} |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
320 >>>>> /path/to/must_be_zero.m |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
321 ***** fail ("must_be_zero (1)"); |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
322 ***** assert (must_be_zero (0), 0); |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18534
diff
changeset
|
323 ***** error <Nonzero> must_be_zero (1); |
16190
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
324 ***** xtest error ("This code generates an error"); |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
325 !!!!! known failure |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
326 This code generates an error |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
327 PASSES 4 out of 4 tests (1 expected failures) |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
328 @end group |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
329 @end example |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
330 |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
331 @subsubheading Block type summary: |
5582 | 332 |
333 @table @code | |
334 @item %!test | |
335 check that entire block is correct | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10496
diff
changeset
|
336 |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
337 @item %!testif HAVE_XXX |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
338 check block only if Octave was compiled with feature HAVE_XXX. |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
339 |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
340 @item %!xtest |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
341 check block, report a test failure but do not abort testing. |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
342 |
5582 | 343 @item %!error |
344 check for correct error message | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10496
diff
changeset
|
345 |
5582 | 346 @item %!warning |
347 check for correct warning message | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10496
diff
changeset
|
348 |
5582 | 349 @item %!demo |
350 demo only executes in interactive mode | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10496
diff
changeset
|
351 |
5582 | 352 @item %!# |
353 comment: ignore everything within the block | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10496
diff
changeset
|
354 |
5582 | 355 @item %!shared x,y,z |
13920
9cf8bd0e13d2
doc: Document %!endfunction keyword
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
356 declare variables for use in multiple tests |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10496
diff
changeset
|
357 |
5582 | 358 @item %!function |
13920
9cf8bd0e13d2
doc: Document %!endfunction keyword
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
359 define a function for use in multiple tests |
9cf8bd0e13d2
doc: Document %!endfunction keyword
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
360 |
9cf8bd0e13d2
doc: Document %!endfunction keyword
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
361 @item %!endfunction |
9cf8bd0e13d2
doc: Document %!endfunction keyword
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
362 close a function definition |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10496
diff
changeset
|
363 |
5582 | 364 @item %!assert (x, y, tol) |
13920
9cf8bd0e13d2
doc: Document %!endfunction keyword
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
365 shorthand for @code{%!test assert (x, y, tol)} |
5582 | 366 @end table |
367 | |
18534
f51c1498b9f3
doc: Replace "builtin" with "built-in" for consistency and correctness.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
368 You can also create test scripts for built-in functions and your own C++ |
f51c1498b9f3
doc: Replace "builtin" with "built-in" for consistency and correctness.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
369 functions. To do so, put a file with the bare function name (no .m |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
370 extension) in a directory in the load path and it will be discovered by |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
371 the @code{test} function. Alternatively, you can embed tests directly in your |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
372 C++ code: |
5582 | 373 |
374 @example | |
375 @group | |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14051
diff
changeset
|
376 /* |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
377 %!test disp ("this is a test") |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14051
diff
changeset
|
378 */ |
5582 | 379 @end group |
380 @end example | |
381 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
382 @noindent |
5582 | 383 or |
384 | |
385 @example | |
386 @group | |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14051
diff
changeset
|
387 #if 0 |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
388 %!test disp ("this is a test") |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14051
diff
changeset
|
389 #endif |
5582 | 390 @end group |
391 @end example | |
392 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
393 @noindent |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
394 However, in this case the raw source code will need to be on the load |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
395 path and the user will have to remember to type |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
396 @code{test ("funcname.cc")}. |
5582 | 397 |
398 @DOCSTRING(assert) | |
399 | |
400 @DOCSTRING(fail) | |
401 | |
402 @node Demonstration Functions | |
403 @section Demonstration Functions | |
404 | |
405 @DOCSTRING(demo) | |
406 | |
14051
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
407 @DOCSTRING(example) |
f07f7dd0d4df
doc: Update documentation chapter on test functions
Rik <octave@nomad.inbox5.com>
parents:
13920
diff
changeset
|
408 |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8481
diff
changeset
|
409 @DOCSTRING(rundemos) |
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8481
diff
changeset
|
410 |
10496
3b77db443cc0
scripts/testfun/runtests.m: new function
John W. Eaton <jwe@octave.org>
parents:
9080
diff
changeset
|
411 @DOCSTRING(runtests) |
3b77db443cc0
scripts/testfun/runtests.m: new function
John W. Eaton <jwe@octave.org>
parents:
9080
diff
changeset
|
412 |
5582 | 413 @DOCSTRING(speed) |