Home > matlab > utilities > tests > mtest.m

mtest

PURPOSE ^

Extract test sections from matlab's routine executes the test and report errors.

SYNOPSIS ^

function check = mtest(fname,fpath)

DESCRIPTION ^

 Extract test sections from matlab's routine executes the test and report errors.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function check = mtest(fname,fpath)
0002 % Extract test sections from matlab's routine executes the test and report errors.
0003 
0004 % Copyright (C) 2011 Dynare Team
0005 % stephane DOT adjemian AT univ DASH lemans DOT fr
0006 %
0007 % This file is part of Dynare.
0008 %
0009 % Dynare is free software: you can redistribute it and/or modify
0010 % it under the terms of the GNU General Public License as published by
0011 % the Free Software Foundation, either version 3 of the License, or
0012 % (at your option) any later version.
0013 %
0014 % Dynare is distributed in the hope that it will be useful,
0015 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 % GNU General Public License for more details.
0018 %
0019 % You should have received a copy of the GNU General Public License
0020 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0021 
0022 % Default answer (no problem).
0023 check = 1;
0024 
0025 % Open the matlab file.
0026 fid = fopen([fpath '/' fname '.m'],'r');
0027 
0028 % Read the matlab file.
0029 file = textscan(fid,'%s','delimiter','\n');
0030 file = file{1};
0031 
0032 % Close the matlab file.
0033 fclose(fid);
0034 
0035 % Locate the test blocks.
0036 b1 = find(strncmp(file,'%@test:',7))+1;
0037 b2 = find(strncmp(file,'%@eof:',6))-1;
0038 nn = length(b2);
0039 
0040 if length(b1)-length(b2)
0041     error('test:: There is a problem with the test blocks definition!')
0042 end
0043 
0044 % Perform the tests.
0045 for i=1:nn
0046     % Write the temporary test routine.
0047     tid = fopen([fname '_test_' int2str(i) '.m'],'w');
0048     fprintf(tid,['function [T,t,LOG] = ' fname '_test_' int2str(i) '()\n']);
0049     fprintf(tid,['try\n']);
0050     for j=b1(i):b2(i)
0051         str = file{j};
0052         fprintf(tid,[str(4:end) '\n']);
0053     end
0054     fprintf(tid,['LOG = NaN;\n']);
0055     fprintf(tid,['catch exception\n']);
0056     fprintf(tid,['LOG = getReport(exception,''extended'');\n']);
0057     fprintf(tid,['T = NaN;\n']);
0058     fprintf(tid,['t = NaN;\n']);
0059     fprintf(tid,['end\n']);
0060     fclose(tid);
0061     % Call the temporary test routine.
0062     [TestFlag,TestDetails,LOG] = feval([fname '_test_' int2str(i)]);
0063     if isnan(TestFlag)
0064         fprintf(['\n'])
0065         fprintf(['Call to ' fname ' test routine n°' int2str(i) ' failed (' datestr(now) ')!\n'])
0066         fprintf(['\n'])
0067         disp(LOG)
0068         check = 0;
0069         continue
0070     end
0071     if ~TestFlag
0072         fprintf(['Test n°' int2str(i) ' for routine ' fname ' failed (' datestr(now) ')!\n']);
0073         for j=1:length(TestDetails)
0074             if ~TestDetails(j)
0075                 fprintf(['Output argument n°' int2str(j) ' didn''t give the expected result.\n']);
0076             end
0077         end
0078         check = 0;
0079     else
0080         delete([fname '_test_' int2str(i) '.m'])
0081     end
0082 end

Generated on Wed 23-May-2012 02:40:54 by m2html © 2005