annotate scripts/testfun/example.m @ 27923:bd51beb6205e

update formatting of copyright notices * Use <https://octave.org/copyright/> instead of <https://octave.org/COPYRIGHT.html/>. * For consistency with other comments in the Octave sources, use C++-style comments for copyright blocks in C and C++ files. * Use delimiters above and below copyright blocks that are appropriate for the language used in the file. * Eliminate extra spacing inside copyright blocks. * lex.ll (looks_like_copyright): Also allow newlines and carriage returns before the word "Copyright". * scripts/mk-doc.pl (gethelp): Also skip empty comment lines. * bp-table.cc, type.m: Adjust tests.
author John W. Eaton <jwe@octave.org>
date Wed, 08 Jan 2020 11:59:41 -0500
parents 1891570abac8
children 28de41192f3c 0a5b15007766
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## Copyright (C) 2000-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
7 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
8 ## This file is part of Octave.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
14 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6494
diff changeset
15 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
19 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
20 ## 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
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
25
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20162
diff changeset
27 ## @deftypefn {} {} example @var{name}
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20162
diff changeset
28 ## @deftypefnx {} {} example @var{name} @var{n}
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20162
diff changeset
29 ## @deftypefnx {} {} example ("@var{name}")
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20162
diff changeset
30 ## @deftypefnx {} {} example ("@var{name}", @var{n})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20162
diff changeset
31 ## @deftypefnx {} {[@var{s}, @var{idx}] =} example (@dots{})
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
32 ##
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
33 ## 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
34 ## @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
35 ##
2645f9ef8c88 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
36 ## 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
37 ##
12643
55430618bd5f Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents: 12505
diff changeset
38 ## 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
39 ## 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
40 ## various examples.
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
41 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
42 ## See @code{demo} for a complete explanation.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5589
diff changeset
43 ## @seealso{demo, test}
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
44 ## @end deftypefn
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
45
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
46 function [ex_code, ex_idx] = example (name, n = 0)
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
47
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
48 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
49 print_usage ();
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
50 endif
12505
6a1fe83fe129 Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
51
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
52 if (ischar (n))
12505
6a1fe83fe129 Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
53 n = str2double (n);
13141
e81ddf9cacd5 maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 12643
diff changeset
54 endif
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
55
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
56 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
57 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
58 endif
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
59
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6046
diff changeset
60 [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
61
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
62 if (idx == -1)
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
63 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
64 return;
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
65 elseif (isempty (idx))
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
66 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
67 return;
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
68 elseif (n >= length (idx))
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
69 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
70 length (idx) - 1, name);
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
71 return;
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
72 endif
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
73
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
74 if (nargout > 0)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
75 if (n > 0)
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6046
diff changeset
76 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
77 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
78 ex_idx = [1, length(ex_code)+1];
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
79 else
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
80 ex_code = "";
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
81 ex_idx = [];
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
82 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
83 else
19014
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
84 ex_code = code;
90a541a12c4b example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents: 17744
diff changeset
85 ex_idx = idx;
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
86 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
87 else
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
88 if (n > 0)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
89 doidx = n;
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
90 else
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
91 doidx = 1:length (idx) - 1;
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
92 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
93
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
94 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
95 block = code(idx(doidx(i)):idx(doidx(i)+1)-1);
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
96 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
97 endfor
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
98 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
99
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
100 endfunction
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
101
14039
e98140f84ae0 test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents: 13141
diff changeset
102
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
103 ## 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
104 %!demo
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14359
diff changeset
105 %! 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
106
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
107 %!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
108 %! clf;
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
109 %! 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
110 %! 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
111 %! plot (t,x)
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
112
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20852
diff changeset
113 %!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
114
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
115 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14359
diff changeset
116 %! [code, idx] = example ("example");
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11563
diff changeset
117 %! assert (code, ...
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
118 %! "\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
119 %! assert (idx, [1, 23, 73]);
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
120
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
121 ## Test input validation
19024
26a770330953 example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents: 19014
diff changeset
122 %!error example ()
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14359
diff changeset
123 %!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
124 %!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
125 %!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
126 %!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
127 %!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
128 %!warning <only 2 examples available for example> example ("example", 10);