]> Creatis software - CreaPhase.git/blob - octave_packages/m/testfun/runtests.m
update packages
[CreaPhase.git] / octave_packages / m / testfun / runtests.m
1 ## Copyright (C) 2010-2012 John W. Eaton
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  {Function File} {} runtests ()
21 ## @deftypefnx {Function File} {} runtests (@var{directory})
22 ## Execute built-in tests for all function files in the specified directory.
23 ## If no directory is specified, operate on all directories in Octave's
24 ## search path for functions.
25 ## @seealso{rundemos, path}
26 ## @end deftypefn
27
28 ## Author: jwe
29
30 function runtests (directory)
31
32   if (nargin == 0)
33     dirs = strsplit (path (), pathsep ());
34   elseif (nargin == 1)
35     if (is_absolute_filename (directory))
36       dirs = {directory};
37     else
38       directory = regexprep (directory, ['\',filesep(),'$'], "");
39       fullname = find_dir_in_path (directory);
40       if (! isempty (fullname))
41         dirs = {fullname};
42       else
43         error ("runtests: DIRECTORY argument must be a valid pathname");
44       endif
45     endif
46   else
47     print_usage ();
48   endif
49
50   for i = 1:numel (dirs)
51     d = dirs{i};
52     run_all_tests (d);
53   endfor
54
55 endfunction
56
57 function run_all_tests (directory)
58   dirinfo = dir (directory);
59   flist = {dirinfo.name};
60   no_tests = {};
61   printf ("Processing files in %s:\n\n", directory);
62   fflush (stdout);
63   for i = 1:numel (flist)
64     f = flist{i};
65     if (length (f) > 2 && strcmp (f((end-1):end), ".m"))
66       ff = fullfile (directory, f);
67       if (has_tests (ff))
68         print_test_file_name (f);
69         [p, n, xf, sk] = test (ff, "quiet");
70         print_pass_fail (n, p);
71         fflush (stdout);
72       else
73         no_tests{end+1} = f;
74       endif
75     endif
76   endfor
77   if (! isempty (no_tests))
78     printf ("\nThe following files in %s have no tests:\n\n", directory);
79     printf ("%s", list_in_columns (no_tests));
80   endif
81 endfunction
82
83 function retval = has_tests (f)
84   fid = fopen (f);
85   if (fid >= 0)
86     str = fread (fid, "*char")';
87     fclose (fid);
88     retval = ! isempty (regexp (str, '^%!(test|assert|error|warning)', "lineanchors"));
89   else
90     error ("runtests: fopen failed: %s", f);
91   endif
92 endfunction
93
94 function print_pass_fail (n, p)
95   if (n > 0)
96     printf (" PASS %4d/%-4d", p, n);
97     nfail = n - p;
98     if (nfail > 0)
99       printf (" FAIL %d", nfail);
100     endif
101   endif
102   puts ("\n");
103 endfunction
104
105 function print_test_file_name (nm)
106   filler = repmat (".", 1, 55-length (nm));
107   printf ("  %s %s", nm, filler);
108 endfunction