annotate scripts/testfun/example.m @ 20162:2645f9ef8c88 stable

doc: Update more docstrings to have one sentence summary as first line. Reviewed specfun, special-matrix, testfun, and time script directories. * scripts/specfun/expint.m, scripts/specfun/isprime.m, scripts/specfun/legendre.m, scripts/specfun/primes.m, scripts/specfun/reallog.m, scripts/specfun/realsqrt.m, scripts/special-matrix/gallery.m, scripts/special-matrix/hadamard.m, scripts/special-matrix/hankel.m, scripts/special-matrix/hilb.m, scripts/special-matrix/invhilb.m, scripts/special-matrix/magic.m, scripts/special-matrix/pascal.m, scripts/special-matrix/rosser.m, scripts/special-matrix/toeplitz.m, scripts/special-matrix/vander.m, scripts/special-matrix/wilkinson.m, scripts/testfun/assert.m, scripts/testfun/demo.m, scripts/testfun/example.m, scripts/testfun/fail.m, scripts/testfun/rundemos.m, scripts/testfun/runtests.m, scripts/testfun/speed.m, scripts/time/asctime.m, scripts/time/calendar.m, scripts/time/clock.m, scripts/time/ctime.m, scripts/time/datenum.m, scripts/time/datestr.m, scripts/time/datevec.m, scripts/time/etime.m, scripts/time/is_leap_year.m, scripts/time/now.m, scripts/time/weekday.m: Update more docstrings to have one sentence summary as first line.
author Rik <rik@octave.org>
date Sun, 03 May 2015 17:00:11 -0700
parents 9fc020886ae9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19024
diff changeset
1 ## Copyright (C) 2000-2015 Paul Kienzle
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
2 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
3 ## This file is part of Octave.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
4 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
6 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
8 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
9 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
13 ## General Public License for more details.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
14 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
18
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
20 ## @deftypefn {Command} {} example @var{name}
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
21 ## @deftypefnx {Command} {} example @var{name} @var{n}
14359
7277fe922e99 doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
22 ## @deftypefnx {Function File} {} example ("@var{name}")
7277fe922e99 doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
23 ## @deftypefnx {Function File} {} example ("@var{name}", @var{n})
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
24 ## @deftypefnx {Function File} {[@var{s}, @var{idx}] =} example (@dots{})
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
25 ##
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
26 ## Display the code for example @var{n} associated with the function
20162
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## @var{name}, but do not run it.
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
28 ##
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ## If @var{n} is not specified, all examples are displayed.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
30 ##
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
31 ## When called with output arguments, the examples are returned in the form of
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
32 ## a string @var{s}, with @var{idx} indicating the ending position of the
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
33 ## various examples.
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
34 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
35 ## See @code{demo} for a complete explanation.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5589
diff changeset
36 ## @seealso{demo, test}
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
37 ## @end deftypefn
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
38
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
39 function [ex_code, ex_idx] = example (name, n = 0)
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
40
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
41 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
42 print_usage ();
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
43 endif
12505
6a1fe83fe129 Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
44
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
45 if (ischar (n))
12505
6a1fe83fe129 Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
46 n = str2double (n);
13141
e81ddf9cacd5 maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 12643
diff changeset
47 endif
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
48
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
49 if (! (isreal (n) && isscalar (n) && n == fix (n)))
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
50 error ("example: N must be a scalar integer");
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
51 endif
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
52
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6046
diff changeset
53 [code, idx] = test (name, "grabdemo");
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
54
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
55 if (idx == -1)
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
56 warning ("example: no function %s found", name);
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
57 return;
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
58 elseif (isempty (idx))
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
59 warning ("example: no example available for %s", name);
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
60 return;
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
61 elseif (n >= length (idx))
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
62 warning ("example: only %d examples available for %s",
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
63 length (idx) - 1, name);
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
64 return;
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
65 endif
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
66
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
67 if (nargout > 0)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
68 if (n > 0)
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6046
diff changeset
69 if (n <= length (idx))
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
70 ex_code = code(idx(n):idx(n+1)-1);
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
71 ex_idx = [1, length(ex_code)+1];
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
72 else
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
73 ex_code = "";
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
74 ex_idx = [];
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
75 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
76 else
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
77 ex_code = code;
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
78 ex_idx = idx;
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
79 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
80 else
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
81 if (n > 0)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
82 doidx = n;
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
83 else
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
84 doidx = 1:length (idx) - 1;
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
85 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
86
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
87 for i = 1:length (doidx)
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
88 block = code(idx(doidx(i)):idx(doidx(i)+1)-1);
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6046
diff changeset
89 printf ("%s example %d:%s\n\n", name, doidx(i), block);
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
90 endfor
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
91 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
92
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
93 endfunction
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
94
14039
e98140f84ae0 test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents: 13141
diff changeset
95
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
96 ## WARNING: don't modify the demos without modifying the tests!
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
97 %!demo
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14359
diff changeset
98 %! example ("example");
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
99
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
100 %!demo
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
101 %! clf;
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
102 %! t = 0:0.01:2*pi;
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
103 %! x = sin (t);
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
104 %! plot (t,x)
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
105
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
106 %!assert (example ("example", 1), "\n example (\"example\");");
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
107
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
108 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14359
diff changeset
109 %! [code, idx] = example ("example");
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11563
diff changeset
110 %! assert (code, ...
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
111 %! "\n example (\"example\");\n clf;\n t = 0:0.01:2*pi;\n x = sin (t);\n plot (t,x)");
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14359
diff changeset
112 %! assert (idx, [1, 23, 73]);
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
113
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
114 ## Test input validation
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
115 %!error example ()
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14359
diff changeset
116 %!error example ("example", 3, 5)
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
117 %!error <N must be a scalar integer> example ("example", {1})
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
118 %!error <N must be a scalar integer> example ("example", ones (2,2))
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
119 %!error <N must be a scalar integer> example ("example", 1.5)
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
120 %!warning <no function .* found> example ("_%NOT_A_FUNCTION%_");
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
121 %!warning <only 2 examples available for example> example ("example", 10);
14039
e98140f84ae0 test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents: 13141
diff changeset
122