Home > eidors > tools > unit_test_cmp.m

unit_test_cmp

PURPOSE ^

UNIT_TEST_CMP: compare matrices in eidors output

SYNOPSIS ^

function 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 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 5784 2018-05-20 23:59:04Z aadler $
0012 
0013 persistent ntotal;
0014 persistent npass;
0015 if strcmp(txt,'RESET_COUNTER'); ntotal=0; npass=0; return; end
0016 if strcmp(txt,'SHOW_COUNTER');
0017   if ntotal == 0;
0018      eidors_msg('%s: pass %d/%d',a, npass, ntotal,0); return;
0019   else
0020      eidors_msg('%s: pass %d/%d = %5.1f%%',a, npass, ntotal, 100*npass/ntotal,0);
0021   end
0022   return;
0023 end
0024 if strcmp(txt,'UNIT_TEST_FCN');
0025   unit_test_cmp('RESET_COUNTER');
0026   feval(a,'UNIT_TEST');
0027   unit_test_cmp( 'SHOW_COUNTER',a);
0028   return
0029 end
0030 
0031 
0032 if strcmp(txt,'UNIT_TEST'); do_unit_test; return; end
0033 
0034 
0035 if nargin < 4; tol = 0; end
0036 if tol<0;
0037    expect_fail = 1;
0038    if tol==-inf; tol= 0; end
0039 else
0040    expect_fail= 0;
0041 end
0042 tolstr='';
0043 
0044    fprintf('TEST: %20s = ',txt);
0045    ok='Fail';
0046    if (isnumeric(a) || islogical(a)) && ...
0047       (isnumeric(b) || islogical(b))
0048       sza = size(a); szb= size(b);
0049       eqsz= isequal( size(a), size(b));
0050       sza1 = all(sza==1); szb1 = all(szb==1);
0051       if ~eqsz && ~sza1 && ~szb1
0052          ok='Fail (size change)';
0053       else
0054          if isnan(a) == isnan(b);
0055              a(isnan(a))=0; b(isnan(b))=0;
0056          end;
0057          if all(abs(double(a) - double(b)) <= tol);
0058             ok='OK';
0059          end;
0060 
0061          if abs(tol)>0
0062             tolstr= sprintf('(%1.3f x tol)', full(max(abs(a(:)-b(:))/tol))); 
0063          end
0064       end
0065    else
0066       if strcmp(eidors_var_id(a), eidors_var_id(b)); ok='OK';end
0067    end
0068 
0069    if expect_fail
0070       ok = 'OK (fail as expected)';
0071    end
0072 
0073    fprintf('%4s %s\n', ok, tolstr);
0074    if strcmp(ok(1:2),'OK'); npass= npass+1; end
0075    ntotal= ntotal+1;
0076 
0077 function do_unit_test
0078    unit_test_cmp('Expect OK'  ,3,3);
0079 
0080    unit_test_cmp('Expect Fail',1,1.01, -inf);
0081    unit_test_cmp('Expect OK'  ,1,.99,.02);
0082    unit_test_cmp('Expect Fail',1,.99,-.002);
0083 
0084    a= rand(10); b = a;
0085    unit_test_cmp('Expect OK'  ,a,b);
0086    unit_test_cmp('Expect Fail',a,b+.001, -inf);
0087    unit_test_cmp('Expect OK  ',a,b+.001, .002);
0088 
0089    a(1,1) = NaN; b=a;
0090    unit_test_cmp('Expect OK'  ,a,b);
0091    unit_test_cmp('Expect Fail',a,b+.001, -inf);
0092    unit_test_cmp('Expect OK  ',a,b+.001, .002);
0093 
0094    unit_test_cmp('Expect Fail',a,'boo', -inf);
0095    unit_test_cmp('Expect OK','boo','boo');
0096 
0097    t.a= a; s.a=b;
0098    unit_test_cmp('Expect OK'  ,t,s);
0099    s2.b= b; 
0100    unit_test_cmp('Expect Fail'  ,t,s2, -inf);
0101 
0102    unit_test_cmp('Expect Fail', ones(3,3), ones(3,3,3), -inf);
0103    unit_test_cmp('Expect Fail', ones(3,1), ones(1,3), -inf);
0104    unit_test_cmp('Expect OK'  ,3,[3,3,3,3]);
0105    unit_test_cmp('Expect OK'  ,3,[3,3,3,3]);

Generated on Fri 01-Jun-2018 15:59:55 by m2html © 2005