File: mtest.m

package info (click to toggle)
dynare 4.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,312 kB
  • ctags: 15,840
  • sloc: cpp: 77,029; ansic: 29,056; pascal: 13,241; sh: 4,811; objc: 3,061; yacc: 3,013; makefile: 1,476; lex: 1,258; python: 162; lisp: 54; xml: 8
file content (137 lines) | stat: -rw-r--r-- 4,245 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
function [check, info] = mtest(fname, fpath)
% Extract test sections from matlab's routine executes the test and report errors.

% Copyright (C) 2011-2013 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

% Original author: stephane DOT adjemian AT univ DASH lemans DOT fr

% Default answer (no problem).
check = 1;

% Open the matlab file.
if nargin<2 || isempty(fpath)
    if nargout<2
        error('mtest:: Wrong calling sequence!')
    end
    % The full path to the matlab routine (with extension) is given.
    fid = fopen(fname,'r');
    [junk, FNAME, vessel] = fileparts(fname);
else
    fid = fopen([fpath '/' fname '.m'],'r');
    FNAME = fname;
end

% Read the matlab file.
file = textscan(fid,'%s','delimiter','\n');
file = file{1};

% Close the matlab file.
fclose(fid);

% Locate the test blocks.
b1 = find(strncmp(file,'%@test:',7))+1;
b2 = find(strncmp(file,'%@eof:',6))-1;
nn = length(b2);

if length(b1)-length(b2)
    error('test:: There is a problem with the test blocks definition!')
end

% Initialize the second output if necessary.
if nargout>1
    % First column   name of the tested routine.
    % Second column  number of the unitary test.
    % Third column   status of the unitary test (0 if the test fails, 1 otherwise)
    % Fourth column  details about the failure (vector of integers)
    % Fifth column   elapsed time in seconds (cpu time).
    info = cell(nn,5);
end

% Perform the tests.
for i=1:nn
    if nargout>1
        info(i,1) = {fname};
        info(i,2) = {i};
    end
    % Write the temporary test routine.
    tid = fopen([FNAME '_test_' int2str(i) '.m'],'w');
    fprintf(tid,['function [T,t,LOG] = ' FNAME '_test_' int2str(i) '()\n']);
    fprintf(tid,['try\n']);
    for j=b1(i):b2(i)
        str = sprintf('%s \n',file{j}(4:end));
        str = regexprep(str, '%', '%%');
        fprintf(tid,str);
    end
    fprintf(tid,['LOG = NaN;\n']);
    if isoctave
        fprintf(tid,'catch\n');
        fprintf(tid,'exception = lasterror;\n');
        fprintf(tid, 'LOG = ''%s'';\n','The Log output is not available with Octave!');
    else
        fprintf(tid,'catch exception\n');
        fprintf(tid,['LOG = getReport(exception,''extended'');\n']);
    end
    fprintf(tid,['T = NaN;\n']);
    fprintf(tid,['t = NaN;\n']);
    fprintf(tid,['end\n']);
    fclose(tid);
    % Call the temporary test routine.
    init = cputime;
    [TestFlag,TestDetails,LOG] = feval([FNAME '_test_' int2str(i)]);
    time = cputime-init;
    if isnan(TestFlag)
        fprintf(['\n'])
        fprintf(['Call to ' FNAME ' test routine n°' int2str(i) ' failed (' datestr(now) ')!\n'])
        fprintf(['\n'])
        if ~isoctave
            disp(LOG)
        end
        check = 0;
        if nargout>1
            info(i,3) = {0};
        end
        continue
    end
    if ~TestFlag
        if nargout>1
            info(i,3) = {0};
            tmp = ones(length(TestDetails),1);
        end
        fprintf(['Test n°' int2str(i) ' for routine ' FNAME ' failed (' datestr(now) ')!\n']);
        for j=1:length(TestDetails)
            if ~TestDetails(j)
                if nargout>1
                    tmp(j) = 0;
                end
                fprintf(['Output argument n°' int2str(j) ' didn''t give the expected result.\n']);
            end
        end
        if nargout>1
            info(i,4) = {tmp};
            info(i,5) = {NaN};
        end
        check = 0;
    else
        if nargout>1
            info(i,3) = {1};
            info(i,4) = {ones(length(TestDetails),1)};
            info(i,5) = {time};
        end
        delete([FNAME '_test_' int2str(i) '.m'])
    end
end