]> Creatis software - CreaPhase.git/blob - octave_packages/m/testfun/example.m
update packages
[CreaPhase.git] / octave_packages / m / testfun / example.m
1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ##
3 ## This file is part of Octave.
4 ##
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.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Command} {} example @var{name}
21 ## @deftypefnx {Command} {} example @var{name} @var{n}
22 ## @deftypefnx {Function File} {} example ('@var{name}')
23 ## @deftypefnx {Function File} {} example ('@var{name}', @var{n})
24 ## @deftypefnx {Function File} {[@var{s}, @var{idx}] =} example (@dots{})
25 ##
26 ## Display the code for example @var{n} associated with the function
27 ## '@var{name}', but do not run it.  If @var{n} is not specified, all examples
28 ## are displayed.
29 ##
30 ## When called with output arguments, the examples are returned in the form of
31 ## a string @var{s}, with @var{idx} indicating the ending position of the
32 ## various examples.
33 ##
34 ## See @code{demo} for a complete explanation.
35 ## @seealso{demo, test}
36 ## @end deftypefn
37
38 function [code_r, idx_r] = example (name, n)
39
40   if (nargin < 1 || nargin > 2)
41     print_usage ();
42   endif
43
44   if (nargin < 2)
45     n = 0;
46   elseif (ischar (n))
47     n = str2double (n);
48   endif
49
50   [code, idx] = test (name, "grabdemo");
51   if (nargout > 0)
52     if (n > 0)
53       if (n <= length (idx))
54         code_r = code(idx(n):idx(n+1)-1);
55         idx_r = [1, length(code_r)+1];
56       else
57         code_r = "";
58         idx_r = [];
59       endif
60     else
61       code_r = code;
62       idx_r = idx;
63     endif
64   else
65     if (n > 0)
66       doidx = n;
67     else
68       doidx = 1:length(idx)-1;
69     endif
70     if (isempty (idx))
71       warning ("no example available for %s", name);
72       return;
73     elseif (n >= length(idx))
74       warning ("only %d examples available for %s", length(idx)-1, name);
75       return;
76     endif
77
78     for i = 1:length (doidx)
79       block = code(idx(doidx(i)):idx(doidx(i)+1)-1);
80       printf ("%s example %d:%s\n\n", name, doidx(i), block);
81     endfor
82   endif
83
84 endfunction
85
86
87 %!## warning: don't modify the demos without modifying the tests!
88 %!demo
89 %! example ('example');
90 %!demo
91 %! t=0:0.01:2*pi; x = sin(t);
92 %! plot (t,x)
93
94 %!assert (example('example',1), "\n example ('example');");
95 %!test
96 %! [code, idx] = example ('example');
97 %! assert (code, ...
98 %!         "\n example ('example');\n t=0:0.01:2*pi; x = sin(t);\n plot (t,x)")
99 %! assert (idx, [1, 23, 63]);
100
101 %% Test input validation
102 %!error example
103 %!error example ('example', 3, 5)
104