5582
|
1 @c Copyright (C) 2005 David Bateman |
|
2 @c Copyright (C) 2002-2005 Paul Kienzle |
|
3 @c This is part of the Octave manual. |
|
4 @c For copying conditions, see the file gpl.texi. |
|
5 |
|
6 @node Test and Demo Functions |
|
7 @appendix Test and Demo Functions |
|
8 @cindex test functions |
|
9 |
|
10 Octave includes a number of functions to allow the integration of testing |
|
11 and demonstration code in the source code of the functions themselves. |
|
12 |
|
13 @menu |
|
14 * Test Functions:: |
|
15 * Demonstration Functions:: |
|
16 @end menu |
|
17 |
|
18 @node Test Functions |
|
19 @section Test Functions |
|
20 |
|
21 @DOCSTRING(test) |
|
22 |
|
23 @code{test} scans the named script file looking for lines which |
|
24 start with @code{%!}. The prefix is stripped off and the rest of the |
|
25 line is processed through the octave interpreter. If the code |
|
26 generates an error, then the test is said to fail. |
|
27 |
|
28 Since @code{eval()} will stop at the first error it encounters, you must |
|
29 divide your tests up into blocks, with anything in a separate |
|
30 block evaluated separately. Blocks are introduced by the keyword |
|
31 @code{test} immediately following the @code{%!}. For example, |
|
32 |
|
33 @example |
|
34 @group |
6728
|
35 %!test error ("this test fails!"); |
5582
|
36 %!test "this test doesn't fail since it doesn't generate an error"; |
|
37 @end group |
|
38 @end example |
|
39 |
|
40 When a test fails, you will see something like: |
|
41 |
|
42 @example |
|
43 @group |
6728
|
44 ***** test error ('this test fails!') |
5582
|
45 !!!!! test failed |
|
46 this test fails! |
|
47 @end group |
|
48 @end example |
|
49 |
|
50 Generally, to test if something works, you want to assert that it |
|
51 produces a correct value. A real test might look something like |
|
52 |
|
53 @example |
|
54 @group |
|
55 %!test |
|
56 %! @var{a} = [1, 2, 3; 4, 5, 6]; B = [1; 2]; |
|
57 %! expect = [ @var{a} ; 2*@var{a} ]; |
|
58 %! get = kron (@var{b}, @var{a}); |
|
59 %! if (any(size(expect) != size(get))) |
|
60 %! error ("wrong size: expected %d,%d but got %d,%d", |
|
61 %! size(expect), size(get)); |
|
62 %! elseif (any(any(expect!=get))) |
|
63 %! error ("didn't get what was expected."); |
|
64 %! endif |
|
65 @end group |
|
66 @end example |
|
67 |
|
68 To make the process easier, use the @code{assert} function. For example, |
|
69 with @code{assert} the previous test is reduced to: |
|
70 |
|
71 @example |
|
72 @group |
|
73 %!test |
|
74 %! @var{a} = [1, 2, 3; 4, 5, 6]; @var{b} = [1; 2]; |
|
75 %! assert (kron (@var{b}, @var{a}), [ @var{a}; 2*@var{a} ]); |
|
76 @end group |
|
77 @end example |
|
78 |
|
79 @code{assert} can accept a tolerance so that you can compare results |
|
80 absolutely or relatively. For example, the following all succeed: |
|
81 |
|
82 @example |
|
83 @group |
|
84 %!test assert (1+eps, 1, 2*eps) # absolute error |
|
85 %!test assert (100+100*eps, 100, -2*eps) # relative error |
|
86 @end group |
|
87 @end example |
|
88 |
|
89 You can also do the comparison yourself, but still have assert |
|
90 generate the error: |
|
91 |
|
92 @example |
|
93 @group |
|
94 %!test assert (isempty([])) |
|
95 %!test assert ([ 1,2; 3,4 ] > 0) |
|
96 @end group |
|
97 @end example |
|
98 |
|
99 Because @code{assert} is so frequently used alone in a test block, there |
|
100 is a shorthand form: |
|
101 |
|
102 @example |
|
103 %!assert (@dots{}) |
|
104 @end example |
|
105 |
|
106 which is equivalent to: |
|
107 |
|
108 @example |
|
109 %!test assert (@dots{}) |
|
110 @end example |
|
111 |
6728
|
112 Sometimes during development there is a test that should work but is |
|
113 known to fail. You still want to leave the test in because when the |
|
114 final code is ready the test should pass, but you may not be able to |
|
115 fix it immediately. To avoid unecessary bug reports for these known |
|
116 failures, mark the block with @code{xtest} rather than @code{test}: |
|
117 |
|
118 @example |
6731
|
119 %!xtest assert (1==0) |
|
120 %!xtest fail ('success=1','error')) |
6728
|
121 @end example |
|
122 |
|
123 Another use of @code{xtest} is for statistical tests which should |
|
124 pass most of the time but are known to fail occasionally. |
|
125 |
5582
|
126 Each block is evaluated in its own function environment, which means |
|
127 that variables defined in one block are not automatically shared |
|
128 with other blocks. If you do want to share variables, then you |
|
129 must declare them as @code{shared} before you use them. For example, the |
|
130 following declares the variable @var{a}, gives it an initial value (default |
|
131 is empty), then uses it in several subsequent tests. |
|
132 |
|
133 @example |
|
134 @group |
|
135 %!shared @var{a} |
|
136 %! @var{a} = [1, 2, 3; 4, 5, 6]; |
|
137 %!assert (kron ([1; 2], @var{a}), [ @var{a}; 2*@var{a} ]); |
|
138 %!assert (kron ([1, 2], @var{a}), [ @var{a}, 2*@var{a} ]); |
|
139 %!assert (kron ([1,2; 3,4], @var{a}), [ @var{a},2*@var{a}; 3*@var{a},4*@var{a} ]); |
|
140 @end group |
|
141 @end example |
|
142 |
|
143 You can share several variables at the same time: |
|
144 |
|
145 @example |
|
146 %!shared @var{a}, @var{b} |
|
147 @end example |
|
148 |
|
149 You can also share test functions: |
|
150 |
|
151 @example |
|
152 @group |
|
153 %!function @var{a} = fn(@var{b}) |
|
154 %! @var{a} = 2*@var{b}; |
|
155 %!assert (@var{a}(2),4); |
|
156 @end group |
|
157 @end example |
|
158 |
|
159 Note that all previous variables and values are lost when a new |
|
160 shared block is declared. |
|
161 |
|
162 Error and warning blocks are like test blocks, but they only succeed |
|
163 if the code generates an error. You can check the text of the error |
|
164 is correct using an optional regular expression @code{<pattern>}. |
|
165 For example: |
|
166 |
|
167 @example |
|
168 %!error <passes!> error('this test passes!'); |
|
169 @end example |
|
170 |
|
171 If the code doesn't generate an error, the test fails. For example, |
|
172 |
|
173 @example |
|
174 %!error "this is an error because it succeeds."; |
|
175 @end example |
|
176 |
|
177 produces |
|
178 |
|
179 @example |
|
180 @group |
|
181 ***** error "this is an error because it succeeds."; |
|
182 !!!!! test failed: no error |
|
183 @end group |
|
184 @end example |
|
185 |
|
186 It is important to automate the tests as much as possible, however |
|
187 some tests require user interaction. These can be isolated into |
|
188 demo blocks, which if you are in batch mode, are only run when |
|
189 called with @code{demo} or @code{verbose}. The code is displayed before |
|
190 it is executed. For example, |
|
191 |
|
192 @example |
|
193 @group |
|
194 %!demo |
|
195 %! @var{t}=[0:0.01:2*pi]; @var{x}=sin(@var{t}); |
|
196 %! plot(@var{t},@var{x}); |
|
197 %! you should now see a sine wave in your figure window |
|
198 @end group |
|
199 @end example |
|
200 |
|
201 produces |
|
202 |
|
203 @example |
|
204 @group |
|
205 > @var{t}=[0:0.01:2*pi]; @var{x}=sin(@var{t}); |
|
206 > plot(@var{t},@var{x}); |
|
207 > you should now see a sine wave in your figure window |
|
208 Press <enter> to continue: |
|
209 @end group |
|
210 @end example |
|
211 |
|
212 Note that demo blocks cannot use any shared variables. This is so |
|
213 that they can be executed by themselves, ignoring all other tests. |
|
214 |
|
215 If you want to temporarily disable a test block, put @code{#} in place |
|
216 of the block type. This creates a comment block which is echoed |
|
217 in the log file, but is not executed. For example: |
|
218 |
|
219 @example |
|
220 @group |
|
221 %!#demo |
|
222 %! @var{t}=[0:0.01:2*pi]; @var{x}=sin(@var{t}); |
|
223 %! plot(@var{t},@var{x}); |
|
224 %! you should now see a sine wave in your figure window |
|
225 @end group |
|
226 @end example |
|
227 |
|
228 Block type summary: |
|
229 |
|
230 @table @code |
|
231 @item %!test |
|
232 check that entire block is correct |
|
233 @item %!error |
|
234 check for correct error message |
|
235 @item %!warning |
|
236 check for correct warning message |
|
237 @item %!demo |
|
238 demo only executes in interactive mode |
|
239 @item %!# |
|
240 comment: ignore everything within the block |
|
241 @item %!shared x,y,z |
|
242 declares variables for use in multiple tests |
|
243 @item %!function |
|
244 defines a function value for a shared variable |
|
245 @item %!assert (x, y, tol) |
|
246 shorthand for %!test assert (x, y, tol) |
|
247 @end table |
|
248 |
|
249 You can also create test scripts for builtins and your own C++ |
|
250 functions. Just put a file of the function name on your path without |
|
251 any extension and it will be picked up by the test procedure. You |
|
252 can even embed tests directly in your C++ code: |
|
253 |
|
254 @example |
|
255 @group |
|
256 #if 0 |
|
257 %!test disp('this is a test') |
|
258 #endif |
|
259 @end group |
|
260 @end example |
|
261 |
|
262 or |
|
263 |
|
264 @example |
|
265 @group |
|
266 /* |
|
267 %!test disp('this is a test') |
|
268 */ |
|
269 @end group |
|
270 @end example |
|
271 |
|
272 but then the code will have to be on the load path and the user |
|
273 will have to remember to type test('name.cc'). Conversely, you |
|
274 can separate the tests from normal octave script files by putting |
|
275 them in plain files with no extension rather than in script files. |
|
276 @c DO I WANT TO INCLUDE THE EDITOR SPECIFIC STATEMENT BELOW??? |
|
277 @c Don't forget to tell emacs that the plain text file you are using |
|
278 @c is actually octave code, using something like: |
|
279 @c -*-octave-*- |
|
280 |
|
281 @DOCSTRING(assert) |
|
282 |
|
283 @DOCSTRING(fail) |
|
284 |
|
285 @node Demonstration Functions |
|
286 @section Demonstration Functions |
|
287 |
|
288 @DOCSTRING(demo) |
|
289 |
|
290 @DOCSTRING(example) |
|
291 |
|
292 @DOCSTRING(speed) |
|
293 |
|
294 |
|
295 @c Local Variables: *** |
|
296 @c Mode: texinfo *** |
|
297 @c End: *** |