Mercurial > hg > octave-nkf
annotate scripts/testfun/example.m @ 12329:5f203b5bbf98
Use testif to only run some sparse tests when necessary libraries are installed.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 02 Feb 2011 21:31:33 -0800 |
parents | c792872f8942 |
children | 6a1fe83fe129 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Paul Kienzle |
5589 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5589 | 4 ## |
7016 | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
5589 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5589 | 18 |
19 ## -*- texinfo -*- | |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} example ('@var{name}', @var{n}) |
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{x}, @var{idx}] =} example ('@var{name}', @var{n}) |
5589 | 22 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
23 ## Display the code for example @var{n} associated with the function |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
24 ## '@var{name}', but do not run it. If @var{n} is not given, all examples |
5589 | 25 ## are displayed. |
26 ## | |
27 ## Called with output arguments, the examples are returned in the form of | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
28 ## a string @var{x}, with @var{idx} indicating the ending position of the |
5589 | 29 ## various examples. |
30 ## | |
31 ## See @code{demo} for a complete explanation. | |
5642 | 32 ## @seealso{demo, test} |
5589 | 33 ## @end deftypefn |
34 | |
6494 | 35 function [code_r, idx_r] = example (name, n) |
5589 | 36 |
37 if (nargin < 1 || nargin > 2) | |
6046 | 38 print_usage (); |
5589 | 39 endif |
40 if (nargin < 2) | |
41 n = 0; | |
42 endif | |
43 | |
6494 | 44 [code, idx] = test (name, "grabdemo"); |
5589 | 45 if (nargout > 0) |
46 if (n > 0) | |
6494 | 47 if (n <= length (idx)) |
10549 | 48 code_r = code(idx(n):idx(n+1)-1); |
49 idx_r = [1, length(code_r)+1]; | |
5589 | 50 else |
10549 | 51 code_r = ""; |
52 idx_r = []; | |
5589 | 53 endif |
54 else | |
55 code_r = code; | |
56 idx_r = idx; | |
57 endif | |
58 else | |
59 if (n > 0) | |
60 doidx = n; | |
61 else | |
6494 | 62 doidx = 1:length(idx)-1; |
5589 | 63 endif |
6494 | 64 if (length (idx) == 0) |
65 warning ("example not available for %s", name); | |
5589 | 66 elseif (n >= length(idx)) |
6494 | 67 warning ("only %d examples available for %s", length(idx)-1, name); |
5589 | 68 doidx = []; |
69 endif | |
70 | |
8507 | 71 for i = 1:length (doidx) |
72 block = code (idx(doidx(i)):idx(doidx(i)+1)-1); | |
6494 | 73 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589 | 74 endfor |
75 endif | |
76 | |
77 endfunction | |
78 | |
79 %!## warning: don't modify the demos without modifying the tests! | |
80 %!demo | |
81 %! example('example'); | |
82 %!demo | |
83 %! t=0:0.01:2*pi; x=sin(t); | |
84 %! plot(t,x) | |
85 | |
86 %!assert (example('example',1), "\n example('example');"); | |
87 %!test | |
88 %! [code, idx] = example('example'); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
89 %! assert (code, ... |
10549 | 90 %! "\n example('example');\n t=0:0.01:2*pi; x=sin(t);\n plot(t,x)") |
5589 | 91 %! assert (idx, [1, 22, 59]); |
92 | |
93 %!error example; | |
94 %!error example('example',3,5) |