unit_test_cmp

PURPOSE ^

UNIT_TEST_CMP: compare matrices in eidors output

SYNOPSIS ^

function res = unit_test_cmp(txt,a,b,tol)

DESCRIPTION ^

 UNIT_TEST_CMP: compare matrices in eidors output
 unit_test_cmp(txt,a,b,tol)
 if tol = -negative it is an expected fail
 if tol = -inf then expented fail with no tolerance
 if a==b print ok, otherwise print fail

 To run unit tests and keep statistics
   unit_test_cmp UNIT_TEST_FCN fun_name

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function res = unit_test_cmp(txt,a,b,tol)
0002 % UNIT_TEST_CMP: compare matrices in eidors output
0003 % unit_test_cmp(txt,a,b,tol)
0004 % if tol = -negative it is an expected fail
0005 % if tol = -inf then expented fail with no tolerance
0006 % if a==b print ok, otherwise print fail
0007 %
0008 % To run unit tests and keep statistics
0009 %   unit_test_cmp UNIT_TEST_FCN fun_name
0010 
0011 % License GPL v2 or v3: $Id: unit_test_cmp.m 6501 2022-12-30 14:22:04Z aadler $
0012 
0013 persistent ntotal;
0014 persistent test_start_time;
0015 persistent npass;
0016 if strcmp(txt,'RESET_COUNTER');
0017     ntotal=0; npass=0; test_start_time=cputime(); return;
0018 end
0019 if strcmp(txt,'SHOW_COUNTER');
0020   if ntotal == 0;
0021      eidors_msg('%s: pass %d/%d (t=%6.2fs)',a, ...
0022          npass, ntotal, cputime() - test_start_time, 0 );
0023   else
0024      eidors_msg('%s: pass %d/%d = %5.1f%% (t=%6.2fs)', a, ...
0025          npass, ntotal, 100*npass/ntotal, ...
0026          cputime() - test_start_time, 0 );
0027   end
0028   return;
0029 end
0030 if strcmp(txt,'UNIT_TEST_FCN');
0031   unit_test_cmp('RESET_COUNTER');
0032   feval(a,'UNIT_TEST');
0033   unit_test_cmp( 'SHOW_COUNTER',a);
0034   return
0035 end
0036 
0037 
0038 if strcmp(txt,'UNIT_TEST'); do_unit_test; return; end
0039 
0040 
0041 if nargin < 4; tol = 0; end
0042 if tol<0;
0043    expect_fail = 1;
0044    if tol==-inf; tol= 0; end
0045 else
0046    expect_fail= 0;
0047 end
0048 tolstr='';
0049 
0050    fprintf('TEST: %20s = ',txt);
0051    ok='Fail';
0052    if (isnumeric(a) || islogical(a)) && ...
0053       (isnumeric(b) || islogical(b))
0054       sza = size(a); szb= size(b);
0055       eqsz= isequal( size(a), size(b));
0056       sza1 = all(sza==1); szb1 = all(szb==1);
0057       if ~eqsz && ~sza1 && ~szb1
0058          ok='Fail (size change)';
0059       elseif any(isnan(a(:)) ~= isnan(b(:)))
0060              ok='Fail (NaNs change)';
0061       else
0062          rel_err = full(max(abs(double(a(:))-double(b(:)))));
0063          if rel_err <= tol;
0064             ok='OK';
0065          end
0066 
0067          if abs(rel_err)>0
0068             tolstr= sprintf('(%1.3f x tol)', rel_err/tol); 
0069          end
0070       end
0071    else
0072       if strcmp(eidors_var_id(a), eidors_var_id(b)); ok='OK';end
0073    end
0074 
0075    if expect_fail
0076       ok = 'OK (fail as expected)';
0077    end
0078 
0079    fprintf('%4s %s\n', ok, tolstr);
0080    if strcmp(ok(1:2),'OK');
0081       npass= npass+1;
0082    else
0083       if eidors_debug('query','unit_test_cmp:fail')
0084           keyboard % uncomment to debug
0085       end
0086    end
0087    ntotal= ntotal+1;
0088    
0089    if nargout>0
0090        res = strcmp(ok(1:2),'OK');
0091    end
0092 
0093 function do_unit_test
0094    unit_test_cmp('Expect OK'  ,3,3);
0095 
0096    unit_test_cmp('Expect Fail',1,1.01, -inf);
0097    unit_test_cmp('Expect OK'  ,1,.99,.02);
0098    unit_test_cmp('Expect Fail',1,.99,-.002);
0099 
0100    a= rand(10); b = a;
0101    unit_test_cmp('Expect OK'  ,a,b);
0102    unit_test_cmp('Expect Fail',a,b+.001, -inf);
0103    unit_test_cmp('Expect OK  ',a,b+.001, .002);
0104 
0105    a(1,1) = NaN; b=a;
0106    unit_test_cmp('Expect OK'  ,a,b);
0107    unit_test_cmp('Expect Fail',a,b+.001, -inf);
0108    unit_test_cmp('Expect OK  ',a,b+.001, .002);
0109 
0110    unit_test_cmp('Expect Fail',a,'boo', -inf);
0111    unit_test_cmp('Expect OK','boo','boo');
0112 
0113    t.a= a; s.a=b;
0114    unit_test_cmp('Expect OK'  ,t,s);
0115    s2.b= b; 
0116    unit_test_cmp('Expect Fail'  ,t,s2, -inf);
0117 
0118    unit_test_cmp('Expect Fail', ones(3,3), ones(3,3,3), -inf);
0119    unit_test_cmp('Expect Fail', ones(3,1), ones(1,3), -inf);
0120    unit_test_cmp('Expect OK'  ,3,[3,3,3,3]);
0121    unit_test_cmp('Expect OK'  ,3,[3,3,3,3]);

Generated on Fri 30-Dec-2022 19:44:54 by m2html © 2005