annotate doc/interpreter/testfun.txi @ 6728:a25173d58101

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